Technical explainer · Machine learning

How the CNN-GRU model decides

DAV NGWAF reads each request as a sequence of characters. A convolutional network finds suspicious fragments, a recurrent network judges what those fragments mean together, and the result is a single score. A policy layer turns that score into an action.

Two kinds of neural network work in stages:

The output is a probability that the request is malicious. The model never bans on its own: DAV NGWAF's policy layer combines the score with rules, exemptions and behavior over time before anything is enforced.

1. Turning a request into numbers

A neural network only works with numbers, so each request (mainly the path and query string) is converted first:

  1. Tokenize by character. Attacks hide in unusual characters and encodings (%27, ../, <, ;), so character-level input holds up better against obfuscation than word-level input.
  2. Map to integer IDs. Each character gets an ID from a fixed vocabulary.
  3. Pad or truncate to a fixed length, so every request has the same shape.
  4. Embedding layer. Each ID becomes a small learned vector. During training, characters that behave alike end up close together, for example ' and %27.

The result is a matrix of sequence length × embedding size, for example 200 × 32.

2. CNN: the local pattern detector

CNNs are best known for images. For text, a one-dimensional convolution slides along the character sequence.

In effect the CNN is a learned, fuzzy fragment matcher. It still responds to variations that an exact rule would miss: mixed case, partial encoding, inserted characters. Every window is computed independently, so it is fast.

What a CNN does not do well is understand order. It can tell that UNION and FROM are present, but not that they appear in sequence, inside a parameter value, right after a quote. That is the GRU's job.

3. GRU: the context and order reader

A GRU is a recurrent neural network. It reads a sequence one step at a time and carries a hidden state forward: a running memory of what it has seen so far.

Simple recurrent networks forget over long sequences. A GRU fixes this with two learned gates that output values between 0 and 1. At each step t, with input xt and previous state ht−1:

Update gate:  z_t = σ(W_z·x_t + U_z·h_(t−1) + b_z)
Reset gate:   r_t = σ(W_r·x_t + U_r·h_(t−1) + b_r)
Candidate:    h̃_t = tanh(W_h·x_t + U_h·(r_t ⊙ h_(t−1)) + b_h)
New state:    h_t = (1 − z_t) ⊙ h_(t−1) + z_t ⊙ h̃_t

σ = sigmoid, ⊙ = element-wise multiplication

Why a GRU and not an LSTM: an LSTM uses three gates and a separate cell state. A GRU uses two gates and one state, so it has roughly a quarter fewer parameters, runs faster, and performs comparably on short sequences such as URLs. That suits per-request inference on CPU.

4. How they work together

Request URL (string)
   │
   ▼
Character IDs → Embedding            200 × 32
   │
   ▼
Conv1D filters + ReLU                200 × 128   which fragments appear where
   │
   ▼
Max pooling                          50 × 128    shorter, position-tolerant
   │
   ▼
GRU                                  reads the 50 feature steps in order
   │
   ▼
Final hidden state                   one vector summarizing the request
   │
   ▼
Dense layer → Sigmoid                P(malicious), from 0 to 1

Dimensions are illustrative and do not describe the production model's exact layers.

The two networks cover each other's weaknesses. The CNN compresses raw characters into a shorter sequence of pattern features, so the GRU reads a few dozen meaningful steps instead of hundreds of raw characters. The GRU adds the order and context the CNN lacks.

Worked example

Attack

/index.php?id=1%27%20UNION%20SELECT%20password%20FROM%20users--

CNN: filters respond to %27 (encoded quote), UNION, SELECT, FROM and -- (SQL comment).

GRU: reads them in order: a parameter value, then a quote that breaks out of it, then UNION SELECT, then FROM, then a comment that ends the query. That sequence is the injection signature, and the hidden state builds evidence step by step.

Score: high (e.g. 0.98)

Benign request with similar words

/blog/union-select-committee-report

CNN: some of the same filters may respond weakly to union and select.

GRU: the words are in the path, separated by hyphens, with no quote, no parameter break-out, no FROM and no comment. The context does not match an attack.

Score: low (e.g. 0.07)

A keyword rule would flag both requests. The model can flag suspicious URL patterns that match no blocklist entry, and avoid flagging benign URLs that happen to contain a keyword.

Scores are illustrative. Real values depend on training data.

5. From score to WAF decision

The model produces a probability. DAV NGWAF's policy layer decides what happens with it:

SignalRole in the decision
ML scoreCompared against thresholds, for example malicious vs suspicious. Thresholds trade detection rate against false positives.
Deterministic rulesMITRE ATT&CK pattern matches, sensitive-file patterns, known bad user agents, and requests the web server already rejected.
ExemptionsAllowlisted paths, allowlisted IPs and protected addresses are checked first and are never banned, whatever the score.
Behavior over timeRepeated hits from one source, and correlation across addresses in the same subnet.
ModeMonitor-only logs the decision. Enforcement blocks subsequent traffic from the source IP at the firewall, can clear tracked flows with conntrack, and can use XDP to drop packets before they reach the web server.

Allowlists, paths and ban patterns live in operator-editable files that the engine picks up on reload, with no rebuild required.

6. Why it runs on CPU

Once trained, inference is a fixed series of operations: embedding lookup, convolution, GRU gate arithmetic, a dense layer and a sigmoid. A compact CNN-GRU model has a modest number of weights, so scoring one URL takes microseconds to low milliseconds on a modern CPU.

In DAV NGWAF the trained model is compiled into the Rust engine. No Python runtime, no separate inference service, and no model files are shipped. CPU is the default. GPU acceleration is an option for very large deployments. GPUs matter mainly for training, where the model processes large labeled datasets many times over.

Why not a transformer?

Transformer models such as BERT are the standard for natural-language tasks, but they are a poor fit for scoring every request on a web server:

Compact CNN-GRUTransformer (BERT-base)
SizeA small fraction of BERT's weightsAbout 110 million parameters, 12 layers
Cost vs input lengthGrows linearlySelf-attention grows with the square of the length
Per-request CPU timeMicroseconds to low millisecondsTypically tens of milliseconds or more without a GPU
MemorySmall, compiled into the engineHundreds of megabytes per loaded model
HardwareCPU by defaultUsually needs a GPU at high request rates

URLs are short and structured. What matters is local fragments and their order, which is exactly what convolution and recurrence capture. A transformer's extra capacity helps with long, ambiguous human language, not with spotting ../ followed by /etc/passwd. A CNN-GRU gives the detection the job needs at a cost that lets every request be scored on ordinary CPUs.

Training in brief

7. In practice

Most traffic a WAF actually sees is not a tailored attack. It is automated, low-cost reconnaissance that sweeps the internet looking for anyone who left a secret in a web root. A typical burst, all from one source within one second:

reason=blocklist_regex   credential files   .aws/credentials, .git-credentials, .npmrc, .pgpass, SSH keys
reason=blocklist_regex   deployment config  docker-compose.yml, CI files, appsettings.json
reason=bad_user_agent    scanner            nmap

That is the threat most small and mid-size sites face: high volume and low effort. Deterministic rules catch the known shapes, the CNN-GRU model catches variations that match no rule, and behavioral correlation catches persistent sources whatever their payloads look like.

Summary

The CNN learns which fragments look dangerous. The GRU learns whether those fragments, in that order and context, form an attack. The sigmoid output is a confidence score, and DAV NGWAF's policy layer turns that score, together with rules, exemptions and behavior, into a decision: log, ban or ignore.