一個自動封IP防御DDOS腳本


DDoS deflate 工作原理

每分鍾檢測一次IP連接狀況,當某些IP連接超過配置腳本限制的連接數,程序會自動禁止這些IP一段時間,以達到防御攻擊的目的

DDoS deflate官方網站:http://deflate.medialayer.com/

 

Installation 安裝

wget http://www.inetbase.com/scripts/ddos/install.sh 
chmod 0700 install.sh 
./install.sh

Uninstallation 卸載

wget http://www.inetbase.com/scripts/ddos/uninstall.ddos 
chmod 0700 uninstall.ddos 
./uninstall.ddos

檢查是否遭到ddos攻擊命令

 

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

 

DDoS deflate的默認配置位於/usr/local/ddos/ddos.conf ,內容如下:

##### Paths of the script and other files 
PROGDIR="/usr/local/ddos" 
PROG="/usr/local/ddos/ddos.sh" 
IGNORE_IP_LIST="/usr/local/ddos/ignore.ip.list"  //IP地址白名單 
CRON="/etc/cron.d/ddos.cron"    //定時執行程序 
APF="/etc/apf/apf" 
IPT="/sbin/iptables"  
##### 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即可。  
##### 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="root"   //當IP被屏蔽時給指定郵箱發送郵件,推薦使用,換成自己的郵箱即可  
##### Number of seconds the banned ip should remain in blacklist. 
BAN_PERIOD=600    //禁用IP時間,默認600秒,可根據情況調整

試用了一會,感覺還不錯,比我以前用的分析nginx日志封IP的腳本要好些,假如幾個虛擬主機使用不同的網站日志,就必須運行幾個分析日志的腳本,

這個腳本就用一個就可以把整個服務器保護起來了。但這個也存在一些問題,剛開始的時候把mysql服務器給封掉了,搞的我手忙腳亂的解封,

然后把mysql服務器IP添加白名單,誰知過會他過了段時間又把白名單刪除了,又給mysql服務器封了,不得以卸載了一次。

研究了一下代碼才發現,他禁止的IP也添加到白名單里,然后解封的時候通過tmp目錄里的unban.xxxxx文件將白名單里禁止的IP再刪除,

這樣這個IP就不再受保護。所以我把mysql服務器IP添加白名單之后程序根據這個BAN_PERIOD=600 參數在10分鍾后刪除,所以就出現了你恢復他繼續封的怪圈,白名單設置文件應該和禁止解禁這個IP記錄文件分開就好了。

使用心得:

安裝后第一時間要將白名單設置好,最好觀察一段時間看是不是正常,以免一些和服務器需要保持連接的IP被封,比如mysql服務器,rsync備份服務器等等,這些機器往往都需要和網站服務器保持大量連接

 

另外假如采用CDN加速不適於用這腳本,cdn節點緩存服務器IP百分百會被他封掉。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM