CentOS 7 Fail2ban防暴力破解
1、安装
yum install epel-release -y yum install fail2ban fail2ban-systemd -y 或 sudo apt update sudo apt install fail2ban
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 start fail2ban.service
说明:
enabled=true 是否启用
ignoreip = 127.0.0.1 忽略的IP
bantime=86400 封锁时间,单位:秒
findtime=600 统计时间范围,在规定时间内满足条件开始执行封锁,单位:秒
maxretry=5 错误次数
port=26613 端口
logpath=/var/log/secure 检测日志路径 
[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、常用操作
//启动 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*
//日志设定文档 /etc/fail2ban/fail2ban.conf // 阻挡设定文档 /etc/fail2ban/jail.conf //具体阻挡内容设定目录 /etc/fail2ban/filter.d
//查看被禁用的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