Stage 4 of 10 · about 14–20 hours · beginner · checked 2026-11-22

Query languages and Sigma

Goal. By the end you can write the same detection logic in SPL and KQL, express it as a Sigma rule, and convert that rule to both languages while knowing what the converter got wrong.

Why it matters#

Query languages are the craft's main tool, and different employers use different platforms. If you understand the ideas behind search (filter, shape, aggregate), a second language takes days, not months. Sigma goes one step further: it is a vendor-neutral format for writing a detection once and converting it to many platforms, which is why public rule collections and shared detections use it.

What you will learn#

Search is a pipeline#

Every detection query does the same four things: pick the data, filter it, shape it, and (sometimes) aggregate it. Learn that shape once and each language is just different words for the same steps.

Task Splunk SPL Microsoft KQL
Choose the data index=... and sourcetype=... A table name, such as SecurityEvent
Filter rows search ... or where ... where ...
Add a computed field eval x = ... extend x = ...
Keep some columns table a b or fields a b project a, b
Pull a value out with a regular expression rex field=f "(?<name>...)" extract("regex", 1, f) or parse
Count by a field stats count by user summarize count() by user
Count distinct values stats dc(user) summarize dcount(user)
Group by time bin _time span=15m bin(TimeGenerated, 15m)
Sort and limit sort - count and head 10 sort by count_ desc and take 10
Enrich or exclude from a list lookup list.csv key OUTPUT ... join, lookup, or a watchlist
Order events in a sequence streamstats, transaction serialize with prev(), or join on a time window

Case rules differ and cause quiet bugs. In KQL == is case-sensitive and =~ is not, and contains is case-insensitive. SPL field values in a plain search are usually matched case-insensitively. Read the documentation for the operator you use.

Five detection patterns#

Almost every detection is one of these, or a mix:

  1. A single event matches. One process, one command line, one log entry. Cheap and precise when the behaviour is rare.
  2. A threshold. More than N of something in a window, such as failed logons per source.
  3. Rare or first-seen. A value you have not seen before for this user, host or service.
  4. A sequence. A then B within a time, such as a download followed by an execution.
  5. An exclusion or enrichment list. The same logic, but ignoring known-good values from a list you maintain, never hard-coded into the search (stage 6).

Sigma: write the logic once#

A Sigma rule is a YAML file. These are its main parts:

Field What it holds
title, id, status, description Name, a unique ID, how mature the rule is, and a plain-language purpose
references, author, date Where the idea came from, who wrote it and when
tags ATT&CK tactics and techniques, such as attack.execution and attack.t1059
logsource The kind of data: a category, product and service (for example Windows process creation)
detection Named selections of field matches, and a condition that combines them
falsepositives Known harmless activity that looks the same
level How serious a hit is: informational, low, medium, high or critical

Field names in a rule are generic (Image, ParentImage, CommandLine). A pipeline maps them to the real tables and fields of your platform when you convert. Matching can be changed with modifiers: a field with |contains, |startswith, |endswith, |re (regular expression) or |cidr matches in that way, |all requires every listed value rather than any one, and |lt, |gt compare numbers. A condition combines named selections with and, or and not, and with shortcuts such as 1 of selection_* or all of them.

SigmaHQ names rule files by log source and behaviour (for example proc_creation_win_...yml), and sigma check flags names that are too short.

A worked example#

Here is the "Office starts a shell" idea from stage 1 as a Sigma rule:

YAML
title: Office application starts a command shell
id: 3f8c2a71-5d94-4e0b-b6a7-91c4d0e5f238
status: experimental
description: >
    Detects a Microsoft Office program starting a command shell or script interpreter,
    a common step after a malicious document is opened.
references:
    - https://attack.mitre.org/techniques/T1059/
author: SecOps RoadMaps
date: 2026-11-22
tags:
    - attack.execution
    - attack.t1059
logsource:
    category: process_creation
    product: windows
detection:
    selection_parent:
        ParentImage|endswith:
            - '\winword.exe'
            - '\excel.exe'
            - '\powerpnt.exe'
            - '\outlook.exe'
    selection_child:
        Image|endswith:
            - '\cmd.exe'
            - '\powershell.exe'
            - '\pwsh.exe'
            - '\wscript.exe'
            - '\cscript.exe'
            - '\mshta.exe'
    condition: selection_parent and selection_child
falsepositives:
    - Legitimate Office add-ins or macros that start a script
level: medium

Converted with sigma-cli (the commands are below), this becomes SPL for Sysmon process events:

SPL
source="WinEventLog:Microsoft-Windows-Sysmon/Operational" EventCode=1 ParentImage IN ("*\\winword.exe", "*\\excel.exe", "*\\powerpnt.exe", "*\\outlook.exe") Image IN ("*\\cmd.exe", "*\\powershell.exe", "*\\pwsh.exe", "*\\wscript.exe", "*\\cscript.exe", "*\\mshta.exe")

KQL for Microsoft Defender's advanced hunting table:

KQL
DeviceProcessEvents
| where (InitiatingProcessFolderPath endswith "\\winword.exe" or InitiatingProcessFolderPath endswith "\\excel.exe" or InitiatingProcessFolderPath endswith "\\powerpnt.exe" or InitiatingProcessFolderPath endswith "\\outlook.exe") and (FolderPath endswith "\\cmd.exe" or FolderPath endswith "\\powershell.exe" or FolderPath endswith "\\pwsh.exe" or FolderPath endswith "\\wscript.exe" or FolderPath endswith "\\cscript.exe" or FolderPath endswith "\\mshta.exe")

And KQL for Windows security events (event 4688) in a Sentinel workspace:

