天天看點

DDOS 攻擊與防禦

1、DDOS 簡介

       分布式拒絕服務(DDoS:Distributed Denial of Service)攻擊,指借助于客戶/伺服器技術,将多個計算機聯合起來作為攻擊平台,對一個或多個目标發動DDoS攻擊,進而成倍地提高拒絕服務攻擊的威力。

  1.1、攻擊原理圖

DDOS 攻擊與防禦

   1.2、實驗環境搭建

  實驗平台:CentOS release 6.4 (Final)

  服務端:node11  192.168.92.21

  攻擊端:node12  192.168.92.22

      web_server伺服器配置:

          yum -y install httpd     #安裝 web 服務

          service httpd start

          vim /var/www/html/index.html  #寫一個簡單的靜态頁面

          service iptables start   #開啟防火牆

          iptables -I INPUT -p TCP --dport 80 -j ACCEPT   #設定防火牆,運作 80 端口的服務通過

         測試站點是否可以進行通路:http://192.168.92.21/

DDOS 攻擊與防禦

    1.3、模拟攻擊

    工具簡介:webbench、ab指令,做壓力測試的工具和性能的監控工具

    wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz

    yum -y install ctags

    tar xf webbench-1.5.tar.gz

    cd webbench-1.5

   注:已經存在 Makefile 檔案,則直接安裝即可

   make

   make install

   注:此時出現以下報錯

    install -s webbench /usr/local/bin

    install -m 644 webbench.1 /usr/local/man/man1

    install: cannot create regular file `/usr/local/man/man1\': No such file or directory

    make: *** [install] Error 1

  解決: mkdir -p /usr/local/man/man1

  再次重新運作 make install 即可

 附:webbench 使用方法

   webbench -c 用戶端 -t 運作測試時間 URL

   例: 模拟100個用戶端10秒内對站點進行通路

   webbench -c 100 -t 10 http://192.168.92.21/

   1.3.1、攻擊測試  node12 機器

    webbench -c 100 -t 10 http://192.168.92.21/index.html   

   Webbench - Simple Web Benchmark 1.5   

   Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

  Benchmarking: GET http://192.168.92.21/index.html

  100 clients, running 10 sec.

  Speed=49032 pages/min, 48383672 bytes/sec.

  Requests: 8172 susceed, 0 failed.

  注:如上面結果所示,短時間内有大量的請求,則懷疑為DDOS攻擊

1.3.1.1、如何檢視是否受到DDOS攻擊? (web伺服器端檢視)

   netstat -ntu | awk \'{print $5}\' | cut -d: -f4 | sort | uniq -c | sort -n

DDOS 攻擊與防禦

 分析:通過指令在被攻擊的一端過濾出相同的連接配接IP在短時間内有上萬個,則可能存在DDOS

 1.3.2、如何防止 DDOS

 方法一:手動寫iptables 規則,ip位址數比較少時

方法二: 檢測到通路次數比較多的ip位址後,自動添加iptables規則。

 例:fail2ban或linux+DDoS deflate

       DDoS deflate介紹:DDoS deflate是一款免費的用來防禦和減輕DDoS攻擊的腳本。它通過netstat監測跟蹤建立大量網絡連接配接的IP位址,在檢測到某個結點超過預設的限制時,該程式會通過APF或IPTABLES禁止或阻擋這些IP

實戰: 使用DDoS deflate 解決伺服器被DDOS攻擊的問題

  DDoS deflate 安裝:

  [root@node11 ~]# wget http://www.inetbase.com/scripts/ddos/install.sh

  [root@node11 ~]# chmod +x install.sh

  [root@node11 ~]# ./install.sh  #此時可以下載下傳源檔案

DDOS 攻擊與防禦

   注:出現以上狀态則證明已經下載下傳成功,直接按q推出即可。

 配置檔案路徑:/usr/local/ddos/ddos.conf

  DDOS配置檔案介紹:

  [root@node11 ~]# cd /usr/local/ddos/

  ddos.conf  #主配置檔案

  ddos.sh    #主程式(調用此程式對DDOS進行防禦)

  ignore.ip.list  # IP位址的白名單

主配置檔案解釋:

[root@node11 ddos]# vim ddos.conf  #以下為部分解釋

PROGDIR="/usr/local/ddos"  #工作目錄

PROG="/usr/local/ddos/ddos.sh"  #主程式

IGNORE_IP_LIST="/usr/local/ddos/ignore.ip.list"  #白名單清單

CRON="/etc/cron.d/ddos.cron"  #計劃任務

APF="/etc/apf/apf"  

IPT="/sbin/iptables"

FREQ=1  #檢查的時間為1分鐘

NO_OF_CONNECTIONS=150  #最大連接配接數,超過這個數IP就會被屏蔽,一般預設即可

APF_BAN=0 #此處預設為1,修改為0 使用iptables防火牆,不使用apf防火牆

KILL=1  #是否屏蔽IP,預設即可

EMAIL_TO="root"  #當IP被屏蔽時給指定郵箱發送郵件報警,換成自己的郵箱即可

BAN_PERIOD=600  #禁用IP時間,預設600秒,可根據情況調整

注:安裝後,不需要手動運作任何軟體,因為有crontab計劃任務,每過一分鐘,會行自動執行一次。檢查是否有不正常的通路量

計劃任務解釋:

[root@node11 ddos]# cat /etc/cron.d/ddos.cron

SHELL=/bin/sh

0-59/1 * * * * root /usr/local/ddos/ddos.sh >/dev/null 2>&1

注:每分鐘檢視一下,是不是有ddos攻擊,如果發現就開始拒絕

腳本修改:

[root@node11 ~]# cp /usr/local/ddos/ddos.sh{,.`date +%Y%m%d`}

[root@node11 ~]# vim /usr/local/ddos/ddos.sh

117 netstat -ntu | awk \'{print $5}\' | cut -d: -f1 | sort | uniq -c | sort -nr > $BAD_IP_LIST

注:将117行的 -f1 修改為 -f4 ,如下:

117 netstat -ntu | awk \'{print $5}\' | cut -d: -f4 | sort | uniq -c | sort -nr > $BAD_IP_LIST

測試1:

[root@node12 ~]# webbench -c 100 -t 10 http://192.168.92.21/index.html

DDOS 攻擊與防禦

服務端防火牆目前狀态檢查:

DDOS 攻擊與防禦

注:檢視以上的防火牆狀态發現已經有 IP 被DROP掉

測試2: 使用 ab 指令進行測試

使用參數: -n  次數    -c  用戶端數量

[root@node12 ~]# ab -n 1000 -c 10 http://192.168.92.21/index.html      #輸出結果如下所示

This is ApacheBench, Version 2.3 <$Revision: 655654 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.92.21 (be patient)

Completed 100 requests

Completed 200 requests

Completed 300 requests

Completed 400 requests

Completed 500 requests

Completed 600 requests

Completed 700 requests

Completed 800 requests

Completed 900 requests

Completed 1000 requests

Finished 1000 requests

Server Software: Apache/2.2.15

Server Hostname: 192.168.92.21

Server Port: 80

Document Path: /index.html

Document Length: 59140 bytes

Concurrency Level: 10

Time taken for tests: 1.176 seconds

Complete requests: 1000

Failed requests: 0

Write errors: 0

Total transferred: 59412000 bytes

HTML transferred: 59140000 bytes

Requests per second: 850.29 [#/sec] (mean)

Time per request: 11.761 [ms] (mean)

Time per request: 1.176 [ms] (mean, across all concurrent requests)

Transfer rate: 49333.15 [Kbytes/sec] received

Connection Times (ms)

min mean[+/-sd] median max

Connect: 0 0 0.7 0 11

Processing: 1 11 9.9 8 127

Waiting: 0 6 2.1 6 15

Total: 2 12 10.0 8 127

Percentage of the requests served within a certain time (ms)

50% 8

66% 9

75% 10

80% 12

90% 29

95% 33

98% 40

99% 44

100% 127 (longest request)

服務端防火牆清單狀态檢視:(等待一段時間再檢視)

DDOS 攻擊與防禦

結果分析:此時服務端防火牆DROP掉了源IP(即:攻擊IP),攻擊方無法再次使用源IP對服務端進行攻擊。

#攻擊方輸出如下資訊:

[root@node12 ~]# ab -n 1000 -c 10 http://192.168.92.21/index.html

This is ApacheBench, Version 2.3 <$Revision: 655654 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.92.21 (be patient)

apr_poll: The timeout specified has expired (70007)

#同時使用webbench發現無法進行攻擊,證明ip被禁止掉了

[root@node12 ~]# webbench -c 100 -t 10 http://192.168.92.21/index.html

Webbench - Simple Web Benchmark 1.5

Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.92.21/index.html

100 clients, running 10 sec.

Connect to server failed. Aborting benchmark.

附錄1:軟體下載下傳位址

Webbench:

http://home.tiscali.cz/~cz210552/webbench.html

DDoS deflate官方網站:

http://deflate.medialayer.com/

附錄2:常見問題解決

1、DDos deflate 安裝第一次可能不成功

解決:解除安裝後再裝一次就行了

[root@node11 ~]# wget http://www.inetbase.com/scripts/ddos/uninstall.ddos

[root@node11 ~]# chmod  +x uninstall.ddos

[root@node11 ~]# ./uninstall.ddos