Fail2ban防止暴力破解工具使用教程
- 场景
- Fail2ban安装和配置
- 安装
- 配置
- 原理
- 遇到的问题以及解决办法
- 问题1:设置的策略是10分钟内ssh连接失败2次的ip进行封禁,日志中实际却出现4次连接。
- 问题2:策略设置为1分钟内失败两次,封禁ip。但通过日志发现,暴力破解也修改了策略,每分钟尝试1次ssh连接,这样导致该ip无法被封禁。
场景
物理主机服务器托管于第三方机房中,仅仅提供电源以及网络服务,并未提供安全防护管理。因此,我们经常遇到网络攻击,常见的就是通过ssh进行暴力破解,进入服务器,植入病毒和程序,进而造成经济损失。
ssh连接日志记录:
/var/log/secure
Mar 9 03:40:00 localhost sshd[78060]: Failed password for root from 14.103.109.71 port 49730 ssh2
Mar 9 03:40:00 localhost sshd[78060]: Connection closed by 14.103.109.71 port 49730 [preauth]
Mar 9 03:40:00 localhost unix_chkpwd[78088]: password check failed for user (root)
Mar 9 03:40:00 localhost sshd[78086]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=14.103.132.8 user=root
Mar 9 03:40:00 localhost sshd[78086]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Mar 9 03:40:01 localhost unix_chkpwd[78107]: password check failed for user (root)
Mar 9 03:40:01 localhost sshd[78090]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=14.103.109.71 user=root
Mar 9 03:40:01 localhost sshd[78090]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Mar 9 03:40:02 localhost sshd[78086]: Failed password for root from 14.103.132.8 port 37816 ssh2
Mar 9 03:40:02 localhost sshd[78086]: Received disconnect from 14.103.132.8 port 37816:11: Bye Bye [preauth]
Mar 9 03:40:02 localhost sshd[78086]: Disconnected from 14.103.132.8 port 37816 [preauth]
Mar 9 03:40:03 localhost sshd[78090]: Failed password for root from 14.103.109.71 port 49742 ssh2
Mar 9 03:40:03 localhost sshd[78090]: Connection closed by 14.103.109.71 port 49742 [preauth]
Mar 9 03:40:03 localhost unix_chkpwd[78125]: password check failed for user (root)
Mar 9 03:40:03 localhost sshd[78123]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=14.103.109.71 user=root
Mar 9 03:40:03 localhost sshd[78123]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Mar 9 03:40:06 localhost sshd[78123]: Failed password for root from 14.103.109.71 port 54450 ssh2
Mar 9 03:40:06 localhost sshd[78123]: Connection closed by 14.103.109.71 port 54450 [preauth]
Mar 9 03:40:06 localhost unix_chkpwd[78131]: password check failed for user (root)
Mar 9 03:40:06 localhost sshd[78129]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=14.103.109.71 user=root
Mar 9 03:40:06 localhost sshd[78129]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Mar 9 03:40:08 localhost sshd[78129]: Failed password for root from 14.103.109.71 port 54458 ssh2
Mar 9 03:40:08 localhost sshd[78129]: Connection closed by 14.103.109.71 port 54458 [preauth]
Mar 9 03:40:09 localhost unix_chkpwd[78138]: password check failed for user (root)
Mar 9 03:40:09 localhost sshd[78136]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=14.103.109.71 user=root
Mar 9 03:40:09 localhost sshd[78136]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
从ssh记录中可以看到,这10秒钟有4次ssh请求,差不多1分钟24次记录,已经属于恶意暴力破解了。
Fail2ban安装和配置
安装
- Linux(CentOS/RHEL)
yum install epel-release -y # 先安装 EPEL 源(CentOS 7)yum install fail2ban -ysystemctl enable --now fail2ban
- Linux(Ubuntu/Debian)
apt update && apt install fail2ban -ysystemctl enable --now fail2ban
配置
- 安装路径:
(base) [root@localhost fail2ban]# cd /etc/fail2ban/
(base) [root@localhost fail2ban]# pwd
/etc/fail2ban
(base) [root@localhost fail2ban]# ls
action.d fail2ban.conf fail2ban.d filter.d jail.conf jail.d jail.local paths-common.conf paths-fedora.conf
(base) [root@localhost fail2ban]#
- 配置文件路径
(1) 主配置文件(不推荐直接修改)
/etc/fail2ban/jail.local
(1) 主配置文件(不推荐直接修改)
/etc/fail2ban/jail.conf
注意:(系统默认配置,升级时会被覆盖)
(2) 自定义配置文件(推荐修改)
/etc/fail2ban/jail.local
注意:(用户自定义配置,优先级高于 jail.conf,不会被升级覆盖)
- 自定义配置文件注释
enabled = true
port = ssh
filter = sshd
logpath = /var/log/secure # CentOS
maxretry = 5 # 允许5次失败尝试(平衡安全与误封风险)
findtime = 300 # 5分钟内的失败尝试计入统计(实时响应攻击)
bantime = 86400 # 封禁24小时(足够阻止暴力破解,避免长期误封)
ignoreip = 127.0.0.1 192.168.1.0/24 # 信任内网IP
- 相关命令
重启fail2ban应用:
systemctl restart fail2ban
实时监控fail2ban工具的日志:
tail -f /var/log/fail2ban.log
输出封禁ip列表:
fail2ban-client status sshd | grep "Banned IP list"
原理
遇到的问题以及解决办法
问题1:设置的策略是10分钟内ssh连接失败2次的ip进行封禁,日志中实际却出现4次连接。
日志内容:
Jul 17 14:43:07 localhost sshd[105931]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=101.37.23.232 user=root
Jul 17 14:43:07 localhost sshd[105931]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Jul 17 14:43:09 localhost sshd[105931]: Failed password for root from 101.37.23.232 port 41776 ssh2
Jul 17 14:43:09 localhost sshd[105931]: Connection closed by 101.37.23.232 port 41776 [preauth]
Jul 17 14:45:42 localhost sshd[126373]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=101.37.23.232 user=root
Jul 17 14:45:42 localhost sshd[126373]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Jul 17 14:45:42 localhost sshd[126375]: Invalid user pi from 101.37.23.232 port 43878
Jul 17 14:45:42 localhost sshd[126375]: input_userauth_request: invalid user pi [preauth]
Jul 17 14:45:42 localhost sshd[126375]: pam_unix(sshd:auth): check pass; user unknown
Jul 17 14:45:42 localhost sshd[126375]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=101.37.23.232
Jul 17 14:45:43 localhost sshd[126499]: Invalid user hive from 101.37.23.232 port 44702
Jul 17 14:45:43 localhost sshd[126499]: input_userauth_request: invalid user hive [preauth]
Jul 17 14:45:43 localhost sshd[126499]: pam_unix(sshd:auth): check pass; user unknown
Jul 17 14:45:43 localhost sshd[126499]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=101.37.23.232
Jul 17 14:45:44 localhost sshd[126373]: Failed password for root from 101.37.23.232 port 43054 ssh2
Jul 17 14:45:44 localhost sshd[126373]: Connection closed by 101.37.23.232 port 43054 [preauth]
Jul 17 14:45:44 localhost sshd[126375]: Failed password for invalid user pi from 101.37.23.232 port 43878 ssh2
Jul 17 14:45:44 localhost sshd[126375]: Connection closed by 101.37.23.232 port 43878 [preauth]
Jul 17 14:45:44 localhost sshd[126499]: Failed password for invalid user hive from 101.37.23.232 port 44702 ssh2
Jul 17 14:45:44 localhost sshd[126499]: Connection closed by 101.37.23.232 port 44702 [preauth]
Jul 17 14:57:58 localhost sudo: root : TTY=pts/5 ; PWD=/root ; USER=root ; COMMAND=/bin/grep Ban /var/log/fail2ban.log
Jul 17 14:57:58 localhost sudo: pam_unix(sudo:session): session opened for user root by root(uid=0)
问题分析:
- 攻击行为分析
从日志来看,IP 101.37.23.232 在短时间内进行了多次SSH暴力破解尝试:
14:43:07
尝试 root 登录 → 失败(1次)
14:45:42
尝试 root 登录 → 失败(2次)
尝试 pi 用户登录 → 无效用户(3次)
尝试 hive 用户登录 → 无效用户(4次)
总计失败登录尝试次数:
root 用户:2次
无效用户(pi、hive):2次
总失败次数:4次
- Fail2Ban 策略匹配
您设置的策略是 10分钟内限制2次(maxretry = 2),但Fail2Ban 默认只统计同一用户的失败登录:
sshd jail 的默认 failregex 会分别统计:
root 的失败登录(2次)→ 未达到 maxretry=2 的封禁阈值(刚好等于,但可能未触发)。
pi 和 hive 的失败登录(各1次)→ 未达到单独封禁条件。
关键问题:
Fail2Ban 默认不合并不同用户的失败登录,因此虽然总失败次数是4次,但每个用户的失败次数均未达到 maxretry=2 的封禁条件。
- 为什么IP没有被封禁?
原因说明:
分用户统计失败次数: Fail2Ban 默认按用户名分别计算 maxretry,root 2次,pi 1次,hive 1次,均未达到封禁条件。
时间窗口 (findtime):所有尝试均在 2分钟 内发生(14:43-14:45),远小于findtime=10m,但按用户分开统计导致未触发。
无效用户未计入:部分Fail2Ban配置可能忽略 invalid user 的日志(需检查 filter.d/sshd.conf 的 failregex)。
处理问题:
方案一:修改 filter.d/sshd.conf 合并统计所有失败登录
编辑 Fail2Ban 的 SSH 过滤规则:
sudo vim /etc/fail2ban/filter.d/sshd.conf
在 failregex 部分取消用户区分,例如:
failregex = ^%(__prefix_line)s(?:error: PAM: )?Authentication failure for .* from <HOST>( port \d+)? ssh2$^%(__prefix_line)sFailed password for .* from <HOST>( port \d+)? ssh2$^%(__prefix_line)sConnection closed by <HOST> port \d+ \[preauth\]$
然后重载配置:
sudo fail2ban-client reload sshd
方案二:降低 maxretry 或缩短 findtime
sudo vim /etc/fail2ban/jail.local
调整参数:
maxretry = 2 # 保持当前值,但修改过滤规则后会更敏感
findtime = 1m # 缩短统计窗口至1分钟
bantime = 1h # 封禁1小时
重启 Fail2Ban:
sudo systemctl restart fail2ban
方案三:手动封禁该IP(临时措施)
sudo fail2ban-client set sshd banip 101.37.23.232
验证是否生效
检查封禁列表:
sudo fail2ban-client status sshd
模拟攻击测试:
使用另一台服务器尝试多次失败登录,观察是否触发封禁。
总结
当前策略问题:Fail2Ban 默认分用户统计失败次数,导致攻击者通过切换用户名绕过封禁。
修复方法:
-
修改 sshd.conf 合并所有失败登录统计。
-
调整 maxretry 和 findtime 提高敏感性。
-
手动封禁 确认的恶意 IP。
最终效果:调整后,同一IP的多次失败登录(无论用户名)将在 findtime 窗口内累计,达到 maxretry 后自动封禁。
问题2:策略设置为1分钟内失败两次,封禁ip。但通过日志发现,暴力破解也修改了策略,每分钟尝试1次ssh连接,这样导致该ip无法被封禁。
解决方法:
将findtime修改时间长一点,保证安全,例如:1小时,1天
findtime = 6000