LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2974|回复: 10

[原创]利用ip_conntrack表实现封ip的shell脚本,并有简单的web发布

[复制链接]
发表于 2005-5-6 23:55:14 | 显示全部楼层 |阅读模式
在下学习shell时间不长, 在此版中收益非潜, 增加了偶的学习积极性和学习效率
知道版主鼓励原创, 故在下发此贴支持版主工作
初来乍到, 还望各位高人指点.  :thank

===============================================
基本原理:
通过过滤ip_conntrack表得到ESTABLISHED状态过多的ip, 然后用iptabels封掉一段时间,同时用hping工具将这些ip从表中清理掉,最后将被封的ip和一些其他信息写到一个html页中,做简单的发布

关于hping:
下载: http://www.hping.org/download.html
安装: ./configure;make;make install
相关联接: http://chinaunix.net/jh/4/367999.html

默认功能:
1, 当一个ip在ip_conntrack表中的ESTABLISHED状态在30-50之间时, 此ip被封10分钟,同时在ip_conntrack表中的记录被清除;50-100之间封15分钟,同时清表;100以上封30分钟,同时清表,
2, 然后生成web页
/var/www/html/wwy/drop/index.html   --- 显示被封的ip, 和cpu状态等信息
/var/www/html/wwy/all/index.html   --- 每一个ip的连接情况
3, 生成简单的日志
/tmp/killip/tmp.log.txt

使用方法:
1, 需要安装hping
2, 建议将脚本放到计划任务中
3, 建议安装并开启apache, 为支持简单的web发布,
默认为 http://127.0.0.1/l/wwy/drop/index.htm
4, 如果表的大小大于20mb请慎用

