Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
← Certified Snort User Β· Lesson 6 of 10

Module Four

πŸ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
6
Module 4: Snort Rules and Detection

πŸ“œ Module 4: Snort Rules and Detection

β€œThe power of Snort lies in its rules – the instructions that tell it what to look for.”

πŸ“– Module Introduction

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! πŸ”‘

🎯 Learning Objectives

By the end of this module, you will be able to:

  • βœ”οΈ Understand the structure of a Snort rule.
  • βœ”οΈ Explain the difference between the rule header and rule options.
  • βœ”οΈ Write a simple custom rule.
  • βœ”οΈ Use common rule options like content, sid, and rev.
  • βœ”οΈ Understand how rules are applied to traffic.
  • βœ”οΈ Test and debug rules.
  • βœ”οΈ Recognise the importance of rule ordering and performance.

πŸ“š Warm-up Story: The Security Guard's Notebook

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:

  • "If a person tries to enter without an ID, stop them."
  • "If someone is carrying a large bag after midnight, question them."
  • "If someone is wearing a mask and hoodie, watch them closely."

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.

πŸ“˜ Main Lessons

Lesson 1: What is a Snort Rule?

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.


Lesson 2: The Anatomy of a Snort Rule

A Snort rule has two main parts:

  1. Rule Header: The first part – it tells Snort the action to take, the protocol, and the source and destination addresses.
  2. Rule Options: The second part (in parentheses) – it provides more specific details about what to look for.
   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).


Lesson 3: Rule Header – Action

Definition: The action tells Snort what to do when the rule matches.

Common actions:

ActionWhat it does
alertGenerate an alert and log the packet
logLog the packet (no alert)
passIgnore the traffic (allow it)
dropBlock the traffic and log it (IPS mode)
rejectBlock 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.


Lesson 4: Rule Header – Protocol, Addresses, and Ports

The rule header also specifies:

  • Protocol: The network protocol (like tcp, udp, icmp, ip).
  • Source IP and Port: Where the traffic comes from.
  • Direction: Which way the traffic is going (-> means from source to destination, <> means either way).
  • Destination IP and Port: Where the traffic is going.

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.


Lesson 5: Rule Options – The Detection Details

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.


Lesson 6: The content Option – Looking for Patterns

Definition: 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.


Lesson 7: The sid and rev Options

Definition: 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.


Lesson 8: The msg Option – Describing the Rule

Definition: 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.


Lesson 9: The offset and depth Options

Definition: 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.


Lesson 10: The pcre Option – Regular Expressions

Definition: 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.


Lesson 11: The classtype Option

Definition: 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.


Lesson 12: Writing a Simple Rule – Step by Step

Let's write a simple rule together:

  1. Action: We want to "alert" on this.
  2. Protocol: We're looking at TCP traffic.
  3. Addresses: We want to check all traffic (any source, any destination).
  4. Options: We'll look for the word "admin" in the traffic.
   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.


Lesson 13: Testing Your Rules

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:

  1. Place the rule in a file (e.g., local.rules).
  2. Include the file in snort.conf.
  3. Run Snort with the -T (test configuration) option.
  4. Generate traffic that should trigger the rule (e.g., send a packet containing "admin").
  5. Check if Snort generates an alert.
   TEST COMMAND:
   snort -T -c /usr/local/snort/etc/snort.conf

πŸ“Œ Mini summary: Test your rules to ensure they work correctly.


Lesson 14: Rule Performance – Being Efficient

Rules can slow down Snort if they are not written efficiently. Here are some tips:

  • Use content as early as possible in the rule.
  • Use offset and depth to limit the search.
  • Avoid using too many complex pcre patterns.
  • Order rules from most specific to least specific.

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.


Lesson 15: Where to Find Rules

You don't have to write all rules from scratch. There are many sources of pre-written rules:

  • Snort Community Rules: Free rules provided by the Snort community.
  • Snort Subscriber Rules: Paid rules with more coverage.
  • Emerging Threats: A popular open-source rule set.
  • Custom Rules: Rules you write for your specific needs.

