防暴力破解sshd脚本


注意: 此脚本并不会定期清理之前添加到/etc/hosts.deny中的ip地址

     为了以防万一,先在/etc/hosts.allow中添加自己本地的ip地址,万一哪天忘记了root密码,还有试一试的机会,不然就呵呵了。。。


此脚本运行在centos6的系统之上,如centos7需要的话,需要修改脚本声明(#!/usr/bin/bash);


第一步:   


mkdir /date/script/ -p


vim /date/script/cron.sh


#!/bin/bash

for ((i=1;i<=12;i++))

do

       source /date/script/sshd_fblpj.sh

       sleep 5

done



第二步:

vim /date/script/sshd_fblpj.sh


#!/bin/bash

#

awk '/Failed password/{print $(NF-3)}' /var/log/secure | sort | uniq -c | awk '{if($1>=4) print $2}' > /tmp/reject.txt

for i in `cat /tmp/reject.txt`

do

ip=$i

grep -w "$ip" /etc/hosts.deny

if [ $? -gt 0 ]

then

echo "sshd:$ip" >> /etc/hosts.deny

fi

done



第三步:

crontab -e


*/1     *       *       *       *        sh /date/script/cron.sh