All posts

Detections are code, so I started treating them that way

Most detection logic lives as a query someone pasted into a SIEM console. No history, no review, no tests. When it stops firing, nobody can say what changed or why.

Software engineering settled this problem a long time ago. Write the logic in a portable format, keep it in version control, review changes through pull requests, and test everything automatically before it merges. I built two public repositories to work that way.

What is covered

ATT&CKDetectionTacticStatusSigma rule
T1110.003Password spray from a single sourceCredential AccessStableYes
T1110.001Brute force against a single accountCredential AccessStableYes
T1098.002Suspicious inbox rule creationPersistenceIn tuningYes
T1528OAuth consent grant abuseCredential AccessExperimentalYes
T1078Impossible travelInitial AccessIn tuningNot yet

Five detections is a small catalog. It is also all identity and mailbox activity, because that is where a Microsoft 365 environment actually gets attacked.

One detection, two formats

The Sentinel version of the password spray detection looks for one source address failing sign-in across many distinct accounts inside a 30-minute window:

let lookback = 1h;
let sprayWindow = 30m;
let accountThreshold = 10;     // distinct accounts targeted from one source
SigninLogs
| where TimeGenerated > ago(lookback)
| where ResultType in ("50126", "50053", "50055")
| summarize
    FailedAttempts   = count(),
    TargetedAccounts = dcount(UserPrincipalName),
    AccountList      = make_set(UserPrincipalName, 100),
    ResultCodes      = make_set(ResultType)
    by IPAddress, bin(TimeGenerated, sprayWindow)
| where TargetedAccounts >= accountThreshold
| order by TargetedAccounts desc

The Sigma version describes the same activity in a backend-neutral format, with its false positives written down next to the logic: a shared corporate egress IP behind NAT, and applications replaying stale credentials. Sigma converts to Sentinel, Defender XDR, Elastic, or Splunk, so the rule is written once.

One honest gap: the Sigma rule matches each failed sign-in on its own. The 10-account threshold lives in the KQL version for now.

Four gates before a merge

Every pull request that touches a rule, the pipeline, or the tests, and every push to main, runs four steps in GitHub Actions:

  1. Rule requirement tests in pytest, checking every rule against the contribution standard.
  2. Sigma syntax validation.
  3. Conversion to KQL through a pySigma pipeline that maps each log source to its Sentinel table.
  4. An uploaded artifact of the generated queries, for review.

If any step fails, nothing merges.

This matters more in a compliance-driven shop than anywhere else. Git history is a timestamped record of what changed, when, and who approved it. That is the kind of evidence an assessor asks for, and a SIEM console does not keep it.

Every rule in the catalog was also tested against real telemetry before I trusted it. That lab is the next post.

The query is the easy part. Knowing exactly what changed, and when, is the part a console never gave me.