1

我已经在 CentOS 7.8 上安装了来自 EPEL 的 fail2ban 0.10.5-2.el7。我试图让它与 systemd 一起处理 Tomcat 日志(也是 systemd)。

在 jail.local 我添加:

[guacamole]
enabled = true
port     = http,https
backend = systemd

在 filter.d/guacamole.conf 中:

[Definition]
failregex = Authentication attempt from <HOST> for user "[^"]*" failed\.$
ignoreregex =
journalmatch = _SYSTEMD_UNIT=tomcat.service + _COMM=java

如果我运行 journalctl -u tomcat.service 我会看到所有日志行。我感兴趣的看起来像这样:

May 18 13:58:26 myhost catalina.sh[42065]: 13:58:26.485 [http-nio-8080-exec-6] WARN  o.a.g.r.auth.AuthenticationService - Authentication attempt from 1.2.3.4 for user "test" failed.

如果我将 journalctl -u tomcat.service 重定向到日志文件,并使用 fail2ban-regex 处理它,那么它会完全按照我想要的方式工作,找到它需要的所有行。

% fail2ban-regex /tmp/j9 /etc/fail2ban/filter.d/guacamole.conf

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

Use   failregex filter file : guacamole, basedir: /etc/fail2ban
Use         log file : /tmp/j9
Use         encoding : UTF-8


Results
=======

Failregex: 47 total
|-  #) [# of hits] regular expression
|   1) [47] Authentication attempt from <HOST> for user "[^"]*" failed\.$
`-

Ignoreregex: 0 total

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

Lines: 571 lines, 0 ignored, 47 matched, 524 missed
[processed in 0.12 sec]


但是,如果 fail2ban 直接读取日志,则它不起作用:

fail2ban-regex systemd-journal /etc/fail2ban/filter.d/guacamole.conf

它立即返回,并处理 0 行!

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

Use   failregex filter file : guacamole, basedir: /etc/fail2ban
Use         systemd journal
Use         encoding : UTF-8
Use    journal match : _SYSTEMD_UNIT=tomcat.service + _COMM=java


Results
=======

Failregex: 0 total

Ignoreregex: 0 total

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

我试图删除 _COMM=java. 这没什么区别。

如果我完全省略了日志匹配行,它至少会处理日志中的所有行,但不会找到任何匹配项(尽管正如我所提到的,它可以很好地处理日志文件的转储):

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

Use   failregex filter file : guacamole, basedir: /etc/fail2ban
Use         systemd journal
Use         encoding : UTF-8


Results
=======

Failregex: 0 total

Ignoreregex: 0 total

Lines: 202271 lines, 0 ignored, 0 matched, 202271 missed
[processed in 34.54 sec]

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

要么这是一个错误,要么我错过了一个小细节。

感谢您的任何帮助,您可以提供。

4

1 回答 1

0

为了确保过滤器定义被正确初始化,最好包含通用定义。您的过滤器定义(/etc/fail2ban/filter.d/guacamole.conf)因此看起来像:

[INCLUDES]

before = common.conf

[Definition]

journalmatch = _SYSTEMD_UNIT='tomcat.service'

failregex = Authentication attempt from <HOST> for user "[^"]*" failed\.$

ignoreregex =

一个小提示,鉴于您的问题仅发生在 systemd 而不是平面文件,您可以尝试相同的模式而不$在最后吗?打印到期刊时,行尾可能有问题?

在您的监狱定义(/etc/fail2ban/jail.d/guacamole.conf)中,如果默认配置中尚未定义禁令时间/查找时间/重试,请记住定义:

[guacamole]
enabled  = true
port     = http,https
maxretry = 3
findtime = 1h
bantime  = 1d

# "backend" specifies the backend used to get files modification.
# systemd: uses systemd python library to access the systemd journal.
# Specifying "logpath" is not valid for this backend.
# See "journalmatch" in the jails associated filter config
backend = systemd

请记住在进行此类更改后重新启动 fail2ban 服务。

于 2020-07-14T14:15:54.900 回答