Skip to content

Detection Testing and Fixtures

github-actions[bot] edited this page Nov 29, 2026 · 1 revision

Last reviewed: 2026-11-28 (worked example: canary lab CI run 46219831976, 20 of 20 fixtures matched)

Prerequisites: Detection Engineering index ยท Rule lifecycle ยท Wazuh + Suricata canary lab.

๐ŸŽฏ Purpose

How to build fixtures that prove a rule both fires and stays silent, how to replay them through the real engine, and how to record the result so another person can check it. The Wazuh + Suricata canary lab is the worked example throughout.

โš™๏ธ Function

Defines four fixture kinds, the PCAP and log replay methods the lab uses, the expectation and evidence manifest formats, and how the tests are wired into CI.

๐Ÿ† Goal

Each rule has at least a positive and a near-miss negative fixture, run by the pinned engine in CI, with an evidence manifest that records the engine version and the hash of every input.

๐Ÿ“‹ When To Use

  • Adding fixtures for a new or changed rule.
  • Adding a new engine or lab to CI.
  • Reading a lab's evidence and deciding what it supports.

๐Ÿงฉ Fixture Kinds

Kind Design rule Lab example
Positive The smallest input that should match, with every field the rule checks suricata-http-positive: full TCP handshake, GET /lab-canary-7f3a, Host example.test โ†’ sid 1007001
Negative (near miss) One small change from the positive, so a loose anchor or a missing field check is caught suricata-http-negative uses /lab-canary-7f3b; suricata-dns-negative uses canary-7f3a.example.test.invalid, which fails endswith
Malformed Input or rule the engine should reject or not match, so error handling is visible Bad TCP checksum replayed with -k all; a truncated EVE line; a Sigma condition that names a missing identifier; a YARA rule that references an undefined pattern (CompileError E025)
Duplicate The same activity twice, to prove thresholding, frequency or match counting Two canary flows โ†’ one Suricata alert (threshold: type limit, track by_src, count 1); the same EVE line twice in one logtest session โ†’ 100601 then 100603; two markers โ†’ one YARA-X rule match with $marker matched twice

Use only synthetic, benign content: RFC 5737 addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24) and names under example.test or .invalid.

A negative fixture for a child rule should still reach the parent. In the lab, wazuh-negative-other-sid carries sid 1007099, and the expected result is built-in rule 86601 at level 3 with no lab rule. That proves the lab rule's own condition rejected it, not that the event never reached Wazuh.

๐Ÿ“ฆ PCAP Replay (Suricata)

  • Generate, don't capture. The lab's tools/gen_pcaps.py is a pure-stdlib, deterministic writer. python3 tools/gen_pcaps.py writes the PCAPs and python3 tools/gen_pcaps.py --check compares the files on disk with what it would generate. The repository .gitignore excludes *.pcap, so the SHA-256 of each generated file is pinned in manifest.json instead.
  • Build complete flows. Stream midstream defaults to false in Suricata 8.0.8, so include SYN, SYN-ACK and ACK. The lab's HTTP flows also carry the response and a FIN/ACK teardown.
  • Load only the rule under test. The lab replays with -r <pcap> -c /etc/suricata/suricata.yaml -S <rules> -l <dir> -k all --runmode single. -S loads the file exclusively, ignoring the rule files in the YAML, and -k all forces checksum checks so the malformed fixture means something.
  • Read alerts from EVE. Compare alert.signature_id values in eve.json with the expected list. An empty list is an expectation too.

Offline replay guidance for real captures stays in Suricata + Zeek.

