fail2ban 防ssh暴力破解
fail2ban 出现主要针对 暴力访问服务器而设计
原理是通过linux 服务器上的访问日志,通过正则表达式,获取访问信息
如ssh登录防暴力破解,通过正则ssh访问日志记录,获取暴利访问ip,家ip 通过防火墙隔离。”
安装
官网 fail2ban
# centos7wget "https://github.com/fail2ban/fail2ban/archive/0.9.4.tar.gz"lltar -zxvf 0.9.4.tar.gzcd fail2ban-0.9.4python setup.py install #注意,这里需要python 2.6以上cp files/redhat-initd /etc/init.d/fail2ban # 启动文件添加到init.d目录chkconfig --add fail2ban #添加启动软链接
配置
ssh 防止暴力登录
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local #添加本地配置文件vim /etc/fail2ban/jail.local # 自动以配置
配置内容
# 禁止ip方式banaction = firewallcmd-ipsetbanaction_allports = firewallcmd-allports# sshd 配置[sshd]enabled = true #启动filter = sshd #过滤规则 目录:filter.d/port = 22 #拦截端口#logpath = %(sshd_log)slogpath = /var/log/secure #监控日志backend = %(sshd_backend)saction = %(action_)s #封禁ip的动作bantime = 172800 #ip封禁时间maxretry = 3 # 阈值 --同一个ip登录失败多少次后封禁ip
启动
fail2ban-client -d #检查 jail.local 配置是否正确systemctl start fail2ban #启动
查看
systemctl status fail2ban # 查看启动状态 running 为成功fail2ban-client status # 查看状态fail2ban-client status sshd #查看sshd 状态tail -f /var/log/fail2ban.log #查看fail2ban监控日志