Dev.to Security 🔐 Cybersecurity 👁 2 📖 6 min read

# Week 8: Network Intrusion Detection & Analysis – Finding the Needle with Snort

This week, we focus on network intrusion detection and prevention systems: how they detect suspicious activity, what evidence they hold for investigators, and how to read and write rules for Snort, the most widely used o

This week, we focus on network intrusion detection and prevention systems: how they detect suspicious activity, what evidence they hold for investigators, and how to read and write rules for Snort, the most widely used open-source IDS.

NIDS/NIPS and HIDS/HIPS

Intrusion detection and prevention systems come in two main forms:

  • Host-Based (HIDS/HIPS): Host-based intrusion detection (or prevention) systems run on an individual machine and monitor activity on that host.
  • Network-Based (NIDS/NIPS): Network-based intrusion detection (or prevention) systems monitor traffic as it crosses the network. A NIDS alerts on suspicious traffic, while a NIPS can also block it.

How an IDS Works

  • Rule-Based Detection: IDSs are rule-based. They issue alerts and can be configured to capture suspicious packet sequences.
  • Sniffing and Inspection: They sniff traffic and inspect it across multiple layers, with protocol awareness and protocol reassembly. Deep packet analysis is possible, though packet contents may need some normalization first.
  • Timing: In a NIPS, processing time is critical because traffic must be inspected inline before it is passed on. In a NIDS, offline analysis and delayed alerting are tolerable.

Modes of Detection

  • Signature-Based Analysis: Matches traffic against known patterns of malicious activity.
  • Protocol Analysis: Checks whether traffic behaves according to the protocol's specification.
  • Behavioral Analysis: Flags traffic that deviates from normal behaviour.

Types of IDS

  • Commercial: Check Point IPS-1, Cisco IPS, Enterasys IPS, and TippingPoint IPS.
  • Open Source: Snort (developed by Sourcefire, which was acquired by Cisco in 2013) and Bro (since renamed Zeek).

Evidence Acquisition

NIDS/NIPS devices hold several types of evidence that are valuable to investigators:

  • Configuration: Alerts can differ between sensors, so the configuration of each sensor, its location within the network, its running configuration, and its rule set all matter.
  • Alert Data: The alerts the IDS has generated.
  • Packet Header Info, Flow Data, and Packet Payloads: Captured details of the suspicious traffic itself.
  • Correlation Across Multiple Sensors: Linking alerts from different sensors helps build a fuller picture of an incident.
  • Comprehensive Logging: Capturing all packets all the time requires massive storage space and lots of CPU, is difficult to archive, and carries a large risk. Alternatives include filtering what is logged or keeping only flow data.

Snort

Snort is the most widely used IDS. It has an open rule language, is extremely versatile, and follows a community/commercial business model with commercial support available.

  • Architecture: Snort uses libpcap to capture packets, which then pass through preprocessors that reassemble fragments (Layer 3), streams (Layer 4), sessions (Layer 5), and transactions (Layer 6). An alert can be issued at any layer. After reassembly and anomaly detection, the information is handed to the rule engine and then to the alerting engine, and subsequent related packets can be marked for capture.
  • Configuration Files: Global Snort values (network addresses, rule locations, and services) are stored in /etc/snort/snort.conf, the rules themselves live in /etc/snort/rules, and logs are written to /var/log/snort, which can grow very large.
  • Keywords: include pulls in rule files (e.g., include /etc/snort/rules/ping.rules), var defines variables (e.g., var HOME_NET 192.16.13.0/24 and var RULE_PATH /etc/snort/rules, used as include $RULE_PATH/ping.rules), and config sets options such as config reference: bugtraq http://www.securityfocus.com/bid.

Snort Rule Structure

Rules are the basis for logging or not logging a packet. The generic syntax is rule_header (rule_options), where the header defines the action, protocol, addresses, and ports, and the options define the message, what to look for, and where to look. A rule can span multiple lines if each continued line ends with a space and a backslash (\).

  • Field 1 – Action: alert generates an alert and then logs the packet, log only logs the packet, and pass ignores it. activate sends an alert and then turns on a dynamic rule, which stays idle until activated and then acts as a log rule (these two were later removed in Snort 3). User-defined rule types are also supported.
  • Field 2 – Protocol: tcp, udp, icmp, and ip, with others such as ARP, IGRP, GRE, OSPF, and RIP planned for the future.
  • Fields 3 & 5 – Addresses: CIDR notation (192.16.13.0/24), a variable ($HOME_NET), the keyword any, a list ([192.16.13.0/24,10.1.1.0/24]), or a negation (!192.16.13.1).
  • Fields 4 & 6 – Ports: A number (80, 21, 23), any, or a range: 100:1024 (100 through 1024), :600 (0 through 600), or 500: (500 and above). A typical address/port section looks like !192.16.13.0/24 any -> 192.16.13.0/24 111.

Rule Options

Options are written as keyword: value; inside the parentheses. Common options include:

  • msg: Puts a message in the log record to identify the rule (e.g., msg:"SYN packet malformed";).
  • ttl / tos / id: Test the IP header's TTL value, type-of-service field, and ID field (e.g., ttl:127;).
  • dsize: Tests the packet's payload size using >, <, or <> for a range (e.g., dsize:400<>500;).
  • fragbits: Tests the IP fragmentation bits: R (reserved bit), M (more fragments), and D (don't fragment). Modifiers are + (all must match), * (any must match), and ! (match if the bits are not set), as in fragbits:R+;.
  • content: Searches the payload for specific content. Binary data is enclosed in pipes and ASCII in quotes, and ! matches packets that do not contain the string (e.g., content:"|90C8 C0FF FFFF|/bin/sh"; or content:!"GET";).
  • offset / depth / nocase: offset sets where the content search starts, depth sets how far it searches, and nocase makes it case-insensitive (e.g., offset:3; depth:22; nocase;).
  • flags: Tests TCP flags: F, S, R, P, A, U, the reserved bits 1 and 2, and 0 for no flags set, with the same +, *, and ! modifiers (e.g., flags:SF;).
  • seq / ack: Test the TCP sequence number and acknowledgment fields.
  • ip_proto: Checks the IP protocol field using numbers or names from /etc/protocols, where ICMP is 1, TCP is 6, and UDP is 17 (e.g., ip_proto:6;).
  • itype / icode: Check the ICMP type and code fields (e.g., an echo request is itype:8; icode:0;).
  • classtype: Categorizes detections into attack classes listed in classification.config (e.g., classtype:misc-attack;).
  • reference: Links to external attack identification systems such as Bugtraq, CVE, arachNIDS, McAfee, or a URL (e.g., reference:bugtraq,1387;).
  • sid / rev: The Snort rule ID and its revision number.
  • flow: Works with TCP stream reassembly to match traffic in a certain direction: to_server/from_client trigger on client requests, to_client/from_server trigger on server responses, and established triggers only on established TCP connections (e.g., flow:from_server;).

Rule Examples

A rule that detects a RealAudio denial-of-service attempt:

alert tcp any any -> any 7070 \
  (msg:"IDS411/dos-realaudio"; flags:AP; content:"|fff4 fffd 06|"; \
  reference:arachNIDS,IDS411;)

A rule that detects the MS-SQL (Slammer) worm spreading over UDP port 1434:

alert udp any any -> any 1434 \
  (msg:"MS-SQL Worm propagation attempt"; content:"|04|"; depth:1; \
  content:"|81 f1 03 01 04 9b 81 f1 01|"; content:"sock"; content:"send"; \
  reference:bugtraq,5310; reference:bugtraq,5311; \
  reference:url,il.nai.com/vil/content/v_99992.htm; \
  classtype:misc-attack; sid:2003; rev:2;)

From Packet to Alert

  • Example 1 – ICMP Ping: The rule alert icmp $EXTERNAL_NET any -> $HOME_NET any (msg:"ICMP PING"; icode:0; itype:8; classtype:misc-activity; sid:384; rev:5;) matches an ICMP echo request from 10.0.1.10 to 10.0.1.254. Snort produces the alert [**] [1:384:5] ICMP PING [**] with classification "Misc activity" and priority 3, followed by the IP and ICMP header details (TTL 64, Type 8, Code 0).
  • Example 2 – /etc/passwd Request: The rule alert tcp $EXTERNAL_NET any -> $HTTP_SERVERS $HTTP_PORTS (msg:"WEB-MISC /etc/passwd"; flow:to_server,established; content:"/etc/passwd"; nocase; classtype:attempted-recon; sid:1122; rev:5;) matches an HTTP request from 192.168.1.50 to port 80 on 172.16.16.217. The packet payload clearly shows GET /etc/passwd HTTP/1.0 sent by a Wget user agent. Snort produces the alert [**] [1:1122:5] WEB-MISC /etc/passwd [**] with classification "Attempted Information Leak" and priority 2, along with the TCP header details and flags (***AP***).

In the alert format [1:1122:5], the numbers are the generator ID, the Snort rule ID (sid), and the revision (rev).

Conclusion

An IDS alert is usually the trigger that launches an investigation, and the IDS provides the data for that investigation. Network traffic is the proverbial haystack, and Snort often finds the needle.

Reference: Davidoff, S., & Ham, J. (2012). Network Forensics: Tracking Hackers Through Cyberspace. Boston: Prentice Hall.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.