====================================================



  1. #!/bin/bash
  2. #
  3. #---------------------------------------------------------------------------------------
  4. #Scrip name: killip, base on ip_conntrack, write by wwy.
  5. #---------------------------------------------------------------------------------------

  6. cpu=`sar -u 1 1 | awk '{print $7}' | tail -1`%
  7. #
  8. while [ "`pidof sleep`" ];do
  9.         echo "she is running, sorry"
  10.         exit 1
  11. done
  12. if [ ! "`lsmod | grep ip_conntrack`" ]; then
  13.          modprobe ip_conntrack
  14. fi

  15. ####################################
  16. ##---------------------- functions -----------------------------##
  17. ####################################

  18. function make_clr {
  19.         while read clr33;do
  20.                 cat /tmp/tmp111.txt | grep $clr33 >> /tmp/tmp33-3-clr.txt
  21.         done < /tmp/tmp33-3.txt
  22.         while read clr22;do
  23.                 cat /tmp/tmp111.txt | grep $clr22 >> /tmp/tmp33-2-clr.txt
  24.         done < /tmp/tmp33-2.txt
  25.         while read clr11;do
  26.                 cat /tmp/tmp111.txt | grep $clr11 >> /tmp/tmp33-1-clr.txt
  27.         done < /tmp/tmp33-1.txt
  28. }
  29. function clr_conns {
  30.         S_IP=$1
  31.         D_IP=$2
  32.         S_PORT=$3
  33.         D_PORT=$4
  34.         hping2 $D_IP -R -s $S_PORT -p $D_PORT -a $S_IP -k -c 1 >/dev/null 2>/dev/null &
  35. }
  36. function kill() {
  37.         SLEEP_TIME=$1
  38.         CLR_LIST=$2
  39.         BLACK_LIST=$3
  40.         while read blackip;do
  41.                 iptables -I FORWARD 2 -i eth0 -s $blackip/32  -j DROP
  42.         done < $BLACK_LIST
  43.         sleep $SLEEP_TIME
  44.         #-----------------------------------#
  45.         while read clr3;do
  46.                 clr_conns $clr3
  47.         done < $CLR_LIST
  48.         #-----------------------------------#
  49.         sleep 1
  50.         while read reblackip;do
  51.                 iptables -D FORWARD -i eth0 -s $reblackip/32  -j DROP
  52.         done < $BLACK_LIST

  53. }
  54. #####################################
  55. ##--------------- To make a "black list" ----------------------##
  56. #####################################

  57. echo > /tmp/tmp11.txt
  58. echo > /tmp/tmp111.txt
  59. echo > /tmp/ip_conntrack.tmp
  60. echo > /tmp/tmp33-3-clr.txt
  61. echo > /tmp/tmp33-2-clr.txt
  62. echo > /tmp/tmp33-1-clr.txt
  63. echo > /tmp/tmp22-3.txt
  64. echo > /tmp/tmp22-2.txt
  65. echo > /tmp/tmp22-1.txt
  66. echo > /tmp/tmp33-3.txt
  67. echo > /tmp/tmp33-2.txt
  68. echo > /tmp/tmp33-1.txt
  69. if [ ! -e /var/www/html/wwy/index.html ];then
  70.         mkdir /var/www/html/wwy/
  71.         mkdir /var/www/html/wwy/all
  72.         mkdir /var/www/html/wwy/drop
  73. 5B
  74.         touch /var/www/html/wwy/index.html
  75. fi
  76. #----------------------------------------------------------------------------#
  77. echo -e "cp /proc/net/ip_conntrack /tmp/ip_conntrack.tmp ......\c"
  78. cp /proc/net/ip_conntrack /tmp/ip_conntrack.tmp
  79. echo -e "done!\n"
  80. sleep 1
  81. #----------------------------------------------------------------------------#
  82. wc=`cat /tmp/ip_conntrack.tmp|grep ESTABLISHED|awk -F= '{print $2,$3,$4,$5}'|grep ^172. |sort|awk '{print $1,$3,$5,$7}'|tee /tmp/tmp111.txt|awk '{print $1}'|uniq -c|tee /tmp/tmp11.txt|wc -l`
  83. date=`date '+%m/%d %H:%M'`
  84. cpu2=`sar -u 1 1 | awk '{print $7}' | tail -1`%
  85. date2=`date '+%H'`
  86. #----------------------------------------------------------------------------#
  87. sleep 1
  88. #----------------------------------------------------------------------------#
  89. #if [ "$wc" -gt 2500 ] && [ "$date2" -gt 10 ]
  90. if [ "$wc" -ge 0 ]
  91. then
  92. #------------------------------
  93.         awk '{$1}{if ($1>30 && $1<50) print $2}' /tmp/tmp11.txt > /tmp/tmp22-1.txt
  94.         awk '{$1}{if ($1>=50 && $1<100) print $2}' /tmp/tmp11.txt > /tmp/tmp22-2.txt
  95.         awk '{$1}{if ($1>=100) print $2}' /tmp/tmp11.txt > /tmp/tmp22-3.txt
  96.         cut -c1-15 /tmp/tmp22-1.txt > /tmp/tmp33-1.txt
  97.         cut -c1-15 /tmp/tmp22-2.txt > /tmp/tmp33-2.txt
  98.         cut -c1-15 /tmp/tmp22-3.txt > /tmp/tmp33-3.txt
  99.         wcblackip1=`cat /tmp/tmp33-1.txt | wc -l`
  100.         wcblackip2=`cat /tmp/tmp33-2.txt | wc -l`
  101.         wcblackip3=`cat /tmp/tmp33-3.txt | wc -l`

  102. ######################################
  103. ##---------------- To make a index.html -----------------------##
  104. ######################################

  105.         echo "<b>If the total IPs >2500 <font color="#ff0000">(total $wc at $date)</font> AND if:</b>" > /var/www/html/wwy/drop/index.html
  106.         echo "<p>you connect <b>">100"</b>, you ip will be killed in <b>30min</b>.</p>" >>/var/www/html/wwy/drop/index.html
  107.         echo "<p>you connect <b>"50-100"</b>, you ip will be killed in <b>15min</b>.</p>" >>/var/www/html/wwy/drop/index.html
  108.         echo "<p>you connect <b>"30-50"</b>, you ip will be killed in <b>10min</b>.</p>" >>/var/www/html/wwy/drop/index.html
  109.         echo "<hr color="#ff8000">" >> /var/www/html/wwy/drop/index.html
  110.         echo "<p><b><font color="#ff0000">These IPs (total $wcblackip3 + $wcblackip2 + $wcblackip1) were killed, at <font size=5>$date</font></font>  <a href=../all>(look-up all IPs)</a></b></p>" >> /var/www/html/wwy/drop/index.html
  111.         awk '{$1}{if ($1>=100) print $1, $2}' /tmp/tmp11.txt|sort -nr|awk '{print "<p>""<font color="#ff0000">"$1"</font>""\t","<b>"$2"</b>""\t""kill 30min""</p>"}' >> /var/www/html/wwy/drop/index.html
  112.         awk '{$1}{if ($1>=50 && $1<100) print $1, $2}' /tmp/tmp11.txt|sort -nr|awk '{print "<p>"$1"\t","<b>"$2"</b>""\t""kill 15min""</p>"}' >> /var/www/html/wwy/drop/index.html
  113.         awk '{$1}{if ($1>30 && $1<50) print $1, $2}' /tmp/tmp11.txt|sort -nr|awk '{print "<p>"$1"\t","<b>"$2"</b>""\t""kill 10min""</p>"}' >> /var/www/html/wwy/drop/index.html
  114.         echo "<p><b>You can "ctrl + F" to find your ip's connects.(total $wc IPs at $date)</b></p>" > /var/www/html/wwy/all/index.html
  115.         echo "<p><a href=../drop> <-- back </a></p>" >> /var/www/html/wwy/all/index.html
  116.         cat /tmp/tmp11.txt | sort -nr | awk '{print "<p>"$1"\t",$2"\t""</p>"}' >> /var/www/html/wwy/all/index.html

  117. #####################################
  118. ##----------------- Use iptables to DROP ---------------------##
  119. #####################################

  120.         make_clr
  121.         if [ -s /tmp/tmp33-3.txt ];then
  122.                 kill 30m /tmp/tmp33-3-clr.txt /tmp/tmp33-3.txt &
  123.                 sleep 1s
  124.         fi
  125.         if [ -s /tmp/tmp33-2.txt ];then
  126.                 kill 15m /tmp/tmp33-2-clr.txt /tmp/tmp33-2.txt &
  127.                 sleep 1s
  128.         fi
  129.         if [ -s /tmp/tmp33-1.txt ];then
  130.                 kill 10m /tmp/tmp33-1-clr.txt /tmp/tmp33-1.txt &
  131.                 sleep 1s
  132.         fi

  133. #-------------------------------
  134. elif [ "$date2" -lt 5 ] && [ "$date2" -gt 3 ]
  135. then
  136.         while read clrall;do
  137.                 clr_conns $clrall
  138.         done < /tmp/tmp111.txt
  139.         echo "clr at $date " >> /tmp/killip/tmp.log.txt
  140. fi

  141. #####################################
  142. ##------------------- make system log ------------------------##
  143. #####################################
  144. if [ ! -e /tmp/killip/tmp.log.txt ]; then
  145.         mkdir /tmp/killip
  146.         touch /tmp/killip/tmp.log.txt
  147. fi
  148. echo "$wc $date $cpu $cpu2 $wcblackip3 + $wcblackip2 + $wcblackip1" >> /tmp/killip/tmp.log.txt

