自学内容网 自学内容网

Linux防止暴力破解密码脚本

 1.认识记录linux记录安全的日志
 [root@testpm ~]# cd /var/log/
 [root@testpm log]# ls | grep secure
 secure
 2.该日志的内容查看
 [root@testpm log]# tail -f secure
 #表示ssh身份验证失败
 Aug 29 23:35:03 testpm sshd[111245]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.43.199  user=root
 Aug 29 23:35:03 testpm sshd[111245]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
 #表示root用户密码输出失败
 Aug 29 23:35:04 testpm sshd[111245]: Failed password for root from 192.168.43.199 port 50602 ssh2
 #表示root用户密码输入正确
 Aug 29 23:35:44 testpm sshd[111245]: Accepted password for root from 192.168.43.199 port 50602 ssh2
 #表示给root用户打开了一个终端
 Aug 29 23:35:44 testpm sshd[111245]: pam_unix(sshd:session): session opened for user root by (uid=0)
 #从192.168.43.199端口50602:11接收到断开连接:用户已断开连接
 Aug 29 23:36:26 testpm sshd[111245]: Received disconnect from 192.168.43.199 port 50602:11: disconnected by user
 #表示断开连接
 Aug 29 23:36:26 testpm sshd[111245]: Disconnected from 192.168.43.199 port 50602
 #表示root用户关闭了会话也就是退出了终端
 Aug 29 23:36:26 testpm sshd[111245]: pam_unix(sshd:session): session closed for user root
 ​
 如果传了公钥通过密钥登陆提示如下:
 #表示接受root用户的密钥
 Aug 29 23:50:31 testpm sshd[13123]: Accepted publickey for root from 192.168.43.199 port 50610 ssh2: RSA SHA256:HiUg8OQ5Zydu7e2vEPHAdM3TiLiQMCXX8m0mLdFgpqA
 #表示为root用户打开会话
 Aug 29 23:50:31 testpm sshd[13123]: pam_unix(sshd:session): session opened for user root by (uid=0)
 2.编写密码输入错误三次将该ip禁止连接的脚本


 [root@testpm script]# vim ssh.sh
 #!/usr/bin/bash
 #这是一个防止破解密码的脚本,当用户输入密码错误次数大于3次时,将禁止连接服务器
 ip=`cat /var/log/secure | awk '/Failed password/ {print $11}' | sort | uniq`
 num=`awk '/Failed password/ {print $11}' /var/log/secure | awk '{ips[i]++} END{print $i, ips[i]}' | awk '{print $2}'`
 if [ $num -ge 3 ];then
         echo "sshd:$ip" >> /etc/hosts.deny
 fi

 添加到计划任务中
 [root@testpm script]# crontab -l
 * * * * * /usr/bin/bash /opt/script/ssh.sh


原文地址:https://blog.csdn.net/eagle89/article/details/137676667

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!