Ubuntu 下自定义嗅探与告警规则的实用方案
一、先明确你的“Sniffer”类型
二、使用 Snort 自定义规则(推荐)
snort -V查看版本信息。Snort 支持多线程、脚本化配置与更好的跨平台能力。/etc/snort/snort.conf中设置var HOME_NET 192.168.1.0/24,并确保包含你的自定义规则文件(如include $RULE_PATH/local.rules)。<action> <protocol> <src_ip> <src_port> -> <dst_ip> <dst_port> (<options>)any、CIDR(如192.168.1.0/24)、端口范围(如1:1024)、取反(!80)。->(单向)、`<>``(双向)。/etc/snort/rules/local.rules)
alert icmp any any -> $HOME_NET any (msg:"ICMP Probe Detected"; sid:1000001; rev:1;)alert tcp any any -> $HOME_NET 80 (msg:"HTTP Suspicious Keyword"; content:"' OR 1=1 --"; http_client_body; sid:1000002; rev:1;)alert tcp any any -> any any (msg:"Possible SYN Flood"; flags:S; threshold: type both, track by_src, count 100, seconds 60; sid:1000003; rev:1;)sudo snort -i <interface> -c /etc/snort/snort.conf -A consolesudo snort -i <interface> -c /etc/snort/snort.conf -Dsudo snort -T -c /etc/snort/snort.conf/var/log/snort/alert(或控制台输出)。三、使用 Suricata 自定义规则(可选)
/etc/suricata/suricata.yaml启用规则路径(如rule-files)与日志输出。/etc/suricata/rules/local.rules)
alert icmp any any -> $HOME_NET any (msg:"ICMP Probe"; sid:1000001; rev:1;)alert http any any -> $HOME_NET 80 (msg:"HTTP SQLi Attempt"; content:"' OR 1=1 --"; http_client_body; sid:1000002; rev:1;)sudo suricata -i <interface> -c /etc/suricata/suricata.yaml -vfast.log、eve.json等输出以确认告警。四、仅用抓包工具实现“规则化”筛选与简单告警
sudo tcpdump -i <interface> -nn -A 'tcp port 8080 and (tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x504f5354)'sudo tcpdump -i <interface> -nn 'tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn'mail/curl等方式通知(需预装邮件或 webhook 工具)。五、排错与性能优化要点
cap_net_raw),确保以sudo运行并指定正确的网卡接口(如eth0/ens33)。snort -T -c /etc/snort/snort.conf做语法与包含路径检查,避免因规则错误导致无法启动。threshold)与内容修饰符(如http_client_body);仅抓取必要流量,减少资源占用。/var/log/snort/alert,并结合实际业务调整规则;保持规则库与软件版本及时更新。