Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Practice library / Security operations basics

Lab · Medium · 1.5 hoursFree account

IDS/IPS Detection Engineering

Snort/Suricata rule writing, tuning, and evasion-aware detection.

  • ids
  • ips
  • suricata
  • snort
  • zeek
On this page 13 sections
  1. ·Prerequisites
  2. ·Lab Setup
  3. ·Build the target
  4. 1Prepare the work area
  5. 2Capture normal and attack traffic
  6. Locked: Read the evidence and choose what to detect
  7. Locked: Write the rule and test it offline
  8. Locked: Replay the traffic to a live sensor
  9. Locked: Tune the rule and test an evasion
  10. Locked: Port the rule to Snort 3
  11. Locked: Defence and Detection
  12. Locked: Check Your Work
  13. Locked: Clean-up

A detection rule turns a packet or a log line into an alert that a human will read. Write the rule too loose and the SOC drowns in false positives; write it too tight and the attacker walks past. The lab builds a Suricata rule from a real pcap, tunes it against a false positive, tests an evasion, and then ports the result to Snort 3 syntax.

Lab Objectives

  • Capture attack and normal web traffic into a pcap file
  • Write a Suricata rule from the evidence in a pcap
  • Test the rule offline with suricata -r and live with tcpreplay
  • Tune the rule to remove a false positive
  • Test the rule against a simple evasion
  • Port the rule to Snort 3 syntax

A detection rule is a hypothesis: "this pattern in the traffic means an attack". A hypothesis needs a test. In this lab you build the test first. You capture real traffic, write the rule from the packets, and replay the capture until the rule fires on the attack and stays silent on normal traffic.

You work as a defender. The "attack" is one web request with an SQL injection string sent to a web server that you own.

Prerequisites

Finish Network Traffic Analysis first. It explains packets, flows and HTTP, which you read in this lab. Practise reading packets in Wireshark if tcpdump output is new to you. The theory of good rules is in Detection Engineering.

Lab Setup

Use your own Kali machine: a virtual machine is fine. You need these tools:

  1. suricata 7.x, tcpreplay, tcpdump, tshark, jq and curl.
  2. zeek for the cross-check in Step 3.
  3. snort 3.x for Step 7. This tool is optional.

Install the missing tools.

sudo apt update
sudo apt install -y suricata tcpreplay tcpdump tshark jq curl

Build the target

KaliRange hosts no targets, no virtual machines and no lab files. You run this lab on your own machine, against a web server that you own. The addresses 10.0.0.5 (your Kali machine) and 10.0.0.20 (the web server) are the example addresses of this lab. Replace them with your own addresses in the commands.

Your lab must have these parts:

  • Your Kali machine, with the interface eth0 on the same network as the web server.
  • A web server at 10.0.0.20 on port 80. It must answer the paths /index.php?page=home, /index.php?id=1 and /blog/union-select-committee. Any answer is fine, even a 404 page, because the lab reads the packets and not the pages.
  • A network whose addresses lie inside HOME_NET of Suricata. The default HOME_NET includes 10.0.0.0/8. If your addresses differ, add them to HOME_NET in /etc/suricata/suricata.yaml.

Build the web server in a virtual machine or a container on your own computer. For example, run a plain Apache or Nginx with a one-line PHP page, or run DVWA (Damn Vulnerable Web Application, by the DVWA team, PHP and MariaDB). Metasploitable 2 (Rapid7, an intentionally vulnerable Ubuntu virtual machine) also works. Take a snapshot of each virtual machine (or note the container state) before Step 1.

Send the test requests only to your own target: a server that you own or have written permission to test. Never send them to any other server. Do not run tcpreplay on a real network: replayed packets go on the wire like real ones. In this lab you replay into a virtual cable that never leaves your machine.

Step 1: Prepare the work area

1

Create a work folder and a virtual cable

tcpreplay writes packets to a network interface. Suricata reads them from another one. A veth pair is a virtual cable: what goes in at one end comes out at the other end.

Create the work folder and the rule file.

mkdir -p ~/ids-lab/out && cd ~/ids-lab
touch local.rules

Create the virtual cable and bring both ends up.

sudo ip link add veth0 type veth peer name veth1
sudo ip link set veth0 up
sudo ip link set veth1 up
ip -br link show type veth

Example output:

veth0@veth1      UP             5e:1c:7a:90:0b:21 <BROADCAST,MULTICAST,UP,LOWER_UP>
veth1@veth0      UP             9a:44:c2:0d:f3:6e <BROADCAST,MULTICAST,UP,LOWER_UP>

Both ends must show UP.

Step 2: Capture normal and attack traffic

2

Record two pcap files

A pcap file is a recording of packets. You need two: one with only normal requests, and one with the attack. The first file later proves that your rule does not fire on normal traffic.

Start a capture of normal traffic. Send two normal requests, then stop the capture.

cd ~/ids-lab
sudo tcpdump -i eth0 -w normal.pcap host 10.0.0.20 and port 80 &
sleep 2
curl -s -o /dev/null "http://10.0.0.20/index.php?page=home"
curl -s -o /dev/null "http://10.0.0.20/blog/union-select-committee"
sleep 1
sudo kill %1

The second request is a normal blog address that contains the words union and select. It is a trap for a weak rule. Step 6 uses it.

Now capture the attack: a scanner user agent, an SQL injection in the URL, and the same injection with percent-encoding.

sudo tcpdump -i eth0 -w attack.pcap host 10.0.0.20 and port 80 &
sleep 2
curl -s -o /dev/null -A "sqlmap/1.7.2#stable" "http://10.0.0.20/index.php?id=1"
curl -s -o /dev/null "http://10.0.0.20/index.php?id=1+UNION+SELECT+null,version()--+-"
curl -s -o /dev/null "http://10.0.0.20/index.php?id=1%20%55NION%20%53ELECT%20null"
sleep 1
sudo kill %1

Count the packets in each file.

capinfos -c normal.pcap attack.pcap

Example output:

File name:           normal.pcap
Number of packets:   18
File name:           attack.pcap
Number of packets:   27

Your numbers differ a little. Both files must have packets.

Sign in to continue

8 more sections are for signed-in members. It is free: sign in with Google.

Related

This lab is extra practice for the course Security operations basics. Do the lessons first.Open the course