Fail2ban防暴力破解
1、安装
yum install epel-release -y yum install fail2ban fail2ban-systemd -y 或 sudo apt update sudo apt install fail2ban -y
2、配置
#新建配置 sudo vim /etc/fail2ban/jail.d/sshd.local
写入以下命令:
[sshd] enabled = true filter = sshd findtime = 120 bantime = 120 maxretry = 3 banaction = iptables-allports
或
[sshd] enabled = true filter = sshd action = iptables[name=sshd-ban, port=ssh, protocol=tcp] findtime =60 bantime = 60 maxretry = 3
或
[sshd] enabled = true filter = sshd findtime = 1d bantime = 4w maxretry = 3
保存退出,重启服务:
#开机设置并重启 sudo systemctl enable fail2ban.service && sudo systemctl restart fail2ban.service
说明:
enabled=true 是否启用
ignoreip = 127.0.0.1 忽略的IP
bantime=86400 封锁时间,单位:秒
findtime=600 统计时间范围,在规定时间内满足条件开始执行封锁,单位:秒
maxretry=5 错误次数
port=26613 端口
logpath=/var/log/secure 检测日志路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | [DEFAULT] #忽略哪些IP,可以是具体IP、CIDR类型的地址,多个IP用空格分开 ignoreip = 127.0.0.1 #设置IP被锁住的时间,单位为秒 bantime = 600 #检测时间,在此时间内超过规定的次数会激活fail2ban findtime = 600 #尝试的次数 maxretry = 3 #日志检测机器,有"gamin", "polling" and "auto"三种模式。 backend = polling #发送报警邮件的地址 destemail = root@localhost #默认的动作执行行为,在action.d目录下有各种行为策略,默认是iptables-#multiport banaction = iptables-multiport #0.8.1版本后fail2ban默认用sendmail MTA mta = sendmail #默认使用tcp协议 protocol = tcp #定义了各种行动的参数 #banaction参数在action.d目录下具体定义,name port protocol 也可以自己定义 #只禁止IP action_ = %(banaction)s[name=%(__name__)s, port= "%(port)s" , protocol="%(protocol)s] #即禁止IP又发送email action_mw = %(banaction)s[name=%(__name__)s, port= "%(port)s" , protocol="%(protocol)s] %(mta)s-whois[name=%(__name__)s, dest= "%(destemail)s" , protocol="%(protocol)s] #禁止IP、发送email、报告有关日志 action_mwl = %(banaction)s[name=%(__name__)s, port= "%(port)s" , protocol="%(protocol)s] %(mta)s-whois-lines[name=%(__name__)s, dest= "%(destemail)s" , logpath=%(logpath)s] #如果没有定义行为,则默认的行为为action,可选择action_,action_mw, action_mwl 等 action = %(action_)s 默认配置文件含有此模块 #定义子模块名 [ssh] #是否激活 enabled = true #定义port,可以是数字端口号表示,也可以是字符串表示 port= ssh #过滤规则,在filter.d目录下定义 filter = sshd #检测日志的路径 logpath = / var /log/auth.log #尝试的次数,覆盖了全局配置的 maxretry = 6 #banaction 在action.d目录下定义,此参数值会替换action中选用的默认行为中定义的banaction参数 banaction = iptables-allports #注意 port protocol banaction 可以不用分开定义,直接使用action定义也可以,例如: #action = iptables[name=SSH, port=ssh, protocol=tcp] #在子模块中定义的port protocol banaction 都会在action_ action_mw, action_mwl中替换成具体的设置值。 //filter.d 目录里面定义的是根据日志文件进行过滤的规则,主要是利用正则匹配出现错误的关键字。 //action.d目录里面是根据过滤的规则对相应的IP采取什么样的行为 |
3、常用操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //启动 systemctl start fail2ban //重启 systemctl restart fail2ban //开机重启 systemctl enable fail2ban //查看状态 systemctl status fail2ban.service //查看配置状态 fail2ban-client status //默认配置 vim /etc/fail2ban/jail.conf //查看攻击者 fail2ban-client status sshd //确保防火墙已开起 systemctl enable firewalld systemctl start firewalld //更新 SELinux 策略 yum update -y selinux-policy* |
1 2 3 4 5 6 | //日志设定文档 /etc/fail2ban/fail2ban.conf // 阻挡设定文档 /etc/fail2ban/jail.conf //具体阻挡内容设定目录 /etc/fail2ban/filter.d |
1 2 3 4 5 6 7 8 | //查看被禁用的ip iptables -L -n //查看登陆失败日志 cat / var /log/secure | grep 'Failed password' //解锁ip fail2ban-client set sshd unbanip IPADDRESS |
参考文献:https://centoshelp.org/security/fail2ban/
https://www.howtoforge.com/tutorial/how-to-install-fail2ban-on-centos/
文章出处:http://www.cnblogs.com/anech/p/6867589.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南