Webbench、ab命令:作壓力測試的工具和性能的監控工具

DDOS攻擊:???
DDOS概述:
分佈式拒絕服務(DDoS:Distributed Denial of Service)攻擊,指藉助於客戶/服務器技術,將多個計算機聯合起來做爲攻擊平臺,對一個或多個目標發動DDoS攻擊,從而成倍地提升拒絕服務攻擊的威力。html

攻擊原理如圖:
我的PC/服務器 訪問web服務器操做
服務器1G
服務器1G
服務器1G web服務器帶寬1G:帶寬被耗盡時掛掉
服務器1G
LINUX虛擬機安裝測試環境
安裝web服務器: [root@xuegod63 ~]# yum install httpd
生成默認首頁: [root@xuegod63 ~]# cp /etc/passwd /var/www/html/index.html
啓動web服務器 [root@xuegod63 ~]# service httpd restart
Stopping httpd: [FAILED]
Starting httpd: [ OK ]
[root@xuegod63 ~]# linux

測試訪問: http://192.168.1.63/ 測試,模擬DDOS web

Webbench、ab命令:作壓力測試的工具和性能的監控工具 vim

webbench: 官網:http://home.tiscali.cz/~cz210552/webbench.html
Yum -y install gcc* ctags bash

下載下來——》解壓——》安裝 服務器

已有Makefile 直接make 報錯:
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 && make install
echo $? 0 分佈式

使用方法: webbench -c 客戶端 -t 運行測試時間 URL 工具

例如: webbench -c 100 -t 10 http://192.168.1.100/index.html
[root@localhost webbench-1.5]# webbench -c 100 -t 10 http://192.168.1.100/index.html
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://192.168.1.100/index.html
100 clients, running 10 sec.
Speed=244026 pages/min, 6732845 bytes/sec. Requests: 40671 susceed, 0 failed.性能

如何查是否受到DDOS攻擊?
腳本內容:
經過:netstat 查看網絡鏈接數。若是一個IP地址對服務器創建不少鏈接數(好比一分鐘產生了100個鏈接),就認爲發生了DDOS
[root@xuegod63 html]# vim ddos-test.sh
#寫入如下內容
#!/bin/bash netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
註釋: #!/bin/bash
netstat -ntu | awk '{print $5}' | cut -d: -f4 | sort | uniq -c | sort -n
截取外網IP和端口 截取外網的IP以:爲分隔符 |排序 | 排除相同的記錄 | 排序並統計

防止DDOS:???
方法一: 手動寫iptables 規則,ip地址數比較少時
方法二: 檢測到訪問次數比較多的ip地址後,自動添加iptables規則。
如fail2ban或linux+DDoS deflate DDoS
deflate介紹 DDoS deflate是一款免費的用來防護和減輕DDoS攻擊的腳本。
它經過netstat監測跟蹤建立大量網絡鏈接的IP地址,在檢測到某個結點超過預設的限制時,該程序會經過APF或IPTABLES禁止或阻擋這些IP.
DDoS deflate 官方網站:http://deflate.medialayer.com/ 被屏蔽,有可能打不開。


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

方法 1: 檢測是否有DDOS攻擊 執行: netstat -ntu | awk '{print $5}' | cut -d: -f4 | sort | uniq -c | sort -n
若是發現某個IP鏈接數據上百的連接,說明就有DDOS攻擊。

方法2: IDC 機房,大家公司服務器,大量往外發送數據包。 IDC機房管理員會給你打電話。

方法3: 阿里雲主機有監測機制,若是你的雲主機大量對外發包,阿里雲會給你發短信,處理不及時,把你的雲主機就封了。

下面開始安裝DDos deflate
一、安裝DDoS deflate
[root@xuegod63 ~]#wget http://www.inetbase.com/scripts/ddos/install.sh //肯定能上網
[root@xuegod63 ~]# chmod 700 install.sh //添加權限
[root@xuegod63 ~]#./install.sh //安裝 第一次可能不成功,卸載後再裝一次就好了
Installing DOS-Deflate 0.6 Downloading source files.........done Creating cron to run script every minute.....
(Default setting).....done Installation has completed.
Config file is at /usr/local/ddos/ddos.conf Please send in your comments and/or suggestions to zaf@vsnl.com
##############################################################################
##############################################################################
# "Artistic License" # #
# # Preamble # #
# # The intent of this document is to state the conditions under which a #
# Package may be copied, such that the Copyright Holder maintains some # 輸入q 退出。