KQL
SecurityEvent
| where (ParentProcessName endswith "\\winword.exe" or ParentProcessName endswith "\\excel.exe" or ParentProcessName endswith "\\powerpnt.exe" or ParentProcessName endswith "\\outlook.exe") and (NewProcessName endswith "\\cmd.exe" or NewProcessName endswith "\\powershell.exe" or NewProcessName endswith "\\pwsh.exe" or NewProcessName endswith "\\wscript.exe" or NewProcessName endswith "\\cscript.exe" or NewProcessName endswith "\\mshta.exe")

These three were generated on 2026-11-22 with sigma-cli 3.1.0, pySigma 1.5.1, the Splunk backend 2.1.0 and the Kusto backend 1.0.1. Read all of them. The same rule produced three different queries against three different data sources, which is the point of the pipelines, and the converter's output is a starting point:

  • The last query has no EventID == 4688 condition. The fields exist only on process-creation events, so it works, but a careful engineer adds the filter by hand.
  • It assumes a source name or table that may not match your environment.
  • A converter cannot know how big your data is. Check the search's cost and speed before you schedule it.

Counting things: correlation rules#

Plain Sigma matches single events. For "many of X in a window" the Sigma project added correlation rules: a second rule that counts or orders the matches of a first one. Here the first rule matches failed logons (Windows event 4625) and the second counts distinct accounts per source:

YAML
title: Failed Windows logon
name: failed_windows_logon
id: d91b5c36-7e28-4a4f-8c03-2f6e1b9a4d75
status: experimental
description: A failed logon (Windows Security event 4625). The building block for the correlation rule below.
author: SecOps RoadMaps
date: 2026-11-22
logsource:
    product: windows
    service: security
detection:
    selection:
        EventID: 4625
    condition: selection
level: informational
---
title: Many accounts fail to log on from one source
id: 62e0f8a4-1b9d-4c37-a5d6-8e3c7f1b0a92
status: experimental
description: >
    Ten or more different accounts fail to log on from the same source within fifteen minutes,
    the pattern of a password spray.
references:
    - https://attack.mitre.org/techniques/T1110/003/
author: SecOps RoadMaps
date: 2026-11-22
tags:
    - attack.credential-access
    - attack.t1110.003
correlation:
    type: value_count
    rules:
        - failed_windows_logon
    group-by:
        - IpAddress
    timespan: 15m
    condition:
        gte: 10
        field: TargetUserName
falsepositives:
    - A shared gateway or VPN concentrator that fronts many users
    - A misconfigured service retrying with stale credentials
level: medium

The Splunk backend converts it to:

SPL
source="WinEventLog:Security" EventCode=4625

| bin _time span=15m
| stats dc(TargetUserName) as value_count by _time IpAddress

| search value_count >= 10

The Kusto backend, in the version tested, answered "Backend does not support correlation rules." That is normal: backends differ in what they support, so always check. Here is a hand-written KQL equivalent:

KQL
SecurityEvent
| where TimeGenerated > ago(1h) and EventID == 4625
| summarize distinct_accounts = dcount(TargetUserName) by IpAddress, bin(TimeGenerated, 15m)
| where distinct_accounts >= 10

Illustrative: test it in your lab before relying on it. Both versions group events into fixed fifteen-minute windows, so a burst that straddles a boundary can be split in two and missed. Sliding windows avoid that, at a higher cost: streamstats in SPL, and a self-join or serialize with prev() in KQL.

Installing the tools, and a warning about versions#

Shell
python -m venv sigma-env
sigma-env/Scripts/pip install "pysigma<2" "sigma-cli<4" pysigma-backend-splunk pysigma-backend-kusto pysigma-pipeline-sysmon pysigma-pipeline-windows
sigma-env/Scripts/sigma check .
sigma-env/Scripts/sigma convert -t splunk -p sysmon -p splunk_windows office_shell.yml
sigma-env/Scripts/sigma convert -t kusto -p microsoft_xdr office_shell.yml
sigma-env/Scripts/sigma convert -t kusto -p azure_monitor office_shell.yml

On Linux and macOS replace Scripts with bin. The pins matter: as of 2026-11-22 a newer pySigma (2.x) was available, but the Kusto backend failed to load under it and took the whole command line down with it. Check the project documentation (linked below) for current guidance before you copy these pins. This is a good habit in general: pin versions, record the date, and expect tooling to move.

Do this#

  1. Translate a requirement (1 to 2 hours). In your lab, write "accounts with more than five failed logons in ten minutes" in both SPL and KQL (use whichever you have data for, and the other from the table). Generate a few failures by mistyping a lab account's password, and check that your query finds them.
  2. Convert a real rule (2 hours). Pick a process-creation rule from the SigmaHQ repository. Convert it for your SIEM with sigma convert. Read the output line by line and list what the converter assumed: the source or table, the field names, anything it dropped or warned about.
  3. Write a Sigma rule from scratch (2 to 3 hours). Detect a command line that deletes Windows shadow copies, a step that often comes before ransomware (look up the technique and copy its ID from the live ATT&CK site). Run sigma check, convert it, run the result in your lab, and note the false positives you would expect.
  4. Cut the noise (1 hour). Add a filter_ selection to the Office rule that excludes one known-good parent, change the condition to ... and not filter_, convert again, and compare the two outputs.

Resources#

Free first

Paid or needs an account

  • Splunk Fundamentals 1 above needs a free account. Nothing paid is required for this stage.

Checkpoint#

You are ready to move on when you can:

Go deeper (optional)#

  • Read the specification pages for modifiers and conditions, and find a SigmaHQ rule that uses all or 1 of.
  • Rewrite your threshold detection with a sliding window and compare the cost of the two versions.

0 / 6 checkpoints ticked

View this page on GitHub