shell防ddos攻击脚本(二)

post by rocdk890 / 2013-6-23 18:37 Sunday linux技术
  在上一篇shell防ddos攻击脚本(一)中,我给大家发了个脚本,那只是针对单机的,如果是在负载均衡下的话,很容易把自己的服务器ip给误封,所以这篇文章就给大家发个可以添加白名单的shell脚本.
  系统:centos 5.9 64位
脚本内容:
vi ip-dos-cc.sh
#!/bin/bash
netstat -an| grep :80 |grep -v -E '127.0|0.0|10.0'|awk '{ print $5 }' | sort|awk -F: '{print $1}' | uniq -c | awk '$1' | awk '$2 != ""' >/root/ddos/black.txt
cnt=0
for i in `awk '{print $2}' /root/ddos/black.txt`
do
  if [ ! -z "$i" ];
  then
          COUNT=`grep $i /root/ddos/black.txt | awk '{print \$1}'`
          DEFINE="20"
          ZERO="0"
    if [ $COUNT -gt $DEFINE ];
          then
                  grep $i /root/ddos/white.txt /dev/null
                  if [ $? -gt $ZERO ];
                  then
        IPEX=`iptables -nL | grep "$i"`
        if [ -z "$IPEX" ];
        then
                      echo "$COUNT $i"
                      iptables -I INPUT -s $i -j DROP
          echo -e "[`date "+%Y-%m-%d %T"`] IP: $i\tCOUNT: $COUNT" /root/ddos/banned.log
        fi
                  fi
          fi
    ((cnt=cnt+1))
  fi
done
echo "[`date "+%Y-%m-%d %T"`] Script runned, found $cnt" > /root/ddos/run.log

在/etc/crontab里设置:
*/1 * * * * root /root/shell/ddos.sh
0 * * * * iptables --flush

第一条是1分钟执行一次防御脚本,第二条是每小时清理一次IP禁止列表.第二条是可选的,主要是为了将已经封杀的IP解禁,避免过多的封杀或者误杀.一个小时的禁止时间也已经足够了.
夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-389.html

标签: shell 脚本 攻击 ddos 黑名单 白名单

评论: