Writing detection rules with Sigma
Write a Sigma rule for a technique, convert it to your SIEM and test it.
By Invicly Tech, , in Cyber Security Fundamentals. About 11 minutes to read.
In short
Sigma is a generic, vendor-neutral format for detection rules. You write a rule describing suspicious log patterns, convert it to your SIEM's query language, and test it against both benign and malicious samples so it fires on threats without drowning analysts.
Explained simply
A detection rule is a saved question that the SIEM asks again and again: did anyone do this suspicious thing? If the answer is yes, it raises an alert. Writing good rules is the core job of a detection engineer, the person who builds and maintains alerts.
Every SIEM has its own query language, so a rule written for Splunk will not run in Elastic. Sigma solves this. You write the rule once in a simple YAML file, and a converter translates it into your SIEM's language, like translating one recipe into many languages.
A Sigma rule has three main parts: which logs to look at (logsource), what to look for (detection), and notes for the analyst who gets the alert (title, level and known false positives). The public SigmaHQ library holds thousands of community rules you can learn from.
A rule is not finished when it is written. You test it: does it fire when the bad thing happens, and does it stay quiet during normal work? A rule that fires 500 times a day on routine admin work will be ignored, which is as bad as having no rule.
Press Play on a diagram to walk through it one step at a time.
- Step 1Pick technique
Choose behaviour to detect.
- Step 2Write Sigma
Describe the log pattern.
- Step 3Convert
Translate to the SIEM query.
- Step 4Test & tune
Check true and false positives.
- 1Research
Pick a technique and its log evidence
- 2Write
Sigma rule with tags and notes
- 3Test
Positive in lab, negative on real data
- 4Deploy
Start as experimental
- 5Tune
Learn from every alert and miss
- Back to step 1
- Matches one file hash
- Matches an exact tool name
- Breaks when the attacker renames a file
- Easy to write, easy to dodge
- Matches behaviour, like Office starting a shell
- Uses OriginalFileName, not just Image
- Survives small attacker changes
- Needs more testing and tuning
In depth
Anatomy of a Sigma rule
A Sigma rule is a YAML document with a small set of known keys. title is a short name. id is a UUID, so the rule can be tracked even if it is renamed. status shows maturity: experimental, test or stable (plus deprecated and unsupported). description, references, author and date help the next person. tags link the rule to MITRE ATT&CK, for example attack.t1059.001 for PowerShell.
logsource says which data the rule needs, using category (such as process_creation), product (such as windows) and service (such as security). detection holds one or more named selections and a condition that combines them. falsepositives lists known harmless causes, which is gold for the analyst at 3 a.m. level is one of informational, low, medium, high or critical.
- Metadata
- title, id
- status, author, date
- description, references
- tags
- ATT&CK tactic
- ATT&CK technique ID
- logsource
- category
- product
- service
- detection
- selections
- filters
- condition
- Analyst notes
- falsepositives
- level
Selections, modifiers and conditions
Inside detection, each selection is a map of field names to values. Fields in the same map are combined with AND. A list of values under one field is combined with OR. So Image ending in powershell.exe AND CommandLine containing any of several flags is one selection with two fields.
Modifiers come after a pipe and change how values match: contains, startswith, endswith, all (every listed value must match instead of any), re for regular expressions, cidr for IP ranges, base64offset for finding text hidden in base64, and windash, which matches both -flag and /flag styles. String matching is case-insensitive by default.
The condition combines selections with and, or, not and brackets. Shortcuts help: 1 of selection_* means any selection whose name starts with selection_, and all of them means every selection.
- condition: selection and not filter_admin_tools
- condition: 1 of selection_*
- Image|endswith beats Image equals: the drive letter and folder can vary
Converting with pySigma and sigma-cli
Conversion is done by pySigma, a Python library, usually through its command-line tool sigma-cli. Install it with pipx install sigma-cli, then install a backend plugin for your SIEM, for example sigma plugin install splunk. sigma list targets shows the backends you have, and sigma list pipelines shows the processing pipelines.
The pipeline is the part people forget. Sigma uses generic field names like Image and CommandLine and generic log sources like process_creation. Your SIEM may store the same data under different names and in a specific index. A processing pipeline maps one to the other. With the wrong pipeline, the converted query is valid but never matches anything, which is worse than an error because nobody notices.
The usual command is sigma convert -t splunk -p sysmon rule.yml. Run sigma check rule.yml first to catch mistakes in the rule itself.
- 1rule.yml
Generic fields and log source
- 2sigma check
Validate the rule
- 3Pipeline
Map fields and log source to your SIEM
- 4Backend
Write the SIEM query
- 5Deploy
Saved search or rule in the SIEM
Testing and tuning before production
Test in two directions. The positive test proves the rule fires: run the technique in a lab (an Atomic Red Team test is ideal) and check that the alert appears with the right fields. The negative test estimates noise: run the converted query as a normal search over the last 30 days of real data and count the hits. Review a sample. Every harmless hit is either a future false positive or a filter you should add now.
Add filters as separate named selections, such as filter_sccm, so the reason for each exclusion is readable. Keep filters narrow: an exact path plus a parent process, never a whole folder like C:\Temp. Start new rules as experimental, move them to test after a week or two in production, and to stable once they behave.
Store rules in git with their test notes. This practice, called detection as code, gives you review, history and a quick way to undo a bad change.
Learn it in 4 short steps
About 55m each. Track them with a timer in your plan.
- 1Read the Sigma rule format~55m
- 2Write a rule for one technique~55m
- 3Convert it for your SIEM~55m
- 4Test with benign and malicious samples~55m
Key points
- Sigma is a portable, SIEM-agnostic rule format.
- Convert a Sigma rule to your platform's query language.
- Test against benign data to estimate false positives.
- Map rules to ATT&CK techniques for coverage.
Key terms
Tap a card to flip it. 0/4 known
Examples
A Sigma rule for encoded PowerShell
Attackers often hide scripts with powershell -EncodedCommand followed by base64 text. PowerShell accepts short forms such as -enc, -en, -e and the alias -ec. Step 1: choose the log source, Windows process creation (Sysmon event 1 or Security 4688 with command lines). Step 2: select PowerShell by the end of its path. Step 3: select the command line flags, with spaces around them so -ExecutionPolicy does not match. Step 4: require both selections. Step 5: note the known false positive. In a real rule you would also add pwsh.exe and an id generated with uuidgen.
title: PowerShell With Encoded Command
status: experimental
tags: [attack.execution, attack.t1059.001]
logsource:
category: process_creation
product: windows
detection:
selection_img:
Image|endswith: '\powershell.exe'
selection_cli:
CommandLine|contains: [' -e ', ' -en ', ' -enc ', ' -ec ', ' -encodedcommand ']
condition: all of selection_*
falsepositives:
- Management tools that pass encoded scripts
level: mediumConverting the rule and testing it both ways
Step 1: install sigma-cli and the Splunk backend and Sysmon pipeline plugins. Step 2: check and convert the rule. Step 3: paste the query into Splunk and run it over 30 days to count normal hits. Step 4: on a lab Windows VM, run a harmless encoded command (it only prints hi) and confirm the alert appears within a few minutes. If step 3 shows hundreds of hits from one management tool, add a narrow filter for its exact parent process.
pipx install sigma-cli
sigma plugin install splunk
sigma plugin install sysmon
sigma check enc_powershell.yml
sigma convert -t splunk -p sysmon enc_powershell.yml
# On the lab Windows VM (PowerShell), a harmless positive test:
# $b = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes('Write-Output hi'))
# powershell.exe -enc $bQuick reference
- Sigma core keys: title, logsource, detection (selections plus condition), falsepositives, level
- Fields in one selection are ANDed; a list of values for one field is ORed
- Common modifiers: contains, startswith, endswith, all, re, cidr, base64offset, windash
- Condition examples: selection and not filter, 1 of selection_*, all of them
- Tag rules with ATT&CK, for example attack.t1059.001, to measure coverage
- sigma convert -t TARGET -p PIPELINE rule.yml; sigma list targets shows backends
- Before deploying, run the query over 30 days of real data and count the hits
- Match OriginalFileName as well as Image to catch renamed tools
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Write only the detection block of a Sigma rule that catches net.exe or net1.exe adding a user to the local Administrators group (net localgroup administrators NAME /add).
- Exercise 2medium
Write a full Sigma rule for scheduled task creation with schtasks.exe /create, with a filter that excludes one known updater as the parent process.
- Exercise 3hard
Take one community rule from SigmaHQ, convert it for your SIEM, and write a short test report: positive test result, 30-day hit count, filters added, and the status you would give it.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is Sigma a SIEM?
- No. Sigma is only a rule format plus converters. You still need a SIEM or log platform to run the converted queries.
- What is the difference between Sigma, YARA and Suricata rules?
- Sigma matches log events, YARA matches patterns inside files or memory, and Suricata or Snort rules match network traffic. Mature teams use all three.
- Can I deploy SigmaHQ community rules straight to production?
- Not blindly. Convert them, run them against your own data to see the noise, and add filters for your environment before they create alerts.
- How do I get a UUID for the id field?
- Run uuidgen on Linux or macOS, or [guid]::NewGuid() in PowerShell. Never copy the id from another rule.