一江春水向東流

          做一個(gè)有思想的人,期待與每一位熱愛思考的人交流,您的關(guān)注是對(duì)我最大的支持。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            44 隨筆 :: 139 文章 :: 81 評(píng)論 :: 0 Trackbacks

          寫了一個(gè)簡(jiǎn)單的防止IP攻擊的腳本
          由于工作需要我就自己寫了一個(gè)簡(jiǎn)單的防止IP攻擊的腳本,可以防止linux虛擬主機(jī)一些小方面的IP攻擊

          系統(tǒng)是基于RHEL的centos,包括3,4,5三個(gè)版本,當(dāng)然自己也初學(xué)shell,中間肯定用了很多笨的辦法,效果也不一定怎么樣,請(qǐng)大家給點(diǎn)意見

          注意:這個(gè)腳本是根據(jù)apache服務(wù)器的server-status和系統(tǒng)的dmesg分析結(jié)果進(jìn)行防范的,所以非apache用戶和沒有開啟server-status的朋友沒法使用

          可以在服務(wù)器的crontab里設(shè)定每一分鐘運(yùn)行一次腳本,
          復(fù)制下面的腳本到autoblock.sh,
          root用戶下# chmod u+x autoblock.sh

          #!/bin/bash
          # author hao32

          # basic setting
          echo 1 > /proc/sys/net/ipv4/tcp_syncookies

          # find? server-status? name
          ss_name="/usr/local/autoblock"

          if [ -e $ss_name/ss_name ];then
          ss_n=`cat $ss_name/ss_name`
          else
          mkdir /usr/local/autoblock >/dev/null 2>&1
          cat `locate httpd.conf|grep -E "httpd/conf/httpd.conf$|apache_ssl/conf/httpd.conf$"`\
          |grep "n /server-status"|cut -d/ -f2|cut -d\> -f1 > $ss_name/ss_name
          ss_n=`cat $ss_name/ss_name`
          fi

          # block setting
          #設(shè)定排除的IP地址
          ip_exclude="192.168.1.*|60.195.249.*|222.76.212.*|218.241.156.*|58.215.87.*|218.107.216.110"
          ip_amou=25
          ss_url="

          ss_tmp="/tmp/server-status"
          poss_ip="/tmp/poss_ip"
          real_ip="/tmp/real_ip"

          # block start...
          if [ -e "$poss_ip" ];then
          echo "" > $poss_ip
          fi
          if [ -e "$real_ip" ];then
          echo "" > $real_ip
          fi

          # analysedemsg
          dmesg |grep "short"|awk '{if($4!="From"){print $4} else {print $5}}'|awk -F: '{print $1}'|sort|uniq>$poss_ip

          wget -q -O "$ss_tmp" "$ss_url"

          grep "<i>" $ss_tmp|grep -vE $ip_exclude|awk '{print $1}'|sed 's/<i>//g'|sort|uniq -c\
          |awk '{if($1>'$ip_amou') print $2}'>>$poss_ip

          #iptables -nvL|grep "DROP? "|awk '{print $8}'|sort|uniq|sed 's/0\/24/*/g'>$rule_ip
          rule_ip=`iptables -nvL|grep "DROP? "|awk '{print $8}'|sort|uniq|sed 's/0\/24/*/g'|xargs|sed 's/\ /|/g'`
          if [ -z $rule_ip ];then
          for i in `cat $poss_ip`
          do
          /sbin/iptables -I INPUT -p all -s $i -j DROP
          done
          else
          cat $poss_ip|grep -vE "$rule_ip" > $real_ip
          for i in `cat $real_ip`
          do
          /sbin/iptables -I INPUT -p all -s $i -j DROP
          done

          fi

          主站蜘蛛池模板: 枣阳市| 广州市| 巴马| 蓝山县| 永川市| 东光县| 长岭县| 威信县| 清河县| 宁蒗| 阿拉尔市| 甘德县| 合水县| 湛江市| 龙口市| 青海省| 油尖旺区| 元朗区| 福州市| 定襄县| 思南县| 苍南县| 乃东县| 北安市| 通江县| 榆社县| 陆河县| 蒙自县| 阜康市| 五台县| 龙井市| 金溪县| 资兴市| 汉中市| 高雄县| 苗栗市| 皋兰县| 蒲江县| 陆河县| 东海县| 望奎县|