βThe power of Snort lies in its rules β the instructions that tell it what to look for.β
In Module 3, we installed Snort and configured it. But a Snort without rules is like a security guard without instructions β it doesn't know what to look for.
Rules are the heart of Snort. They tell Snort what patterns to watch for, what to do when it finds them, and how to react. Writing good rules is a key skill for any Snort user.
In this module, we will learn the anatomy of a Snort rule, how to read and write rules, and how to use them to detect attacks. We'll start with simple rules and gradually build up to more complex ones.
Let's unlock the power of Snort rules! π
By the end of this module, you will be able to:
content, sid, and rev.In a busy office building in Lagos, Nigeria, there was a security guard named Chidi. Chidi was very observant. He had a notebook where he wrote down descriptions of suspicious people and activities.
His notebook had rules like:
Chidi's notebook was like Snort's rules. It told him exactly what to look for and what to do. Because of his rules, he caught many intruders and kept the building safe.
Snort's rules work the same way. They are instructions that describe what to look for and what action to take.
Definition: A Snort rule is a set of instructions that tells Snort what to look for in network traffic and what to do when it finds it.
Why it matters: Rules are what make Snort useful. Without rules, Snort is just a traffic monitor. With rules, it becomes a detective.
Simple explanation: Think of a rule as a "Wanted Poster". It has a description of the bad person (pattern) and instructions on what to do when you see them (action).
+-----------------------------------+ | RULE = INSTRUCTION + ACTION | | "If you see THIS, do THAT" | +-----------------------------------+
π Mini summary: A Snort rule tells Snort what to detect and what to do about it.
A Snort rule has two main parts:
RULE STRUCTURE: [ACTION] [PROTOCOL] [SOURCE IP] [SOURCE PORT] [DIRECTION] [DEST IP] [DEST PORT] ( [OPTIONS] )
π Mini summary: A rule has a header (action and addresses) and options (specific detection details).
Definition: The action tells Snort what to do when the rule matches.
Common actions:
| Action | What it does |
|---|---|
| alert | Generate an alert and log the packet |
| log | Log the packet (no alert) |
| pass | Ignore the traffic (allow it) |
| drop | Block the traffic and log it (IPS mode) |
| reject | Block and send a rejection message |
Simple explanation: If the action is "alert", it's like saying "shout loudly when you see this". If it's "drop", it's like "stop this person from entering".
alert tcp 192.168.1.0/24 any -> any any ( ... ) ^^^^^ action
π Mini summary: The action tells Snort what to do when a rule matches.
The rule header also specifies:
-> means from source to destination, <> means either way).Real-life example: "Alert on any TCP traffic coming from the school network (192.168.1.0/24) to anywhere."
alert tcp 192.168.1.0/24 any -> any any ^prot. ^source ^dir ^dest
π Mini summary: The header defines which traffic the rule applies to.
Definition: Rule options are the specific conditions that traffic must meet to match the rule. They are inside parentheses ( ).
Why they matter: The header narrows down the traffic, but options allow you to be very precise about what you're looking for.
Fun example: If the header is like "look for cars", the options are like "look for red cars with black wheels and a sunroof".
π Mini summary: Rule options provide the detailed detection criteria.
content Option β Looking for PatternsDefinition: The content option tells Snort to look for a specific string (text pattern) in the packet payload.
Why it's powerful: Many attacks contain specific words or patterns. For example, an SQL injection attack might contain the word "SELECT" or "UNION".
School example: It's like a teacher looking for the word "cheat" in a student's paper.
alert tcp any any -> any any (content:"SELECT"; sid:1000001;) ^look for the word "SELECT" in the traffic
π Mini summary: content looks for specific text patterns in the traffic.
sid and rev OptionsDefinition: sid (Signature ID) is a unique number that identifies the rule. rev (Revision) is the version number of the rule.
Why they matter: sid helps you refer to rules (for logging, disabling, etc.). rev helps you track changes.
Simple explanation: sid is like a student ID number β it uniquely identifies each rule.
alert tcp any any -> any any (content:"SELECT"; sid:1000001; rev:1;) ^sid and rev help identify and track rules
π Mini summary: sid is the unique ID for a rule; rev is the version number.
msg Option β Describing the RuleDefinition: The msg option provides a human-readable description of what the rule detects.
Why it matters: When an alert is generated, the msg tells you what the alert is about.
Fun example: It's like a label on a jar β "This jar contains cookies" so you know what's inside.
alert tcp any any -> any any (content:"SELECT"; msg:"SQL injection attempt detected"; sid:1000001;) ^msg describes the alert
π Mini summary: msg gives a description of the alert.
offset and depth OptionsDefinition: offset tells Snort where to start searching in the payload. depth tells Snort how far to search.
Why they matter: They make rule matching more efficient and precise.
Home example: If you're looking for your keys, you might start searching on the table (offset) and stop after looking for 2 minutes (depth).
content:"SELECT"; offset:0; depth:100; ^start at beginning, search first 100 bytes
π Mini summary: offset and depth control where Snort looks.
pcre Option β Regular ExpressionsDefinition: pcre (Perl Compatible Regular Expressions) allows you to use complex patterns to match traffic.
Why it's powerful: It's like content on steroids β you can match patterns, not just exact strings.
School example: It's like searching for any word that starts with "S" and ends with "t" β a pattern, not a specific word.
content:"SELECT"; pcre:"/SELECT\s+.*FROM/i"; ^matches "SELECT" followed by spaces and "FROM"
π Mini summary: pcre uses regular expressions for powerful pattern matching.
classtype OptionDefinition: classtype categorises the type of attack being detected (e.g., "attempted-dos", "web-application-attack").
Why it matters: It helps prioritise and organise alerts.
classtype:attempted-dos; ^this rule detects a denial-of-service attempt
π Mini summary: classtype categorises the alert.
Let's write a simple rule together:
alert tcp any any -> any any (content:"admin"; msg:"Admin string detected"; sid:1000001; rev:1;)
π Mini summary: Write rules by choosing an action, protocol, addresses, and options.
Definition: Testing a rule means making sure it works as expected β it alerts when it should and doesn't alert when it shouldn't.
How to test:
-T (test configuration) option.TEST COMMAND: snort -T -c /usr/local/snort/etc/snort.conf
π Mini summary: Test your rules to ensure they work correctly.
Rules can slow down Snort if they are not written efficiently. Here are some tips:
content as early as possible in the rule.offset and depth to limit the search.pcre patterns.Simple explanation: It's like finding a book in a library β you use the index (content) to find the right shelf (offset/depth) quickly.
π Mini summary: Write rules efficiently to maintain Snort's performance.
You don't have to write all rules from scratch. There are many sources of pre-written rules:
π Mini summary: Use pre-written rules from trusted sources to save time.
touch /usr/local/snort/etc/rules/local.rulesinclude $RULE_PATH/local.rulessnort -T -c /usr/local/snort/etc/snort.confsnort -A console -c /usr/local/snort/etc/snort.confcurl or ping to send traffic that should trigger the rule.CREATE β ADD β INCLUDE β TEST β RUN β GENERATE β CHECK
Tip: Have students write a few simple rules and test them in a lab environment. Use a tool like nping to generate test traffic. Emphasise the importance of the sid and rev fields for rule management.
The Snort rule language is so popular that it has been adopted by other tools like Suricata and Zeek. Learning Snort rules gives you a skill that applies to many other security tools!
content option is the most commonly used detection method.sid and rev in your rules.sid β rules must have a unique SID.pcre patterns without need β they can slow down Snort.msg to describe the alert.offset and depth to improve performance.+---------------------------------------------------+ | SNORT RULE | +---------------------------------------------------+ | HEADER | | [ACTION] [PROTOCOL] [SRC IP] [SRC PORT] -> | | [DEST IP] [DEST PORT] | +---------------------------------------------------+ | OPTIONS (inside parentheses) | | (content:"pattern"; msg:"description"; sid:ID;) | +---------------------------------------------------+
| Option | Purpose | Example |
|---|---|---|
| content | Search for a string | content:"admin"; |
| msg | Alert description | msg:"Admin detected"; |
| sid | Unique ID | sid:1000001; |
| rev | Revision number | rev:1; |
| offset | Start position | offset:0; |
| depth | Search limit | depth:100; |
| pcre | Regular expression | pcre:"/SELECT.*FROM/i"; |
| classtype | Attack category | classtype:attempted-dos; |
+-------------------+-------------------+ | Action | Description | +-------------------+-------------------+ | alert | Alert + log | | log | Log only | | pass | Ignore | | drop | Block (IPS) | | reject | Block + reject | +-------------------+-------------------+
We included mini summaries after each lesson. Let's now wrap up the entire module.
In this module, we explored the heart of Snort β its rules. We learned the structure of a rule: the header (action, protocol, addresses) and the options (detection details). We covered common options like content, msg, sid, rev, offset, depth, pcre, and classtype. We also learned how to write, test, and deploy rules effectively.
Rules are what make Snort a powerful IDS/IPS. With practice, you'll be able to write rules to detect a wide range of threats. In the next module, we'll dive deeper into preprocessors and how they work with rules.
alert action do? It generates an alert and logs the packet.content option used for? To look for a specific string in the packet payload.sid important? It uniquely identifies the rule.msg option? It provides a description of the alert.offset do? It tells Snort where to start searching.alert and drop? alert alerts, drop alerts and blocks.snort -T to test configuration, and generate test traffic to trigger the rule.alert action do?content option do?sid option?msg option?offset do?depth do?pcre option used for?classtype option?alert and drop?alert action generates an __________. (alert)content option searches for a __________ in the payload. (string/pattern)sid is a unique __________ for each rule. (ID/number)msg option provides a __________ of the alert. (description)sid. (True)content option is case-sensitive by default. (True)drop action only works in IDS mode. (False β it's for IPS mode)pcre for complex pattern matching. (True)sid stand for? offset do? depth do? classtype? rev stand for? Match the option with its purpose:
| Option | Purpose |
|---|---|
| 1. content | A. Unique rule ID |
| 2. msg | B. Search for a string |
| 3. sid | C. Rule description |
| 4. offset | D. Start position for search |
| 5. classtype | E. Attack category |
Answers: 1-B, 2-C, 3-A, 4-D, 5-E
content option?Scenario: You are managing a school network. You want to detect any attempt to access adult content (like websites containing the word "adult" or "xxx").
In groups, write three rules to detect different types of attacks (e.g., SQL injection, port scan, malware callback). Include msg, sid, and rev. Test your rules in a lab environment (or describe how you would test them). Present your rules to the class.
Write a rule that detects an attempt to use the word "password" in plain text in traffic. Explain what the rule does and how you would test it.
Create a rule set (at least 5 rules) to protect a small business network. Include rules for: detecting port scans, SQL injection, and malware. Write each rule with proper sid, msg, and other relevant options. Write a brief explanation of what each rule does.
Install Snort (if not already installed). Add at least 3 custom rules to your local.rules file. Test the rules by generating traffic (you can use nping or a web browser). Take screenshots of the alerts generated. Submit a report with your rules, test steps, and screenshots.
Write a rule that detects a specific vulnerability like "Heartbleed" or "Shellshock". Research the vulnerability's traffic patterns, then write a rule to detect it. Explain how your rule works and how you would test it.
content option is used to search for patterns.sid and rev in your rules.In Module 5, we will explore Preprocessors and Packet Decoding. We'll learn how Snort processes traffic before the detection engine kicks in. Preprocessors are essential for handling fragmented packets, reassembling streams, and normalising traffic. Get ready to dive deeper into Snort's internal workings!
See you in Module 5!
π End of Module 4 β Snort Rules and Detection π