πŸ“Œ Mini summary: Use pre-written rules from trusted sources to save time.


πŸ“ Key Vocabulary (simple definitions)

  • Rule: An instruction that tells Snort what to detect.
  • Rule Header: The first part of a rule – action, protocol, addresses.
  • Rule Options: The second part – detailed detection conditions.
  • Action: What to do when a rule matches (alert, log, drop, etc.).
  • content: A pattern to search for in the traffic.
  • sid: Signature ID – a unique number for each rule.
  • rev: Revision number – tracks rule changes.
  • msg: A human-readable description of the rule.
  • pcre: Regular expression matching.
  • classtype: Categorises the attack type.

🧠 Important Concepts

  • Rule Ordering: Rules are evaluated in order. Place more specific rules earlier.
  • Fast Pattern Matcher: Snort uses an efficient engine to match content patterns quickly.
  • False Positives: When a rule matches traffic that is not actually malicious.
  • False Negatives: When a rule fails to match malicious traffic.

πŸͺœ Step-by-step: Writing and Testing a Custom Rule

  1. Create a rule file: touch /usr/local/snort/etc/rules/local.rules
  2. Add your rule: Write a rule like the one above.
  3. Include the file: In snort.conf, add include $RULE_PATH/local.rules
  4. Test configuration: snort -T -c /usr/local/snort/etc/snort.conf
  5. Run Snort: snort -A console -c /usr/local/snort/etc/snort.conf
  6. Generate test traffic: Use a tool like curl or ping to send traffic that should trigger the rule.
  7. Check output: See if Snort generates the alert.
   CREATE β†’ ADD β†’ INCLUDE β†’ TEST β†’ RUN β†’ GENERATE β†’ CHECK

🌍 Real-life Examples

  • SQL Injection: A rule looking for "SELECT" or "UNION" in web traffic to detect SQL injection attacks.
  • Port Scan: A rule that detects when someone is scanning many ports on a server.
  • Malware Callback: A rule that looks for a known command-and-control server address.

πŸ‡³πŸ‡¬ Nigerian Examples

  • Bank Security: A Nigerian bank uses a rule to detect "SELECT" in traffic to its online banking portal to prevent SQL injection.
  • School Network: A school in Abuja writes a rule to detect "porn" or "gambling" in web traffic to block inappropriate content.
  • Telecom Company: A telecom in Lagos uses rules to detect scanning attempts on its network infrastructure.

🎈 Fun Examples children can relate to

  • Lemonade stand: A rule: "If someone tries to take lemonade without paying, alert the owner."
  • Classroom: A rule: "If a student uses the word 'homework' more than 5 times in a sentence, alert the teacher."

🏠 Everyday Examples

  • Home: A rule on your home network: "If someone tries to access your bank website from an unknown device, alert you."
  • Market: A rule: "If someone keeps checking the same stall without buying, alert the trader."

πŸ‘©β€πŸ« Teacher Notes

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.

πŸ‘¨β€πŸ‘©β€πŸ‘¦ Parent Tips

  • Explain to your child that rules are like safety instructions – they help keep the network safe.
  • Encourage them to think about what patterns they would look for to detect something wrong.

πŸ€“ Interesting Facts

  • The Snort community rule set contains over 30,000 rules.
  • Some rules are designed to detect very specific attacks, like the "Heartbleed" vulnerability.
  • Rules are updated regularly to keep up with new threats.

πŸ’‘ Did You Know?

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!

πŸ”” Remember This

  • A Snort rule has a header and options.
  • The action (alert, log, drop) tells Snort what to do.
  • The content option is the most commonly used detection method.
  • Always include sid and rev in your rules.
  • Test your rules thoroughly.
  • Use pre-written rules from trusted sources to save time.

