Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 37 ยท Defence and Detection

Writing Detection Signatures

Learn how detection rules are structured in Suricata, Snort and Sigma, and write a small signature engine in Python to test rules against sample events.

A signature is a rule that says "if you see this, raise an alert". Intrusion detection systems such as Suricata and Snort match packets; log platforms use rule formats such as Sigma to match events. Writing good rules is the defender's counterpart to using Metasploit: the same lab that teaches an attack should end with a rule that detects it.

This lesson shows rule anatomy, then builds a toy engine so you can feel how matching, thresholds and false positives work. Rules are shown in read-only blocks; the engine runs in your browser on invented events.

Anatomy of a network rule

Suricata and Snort share a syntax: an action, a protocol, addresses and ports, then options in parentheses.

TEXT
alert tcp $HOME_NET any -> $EXTERNAL_NET 4444 (
    msg:"LAB Outbound connection to port 4444";
    flow:established,to_server;
    threshold:type limit, track by_src, count 1, seconds 60;
    classtype:trojan-activity;
    sid:1000001; rev:1;
)
  • Header: alert tcp $HOME_NET any -> $EXTERNAL_NET 4444 is the action, protocol, source, direction and destination.
  • msg: the human-readable alert text.
  • flow: conditions on the connection state and direction.
  • threshold: limit the alert rate so one noisy host does not flood the console.
  • sid/rev: unique id and revision; local rules use ids from 1,000,000 upward.

Content matches use content:"..." with modifiers for position, case and protocol buffers (http.uri, http.header). Good rules combine more than one condition instead of one short string.

Rules for logs: Sigma

Sigma is a vendor-neutral YAML format for log detections that converts to many SIEM query languages:

YAML
title: Suspicious PowerShell With Encoded Command
status: experimental
logsource:
    category: process_creation
    product: windows
detection:
    selection:
        Image|endswith: '\powershell.exe'
        CommandLine|contains: '-enc'
    condition: selection
falsepositives:
    - Administrative scripts that use encoded commands
level: medium

Notice the falsepositives field. A rule without a note about its false positives is unfinished.

A toy signature engine

The engine below keeps rules as data (field, operator, value) and evaluates them against event dictionaries, much as a SIEM does after parsing logs.

Python Evaluate rules against sample events

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Event 5 (bash started by sshd) correctly raises nothing, because rule 3 needs a web server as the parent. That "parent" condition is what turns a noisy rule into a useful one.

Thresholds and tuning

Many attacks only show up as volume. A threshold rule counts events per key in a time window. This stand-in groups failed logins by source over a sliding window.

Python A sliding-window threshold

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The rule lifecycle

  1. Hypothesis. "Exploits against our web server will show as web processes spawning shells."
  2. Lab test. Reproduce the behaviour in a lab you own and capture the evidence.
  3. Write. The narrowest rule that still catches it; add a threshold if needed.
  4. Test for false positives against normal traffic and logs from production-like data.
  5. Deploy in alert-only mode, review, then tune.
  6. Maintain. Review rules regularly; attackers adapt and software changes.

Version your rules in git with a short note on why each exists and how it was tested. A rule nobody understands gets disabled the first time it is noisy.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a Suricata rule's sid identify?

    sid is the unique rule id; local rules conventionally start at 1,000,000.
  2. 2. What is Sigma?

    Sigma rules convert to the query languages of many SIEM products.
  3. 3. Why add a parent-process condition to the 'shell started' rule?

    Requiring a web-server parent keeps only the suspicious case.
  4. 4. What should happen before a new rule is set to block?

    Test in alert-only mode first so a bad rule does not disrupt real traffic.

Summary

  • Network rules combine a header and options; log rules (Sigma) match event fields.
  • The best rules combine several conditions and document their false positives.
  • Thresholds detect volume-based behaviour; windows keep them accurate.
  • Follow a lifecycle: hypothesis, lab test, write, test, deploy quietly, maintain.

Next: Hardening Against Exploitation