复制代码
发表于 2005-5-7 17:44:56 | 显示全部楼层
为什么不直接用netstat来获取那些ESTABLISHED信息?!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-7 20:02:01 | 显示全部楼层
Post by Freebird
为什么不直接用netstat来获取那些ESTABLISHED信息?!


如果是做nat或route的机器,netstat就不行了
回复 支持 反对

使用道具 举报

发表于 2005-5-8 07:40:01 | 显示全部楼层
sar这个命令不在linux的基本命令中,最好在用之前check一下。不过,非常支持原创是真的。谢谢
回复 支持 反对

使用道具 举报

发表于 2005-7-22 15:38:04 | 显示全部楼层
郁闷,我的linux(redhat 9.0 2.4.20)没有sar命令,我想请问各位大侠,哪里可以下载sar,让我装上使用。

谢谢!
回复 支持 反对

使用道具 举报

发表于 2005-7-22 16:17:58 | 显示全部楼层
已经找到,只需要安装sysstat即可,正在测试wwy的脚本,不管成功与否,都什么感谢wwy的脚本!^_^
回复 支持 反对

使用道具 举报

发表于 2005-7-22 16:21:25 | 显示全部楼层
使用sysstat就可以了,^_^,正在测试wwy的脚本
回复 支持 反对

使用道具 举报

发表于 2005-7-22 16:24:26 | 显示全部楼层
为何执行了wwy的脚本后,无法显示/var/www/html/wwy/all/index.html --- 每一个ip的连接情况,提示内容为
You can "ctrl + F" to find your ip's connects.(total 0 IPs at 07/22 16:15)

<-- back

执行脚本没有出错,[root@nci root]# ./hping
cp /proc/net/ip_conntrack /tmp/ip_conntrack.tmp ......done!

hping为脚本的名称,已经安装hping2,使用版本为( hping2.0.0-rc3.tar.gz)
请wwy予以指教,谢谢!
回复 支持 反对

使用道具 举报

发表于 2005-7-25 13:07:22 | 显示全部楼层
Post by king.zhangyu
为何执行了wwy的脚本后,无法显示/var/www/html/wwy/all/index.html --- 每一个ip的连接情况,提示内容为
You can "ctrl + F" to find your ip's connects.(total 0 IPs at 07/22 16:15)

<-- back

执行脚本没有出错,[root@nci root]# ./hping
cp /proc/net/ip_conntrack /tmp/ip_conntrack.tmp ......done!

hping为脚本的名称,已经安装hping2,使用版本为( hping2.0.0-rc3.tar.gz)
请wwy予以指教,谢谢!


把脚本的这几行加一个参数试一下:
        mkdir /var/www/html/wwy/
        mkdir /var/www/html/wwy/all
        mkdir /var/www/html/wwy/drop
改成:
        mkdir -p /var/www/html/wwy/
        mkdir -p /var/www/html/wwy/all
        mkdir -p /var/www/html/wwy/drop

另外要确定APACHE的主目录在/var/www/html

如果还不行,只好请wwy帮忙了.
回复 支持 反对

使用道具 举报

发表于 2005-7-26 01:10:10 | 显示全部楼层
Post by kiron
把脚本的这几行加一个参数试一下:
        mkdir /var/www/html/wwy/
        mkdir /var/www/html/wwy/all
        mkdir /var/www/html/wwy/drop
改成:
        mkdir -p /var/www/html/wwy/
        mkdir -p /var/www/html/wwy/all
        mkdir -p /var/www/html/wwy/drop

另外要确定APACHE的主目录在/var/www/html

如果还不行,只好请wwy帮忙了.

简化一下,可以变成
  1. mkdir -p /var/www/html/wwy/{all,drop}
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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