转截请写明出处,谢谢!
最近老同事的电子商务网站经常性的对手ab,无奈我花些时间google,发现有不少朋友用mod_dosevasive来对抗ddos攻击(当然是小范围的,如果没有带宽和硬件的保障效果会更好).
居体的安装可以参看
关于Apache (httpd)服务器防DDOS模块mod_evasive的使用说明
上面写的够详细了。
下面是我的安装过程
1.安装apxs 即要有 apache开发版本的支持
#yum install httpd-devel
2.进入mod_evasive目录
查看apxs在哪里
#whereis apxs /* /usr/bin/apxs*/
#/usr/bin/apxs -i -a -c mod_dosevasive20.c
执行上面的命令后系统会在/etc/httpd/conf/httpd.conf中加入下面一行
LoadModule evasive20_module /usr/lib/httpd/modules/mod_evasive20.so
3.加入在被DDOS时的处理操作
手工在/etc/httpd/conf/httpd.conf最下方加入下面内容
<IfModule mod_evasive20.c>
DOSHashTableSize 3097
DOSPageCount 2
DOSSiteCount 50
DOSPageInterval 1
DOSSiteInterval 1
DOSBlockingPeriod 10
DOSEmailNotify [email protected]
DOSSystemCommand "sudo iptables -A INPUT -s %s -p tcp --dport 80 -j DROP"
DOSLogDir "/tmp/lock/mod_evasive"
</IfModule>
针对上面的配置在mod_dosevasive目录下面的REAME中有详细说明。
做了上面的配置之后记得要重启apache
4.重启apache
#service httpd restart
4.测试
我用压力测试工具ab
#ab -n 1000 -c 100 http://192.168.1.254/index.php
5.查看是否本机IP(192.168.1.100)是否被封
#iptables -nvL
显示:
Chain INPUT (policy ACCEPT 64628 packets, 6176K bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 49010 packets, 10M bytes) pkts bytes target prot opt in out source destination
结果显示并没有封着我的本机IP
分析原因是apache用户执行sudo处没有执行,因为我把
DOSSystemCommand "sudo iptables -A INPUT -s %s -p tcp --dport 80 -j DROP"
改成了
DOSSystemCommand "/bin/touch /tmp/%s.log"
重启apache没有问题故可以判断是sudo执行iptables的问题。因iptables命令是一定要root用户才能执行的。
中间我还把相关的命令写在了一个shell(/tmp/do.sh)中,并且把do.sh的权限改为4755(该权限会改变文件执行的属主为root)
do.sh
#!/bin/sh #相关的参数判断自己来吧 /sbin/iptables -A INPUT -s $1 -p tcp --dport 80 -j DROP
重启apache再压力测试。用iptables -nvL查看还是没有看到策略中有被封的IP
针对上面的测试问题就在定位到了sudo 下面就是google.
最终找到相关的答案
简单的步骤就是
#visudo
加入下面的内容
apache ALL=NOPASSWD: /sbin/iptables
然后用sudo -l可以查看apache用户是否成功加入可以执行sudo的行列
重启apache->压力测试->查看防火墙策略->结果依旧(失败)查看apache的error日志显示
sudo: sorry, you must have a tty to run sudo
意思就是apache运行sudo要求是终端模式,在做web服务器用sudo的时候肯定是不能用终端了。
经查看visudo的配置和google的结果显示 我们需要将visudo中的
Defaults requiretty
注解掉
#Defaults requiretty
最终重复测试流程在
#iptables -nvL的命令下看到了被封堵的记录
Chain INPUT (policy ACCEPT 64543 packets, 6168K bytes) pkts bytes target prot opt in out source destination 836 39828 DROP tcp -- * * 192.168.1.100 0.0.0.0/0 tcp dpt:80 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 48945 packets, 10M bytes) pkts bytes target prot opt in out source destination