failregex 遗漏条目

failregex misses entries

我需要点击 ›page not found‹ 这样的日志条目:

185.220.100.252 - - [13/May/2022:10:03:58 +0200] "GET /EXPLOIT.php HTTP/1.1" 404 14780 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"

这个 failregex 基本有效

^<HOST> -\s*- \[.*\] "GET .*" 404 \d+ "-" ".*"$

并从 30k 个条目中找到 8900 个。我正在测试

fail2ban-regex /var/log/apache2/scienceblog.at.access.log '^<HOST> -\s*- \[.*\] "GET .*" 404 \d+ "-" ".*"$'

也是

^<HOST> -\s*- \[.*.*\] "GET .*" 404 \d+ "-" ".*"$

但是当我尝试在方括号之间进行具体说明时,例如

^<HOST> -\s*- \[.*\d.*\] "GET .*" 404 \d+ "-" ".*"$
^<HOST> -\s*- \[.*\s.*\] "GET .*" 404 \d+ "-" ".*"$
^<HOST> -\s*- \[.* .*\] "GET .*" 404 \d+ "-" ".*"$
^<HOST> -\s*- \[\d.*\] "GET .*" 404 \d+ "-" ".*"$
^<HOST> -\s*- \[.*0200\] "GET .*" 404 \d+ "-" ".*"$
^<HOST> -\s*- \[.* .*\] "GET .*" 404 \d+ "-" ".*"$

任何东西 其他(更不用说评估整个日期字符串的正则表达式)过滤器找不到单个日志条目,我不知道为什么。我已经阅读过我在 fail2ban-regex 上和其他地方找到的内容,但无济于事。

failregex 匹配没有日期的日志文件条目,所以对于您的示例

185.220.100.252 - - [13/May/2022:10:03:58 +0200] "GET /EXPLOIT.php HTTP/1.1" 404 14780 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"

fail2ban 已自行提取日期

13/May/2022:10:03:58 +0200

并将其从日志条目中删除,因此实际上是将您的正则表达式与

匹配
185.220.100.252 - - [] "GET /EXPLOIT.php HTTP/1.1" 404 14780 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"

所以为您工作的正则表达式正在工作,因为

\[.*\]\[.*.*\] 都匹配 [] 但其他的只有在括号之间确实有内容时才匹配。

恕我直言,这一点都不直观,因为“缺失行”的输出包括日期:

Lines: 1 lines, 0 ignored, 0 matched, 1 missed
[processed in 0.01 sec]

|- Missed line(s):
|  185.220.100.252 - - [13/May/2022:10:03:58 +0200] "GET /EXPLOIT.php HTTP/1.1" 404 14780 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"

但您可以验证情况是否如此,因为这将给出一个成功的匹配:

'^<HOST> -\s*- \[\] "GET .*" 404 \d+ "-" ".*"$'

进一步阅读:

https://dee.underscore.world/blog/fail2ban-filters/