Retrieved article excerpt
Open article Β· Retrieved 2026-11-14T00:21:27.029631+00:00
# Detection-as-Code Foundry
### An adversarial multi-agent detection-engineering workflow β LangGraph Β· Pydantic Β· FastMCP Β· Claude
Turn a threat (a CVE, a CISA KEV entry, an incident writeup, an ATT&CK technique)
into a **validated first-version detection** β a proven starting point a detection
team hardens further, not a claim of finished, forever-bulletproof coverage β plus
**three artifacts that ship together**:
1. a **Sigma detection rule** that has survived a Blue-vs-Red adversarial loop,
2. a **threat report** with the round-by-round **convergence history**, and
3. a **proof bundle** β a positive corpus the rule fires on, a benign baseline it
stays silent on, an adversarial mutation set, and a **computed coverage label**
(`real-coverage` / `brittle` / `broken`) with FP/FN numbers and a reproducibility hash.
The thing nobody else produces is #3. A model can write you a Sigma rule; it cannot
hand you a *proof* that the rule works, an adversary that *provably* evades the weak
version, and an honest record of how the rule was hardened until it held.
> **A bounded, fixed adversary.** Red commits to **two real, documented attacks** in
> round one and re-throws *those same two* against the hardened rule each round β the
> same "built once, can't move its goalposts" discipline as the fixed test corpus.
> `real-coverage` therefore means something concrete and honest: the first-version rule
> fires on every attack in the corpus, stays silent on benign look-alikes, and caught
> **both** of Red's real attacks. It is a validated baseline, not a promise of coverage
> against every future evasion β that is the detection team's job to extend.
> **Agents propose; the engine proves.** Four role-specialized LLM agents (Threat
> Analyst, Blue Detection Engineer, Telemetry, Red-Team Adversary) do the creative
> work. Everything that decides truth β FP/FN evaluation, adversarial evasion,
> the coverage verdict, red-team verification, and the deployment gate β is a
> deterministic referee. Same input β same verdict β same reproducibility hash.
---
**Two pipelines share this agent cast.** The one described above **proves** β Blue
authors, Red attacks, a deterministic referee decides. The
[**Review Chain**](https://github.com/mjha-arya/DOC-foundry#the-review-chain--a-second-different-pipeline---chain-and-the-default-ui)
**reviews** β draft β Red Team β revision β SOC Lead β final rule, with every note
that shaped it. Different question, different deliverable; pick per what you need.
π **See real output first:**
[`examples/joomla-icagenda-cve-2026-48939/review-history.md`](https://github.com/mjha-arya/DOC-foundry/blob/main/examples/joomla-icagenda-cve-2026-48939/review-history.md)
β a live run on a CISA KEV entry, three rule versions, eight reviewer findings.
---
## Run it
```
pip install -r requirements.txt # or just run --evals on stdlib alone
# LIVE β any threat text; a real model authors it (needs GEMINI_API_KEY or ANTHROPIC_API_KEY)
python src/run.py "CVE-2026-45659 SharePoint deserialization RCE"
python src/run.py "T1218.011 rundll32 remote scriptlet execution via mshtml"
# CANNED β a fixture scenario, fully offline, no key (what the test suite drives)
python src/run.py --threat fixtures/certutil_t1105.json # brittleβrepairβproven
python src/run.py --corpus atomic # positives from REAL Atomic Red Team commands
python src/run.py --max-rounds 3 # self-repair loop cap (default 3)
python src/run.py --evals # deterministic engine + proof + workflow tests
python src/run.py --approve alice # human approves β gated deploy succeeds
python src/run.py --claude "..." # force claude-opus-4-8 authoring (ANTHROPIC_API_KEY)
python src/run.py --gemini "..." # force Gemini authoring (GEMINI_API_KEY)
```
There is **no default scenario** β the Foundry takes the threat you give it. A bare
`python src/run.py` has nothing to work on and says so.
Bundles are written to `Outputs/<rule-slug>/` (`rule.yml`, `report.md`, `proof.json`).
## The Review Chain β a second, different pipeline (`--chain`, and the default UI)
The pipeline above asks *"is this rule provably correct?"* and answers with a
deterministic verdict. The **Review Chain** asks a different question β *"what would
each stakeholder say about this rule, and what does it look like after their feedback
is applied?"* β and answers with a **final rule plus the complete trail that shaped it**.
```
ingest ββΆ extract ββΆ telemetry ββΆ profile
ββΆ Blue: DRAFT v1
ββΆ RED TEAM review (findings: where is this evadable / blind)
ββΆ Blue: REVISION v2 (each edit cites the finding id it answers)
ββΆ SOC LEAD review (findings: volume, triage, severity, tuning)
ββΆ Blue: FINAL v3
ββΆ package
```
```
python src/run.py --chain "CVE-2026-48939 Joomla iCagenda arbitrary file upload RCE"
python -m uvicorn webui.server:app --port 8000 # β http://127.0.0.1:8000 (chain UI)
# β /classic for the proof loop above
```
Red and SOC are not the same reviewer in two hats β they pull in **opposite**
directions. Hardening against evasion broadens a rule; broadening raises alert volume.
Running both, in that order, is the point: the final rule is the one that survived both
pressures, and the trail shows exactly what each pass cost.
Output lands in `Outputs/<rule-slug>/` as `final-rule.yml`, `review-history.md`
(the full progression, rendered) and `review.json`.
### A worked example β read the actual output
**β [`examples/joomla-icagenda-cve-2026-48939/`](https://github.com/mjha-arya/DOC-foundry/blob/main/examples/joomla-icagenda-cve-2026-48939)**
β a real run on CVE-2026-48939 (Joomla iCagenda upload-to-RCE, CISA KEV), authored by
`claude-opus-4-8`. Three rule versions, eight findings, the whole trail:
[`review-history.md`](https://github.com/mjha-arya/DOC-foundry/blob/main/examples/joomla-icagenda-cve-2026-48939/review-history.md).
Blue's first draft fired on a web daemon spawning a shell **that runs recon commands**.
Both reviewers took it apart β in opposite directions:
> **Red Team:** *"No β I'd just skip the recon commands (or use a container's busybox
> shell) and this rule never fires despite a full RCE."*
>
> `R1` mandatory `recon_intent` turns a lineage detection into a keyword detection Β·
> `R2` on Alpine `/bin/sh` is busybox, so every shell clause misses Β·
> `R3` literal `contains` dies to shell obfuscation Β·
> `R4` `setsid`/double-fork re-parents to PID 1 and lineage fails
> **SOC Lead:** *"No β this fires on your own benign cron example on day one; the parent
> scoping is broken and it'll bury the rota in www-data noise."*
>
> `S1` the `OR` made web-server parentage optional β any www-data process spawning a
> shell fired Β·
> `S2` php-fpmβphp is normal web-app behaviour, not RCE Β·
> `S3` the alert carried one field, so the analyst pivots consoles Β·
> `S4` `level: high` on that noise floor burns out the on-call
Red pushed Blue to **broaden**; the SOC Lead pushed it to **narrow** straight back.
That tension is the product: hardening against evasion raises alert volume, and the
final rule is the one that survived both pressures. SOC reviews last, so what ships is
tuned for the queue β v3 made parent lineage mandatory, excluded the known-good
interpreter chains, expanded the triage fields, dropped to `level: medium`, and carries
a three-step response playbook with a containment threshold.
> **Reviewed, not validated β and the artifact says so.** This chain does not invoke
> the proof core. It never executes the rule against telemetry, counts no false
> positives, and issues no coverage verdict. A rule that has been through four
> professional reviewers *looks* finished, which is precisely why the packaged output
> leads with that sentence. Run the classic pipeline (or `--evals`) when you need proof
> rather than review.
## The demo UI (`webui/`) β live authoring, watch the agents work
```
python -m uvicorn webui.server:app --port 8000
# β http://127.0.0.1:8000 ; type ANY threat, watch the loop run
python tools/warm_cache.py "the exact threat you'll demo" # optional β see below
```
FastAPI + SSE over the **real** graph. Two streams are interleaved and neither is
scripted: **node events** fire when a graph node genuinely completes, and **token
events** stream each agent's raw model output *as it is generated*, via the token sink
on `providers.py`. The graph runs on a worker thread precisely because tokens arrive
mid-node; the queue between them is the only coupling, and it is one-way.
You watch: the LangGraph shape animate (the node in flight glows, the edge into it
flows, the **loop-back arc fires red on a repair round**), the agent console type out
what the model is producing right now, the Sigma rule write itself line by line, and
the convergence timeline, Kartikeya gate and reproducibility hash land at the end.
**It authors LIVE β there are no presets and no fixture/replay path in the UI**, so any
threat works. Determinism comes from `providers.py`'s on-disk cache: temperature 0,
keyed by `sha256(model|system|user)`, so re-running the *same threat text* replays
byte-identical model output β same rule β same bundle hash. Cold-run latency depends
entirely on the model (a 3-round convergence on `gemini-flash-lite` lands in ~20-40s;
a bigger model is minutes). `warm_cache.py` pre-pays it for a threat you plan to demo.
> **Replay is labeled, always.** A cached token stream is marked `cached` end-to-end and
> the console badge reads **β³ REPLAY (cached)** instead of **β GENERATING**. A viewer
> cannot tell replay from generation by looking, so the UI says which it is. Dressing
> cached bytes up as live motion would be exactly the "vibes" this project refuses.
> The sink is observation only: detach it and the verdict and hash are byte-identical.
> **Red-team refusals are handled honestly.** Safety-tuned models sometimes decline the
> adversary role. A refusal is recorded, surfaced in the UI, and β critically β
> `graph.node_verdict` refuses to award `real-coverage` to a round nobody attacked.
> "No evasions found" and "the adversary never ran" must never look alike.
## Architecture β Blue vs. Red, a deterministic referee, and a self-repair loop
```
ingest ββΆ extract ββΆ telemetry (Analyst normalizes; Telemetry fixes the test set)
β
βΌ
βββββββββββββββββ SELF-REPAIR LOOP (LangGraph conditional edges) ββββββββββββββββββ
β β
β blue_author ββΆ evaluate ββΆ battery ββΆ red_propose ββΆ red_verify ββΆ verdict β
β β² (FP/FN) (mutation (Red-Team (engine- (coverage) β
β β β battery) proposes) verifies) β β
β β βββ un-evaluable rule ββββββββββββββββββββββββββββββ€ β
β β (engine refuses β broken, skip the round) β β
β ββββββββββββββββββ repair: verdict β real-coverage ββββββββββββββ β
β β β
βββββββββββββββββββββββββββββ done: real-coverage OR round == cap βββββββ β
βΌ
gate ββΆ bundle
[ blue/red/telemetry/analyst = LLM agents β PROPOSE ]
[ evaluateΒ·batteryΒ·red_verifyΒ·verdictΒ·gate = deterministic referee β PROVE ]
```
- **Agents (`src/agents.py`)** β four distinct personas. The **Blue Detection
Engineer** authors a robust-by-construction first version (behavior + command-line
intent, not one brittle string) and re-authors on a brittle/broken verdict using the
*concrete* evidence (the exact evasions + false positives). The **Red-Team Adv