AlickGuo +

Snort 初探

Snort 初探

概述

Snort 是免费 Network Intrusion Prevention System(NIPS) 及 Network Intrusion Detection System (NIDS) 软件,其具有对数据流量分析和对网络数据包进行协议分析处理的能力,通过灵活可定制的规则库(Rule),可对处理的报文内容进行搜索和匹配,能够检测出各种攻击,并进行实时预警。

软件采用双授权:

架构

规则

Decoder 与 Preprocessor 规则

Decoder 阶段使用 snort.conf 文件中 config disbale_decode_alerts 参数配置告警行为,通过 config enable_decode_drops 参数的优先级高于 rule 中的 drop 行为优先级

Decoder 与 Preprocessor 的 Rule 通过配置文件保存在源代码的 preproc_rules/ 目录,对应于 decoder.rulespreprocessor.rules 文件, Decoder 与 Preprocessor 的 Rule 的启用需要在 snort.conf 中增加相应的配置:


    var PREPROC_RULE_PATH /path/to/preproc_rules
    ...
    include $PREPROC_RULE_PATH/preprocessor.rules
    include $PREPROC_RULE_PATH/decoder.rules

Decoder 与 Preprocessor 的 Rule 支持以下行为:

Rule 的样例 “对于 Ether-Type 为 0x0800 但 IPv4 首部中协议版本号不为 v4 的报文进行告警” 描述如下:

alert ( msg: "DECODE_NOT_IPV4_DGRAM"; sid: 1; gid: 116; rev: 1; \
        metadata: rule-type decode ; classtype:protocol-command-decode;)

Snort 支持多种类似的 Decoder 与 Preprocessor 处理,详细信息如下:

基本上常见的应用协议 Preprocessor 都支持

Detection 规则

Snort Detection 的 Rule 规则由 Rule Header 与 Options 两个部分构成

下面的例子: 从 $BAD_NETK 网络任意源端口到 $HOME 网络的 80 端口 且有效载荷中包含十六进制为 0x47 0x45 0x54(即 “GET”)字符串的报文

Rule Header

Detection 阶段的 Rule Action 比 Decoder/Preprocessor 阶段增加了 Activate / Dynamic 行为, Activate 定义流规则同时指定规则 ID (activates:ID),Dynmaic 规则通过指定的规则 ID 进行激活(activated_by:ID),这两个行为能够组合出更灵活的 Snort Rule 规则

Rule 样例 “检测到非 $HOME_NET 对 IMAP(TCP 80端口) 的 buffer overflow 攻击后,记录后续的 50 个报文首部信息” 如下:

activate tcp !$HOME_NET any -> $HOME_NET 143 (flags:PA; \
    content:"|E8C0FFFFFF|/bin"; activates:1; \
    msg:"IMAP buffer overflow!";)
dynamic tcp !$HOME_NET any -> $HOME_NET 143 (activated_by:1; count:50;)

通过 Activate/Dynamic 组合,在识别出流量后,可收集后续关联流量,以供进一步的流量分析处理。

Rule Options

规则 Options 是 Snort 的 Detetction Engine 最核心的技术,通过丰富的 Options 提供易用、灵活且强大的规则定义机制,规则的 Options 为 Rule 中的 (;) 定义的部分,通过 ; 定义多个 option,每个 option 内的关键字使用 : 标识。

包括四个分别为 flags, content, activatesmsg 关键字的 Option 样例:

activate tcp !$HOME_NET any -> $HOME_NET 143 (flags:PA;
    content:"|E8C0FFFFFF|/bin"; activates:1; \
    msg:"IMAP buffer overflow!";)

Snort 的 Rule Options 主要有以下四类:

通用选项(General)

