1、 mod_evasive 介绍;
mod_evasive 是Apache(httpd)服务器的防DDOS的一个模块。对于WEB服务器来说,是目前比较好的一个防护DDOS攻击的扩展模块。虽然并不能完全防御 DDOS攻击,但在一定条件下,还是起到缓服Apache(httpd)服务器的压力。如果配合iptables、硬件防火墙等防火墙设备配合使用,可能 有更好的效果。
mod_evasive 的官方地址:http://www.zdziarski.com/blog/wp-content/uploads/2010/02/mod_evasive_1.10.1.tar.gz
2、 mod_evasive 的安装和配置;
因为现在基本上都用apache2.X的版本了,就不介绍apache1.X版本的安装方法了。
安装 mod_evasive 之前,你要用安装Apache(httpd)服务器软件包,还要安装httpd-devel或 apache-dev。在Slackware 12.0中,安装httpd软件即可;
对于Apache 2.x 可以用下面的办法;
- #tar zxvf mod_evasive_1.10.1.tar.gz
- #cd mod_evasive
- #/usr/sbin/apxs -i -a -c mod_evasive20.c
注:apxs 用于编译模块工具;如果是用系统自带的软件包,一般位于/usr/sbin目录。如果您是自己编译安装Apache(httpd)的,你应该自己来指定路径;
便已完成后会提示这个模块的位置等一些信息,如下:
- Libraries have been installed in:
- /usr/lib64/httpd/modules
- If you ever happen to want to link against installed libraries
- in a given directory, LIBDIR, you must either use libtool, and
- specify the full pathname of the library, or use the `-LLIBDIR'
- flag during linking and do at least one of the following:
- - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
- during execution
- - add LIBDIR to the `LD_RUN_PATH' environment variable
- during linking
- - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- - have your system administrator add LIBDIR to `/etc/ld.so.conf'
- See any operating system documentation about shared libraries for
- more information, such as the ld(1) and ld.so(8) manual pages.
- ----------------------------------------------------------------------
- chmod 755 /usr/lib64/httpd/modules/mod_evasive20.so
- [activating module `evasive20' in /etc/httpd/conf/httpd.conf]
我们然后修改/etc/ld.so.conf 文件,把编译出来的动态模块的所在位置指定在 ld.so.conf中;比如我用的是Aapche 2.x ,编译完成后,模块mod_evasive20.so 安装到了 /usr/lib64/httpd/modules 目录中;那我们就要把这个目录写入到ld.so.conf中。
- #echo "/usr/lib64/httpd/modules" >> /etc/ld.so.conf
2.3 mod_evasive 的配置;
在编译安装完成后,会自动插入一行到Apache 配置文件中,对于Apache 2.x 版本中,应该在其配置文件中有类似下面的一行;
LoadModule evasive20_module lib/httpd/modules/mod_evasive20.so
然后我们再修改 Apache 的配置文件,配置文件名为httpd.conf;
- <IfModule mod_evasive20.c>
- DOSHashTableSize 3097 //记录和存放黑名单的哈西表大小,如果服务器访问量很大,可以加大该值
- DOSPageCount 2 //同一个页面在同一时间内可以被统一个用户访问的次数,超过该数字就会被列为攻击,同一时间的数值可以在DosPageInterval参数中设置。
- DOSSiteCount 50 //同一个用户在同一个网站内可以同时打开的访问数,同一个时间的数值在DOSSiteInterval中设置。
- DOSPageInterval 1 //设置DOSPageCount中时间长度标准,默认值为1。
- DOSSiteInterval 1 //设置DOSSiteCount中时间长度标准, 默认值为1。
- DOSBlockingPeriod 10 //被封时间间隔秒,这中间会收到 403 (Forbidden) 的返回。
- </IfModule>
修改完成后,我们要重启Apache服务器;
2.4 对mod_evasive测试验证 ;
防DDOS的模块做好后,我们可以要验证,可以用Apache 自带的ab工具,系统默认安装在/usr/sbin目录中;比如;
#/usr/sbin/ab -n 1000 -c 50 http://127.0.0.1/
发送数据请求包,总共1000个,每次并发50个;
另外一个测试工具就是mod_evasive的解压包的目录中,有个test.pl ,你可以修改IP地址,然后用
#perl test.pl
是不是有效果,请根据 ab工具或 测试脚本出来的结果来查看;
因为我们编译mod_evasive时,用的是默认配置,所以日志被存放在/tmp目录中。如果有DDOS攻击,会在/tmp产生日志。日志的文件是以 dos-开头的;
3、mod_evasive 的高级配置;
如果想更改一些适合自己的参数,有些必要的参数,并不是通过配置文件修改就一下起作用的,我们要修改源码包中的mod_evasive20.c
- #define DEFAULT_HASH_TBL_SIZE 3097ul // Default hash table size
- #define DEFAULT_PAGE_COUNT 2 // Default maximum page hit count per interval
- #define DEFAULT_SITE_COUNT 50 // Default maximum site hit count per interval
- #define DEFAULT_PAGE_INTERVAL 1 // Default 1 Second page interval
- #define DEFAULT_SITE_INTERVAL 1 // Default 1 Second site interval
- #define DEFAULT_BLOCKING_PERIOD 10 // Default for Detected IPs; blocked for 10 seconds
- #define DEFAULT_LOG_DIR "/tmp" // Default temp directory
比如我们改改其中的数字,根据英文很容易理解。比如修改日志存放目录,就把/tmp改成别的目录。如果您不知道放在哪好,还是用默认的吧;
如果您在这里更改了参数,不要忘记修改Apache 配置文件中关于mod_evasive 的参数