# Suricata DPI and Rule Management (Part 3)

- URL: https://wuming.si/blog/4026/
- Author: Wuming
- Published: 2025-02-04
- Language: en
- Categories: Technology, English

> Previous: [10G Packet Capture](https://wuming.si/blog/4025) · Next: [Protocol Analysis with Zeek](https://wuming.si/blog/4027)

In [Part 2](https://wuming.si/blog/4025) we got packets onto isolated CPU cores, with every connection kept on one worker. Now something has to read them. In this platform that is **Suricata**, an open-source IDS/IPS (intrusion detection / prevention system) engine. It sits right above the capture layer and produces almost everything the rest of the system consumes: alerts, protocol metadata and extracted files.

A DPI engine is only as good as its rules, so this post has two halves. The first is the engine: how packets become streams, streams become protocol transactions, and transactions get matched. The second is rule management: where large rule sets come from, how each rule is classified and mapped to a kill-chain stage, and how updates reach the sensors without wiping out local tuning.

## Part one: the engine

### The processing pipeline

```
 per-core queue (from the capture layer)
        │
 ┌──────▼──────┐  Ethernet / VLAN / MPLS / IP / TCP / UDP headers,
 │ Decode      │  IP fragment reassembly
 └──────┬──────┘
 ┌──────▼──────┐  5-tuple flow table, per-protocol timeouts,
 │ Flow        │  memory cap with emergency cleanup
 └──────┬──────┘
 ┌──────▼──────┐  TCP stream reassembly in both directions,
 │ Stream      │  out-of-order handling, depth limit per flow
 └──────┬──────┘
 ┌──────▼──────┐  protocol detection by content, not port;
 │ App-layer   │  HTTP, DNS, TLS, SMB, SMTP, FTP, SSH, ...
 └──┬───┬───┬──┘
    │   │   │
    ▼   ▼   ▼
 detect  files  logs
    │   │   │
 ┌──▼───▼───▼──┐  alerts, protocol records, file info
 │ Outputs     │  → JSON file, ZeroMQ stream, file store
 └─────────────┘
```

The engine runs in **workers** mode: each thread takes packets from one queue and runs the whole pipeline on them, start to finish. No hand-off between threads, no locks on the hot path, and because the balancer keeps flows together, each thread owns its flows outright.

### Why application-layer parsing matters

A naive IDS searches raw packet bytes. That fails as soon as a URL is split across two TCP segments, compressed with gzip, or sent on a non-standard port. Suricata reassembles streams and then parses protocols, so rules can target **buffers** such as "the HTTP URI", "the DNS query name" or "the TLS SNI" (Server Name Indication, the hostname a client asks for in the TLS handshake) after normalization. This makes rules both more precise and cheaper: the engine only searches the buffer a rule cares about.

Protocol detection is content-based. HTTP on port 8081 is still recognized as HTTP. That one feature closes a lot of easy evasion.

### Fast matching at scale

With tens of thousands of active rules, checking each rule against each packet is impossible. Suricata uses a **multi-pattern matcher** as a prefilter. Every rule nominates one "fast pattern", a literal string that must appear for the rule to match. All fast patterns go into one automaton (Hyperscan on modern Intel CPUs), which scans each buffer once and returns the short list of rules worth evaluating in full. Rules without a good fast pattern are the ones that hurt performance, and they show up clearly in rule profiling.

### Three kinds of output

**1. Alerts.** A rule fired. The alert record carries the signature ID, message, classification, severity, flow identifiers, and optionally the payload and the HTTP request that triggered it.

**2. Protocol metadata.** Even with no alert, the engine logs one record per HTTP transaction, DNS query and answer, TLS handshake (with SNI, certificate subject and issuer, and a **JA3** fingerprint, which is a hash of the client's TLS hello parameters that identifies client software), SMTP session, SSH banner, and a flow record per connection. This is what the threat-intel matcher, the ML detectors and the analysts actually live on. It is also 10 to 100 times more data than alerts.

**3. Files.** The engine can reassemble files carried over HTTP, SMTP, FTP and SMB, compute MD5/SHA-1/SHA-256, identify the real type by magic bytes (not by file name), and write selected types to disk for the sandbox and AV scanners.

An illustrative output section:

```yaml
# illustrative only
outputs:
  - eve-log:
      filetype: regular            # durable copy on disk
      filename: eve.json
      types: [alert, http, dns, tls, files, smtp, ssh, flow]
  - eve-log:
      filetype: zmq                # live stream to the event pipeline
      address: "<pipeline endpoint>"
      types: [alert, http, dns, tls, files]
  - file-store:
      enabled: yes
      force-hash: [sha256]
```

Writing to both a local file and a live stream is deliberate. The stream feeds the real-time pipeline (Part 5). The file is the fallback when the pipeline is down and the source for offline replay.

### Picking which files to keep

Extracting every image and stylesheet would flood the disk and the sandbox. A small policy decides what is worth keeping, based on the **detected** type:

| Keep | Usually skip |
|---|---|
| Executables and libraries (PE, ELF, DEX/APK, JAR) | Images, fonts, CSS |
| Office documents and PDFs | Media streams |
| Archives (ZIP, RAR, 7z, gzip) | Very small files |
| Scripts delivered as downloads | Files above a size cap |

A fast magic-byte check (for example `MZ` for Windows executables, `\x7fELF` for Linux binaries, `%PDF-` for PDFs) runs before anything heavier. Deduplicating by hash before handing off saves a lot of sandbox time: the same installer downloaded by 300 employees only needs to be analyzed once.

### IDS or IPS

In inline (IPS) mode the engine can drop or reject packets. In passive mode, the common case for a mirror-port sensor, it only alerts. Most deployments here are passive, so "blocking" is done by pushing an indicator to a firewall rather than by the engine itself.

### Engine tuning knobs that matter

| Knob | Effect | Trade-off |
|---|---|---|
| Flow memory cap and pre-allocation | How many concurrent flows fit | Too low → emergency mode, flows evicted early |
| Stream reassembly depth | How far into each flow content is inspected | Deeper → more CPU and RAM, sees more of large transfers |
| Disabled noisy rules | Fewer pointless alerts | Every disabled rule is a blind spot |
| BPF filter at capture | Skip known-irrelevant traffic | Invisible to every detector |
| CPU affinity sets | Separate management, receive and worker threads | Needs a careful core plan (Part 2) |

A reassembly depth of around 1 MB per flow is a common middle ground. Most attacks show themselves in the first part of a connection, while bulk transfers past that point are mostly payload that file extraction handles separately.

## Part two: rule management

### Where rules come from

| Source | What it covers |
|---|---|
| Open community rule sets (for example Emerging Threats Open) | Malware, exploits, scans, web attacks, policy |
| In-house rules | Gaps the community sets miss, local applications |
| Indicator-derived rules | Generated from intel feeds: bad IPs, domains, certificates |
| Protocol-anomaly rules | RFC violations, malformed packets |
| Attack-result rules | Look at the **response** to decide if an attack worked |
| File-type rules | Magic-byte patterns for fast type identification |

Attack-result rules are a small idea with a big payoff. A SQL injection attempt is common background noise on the internet. A SQL injection attempt **followed by a database error message in the response** is something an analyst should look at today. An illustrative pair:

```
# illustrative only
alert http any any -> $HOME_NET any (msg:"Web SQLi attempt in URI";
  flow:to_server,established; http.uri; content:"union select"; nocase;
  classtype:web-application-attack; sid:1000101; rev:1;)

alert http $HOME_NET any -> any any (msg:"Possible successful SQLi: DB error in response";
  flow:to_client,established; http.response_body; content:"SQL syntax"; nocase;
  classtype:successful-admin; sid:1000102; rev:1;)
```

The correlation layer can then upgrade the first alert when the second one appears on the same flow.

### A rule is more than a rule

A raw Suricata rule says "if these bytes, then alert with this message". An analyst needs much more: what kind of threat is it, how bad, who is the attacker and who is the victim in this alert, what should I do about it, and where does it sit in an attack. So every rule lives in a **knowledge base** with metadata around it. A generic version of the model:

```
classification  (broad class: web attack, trojan activity, recon, DoS, policy, ...)
   └── category (finer: SQL injection, C2 beacon, port scan, ...)
          severity, attacker/victim role, description,
          remediation advice, references

rule
   signature text, signature id, revision
   category → classification
   kill-chain stage
   ATT&CK technique (optional)
   severity, reliability (how often it is right)
   enabled, deprecated
   locally_modified flag
```

**Reliability** is separate from **severity** on purpose. A rule can describe something very serious (remote code execution) and still be noisy. Correlation weighs both.

The **kill-chain stage** is what lets the correlation engine order events into a story. The model follows the well-known seven stages:

| Stage | Example rule types |
|---|---|
| Reconnaissance | Port and service scans, DNS enumeration |
| Weaponization | Exploit-kit landing pages, dropper patterns |
| Delivery | Malicious attachments, drive-by downloads, phishing |
| Exploitation | Buffer overflows, injection, remote code execution |
| Installation | Malware download, persistence |
| Command and control | Beaconing, known C2 protocols |
| Actions on objectives | Lateral movement, privilege escalation, exfiltration |

### Two databases: master and operational

The design uses two copies of the rule data:

```
 ┌───────────────────────────────┐
 │ Knowledge base (master)       │  curated centrally, shipped with updates
 │ taxonomy · kill chain · rules │
 └──────────────┬────────────────┘
                │ scheduled sync (e.g. nightly, and after each update)
 ┌──────────────▼────────────────┐
 │ Operational DB (per system)   │  what the console edits
 │ + local enable/disable        │
 │ + locally modified rules      │
 └──────────────┬────────────────┘
                │ render enabled rules → rule files
 ┌──────────────▼────────────────┐
 │ Suricata (live reload)        │  and the correlation engine, the console
 └───────────────────────────────┘
```

The sync job follows a fixed order, and the order is the whole point:

1. Refresh the reference tables first (kill-chain stages, classifications, categories), because rules point at them.
2. Delete rules that **are not** locally modified.
3. Insert rules from the master that are not present locally, but only if their stage, classification and category exist (referential integrity, so a bad update cannot create orphans).
4. Remove rules that disappeared from the master (retired upstream).
5. Re-render rule files from the enabled set and tell Suricata to reload.

In pseudo-SQL:

```sql
-- illustrative only
DELETE FROM op.rule WHERE locally_modified = 0;

INSERT INTO op.rule
SELECT m.*, 0 AS locally_modified
FROM   master.rule m
WHERE  m.category_id IN (SELECT id FROM op.category)
  AND  m.stage_id    IN (SELECT id FROM op.kill_chain_stage)
  AND  m.sid NOT IN (SELECT sid FROM op.rule);

DELETE FROM op.rule WHERE sid NOT IN (SELECT sid FROM master.rule);
```

The `locally_modified` flag is the critical detail. Without it, every nightly sync would quietly undo the analyst who spent a week tuning a rule for their network.

### Turning rules off without deleting them

Some rules are correct but useless in a given environment: "a curl user agent was seen", internal stream-engine events, informational categories. These go in a **disable list** that matches by signature ID or by message pattern, for example "everything in the informational category". Disabling (instead of deleting) keeps the rule in the knowledge base, so it can be turned back on for an investigation, and it survives the next sync.

### Reloading without dropping packets

Restarting a DPI engine on a 10G link means losing traffic for the seconds it takes to start up. Suricata supports a **live rule reload**: it builds the new detection engine in the background and swaps it in. The update flow is: sync, render, test-load the new files in a separate process to catch syntax errors, then trigger the live reload. A bad rule must never take the sensor down.

## Pitfalls and lessons

- **Rule count is not coverage.** Large rule sets look impressive, but a few hundred noisy rules can generate most of the alerts. Track alerts per rule, review the top talkers regularly, and tune or disable.
- **Profile rule cost.** One rule with a weak fast pattern or a heavy regex can cost more CPU than a thousand good ones. Suricata's rule profiling shows which ones.
- **Keep identifiers stable.** Correlation, dashboards and whitelists all key off signature IDs. Never reuse an ID for a different meaning; bump the revision instead.
- **Put local rules in their own ID range** so they never collide with upstream sets.
- **Metadata is the product.** In practice the protocol logs (DNS, HTTP, TLS, files) produce more detections downstream than the signatures do. Size storage and the pipeline for them.
- **Test before reload.** A validation step before every reload is cheap insurance.

## What's next

Suricata is precise but rule-driven. In the next post we look at the other engine reading the same packets, Zeek, which turns traffic into rich protocol events and lets us express detection as scripts, and how custom output plugins feed those events into the rest of the platform.

## The Series

1. [The Big Picture](https://wuming.si/blog/4024)
2. [10G Packet Capture](https://wuming.si/blog/4025)
3. [Suricata DPI and Rule Management](https://wuming.si/blog/4026)
4. [Protocol Analysis with Zeek](https://wuming.si/blog/4027)
5. [An Event Pipeline with Flume and Esper](https://wuming.si/blog/4028)
6. [Threat Intel Matching and the Knowledge Base](https://wuming.si/blog/4029)
7. [Rule-Based Anomaly Detection](https://wuming.si/blog/4030)
8. [Machine Learning Detection](https://wuming.si/blog/4031)
9. [Alert Correlation, Noise Reduction and Aggregation](https://wuming.si/blog/4032)
10. [Dynamic Sandbox](https://wuming.si/blog/4033)
11. [Multi-Engine AV Scanning](https://wuming.si/blog/4034)
12. [The Management Console](https://wuming.si/blog/4035)
13. [A Lightweight Sensor](https://wuming.si/blog/4036)
14. [Deployment, Versioning, Upgrade and Rollback](https://wuming.si/blog/4037)
15. [Ops Monitoring and Self-Healing](https://wuming.si/blog/4038)

---

*This series is a personal study of system architecture. Details are abstracted and anonymized; it does not describe any specific commercial product or any organization's internal configuration or data.*
