All Projects
Research PrototypeSolo Researcher & EngineerFlagship · Advanced

Waf-Transformer

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.

GitHub
Waf-Transformer

The Story

Why this exists.

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

What it does.

DistilBERT Threat Classification

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.

DistilBERT Threat Classification

Hybrid Rule + ML Pipeline

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.

Real-Time Request Dashboard

A React dashboard shows live request traffic, blocked requests with threat categories, confidence scores, and model decision explanations.

Attack Pattern Analysis

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

How every layer connects.

IngestionNode.js proxyHTTP request capture, forwarding, blocking
Rule EngineModSecurity-inspired rulesFast O(1) signature matching for known attacks
ML LayerPython + DistilBERTTransformer-based semantic threat classification
Model ServingFastAPIREST endpoint for ML inference, < 20ms p95
FrontendReact.jsLive dashboard — traffic, blocks, scores
StorageSQLite + JSON logsRequest log, attack pattern storage

Technical Deep Dive

Under the hood.

DistilBERT Fine-Tuning for WAF

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()

Hybrid Pipeline Latency Optimisation

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

Challenge → Decision → Result.

BERT vs. DistilBERT for inference latency

Decision

DistilBERT

Why

BERT inference at 45ms is too slow for a synchronous WAF middleware. DistilBERT at 18ms is within acceptable latency budget with only 3% accuracy trade-off.

Result

p95 inference latency 18ms, 94.2% classification accuracy.

When to invoke ML vs. rule-only decision

Decision

Escalate only parameterised or body-carrying requests

Why

73% of web traffic is static assets or parameter-free GETs — these are low attack surface. Focusing ML on high-risk request types preserves throughput.

Result

73% reduction in ML invocations, no meaningful coverage loss.

Performance & Scale

By the numbers.

94.2%Classification Accuracyon held-out test set
1.8%False Positive Ratebenign requests blocked
18msML Inference Latencyp95 on CPU
73%ML Invocation Reductionvs. all-requests ML

Deployment & Infrastructure

Production setup.

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 broke — and how I fixed it.

Challenge

High false positive rate on URL-encoded legitimate requests.

Fix

Added a URL decode + normalisation step before tokenisation — the model now sees the semantic content, not the encoding.

Challenge

DistilBERT tokeniser truncating long payloads beyond 512 tokens.

Fix

Chunked long request bodies and aggregated scores with max-pooling — the highest-risk chunk determines the final decision.

What I Learned

Key takeaways.

Future Roadmap

Where this is going.

v2.0

  • GPU inference for sub-5ms latency at production throughput
  • Online learning — model updates from new attack patterns in production
  • Multi-class classification — distinguish SQLi, XSS, RCE, path traversal specifically

Screenshots

The product.

WAF dashboard

Live request traffic with ML threat scores

Attack analysis

Attack pattern clustering and model confidence

Technology Stack

Built with.

ML / Backend

Python

ML Model

DistilBERT

API

FastAPI

Proxy

Node.js

Frontend

React.js

Infrastructure

Docker

Ready to dive in?

This isn't just a project.
It's a system I designed,
engineered, and shipped.

Explore the code← All Projects