shell 自動檢查SSH暴力破解IP並阻斷攻擊
#!/bin/bash
local_ip="192.168.0.4" #定義本地IP不會被拒絕
tmp_log=`mktemp`
cat /var/log/secure |grep "Failed password for root from" |awk ‘{print $11}‘ |uniq -c > $tmp_log
cat $tmp_log |while read line
do
attack_num=`echo $line |awk ‘{print $1}‘`
attact_ip=`echo $line |awk ‘{print $2}‘`
if [ "$attack_num" -gt "10" ] && [ "$local_ip" != "$attack_ip" ]
then
echo sshd:"$attact_ip":deny >> /etc/hosts.deny
fi
done
sort -u /etc/hosts.deny > $tmp_log #過濾相同IP
cp -f $tmp_log /etc/hosts.deny
rm -f $tmp_log
每5分鐘檢測一次,在5分鐘內嘗試密碼登錄錯誤超過10次的拒絕這個IP
crontab -e
*/5 * * * * /sh/sshtool.sh
查看被拒絕的IP
cat /etc/hosts.deny
本文出自 “Shell” 博客,請務必保留此出處http://zhizhimao.blog.51cto.com/3379994/1936556
shell 自動檢查SSH暴力破解IP並阻斷攻擊