本帖最后由 冲浪麦浪花郎 于 2022-1-14 16:18 编辑
之前的帖子写少了,补充下Fail2Ban。 前言:https://www.hostloc.com/thread-909578-1-1.html原先写的不完全正确。又过了30天无法修改…..预计Fail2Ban根本没实现……(尴尬)
然后重新开个帖子 之前没有考虑到换端口后MJJ可能非22情况,切文本没有覆写更近。参考github项目某博客文本后把 SSH 密码爆破 阻止恶意扫描 CC 攻击 vsftpd 攻击规则。其实还可以加很多项目(只不过有些都没见过就算了)外补充个日志删除。基本上就这么多了的。
参考资料(建议都看看Centos和Ubuntu好几处不一样…) https://www.escapelife.site/posts/d5f0eaf3.html https://github.com/fail2ban/fail2ban https://github.com/fail2ban/fail2ban/issues
- #以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, [email protected]]
- 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, [email protected]]
- 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
复制代码
删除多余日志(这里的+30是30天时间,可以自己改。对5g 10g vps建议都加上,不加也无所谓。)
- #fail2ban日志 开机自删+定时删日志
- logdelete="./fail2banlogdelete.sh"
- cat>"${logdelete}"<<EOF
- #!/bin/bash
- find /var/log/auth.log -mtime +30 -name "*.log" | xargs -i mv {} /root/RecycleBin/;
- find /path/to/nginx/access.log -mtime +30 -name "*.log" | xargs -i mv {} /root/RecycleBin/;
- find /usr/local/nginx/logs/access.log -mtime +30 -name "*.log" | xargs -i mv {} /root/RecycleBin/;
- find /var/log/vsftpd.log -mtime +30 -name "*.log" | xargs -i mv {} /root/RecycleBin/;
- find /var/log/vsftpd.log -mtime +30 -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 1 * * 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检查是否有此项
PS: 摸鱼时间可以做评测,有需要的MJJ可以发来鉴定,或是有头铁 底气足 后台硬 的商家可以投稿 绝对客观diss (* ̄︶ ̄*)(私信@即可)
捐赠 打赏 投食[ 谢谢哥哥姐姐(#^.^#) ] BTC: 1QHQ95X7qhXkSqeUFDFw2M22JG9tnTCXsE DOGE: DAZ7H64MYcbXQy4xnkwXh2GbdxP2Tc42Mu BUSD: 0xe727335a145e716c918817e5b3cdc7c6d9c833fa ETH: 0xe727335a145e716c918817e5b3cdc7c6d9c833fa USDT: 0xe727335a145e716c918817e5b3cdc7c6d9c833fa BNB: 0xe727335a145e716c918817e5b3cdc7c6d9c833fa ADA: DdzFFzCqrhsq8VNQSTVSzjSUWDLEMFiwieADeJHHNNDjPczTsUJDDZTcTobZuQsrpszoPmuuDJ21xgp2UTahEN6G5QyEbryUifUAUtMF XMR: 87NhgxMFMWgiLBhVYcwV4wYr3cXit6uRdWzdHX6cPxbQSXHFqCuoZPaKJfuaYDUwyQS6tGTWWYeRgFUTjrYXXiGn5LsXNQ7
|