Fail2ban 是一个免费的开放源代码且广泛使用的入侵防御工具,可以扫描日志文件中的IP地址,这些IP地址显示出恶意迹象,例如密码失败过多等等,并禁止它们(更新防火墙规则以拒绝IP地址), 默认情况下,附带用于各种服务的过滤器,包括sshd。
[root@rhel8-study ~]# dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
[root@rhel8-study ~]# dnf install fail2ban -y
[root@rhel8-study fail2ban]# vi /etc/fail2ban/jail.local # 在/etc/fail2ban/jail.d/目录下的jail.local文件或单独的.conf文件中创建和添加配置。 注意,jail.local中设置的配置参数将覆盖jail.conf中定义的任何参数
[DEFAULT] #[DEFAULT]部分包含全局选项, [sshd]部分包含sshd监狱的参数
ignoreip = 203.0.113.12 #ignoreip :指定不禁止的IP地址或主机名列表
bantime = 21600 #bantime :指定禁止主机的秒数(即有效禁止持续时间)
findtime = 300 #findtime :fail2ban将禁止主机,如果主机在最后一个“ findtime ”秒内生成了“ maxretry ”
maxretry = 3
banaction = iptables-multiport
backend = systemd #backend :指定用于修改日志文件的后端
[sshd]
enabled = true
"jail.local" 10L, 154C written
#上述配置如果IP在最近5分钟内发生3次故障,则将其禁止6个小时,并忽略IP地址203.0.113.12
[root@rhel8-study fail2ban]# systemctl start fail2ban && systemctl enable fail2ban
[root@rhel8-study fail2ban]# fail2ban-client status #将fail2ban配置为保护sshd后 ,可以使用fail2ban-client监视失败和被禁止的IP地址
[root@rhel8-study fail2ban]# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 0
| |- Total failed: 0
| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
|- Currently banned: 0
|- Total banned: 0
`- Banned IP list:
[root@rhel8-study fail2ban]# exit
logout
使用fail2ban-client监视失败和禁止的IP地址
[root@rhel7 ~]# ssh 203.0.113.11 # 测试位于203.0.113.0/24的203.0.113.1主机3次SSH键入用户名密码错误后,无法再连接,而203.0.113.12作为放行IP可以访问
[root@rhel8-study ~]# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 0
| |- Total failed: 3
| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
|- Currently banned: 1
|- Total banned: 1
`- Banned IP list: 203.0.113.1
[root@rhel8-study ~]# fail2ban-client unban 203.0.113.1 #取消禁止IP地址,有关fail2ban的更多信息 # man jail.conf && man fail2ban-client