Detection as code for incident responders
Turn what you learn in one investigation into rules that run on the next. A practical approach to small, testable, versioned detection rules for DFIR.
Detection as code usually means SIEM rules: written by a detection engineering team, versioned in Git, tested in CI, deployed to production. Incident responders rarely work that way. A finding in one case becomes a line in a report, maybe a note in a wiki, and then it is gone.
It does not have to be. The same discipline works at the scale of a forensic case, and it pays back on the next one.
Why responders lose what they learn
Every investigation produces a set of specific patterns. For example:
- this user agent belongs to the scanner;
- this command line is the reverse shell;
- this service name imitates a system service;
- this account should never log in from the web server.
In the moment they are queries in someone's history. Unless they are written down in a form that runs, they will not be in the next case, and the next client with the same attacker starts from zero.
What makes a good case-level rule
A detection rule for DFIR is not a SIEM alert. It runs over collected evidence, after the fact, and its job is to label events so the story becomes visible. A good one has five properties.
1. One behaviour per rule
"Reverse shell via bash" is one rule. "Suspicious activity on web01" is not a rule. Small rules are easier to read, to trust and to reuse.
2. A query a person can read
If a reviewer cannot read the query, they cannot trust its matches. Prefer a readable pipeline:
filter process_name == "bash" && process_command_line ~= ">& /dev/tcp/"over a 40-line regular expression.
3. A label that explains the match
Every match should carry:
- a verdict: malicious or suspicious;
- a summary a reader understands without opening the rule, such as "Reverse shell to an external host";
- the ATT&CK technique, such as T1059.004 (Unix shell);
- the kill-chain stage, such as execution.
The summary does the most work. When matches land on a timeline, a column of good summaries reads as the story of the intrusion.
4. Validated before it runs
The commonest failure in detection content is a rule that silently matches nothing, because of a typo in a field name or a field that does not exist for that event type. Rules should be checked against the schema when they are saved, not discovered broken mid-case.
5. Scoped, then promoted
Some rules belong to one case, such as "SSH from 10.20.1.10 as deploy". Others generalise, such as "web server account spawning a shell". Keep the first kind with the case. Promote the second kind to a shared library you run at the start of every case.
A worked example
In our Halden Freight demo case, we wrote 26 rules after working the intrusion. Two of them, for example:
[meta]
id = "hf-lateral-db01"
name = "SSH from web01 to db01 as deploy"
[query]
type = "aql"
event_type = ["syslog"]
body = "filter message ~= \"Accepted publickey for deploy from 10[.]20[.]1[.]10\""
[action]
verdict = "malicious"
summary = "Lateral movement with deploy's stolen key"
mitre_techniques = ["T1078"]
ukc_stages = ["lateral-movement"][meta]
id = "hf-rclone-exfil"
name = "rclone upload to MEGA"
[query]
type = "sql"
event_type = ["linux_auditd"]
body = "select * from scope where regexp_matches(array_to_string(process_args, ' '), 'rclone|mega')"
[action]
verdict = "malicious"
summary = "Stolen data uploaded to cloud storage"
mitre_techniques = ["T1567"]
ukc_stages = ["exfiltration"]The first is case-specific. The second, with the address-specific parts removed, belongs in every Linux case.
Run together, the 26 rules turned 62,295 events into a timeline with one lane per kill-chain stage, running from reconnaissance to exfiltration.
Building the habit
- Write the rule when you confirm the finding, not at the end of the case.
- Review rules like code. A second person reads the query and the summary.
- Keep them in version control. Plain-text rules diff cleanly.
- Run the shared library first on every new case, and note which rules fired. That list is a quick first view of the case.
Where AI helps
Writing rules is the step people skip under time pressure, so it is a good job for an AI investigator. Ask it to write a rule for something it found. It drafts the rule and shows the full text for review before saving. You still read the query. But the rule gets written while the case is open, instead of never.
In Arcsentry, rules are called arrows and libraries are called quivers. Arrows are validated against the declared event types when saved. They can be promoted from a case to the global quiver, and the AI investigator can draft one on request, behind an approval card.
How we wrote this
Drafted with AI from evidence in our own lab, with Laura Pompeu responsible for the article. The evidence comes from Halden Freight portal breach (Linux), built and investigated in our lab, so every example can be checked against real records.
See it on your own evidence. Free for 7 days.
Tell us what you want to investigate. A person replies within one business day with the download and a trial licence.
Request a trial