Writing the rule: Sigma in depth
Unit 2 of 5 in Detection engineering.
You will be able to write a Sigma rule with modifiers, conditions, a filter and a correlation, and explain what each part does.
Lesson
The parts of a rule
The Sigma specification lists the parts of a rule. Mandatory: a title, a log source and a detection. Optional but valuable: a globally unique id (a random version 4 UUID), a status, a description, references, author, date and modified date, a list of fields worth showing the analyst, a list of known false positives, a level and tags. The specification says to give a rule a new id when its logic changes in a big way, when you derive a new rule from it and keep both active, or when you merge rules; the related field records the relationship as derived, obsolete, merged, renamed or similar.
The status field tells readers how far to trust the rule: stable may be used in production; test is mostly stable but may need adjusting for the environment; experimental could be noisy; deprecated has been replaced; unsupported cannot be used as written. Tags are namespaced with a dot, for example attack.t1234 for ATT&CK technique 1234. Use all of these. A rule with no status and no known false positives asks the next reader to guess.
Selections: maps are AND, lists are OR
Inside the detection, a search identifier is a named group of matches. The specification gives two shapes. A map is a set of field and value pairs, and all pairs must match (logical AND). A list of values for one field matches if any value matches (logical OR), and a list of maps matches if any map matches. Values are treated as case-insensitive strings, with * and ? as wildcards, while regular expressions are case-sensitive by default.
Read this slowly, because it is the source of many wrong rules. A map with an event identifier of two values and a user name means 'either event, and that user'. When a rule fires on far too much, check whether a list should have been a map.
Modifiers and conditions
Modifiers change how a value is matched, and are appended to the field name with a pipe: contains, startswith and endswith for text, re for regular expressions, cidr for network ranges, gt and lt for numbers, base64offset for text that may appear encoded, and others. Lists of values are joined with OR, and the all modifier changes that to AND. It is for cases like a command line with several parameters in any order; a single value is not allowed to use it.
The condition combines search identifiers with and, or, not and brackets, and with 'one of' and 'all of' patterns such as 'selection*'. Precedence from loosest to tightest is or, and, not, then the 'x of' expressions and brackets. The specification discourages 'all of them' because it stops downstream users from generically filtering unwanted matches; use 'all of selection*' and keep filter identifiers named with a common prefix. Conditions also let a rule exclude a known false positive, for instance 'selection and not filter_admin'.
Filters: keep your exceptions out of the shared rule
Every organization has its own harmless exceptions: a management agent, a jump host, a scanner. Sigma filters let you keep them in separate files that look like rules but use a filter section instead of a detection, and that name the rules they apply to by name or id. The documentation says this lets you split exclusions out, apply one filter to several rules, and keep the original rule in its pristine state, which makes updating from rule packs easier. When you convert, a command-line option applies the filter to the generated query.
The habit to learn: put what is true everywhere in the rule and what is true only for you in a filter. Then your tuning survives the next upstream update, and anyone can review your exceptions on their own.
Correlation: a behaviour made of several events
Some behaviour is invisible in one event. Many failed sign-ins followed by a successful one is the textbook example. Sigma correlation rules express this on top of ordinary rules. The specification defines types: event_count (how many matching events), value_count (how many distinct values of a field), temporal (several different rules matching within a timespan) and temporal_ordered (in a given order). A correlation names the rules it refers to, a group-by that sets the scope (for example per user, or per user and host), a timespan, and a condition such as a count threshold. It can also set a level, so single hits stay low or informational and the correlation raises it.
The specification is also honest about limits. A target system that cannot recognise the order of events may produce false positives from differently ordered events. If it only recognises fixed time windows, a one-hour span only matches events inside a single clock hour, so a pattern that straddles two hours could be missed. Whenever you convert a correlation, test it near window edges.
Key points
- Mandatory parts are title, log source and detection; add id, status, falsepositives, level and tags every time.
- In a map all pairs must match; in a list any item may match. Mixing them up is a common bug.
- Modifiers shape matching (contains, startswith, endswith, re, cidr, all); prefer 'all of selection*' over 'all of them'.
- Filters keep your own exceptions outside the shared rule and survive rule updates.
- Correlation rules count events, distinct values or ordered events in a timespan; test them at window edges.
Sources
- Sigma Rules Specification, version 2.1.0
- Sigma documentation: Modifiers
- Sigma documentation: Conditions
- Sigma documentation: Filters
- Sigma Correlation Rules Specification
Practice in the labs
Write rules against Linux audit events, process events and a sign-in sequence, then check them in the Detection Lab.
- Catch ATT&CK T1033 System Owner/User Discovery with an auditd rule (Purple Discovery 2 of 6): You write a narrow audit rule that records only three executables and compare its event count with a broad rule (Purple Discovery series). Runs on a Linux machine you own.
- Catch ATT&CK T1087.001 local account discovery and watch who reads the account file (Purple Discovery 4 of 6): You compare a filtered and an unfiltered file rule by event count and write a keep-or-drop decision with a reason. Runs on a Linux machine you own.
- Catch shell script execution (T1059.004), and see what a rule on the shell can and cannot see (Purple Execution 1 of 3): You load a shell rule and a temporary-folder rule and record which fired for each test, learning what each rule can and cannot see (Purple Execution series). Runs on a Linux machine you own.
- Catch a new local account (T1136.001), then remove it and prove it is gone (Purple Persistence 2 of 5): You catch a new account with a file rule and a program rule, then prove the account is gone (Purple Persistence series). Runs on a Linux machine you own.
- Detect Office apps spawning suspicious child processes from Sysmon Event ID 1: You score a naive rule, a denylist rule and a tuned rule on 280 synthetic events and see why a process started by management software slips past an ancestry rule. Runs offline.
- Write a Sigma correlation rule: catch a brute-force that SUCCEEDED (failed logins then a login): You write a correlation rule for failed logins followed by a success, validate it with sigma check and test it on a malicious and a benign bundle. Runs offline.
Also try, in the simulators
- Sigma: one rule format for logs (Detection Lab lesson): The Detection Lab course lesson on Sigma, a short refresher on the rule format.
- Find the accounting updater starting a command shell (Detection Lab task): You write a rule that finds the accounting updater starting a command shell, a parent and child process rule from a real incident.
- Find requests to a script page nobody links to (Detection Lab task): You write a rule for requests to a script page nobody links to, a web log rule with a clear false-positive question.
Knowledge check
- In Sigma, a selection has two fields, each with one value. What does it mean? Answer: Both fields must match in the same event (logical AND). The specification says all elements of a map are joined with a logical AND; it is a list of values for one field that is joined with OR.
- Why keep a site-specific exception in a Sigma filter instead of editing the rule? Answer: The shared rule stays unchanged, so updates are easy and your exceptions can be reviewed separately. The Sigma documentation says filters let you split exclusions out, apply them to several rules and keep the original rule in its original state, which eases updating from rule packs.
- A correlation rule uses a one-hour timespan. What limit does the specification warn about? Answer: Some systems only match events inside a single clock hour, so a pattern across two hours could be missed. The correlation specification warns that where a target recognises only static time boundaries, events split across two hours might not match, causing false negatives.
- Which condition style does the specification prefer, and why? Answer: 'all of selection*', because 'all of them' stops downstream users filtering unwanted matches. The specification discourages 'all of them' because it prevents generic filtering of unwanted matches downstream, and points to 'all of' with a search-identifier pattern as the preferred method.
Back to the syllabus or see all classes.
Related
- Detection engineering
- Catch ATT&CK T1033 System Owner/User Discovery with an auditd rule (Purple Discovery 2 of 6)
- Catch ATT&CK T1087.001 local account discovery and watch who reads the account file (Purple Discovery 4 of 6)
- Catch shell script execution (T1059.004), and see what a rule on the shell can and cannot see (Purple Execution 1 of 3)
- Catch a new local account (T1136.001), then remove it and prove it is gone (Purple Persistence 2 of 5)
- Detect Office apps spawning suspicious child processes from Sysmon Event ID 1
- Write a Sigma correlation rule: catch a brute-force that SUCCEEDED (failed logins then a login)
- All classes
- Free cyber labs