gpt4 book ai didi

CentOS EPEL fail2ban 不处理 tomcat 的 systemd 日志

转载 作者:行者123 更新时间:2023-12-04 19:31:49 25 4
gpt4 key购买 nike

我已经在 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

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

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

最佳答案

为了确保过滤器定义被正确初始化,最好包含通用定义。您的过滤器定义(/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 而不是平面文件中,您可以在没有 $ 的情况下尝试相同的模式吗?在最后?打印到期刊时,行尾可能有问题?
在您的 jail 定义(/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 服务。

关于CentOS EPEL fail2ban 不处理 tomcat 的 systemd 日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61878538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com