Skip to content
Documentation

Detection engines

This page is published from the agent repository. Edit on GitHub View source

The agent judges every event on the endpoint, with no help from the network. Seven crates do the judging. Each one handles a different kind of evidence, and the agent's detection sink connects them:

CrateWhat it judgesRuns onPage
rulesHard-coded ATT&CK detections: one event at a time (stateless) or a bounded history (stateful, RuleState)capture threadrules.md
sigmaSigma process_creation rules shipped as contentcapture threadsigma.md
yaraYARA-X rules over written files and, on Linux, process memoryits own worker threadsyara.md
intelAtomic indicators (SHA-256, IP, domain)capture thread and enrichment workerioc.md
enrichNot a detector: SHA-256, code signature and PE version strings of executed imagesenrichment workerenrichment.md
mlONNX Isolation Forests over feature vectors, with attributioncapture thread (T2)ml.md
deceptionCanary files planted per install; any touch is a detectioncapture threaddeception.md

The correlator and verdict fusion consume what these engines produce. They are documented separately; this section stops at the point where a finding leaves an engine.

Data flow

Every sensor hands events to one EventSink, the agent's DetectionSink. DetectionSink::on_event runs synchronously on the thread that delivered the event (on Linux, the thread that drains the eBPF ring buffer). Only in-memory work may run there. Anything that reads files or runs a crypto check goes behind a bounded queue.

flowchart LR
    S[Sensor thread] -->|Event| OE[DetectionSink::on_event]
    OE --> COR[correlator + ML T2]
    OE --> CAN[deception::Tripwires]
    OE --> ST[rules::evaluate_*]
    OE --> RS[RuleState::on_*]
    OE --> SG[SigmaEngine::eval_exec]
    OE --> IOCN[IocMatcher: connect / DNS]
    OE -->|write-intent open| YQ[(yara::ScanQueue 512)]
    RS -->|T1620 memfd exec| MQ[(MemoryScanQueue 16)]
    OE -->|try_send| EQ[(EnrichQueue 4096)]
    EQ --> W[enrichment worker]
    W --> EN[enrich::Enricher]
    W --> GATE[SignatureGatedAlert::resolve]
    W --> IOCH[IocMatcher: exec hash]
    W --> RAR[common-set rarity]
    YQ --> YW[yara-scan worker]
    MQ --> MW[memory-scan worker]
    ST & RS & SG & CAN & IOCN & GATE & IOCH & YW & MW --> V[verdict fusion + alert log]

What on_event does, in order (see DetectionSink::on_event):

  1. Correlate. The event goes to the correlator. If an ML correlation scorer is loaded, it scores the pid's window and the score is added to the belief state (ml.md).
  2. Canary check. Held canary touches are expired against this event's clock, then the event is matched against the planted canaries (deception.md).
  3. Per-type dispatch. A match on the event variant calls the stateless dispatchers (rules::evaluate_exec, evaluate_connect, ...) and the matching RuleState::on_* method. For an Exec it also runs Sigma. For a write-intent FileOpen it queues a YARA scan. For a Connect, NetworkFlow or DnsQuery it runs the IOC check.
  4. Hand-off. The event goes to the enrichment worker with a non-blocking send. For an Exec, the signature-gated finding of RuleState::on_exec_signature_gated travels with it. When the queue is full, the gated finding is raised at once as "not verified" instead of being lost.
  5. Progress. A counter is bumped last. The watchdog's heartbeat samples it, so a hang anywhere above (a poisoned lock, a stuck engine) shows up as a stall.

Shared conventions

These hold across the engines. When a rule's comment points here, this is the section it means.

Findings

  • The rules produce rules::Alert: an ATT&CK technique, a Severity (Low, Medium, High, Critical) and a message. Severity ranks and fuses findings. It never gates a destructive action on its own.
  • Sigma produces SigmaAlert, YARA produces YaraMatch and the IOC matcher produces IocHit. The sink turns each one into a schema::detection::Detection with a DetectionSource that names the engine, and records it through DetectionSink::record. That writes the alert log (the audit trail: every finding, duplicates included) and folds the finding into verdict fusion.
  • Every finding is keyed to the entity (ppid, comm), plus the parent's incarnation when the sensor stamps one (entity_key). Engines that work off the capture thread (YARA, memory scans) carry this identity with the request (yara::ScanContext). A late match then joins the same entity, stamped with the triggering event's time.