๐Ÿ“œ Synthetic Log Replay (Wazuh)

  • Use the logtest socket or CLI, not a live agent. The lab sends framed JSON requests to /var/ossec/queue/sockets/logtest inside a wazuh/wazuh-manager:4.14.11 container that publishes no ports. Each message has a 4-byte little-endian length header, as in upstream wazuh_logtest.py.
  • One line per event. Each EVE fixture line must be one JSON object; the Wazuh json decoder prematch is ^{\s*".
  • Sessions carry state. Events sent with the same session token share frequency state. The lab's duplicate fixture depends on this: the second identical event in one session fired 100603 (frequency="2"). Start a new session for independent events.
  • Check the type you compare. Logtest returned output.rule.id as a JSON string (observed), so compare "100601", not 100601.
  • Chain engines. chain-suricata-http-to-wazuh feeds the real eve.json alert line that Suricata wrote for the positive PCAP into logtest. This tests the field names Suricata actually emits, not hand-written ones.

The manual CLI procedure for the Kismet, nzyme and honeypot rules is in Wazuh synthetic rule validation.

๐Ÿ“ Expectation Manifest

The expectation manifest is written before the run and never edited to match a result. The lab's manifest.json records:

  • engines: the version, package or image digest, and the exact replay flags;
  • rule_files and lab_rule_ids;
  • fixtures: one object per case with id, engine, kind, input, description and expect;
  • sha256: the pinned hash of every rule and fixture file, including the generated PCAPs.

expect is engine specific:

Engine expect keys used
Suricata returncode, alert_sids
Wazuh events: a list of rule_id, level, alert, decoder, or rule_id_not_in for the malformed case
Sigma returncode, plus stdout (exact golden query) or output_contains
YARA-X matching_rules and pattern_match_counts, or compile_error and error_code

๐Ÿงพ Evidence Manifest

run_lab.py writes the evidence manifest with --evidence <path> and refuses to overwrite an existing file. The committed evidence/ci-run-46219831976.json has these top-level keys:

Key Content
evidence_origin ci-observed only when GITHUB_RUN_ID was set; local runs are local-observed
scope_statement What the evidence does not cover
git_commit, ci_run_url, generated_at Where and when the run happened
runner, engines_selected, engines Python and platform; engine versions, suricata -T result, Wazuh image ID and digest, wazuh-control info, rule-file hash match
inputs One entry per input file: path, observed sha256, expected_sha256 and match
results Per fixture: expected, observed, status (pass, fail, error or not-run) and problems
summary Counts, input_hashes_match, complete and overall

--require-complete makes the run fail if any fixture was not run, so a missing engine cannot produce a partial pass.

๐Ÿ”Œ CI Wiring

Piece Where Notes
Offline contract tests tools/tests/test_detection_lab.py Runs in the normal tools/tests discovery. It also fails if the lab README's observed rows disagree with the committed evidence
Engine run .github/workflows/detection-lab.yml runs-on: ubuntu-24.04 (ubuntu-latest now resolves to 26.04), pinned action SHAs, permissions: contents: read, path filters on the lab directory
Wazuh rule contract tools/tests/test_wazuh_rule_contracts.py Add new rule XML to FILES

Put new unit tests in tools/tests/. Tests placed only under IncidentResponse/DetectionEngineering/ are not discovered by guide-tests.yml or make tests. CI does not parse .jsonl files, so fixture JSONL needs its own test.

โœ… What The Lab Proves

  • Suricata 8.0.8, with only the lab rules loaded, alerts on the two positive PCAPs and stays silent on the near-miss and bad-checksum PCAPs. The threshold limit setting turned two canary flows into one alert.
  • Wazuh 4.14.11 logtest maps the hand-written HTTP and DNS EVE fixture lines to rules 100601 and 100602 at level 10, and the real eve.json alert line Suricata wrote for the HTTP positive PCAP to 100601. The DNS alert is not chained from Suricata output. It fires 100603 on a repeat within one session and does not attribute a truncated line to any Suricata rule.
  • sigma-cli 4.0.1 accepts the valid rule, rejects the malformed one, refuses to convert for Splunk without a pipeline, and produces the exact golden query with the sysmon and splunk_windows pipelines.
  • YARA-X 1.25.0 matches the canary text, ignores the near miss, counts duplicate pattern matches and rejects the malformed rule.
  • The rule file loaded in the container matched the committed file, and every input matched its pinned hash.

โ›” What The Lab Does Not Prove

  • Sensor placement, packet capture on a live network, or packet loss handling.
  • Wazuh agents, <localfile> collection, Filebeat, the indexer, the dashboard or the REST API.
  • That the converted Splunk query runs or matches anything; no SIEM executes it.
  • Behavior on real traffic or real endpoints, or false-positive rates.
  • Every malformed-input case; there is one per engine.
  • Anything about Zeek, which the lab does not run.

See also

Related Files


โฌ…๏ธ Detection Engineering index | Master Index | ๐ŸŽฏ Role Navigation | Legal Notice


๐Ÿ“„ Source: IncidentResponse/DetectionEngineering/testing-and-fixtures.md | โœ๏ธ Suggest an edit | โฌ…๏ธ Back to Incident Response

Clone this wiki locally