1、DDOS 簡介 html
分佈式拒絕服務(DDoS:Distributed Denial of Service)攻擊,指藉助於客戶/服務器技術,將多個計算機聯合起來做爲攻擊平臺,對一個或多個目標發動DDoS攻擊,從而成倍地提升拒絕服務攻擊的威力。node
1.1、攻擊原理圖linux
1.2、實驗環境搭建web
實驗平臺:CentOS release 6.4 (Final)apache
服務端:node11 192.168.92.21vim
攻擊端: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/網絡
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
分析:經過命令在被攻擊的一端過濾出相同的鏈接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 #此時能夠下載源文件
注:出現以上狀態則證實已經下載成功,直接按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
服務端防火牆當前狀態檢查:
注:查看以上的防火牆狀態發現已經有 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)
服務端防火牆列表狀態查看:(等待一段時間再查看)
結果分析:此時服務端防火牆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