关键字
关键字 格式 说明
msg msg:”message text” 用于 alert/log 引擎进行信息告警/记录及报文记录
reference reference:, ; [reference:, ;] 外部信息引用,包括 URL/CVE 等等
gid <div>gid:;</div> General ID,区分 Snort 事件分类,ID 大于 100 是给特定 Preprocessor 与 decoder
sid sid:; Snort Rule ID,每个 Rule 的唯一标识
rev rev:; sid 所对应 rule 的版本号
classtype classtype:; Rule 分类,Snort 已做了 4 个优先级 34 种分类
priority priority:; Rule 优先级
metadata metadata:key1 value1, key2 value2; Key-Value 格式存储的 Rule 扩展信息
样例
alert tcp any any -> any 7070 (msg:"IDS411/dos-realaudio"; \
    flags:AP; content:"|fff4 fffd 06|"; reference:arachnids,IDS411;)

alert tcp any any -> any 21 (msg:"IDS287/ftp-wuftp260-venglin-linux"; \
    flags:AP; content:"|31c031db 31c9b046 cd80 31c031db|"; \
    reference:arachnids,IDS287; reference:bugtraq,1387; \
    reference:cve,CAN-2000-1574;)
alert tcp any any -> any 80 (content:"BOB"; gid:1000001; sid:1; rev:1;)
alert tcp any any -> any 25 (msg:"SMTP expn root"; flags:A+; \
    content:"expn root"; nocase; classtype:attempted-recon;)
alert tcp any any -> any 80 (msg:"WEB-MISC phf attempt"; flags:A+; \
    content:"/cgi-bin/phf"; priority:10;)
alert tcp any any -> any 80 (msg:"EXPLOIT ntpdx overflow"; \
    dsize:>128; classtype:attempted-admin; priority:10 )
alert tcp any any -> any 80 (msg:"Shared Library Rule Example"; \
    metadata:engine shared; metadata:soid 3|12345;)

alert tcp any any -> any 80 (msg:"Shared Library Rule Example"; \
    metadata:engine shared, soid 3|12345;)

alert tcp any any -> any 80 (msg:"HTTP Service Rule Example"; \
    metadata:service http;)

载荷选项(Payload)

关键字

Snort 的 Payload 选项的关键字(Key)非常丰富,主要分为三大类

具体的参看下图 Payload Rule

样例

载荷查找样例:


# 匹配 HTTP Cookie 中包括 ABC 与 EFG 的流
alert tcp any any -> any 80 (content:"ABC"; content:"EFG"; http_cookie;)

# 匹配 TCP 报文 ABC 之后 10 个字节内存在 EFG 的流
alert tcp any any -> any any (content:"ABC"; content:"EFG"; within:10;)

# 匹配 HTTP 报文的 URI 满足 foo.php?id=<数值> 的流
alert tcp any any -> any 80 (content:"/foo.php?id="; pcre:"/\/foo.php?id=[0-9]{1,10}/iU";)

# 匹配 HTTP 报文首部包含 User-Agent 的流
alert tcp any any -> any 80 (content:"User-Agent:"; http_header; nocase;)

非载荷选项(Non-Payload)

关键字

Non-Payload 的关键字用于匹配 IP 层、传输层协议的关键字段,主要包括:

样例

非载荷查找样例:

# 记录 IGMP 报文
alert ip any any -> any any (ip_proto:igmp;)

# 记录源 IP 与目的 IP 相同报文
alert ip any any -> any any (sameip;)

# 记录客户端发出的小于 6 Byte 的 TCP 流
alert tcp any any -> any any (stream_size:client,<,6;)

检测后处理选项(Post-Detection)

关键字
样例

检测后处理样例:

# 记录 TELNET 中所有可见字符内容
log tcp any any <> any 23 (session:printable;)

# 丢弃 1 分钟内登录失败超过 30 次的 SSH 请求
drop tcp 10.1.2.100 any > 10.1.1.100 22 ( \
    msg:"SSH Brute Force Attempt";
    flow:established,to_server; \
    content:"SSH"; nocase; offset:0; depth:4; \
    detection_filter:track by_src, count 30, seconds 60; \
    sid:1000001; rev:1;)

[End]

Blog

Opinion

Project