你的问题的答案是你是正确的,它不会起作用!他们最终在 0.10 中修复它并在 0.11 中改进。这是我使用修复答案的作者之一构建的示例,该答案类似于您的问题:
从 0.10 开始,围绕日期模式提取的处理更加精确(因此更不容易受到攻击),请参阅 #1583 了解详细信息。
很快,如果在某侧没有指定锚点,它会自动获得一个单词边界 - 在您的情况下,datepattern 应该将字符串匹配到单词的结尾,但是在几秒钟之后,时区之前仍然有 3 位数字 050,所以它不匹配。
以下示例在 0.9.4 版本中运行良好:
# fail2ban-regex -v --datepattern=' ^%Y%m%d\s+%H%M%S' '20200313 122326050+1100 srv1 popserv 27511 31051 139647144740608 Note;AcctBadPswd(50/6) user=user@example.com:cmd=PASS <pswd>:fromhost=10.0.0.1' '(?:popserv).* (?:Note;AcctBadPswd).*fromhost=(?P<host>\S*).*'
Running tests
=============
Use datepattern : ^YearMonthDay\s+24hourMinuteSecond
Use failregex line : (?:popserv).* (?:Note;AcctBadPswd).*fromhost=(?P<h...
Use single line : 20200313 122326050+1100 srv1 popserv 27511 31051 1...
Results
=======
Failregex: 1 total
|- #) [# of hits] regular expression
| 1) [1] (?:popserv).* (?:Note;AcctBadPswd).*fromhost=(?P<host>\S*).*
| 10.0.0.1 Fri Mar 13 12:23:26 2020
`-
Ignoreregex: 0 total
Date template hits:
|- [# of hits] date format
| [1] ^YearMonthDay\s+24hourMinuteSecond
`-
Lines: 1 lines, 0 ignored, 1 matched, 0 missed
[processed in 0.00 sec]
在 0.11.1 上它无法匹配。
# fail2ban-regex -v --datepattern=' ^%Y%m%d\s+%H%M%S' '20200313 122326050+1100 srv1 popserv 27511 31051 139647144740608 Note;AcctBadPswd(50/6) user=user@example.com:cmd=PASS <pswd>:fromhost=10.0.0.1' '(?:popserv).* (?:Note;AcctBadPswd).*fromhost=<ADDR>.*'
Running tests
=============
Use datepattern : ^YearMonthDay\s+24hourMinuteSecond
Use failregex line : (?:popserv).* (?:Note;AcctBadPswd).*fromhost=<ADDR>.*
Use single line : 20200313 122326050+1100 srv1 popserv 27511 31051 1...
Results
=======
Failregex: 0 total
|- #) [# of hits] regular expression
| 1) [0] (?:popserv).* (?:Note;AcctBadPswd).*fromhost=<ADDR>.*
`-
Ignoreregex: 0 total
Date template hits:
|- [# of hits] date format
| [0] ^YearMonthDay\s+24hourMinuteSecond
`-
Lines: 1 lines, 0 ignored, 0 matched, 1 missed
[processed in 0.00 sec]
|- Missed line(s):
| 20200313 122326050+1100 srv1 popserv 27511 31051 139647144740608 Note;AcctBadPswd(50/6) user=user@example.com:cmd=PASS <pswd>:fromhost=10.0.0.1
`-
问题在于字符串的正确日期模式:
20200313 122326050+1100 ... 看起来像 ^%Y%m%d\s+%H%M%S%f?%z。
可能额外的 3 个数字是毫秒,现在将与 %f? 匹配,在哪里?使其成为可选(如果您不需要这样的毫秒精确时间,也可以将其替换为 \d+ 或 \d* 。
$ msg='20200313 122326050+1100 srv1 popserv 27511 31051 139647144740608 Note;AcctBadPswd(50/6) user=user@example.com:cmd=PASS <pswd>:fromhost=10.0.0.1'
$ fail2ban-regex -v --VD --datepattern='^%Y%m%d\s+%H%M%S%f?%z' "$msg" '(?:popserv).* (?:Note;AcctBadPswd).*fromhost=<ADDR>.*'
Running tests
=============
Use datepattern : ^YearMonthDay\s+24hourMinuteSecondMicroseconds?Zone offset
Use failregex line : (?:popserv).* (?:Note;AcctBadPswd).*fromhost=<ADDR>.*
Use single line : 20200313 122326050+1100 srv1 popserv 27511 31051 1...
Results
=======
Failregex: 1 total
|- #) [# of hits] regular expression
| 1) [1] (?:popserv).* (?:Note;AcctBadPswd).*fromhost=<ADDR>.*
| 10.0.0.1 Fri Mar 13 02:23:26 2020
`-
Ignoreregex: 0 total
Date template hits:
|- [# of hits] date format
| [1] ^YearMonthDay\s+24hourMinuteSecondMicroseconds?Zone offset
| # weight: 1.000 (1.000), pattern: ^%Y%m%d\s+%H%M%S%f?%z
| # regex: ^((?P<Y>\d\d\d\d)(?P<m>1[0-2]|0[1-9]|[1-9])(?P<d>3[0-1]|[1-2]\d|0[1-9]|[1-9]| [1-9])\s+(?P<H>2[0-3]|[0-1]\d|\d)(?P<M>[0-5]\d|\d)(?P<S>6[0-1]|[0-5]\d|\d)(?P<f>[0-9]{1,6})?(?P<z>Z|UTC|GMT|[+-][01]\d(?::?\d{2})?))(?=\b|\W|$)
`-
Lines: 1 lines, 0 ignored, 1 matched, 0 missed
[processed in 0.00 sec]
还请注意,第一个示例中的正则表达式不包含足够的锚点,因此这使其很弱并且可能被错误的日志条目触发。
我使用来自 Sebres 的信息构建了这个答案here