且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

防止暴力破解ssh

更新时间:2022-02-18 12:15:26

centos rhel下如下


  1. #! /bin/bash  
  2. cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /root/black.txt  
  3. cat /var/log/secure|awk '/Invalid user/{print $NF}'|sort|uniq -c|awk '{print $2"="$1;}' >> /tmp/black.txt
  4. DEFINE="20"  
  5. for i in `cat  /root/black.txt`  
  6. do  
  7.         IP=`echo $i |awk -F'{print $1}'`  
  8.         NUM=`echo $i|awk -F'{print $2}'`  
  9.         if [ $NUM -gt $DEFINE ];  
  10.         then  
  11.          grep $IP /etc/hosts.deny > /dev/null  
  12.           if [ $? -gt 0 ];  
  13.           then  
  14.           echo "sshd:$IP" >> /etc/hosts.deny  
  15.           fi  
  16.         fi  
  17. done 

opensuse下如下


  1. #! /bin/bash  
  2. cat /var/log/messages|awk '/error: PAM: Authentication failure/{print $NF}'|sort|uniq -c|awk '{print $2"="$1;}' > /tmp/black.txt  
  3. cat /var/log/secure|awk '/Invalid user/{print $NF}'|sort|uniq -c|awk '{print $2"="$1;}' >> /tmp/black.txt
  4. DEFINE="10"  
  5. $2"="$1;}' >> /tmp/black.txt
  6. for i in `cat  /tmp/black.txt`  
  7. do  
  8.         IP=`echo $i |awk -F'{print $1}'`  
  9.         NUM=`echo $i|awk -F'{print $2}'`  
  10.         if [ $NUM -gt $DEFINE ];  
  11.         then  
  12.          grep $IP /etc/hosts.deny > /dev/null  
  13.           if [ $? -gt 0 ];  
  14.           then  
  15.           echo "sshd:$IP" >> /etc/hosts.deny  
  16.           fi  
  17.         fi  
  18. done 

效果不错 



本文转自it你好 51CTO博客,原文链接:http://blog.51cto.com/itnihao/745951,如需转载请自行联系原作者