Cyber Services / All lessons / CEH — Ethical Hacking / Snort handouts Complete Lab

Snort handouts Complete Lab

Module 102 of 130 · 11 min read · Free to read

MODULES ↓ Command blocks have a COPY button

Master Intrusion Detection with Hands-on Configuration, Rule Writing & Advanced Threat Detection

Ethical Usage Notice

This lab guide is designed for educational purposes in controlled environments. Always ensure you have explicit permission before testing Snort rules on any network. Unauthorized security testing may violate laws and regulations.

Lab Objectives

What You Will Master:

  • Install and configure Snort IDS/IPS on Ubuntu Server
  • Understand Snort architecture and operating modes
  • Write custom detection rules for various attack scenarios
  • Configure network variables and optimize performance
  • Implement advanced detection techniques and thresholding
  • Integrate with security monitoring workflows
  • Troubleshoot common Snort configuration issues

Snort Operating Modes

Sniffer Mode

Displays packet data on console. Perfect for network troubleshooting and learning.

snort -v -i eth0

Packet Logger Mode

Logs packets to disk for later analysis. Essential for forensic investigations.

snort -l /var/log/snort -i eth0

NIDS Mode

Network Intrusion Detection System mode - analyzes traffic against rules.

snort -c /etc/snort/snort.conf -i eth0

IPS Mode

Intrusion Prevention System mode - actively blocks malicious traffic.

snort -c /etc/snort/snort.conf -i eth0 -Q

Lab Setup & Installation

Environment Installation Configuration Testing
  1. Environment Preparation

    Set up your Ubuntu Server virtual machine with proper network configuration:

    # Check network interface ip a show

    Pro Tip:

    Use bridged networking for full network visibility or host-only for isolated testing. Ensure your VM has at least 2GB RAM and 20GB disk space.

  2. Snort Installation

    Install Snort and essential dependencies:

    # Update system and install dependencies
    sudo apt update && sudo apt upgrade -y
    sudo apt install -y build-essential libpcap-dev libpcre3-dev libdumbnet-dev \
    bison flex zlib1g-dev liblzma-dev openssl libssl-dev
    
    # Install Snort
    sudo apt install -y snort

    Important:

    During installation, you'll be prompted for your HOME_NET configuration. Use CIDR notation (e.g., 192.168.1.0/24) that matches your lab network.

  3. Configuration & Validation

    Verify and customize your Snort configuration:

    # Test configuration syntax
    sudo snort -T -c /etc/snort/snort.conf
    
    # Check for any configuration errors
    sudo snort -c /etc/snort/snort.conf --dump-dynamic-rules

    Key Configuration Files:

    • /etc/snort/snort.conf - Main configuration
    • /etc/snort/rules/local.rules - Custom rules
    • /etc/snort/preproc_rules/ - Preprocessor rules

15 Comprehensive Hands-on Lab Activities

Activity 1: Network Configuration & Interface Setup

Beginner

Configure your network interface and verify Snort can monitor traffic properly.

  • Step 1: Identify all network interfaces using ip a or ifconfig
  • Step 2: Determine which interface connects to your monitoring network
  • Step 3: Enable promiscuous mode: sudo ip link set eth0 promisc on
  • Step 4: Verify promiscuous mode: ip link show eth0 (look for PROMISC)
  • Step 5: Test interface monitoring: sudo tcpdump -i eth0 -c 10 -n
  • Step 6: Verify Snort can access interface: sudo snort -i eth0 -v
  • Step 7: Document your interface name and IP address for future reference
# Complete interface setup sequence ip a show sudo ip link set eth0 promisc on sudo tcpdump -i eth0 -c 5 -n sudo snort -i eth0 -v -c /etc/snort/snort.conf

Activity 2: Snort Configuration Validation

Beginner

Validate your Snort configuration and troubleshoot common issues.

  • Step 1: Test basic configuration syntax: sudo snort -T -c /etc/snort/snort.conf
  • Step 2: Check for specific configuration sections: grep -n "include" /etc/snort/snort.conf
  • Step 3: Verify rule files exist: sudo snort -c /etc/snort/snort.conf --dump-dynamic-rules
  • Step 4: Check preprocessor configurations: grep -i "preprocessor" /etc/snort/snort.conf
  • Step 5: Validate network variables: grep -E "ipvar (HOME_NET|EXTERNAL_NET)" /etc/snort/snort.conf
  • Step 6: Test with a simple rule: Create a basic ICMP rule in local.rules and test
  • Step 7: Document any errors and their resolutions for future reference
