In this step, you'll learn how to create and test your own Suricata rule to detect specific patterns in network traffic. We'll focus on detecting HTTP requests to a test domain (example.com) as a practical example. Custom rules allow you to extend Suricata's detection capabilities beyond its default rule set.
-
First, navigate to the rules directory where Suricata stores its detection rules:
cd /var/lib/suricata/rules
This is where all Suricata rule files are typically stored. We'll add our custom rules here to keep them organized with the existing rules.
-
Create a new rule file specifically for your custom rules:
sudo nano custom.rules
Using sudo
is necessary because the rules directory requires administrative privileges to modify. We're naming the file custom.rules
to clearly distinguish it from Suricata's default rule files.
-
Add the following rule to detect HTTP requests to example.com:
alert http any any -> any any (msg:"HTTP Request to example.com"; flow:to_server; http.host; content:"example.com"; nocase; sid:1000002; rev:1;)
Let's break down what this rule does:
alert http
: Triggers an alert for HTTP traffic
any any -> any any
: Applies to traffic from any source port to any destination port
msg
: Provides a human-readable alert message
flow:to_server
: Only matches traffic going to servers
http.host
: Examines the HTTP Host header
content:"example.com"
: Looks for this specific domain
nocase
: Makes the match case-insensitive
sid:1000002
: Gives the rule a unique ID (above 1,000,000 for custom rules)
rev:1
: Indicates this is the first version of the rule
-
Save the file in nano editor by pressing Ctrl+O (write out), then Enter to confirm, and Ctrl+X to exit.
-
Now we need to tell Suricata to load our new rule file. Edit the main configuration:
sudo nano /etc/suricata/suricata.yaml
Find the rule-files:
section (usually around line 50-60) and add:
- custom.rules
This addition ensures Suricata will load our custom rules when it starts.
-
Before applying the changes, verify the rule syntax is correct:
sudo suricata -T -c /etc/suricata/suricata.yaml -v
The -T
flag tells Suricata to test the configuration without actually running. This helps catch any syntax errors in your rules before they're used.
-
Restart Suricata to load the new rule:
sudo pkill suricata
sudo suricata -c /etc/suricata/suricata.yaml -i eth0
The first command stops any running Suricata instance, while the second starts it fresh with our updated configuration.
-
To test if our rule works, generate some HTTP traffic to example.com:
curl http://example.com
This command makes a simple HTTP request that should trigger our new rule.
-
Finally, check if Suricata detected the traffic by viewing the latest alerts:
sudo tail -n 5 /var/log/suricata/fast.log
You should see an alert matching our custom rule's message about HTTP requests to example.com. If not, double-check each step and verify the rule syntax.