DDoS deflate安裝路徑:
[root@xuegod63 ~]# ls /usr/local/ddos/
配置文件:
[root@xuegod63 ~]# ls /usr/local/ddos/ddos.conf /usr/local/ddos/ddos.conf
[root@xuegod63 ~]# cat /usr/local/ddos/ignore.ip.list #IP地址白名單 127.0.0.1
[root@xuegod63 ~]# vim /usr/local/ddos/ddos.conf #查看
##### Paths of the script and other files
PROGDIR="/usr/local/ddos" #主目錄
PROG="/usr/local/ddos/ddos.sh" #要執行的DDOS腳本
IGNORE_IP_LIST="/usr/local/ddos/ignore.ip.list" //IP地址白名單,注:在這個文件中IP不受控制。
CRON="/etc/cron.d/ddos.cron" //定時執行程序
查看定時任務:
[root@xuegod63 ~]# cat /etc/cron.d/ddos.cron
SHELL=/bin/sh 0-59/1 * * * * root /usr/local/ddos/ddos.sh >/dev/null 2>&1

注:每分鐘查看一下,是否是有ddos攻擊,若是發現就開始拒絕
擴展: install.sh #DDOS deflate安裝腳本的功能:1,自動下載文件進行安裝 2,自動執行。
聯想:木馬程序。 變種成木馬程序。 最重要的是:他的這個計劃任務,你查不到。
實戰: 若是1分鐘內,一個IP地址對咱們服務器訪問150次以上,就認爲發生DDOS,使用iptables把這個IP地址自動屏蔽掉。

[root@xuegod63 ~]# vim /usr/local/ddos/ddos.conf
配置文件中的註釋以下:
##### frequency in minutes for running the script
##### Caution: Every time this setting is changed, run the script with --cron
##### option so that the new frequency takes effect
FREQ=1 //檢查時間間隔,默認1分鐘
##### How many connections define a bad IP? Indicate that below.
NO_OF_CONNECTIONS=150 //最大鏈接數,超過這個數IP就會被屏蔽,通常默認便可
##### APF_BAN=1 (Make sure your APF version is atleast 0.96)
##### APF_BAN=0 (Uses iptables for banning ips instead of APF)
APF_BAN=1 //使用APF仍是iptables。推薦使用iptables,將APF_BAN的值改成0便可。
改:19 APF_BAN=1
爲:19 APF_BAN=0
##### KILL=0 (Bad IPs are'nt banned, good for interactive execution of script)
##### KILL=1 (Recommended setting) KILL=1 //是否屏蔽IP,默認便可
##### An email is sent to the following address when an IP is banned.
##### Blank would suppress sending of mails EMAIL_TO=cd@xuegod.cn //當IP被屏蔽時給指定郵箱發送郵件報警,換成本身的郵箱便可
##### Number of seconds the banned ip should remain in blacklist. BAN_PERIOD=600 //禁用IP時間,默認600秒,可根據狀況調整 用戶可根據給默認配置文件加上的註釋提示內容,修改配置文件。
須要重啓服務???
注:安裝後,不須要手動運行任何軟件,由於有crontab計劃任務,每過一分鐘,會行自動執行一次。
檢查是否有不正常的訪問量 必須改:
[root@xuegod63 ~]# vim /usr/local/ddos/ddos.sh #把原來的f1改成f4 ,第四列

測試:
在Linux模擬DDOS
[root@xuegod64 html]# ab -n 1000 -c 10 http://192.168.1.63/index.html
當即查看防火牆規則是沒有的:
[root@xuegod63 ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@xuegod63 ~]# 開啓防火牆 iptables -I INPUT -p TCP --dport 80 -j ACCEPT
等待一分鐘後查看結果,出現訪問的ip(本地Linux虛擬機)說明成功。
再執行一下ab你會發現已經不能執行了。

一鍵卸載:
[root@xuegod63 ~]#wget http://www.inetbase.com/scripts/ddos/uninstall.ddos
[root@xuegod63 ~]# chmod +x uninstall.ddos
[root@xuegod63 ~]# ./uninstall.ddos
Uninstalling DOS-Deflate
Deleting script files.........done Deleting cron job.......done
Uninstall Complete
---恢復內容結束---

蹭課地址:https://ke.qq.com/course/139455#tuin=99f06607

相關文章
相關標籤/搜索