# Comprehensive configuration validation sudo snort -T -c /etc/snort/snort.conf sudo snort -c /etc/snort/snort.conf --dump-dynamic-rules grep -n "include" /etc/snort/snort.conf | head -10

Activity 3: Basic Rule Writing & Testing

Beginner

Create and test basic detection rules for common protocols.

  • Step 1: Open the local rules file: sudo nano /etc/snort/rules/local.rules
  • Step 2: Create ICMP ping detection rule with proper SID (1000001+)
  • Step 3: Create SSH connection attempt rule targeting port 22
  • Step 4: Create HTTP web traffic detection rule
  • Step 5: Validate rule syntax: sudo snort -T -c /etc/snort/snort.conf
  • Step 6: Test rules by generating traffic (ping, SSH attempts, web requests)
  • Step 7: Monitor alerts in console mode and verify rule triggers
# ICMP Ping Detection
alert icmp any any -> $HOME_NET any (msg:"ICMP Ping Detected"; itype:8; sid:1000001; rev:1;)

# SSH Connection Attempts
alert tcp any any -> $HOME_NET 22 (msg:"SSH Connection Attempt"; flags:S; sid:1000002; rev:1;)

# HTTP Traffic Detection
alert tcp any any -> $HOME_NET 80 (msg:"HTTP Traffic Detected"; flow:to_server; sid:1000003; rev:1;)

Activity 4: Port Scan Detection Setup

Intermediate

Implement detection for various Nmap scanning techniques.

  • Step 1: Locate and configure the sfPortscan preprocessor in snort.conf
  • Step 2: Set appropriate thresholds for your network size
  • Step 3: Create custom rules for specific scan types (SYN, FIN, NULL, XMAS)
  • Step 4: Test SYN scan detection: nmap -sS target_ip
  • Step 5: Test FIN scan detection: nmap -sF target_ip
  • Step 6: Test NULL scan detection: nmap -sN target_ip
  • Step 7: Analyze detection effectiveness and adjust thresholds as needed
# Configure portscan preprocessor
preprocessor sfPortscan: proto { all } scan_type { all } \
memcap { 10000000 } sense_level { low }

# Custom scan detection rules
alert tcp any any -> $HOME_NET any (msg:"TCP SYN Scan Detected"; flags:S; \
detection_filter: track by_src, count 30, seconds 10; sid:1000004;)

alert tcp any any -> $HOME_NET any (msg:"TCP FIN Scan Detected"; flags:F; \
flags:0; sid:1000005;)

Activity 5: Web Application Attack Detection

Intermediate

Create rules to detect common web application attacks.

  • Step 1: Create SQL injection detection rules for common patterns
  • Step 2: Implement directory traversal attack detection
  • Step 3: Create XSS detection rules for script tags and event handlers
  • Step 4: Set up detection for common web shells and backdoors
  • Step 5: Test with vulnerable web applications (DVWA, WebGoat)
  • Step 6: Monitor false positives and refine rule specificity
  • Step 7: Document detection rates and rule effectiveness
# SQL Injection Detection
alert tcp $EXTERNAL_NET any -> $HOME_NET 80 (msg:"SQL Injection Attempt"; \
content:"union select"; http_client_body; content:"information_schema"; \
http_client_body; distance:0; sid:1000006;)

# Directory Traversal
alert tcp $EXTERNAL_NET any -> $HOME_NET 80 (msg:"Directory Traversal Attempt"; \
content:"../"; http_uri; content:"etc/passwd"; http_uri; sid:1000007;)

# XSS Detection
alert tcp $EXTERNAL_NET any -> $HOME_NET 80 (msg:"XSS Attempt"; \
pcre:"/]*>/i"; http_client_body; sid:1000008;)

Activity 6: Performance Optimization & Tuning

Intermediate

Optimize Snort for high-traffic environments.

  • Step 1: Monitor baseline performance: top -p $(pgrep snort)
  • Step 2: Configure threading options in snort.conf
  • Step 3: Set appropriate memory limits for packet processing
  • Step 4: Optimize rule performance using fast_pattern and content modifiers
  • Step 5: Implement rule profiling to identify slow rules
  • Step 6: Test performance under simulated load
  • Step 7: Document performance improvements and optimal settings
