Anatomy of a Sigma rule
Sigma is to logs what YARA is to files. It is a vendor-neutral way of describing what a malicious event looks like. SOC teams convert Sigma rules into SIEM queries, incident responders run them over exported event logs with tools such as Chainsaw and Hayabusa, and the community shares thousands of them in the SigmaHQ repository. Writing a rule that parses is easy. Writing one that is precise, portable, and cheap to run takes a few habits.
Explore also my posts about YARA and ClamAV rules.
Anatomy of a rule
The running example is a rule I wrote for k-evtrace, a tool that applies Sigma rules to Kaspersky event logs. It matches the events Kaspersky Endpoint Security writes to the Windows event log when it detects malware, and keeps only the ones that carry a file hash:
title: Kaspersky Detection Event
id: d2a938b0-2519-4b74-b4be-32e70aef804c
status: experimental
description: |
Detects malware detections from Kaspersky Endpoint Security (Event IDs 302 and 231).
This rule matches on presence of file hash patterns (MD5, SHA256) in the EventData.
author: Cristian Souza
date: 2025-07-23
modified: 2025-07-23
level: critical
logsource:
product: windows
service: 'Kaspersky Endpoint Security'
detection:
selection:
Provider_Name: 'avp'
EventID:
- 302
- 231
Channel: 'Kaspersky Endpoint Security'
hash_match:
Data|contains:
- 'MD5:'
- 'SHA256:'
condition: selection and hash_match
falsepositives:
- EICAR test files
ruletype: Sigma
engine: k-evtrace
Reading the pieces:
- Metadata: The
titlesays what is detected and thedescriptionstarts with "Detects". Theidis a random UUID and it is the rule's identity. Alerts, suppression lists, and tickets will refer to it, so keep it stable.statusfollows a lifecycle, fromexperimentalwhen written, totest, tostablewhen it is ready for production.dateandmodifieduse ISO 8601 (YYYY-MM-DD). - Level:
levelis a promise about alerting.informationalandloware for enrichment and hunting,mediumfor periodic review,highandcriticalfor immediate triage. An antivirus detection deserves someone's attention right away, so this one iscritical. - Log source:
productandservicesay where the event comes from. There is a whole section about this below, because it is the part most often done wrong. - Detection: Each named block under
detectionis a selection. Inside a selection, fields are joined with AND and the items of a list with OR. Soselectionreads "the provider isavp, the channel is Kaspersky Endpoint Security, and the event ID is 302 or 231", andhash_matchreads "the event data containsMD5:orSHA256:". - Condition: The
conditioncombines the selections by name withand,or, andnot. Here both must match. - False positives:
falsepositivesmust be specific enough that the next person can write a filter from it. UseUnknownwhen you did not find any. - Custom fields: Sigma allows extra top-level attributes, and parsers ignore the ones they do not know.
ruletypeandengineare read by the tooling that runs the rule (Hayabusa usesruletypethe same way).
Naming: the SigmaHQ conventions ask for a lowercase file name that starts with the log source, such as win_kaspersky_detection_event.yml. A consistent scheme lets you slice a rule set by prefix and triage alerts at a glance.
The log source
The logsource section has three attributes: category, product, and service. Generic categories (process_creation, image_load, network_connection, file_event, registry_set, dns_query...) describe an event type, not a channel, and the processing pipeline used at conversion time decides which telemetry provides it. Take this rule:
title: Whoami Executed By IIS Worker Process
logsource:
category: process_creation
product: windows
detection:
selection:
Image|endswith: '\whoami.exe'
ParentImage|endswith: '\w3wp.exe'
condition: selection
The same file converts to Sysmon or to Windows Security auditing, depending only on the pipeline:
$ sigma convert -t splunk -p sysmon proc_creation_win_whoami_iis_child.yml
EventID=1 Image="*\\whoami.exe" ParentImage="*\\w3wp.exe"
$ sigma convert -t splunk -p windows-audit proc_creation_win_whoami_iis_child.yml
EventID=4688 NewProcessName="*\\whoami.exe" ParentProcessName="*\\w3wp.exe"
This works because the rule uses the field names of the Sigma taxonomy (Image, CommandLine, ParentImage, User...) instead of the vendor's. Two rules of thumb follow from it:
- Do not hardcode an EventID when a category exists.
category: process_creationtogether withEventID: 4688ties the rule to one telemetry and makes the pipeline pointless. The Kaspersky rule above pins its EventIDs because no generic category covers that channel. - Know which fields exist in each telemetry.
OriginalFileName,Hashes, andParentCommandLineexist in Sysmon but not in event 4688, and the pipeline does not warn you.
Picking fields and values
Similar to what we did with YARA and ClamAV, the best candidates are the things an attacker cannot change without changing the technique:
- Parent-child relationships (an Office application spawning a script host,
w3wp.exespawningcmd.exe); - Command line switches and their combinations (
-encwith-w hidden); - Locations such as user-writable directories and UNC paths;
- The identity of the binary from its version info (
OriginalFileName), not only from its path.
Things to avoid:
- Specific hashes, IP addresses, and domains. They expire with the campaign and belong in IOC matching.
- Full paths.
C:\Windows\System32\also exists asSysWOW64and on another drive letter. - The exact command line from one report.
powershell -nop -w hidden -enc ...is one of a thousand spellings of the same thing. - Keyword lists without a field. They become full-text searches over every field of every event.
Then choose the right modifiers. They are appended to the field name with a pipe and can be chained:
Data|contains: 'MD5:' # wildcards on both sides
Image|endswith: '\schtasks.exe' # wildcard before the value
CommandLine|contains|all: # every value must appear (AND)
- ' -e'
- ' -w hidden'
CommandLine|contains|windash: ' -enc ' # also matches /enc and the Unicode dashes
CommandLine|base64offset|contains: 'Invoke-Expression' # the three Base64 alignments
CommandLine|re|i: 'frombase64string\(.{200,}\)' # regex, case-insensitive
DestinationIp|cidr: '10.0.0.0/8' # network match
A few notes on these:
- Plain values are case-insensitive, and
*and?are wildcards. Prefer the modifiers over writing the wildcards yourself, becauseImage|endswithsays what you mean. - Lists are OR by default, and
|allturns them into AND.contains|allis the workhorse for "these fragments in any order". windashreplaces the old habit of writing-encand/enctwice.- Regular expressions are the one place where Sigma is case-sensitive by default, so add
|iwhen the case may vary. They are also the most expensive thing a rule can ask for.
The full list is in the modifiers appendix, or in sigma list modifiers.
Writing conditions
The condition combines selections with and, or, not, parentheses, and the x of pattern forms (1 of selection_*, all of selection_*). Write the parentheses whenever a reader could hesitate.
The Kaspersky rule has two selections and needs both. Larger rules follow a naming convention that makes the condition read like a sentence. Evidence goes into selection_*, exclusions that apply everywhere into filter_main_*, and environment-specific tuning into filter_optional_*:
condition: all of selection_* and not 1 of filter_main_* and not 1 of filter_optional_*
Filters deserve the same care as selections, because every filter is a hole:
- Filter exact things, such as a specific parent image or path.
User|contains: 'admin'excludes the account the attacker is most likely to be using. - Avoid
all of them. It includes every selection, which prevents anyone downstream from adding a filter. Name the pattern instead.
Converting and testing
The reference implementation is pySigma, wrapped by sigma-cli. Backends (one per query language) and pipelines (field and log source mappings) are installed as plugins:
pipx install sigma-cli
sigma plugin install splunk sysmon windows # backend + pipelines
sigma list targets # installed backends
sigma list pipelines # installed pipelines
sigma check rules/ # parser errors + validators
sigma convert -t splunk -p sysmon rules/windows/process_creation/
The Kaspersky rule already uses the raw field names, so it converts without a pipeline:
$ sigma convert -t splunk --without-pipeline win_kaspersky_detection_event.yml
Provider_Name="avp" (EventID IN (302, 231)) Channel="Kaspersky Endpoint Security" (Data IN ("*MD5:*", "*SHA256:*"))
A rule that converts and passes the validators is still not done. Before deploying it:
- Test against positives: produce the event in a lab and confirm the rule matches. For this rule, an EICAR file is enough to make the antivirus log a detection.
- Test offline: Chainsaw and Hayabusa run Sigma rules directly over
.evtxfiles, so you can test against collected logs before the rule ever touches the SIEM. - Test against the baseline: run the converted query over the last weeks of production data before enabling it as an alert. Every false positive you find here is one your SOC will not have to triage :).
- Treat rules as code: keep them in version control, run
sigma checkin CI, and bumpmodifiedon every change.
Going further
Once single-event rules feel comfortable, these are the next things to look at:
- Correlation rules: Some detections are about more than one event, such as many failed logons from one source. Sigma 1 expressed this with
| count() byin the condition. That syntax is gone, and the replacement is the correlation rule, a separate YAML document that references other rules and applies a threshold over a time window. - Sigma filters: Exclusions that are specific to one environment used to be edited into each rule. Sigma filters keep them in their own file, so the rule stays identical to upstream.
- Performance: Sigma does not search anything, the SIEM does, so the cost of a rule is the cost of the query it becomes. Give every selection at least one selective field (
EventID,Image,Channel), keep regular expressions behind a selection that narrows the events first, and do not deploy rules for log sources you do not collect. - Hunting rules: A detection rule should almost never be wrong. A hunting rule can be looser, since a human reviews what it finds. Mark it with
level: lowand thedetection.threat-huntingtag so nobody turns it into an alert by accident.
A quick checklist
- Generic
logsourcecategory and taxonomy field names where they exist,serviceplusEventIDwhen they do not; - Behavior over indicators;
- Modifiers instead of hand-written wildcards;
- Named selections, exact filters, no
all of them; - Metadata with a stable UUID, status, ISO dates, specific false positives, and an honest level;
- Tested against positives, offline logs, and the production baseline;
sigma checkpasses without errors.
Conclusion
Good Sigma rules come from understanding what is truly specific to the behavior, and verifying that assumption against real logs. Describe the technique rather than the sample, let the pipeline deal with the telemetry, and test before you deploy. Happy hunting!