Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Writing Sigma rules for logs

After this lesson: I can write a Sigma rule for process events and convert it into a query.

65 min lesson6 commandsDetection engineering lab: Suricata rules, Sigma rules and Zeek-style logs on one private Ubuntu VM

Write the rule once, search anywhere

Sigma is an open format for writing detections on logs, in YAML. A converter turns one Sigma rule into the query language of whichever log search a team uses.

Teams share Sigma rules because a rule written once can run in many places. You write rules for a made-up office's Windows events, in your own lab copy only.

By the end of this lesson

Note

I can write a Sigma rule for process events and convert it into a query.

Key words

WordWhat it meansExample
Sigmaan open format for log detections, written in YAML
Log sourcethe kind of log a rule is forprocess_creation
Selectiona named group of field tests that must all pass
Modifiera word after a field that changes how it comparesendswith
Conditionhow the selections combine into a match
Backendthe part that turns a rule into one search language

The cells students fill in are left blank in this preview.

The one rule

Important

These events are made up for the lab. Search real logs only on systems you look after or have written permission to search. Logs about people's work are personal data, so read only what the job needs.

A Sigma rule says where and what

A Sigma rule has a log source, which kind of log it reads, and a detection. The detection holds named selections and a condition that combines them.

The Run prints a rule for a classic attack: a Word or Excel file that starts PowerShell with an encoded command. Each field test has a modifier after a bar.

Move into the lab's folder

Command
cd ~/detect

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read a Sigma rule

Command
cat sigma/office-powershell.yml

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: the parts of the rule

PartIn the ruleWhat it means
Log sourceprocess_creation, windowsRead Windows process start events
Selection parentParentImage|endswith, two valuesStarted by Word or by Excel
Selection childImage|endswith and CommandLine|containsPowerShell, with " -enc " in its command
Conditionparent and childBoth selections must pass
Tagsattack.t1059.001The ATT&CK technique it detects

The cells students fill in are left blank in this preview.

A backend turns the rule into a query

sigma convert uses a backend to turn a rule into one search language. The lab has the SQLite backend, so the rule becomes SQL. Look for endswith becoming LIKE with a % in front, a list becoming OR, and a selection becoming AND.

The second Run converts the rule and runs the SQL on the lab's 41 events. One event matches.

Convert the rule into SQL

Command
sigma convert -t sqlite sigma/office-powershell.yml

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Run the rule on the events

Command
det sigma sigma/office-powershell.yml

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

A common mistake

Warning

Many people think Sigma is a program that searches logs. It is a format. A backend converts the rule into the search language of the tool that holds the logs. The same rule becomes SQL here and a different query for another tool.

Test a rule against everything near it

The first Run lists every PowerShell start in the events. There are three. Only one came from Word with -enc. An admin ran -enc from the desktop, and Excel ran a script by name. The rule rightly skipped both.

The second Run tries a looser rule, any run of whoami. It matches the attack and an admin. A filter selection with and not filter would remove the admin.

Every PowerShell start in the events

Command
sqlite3 logs/events.db "SELECT Computer, User, ParentImage, CommandLine FROM logs WHERE Image LIKE '%powershell.exe'"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

A looser rule and its matches

Command
det sigma sigma/whoami.yml

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try it: how the modifiers compare

Code: python
value = "C:/Windows/System32/whoami.exe"

print("endswith:", value.lower().endswith("whoami.exe"))
print("contains:", "system32" in value.lower())
print("startswith:", value.lower().startswith("d:/"))
print("equals:", value.lower() == "whoami.exe")
# Sigma compares without caring about capitals. Why use lower() on both sides?

About this lab and its source: Detection engineering lab: Suricata rules, Sigma rules and Zeek-style logs on one private Ubuntu VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.