apache -- 安装mod_evasive模块来防御ddos攻击

原文:http://www.mydigitallife.info/2007/08/15/install-mod_evasive-for-apache-to-prevent-ddos-attacks/

 

 

mod_evasive, formerly known as mod_dosevasive is a Apache module that provides evasive maneuvers action in the event of an HTTP DoS or DDoS (Denial of Service) attack or brute force attack at the web server. When possible attacks are detected, mod_evasive will block the traffic from the source for a specific duration of time, while reports abuses via email and syslog facilities. Or administrators can configure mod_evasive to talk to iptables, ipchains, firewalls, routers , and etc. to build a comprehensive DDOS prevention system for the high traffic busy web server.

Although mod_evasive is not a foolproof and complete DOS prevention system, but installing mod_evasive module for Apache will likely to reduce and stop certain DDOS attacks, minimizing the risks of web hosts and web sites been completely brought down inaccessible by malicious denial of service attack attempts.

How to Install mod_evasive

  1. Login to web server via SSH.
  2. For Apache 2.0.x, execute the following command:

    up2date -i httpd-devel

  3. Continue with the following commands one by one for all version of Apache HTTPD server. wget command will download the current stable version 1.10.1 source tarball.

    cd /usr/local/src
    wget http://www.zdziarski.com/projects/mod_evasive/mod_evasive_1.10.1.tar.gz
    tar -zxvf mod_evasive_1.10.1.tar.gz
    cd mod_evasive

  4. For Apache 2.0.x , execute the following command:

    /usr/sbin/apxs -cia mod_evasive20.c

    Else, for Apache 1.3.x,

    /usr/local/apache/bin/apxs -cia mod_evasive.c

    Above commands will compile mod_evasive to .so and subsequently add corrensponding AddModule and LoadModule lines into httpd.conf.

  5. mod_evasive comes with default configuration value preset, however, if webmasters want to configure and set the value themselves, the following parameters have to be added into httpd.conf Apache configuration file below the AddModule section.

    For Apache 2.0.x, add the following text to httpd.conf below AddModule section:

    <IfModule mod_evasive20.c>
    DOSHashTableSize 3097
    DOSPageCount 5
    DOSSiteCount 100
    DOSPageInterval 1
    DOSSiteInterval 1
    DOSBlockingPeriod 600
    </IfModule>

    For apache 1.3.x, add the following text to httpd.conf below AddModule section:

    <IfModule mod_evasive.c>
    DOSHashTableSize 3097
    DOSPageCount 5
    DOSSiteCount 100
    DOSPageInterval 1
    DOSSiteInterval 1
    DOSBlockingPeriod 600
    </IfModule>

    Save and exit the httpd.conf Apache configuration file.

  6. Restart the Apache server with the following command:

    /etc/init.d/httpd restart

Note: If apxs is not found, it can be installed via “yum install httpd-devel” command.

Installation is completed. Note that mod_evasive has known issues with FrontPage Server Extensions. Administrator can configure the variables such as enlarging the DOSHashTableSize especially for busy server. But note that whenever when a sournce of attack is blocked, the blocking duration is automatically extended whenever the source attempts to connect again, thus the DOSBlockingPeriod needs not to be too long. Beside, the blocking is based on each sessions of Apache child process, thus the blocking has the lifespan of that particular session only. If webmaster set the maximum clients per process to a very low value, the blocking may not be very effective. All definitions of mod_evasive directives can be found on README file comes with the source codes.

Other than above common configuration parameters, mod_evasive also supports the following three advanced directives:

DOSEmailNotify users@example.com
DOSSystemCommand “su – someuser -c ‘/sbin/… %s …’”
DOSLogDir “/var/lock/mod_evasive”

The DOSEmailNotify is particular useful, where you can set mod_evasive to send a notification email whenever a possible DOS attack is detected and blocked. For example, “DOSEmailNotify root” will send the email to root user. But note that mailer configuration (by default is “/bin/mail -t %s”) in mod_evasive.c or mod_evasive20.c is correct. You can create a symbolic link if needed to or modify the source code file.

 

 

 

