【问题标题】:fail2ban regular to find 403 request in nginxfail2ban 定期在 nginx 中查找 403 请求
【发布时间】:2014-09-11 03:10:38
【问题描述】:

我想找到403请求并禁止它们,这是我的日志格式

112.253.6.182 - - [08/Sep/2014:17:42:56 -0400] "GET / HTTP/1.1" 403 579 "baidu" "Mozilla/4.0" 50.117.86.72
106.37.177.251 - - [08/Sep/2014:17:42:56 -0400] "GET /index.php HTTP/1.1" 404 576 "baidu" "Mozilla/4.0" 204.44.65.173
190.254.173.14 - - [08/Sep/2014:17:42:56 -0400] "GET /index.php HTTP/1.1" 404 576 "baidu" "Mozilla/4.0" 204.44.65.173
41.222.196.37 - - [08/Sep/2014:17:42:56 -0400] "GET / HTTP/1.1" 403 579 "baidu" "Mozilla/4.0" 50.117.86.72

我的失败记录是:

failregex = ^<HOST> -.*"(GET|POST).*.php.*\ 403\ .*$

忽略正则表达式 =

但是当我使用 fail2ban-regex 命令对其进行测试时,它会在下面返回

Failregex: 32 total
|-  #) [# of hits] regular expression
|   1) [32] ^<HOST> -.*"(GET|POST).*.php.*\ 403\ .*$
`-

Ignoreregex: 0 total

Date template hits:
|- [# of hits] date format
|  [3266] Day/MONTH/Year:Hour:Minute:Second
`-

Lines: 3266 lines, 0 ignored, 32 matched, 3234 missed
Missed line(s): too many to print.  Use --print-all-missed to print all 3234 lines

你能帮我做一个正则表达式来匹配 403 请求并打印出 ip 吗?提前致谢

【问题讨论】:

    标签: regex linux nginx fail2ban


    【解决方案1】:

    首先,您的示例日志条目是 / 的 403s 和 /index.php 的 404s,而您的正则表达式尝试匹配 php 扩展名和 403 代码。难怪你没有对手。

    因此,如果您的兴趣只是 403 错误条目而不管路径如何,这应该可行。

    ^<HOST> .* "(GET|POST) [^"]+" 403
    

    要调试您的正则表达式,您可以使用this snippet。请注意,&lt;HOST&gt;fail2ban 预处理为 (?:::f{4,6}:)?(?P&lt;host&gt;\S+)

    【讨论】:

    • 非常感谢您的帮助。在线正则表达式测试器很棒。
    【解决方案2】:

    根据@saaj 的回答,虽然四行日志没有匹配项,但从完整日志的测试结果可以明显看出,有 32 个匹配项。也就是说,显然,新模式(不包括 php 扩展)确实捕获了更多的 403。

    一个更简单的匹配模式是:^&lt;HOST&gt; .* 403

    这可以通过命令行进行测试:

    fail2ban-regex '112.253.6.182 - - [08/Sep/2014:17:42:56 -0400] "GET / HTTP/1.1" 403 579 "baidu" "Mozilla/4.0" 50.117.86.72' '^&lt;HOST&gt; .* 403'

    对于积极的结果,并且

    fail2ban-regex '106.37.177.251 - - [08/Sep/2014:17:42:56 -0400] "GET /index.php HTTP/1.1" 404 576 "baidu" "Mozilla/4.0" 204.44.65.173' '^&lt;HOST&gt; .* 403'

    对于一个否定的结果。

    【讨论】:

      猜你喜欢
      • 2017-05-17
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      • 2019-02-11
      • 2016-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多