您的位置:首页 > 运维架构 > Shell

shell结合iptables自动拒绝恶意连接ssh

2011-08-04 14:26 288 查看
修改功能,增加自动释放被锁定的IP地址功能
增加自动执行时间,无需在任务计划中修改
不会频繁报警
发送报警邮件时,自动判断IP地址来源,需下载QQWry.Dat这个IP地址库,再做处理。

刚辛苦的一下,打包了俩个文件传上来。
将俩个文件减压
将文件放到/usr/log目录。
tar xzf ipadd12.rar
tar xzf ipadd13.rar
tar xzf ipadd14.rar
cat ipadd14 >> ipadd1
cat ipadd13 >> ipadd1
cat ipadd12 >> ipadd1

使用方法,将标为红色的地方修改成自己需求的
运行方法,新建一个名为ssh的文件,将脚本内容复制到ssh文件中
执行命令
#nohup ./ssh &
在后台不间断运行。

停止方法:
#ps aux | grep ./ssh |grep -v '/usr/sbin/sshd' |grep -v grep |awk '{print $2}' |xargs kill -9

【脚本内容】直接复制运行时,可能出现字符集错误,造成脚本不能运行,需将脚本复制到机器上时,再从机器复制一份到另一个文件。

#!/bin/bash
while [ 1 ]
do
#设置脚本运行间隔时间(单位秒)
EXEC_TIME=60
#设置连接出错次数
NUMBER=5
#邮件报警地址设置
#发件地址
MAILFROM=monitor@x.x.x.x.com
#收件地址
MAILTO=x.x.x.x@sina.cn
#抄送地址
CCTO=x.x.x.x@qq.com
#设置释放锁定IP时间(单位秒)
RETIME=3000

#设置获取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
fi
sleep $EXEC_TIME
done

邮件效果截图
附加自动判断IP地址来源功能
ipadd1文件可自己下载下QQWry.Dat的一个文件,完了将IP地址解压出来到一个记事本文件中,将字符集改成UTF8的格式,上传到运行这个脚本的机器在/usr/log目录下,命名为ipadd1,这里只能上传2M,我那个文件压缩后还3M多。





本文出自 “技术交流” 博客,请务必保留此出处http://zhaoyun.blog.51cto.com/2090116/631044
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: