一、正则表达式基础语法
1. 基本元字符
. 匹配任意单个字符(除了换行符)
* 匹配前一个字符0次或多次
+ 匹配前一个字符1次或多次
? 匹配前一个字符0次或1次
{n} 匹配前一个字符n次
{n,} 匹配前一个字符至少n次
{n,m} 匹配前一个字符n到m次
2. 字符类
[abc] 匹配a、b或c中的任意一个
[^abc] 匹配除了a、b、c以外的任意字符
[a-z] 匹配a到z的任意小写字母
[0-9] 匹配数字
[[:alnum:]] 字母数字
[[:alpha:]] 字母
[[:digit:]] 数字
3. 预定义字符类
\d 数字 [0-9]
\D 非数字 [^0-9]
\s 空白字符 [ \t\r\n\f]
\S 非空白字符 [^ \t\r\n\f]
\w 单词字符 [a-zA-Z0-9_]
\W 非单词字符 [^a-zA-Z0-9_]
4. 边界匹配
^ 字符串开始
$ 字符串结束
\b 单词边界
\B 非单词边界
5. 转义字符
\\ 反斜线
\. 点号
\* 星号
\+ 加号
\? 问号
\( 左括号
\) 右括号
\[ 左方括号
\] 右方括号
\{ 左花括号
\} 右花括号
二、Suricata专用语法
1. 十六进制表示
\xHH HH为两位十六进制数
\x{HHHH} 四位十六进制数
示例:
yaml
\x27 单引号 '
\x26 与符号 &
\x2d 减号 -
\x23 井号 #
2. URL编码匹配
yaml
# 匹配URL编码的单引号
%27|\x27
# 匹配URL编码的注释
(\x2d|%2d){2} # 匹配 --
3. PCRE标志
/P 启用PCRE库
/i 不区分大小写
/s 让.匹配换行符
/m 多行模式
/U 非贪婪模式
三、Suricata规则中正则的用法
基本格式
yaml
pcre:"/正则表达式/标志";
常用模式示例
1. SQL注入检测
yaml
# 匹配基础SQL关键词
pcre:"/(select|union|insert|update|delete|drop|create|alter)\s+/Pi";
# 匹配SQL注释
pcre:"/(--|\x23|\/\*.*?\*\/)/sPi";
# 匹配单引号注入
pcre:"/['\x27%27].*?['\x27%27]/Pi";
# 匹配时间盲注
pcre:"/(waitfor\s+delay|sleep\s*\(|benchmark\s*\()/Pi";
2. XSS攻击检测
yaml
# 匹配script标签
pcre:"/<script[^>]*>.*?<\/script>/sPi";
# 匹配JavaScript事件
pcre:"/on\w+\s*=/Pi";
# 匹配alert函数
pcre:"/alert\s*\(.*?\)/Pi";
3. 文件包含/路径遍历
yaml
# 匹配目录遍历
pcre:"/(\.\.\/|\.\.\\\\)+/Pi";
# 匹配绝对路径
pcre:"/(\/etc\/|\/bin\/|C:\\\\)/Pi";
# 匹配文件包含
pcre:"/(include|require)(_once)?\s*\(/Pi";
4. 命令注入
yaml
# 匹配系统命令
pcre:"/(system|exec|shell_exec|passthru|popen)\s*\(/Pi";
# 匹配管道符
pcre:"/(\||\x3b|&&|\|\|)/Pi";
# 匹配反引号
pcre:"/`.*?`/Pi";
四、性能优化技巧
1. 使用content先行过滤
yaml
# 先使用content快速过滤
content:"select"; nocase;
content:"from"; nocase;
# 再用pcre精确匹配
pcre:"/select\s+.*?\s+from\s+/Pi";
2. 限制匹配长度
yaml
# 避免长字符串匹配
pcre:"/pattern[^&\r\n]{0,100}/Pi";
3. 使用非贪婪匹配
yaml
# 贪婪匹配(默认)
pcre:"/<script>.*<\/script>/sPi";
# 非贪婪匹配(更高效)
pcre:"/<script>.*?<\/script>/sPi";
4. 避免复杂回溯
yaml
# 避免 - 复杂分组
pcre:"/(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)+/Pi";
# 推荐 - 使用字符类
pcre:"/[a-z]+/Pi";
五、实战示例
示例1:检测URL参数中的SQL注入
yaml
alert http any any -> any any (msg:"SQLi in URL Parameter"; \
flow:established,to_server; \
http.uri; \
pcre:"/\?.*?=[^&]*?(union\s+select|select\s+.*?\s+from|waitfor\s+delay|['\x27].*?['\x27])/Pi"; \
sid:1001; rev:1;)
示例2:检测POST数据中的XSS
yaml
alert http any any -> any any (msg:"XSS in POST Body"; \
flow:established,to_server; \
http.request_body; \
pcre:"/<script[^>]*>|javascript:|on\w+\s*=|alert\s*\(/Pi"; \
sid:1002; rev:1;)
示例3:检测路径遍历攻击
yaml
alert http any any -> any any (msg:"Path Traversal Attack"; \
flow:established,to_server; \
http.uri; \
pcre:"/(\.\.%2f|\.\.\/|\.\.%5c|\.\.\\\\)/Pi"; \
sid:1003; rev:1;)
示例4:检测命令注入
yaml
alert http any any -> any any (msg:"Command Injection"; \
flow:established,to_server; \
http.request_body; \
content:"cmd="; nocase; \
pcre:"/cmd=.*?[;&|`].*?(ls|cat|id|whoami|netstat)/Pi"; \
sid:1004; rev:1;)
六、调试与测试
1. 测试正则表达式
bash
# 使用pcretest工具
echo "yeah=1'waitfor delay'0:0:3'--" | pcretest -i
# 在线测试网站
# regex101.com
# regextester.com
2. Suricata测试命令
bash
# 测试规则语法
suricata -T -c suricata.yaml
# 测试特定规则
suricata -c suricata.yaml --lua='print(require("rule").load("your_rule.rules"))'
3. 查看匹配日志
bash
# 实时查看匹配
tail -f /var/log/suricata/fast.log
# 查看详细匹配信息
tail -f /var/log/suricata/eve.json | jq '. | select(.alert)'
七、常见问题解决
1. 规则不匹配
- 检查
flow:established,to_server - 确认
http.uri或http.request_body使用正确 - 检查正则表达式标志是否正确
2. 性能问题
- 使用
content先行过滤 - 避免过于复杂的正则
- 限制匹配范围
3. 误报率高
- 精确匹配路径/参数
- 增加白名单机制
- 使用阈值限制
4. 漏报问题
- 考虑多种编码方式
- 覆盖更多变体
- 使用多重规则检测
八、最佳实践
- 分层防御:先用简单规则过滤,再用复杂正则
- 测试验证:所有规则都要经过测试
- 性能监控:监控Suricata的CPU和内存使用
- 定期更新:更新正则表达式库
- 日志分析:定期分析告警日志优化规则
- 黑白名单:结合使用减少误报
九、资源推荐
- PCRE官方文档:http://www.pcre.org/
- Suricata规则文档:https://suricata.readthedocs.io/
- 正则测试工具:https://regex101.com/
- OWASP正则库:https://owasp.org/www-community/OWASP_Validation_Regex_Repository
掌握这些内容后,你就能编写高效、准确的Suricata正则规则了!