Fail2ban
Fail2ban 扫描日志文件 (例如 /var/log/httpd/error_log
) 并封禁显示恶意迹象的 IP,例如过多的身份验证尝试、漏洞扫描等。通常 Fail2ban 用于更新 防火墙 规则,以拒绝指定时间段内的 IP 地址,尽管也可以配置任何其他任意操作(例如发送电子邮件)。
安装
安装 以下软件包之一
- fail2ban - 最新稳定版本。
- fail2ban-gitAUR - 最新的 master 分支提交。
用法
配置 Fail2ban 并 启用/启动 fail2ban.service
。
fail2ban-client
fail2ban-client 允许监控 jails(重载、重启、状态等),要查看所有可用命令
$ fail2ban-client
要查看所有已启用的 jails
# 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
对于所有 jails 的紧凑版本,包括被封禁的 IP
# fail2ban-client banned
[{'sshd': ['192.168.100.50']}, {'apache-auth': []}]
配置
由于在 升级 期间,可能会为 /etc/fail2ban/jail.conf
创建 Pacnew 和 Pacsave 文件,jail.conf(5) § 配置文件格式 建议用户 创建 /etc/fail2ban/jail.local
文件以“简化升级”。
例如,要将默认 ban 时间更改为 1 天
/etc/fail2ban/jail.local
[DEFAULT] bantime = 1d
或者在 /etc/fail2ban/jail.d
目录下创建单独的 name.local
文件,例如 /etc/fail2ban/jail.d/sshd.local
。
重载 fail2ban.service
以应用配置更改。
启用 jails
默认情况下,所有 jails 均已禁用。添加 enabled = true
到您要使用的 jail,例如启用 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
- 可能需要在
/etc/ssh/sshd_config
中设置LogLevel VERBOSE
以允许完整的 fail2ban 监控,否则密码失败可能无法正确记录。 - Fail2ban 自 0.10 版本起支持 IPv6。相应地调整您的防火墙,例如启动/启用
ip6tables.service
。 - 当使用 journal 命名空间(通过向单元文件添加
LogNamespace=something
)时,您可以通过如下设置backend
来使 fail2ban 读取这些日志:backend = systemd[journalfiles="/var/log/journal/*.something/system.journal"]
。
Systemd 后端:journald 过滤
当使用 systemd 后端以提高性能时,请使用 journalmatch
配置过滤器。例如,仅解析内核级日志消息
/etc/fail2ban/filter.d/fwdrop.local
[Definition] failregex = ^.*DROP_.*SRC=<ADDR> DST=.*$ journalmatch = _TRANSPORT=kernel
另请参阅 systemd.journal-fields(7)。
服务加固
目前,Fail2ban 必须以 root 身份运行。因此,您可能希望考虑使用 systemd 加固该进程。
创建 fail2ban.service
的 drop-in 配置文件
/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 在任何具有 命令行 shell 界面的防火墙上运行。有关更多信息,请参见 capabilities(7)。
通过使用 ProtectSystem=strict
,文件系统 层次结构将仅为只读,ReadWritePaths
允许 Fail2ban 对所需路径具有写入权限。
最后,重载 systemd 守护程序 以应用单元的更改,并重启 fail2ban.service
。