【问题标题】:fail2ban does not match date patternfail2ban 与日期模式不匹配
【发布时间】:2021-01-02 14:28:38
【问题描述】:

我正在尝试调试我的 fail2ban 过滤器和一些关于我的自定义日期模式的奇怪错误,并偶然发现了这个 documentation。 根据该命令的输出 fail2ban-regex "2013-09-19 02:46:12 1.2.3.4" "<HOST>" 应该显示如下:

Date template hits:
|- [# of hits] date format
|  [1] Year-Month-Day Hour:Minute:Second

但是这在我的系统上已经不起作用了,因为我得到的输出是:

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

Use   failregex line : <HOST>
Use      single line : 2013-09-19 02:46:12 1.2.3.4


Results
=======

Failregex: 0 total

Ignoreregex: 0 total

Date template hits:

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

|- Missed line(s):
|  2013-09-19 02:46:12 1.2.3.4

为什么fail2ban 甚至不能正确识别日期?

PS:

$ fail2ban-client -V
0.11.2
$ fail2ban-regex -V
0.11.2
$ uname -r
5.8.18-1-MANJARO

【问题讨论】:

    标签: fail2ban


    【解决方案1】:

    这也不适用于我的系统,这意味着 fail2ban 显然无法识别该日期格式。

    它会以不同的格式识别相同的日期,例如:

    fail2ban-regex "Sep 19 02:46:12 2013 1.2.3.4" "<HOST>"
    
    Date template hits:
    |- [# of hits] date format
    |  [1] {^LN-BEG}(?:DAY )?MON Day %k:Minute:Second(?:\.Microseconds)?(?: ExYear)?
    

    这就是您可以通过指定自定义日期模式来强制它识别您的时间戳的方法:

    fail2ban-regex -d '%Y-%m-%d %H:%M:%S' "2013-09-19 02:46:12 1.2.3.4" "<HOST>"
    
    Date template hits:
    |- [# of hits] date format
    |  [1] Year-Month-Day 24hour:Minute:Second
    

    【讨论】:

    • 你从哪里得到 datepattern 选项 -d 的格式?到处都找不到。就我而言: 'fail2ban-regex -d '%d/%m/%Y:%H:%M:%S' "07/Jun/2019:16:02:47 1.2.3.4" "" ' 失败并显示 'Use datepattern : Day/Month/Year:24hour:Minute:Second Use failregex line : Use single line : 07/Jun/2019:16:02:47 1.2.3.4' 然后 0 被忽略,0匹配,错过 1 个
    • 刚刚找到这个链接,还有albricias!我们只需要假设/知道他们使用 IBM 的“precambric”格式:[ibm.com/docs/en/cmofm/9.0.0?topic=SSEPCD_9.0.0/…jajajajaja....哦,抱歉,刚刚将 %m 替换为 %b ,这是三个月份的缩写形式字母。
    • 所以我有一个很好的日期模式,现在我应该把它放在过滤器文件的什么地方让它工作?
    【解决方案2】:

    你的问题的答案是你是正确的,它不会起作用!他们最终在 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

    【讨论】:

    • 我现在使用的是最新版本 1.01。奇迹般有效。只是不要做我做的事,帮自己一个忙,卸载旧版本并在安装新版本之前用 apt 清理。此外,如果您的服务器中有多个版本的 Phyton,请确保在命令中使用 Phyton3 或 4 安装它。否则作者告诉我它可以选择一个较旧的模块。
    • 您的评论@Julio Spinelli 中有错字吗?最新版本是 0.11.2 而你使用的是 1.01 ?这些数字之间似乎有相当大的距离。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    相关资源
    最近更新 更多