你想不到的方法,使用DDOS腳本防止DDOS攻擊

文本編輯器 腳本語言 HTML AWK 小木IT 2017-04-21

今天來給大家分享一下如何使用DDOS腳本防止DDOS攻擊,希望對大家有所幫助!

廢話不多說,現在開始拉!

DDOS概述:

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

攻擊原理如圖:

你想不到的方法,使用DDOS腳本防止DDOS攻擊

你想不到的方法,使用DDOS腳本防止DDOS攻擊

安裝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 ~]#

測試訪問:

http://192.168.1.63/

你想不到的方法,使用DDOS腳本防止DDOS攻擊

測試,模擬DDOS

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

webbench:

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

Yum -y install gcc* ctags

下載下來——》解壓——》安裝

報錯:

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 10http://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

1、安裝DDoS deflate

[root@xuegod63 ~]#wgethttp://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 [email protected]

##############################################################################

##############################################################################

# "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 輸入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=[email protected]n //當IP被屏蔽時給指定郵箱發送郵件報警,換成自己的郵箱即可

##### Number of seconds the banned ip should remain in blacklist.

BAN_PERIOD=600 //禁用IP時間,默認600秒,可根據情況調整

用戶可根據給默認配置文件加上的註釋提示內容,修改配置文件。

需要重啟服務???

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

測試:

在xuegod64上模擬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

等待一分鐘後查看結果,出現如下截圖說明成功。

你想不到的方法,使用DDOS腳本防止DDOS攻擊

再執行一下ab你會發現已經不能執行了。

你想不到的方法,使用DDOS腳本防止DDOS攻擊

一鍵卸載:

[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

以上內容希望對大家有所幫助,歡迎點擊關注我哦!更多精彩內容等著你!

你想不到的方法,使用DDOS腳本防止DDOS攻擊

相關推薦

推薦中...