# Performance monitoring and configuration config pkt_mem_len: 104857600 config pkt_threads: 4 config daq_dir: /usr/local/lib/daq # Rule performance optimization alert tcp any any -> $HOME_NET 80 \ (msg:"Optimized Rule"; content:"malicious"; fast_pattern; \ content:"pattern"; within:100; sid:1000009;)

Activity 7: Logging & Alerting Configuration

Intermediate

Set up comprehensive logging and alerting systems.

  • Step 1: Configure unified2 binary logging for efficient storage
  • Step 2: Set up syslog integration for centralized logging
  • Step 3: Create custom alert formats for different severity levels
  • Step 4: Implement log rotation to manage disk space
  • Step 5: Configure email alerts for critical security events
  • Step 6: Test alert delivery and formatting
  • Step 7: Document the logging architecture and procedures
# Comprehensive logging configuration
output alert_syslog: LOG_AUTH LOG_ALERT
output alert_fast: alert_fast.txt
output unified2: filename merged.log, limit 128, nostamp
output alert_csv: alert.csv default
output log_tcpdump: snort.log

# Log rotation configuration
/var/log/snort/*.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
}

Activity 8: Database Integration

Intermediate

Integrate Snort with database for alert storage and analysis.

  • Step 1: Install and configure MySQL/PostgreSQL database
  • Step 2: Create Snort database schema using provided scripts
  • Step 3: Configure database output plugin in snort.conf
  • Step 4: Test database connectivity and data insertion
  • Step 5: Create basic SQL queries for alert analysis
  • Step 6: Set up automated database maintenance
  • Step 7: Implement database backup procedures
# Database output configuration output database: log, mysql, user=snort password=password \ dbname=snort host=localhost # Sample database queries SELECT COUNT(*) FROM event WHERE timestamp > NOW() - INTERVAL 1 HOUR; SELECT sig_name, COUNT(*) FROM event GROUP BY sig_name;

Activity 9: IPS Mode Configuration

Advanced

Configure Snort for inline intrusion prevention.

  • Step 1: Set up network interface for inline operation
  • Step 2: Configure policy mode to inline in snort.conf
  • Step 3: Create drop rules for known malicious traffic patterns
  • Step 4: Test blocking capabilities in controlled environment
  • Step 5: Monitor for false positives and adjust rules accordingly
  • Step 6: Implement whitelists for critical systems
  • Step 7: Document IPS policies and procedures
# IPS mode configuration
config policy_mode: inline

# Drop rules for malicious traffic
drop tcp $EXTERNAL_NET any -> $HOME_NET 23 (msg:"Telnet Blocked"; sid:1000010;)
drop tcp $EXTERNAL_NET any -> $HOME_NET 1433 (msg:"SQL Server Exploit Blocked"; \
content:"xp_cmdshell"; sid:1000011;)

# Reject rules with TCP resets
reject tcp $EXTERNAL_NET any -> $HOME_NET 22 (msg:"SSH Bruteforce Blocked"; \
flow:established; detection_filter: track by_src, count 5, seconds 60; sid:1000012;)

Activity 10: Advanced Rule Writing

Advanced

Create complex rules with advanced options and state tracking.

  • Step 1: Implement flowbits for multi-packet attack detection
  • Step 2: Use byte_test and byte_jump for protocol analysis
  • Step 3: Create rules with PCRE for complex pattern matching
  • Step 4: Implement detection_filter for event thresholding
  • Step 5: Use flow and stream reassembly options
  • Step 6: Create rules with depth, offset, and within modifiers
  • Step 7: Test rule performance and accuracy
# Advanced rule with flowbits and PCRE
alert tcp $HOME_NET any -> $EXTERNAL_NET 80 (msg:"Suspicious User Agent"; \
flow:established; content:"User-Agent|3A 20|"; pcre:"/bot|crawler|scraper/i"; \
sid:1000013;)

# Protocol analysis with byte_test
alert udp any any -> any 53 (msg:"DNS Buffer Overflow Attempt"; \
content:"|01 00 00 01 00 00 00 00 00 00|"; depth:10; \
byte_test:2,>,512,2; sid:1000014;)

# State tracking with flowbits
alert tcp $EXTERNAL_NET any -> $HOME_NET 21 (msg:"FTP User Attempt"; \
flow:established; content:"USER"; flowbits:set,ftp.user; \
flowbits:noalert; sid:1000015;)

alert tcp $EXTERNAL_NET any -> $HOME_NET 21 (msg:"FTP Password Attempt"; \
flow:established; content:"PASS"; flowbits:isset,ftp.user; sid:1000016;)

Activity 11: Real Attack Simulation & Testing

Advanced

Simulate real attacks and test detection capabilities.

  • Step 1: Simulate SSH brute force attacks using Hydra
  • Step 2: Conduct comprehensive port scanning with Nmap
  • Step 3: Simulate DDoS attacks using hping3
  • Step 4: Test web application attacks against vulnerable apps
  • Step 5: Simulate malware C&C communication patterns
  • Step 6: Analyze detection effectiveness and coverage gaps
  • Step 7: Document detection metrics and improvement areas
# Attack simulation commands # SSH brute force hydra -l root -P wordlist.txt ssh://target_ip # Port scanning nmap -sS -T4 -A target_ip nmap -sU -p 1-1000 target_ip # DDoS simulation hping3 -S --flood -p 80 target_ip # Web vulnerability scanning nikto -h http://target_ip

Activity 12: Monitoring & Dashboard Setup

Intermediate

Create comprehensive monitoring and visualization dashboards.

  • Step 1: Set up Snort statistics monitoring
  • Step 2: Configure real-time alert monitoring
  • Step 3: Create performance metrics dashboard
  • Step 4: Implement alert correlation and grouping
  • Step 5: Set up automated reporting
  • Step 6: Create custom alert views for different teams
  • Step 7: Test monitoring system under various load conditions
# Snort monitoring configuration config stats: events config stats: file /var/log/snort/stats.log # Real-time monitoring tail -f /var/log/snort/alert snort -c /etc/snort/snort.conf -A console -i eth0

Activity 13: Rule Management & Updates

Intermediate

Implement automated rule management and update procedures.

  • Step 1: Set up PulledPork for automatic rule updates
  • Step 2: Configure rule categories and enable/disable sets
  • Step 3: Implement rule testing before production deployment
  • Step 4: Create rule change management procedures
  • Step 5: Set up rule performance monitoring
  • Step 6: Implement rule version control
  • Step 7: Document rule management workflow
# PulledPork configuration pulledpork.pl -c /etc/pulledpork/pulledpork.conf -T # Rule management commands snort -c /etc/snort/snort.conf --dump-rule-metrics snort -c /etc/snort/snort.conf --rule-test

Activity 14: Distributed Deployment

Advanced

Implement Snort in a distributed architecture for large networks.

  • Step 1: Design distributed sensor architecture
  • Step 2: Configure multiple sensor nodes
  • Step 3: Set up centralized management server
  • Step 4: Implement sensor communication and synchronization
  • Step 5: Configure load balancing and failover
  • Step 6: Test distributed detection capabilities
  • Step 7: Document deployment architecture and procedures
# Distributed sensor configuration # Sensor 1 - Internal network snort -c /etc/snort/snort-internal.conf -i eth0 # Sensor 2 - DMZ network snort -c /etc/snort/snort-dmz.conf -i eth1 # Centralized management barnyard2 -c /etc/snort/barnyard2.conf -d /var/log/snort -f merged.log

Activity 15: Incident Response Integration

Advanced

Integrate Snort with incident response procedures and tools.

  • Step 1: Create incident classification matrix
  • Step 2: Implement automated incident ticket creation
  • Step 3: Set up alert escalation procedures
  • Step 4: Create incident response playbooks
  • Step 5: Integrate with forensic analysis tools
  • Step 6: Conduct tabletop exercises
  • Step 7: Document lessons learned and improvement areas
# Incident response automation # Example: Auto-create tickets for high-severity alerts grep -i "Priority: 1" /var/log/snort/alert | \ while read line; do echo "High severity alert: $line" | \ mail -s "Snort Alert - Priority 1" security-team@company.com done

Official Resources & Documentation

Conclusion & Next Steps

You've now mastered the fundamentals of Snort IDS/IPS deployment and advanced rule writing. Continue your journey with these next steps:

Production Deployment

  • Implement Snort in monitoring mode first
  • Gradually enable prevention capabilities
  • Set up proper logging and alerting
  • Establish baseline performance metrics

Advanced Integration

  • Integrate with SIEM solutions
  • Set up automated rule updates
  • Implement distributed deployment
  • Create custom dashboards
Modules (130) ← CEH — Ethical Hacking