content 是 Snort 規則中最核心的載荷匹配選項,用於在數據包 Payload 中搜索特定模式。匹配引擎採用 Boyer-Moore 算法,支持文本與二進制混合模式,默認大小寫敏感。
content: [!] "<pattern>";
! 取反匹配(不包含該模式時觸發)。: ; \ " 必須轉義:\(如 \:)|3A| 表示 :。| 修飾符 | 作用 | 示例 |
|---|---|---|
nocase | 忽略大小寫匹配 | content:"USER root"; nocase; |
rawbytes | 匹配原始數據,繞過預處理器解碼(如 Telnet/HTTP 解碼) | content:"|FF F1|"; rawbytes; |
depth:<n> | 從起始偏移處向後 n 字節內匹配(默認起始為 payload 開頭) | content:"cgi-bin/phf"; offset:4; depth:20; |
offset:<n> | 從 payload 開頭跳過 n 字節開始匹配 | 同上 |
distance:<n> | 相對於上一個 content 匹配結束位置 再跳過 n 字節 | content:"ABC"; content:"DEF"; distance:1; 等價正則 /ABC.DEF/ |
within:<n> | 在 distance 指定位置起向後 n 字節內匹配 | content:"ABC"; content:"EFG"; within:10; |
針對 HTTP URI 經預處理器正規化後的內容匹配(如 %2f → /,目錄遍歷序列被移除)。規則中必須寫正規化後的模式,否則無法匹配。
uricontent:"/winnt/system32/cmd.exe";
# 檢測 SMB 管道滲透
alert tcp any any -> any 139 (content:"|5c 00|P||00|I|00|P|00|E|00 5c|";)
# 非 GET 請求告警(前 3 字節非 GET)
alert tcp any any -> any 80 (content:!"GET"; depth:3; nocase;)
# Telnet NOP 指令檢測(需繞過 Telnet 解碼器)
alert tcp any any -> any 23 (msg:"Telnet NOP"; content:"|FF F1|"; rawbytes;)
content 降低誤報率。depth/offset 限制搜索範圍:避免全 payload 掃描,大幅提升性能。distance/within 處理相對位置關係:替代複雜正則,效率更高。snort -T -c snort.conf 驗證規則語法與性能統計。