源代码:
国内:https://gitee.com/mirrors/ddos-deflate/
国外:https://github.com/jgmdev/ddos-deflate
DDoS Deflate �0�2是个轻量级的 Bash Shell 脚本设计的来帮助阻止/拒绝服务攻击的程序,它利用�0�2 netstat �0�2命令来获取连接到服务器的IP地址列表,以及它们的连接总数。这是个软件级最简单、最容易安装的防DDos的解决方案之一。就明月的经验来说是非常适合我们博客站长们装在自己的服务器上的一个防御脚本,配合�0�2 iptables �0�2防火墙使用可以说是价廉物美。
DDoS Deflate 获取连接到服务器的IP地址命令:
netstat -an | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
或者 netstat -ntu |awk '{print $5}'|cut -d: -f1 |sort |uniq -c |sort -n netstat�0�2-ntp�0�2|awk�0�2'{print�0�2$5}'|awk�0�2'BEGIN{FS=":"}�0�2{print�0�2$1}'�0�2|sort�0�2|uniq�0�2-c�0�2|sort�0�2-k�0�21�0�2-n
PS:建议大家保留这个命令在Xshell终端里面,可以随时观察连接到服务器的IP情况。
当IP地址连接数达到预先配置好的的连接数时, DDoS Deflate 就会自动在服务器的防火墙里屏蔽这个IP(还可以设定屏蔽的时间长短),它可以直接使用呢IPFW、iptables、或高级策略防火墙(APF)。(官方强烈建议使用 APF 高级策略防火墙。这个有机会明月准备试试:-)。)
DDoS Deflate的主要特点(就不翻译了,英文的原汁原味比较好)(其实是明月英语太渣了)
It is possible to whitelist IP addresses,via/etc/ddos/ignore.ip.list.
It is possible to whitelist hostnames,via/etc/ddos/ignore.host.list.
Simple configuration file:/etc/ddos/ddos.conf
IP addresses are automatically unblocked after a preconfigured time
limit(default:600 seconds)
limit(default:600 seconds) The script can run as a cron job at chosen frequency via the
configuration file(default:1 minute)
configuration file(default:1 minute) The script can run as a daemon at chosen frequency via the
configuration file(default:5 seconds)
configuration file(default:5 seconds) You can receive email alerts when IP addresses are blocked.
Control blocking by connection state(see man netstat).
Auto-detection of firewall.
Support for APF,CSF,ipfw,and iptables.
Logs events to/var/log/ddos.log
Uses tcpkill to reduce the amount of processes opened by attackers.
DDoS Deflate 的下载和安装
安装
必须在root权限下的终端账户里下载和安装,具体命令如下:
wget wget https://github.com/jgmdev/ddos-deflate/archive/master.zip -O ddos�6�7�6�7.zip unzip ddos.zip cd ddos-deflate-master ./install.sh
卸载
同样是root权限账户下执行如下命令来卸载 DDoS Deflate 。
cd ddos-deflate-master ./uninstall.sh
DDoS Deflate 的使用
安装程序会自动检测系统是否支持�0�2 init.d �0�2脚本,当支持的话将安装文件和启动脚本的�0�2 DDoS apropiate �0�2。在初始化的情况。 DDoS Deflate 开始作为一个守护进程,它监测间隔设置为5秒默认。
你也可以手动配置 DDoS Deflate 的各项参数,配置文件如下:
/etc/ddos/ignore.host.list
在这个文件里可以添加主机列表为白名单,一般都是搜索引擎(当然你可以根据自己的情况来加入允许的主机到这个白名单),示例如下:
googlebot.com
my-dynamic-ip.somehost.com
baidu.com
sogou.com
so.com
bing.com
/etc/ddos/ignore.ip.list
还可以使用上面的文件来加入IP地址白名单,这个主要是比较适用 CDN 节点IP的加入。示例如下:
12.43.63.13
129.134.131.2
/etc/ddos/ddos.conf
这个是 DDoS Deflate 的主配置文件,大家可以参考阿里云社区里的中文注释配置文件,如下:
##### 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” //白名单 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 �Ccron ##### option so that the new frequency takes effect FREQ=1 //ddos.sh执行的频率,单位是分钟,搭配crontab来执行. 如果你更改了该设置,请执行 /usr/local/ddos/ddos.sh �Ccron ―――――――――――――――――――――――――- ##### How many connections define a bad IP? Indicate that below. NO_OF_CONNECTIONS=150 //当哪个IP的连接数达到150就加入黑名单;查看连接数你可以执行 #netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n ―――――――――――――――――――――――――- ##### 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=0 //一般情况下你是使用iptables来做防火墙,所以这里你需要将 APF_BAN的值改为0. ―――――――――――――――――――――――――- ##### KILL=0 (Bad IPs are’nt banned, good for interactive execution of script) ##### KILL=1 (Recommended setting) KILL=1 //将非法的的IP加入iptables的INPUT链中,DROP之. ―――――――――――――――――――――――――- ##### An email is sent to the following address when an IP is banned. ##### Blank would suppress sending of mails EMAIL_TO=”root” //给root发一封邮件 ―――――――――――――――――――――――――- ##### Number of seconds the banned ip should remain in blacklist. BAN_PERIOD=600 //屏蔽黑名单中的IP的时长
在修改了上面的注配置文件后,记得要重启一下 DDoS Deflate 进程以便配置生效,命令如下:
systemctl restart ddos
也可以使用 init.d 或者 service 的方式重启
/etc/init.d/ddos restart
or
service ddos restart
在终端命令行里输入: ddos �0�2既可调出 ddos 各个参数的用法提示,如下图:
2.png
这些都是很简单的技术英语,大家可以自己试试加上各个参数的效果。整体上来说 DDoS Deflate 还是一个非常方便的防御脚本程序的,设定好检测频率和屏蔽IP的连接数量以及屏蔽IP时间长短后结合有防护能力的 CDN 服务器,只要服务器真实IP没有泄露的话,可以防御常见的CC/DDOS攻击的,特别是脑残手贱小白一类的攻击。明月建议各个站长们都尽量在服务器上保留着 DDoS Deflate ,到用的时候只需要修改IP连接数量就可以开启屏蔽防御了,还是很不错的,至少明月曾用 DDoS Deflate 抵挡了近一周的CC攻击呢(最后,还是因为泄露了真实IP,还是被攻破沦陷了,��!!!)
就到这里了,如果你在使用 DDoS Deflate 时有什么不明白的,可以在本文最后留言给明月,明月会及时回复你的,也欢迎各个站长们在评论里交流使用心得哦!
如果总是会收到以下类似的邮件提示的话
Banned the following ip addresses on Tue Aug 5 01:32:01 BST 2008 1120 with 1120 connections
请查看 /usr/local/ddos/ddos.sh 文件的第117行
netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -nr > $BAD_IP_LIST
修改为以下代码即可!
netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sed -n ‘/[0-9]/p’ | sort | uniq -c | sort -nr > $BAD_IP_LIST
上述好像新版(0.9版�0�2 GitHub专页) DDoS Deflate 已经不需要了,仅供大家参考了。
最后为了不误导大家,明月要强调一下,目前防御�0�2 CC/DDos �0�2攻击最有效的依然是“硬防”设备,所有软防都是近乎“摆设”的存在,我曾经举过一个例子来阐述了这点:
软防类似于给服务器加了一个智能安全锁,对于“撬门“这类还是有一些效果的,但大流量的�0�2 CC/DDos �0�2攻击就类似于对方开着重装甲“坦克”直接推到房子,所以软防是毫无效果的,只有“硬防”(准备N多重装甲坦克防护)才可以阻挡的,但“硬防”的成本不是个人、小企业可以承担的。
至于说网上各类标榜“硬防”的主机,我只能说是为了忽悠你兜里的钱的,仅此而已。有用的真心很少!!!大家不要轻信这类推广就可以了。
其实防御�0�2 CC/DDos �0�2攻击的最好办法就是隐藏好自己,大家可以参考【互联网世界里的“黑暗森林法则”】一文,我专门详述过这个。
�0�2