Process incarnations

The kernel recycles pids, and the schema has no exit event. A cache keyed on the pid alone would give a new process the facts of the one that held the number before it. Sensors stamp each event with process_generation (the incarnation, set at fork) and parent_process_generation. Every per-pid table in the engines compares that stamp:

  • PidFact in rules misses when the recorded generation and the wanted one differ. None on either side means "cannot tell" and counts as a match. That covers entries seeded at startup and platforms with no stamp (macOS).
  • The memory-scan cooldown is keyed on (pid, generation) (memory.rs).
  • The ML extractors read events through EventBus::events_for_pid(pid, generation).
  • The canary cooldown and the hold for a late exec key on (canary, pid, generation).

Bounded memory, counted loss

The agent runs for months on hosts an attacker controls. Every table and queue in this section has a hard cap. Reaching the cap sheds work, and the shedding is counted, never silent:

StructureBoundOn overflow
RuleState per-pid tables65,536 entries (PID_COMM_CAP)LRU eviction of the oldest eighth
RuleState counters16,384 keys (COUNTER_CAP)same
Sliding counter timestamps256 per key (sliding.rs)oldest dropped
YARA file queue512 requests (queue.rs)dropped, ScanStats::dropped
YARA memory queue16 requests, 6 scans/min, 5 min per processdropped, counted by reason
Enrichment queue4,096 events (enrich_queue.rs)event not enriched, gated finding raised unverified
Enrichment cache16,384 files (lib.rs)LRU
IOC set64 MiB budget (set.rs)the whole set is refused
IOC cooldown4,096 indicators (ioc.rs)LRU
Canary hold256 touches (sink/mod.rs)judged at once, counted

store::BoundedMap is the LRU used throughout. It is a HashMap with a use counter. Past the cap it evicts the least recently used eighth in one batch, which keeps inserts amortized O(1). get refreshes recency and peek does not.

Content loading and reload

Sigma rules, YARA rules, IOC feeds and the common-set snapshot are content under <content_root>/. They are loaded at startup and again on reload_content (IPC ReloadContent). Each loader returns Load::Loaded, Load::Absent or Load::Failed:

OutcomeEffect on the running engine
Loadedswapped in
Absent (directory or file missing)engine unloaded, which is not an error
Failed (content present but broken)previous engine kept, error logged

DetectionSink::reload_content parses and compiles everything before it takes any lock, and holds each lock only for the swap. The capture thread takes the same locks for every exec and write-open, so loading under them would stall capture. The replaced engines are dropped after the locks are released, because dropping a ScanQueue joins its worker thread.

ML models are not reloaded. The correlation scorer is loaded once at construction (ml.md).

Platform matrix

EngineLinuxWindowsmacOS
rules (stateless + stateful)yesyesyes (fewer event sources)
Sigmalinux/windows/macos/ + linux/
YARA file scanyes, with per-user read checkyes, no uid checkyes, with per-user read check
YARA memory scanyesnono
IOC (hash, IP, domain)yesyesyes
Signature verdictalways UnsupportedAuthenticode + catalogcodesign (chain not verified)
PE version stringsnoyesno
ML T2 scorerwhen a model is presentwhen a model is presentwhen a model is present
Canary filesyesyes (default metadata)yes

Testing approach

  • Unit tests live next to each engine. The rules crate keeps one file per rule family under crates/rules/src/tests/, and a few rules also have proptest suites (rdp.rs, sudo.rs).
  • Sink-level tests in apps/agent/src/sink/tests.rs drive real events through DetectionSink::on_event and assert on alerts.ndjson. They cover the routing, redaction, canary hold and cooldown, YARA quarantine and reload.
  • Contract tests against the sibling public repos (rules, ml, server) read their half through the siblings crate. Without the sibling checkout they print skipped: and pass. With SYNTHAEA_REQUIRE_SIBLINGS=1 (set in CI) a missing sibling fails the test, so a contract test cannot pass by not running.