#设置获取IP地址的存放位置
BADIP=/tmp/.ssh/.ssh_badip
BKIP=/tmp/.ssh/.back_ssh_badip
BIP=/tmp/.ssh/.bip
> $BIP
mkdir /tmp/.ssh 2>/dev/null
touch $BADIP $BKIP
LOG=/var/log/messages
#获取sshd服务端口
SSHPORT=`netstat -antlp |grep sshd |awk -F: '{print $4}'|tail -n1`
TIME=`date +"%Y-%m-%d %H:%M:%S"`
IPTFILE=/tmp/.ssh/.iptables
IPLIST=/tmp/.ssh/.iplist
touch $IPTFILE $IPLIST
LINEA=`grep -v 日期 $BKIP| wc -l |awk '{print $1}'`
echo "" "日 期"" ""时 间"" ""连接次数"" ""IP 地 址"" ""日期"" ""小时" > $BADIP ;lastb -i | awk '{print $3" "$6" "$7}' | awk -F: '{print $1}' |sort |uniq -c|awk '$1 > '$NUMBER' {print $1" " $2" "$3" "$4}'| awk -vtime="$TIME" '{print time" "$1 " "$2" "$3" "$4}'|column -t >>$BADIP
cat $BADIP >> $BKIP
DROPIP=`cat $BADIP | wc -l `
ipline=/tmp/.ssh/.ipline
touch $ipline
if [ $DROPIP -gt 1 ] ; then
for bip in `grep -v 日期 $BADIP | awk '{print $4}'`
do
IPLINEA=`/sbin/iptables -L -n --line-number |egrep '[DROP|$SSHPORT]' | grep -v Ch |awk '{print $1}'|wc -l`
echo $IPLINEA > $ipline
echo $bip > $BIP
iptables -I INPUT -s $bip -p tcp --dport $SSHPORT -j DROP
echo $bip >> $IPLIST
echo "$TIME Lock IP address $bip iptables " >> $LOG
cat /var/log/btmp >> /var/log/btmp.bak ; >/var/log/btmp
TIME_NOWA=`date +%s`
echo $TIME_NOWA > time
done
fi
LINEVE=`wc -l /tmp/.ssh/.ipline | awk '{print $1}' `
if [ $LINEVE -gt 0 ] ; then
echo linefile ok > /dev/null
else
echo 0 > $ipline
fi
IPLINE=`cat $ipline`
LINEB=`grep -v 日期 $BKIP|wc -l |awk '{print $1}'`
VALUE=`echo "$LINEB-$LINEA"|bc`
#获取被列入$BKIP的ip地址
IPLINEB=`/sbin/iptables -L -n --line-number |egrep '[DROP|$SSHPORT]' | grep -v Ch |awk '{print $1}'|wc -l`
if [ $IPLINEB -eq 1 ] ; then
IPLINEB=`/sbin/iptables -L -n --line-number |egrep '[DROP|$SSHPORT]' | grep -v Ch |awk '{print $1}'|wc -l >/dev/null ; echo "$IPLINEB+1"|bc `
fi
#当前时间
OLD_TIME=`cat time`
TIME_NOWB=`date +%s`
#间隔时间判断
TIME_IN=`echo "$TIME_NOWB-$OLD_TIME" | bc`
#删除禁止的IP地址
LNUMBER=`echo "$IPLINEB-$IPLINE"|bc`
if [ $LNUMBER -lt 2 ] ; then
LNUMBER=`echo "$IPLINEB-$IPLINE+2"|bc`
else
LNUMBER=`echo "$IPLINEB-$IPLINE"|bc`
fi
if [ $LNUMBER -gt 1 ] ; then
if [ $TIME_IN -gt $RETIME ] ; then
iptables -L -n --line-number | awk '{print $5" "$2" "$1" "$8}' | awk -Fdpt: '{print $1" "$2}' | egrep -v '[num|Ch]' | grep $SSHPORT|column -t > $IPTFILE
RMIP=`cat $IPLIST |awk '{print $1}'`
for i in `awk 'NR==FNR{a[$1]=$2" "$3" "$4}NR>FNR{print $0,a[$1]}' $IPTFILE $IPLIST | awk '{print $3}'`
do
iptables -D INPUT $i
>$IPLIST
echo "$TIME Remove lock $RMIP IP address " >> $LOG
done
fi
fi
ip_dir=/usr/log
ipfile=$ip_dir/ipadd1
ip_res_tmp=/tmp/.ssh/.ip_res_tmp
ip_res_tmp2=/tmp/.ssh/.ip_res_tmp2
ip_res_tmp3=/tmp/.ssh/.ip_res_tmp3
ip_res_tmp5=/tmp/.ssh/.ip_res_tmp5
touch $ip_res_tmp
mailsent=/tmp/.ssh/.mailsent
>$mailsent
cat $BIP | while read ip1abcd
do
ip1a=`echo "$ip1abcd"|awk -F"." '{print $1}'`
ip1b=`echo "$ip1abcd"|awk -F"." '{print $2}'`
ip1c=`echo "$ip1abcd"|awk -F"." '{print $3}'`
ip1d=`echo "$ip1abcd"|awk -F"." '{print $4}'`
ipa1=`echo "obase=2;$ip1a"|bc``echo "obase=2;$ip1b"|bc``echo "obase=2;$ip1c"|bc``echo "obase=2;$ip1d"|bc`
tmp1=`grep ^"$ip1a" $ipfile`
if [ -n "$tmp1" ]
then
grep ^"$ip1a" $ipfile > $ip_res_tmp
tmp2=`grep ^"$ip1a"."$ip1b" $ip_res_tmp`
if [ -n "$tmp2" ]
then
grep ^"$ip1a"."$ip1b" $ip_res_tmp > $ip_res_tmp2
tmp3=`grep ^"$ip1a"."$ip1b"."$ip1c" $ip_res_tmp2`
if [ -n "$tmp3" ]
then
grep ^"$ip1a"."$ip1b"."$ip1c" $ip_res_tmp2 >$ip_res_tmp3
else
rm -rf $ip_res_tmp3
fi
else
rm -rf $ip_res_tmp2
fi
else
rm -rf $ip_res_tmp
fi
if [ -f "$ip_res_tmp3" ]
then
cat $ip_res_tmp3 > $ip_res_tmp5
elif [ -f "$ip_res_tmp2" ]
then
cat $ip_res_tmp2 >$ip_res_tmp5
elif [ -f "$ip_res_tmp" ]
then
cat $ip_res_tmp > $ip_res_tmp5
else
echo "cant find it!"
fi
cat "$ip_res_tmp5"|while read ip23abcd
do
ip2a=`echo "$ip23abcd"|awk '{print $1}'|awk -F"." '{print $1}'`
ip2b=`echo "$ip23abcd"|awk '{print $1}'|awk -F"." '{print $2}'`
ip2c=`echo "$ip23abcd"|awk '{print $1}'|awk -F"." '{print $3}'`
ip2d=`echo "$ip23abcd"|awk '{print $1}'|awk -F"." '{print $4}'`
ip3a=`echo "$ip23abcd"|awk '{print $2}'|awk -F"." '{print $1}'`
ip3b=`echo "$ip23abcd"|awk '{print $2}'|awk -F"." '{print $2}'`
ip3c=`echo "$ip23abcd"|awk '{print $2}'|awk -F"." '{print $3}'`
ip3d=`echo "$ip23abcd"|awk '{print $2}'|awk -F"." '{print $4}'`
if [ "$ip1a" -ge "$ip2a" ] && [ "$ip1a" -le "$ip3a" ]
then
if [ "$ip1b" -ge "$ip2b" ] && [ "$ip1b" -le "$ip3b" ]
then
if [ "$ip1c" -ge "$ip2c" ] && [ "$ip1c" -le "$ip3c" ]
then
if [ "$ip1d" -ge "$ip2d" ] && [ "$ip1d" -le "$ip3d" ]
then
> $mailsent
echo "$ip1abcd"-----`echo $ip23abcd |awk '{print $3,$4}'` > $mailsent
fi
fi
fi
fi
done
done
#获取被列入$BKIP的ip地址
LAST=`tail -n $VALUE $BKIP`
MAILSENT=`cat $mailsent`
if [ $VALUE -gt 0 ] ; then
sendmail -t <<EOF
from: $MAILFROM
to: $MAILTO
cc: $CCTO
subject: 严重警告
$time 当前有人正在试探性连接SSH服务,系统已经帮你拦截,查看详情请登录服务器 $IPADDR。
$LAST
$MAILSENT
EOF
echo "$TIME send mail to $MAILTO" >> $LOG
echo "$TIME send mail to $CCTO" >> $LOG