一个自动封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百分百会被他封掉。

posted @ 2017-04-28 11:25  玩蛇者  阅读(3945)  评论(0编辑  收藏  举报