为 fail2ban 失败创建自己的过滤器

为 fail2ban 失败创建自己的过滤器

我正在 Kubernetes 中使用 fail2ban。为此,我创建了一个用于创建虚假日志消息的 pod:2021-08-04 18:33:13 身份验证失败 15.15.15.15 我创建了一个自定义过滤器,以测试 fail2ban 是否正常工作。我首先尝试使用包含 10 行这些日志消息的文件使用 fail2ban-regex 实用程序,并得到以下输出:

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

Use   failregex filter file : test, basedir: /etc/fail2ban
Use         log file : /logs.txt
Use         encoding : UTF-8


Results
=======

Failregex: 10 total
|-  #) [# of hits] regular expression
|   1) [10] \sAuthentication failed\s<HOST>
`-

Ignoreregex: 0 total

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

Lines: 10 lines, 0 ignored, 10 matched, 0 missed
[processed in 0.12 sec]

但是当我尝试对包含连续日志的文件使用相同的过滤器时,出现此错误:

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

Use   failregex filter file : test, basedir: /etc/fail2ban
Use         log file : /logs/kubernetes.logs
Use         encoding : UTF-8

Traceback (most recent call last):
 File "/usr/bin/fail2ban-regex", line 34, in <module>
   exec_command_line()
 File "/usr/lib/python3.8/site-packages/fail2ban/client/fail2banregex.py", line 836, in exec_command_line
   if not fail2banRegex.start(args):
 File "/usr/lib/python3.8/site-packages/fail2ban/client/fail2banregex.py", line 776, in start
   self.process(test_lines)
 File "/usr/lib/python3.8/site-packages/fail2ban/client/fail2banregex.py", line 584, in process
   line_datetimestripped, ret, is_ignored = self.testRegex(line)
 File "/usr/lib/python3.8/site-packages/fail2ban/client/fail2banregex.py", line 456, in testRegex
   found = self._filter.processLine(line, date)
 File "/usr/lib/python3.8/site-packages/fail2ban/server/filter.py", line 613, in processLine
   timeMatch = self.dateDetector.matchTime(line)
 File "/usr/lib/python3.8/site-packages/fail2ban/server/datedetector.py", line 368, in matchTime
   (line[distance] == self.__lastPos[2] and not self.__lastPos[2].isalnum())
IndexError: string index out of range

当我激活 fail2ban 过滤器时,没有 IP 被禁止,所以我认为我的过滤器没有工作,但我找不到错误。

过滤器.conf:

[Definition]

failregex = \sAuthentication failed\s<HOST>

答案1

这是一个已知错误(已修复),请参阅https://github.com/fail2ban/fail2ban/issues/3020

但实际原因是日期模式不精确,时间戳不完整(与默认日期模式集匹配)或某些行根本没有时间戳。解决方案是更新或(更好的)尽可能精确的日期模式,最好在开始(或结束)时固定。

相关内容