Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 37 ยท IDS/IPS and Monitoring

Snort and Suricata Rules

Learn the structure of Snort and Suricata IDS rules: header, options, content matches, flow and thresholds, with a runnable rule parser and safe defensive examples.

Snort and Suricata are the two best-known open-source network IDS/IPS engines. They share a rule language so similar that most simple Snort 2 rules run unchanged in Suricata. If you can read this language, you can understand the detection logic of most commercial products too, since many borrow its ideas.

This lesson teaches how a rule is built, what the common keywords mean, and how to write and test your own defensive rules. We cannot run Snort or Suricata on this site, so the rules in code fences are read-only examples, while the Python programs are labelled stand-ins that parse and evaluate a simplified version of the language.

Rules and sample output here are illustrative. Real sensors need a mirrored interface or inline setup, privileges and a rule set you tune to your own network. Test rules only on traffic you are allowed to inspect.

Rule anatomy

A rule has a header (who and where) and options (what to look for), written in parentheses.

TEXT
alert tcp $EXTERNAL_NET any -> $HOME_NET 80 (msg:"WEB suspicious path traversal"; flow:to_server,established; content:"../"; http_uri; classtype:web-application-attack; sid:1000001; rev:1;)

Read the header left to right:

Part Example Meaning
Action alert What to do on a match: alert, drop, reject, pass, log
Protocol tcp tcp, udp, icmp, ip, or application protocols such as http, dns, tls in Suricata
Source $EXTERNAL_NET any Address and port the traffic comes from
Direction -> One way (<> means both ways)
Destination $HOME_NET 80 Address and port it goes to

$HOME_NET and $EXTERNAL_NET are variables you set in the configuration to describe your own address ranges. Getting HOME_NET wrong is one of the most common reasons rules never fire.

Common options

  • msg is the text shown in the alert. Make it descriptive, with a category prefix.
  • content matches literal bytes or text. nocase ignores case. Modifiers such as http_uri, http_header, http_client_body (Snort) or sticky buffers like http.uri; (Suricata) restrict where the match happens.
  • pcre matches a regular expression, more powerful and slower. Use it after a cheap content so the engine only runs it on likely packets.
  • flow constrains the connection state and direction: established, to_server, to_client.
  • flags, dsize, itype and similar keywords test packet fields.
  • threshold / detection_filter fire only when a rate is exceeded, for example 10 hits in 60 seconds from one source.
  • classtype and priority classify the alert; reference links to a CVE; sid and rev are the unique rule id and its revision (ids from 1,000,000 up are reserved for local rules).

A few more defensive examples, again read-only:

TEXT
# Repeated failed SSH logins from one source: a possible password-guessing run
alert tcp $EXTERNAL_NET any -> $HOME_NET 22 (msg:"SSH many connections in a minute"; flow:to_server; flags:S; threshold:type both, track by_src, count 10, seconds 60; classtype:attempted-recon; sid:1000002; rev:1;)

# Outdated, clear-text FTP credentials on the network
alert tcp any any -> $HOME_NET 21 (msg:"FTP clear-text login attempt"; flow:to_server,established; content:"USER "; depth:5; classtype:policy-violation; sid:1000003; rev:1;)

# Suricata: a DNS query for a domain on your internal block list
alert dns any any -> any any (msg:"DNS query for blocked domain"; dns.query; content:"bad.example"; nocase; classtype:trojan-activity; sid:1000004; rev:1;)

depth:5 tells the engine to look only in the first five bytes of the payload, which is faster and more accurate than searching the whole packet.

Where an alert appears

A Suricata alert is written as one JSON object (the "eve.json" log). The values below are read-only and made up:

JSON
{"timestamp":"2026-03-14T10:22:41.512+0000","event_type":"alert","src_ip":"203.0.113.50","src_port":51022,"dest_ip":"192.0.2.10","dest_port":80,"proto":"TCP","alert":{"action":"allowed","gid":1,"signature_id":1000001,"rev":1,"signature":"WEB suspicious path traversal","category":"Web Application Attack","severity":2}}

Notice "action":"allowed". In alert-only mode the packet was delivered; with drop it would read blocked. These JSON lines are what your SIEM ingests.

A rule parser and matcher stand-in

To really understand the language, build a miniature version. The program below parses a simplified rule (header plus msg, content, nocase, sid options) and evaluates it against sample packets represented as dictionaries.

Python Parse and run simplified rules

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.

This is the essence of the engine: parse once, then test each packet against every rule. Real engines are far faster because they organize rules into groups by protocol and port and use multi-pattern algorithms such as Aho-Corasick to test thousands of strings in a single pass.

Thresholds and rates

A rule that fires on a single failed login would flood you. Rate-based options count events. The stand-in below mimics threshold: track by_src, count 5, seconds 60 over a list of timestamped events.

Python Threshold: N events per source per window

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 slow source never accumulates five events in a minute and stays quiet; the burst source trips the threshold once (suppressed afterwards, like Snort's type limit).

Managing rules in practice

  • Rule sources. Community rule sets, vendor sets and your own local rules. Update them on a schedule (tools such as suricata-update handle downloads and enabling).
  • Disable what you do not need. Rules for products you do not run only add load and noise.
  • Test before blocking. Run Suricata offline on a recorded capture and read the alerts (suricata -r capture.pcap) before enabling drop.
  • Version your local rules in source control; rev counts changes.
  • Measure performance. Rules without a good fast pattern are expensive. The engine can print per-rule profiling to find them.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In the rule header 'alert tcp $EXTERNAL_NET any -> $HOME_NET 80', what does the arrow mean?

    '->' is one directional: source on the left, destination on the right. '<>' would mean both.
  2. 2. Why use 'depth:5' with content:"USER "?

    depth restricts how far into the payload the engine searches.
  3. 3. What does a threshold option achieve?

    Thresholds like count 10, seconds 60 turn single events into rate-based detection.
  4. 4. Which option uniquely identifies a rule and should be at 1,000,000 or above for local rules?

    sid is the rule id; the range from 1,000,000 up is reserved for local rules.
  5. 5. What is the safe way to introduce a new blocking rule?

    Observing alerts first reveals false positives before they can drop legitimate traffic.

Summary

  • A rule is a header (action, protocol, addresses, ports, direction) plus options in parentheses.
  • Key options: msg, content with buffer modifiers, pcre, flow, threshold, classtype, sid, rev.
  • Put a cheap content before a pcre, and restrict where matches happen.
  • Alerts are written as structured logs (Suricata eve.json) that feed a SIEM.
  • Parse once, match many: engines group rules and use multi-pattern search for speed.
  • Tune, test on recorded traffic and version your local rules.

Next: Log Analysis and SIEM