了解 Nginx 上 运行 fail2ban-regex 时的 fail2ban 测试输出

Understanding fail2ban test output when running fail2ban-regex on Nginx

我正在 Ubuntu 18.04 Digital Ocean droplet 运行 Nginx 上设置 fail2ban。

我按照 this tutorial for setup and this tutorial 创建了一个自定义过滤器来捕获对禁止 url 的请求。

这是我的过滤器:

[Definition]
failregex = ^ \[error\] \d+#\d+: .* forbidden .*, client: <HOST>, .*$

ignoreregex =

这是我试图捕获的示例日志文件条目的样子:

2020/03/09 12:18:08 [error] 14843#14843: *8 access forbidden by rule, client: xx.xx.xx.xx, server: xx.xx.xx.xx, request: "GET /var/ HTTP/1.1", host: "xxx.xxx.xxx.xx"

这是我的测试 运行:

/etc/fail2ban/filter.d# fail2ban-regex /var/log/nginx/error.log /etc/fail2ban/filter.d/nginx-forbidden.conf

这是输出:

Running tests
=============

Use   failregex filter file : nginx-forbidden, basedir: /etc/fail2ban
Use         log file : /var/log/nginx/error.log
Use         encoding : UTF-8


Results
=======

Failregex: 131 total
|-  #) [# of hits] regular expression
|   1) [131] ^ \[error\] \d+#\d+: .* forbidden .*, client: <HOST>, .*$
`-

Ignoreregex: 0 total

Date template hits:
|- [# of hits] date format
|  [200] {^LN-BEG}ExYear(?P<_sep>[-/.])Month(?P=_sep)Day(?:T|  ?)24hour:Minute:Second(?:[.,]Microseconds)?(?:\s*Zone offset)?
|  [254] ExYear(?P<_sep>[-/.])Month(?P=_sep)Day(?:T|  ?)24hour:Minute:Second(?:[.,]Microseconds)?(?:\s*Zone offset)?
`-

Lines: 1060 lines, 0 ignored, 131 matched, 929 missed
[processed in 0.47 sec]

Missed line(s): too many to print.  Use --print-all-missed to print all 929 lines

查看我的日志文件,大约有 1060 行,其中 200 行包含日期。在这 200 个中,有 131 个包含 'forbidden',所以我认为它可能有效,但我不确定。

这是我的问题:

1) 什么是'date template hits'?它们是以预期格式的日期开头的行吗?因此,它们 'candidates' 是应该匹配的日志条目吗?

2) 'ignored'和'missed'有什么区别?

3) 有没有办法查看 131 'matched' 个条目,以便我可以检查它们看起来像我要捕捉的内容?

4) 过滤器中的正则表达式显示 client: <HOST>,匹配的是什么?是日志条目中的 'client' IP 地址,还是行尾的 'host' 条目?

一旦您知道这些术语的含义,答案可能就很明显了,但由于我的无知,我找不到任何足够简单的解释。

太晚了,但是...

  1. fail2ban 通过尝试将它们与默认日期模式(模板)相匹配来搜索日志中的日期。如果您命中,则匹配了日期。在您的示例中,共有 found/matched 两种不同的日期格式(一种格式分别为 200 个日期和另一种格式的 254 个日期)。

  2. 忽略的行是与您的 ignoreregex 匹配的行(如果存在)(在您的情况下有 none)。遗漏的行是那些没有被任何 failregexignoreregex.

    匹配的行
  3. --print-all-matched 标记添加到您的 fail2ban-regex 命令中

  4. client: <HOST>, 将匹配日志行中的 client: xx.xx.xx.xx, 部分。 <HOST> 是与主机名或 IP 地址匹配的预定义正则表达式的别名。