Fail2ban 会检查日志文件(例如 /var/log/httpd/error_log
)并封禁存在恶意行为的 IP,例如大量身份验证请求,漏洞扫描等。Fail2ban 通常会在防火墙规则上将这些 IP 封禁一段时间,但也可以配置像发送邮件等其它操作。
安装
安装一下软件包之一:
- fail2ban包 - 最新的稳定版
- fail2ban-gitAUR - 包含添加到代码库的最新更改
用法
配置 Fail2ban 并启用/启动 fail2ban.service
。
fail2ban-client
fail2ban-client 可以用于管理 jail 的状态(包括重新加载、重启、查看状态等)。要查看所有可用命令:
$ fail2ban-client
要查看已启用的 jail:
# fail2ban-client status
要查看 jail 的状态(以 sshd 为例):
# fail2ban-client status sshd
Status for the jail: sshd |- Filter | |- Currently failed: 1 | |- Total failed: 9 | `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd `- Actions |- Currently banned: 1 |- Total banned: 1 `- Banned IP list: 0.0.0.0
要查看所有 jail 的简略信息(包括已封禁的 IP):
# fail2ban-client banned
[{'sshd': ['192.168.100.50']}, {'apache-auth': []}]
配置
jail.conf(5) § CONFIGURATION FILES FORMAT 建议用户创建 /etc/fail2ban/jail.local
文件,否则在更新时可能会为 /etc/fail2ban/jail.conf
创建新的 Pacnew 和 Pacsave 文件。
以将默认封禁时间设为一天为例:
/etc/fail2ban/jail.local
[DEFAULT] bantime = 1d
也可以在 /etc/fail2ban/jail.d
目录下创建单独的 name.local
文件(例如 /etc/fail2ban/jail.d/sshd.local
)。
重载 fail2ban.service
以应用配置更改。
启用 jail
所有 jail 默认都是禁用的。要启用 jail,需要在对应项添加 enabled = true
。以启用 OpenSSH jail 为例:
/etc/fail2ban/jail.local
[sshd] enabled = true
具体配置请参考 #配置 SSH jail。
接收告警邮件
如果你想在封禁时收到邮件提醒,需要参考下方配置 SMTP 客户端(例如 msmtp)并修改默认操作:
/etc/fail2ban/jail.local
[DEFAULT] destemail = yourname@example.com sender = yourname@example.com # to ban & send an e-mail with whois report to the destemail. action = %(action_mw)s # same as action_mw but also send relevant log lines #action = %(action_mwl)s
防火墙和服务
Fail2ban 默认使用 iptables,但要配置大部分防火墙和服务也很简单,以 nftables 为例:
/etc/fail2ban/jail.local
[DEFAULT] banaction = nftables banaction_allports = nftables[type=allports]
其它示例请参考 /etc/fail2ban/action.d/
,例如 ufw.conf。
小技巧
配置 SSH jail
编辑 /etc/fail2ban/jail.d/sshd.local
,添加以下内容,并在 ignoreip
中修改可信 IP 清单:
/etc/fail2ban/jail.d/sshd.local
[sshd] enabled = true filter = sshd banaction = iptables backend = systemd maxretry = 5 findtime = 1d bantime = 2w ignoreip = 127.0.0.1/8
- 为使 fail2ban 具有完整监控能力,可能需要在
/etc/ssh/sshd_config
中设置LogLevel VERBOSE
,否则像密码错误等可能不会被正确记录。 - Fail2ban 从 0.10 版本开始支持 IPv6,请按需调整防火墙(例如启动/启用
ip6tables.service
)。 - 在使用 journal 命名空间(即在单元文件中使用
LogNamespace=something
)时,可以像这样配置backend
来让 fail2ban 读取这些日志:backend = systemd[journalfiles="/var/log/journal/*.something/system.journal"]
Systemd 后端:journald 过滤
When using the systemd backend to improve performance, configure a filter with journalmatch
. For example, to parse only kernel-level log messages:
/etc/fail2ban/filter.d/fwdrop.local
[Definition] failregex = ^.*DROP_.*SRC=<ADDR> DST=.*$ journalmatch = _TRANSPORT=kernel
See also systemd.journal-fields(7).
服务加固
现在 Fail2ban 只能以根用户权限运行。因此,你可能回想使用 systemd 加固进程。
为 fail2ban.service
创建附加配置片段文件:
/etc/systemd/system/fail2ban.service.d/override.conf
[Service] PrivateDevices=yes PrivateTmp=yes ProtectHome=read-only ProtectSystem=strict ReadWritePaths=-/var/run/fail2ban ReadWritePaths=-/var/lib/fail2ban ReadWritePaths=-/var/log/fail2ban.log ReadWritePaths=-/var/spool/postfix/maildrop ReadWritePaths=-/run/xtables.lock CapabilityBoundingSet=CAP_AUDIT_READ CAP_DAC_READ_SEARCH CAP_NET_ADMIN CAP_NET_RAW
CapabilityBoundingSet
的 CAP_DAC_READ_SEARCH
参数会允许 Fail2ban 读取所有目录和文件,CAP_NET_ADMIN
和 CAP_NET_RAW
会允许 Fail2ban 操作任何具有命令行解释器界面的防火墙。具体信息请参考 capabilities(7)。
使用 ProtectSystem=strict
会使文件系统层次结构设为只读,ReadWritePaths
会允许 Fail2ban 对特定路径具有写入权限。
最后重载 systemd 守护进程以应用对单元文件的修改,并重启 fail2ban.service
。