设为首页 收藏本站
查看: 942|回复: 0

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

[复制链接]

尚未签到

发表于 2018-8-21 07:45:05 | 显示全部楼层 |阅读模式
  #设置获取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  $LOG
  echo "$TIME        send      mail      to    $CCTO" >> $LOG


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-554445-1-1.html 上篇帖子: [SHELL] WEB页面监控脚本 下篇帖子: Shell函数与数组
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表