潜在的ufw和fail2ban冲突

运行fail2ban和ufw会导致问题吗?我注意到fail2ban会修改iptables规则,但是ufw已经定义了大量的iptables规则...所以我不确定fail2ban会不会搞乱这些规则。

哪个Ubuntu版本?('ufw'在10.04 LTS和11.x之间有所改进) - david6
@david6:抱歉,我不记得当初提问时使用的是哪个版本。 - Adam Monsen
3个回答

你可以同时使用ufw和fail2ban,但正如之前所指出的,规则的顺序是很重要的。
默认情况下,fail2ban使用iptables,并首先在INPUT链中插入规则。这不会对ufw造成任何伤害或冲突。
如果你希望完全整合fail2ban以使用ufw(而不是iptables),你需要编辑一些文件,包括...
/etc/fail2ban/jail.local

jail.local 是你定义服务的地方,包括它们监听的端口(比如将 ssh 改为非默认端口)以及要采取的操作。
请注意:绝对不要编辑 jail.conf,你应该在 jail.local 中进行修改!该文件开头是这样的:
# Changes:  in most of the cases you should not modify this
#           file, but provide customizations in jail.local file,
#           or separate .conf files under jail.d/ directory

使用ssh作为例子,注意非默认端口的定义 =)
[ssh]
enabled = true
banaction = ufw-ssh
port = 2992
filter = sshd
logpath = /var/log/auth.log
maxretry = 3

您可以将fail2ban配置为使用ufw(每个服务一个.conf文件)。
/etc/fail2ban/action.d/ufw-ssh.conf

语法是
[Definition]
actionstart =
actionstop =
actioncheck =
actionban = ufw insert 1 deny from <ip> to any app OpenSSH
actionunban = ufw delete deny from <ip> to any app OpenSSH

注意:您配置fail2ban使用ufw并使用"insert 1"语法首先插入新规则。无论顺序如何,删除操作都能找到该规则。

这里有一篇详细的博文。

http://blog.vigilcode.com/2011/05/ufw-with-fail2ban-quick-secure-setup-part-ii/

[EDIT] For ubuntu 16.04+

by default a "defaults-debian.conf" in /etc/fail2ban/jail.d with content

[sshd]
enabled = true

will activated a the ssh protection of fail2ban.

You need to put it at false.

Then create a jail.local like you would do in general, mine would be like this:

[ssh-with-ufw] 
enabled = true 
port = 22 
filter = sshd 
action = ufw[application="OpenSSH", blocktype=reject] 
logpath = /var/log/auth.log 
maxretry = 3

There is already a ufw.conf in the fail2ban default installation so no need to create one.

The only specific change for you jail.local would be at action line where you need to put the application concerned for the protection and what you want to get as result.

ufw tend to detect automatically a certain amount of app running using the network. To have the list just type sudo ufw app list. It's case-sensitive.

reload fail2ban and you'll no longer see the fail2ban chain and if any IP get a block you'll see it in sudo ufw status


7所以简而言之,如果不按照说明进行集成,ufw和fail2ban都会按照预期工作。Fail2ban将在应用ufw规则之前插入其阻止定义。另一方面,如果希望在ufw status中显示封锁的内容,就需要进行集成。除了在ufw status中显示封锁的内容外,还有其他好处吗?特别是因为博客作者说过以下话:开箱即用的Fail2ban适用于iptables规则,但这些规则与我们更简单的UFW命令不兼容(...) - bouke
1没错。“Don't play nice” 相当于在检查 ufw 状态时不要显示出来。集成的好处是你只需使用一个工具ufw来管理和显示防火墙规则。关于功能而言,使用原装的fail2ban没有问题。问题在于如果想要查看 fail2ban 的规则,你需要使用 iptables -L -v -n 命令,正如你已经看到的那样,当使用 ufw 时,输出结果很长且难以理解。集成的优势在于规则和语法更易于理解(假设这也是你为什么首选 ufw 的原因)。 - Panther
就记录而言,如果您在您提供的网站上点击任何地方,您将被重定向到恶意软件/广告软件。 - Antonio Cangiano
@AntonioCangiano - 这里链接正常工作,请检查您的浏览器和DNS设置。 - Panther
@bodhi.zazen 正如Antonio所指出的那样,点击链接文章将会重定向到一个不良网站。因此,我对从这样的文章中获取安全建议持保留态度。 - Goran Miskovic
@GoranMiskovic - 这里链接有效,不确定你所说的“不受信任的网站”是什么意思 - 如果你不喜欢这个网站,就不要使用它。我在上面的回答中发布的说明依然适用,即使没有链接。 - Panther
@GoranMiskovic 我不明白你的意思。我没有看到任何重定向。 - terdon
点击链接查看文章第一部分。这将带您到facebook.com-prize.us。这里有一篇更好的文章。请访问https://www.digitalocean.com/community/tutorials/how-to-protect-ssh-with-fail2ban-on-ubuntu-14-04。 - Goran Miskovic
@GoranMiskovic - 我无法在这里复制你报告的行为。你在使用什么DNS服务? - Panther
看起来那个网站正在使用一个声誉不佳的广告网络,有时会弹出一则广告,当点击后运行恶意代码。 - Eric J.
@antivirtel我们还需要创建一个ufw-ssh配置文件吗?因为action.d中已经有一个ufw.conf在运行。 - vigilian
2我想要加一句话,不推荐使用ufw的操作,因为它不能阻止已建立的连接。所以在ip规则最终阻止某个IP之前,你仍然可能收到成千上万次的攻击请求。默认设置效果很好。在ufw status中看到被阻止的IP是没有意义的,因为你可以使用fail2ban-client工具来查看被禁止的IP。 - Michael Härtl

安装0.9.5版本的fail2ban包含了一个名为ufw的操作,我只需要将其设置为banaction即可。

2就记录而言,在0.8.13版本中也存在这个功能。 - Joril

我多年来一直在几台不同的电脑上使用fail2ban和ufw,从未遇到任何问题。设置fail2ban的步骤如下:
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano jail.local

现在按照您的意愿编辑文件,例如,如果您想要阻止未经授权的ssh,请找到以下行:
[ssh]
enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 6

如果"enabled"设置为"false",请根据此处的说明将其更改为"true"。 在设置规则后,您需要重新启动fail2ban进程。
sudo /etc/init.d/fail2ban restart

如果您在ufw防火墙上打开了22号端口,fail2ban将会封禁那些尝试连接超过6次但未成功的客户端,这不会破坏您的防火墙。