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:

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:

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:

Things to avoid:

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:

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:

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:

Going further

Once single-event rules feel comfortable, these are the next things to look at:

A quick checklist

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!