Detection as Code: One Detection Rule for More Than One SIEM
One detection rule written in Sigma, reviewed in Git, converted to KQL and SPL, and tested before it ever reaches the SOC.
Ahmed Eid 6 min read
- Topic
- Threat Detection & Response
- Level
- Intermediate
- Techniques
- T1059.001 · T1204.002
- Tools
- Sigma · sigma-cli · Git · KQL · SPL
- You leave with
- A pipeline that converts one detection rule for several SIEMs and tests it automatically

From one rule to several SIEMs
Most SOC teams write detection rules directly in the portal: open Microsoft Sentinel, write the query, hit Save. That works until you have 300 rules and nobody remembers who changed what, or why a rule stopped firing two months ago.
The problem with rules that live in the portal
A rule that exists only in the portal has no history. Nobody reviewed it before it went live, and no test tells you it still detects what it was built for. And if you decide to move from one SIEM to another, you rewrite everything from scratch.
Software development was in the same position before version control and CI, which is why the solution comes from there.
What Detection as Code means
It means treating a detection rule like code: a file in Git, changed through a pull request, reviewed by a colleague, covered by automated tests, and deployed by a pipeline rather than by hand.
- Version control: every change has an owner, a reason and a date, and you can return to any version.
- Review: a second pair of eyes catches mistakes and false positives before they reach SOC analysts.
- Testing: the rule runs against data containing the attack, and against normal data, before deployment.
- CI/CD: deployment happens from the pipeline, so what’s in the SIEM is exactly what’s in Git.
A Sigma rule
Sigma is an open language for writing detection rules independently of any SIEM. A rule is a YAML file describing the behavior you’re looking for, without being tied to field names in Microsoft Sentinel or Splunk. This is the same hypothesis from the Threat Hunting article: Word or Excel starting PowerShell or cmd.
title: Office Application Spawning a Script Interpreter
id: 3f1c2a7e-8b4d-4c6e-9a51-2d0f7b6e1c42
status: experimental
description: Word or Excel starting PowerShell or cmd, a common sign of a malicious macro.
author: Ahmed Eid
date: 2026-11-04
tags:
- attack.execution
- attack.t1059.001
- attack.t1204.002
logsource:
category: process_creation
product: windows
detection:
selection:
ParentImage|endswith:
- '\WINWORD.EXE'
- '\EXCEL.EXE'
Image|endswith:
- '\powershell.exe'
- '\cmd.exe'
condition: selection
falsepositives:
- Business macros that launch scripts on purpose (document each one)
level: high
The two most important parts of the file are logsource, which says where the data comes from, and detection, which says what you’re looking for. The tags link the rule to ATT&CK, and that’s what lets you measure your coverage on the matrix.
Converting to KQL and SPL
The official tool is sigma-cli, with a backend for each SIEM. The backend translates the rule, and the pipeline maps field names to your data source:
pip install sigma-cli
sigma plugin install splunk
pip install pysigma-backend-kusto
sigma convert -t splunk -p sysmon rules/office_spawn_shell.yml
sigma convert -t kusto -p microsoft_xdr rules/office_spawn_shell.yml
The output will be close to these queries. The exact form varies a little with the backend version and the pipeline you choose:
DeviceProcessEvents
| where InitiatingProcessFolderPath endswith "\\winword.exe"
or InitiatingProcessFolderPath endswith "\\excel.exe"
| where FolderPath endswith "\\powershell.exe"
or FolderPath endswith "\\cmd.exe"EventCode=1
(ParentImage="*\\WINWORD.EXE" OR ParentImage="*\\EXCEL.EXE")
(Image="*\\powershell.exe" OR Image="*\\cmd.exe")The point is that you no longer write both by hand. Change the filter in the Sigma file and both update together.
Testing before deployment
Testing a detection rule comes down to 3 questions, and CI should answer them on every pull request:
- Is the file valid?
sigma checkconfirms the rule is well formed and free of common errors. - Does it detect the attack? Run the attack itself in a lab (tools like Atomic Red Team have ready-made tests for
T1059.001) and confirm the rule fires. - Will it flood the SOC? Run the query over the last 30 days of your real environment. If it returns hundreds of results, the rule needs tuning before it ships.
A simple ruleIf a rule has no test proving it detects the attack, you don't know it works. You only think it does.
The detection rule lifecycle
A rule isn’t finished when it ships. Environments change, attackers change, and some rules have to be retired:
- IdeaFrom threat intelligence, an ATT&CK technique with no coverage, or a hunt that found something.
- ResearchHow exactly does the attacker do this? Which log sees it? Are you collecting that log?
- WriteA Sigma rule on a new branch, with a description, ATT&CK tags and expected false positives.
- TestCheck, a true positive in a lab, and a run over 30 days of real data.
- DeployThe pipeline converts and deploys. Nobody edits in the portal by hand.
- TuningEvery false positive returns as a pull request: a documented exception, not a disabled rule.
- RetireRule no longer detects anything, or its source is gone? Close it with a written reason rather than keep the noise.
Repo layout
There’s no mandatory structure, but a clear separation makes the work easier. An illustrative example:
detections/
rules/
windows/
office_spawn_shell.yml
cloud/
tests/
office_spawn_shell/
malicious.json
benign.json
pipelines/
docs/
- rules: Sigma files organized by source.
- tests: log samples containing the attack, and normal samples, for each rule.
- pipelines: conversion settings for each SIEM.
- docs: how to handle the alert, so the SOC analyst knows what to do.
The path of a change
Let’s follow a small edit from start to finish:
- 1Create a branch, then edit the rule or add a documented exception.
- 2A colleague reviews the logic, false positives and description.
- 3CI validates the file, converts it and runs the tests.
- 4After the merge, the pipeline deploys the approved version.
- 5Track the results. Any finding returns as a new change.
The big benefit is that every rule has a history. When someone asks why an exception exists, the answer is in the pull request.
Writing a good rule
The pipeline protects the work, but it won’t write a good detection for you. Before shipping a rule, make sure that:
- The description says what it detects and why.
- The technique in the tags matches the behavior.
- Expected false positives are written down.
- The alert comes with triage steps.
- The log source is actually being collected.
What to measure
- The true positive rate for each rule.
- The number of alerts closed with no action.
- The time from identifying the need for a rule to deploying it.
- The number of rules that have tests.
- Coverage of the techniques that matter most to your environment.
Common mistakes
- Migrating every rule at once rather than starting with the most important.
- Editing directly in the portal, so Git drifts from reality.
- Writing only success tests, with no normal samples.
- Relying on the conversion without reviewing the resulting query.
References
- SigmaHQ/sigma: the specification and thousands of ready-made rules.
- SigmaHQ/sigma-cli: the
convert,pluginandcheckcommands. - pySigma-backend-kusto: the KQL backend, whose pipeline is named
microsoft_xdr.