fail2ban 不会将 IP 添加到 ipset(firewalld)

fail2ban 不会将 IP 添加到 ipset(firewalld)

由于某些我无法弄清楚的原因,Fail2Ban 拒绝将 IP 地址添加到 ipset/firewalld。

我被一个中国 IP 地址暴力破解,Fail2Ban 似乎确实试图禁止它(至少它出现在 fail2ban-client status sshd 命令中),但它实际上并没有停止暴力破解服务器,而且它没有出现在 ipset/iptables/firewalld 中。

fail2ban-客户端状态sshd:

[root@GITserver alex]# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed: 2
|  |- Total failed: 93613
|  `- File list:    /var/log/secure
`- Actions
   |- Currently banned: 2
   |- Total banned: 1003
   `- Banned IP list:   61.177.172.17 95.103.88.106

ipset/firewalld:

[root@GITserver alex]# ipset list
[root@GITserver alex]# firewall-cmd --direct --get-all-rules
[root@GITserver alex]#

有什么想法吗?可以根据要求添加更多日志

编辑,仍然没有解决方案,仍然受到暴力破解和 DDoS 攻击,这是完整的 /var/log/fail2ban.log,希望它能有所帮助https://paste.fedoraproject.org/paste/Y5aPF8~WY~fpuhaZBDo7Ml5M1UNdIGYhyRLivL9gydE=/raw(警告:11mb)

任何建议都将不胜感激。仍然受到攻击。

编辑 2:查看日志,它似乎仍然使用 iptables,即使 /etc/fail2ban/jail.d/00-firewalld.conf 定义了 banaction=firewallcmd-ipset。我编辑了主 jail.conf 来修复这个问题,但什么也没改变。您可以在上面的日志中看到重新加载后的输出。

答案1

我花了很长时间尝试在 VPS(CentOS Linux 7.6.1810)上通过 VirtualMin 安装了 fail2ban 来让它正常工作。

在我的案例中,Fail2Ban 的日志显示它运行正常

tail -f /var/log/fail2ban.log

并正确记录了禁止的决定,但这些决定并未被处理到防火墙规则中。阅读完全部在 Moshe 发表的答案中,我尝试了三种解决方案,但没有成功,我发现Geraden07 的解决方案在从该论坛帖子链接的 GitHub 讨论中,在 Centos 上工作。

1. 创建新的操作规则(/etc/fail2ban/action.d/custom-firewalld.conf)

[INCLUDES]
before  =

[Definition]
actionstart =
actionstop =
actioncheck =

actionflush = sed -i '/<source address=/d' /etc/firewalld/zones/drop.xml
actionban = firewall-cmd --change-source=<ip> --zone=drop && firewall-cmd --change-source=<ip> --zone=drop --permanent
actionunban = firewall-cmd --remove-source=<ip> --zone=drop && firewall-cmd --remove-source=<ip> --zone=drop --permanent || echo 0

[Init]

2. 创建/更新 jail 配置以使用此新规则作为默认规则(/etc/fail2ban/jail.local)

[DEFAULT]
banaction = custom-firewalld

3. 如果需要,更新所有覆盖默认规则的 jail,以使用新规则

我已经测试过禁止和取消禁止的功能,这是唯一对我有用的方法。

答案2

检查以确保firewallcmd-ipset 确实存在于您的系统上;它不再存在于 CentOS 7.3 及更高版本中。

基于防火墙的解决方法重新创建firewallcmd-ipset。我即将在我的系统上实现它。

答案3

我遇到了同样的问题,我认为 fail2ban 中有几个错误需要修复,例如,00-firewalld.conf 似乎被完全忽略了。

为了解决这个问题在 sshd 过滤器部分设置所需的操作。我的 sshd jail 如下所示 (/etc/fail2ban/jail.d/sshd.local)

[sshd]
enabled = true
port = ssh
# set the action explicitly or the default iptables-allports will be used
# ==============================
action = firewallcmd-ipset
# ==============================
logpath = %(sshd_log)s
findtime = 600
maxretry = 3
bantime = 86400

现在的问题是,你的 jail 中的 bantime 被忽略,而 /etc/fail2ban/action.d/firewalld-ipset.conf 中定义的 bantime 优先。因此,编辑该操作配置文件以包含你的新 bantime

...
bantime=86400
...

最后一个问题是 bantime=-1 似乎无法永久封禁。开发人员固定的这个在 fail2ban +0.9 中但它对我来说不起作用,尽管我使用的是 fail2ban-0.9.7-1.el7

对于您的商品,我所做的就是将firewallcmd-ipset.conf复制到firewalldcmd-ipset-custom.conf并在那里编辑禁令。最后在您的sshd jail定义中将操作设置为action = firewallcmd-ipset-custom

更改后重新启动 fail2ban 服务。

一些输出:

# firewall-cmd --direct --get-all-rules
ipv4 filter INPUT_direct 0 -p tcp -m multiport --dports ssh -m set --match-set fail2ban-default src -j REJECT --reject-with icmp-port-unreachable
# iptables -L -n | grep fail2ban
REJECT tcp -- 0.0.0.0/0   0.0.0.0/0    multiport dports 22 match-set fail2ban-default src reject-with icmp-port-unreachable
# ipset list | head
Name: fail2ban-default
Type: hash:ip
Revision: 1
Header: family inet hashsize 1024 maxelem 65536 timeout 86400
Size in memory: 53520
References: 1
Members:
x.x.x.x timeout 78498
y.y.y.y timeout 78506
z.z.z.z timeout 78454

我仍然不明白为什么 ipset 被称为 fail2ban-default 而不是 fail2ban-sshd,可能是我的配置有误,但是我描述的步骤正在起作用,现在 IP 已被阻止,并且我在 iptables 中不再有 +1000 条规则。

答案4

Wordpress 的示例规则:

cd /etc/fail2ban/filter.d
sudo -e wordpress.conf

粘贴、保存并关闭

#Fail2Ban filter for WordPress
[Definition]
failregex =  - - [(\d{2})/\w{3}/\d{4}::: -\d{4}] "POST /wp-login.php HTTP/1.1" 200
ignoreregex =

然后为 Wordpress 创建一个监狱。

cd /etc/fail2ban/jail.d
sudo -e wordpress.local

粘贴、保存并关闭

[wordpress]
enabled = true
port = http,https
#port - 80,443  // sometimes port = http,https does not work
filter = wordpress
action = firewallcmd-ipset
logpath = /var/log/httpd/access_log 
#CentOS - Pay attention if it is _ or . 
#In the file /etc/httpd/conf/httpd.conf you have information about where the log is 
stored.
#logpath = /var/log/apache2/access.log // Ubuntu/Debian
findtime = 600
maxretry = 3
bantime = 86400

相关内容