⚠️ Common Mistakes

  • Mistake: Forgetting the sid – rules must have a unique SID.
  • Mistake: Using too broad rules – they can cause many false positives.
  • Mistake: Not testing rules – always test before deploying.
  • Mistake: Using complex pcre patterns without need – they can slow down Snort.

βœ… Best Practices

  • Always include a msg to describe the alert.
  • Use offset and depth to improve performance.
  • Test rules in a lab before deploying to production.
  • Keep your rules up to date – update them regularly.
  • Review and refine rules based on false positives.

πŸ“Š ASCII Illustrations & Tables

Rule Structure Diagram

   +---------------------------------------------------+
   |                  SNORT RULE                       |
   +---------------------------------------------------+
   |  HEADER                                           |
   |  [ACTION] [PROTOCOL] [SRC IP] [SRC PORT] ->       |
   |  [DEST IP] [DEST PORT]                            |
   +---------------------------------------------------+
   |  OPTIONS (inside parentheses)                      |
   |  (content:"pattern"; msg:"description"; sid:ID;)   |
   +---------------------------------------------------+

Common Rule Options

OptionPurposeExample
contentSearch for a stringcontent:"admin";
msgAlert descriptionmsg:"Admin detected";
sidUnique IDsid:1000001;
revRevision numberrev:1;
offsetStart positionoffset:0;
depthSearch limitdepth:100;
pcreRegular expressionpcre:"/SELECT.*FROM/i";
classtypeAttack categoryclasstype:attempted-dos;

Rule Actions

   +-------------------+-------------------+
   |  Action           |  Description      |
   +-------------------+-------------------+
   |  alert            |  Alert + log      |
   |  log              |  Log only         |
   |  pass             |  Ignore           |
   |  drop             |  Block (IPS)      |
   |  reject           |  Block + reject   |
   +-------------------+-------------------+

πŸ“Œ Summary after every lesson

We included mini summaries after each lesson. Let's now wrap up the entire module.

πŸ“˜ End-of-Module Summary

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.

❓ Frequently Asked Questions (10)

1. What is a Snort rule? A set of instructions that tells Snort what to look for in network traffic.
2. What are the two main parts of a rule? The header and the options.
3. What does the alert action do? It generates an alert and logs the packet.
4. What is the content option used for? To look for a specific string in the packet payload.
5. Why is the sid important? It uniquely identifies the rule.
6. What is the msg option? It provides a description of the alert.
7. What does offset do? It tells Snort where to start searching.
8. What is the difference between alert and drop? alert alerts, drop alerts and blocks.
9. What are pre-written rules? Rules created by the community or security vendors.
10. How can I test my rules? Use snort -T to test configuration, and generate test traffic to trigger the rule.

πŸ“ Review Questions (15)

  1. What is a Snort rule?
  2. What are the two main parts of a rule?
  3. What does the alert action do?
  4. What does the content option do?
  5. What is the purpose of the sid option?
  6. What is the purpose of the msg option?
  7. What does offset do?
  8. What does depth do?
  9. What is the pcre option used for?
  10. What is the classtype option?
  11. What is the difference between alert and drop?
  12. Why is rule ordering important?
  13. What are some sources of pre-written rules?
  14. How do you test a rule?
  15. What is a false positive?

✏️ Fill-in-the-Blank Exercises

  1. A Snort rule has a __________ and __________. (header, options)
  2. The alert action generates an __________. (alert)
  3. The content option searches for a __________ in the payload. (string/pattern)
  4. The sid is a unique __________ for each rule. (ID/number)
  5. The msg option provides a __________ of the alert. (description)

