action protocol from_ip port -> to_ip port (msg:"Known malicious behavior, possible X malware infection"; content:"some thing"; content:"some other thing"; sid:10000001; rev:1;)
action protocol from_ip port -> to_ip port part): The header section of a rule encapsulates the intended action of the rule, along with the protocol where the rule is expected to be applied. Additionally, it includes IP addresses, port information, and an arrow indicating traffic directionality.
action instructs Suricata on what steps to take if the contents match. This could range from generating an alert (alert), logging the traffic without an alert (log), ignoring the packet (pass), dropping the packet in IPS mode (drop), or sending TCP RST packets (reject).protocol can vary, including tcp, udp, icmp, ip, http, tls, smb, or dns.rule host variables (such as $HOME_NET, $EXTERNAL_NET, etc. that we saw inside suricata.yaml) and rule direction. The direction arrow between the two IP-Port pairs informs Suricata about the traffic flow.
$HOME_NET any -> $EXTERNAL_NET 9090$EXTERNAL_NET any -> $HOME_NET 8443$EXTERNAL_NET any <> $HOME_NET anyRule ports define the ports at which the traffic for this rule will be evaluated.
alert tcp $HOME_NET any -> $EXTERNAL_NET 9443alert tcp $HOME_NET any -> $EXTERNAL_NET $UNCOMMON_PORTS
suricata.yamlalert tcp $HOME_NET any -> $EXTERNAL_NET [8443,8080,7001:7002,!8443](msg:"Known malicious behavior, possible X malware infection"; content:"some thing"; content:"some other thing"; part)The rule message & content section contains the message we wish to be displayed to the analysts or ourselves when an activity we want to be notified about is detected. content are the segments of the traffic that we deem essential for such detections.
Rule message (msg) is an arbitrary text displayed when the rule is triggered. Ideally, the rule messages we create should contain details about malware architecture, family, and action.
flow identifies the originator and responder. Always remember, when crafting rules, to have the engine monitor "established" tcp sessions.
alert tcp any any -> 192.168.1.0/24 22 (msg:"SSH connection attempt"; flow:to_server; sid:1001;)alert udp 10.0.0.0/24 any -> any 53 (msg:"DNS query"; flow:from_client; sid:1002;)alert tcp $EXTERNAL_NET any -> $HOME_NET 80 (msg:"Potential HTTP-based attack"; flow:established,to_server; sid:1003;)dsize matches using the payload size of the packet. It relies on TCP segment length, not the total packet length.
alert http any any -> any any (msg:"Large HTTP response"; dsize:>10000; content:"HTTP/1.1 200 OK"; sid:2003;)Rule content comprises unique values that help identify specific network traffic or activities. Suricata matches these unique content values in packets for detection.
content:"User-Agent|3a 20|Go-http-client/1.1|0d 0a|Accept-Encoding|3a 20|gzip";
|3a 20|: This represents the hexadecimal representation of the characters ":", followed by a space character. It is used to match the exact byte sequence in the packet payload.|0d 0a|: This represents the hexadecimal representation of the characters "\r\n", which signifies the end of a line in HTTP headers.Rule Buffers, we don't have to search the entire packet for every content match. This saves time and resources. More details can be found here: https://suricata.readthedocs.io/en/latest/rules/http-keywords.html
alert http any any -> any any (http.accept; content:"image/gif"; sid:1;)
http.accept: Sticky buffer to match on the HTTP Accept header. Only contains the header value. The \r\n after the header are not part of the buffer.Rule options act as additional modifiers to aid detection, helping Suricata locate the exact location of contents.
nocase ensures rules are not bypassed through case changes.
alert tcp any any -> any any (msg:"Detect HTTP traffic with user agent Mozilla"; content:"User-Agent: Mozilla"; nocase; sid:8001;)offset informs Suricata about the start position inside the packet for matching.
alert tcp any any -> any any (msg:"Detect specific protocol command"; content:"|01 02 03|"; offset:0; depth:5; sid:3003;)
|01 02 03| in the TCP payload. The offset:0 keyword sets the content match to start from the beginning of the payload, and depth:5 specifies a length of five bytes to be considered for matching.distance tells Suricata to look for the specified content n bytes relative to the previous match.
alert tcp any any -> any any (msg:"Detect suspicious URL path"; content:"/admin"; offset:4; depth:10; distance:20; within:50; sid:3001;)
/admin in the TCP payload, starting from the fifth byte (offset:4) and considering a length of ten bytes (depth:10). The distance:20 keyword specifies that subsequent matches of /admin should not occur within the next 20 bytes after a previous match. The within:50 keyword ensures that the content match occurs within the next 50 bytes after a previous match.sid:10000001; rev:1; part):reference provides us with a lead, a trail that takes us back to the original source of information that inspired the creation of the rule.sid (signature ID). The unique quality of this numeric identifier makes it essential for the rule writer to manage and distinguish between rules.revision offers insights into the rule's version. It serves as an indicator of the evolution of the rule over time, highlighting modifications and enhancements made.