如何将正则表达式变量提交给 fail2ban 中的动作脚本?

How to commit a regex-variable to the action-script in fail2ban?

我想使用 regex-variable 提交从 filter.conf 到 action.conf 的 CIDR 子网掩码。这个想法是 block/ban 一个 ip-range fail2ban。由于 iptablesCIDR 一起工作,我想通过 fail2ban 使用它。这可能吗?


所以我想将 failregex 变量 mask 提交给动作脚本。这是我创建 mask 变量的 failregex:

filter.d/test-filter.conf

[Definition]
failregex = ^<HOST>\/(?P<mask>(8|16|24|32))


我的测试字符串如下所示:10.10.10.10/16 [2015-02-11 12:00:00]
这是我的 action-script 如果我直接设置 mask 变量或通过 jail.local (比如: action = test-action[mask=16]).

action.d/test-action.conf

[Definition]
...
actionban = iptables -I fail2ban-<name> 1 -s <ip>/<mask> -j DROP
actionunban = iptables -D fail2ban-<name> -s <ip>/<mask> -j DROP
...

[Init]
# Option:  mask
# Notes.:  used to ban an address-range by netmask(s) in CIDR notation.
# Values:  [ 32 | 24 | 16 | 8 ] Default: 32
#
mask = 32

这是我的jail.local(没有设置任何变量)。

jail.local

[test]
enabled   = true
action    = test-action
filter    = test-filter
logpath   = /etc/fail2ban/test.log
maxretry  = 5
findtime  = 1000
bantime   = -1

您问题的答案是。标签 <ip> <matches> <failures> etc. 都是 fail2ban 提供的特殊标签,您无法访问 ACTIONS 中过滤器正则表达式捕获组的任何数据。

我已经在 fail2ban 项目中针对此类功能打开了一个问题。 https://github.com/fail2ban/fail2ban/issues/1110

自从问了这个问题后,有一个很少记录的方法可以使用此类变量将数据从过滤器传递到操作:
您只需要在变量名前加上 'F-' 并且整个名称必须大写..
因此,在您的情况下,在您的过滤器中,您将使用:

[Definition]
failregex = ^<HOST>\/(?P<F-MASK>(8|16|24|32)</F-MASK>)

您的操作将如下所示:

[Definition]
...
actionban = iptables -I fail2ban-<name> 1 -s <ip>/<F-MASK> -j DROP
actionunban = iptables -D fail2ban-<name> -s <ip>/<F-MASK> -j DROP
...

很抱歉重新提出一个老问题..