改善被暴力入侵 ssh (二 ) sshdropinvalidip.sh
作者: cross 日期: 2006-11-07 11:31
#!/bin/bash
# fixed 95/01/24
# purpose: check /var/log/secure with illegal trying to login, and
# call iptables to drop this ip
# vi /etc/hosts.allow
# sshd: ALL: spawn ( /etc/sshdropinvalidip.sh )& : ALLOW
#
logfile="/var/log/secure"
droplist="/etc/sshdropinvalidip.txt"
filterroot="Failed password for root"
filterinvaliduser="Failed password for invalid user"
getlines="33"
limitcounts="5"
checkfilterroot=`/usr/bin/tail -n $getlines $logfile | grep -ir "$filterroot" | awk '{print $11}' | sort`
checkfilterinvaliduser=`tail -n $getlines $logfile | grep "$filterinvaliduser" | awk '{print $13}' | sort`
echo "### check Failed password for root"
echo "$checkfilterroot"
echo "### check Failed password for invalid user"
echo "$checkfilterinvaliduser"
echo ""
echo "### filterroot line"
/usr/bin/tail -n $getlines $logfile | grep -ir "$filterroot" | awk '{print $11}' | sed 's/::ffff://' | sort | uniq | \
while read ip; do
echo "try to illegally login from $ip"
checkfilterrootipcount=`echo $checkfilterroot | grep $ip | wc -w`
echo "try to illegally login counts $checkfilterrootipcount"
if [ $checkfilterrootipcount -gt $limitcounts ]; then
echo "must be dropped $ip"
iptablescurrentip=`iptables -L INPUT -n | grep "$ip" | grep "tcp dpt:22" | awk '{print $4}'`
if [ -z $iptablescurrentip ]; then
echo "iptables insert $ip"
echo $ip >> $droplist
iptables -I INPUT -s $ip -p tcp --dport 22 -j DROP
iptables -I FORWARD -s $ip -p tcp --dport 22 -j DROP
echo "done"
else
echo "had has $ip, do nothing"
echo "done"
fi
else
echo "pass $ip"
fi
done
echo "### filterinvaliduser"
/usr/bin/tail -n $getlines $logfile | grep "$filterinvaliduser" | awk '{print $13}' | sed 's/::ffff://' | sort | uniq | \
while read ip; do
echo "try to illegally login from $ip"
checkfilterinvaliduseripcount=`echo $checkfilterinvaliduser | grep $ip | wc -w`
echo "try to illegally login counts $checkfilterinvaliduseripcount"
if [ $checkfilterinvaliduseripcount -gt $limitcounts ]; then
echo "must be dropped $ip"
iptablescurrentip=`iptables -L INPUT -n | grep "$ip" | grep "tcp dpt:22" | awk '{print $4}'`
if [ -z "$iptablescurrentip" ]; then
echo "iptables insert $ip"
echo $ip >> $droplist
iptables -I INPUT -s $ip -p tcp --dport 22 -j DROP
iptables -I FORWARD -s $ip -p tcp --dport 22 -j DROP
echo "done"
else
echo "had has $ip, do nothing"
echo "done"
fi
else
echo "pass $ip"
fi
done
上一篇
返回
下一篇
標籤:




如何在ssh登入主機遠端主機時不要有key的檢查 (2012-01-03 09:16)
設定ssh連線30分鐘後沒有動作就自動斷線 (2011-12-26 23:59)
把 ssh public key 遠端複製過去 (另一種方式) (2011-10-26 14:32)
ssh搭配pam_tally2讓使用者登入,幾次失敗就鎖定時間,過了才可再次登入 (2011-04-28 14:58)
SSH 幾項安全性設置 (2011-04-27 00:19)
防ssh的暴力式攻擊-DenyHosts (2011-04-17 23:45)
怎麼在ssh console底下刪除特殊字元的檔案 (2010-02-22 19:28)
SSH 使用技巧 - 縮短名稱 (2009-04-21 21:04)
sudo 讓一般使用者有 Root 權限 (Part II) (2008-08-04 22:19)