[root@LSN-D3219 mod_evasive]# /usr/local/apache2/bin/apxs
Usage: apxs -g [-S <var>=<val>] -n <modname>
       apxs -q [-S <var>=<val>] <query> ...
       apxs -c [-S <var>=<val>] [-o <dsofile>] [-D <name>[=<value>]]
               [-I <incdir>] [-L <libdir>] [-l <libname>] [-Wc,<flags>]
               [-Wl,<flags>] [-p] <files> ...
       apxs -i [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...
       apxs -e [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...
[root@LSN-D3219 mod_evasive]# /usr/local/apache2/bin/apxs -cia mod_evasive
mod_evasive20.c     mod_evasive.c       mod_evasiveNSAPI.c
[root@LSN-D3219 mod_evasive]# /usr/local/apache2/bin/apxs -cia mod_evasive20.c
/usr/local/apache2/build/libtool --silent --mode=compile gcc -prefer-pic   -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -g -O2 -pthread -I/usr/local/apache2/include  -I/usr/local/apache2/include   -I/usr/local/apache2/include   -c -o mod_evasive20.lo mod_evasive20.c && touch mod_evasive20.slo
/usr/local/apache2/build/libtool --silent --mode=link gcc -o mod_evasive20.la  -rpath /usr/local/apache2/modules -module -avoid-version    mod_evasive20.lo
/usr/local/apache2/build/instdso.sh SH_LIBTOOL='/usr/local/apache2/build/libtool' mod_evasive20.la /usr/local/apache2/modules
/usr/local/apache2/build/libtool --mode=install cp mod_evasive20.la /usr/local/apache2/modules/
cp .libs/mod_evasive20.so /usr/local/apache2/modules/mod_evasive20.so
cp .libs/mod_evasive20.lai /usr/local/apache2/modules/mod_evasive20.la
cp .libs/mod_evasive20.a /usr/local/apache2/modules/mod_evasive20.a
chmod 644 /usr/local/apache2/modules/mod_evasive20.a
ranlib /usr/local/apache2/modules/mod_evasive20.a
PATH="$PATH:/sbin" ldconfig -n /usr/local/apache2/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/local/apache2/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/local/apache2/modules/mod_evasive20.so
[activating module `evasive20' in /usr/local/apache2/conf/httpd.conf]

安装方法: 1、下载附件中的压缩包,解压并拷贝mod_dosevasive22.dll到Apache安装目录下的modules目录(当然也可以是其他目录,需要自己修改路径)。 2、修改Apache的配置文件http.conf。 添加以下内容 LoadModule dosevasive22_module modules/mod_dosevasive22.so DOSHashTableSize 3097 DOSPageCount 3 DOSSiteCount 50 DOSPageInterval 1 DOSSiteInterval 1 DOSBlockingPeriod 10 其中DOSHashTableSize 3097 记录黑名单的尺寸 DOSPageCount 3 每个页面被判断为dos攻击的读取次数 DOSSiteCount 50 每个站点被判断为dos攻击的读取部件(object)的个数 DOSPageInterval 1 读取页面间隔秒 DOSSiteInterval 1 读取站点间隔秒 DOSBlockingPeriod 10 被封时间间隔秒 mod_dosevasive v1.10 什么是mod_dosevasive? mod_dosevasive是一种提供躲避 HTTP DOS/DDOS攻击或暴力强制攻击的apache模块。它同样可以用作网络探测和管理的工具,通过简单的配置,就可以同ipchains(ip链?)防 火墙,路由器等设备进行对话。并通过email或系统日志提供报告。 发现攻击是通过创建一个内建的IP地址和URIs的动态哈希表来完成,并且阻止同一ip在以下的情况: 1.在同一秒多次请求同一页面 2.对同一child(对象?)作出超过50个并发请求 3.被列入黑名单的ip 这种方式在单点攻击和分布式多点攻击的状况下都能很好工作,但如同其它的防黑软件一样,只是针对于那些对网络带宽和处理器消耗的攻击,所以这就是为什么我们要推荐你将它与你的防火墙和路由器配合使用,因为这样才能提供最大限度的保护。 这 个模块有一个内建的滤除机制和级别设定,对付不同情况,正因如此合法请求不会受到妨碍,即使一个用户数次连击“刷新”,也不会受到影响,除非,他是故意这 样做的。mod_dosevasive完全可以通过apache配置文件来配置,很容易就可以集成到你的web服务器,并且容易使用。 DOSHashTableSize ---------------- 哈希表的大小决定每个子级哈希表的顶级节点数,越多则越可避免反复的查表,但会占据更多内存,如果你的服务器要应付很多访问,那就增大它。The value you specify will automatically be tiered up to the next prime number in the primes list (see mod_dosevasive.c for a list of primes used). DOSPageCount ------------ 规定请求同一页面(URI)的时间间隔犯规的次数,一旦超过,用户ip将被列入黑名单 DOSSiteCount ------------ 规定请求站内同一物件的时间间隔犯规的次数,一旦超过,用户ip将被列入黑名单 DOSPageInterval --------------- 同一页面的规定间隔时间,默认为1秒 DOSSiteInterval --------------- 站内同一物件的时间间隔,默认为1秒 DOSBlockingPeriod ----------------- The blocking period 是规定列入黑名单内ip的禁止时限,在时限内,用户继续访问将收到403 (Forbidden)的错误提示,并且计时器将重置。由于列入黑名单后每次访问都会重新计时,所以不必将时限设置太大。在Dos攻击下,计时器也会保持 重置 DOSEmailNotify -------------- 假如这个选项被设置,每个ip被列入黑名单时,都将发送email通知。但有机制防止重复发送相同的通知 注意:请确定mod_dosevasive.c (or mod_dosevasive20.c)已正确配置。 默认配置是 "/bin/mail -t %s" %s 是email发送的目的地址,假如你是linux或其它使用别的邮箱的操作系统,你需要修改这里 DOSSystemCommand ---------------- 假如设置了此项,当有ip被列入黑名单,指定的系统命令将被执行,此项功能被设计为受攻击时可以执行ip过滤器和其它的工具软件,有内建机制避免对相同攻击作重复反应 用 %s 表示黑名单中的ip DOSLogDir --------- 系统日志目录 默认"/tmp"目录用作保密机制,仅向一些特权级用户开放 (这里有些不懂,可能翻错了) By default "/tmp" will be used for locking mechanism, which opens some security issues if your system is open to shell users. 在没有特权shell用户时,你应该为apache服务器用户(通常为root)创建一个只可写的文件夹,并在httpd.conf里配置访问权限 WHITELISTING IP ADDRESSES 在1.8版中提供信任ip机制 进入信任ip名单的ip它们将不会受到阻止,次功能主要是用于保护软件,scripts,本地搜索,或其它的自动工具对服务器进行大量的资源请求时不会被阻止 请确认而不是用在添加客户或其它什么类别的人员,因为这将给本模块进行正常工作带来阻碍 信任ip名单应在apache配置文件中添加如下格式的字段(可以是ip段) DOSWhitelist 127.0.0.1 DOSWhitelist 127.0.0.* ip地址后三段可以使用通配符,并且可以各格式复用 TWEAKING APACHE keep-alive项是确保能阻止攻击(至少其中一部分攻击). 但在阻止攻击,发送403页面前,5-10 个页面还是会通过阻止而发送 . 所以你必须设置足够高的MaxRequestsPerChild, 但不是无限,是一个非零的数,默认的MaxRequestsPerChild设定是10000. 这还是将允许少量的攻击进入,但如果你配合防火强使用,将天衣无缝 TESTING 运行 test.pl来测试是否安装成功,如果不是localhost的话,修改其中的服务器地址字段,成功的话在25-50个(取决于你的配置)请求后你将收到403 forbidden回复。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值