Skip to content
All articles

Detection as code: a practical introduction for SOC teams

Detection as code keeps rules in version control. The practice rests on review, a sample each rule must match and clean logs it must stay quiet on.

8 min read

An overhead grid of identical clear glass test tubes in small white racks on grey linen, with one tube in the middle row holding red liquid
AI-generated editorial illustration.

Detection as code is the practice of managing detection rules the way developers manage code: in a shared format, under version control, tested before deployment and changed through review. For a SOC team, that means a repository of rules, a review for every change, tests that run on every commit, a deliberate deployment and a quick way back.

Key takeaways

  • A Git repository gives detection rules a history; the tests that run on every change decide what reaches production.
  • Test each rule in both directions: against a sample event it must match and against logs from clean systems where it must stay quiet.
  • Deploy from the repository on purpose, and keep rollback as a revert.

What is detection as code?

Detection as code applies software practice to the rules that raise alerts. Detection engineering is the work of designing, testing and maintaining the rules that turn logs into alerts; detection as code is how a team keeps that work reviewable and repeatable as the rule set grows.

The practice answers the questions a SOC team asks when a detection misbehaves: who changed this rule and when, why did it stop firing, and was it tested before it shipped? Without those answers, a broken rule fails quietly, because a rule that matches nothing raises no alert about itself. The authors of the Alerting and Detection Strategies (ADS) framework built their framework after "sub-optimal alerts going to production without documentation or peer-review."

Sigma detection rules are YAML files that describe suspicious activity in log data in an open, vendor-neutral format, so tools can convert one rule into your SIEM's query language. Our OpenSSH post applies it to one vulnerability.

What goes into a detection as code repository?

A detection as code repository holds everything needed to rebuild and retest a detection, the rule included. NIST's Secure Software Development Framework (SSDF), in task PS.1.1, asks teams to "Store all forms of code" with access limited to those who need it, and gives as an example: "Use version control features of the repository to track all changes made to the code with accountability to the individual account." Applied to detections, a repository holds:

  • The rules, in Sigma or your platform's query language, one file per rule with a stable id.
  • Test data for each rule: a sample event the rule must match.
  • Field mappings or processing pipelines for each target SIEM.
  • A short write-up per detection: what it is for, its blind spots, its known false positives and how it was validated.
  • The continuous integration (CI) configuration that runs the checks on every change.

The Sigma specification already supports this. A rule can carry a random UUID as its id, a modified date for its last change, and related ids for rules it was derived from or replaces. For major changes, such as different rule logic, the specification advises a new id.

Why isn't putting rules in Git enough?

A repository records changes; tests and review judge them. The short version of detection as code, "keep your rules in Git", leaves out the part that catches mistakes.

SigmaHQ, the project behind the Sigma format and its public rule collection, shows what the full version looks like. Its repository validates its rule folders against the Sigma schema on each push and pull request. Its regression tests "verify that Sigma rules actually match the events they are supposed to detect": each rule at status test or stable points to a real event sample and the number of matches expected from it, and CI "fails if the expected number of matches is not produced." A separate workflow runs its Windows rules over baseline event logs from clean Windows client and server systems, where any match is a likely false positive.

That two-sided test matches what NIST SP 800-61r3 asks of continuous monitoring (DE.CM): "Tune the continuous monitoring technologies to reduce false positives and false negatives to acceptable levels." A rule that never fires on clean logs may also never fire on the attack. Only the positive sample tells you which.

A review needs a checklist. The ADS template asks for the goal, blind spots and assumptions, false positives and validation, in sections that "must be completed prior to production implementation".

Diagram: a detection as code pipeline from pull request to review, schema check, positive test on a sample event, clean-baseline test, deployment and rollback by revert.
A detection as code pipeline with both tests. Sources: SigmaHQ repository workflows, NIST SP 800-61r3, ADS framework.

How do you set up detection as code, step by step?

Start with one rule set and one target, then add checks as the team trusts them:

  1. Put the rules in a repository and protect the main branch, so every change arrives as a pull request. The SSDF gives the matching example: "Have the code owner review and approve all changes made to the code by others."
  2. Review each pull request against a short template: goal, blind spots, false positives and how the rule was validated.
  3. Validate syntax in CI on every push. For Sigma rules, the sigma check command flags issues a reviewer can miss, such as a modifier combination that produces a wrong query.
  4. Add a positive test: a sample event per rule and the number of matches you expect.
  5. Add a negative test: run the rules over logs from known-clean systems and fail on new matches you have not listed as known false positives.
  6. Deploy from the repository after the checks pass, and keep rollback as a revert. In Git, a revert records a new commit that undoes the change, so the history stays complete.

