路漫漫其修远兮,吾将上下而求索

导航

安装并配置fail2ban防止ssh爆破攻击

通过下面命令安装fail2ban
yum -y install fail2ban

启动
systemctl start fail2ban

查看状态
systemctl status fail2ban
image

设置开机启动
systemctl enable fail2ban

检查版本
fail2ban-client -V
fail2ban-sever -V
image

检查配置文件是否有误
fail2ban-client -d
image

配置ssh防爆破策略文件
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
vi /etc/fail2ban/jail.local

添加下面内容,点击查看
[SSH-iptables]
enabled = true
filter = sshd
action = iptables[name=SSH, port=22, protocol=tcp]
logpath = /var/log/secure
maxretry = 3
bantime = 300

maxretry是3次,bantime是300秒5分钟

配置后需要重启服务
systemctl restart fail2ban

通过ssh登录错误尝试看看

查看日志
cat /var/log/fail2ban.log
image

三次后ip被ban了,直接给自己踢下线了
image
换地址登录后查看日志
image

十分钟后解锁了
image
PS:SSH-iptables里写错了bintime,导致5分钟的没生效是10分钟

验证fail2ban是否正常运行
fail2ban-client ping
正常情况回复pong
image

查看被禁的ip
fail2ban-client status SSH-iptables
image

在日志里也看到重启服务后的新策略
image

通过系统iptables加黑名单
iptables -vnL
image

感觉这个比denyhosts好用,那个只能用来防止ssh爆破攻击,而且配置不难

参考:
Centos7配置fail2ban防止ssh被暴力破解 -CSDN博客

posted on 2024-03-28 15:38  爱在西元间  阅读(170)  评论(0编辑  收藏  举报