永久禁止 IP 时 fail2ban 的行为异常

永久禁止 IP 时 fail2ban 的行为异常

根据文档,将 jail bantime 设置为负值应导致永久禁令。但是,一旦完成此操作,与将 bantime 设置为正整数相比,以下行为会发生变化:

1)ipset list不显示 fail2ban-sshd 哈希表

2)firewall-cmd --direct --get-all-rules为空

3)/var/log/fail2ban.log变成一行。有趣的条目

sshd[25772]: 来自 93.174.89.88 端口 37477 的无效用户 ubuntu', 'ip': '93.174.89.88', 'ipmatches': 位于 0x7f4588f9dc08>, 'ipfailures': 位于 0x7f4588f9daa0>, 'time': 1536301842.088076, 'failures': 1443, 'ipjailfailures': 位于 0x7f4588f9dd70>})': 禁止 93.174.89.88 时出错

4)/var/log/messages具有以下

firewalld[916]: 警告: '/usr/sbin/iptables-restore --wait=2 -n' 失败: iptables-restore v1.4.21: 设置 fail2ban-sshd 不存在。#012#012错误发生在第 2 行#012请尝试“iptables-restore -h”或“iptables-restore --help”获取更多信息。firewalld[916]: 错误: COMMAND_FAILED

唯一按预期工作的命令是fail2ban-client status sshd,但是显示被禁止的 IP 仍然尝试连接。我认为所有问题的根源在于,一旦整数为负数,ipset 就不会因任何原因被创建。

有什么想法吗?另外,该命令是否具有与应用新配置时fail2ban-client reload相同的效果?systemctl restart fail2ban.service

就我而言,/etc/fail2ban/jail.d/local.conf

[sshd]
enabled = true
bantime = -1
findtime = 3600
maxretry = 5
action = %(action_)s

答案1

这是旧版 fail2ban 的一个错误。它已经修复,但如果您的 Linux 发行版仍使用旧版本,您可能还需要一个解决方法。

GitHub 问题它解释了问题并修复了它,还提供了一种解决方法:

新版本已修复此问题。对于 0.9,您只需在 jail 内的操作中覆盖 bantime(超时)参数即可(ipset 持久规则的参数超时为 0)。

[sshd]
bantime = -1
action = %(banaction)s[name=%(__name__)s, bantime=0, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]

答案2

ipset 的最小超时时间只能为 0,最大超时时间只能为 2147483,请参见http://ipset.netfilter.org/ipset.man.html

timeout 所有集合类型在创建集合并添加条目时都支持可选的 timeout 参数。create 命令的 timeout 参数值表示新条目的默认超时值(以秒为单位)。如果创建集合时支持超时,则在添加条目时可以使用相同的 timeout 选项来指定非默认超时值。零超时值表示条目将永久添加到集合中。可以使用 -exist 选项重新添加元素来更改已添加元素的超时值。最大可能的超时值为 2147483(以秒为单位)。

用于创建 ipset 超时的值是 conf 值 bantime。由于 bantime conf 值中有一个 -1,所以您的系统在为 fail2ban 的 jails 创建 ipset 时出现错误,因为没有创建 ipset,因为您实际上是让系统运行如下内容:

ipset create fail2ban-sshd hash:ip timeout -1

可接受的超时值范围是 0-2147483 秒。之后,在没有名为 fail2ban-sshd 的 ipset 的情况下运行以下命令:

firewall-cmd --direct --add-rule ipv4 filter INPUT_direct 0 -p tcp -m multiport --dports http,https -m set --match-set fail2ban-sshd src -j REJECT --reject-with icmp-port-unreachable

或者:

iptables -t filter -I INPUT_direct 1 -p tcp -m multiport --dports http,https -m set --match-set fail2ban-sshd src -j REJECT --reject-with icmp-port-unreachable

肯定会失败,因为没有创建名为 fail2ban-sshd 的 ipset。您必须将 -1 bantime 更改为 0。

相关内容