βœ… True or False Exercises

  1. A Snort rule must always have a sid. (True)
  2. The content option is case-sensitive by default. (True)
  3. The drop action only works in IDS mode. (False – it's for IPS mode)
  4. You can use pcre for complex pattern matching. (True)
  5. Rules are only useful if you write them yourself. (False – you can use pre-written rules)

πŸ”˜ Multiple Choice Questions (15) with Answers

  1. What is the action that generates an alert and logs the packet?
    A) log
    B) alert
    C) pass
    Answer: B
  2. Which option is used to search for a specific string?
    A) sid
    B) msg
    C) content
    Answer: C
  3. What does sid stand for?
    A) Security ID
    B) Signature ID
    C) System ID
    Answer: B
  4. Which option describes the alert?
    A) msg
    B) sid
    C) rev
    Answer: A
  5. What does offset do?
    A) Sets the end of search
    B) Sets the start of search
    C) Sets the protocol
    Answer: B
  6. What does depth do?
    A) Sets the search limit
    B) Sets the protocol
    C) Sets the action
    Answer: A
  7. Which option is used for regular expressions?
    A) content
    B) pcre
    C) msg
    Answer: B
  8. What is the purpose of classtype?
    A) To describe the rule
    B) To categorise the attack
    C) To identify the rule
    Answer: B
  9. Which action blocks traffic?
    A) alert
    B) log
    C) drop
    Answer: C
  10. What is a false positive?
    A) A real threat that was missed
    B) A false alarm
    C) A correct alert
    Answer: B
  11. What is a false negative?
    A) A real threat that was missed
    B) A false alarm
    C) A correct alert
    Answer: A
  12. What is the first part of a Snort rule?
    A) Options
    B) Header
    C) Action
    Answer: B
  13. What does rev stand for?
    A) Revision
    B) Reverse
    C) Review
    Answer: A
  14. Which of these is a source of pre-written rules?
    A) Snort Community Rules
    B) Python
    C) MySQL
    Answer: A
  15. Why should you test rules?
    A) To ensure they work
    B) To make them slower
    C) To break Snort
    Answer: A

πŸ”— Matching Exercises

Match the option with its purpose:

OptionPurpose
1. contentA. Unique rule ID
2. msgB. Search for a string
3. sidC. Rule description
4. offsetD. Start position for search
5. classtypeE. Attack category

Answers: 1-B, 2-C, 3-A, 4-D, 5-E

✍️ Short Answer Questions

  1. List the parts of a Snort rule header.
  2. What is the purpose of the content option?
  3. How do you test a Snort rule?

🎬 Scenario-based Exercises

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").

  1. Write a rule to detect such content.
  2. What action would you choose? Why?
  3. How would you test this rule?

πŸ‘₯ Group Activity

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.

πŸ§‘ Individual Activity

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.

πŸ—£οΈ Classroom Discussion Questions

  1. What are the challenges of writing good Snort rules?
  2. How can you reduce false positives in your rules?
  3. Why is it important to keep rules up to date?

πŸ› οΈ Mini Project

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.

πŸ“‹ Practical Assignment

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.

πŸ† Challenge Exercise

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.

πŸ”‘ Quiz Answers

  • Fill-in-the-Blanks: 1. header, options; 2. alert; 3. string/pattern; 4. ID/number; 5. description.
  • True/False: 1T, 2T, 3F, 4T, 5F.
  • Multiple Choice: 1B, 2C, 3B, 4A, 5B, 6A, 7B, 8B, 9C, 10B, 11A, 12B, 13A, 14A, 15A.

🎯 Key Takeaways

  • βœ… A Snort rule consists of a header and options.
  • βœ… The header defines the action, protocol, and addresses.
  • βœ… Options provide detailed detection criteria.
  • βœ… The content option is used to search for patterns.
  • βœ… Always use sid and rev in your rules.
  • βœ… Test your rules thoroughly before deployment.
  • βœ… Use pre-written rules to save time and effort.

πŸ”œ Preparation for the Next Module

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 πŸš€

πŸ† Get Certified

πŸ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (₦2,000 or 20,000 coins), and get a verified certificate with your name on it.

πŸŽ“ Sign Up to Get Certified
πŸ› οΈ Practice Tools
Hands-on simulators & labs - subscription required.
β†’
🎯 Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
β†’