A firewall that thinks before it blocks.
Waf-Transformer proves that transformer-based NLP models can be practical security infrastructure — combining the speed of rule-based WAFs with the contextual intelligence of DistilBERT to detect attacks that pattern matching alone will never catch.
The Story
The Problem
Traditional Web Application Firewalls rely on regex patterns and known attack signatures. They block yesterday's attacks well. But novel payloads — obfuscated SQL injections, polymorphic XSS, zero-day command injections — slip through because they don't match any known pattern. The attacker just needs one variation that isn't in the ruleset.
Why It Matters
OWASP's top 10 vulnerabilities have been the same for over a decade. Injection attacks alone account for the majority of web application breaches. The industry has known about the problem for years — but signature-based defences keep losing to creative attackers. ML-powered WAFs are not the future; they're the necessary present.
The Solution
A hybrid WAF prototype that layers a DistilBERT transformer model on top of a traditional rule engine. Requests that pass the rule layer are scored by the transformer for semantic attack likelihood. The system learns from request context, not just patterns — detecting obfuscated and novel payloads that regex can't catch.
Product Features
Each incoming HTTP request is tokenised and scored by a fine-tuned DistilBERT model trained on a labelled dataset of benign and malicious web requests. Confidence scores above the threshold trigger a block.
Known attack signatures are caught by a fast rule layer (O(1) lookup). Novel or ambiguous requests escalate to the ML layer — balancing throughput with detection accuracy.
A React dashboard shows live request traffic, blocked requests with threat categories, confidence scores, and model decision explanations.
Historical attack logs are clustered by semantic similarity — surfacing attack campaign patterns and novel payload families that weren't in the original training data.
System Architecture
Technical Deep Dive
DistilBERT was chosen over BERT for its 60% size reduction with only 3% accuracy loss — critical for latency-sensitive security middleware. The model was fine-tuned on a dataset of 80,000 labelled HTTP requests (SQL injection, XSS, CSRF, path traversal, benign). Training used a binary classification head on the [CLS] token representation. The final model achieves 94.2% accuracy on the test set with a false positive rate of 1.8%.
from transformers import DistilBertForSequenceClassification, Trainer
model = DistilBertForSequenceClassification.from_pretrained(
"distilbert-base-uncased",
num_labels=2 # benign / malicious
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_ds,
eval_dataset=eval_ds,
compute_metrics=compute_metrics,
)
trainer.train()The rule layer processes requests in under 0.5ms. The ML layer adds 18ms on average (DistilBERT inference on CPU). To minimise impact: only requests that pass the rule layer AND have a query parameter or request body escalate to ML. Static asset requests, GET requests with no parameters, and known-safe IPs bypass ML entirely. This reduces ML invocations by 73% while maintaining coverage of all high-risk request types.
Engineering Decisions
Performance & Scale
Deployment & Infrastructure
Deployment
Node.js proxy and React frontend on local/development environment. FastAPI ML service containerised with Docker. Prototype — not production-deployed.
CI/CD
GitHub Actions — Python tests on PR, React lint check.
Monitoring
Custom request log dashboard. Confusion matrix and accuracy tracked on every model retrain.
Challenges & Failures
What I Learned
Transformer models are practical for security inference when you optimise invocation scope — don't run ML on every request.
URL normalisation before tokenisation is not optional — encoded payloads defeat semantic models without it.
False positive rate matters as much as accuracy — a WAF that blocks 1.8% of legitimate traffic is a business problem.
Future Roadmap
v2.0
Screenshots
Live request traffic with ML threat scores
Attack pattern clustering and model confidence
Technology Stack
ML / Backend
ML Model
API
Proxy
Frontend
Infrastructure
Ready to dive in?