STM32F407调试日志输出实战:除了串口1,还能用SWO和RTT吗?三种方案对比评测
2026/6/14 3:56:03
. 匹配任意单个字符(除了换行符) * 匹配前一个字符0次或多次 + 匹配前一个字符1次或多次 ? 匹配前一个字符0次或1次 {n} 匹配前一个字符n次 {n,} 匹配前一个字符至少n次 {n,m} 匹配前一个字符n到m次[abc] 匹配a、b或c中的任意一个 [^abc] 匹配除了a、b、c以外的任意字符 [a-z] 匹配a到z的任意小写字母 [0-9] 匹配数字 [[:alnum:]] 字母数字 [[:alpha:]] 字母 [[:digit:]] 数字\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_]^ 字符串开始 $ 字符串结束 \b 单词边界 \B 非单词边界\\ 反斜线 \. 点号 \* 星号 \+ 加号 \? 问号 \( 左括号 \) 右括号 \[ 左方括号 \] 右方括号 \{ 左花括号 \} 右花括号\xHH HH为两位十六进制数 \x{HHHH} 四位十六进制数示例:
\x27 单引号 ' \x26 与符号 & \x2d 减号-\x23 井号## 匹配URL编码的单引号%27|\x27# 匹配URL编码的注释(\x2d|%2d){2}# 匹配 --/P 启用PCRE库 /i 不区分大小写 /s 让.匹配换行符 /m 多行模式 /U 非贪婪模式pcre:"/正则表达式/标志";# 匹配基础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";# 匹配script标签pcre:"/<script[^>]*>.*?<\/script>/sPi";# 匹配JavaScript事件pcre:"/on\w+\s*=/Pi";# 匹配alert函数pcre:"/alert\s*\(.*?\)/Pi";# 匹配目录遍历pcre:"/(\.\.\/|\.\.\\\\)+/Pi";# 匹配绝对路径pcre:"/(\/etc\/|\/bin\/|C:\\\\)/Pi";# 匹配文件包含pcre:"/(include|require)(_once)?\s*\(/Pi";# 匹配系统命令pcre:"/(system|exec|shell_exec|passthru|popen)\s*\(/Pi";# 匹配管道符pcre:"/(\||\x3b|&&|\|\|)/Pi";# 匹配反引号pcre:"/`.*?`/Pi";# 先使用content快速过滤content:"select"; nocase; content:"from"; nocase;# 再用pcre精确匹配pcre:"/select\s+.*?\s+from\s+/Pi";# 避免长字符串匹配pcre:"/pattern[^&\r\n]{0,100}/Pi";# 贪婪匹配(默认)pcre:"/<script>.*<\/script>/sPi";# 非贪婪匹配(更高效)pcre:"/<script>.*?<\/script>/sPi";# 避免 - 复杂分组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";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;)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;)alert http any any->any any (msg:"Path Traversal Attack"; \ flow:established,to_server; \ http.uri; \ pcre:"/(\.\.%2f|\.\.\/|\.\.%5c|\.\.\\\\)/Pi"; \ sid:1003; rev:1;)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;)# 使用pcretest工具echo"yeah=1'waitfor delay'0:0:3'--"|pcretest -i# 在线测试网站# regex101.com# regextester.com# 测试规则语法suricata -T -c suricata.yaml# 测试特定规则suricata -c suricata.yaml --lua='print(require("rule").load("your_rule.rules"))'# 实时查看匹配tail-f /var/log/suricata/fast.log# 查看详细匹配信息tail-f /var/log/suricata/eve.json|jq'. | select(.alert)'flow:established,to_serverhttp.uri或http.request_body使用正确content先行过滤掌握这些内容后,你就能编写高效、准确的Suricata正则规则了!