How does detection as code work in Imperum, step by step?

In Imperum, two modules support the practice: Detection Lake, Imperum's threat detection module, for the rules it runs itself, and Virtus Optimus, the Imperum module for detection as code, for the rules in your connected security tools.

Step 1: Keep rules in Git and test them in Detection Lake

Detection Lake takes rules from built-in Sigma rules, uploaded YAML files or a synced Git repository. Keep rules in Git when you need a versioned source, and test each rule against compatible log data before deploying it.

Automatic deployment is off on a fresh installation, and deployment settings can change that behavior. Importing a rule alone does not establish detection coverage: the data must contain the fields the rule expects.

Illustrative Detection Lake rules synced from a Git repository, with a Test Query result for one rule and the Deploy action.
Illustrative mockup of Detection Lake rules synced from Git, with synthetic data.

Step 2: Review proposed fixes as diffs in Virtus Optimus

Virtus Optimus maintains the detection rules across your connected security tools, with reviewable changes your team can test and control. A fix is a proposal with a risk badge, a summary and a diff, and Optimus shows it only after its simulation passed.

An authorized operator can approve, then apply the change. Approval is the default path; Autopilot and Library auto-deploy are separate opt-ins per technology. A passed simulation supports review and does not prove the rule will detect.

Illustrative Virtus Optimus fix for a noisy rule: a threshold change shown as a diff with its risk badge, simulation result and approval buttons.
Illustrative mockup of a Virtus Optimus fix review, with synthetic data.

Step 3: Verify after apply, and roll back when needed

Each rule keeps a version history. For a fix raised from an issue, Optimus re-runs the check that raised it and marks the fix verified or still failing. Rollback reverts an applied fix, and what it can undo depends on the target and the change type.

Illustrative Virtus Optimus rule history: four versions of a rule with one rolled-back change, the latest fix marked verified, and the rollback action for it.
Illustrative mockup of a Virtus Optimus rule history, with synthetic data.

What do you need before you start in Imperum?

In Detection Lake, test and deploy actions require the appropriate permissions. Optimus needs its own license, a supported technology connected with the relevant access, its rules imported and its schema harvested. Fix proposals also need a configured model provider. Read-only access reaches a simulated, reviewed fix; applying it needs write access and the apply permission.

Human judgment stays in the review. A pipeline cannot decide whether a detection is worth its noise, which blind spots you can live with or when a rule should retire.

What should you check first?

Check whether the repository, not the SIEM, holds the production rule set, and whether every rule is tested in both directions:

  • Can you rebuild every production rule from your repository, with no rule that exists only in the SIEM?
  • Does every rule have a sample event it must match?
  • Do you run your rules over clean logs before release?
  • How long would it take to revert the last deployment?

Frequently asked questions

What is the difference between detection engineering and detection as code?

Detection engineering is the work of designing, testing and maintaining the rules that turn logs into alerts. Detection as code is the way of doing that work with software practice: rules in version control, review on every change and automated tests. Without it, a team loses history and repeatability as the rule set grows.

Do you need Sigma rules for detection as code?

No. Detection as code works with any rule format a tool can read, including your SIEM's own query language. Sigma rules help when you run more than one platform or want community rules, because one YAML file converts to several query languages. The repository, review and tests matter more than the format you choose.

How do you validate detections before deploying them to the SOC?

Run three checks before a rule ships. A syntax check confirms the rule is well formed. A positive test confirms it matches a sample of the behavior it targets. A negative test runs it over logs from clean systems to catch false positives. Then deploy deliberately and watch the rule for silence or noise afterward.

How do you roll back a bad detection rule?

Keep every deployment traceable to a commit, so a rollback is a revert. In Git, a revert records a new commit that undoes the earlier change, which keeps the history complete. Then redeploy from the repository. In tools that apply changes directly, check what rollback can undo for each target before you rely on it.

Next step

See how Virtus Optimus proposes reviewable fixes your team can simulate, approve and apply.

Film · 1:00

Imperum AI Fabric

One minute on the AI Fabric appliance: the agents, Agent Studio, routing, pools, Veil and token metering.