微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

fail2ban 和特定的 RegEx

如何解决fail2ban 和特定的 RegEx

我需要一个 fail2ban 过滤器的帮助,该过滤器应该匹配 openHAB 中失败的登录尝试。

06-Apr.-2021 18:49:52.726 [WARN ] [core.io.http.auth.internal.AbstractAuthPageServlet] - Authentication Failed: Wrong password for user UserThatFailedLogin

06-Apr.-2021 19:41:11.456 [WARN ] [core.io.http.auth.internal.AbstractAuthPageServlet] - Authentication Failed: User not found: UserThatFailedLogin

这些都是 A) 错误密码和 B) 不存在用户的日志条目示例。

我尝试了 https://regex101.com 并想出了以下 RegEx(我的第一个接触点);

(\bAuthentication Failed: Wrong password\b|\bAuthentication Failed: User not found\b)

虽然它在 regex101-tool 中匹配上述示例日志条目,但不幸的是我在 fail2ban 中没有看到任何匹配项。

如果您能在这里帮助我,将不胜感激,非常感谢!

解决方法

你到底想禁止什么?

首先,您的示例中的两条消息都不包含任何要禁止的标识符 - 既不包含 IP,也不包含除用户名之外的其他内容。可以禁止一切,但您肯定不会禁止用户名。

然后你需要提供一个自定义的 datepattern,它会像下面这样(只是我不确定月份标记是否总是匹配(因为它后面有 .) .

假设我们在 failed 之后有一个 IP 地址,如下所示:

06-Apr.-2021 18:49:52.726 [WARN ] [core.io.http.auth.internal.AbstractAuthPageServlet] - Authentication failed from 192.0.2.1: Wrong password for user UserThatFailedLogin
06-Apr.-2021 19:41:11.456 [WARN ] [core.io.http.auth.internal.AbstractAuthPageServlet] - Authentication failed from 192.0.2.1: User not found: UserThatFailedLogin

您的过滤器可能如下所示:

[Definition]
datepattern = %%d-%%b\.-%%Y %%H:%%M:%%S\.%%f
failregex = ^\s*\[WARN\s*\] \[core.io.http.auth.internal.AbstractAuthPageServlet\] - Authentication failed from <ADDR>: (?:Wrong password for user|User not found:) <F-USER>\S+</F-USER>

但是没有任何标识符可以禁止,嗯...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。