参考github项目某博客文本后把 SSH 密码爆破 阻止恶意扫描 CC 攻击 vsftpd 攻击规则。其实还可以加很多项目(只不过有些都没见过就算了)外补充个日志删除。基本上就这么多了的。
首先这个只是简易方案,适用于linux各类发行版本,包括容器类的openVZ LXC类。能治标不治本。被打还是会被打,防御一些阿猫阿狗还是没毛病的......
参考资料(建议都看看Centos和Ubuntu好几处不一样...)
https://www.escapelife.site/posts/d5f0eaf3.html
https://github.com/fail2ban/fail2ban
https://github.com/fail2ban/fail2ban/issues
https://www.有图比.com/results?search_query=fail2ban
#以Debian为例(别的可以依葫芦画瓢) #更新&安装 apt-get update && apt-get upgrade -y apt-get -y install fail2ban
温馨提示:
下面有几个数值需要自己修改“port=22”如果你的SSH端口不是22的话改成你对应的值
“# 最多...”# 禁止...”这样的含义可以视为注释,前面对应的数值可以自己修改。
修改好自己的值后拷贝修改的文本直接终端黏贴就可以了
#配置参数 fail2ban="/etc/fail2ban/jail.d/sshd.local" cat>"${fail2ban}"<<EOF [ssh-iptables] enabled = true filter = sshd action = iptables[name=SSH, port=22, protocol=tcp] logpath = /var/log/auth.log # 如果是Centos路径要改/var/log/secure这个 maxretry = 9 # 最多连接9次失败 bantime = 777600 # 禁止9天(按秒单位1天86400) #阻止恶意扫描 [nginx-dir-scan] enabled = true filter = nginx-dir-scan action = iptables[name=nginx-dir-scan, port=443, protocol=tcp] logpath = /path/to/nginx/access.log maxretry = 1 # 最多扫描1次 bantime = 777600 # 禁止9天(按秒单位1天86400) findtime = 300 #防CC攻击 [nginx-cc] enabled = true port = http,https filter = nginx-cc action = %(action_mwl)s maxretry = 20 # 最多C20次 findtime = 60 bantime = 777600 # 禁止9天(按秒单位1天86400) logpath = /usr/local/nginx/logs/access.log #防vsftpd攻击规则 [vsftpd-notification] enabled = true filter = vsftpd action = sendmail-whois[name=VSFTPD, dest=you@example.com] logpath = /var/log/vsftpd.log maxretry = 5 # 最多5次 bantime = 777600 # 禁止9天(按秒单位1天86400) [vsftpd-iptables] enabled = true filter = vsftpd action = iptables[name=VSFTPD, port=ftp, protocol=tcp] sendmail-whois[name=VSFTPD, dest=you@example.com] logpath = /var/log/vsftpd.log maxretry = 5 # 最多5次 bantime = 777600 # 禁止9天(按秒单位1天86400) EOF
#覆写&启动&开机启动 cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.d/sshd.local #先暂停#启动&开机自启 systemctl stop fail2ban systemctl start fail2ban.service systemctl enable fail2ban.service
删除多余日志
#fail2ban日志 开机自删 定时删日志 logdelete="./fail2banlogdelete.sh" cat>"${logdelete}"<<EOF #!/bin/bash find /path/to/nginx/ -mtime 15 -name "*.log" | xargs -i mv {} /root/RecycleBin/; find /usr/local/nginx/logs/ -mtime 15 -name "*.log" | xargs -i mv {} /root/RecycleBin/; find /var/log/ -mtime 15 -name "*.log" | xargs -i mv {} /root/RecycleBin/; find /root/RecycleBin/ -name "*.log" -exec rm -rf {} ; EOF cp ./fail2banlogdelete.sh /etc/init.d/ #读挡并赋予权限 chmod 777 /etc/init.d/fail2banlogdelete.sh update-rc.d ./fail2banlogdelete.sh defaults 90 cd (echo "0 0 */15 * * bash /etc/init.d/fail2banlogdelete.sh >>/dev/null 2>&1" ; crontab -l )| crontab cd
#启动 systemctl start fail2ban #停止 systemctl stop fail2ban #开机启动 systemctl enable fail2ban #状态 systemctl status fail2ban #开机关闭 systemctl disable fail2ban #查看被ban IP,其中sshd为名称,比如上面的[wordpress] fail2ban-client status sshd #删除被ban IP fail2ban-client set sshd delignoreip X.X.X.X #查看日志 tail /var/log/fail2ban.log
检验方法:
直接重启呼出TOP 或者HTOP检查是否有此项
原文:https://hostloc.com/forum.php?mod=viewthread&tid=957070&extra=page%3D1%26filter%3Dtypeid%26typeid%3D6
文章评论