将日志字符串转换为 fail2ban 的正则表达式

Convert log string into regex for fail2ban

我在转换与 VNC 服务器和客户端交互相关的日志字符串时遇到问题,因此我可以使用 fail2ban 禁止某些 IP。

VNC 日志中有问题的字符串如下:

** (vino-server:28986): WARNING **: 01:02:54.300: VNC authentication failure from '888.88.9.999.dynamic.whatever.net'

这是我需要匹配的确切字符串(带有所有括号、星号...),显然告诉 fail2ban 命令后面的主机所在的位置。

到目前为止我尝试了什么:

执行检查的命令: fail2ban-regex "log line" "failregex"


fail2ban-regex "00:19:51.297: VNC authentication failure from 'server-185-153-197-251.cloudedic.net'" "VNC authentication failure from '<HOST>'"

有效;但是日志行字符串与日志中的不一样。

当使用准确的日志行时,我不断失败:

fail2ban-regex "** (vino-server:11241): WARNING **: 00:19:51.297: VNC authentication failure from 'server-185-153-197-251.cloudedic.net'" "authentication failure from '<HOST>'"

如果你能帮助我朝着正确的方向前进,我会非常感激,这样我就可以更多地了解正则表达式并能够保护系统。

这基本上与正则表达式无关,更多的是 fail2ban 的东西 - 为了避免与预期数据混淆,它的几个默认日期模式是 "anchored" 到行的开头(特别是最简单的时间,比如你的格式),所以你必须为此指定你自己的datepattern

这应该适合你:

msg="** (vino-server:11241): WARNING **: 00:19:51.297: VNC authentication failure from 'server-185-153-197-251.cloudedic.net'"
fail2ban-regex -d '\s%H:%M:%S\.%f:' "$msg" "authentication failure from '<HOST>'"

请注意,fail2ban 会在 failregex 搜索开始之前删除消息匹配 datepattern 的部分,因此提前锚定 failregex 您的日志摘录如下:

fail2ban-regex -d '\s%H:%M:%S\.%f:' "$msg" "^\*\* \(\S+\): WARNING \*\*: VNC authentication failure from '<HOST>'"

另请注意,在 fail2ban 配置文件中,您必须使用 %% 指定 %-char,因此它看起来像这样:

datepattern = \s%%H:%%M:%%S\.%%f: