Linux apache mod evasive module howto

WHAT IS MOD_EVASIVE ?

mod_evasive is an evasive maneuvers module for Apache to provide evasive
action in the event of an HTTP DoS or DDoS attack or brute force attack. It
is also designed to be a detection tool, and can be easily configured to talk
to ipchains, firewalls, routers, and etcetera.

Detection is performed by creating an internal dynamic hash table of IP
Addresses and URIs, and denying any single IP address from any of the following:

- Requesting the same page more than a few times per second
- Making more than 50 concurrent requests on the same child per second
- Making any requests while temporarily blacklisted (on a blocking list)

This method has worked well in both single-server script attacks as well
as distributed attacks, but just like other evasive tools, is only as
useful to the point of bandwidth and processor consumption (e.g. the
amount of bandwidth and processor required to receive/process/respond
to invalid requests), which is why it’s a good idea to integrate this
with your firewalls and routers.

This module instantiates for each listener individually, and therefore has
a built-in cleanup mechanism and scaling capabilities. Because of this,
legitimate requests are rarely ever compromised, only legitimate attacks. Even
a user repeatedly clicking on ‘reload’ should not be affected unless they do
it maliciously.

Three different module sources have been provided:

Apache v1.3 API:mod_evasive.c
Apache v2.0 API:mod_evasive20.c

HOW IT WORKS

A web hit request comes in. The following steps take place:

- The IP address of the requestor is looked up on the temporary blacklist
- The IP address of the requestor and the URI are both hashed into a “key”.
A lookup is performed in the listener’s internal hash table to determine
if the same host has requested this page more than once within the past
1 second.
- The IP address of the requestor is hashed into a “key”.
A lookup is performed in the listerner’s internal hash table to determine
if the same host has requested more than 50 objects within the past
second (from the same child).

If any of the above are true, a 403 response is sent. This conserves
bandwidth and system resources in the event of a DoS attack. Additionally,
a system command and/or an email notification can also be triggered to block
all the originating addresses of a DDoS attack.

Once a single 403 incident occurs, mod_evasive now blocks the entire IP
address for a period of 10 seconds (configurable). If the host requests a
page within this period, it is forced to wait even longer. Since this is
triggered from requesting the same URL multiple times per second, this
again does not affect legitimate users.

The blacklist can/should be configured to talk to your network’s firewalls
and/or routers to push the attack out to the front lines, but this is not
required.

mod_evasive also performs syslog reporting using daemon.alert. Messages
will look like this:

Aug 6 17:41:49 elijah mod_evasive[23184]: [ID 801097 daemon.alert] Blacklisting
address x.x.x.x: possible attack.

WHAT IS THIS TOOL USEFUL FOR?

This tool is *excellent* at fending off request-based DoS attacks or scripted
attacks, and brute force attacks. When integrated with firewalls or IP filters,
mod_evasive can stand up to even large attacks. Its features will prevent you
from wasting bandwidth or having a few thousand CGI scripts running as a
result of an attack.

If you do not have an infrastructure capable of fending off any other types
of DoS attacks, chances are this tool will only help you to the point of
your total bandwidth or server capacity for sending 403′s. Without a solid
infrastructure and address filtering tool in place, a heavy distributed DoS
will most likely still take you offline.

Howto Install on Apache 2.X

[root@station100 ~]# rpm -q httpd
httpd-2.2.3-31.el5

First download it from internet or given location.

[root@station100 ~]# wget http://www.zdziarski.com/blog/wp-content/uploads/2010/02/mod_evasive_1.10.1.tar.gz

[root@station100 ~]# cp mod_evasive_1.10.1.tar.gz /tmp/

[root@station100 ~]# cd /tmp/

[root@station100 tmp]# tar -zxf mod_evasive_1.10.1.tar.gz

[root@station100 tmp]# cd mod_evasive

[root@station100 mod_evasive]# ls
CHANGELOG Makefile.tmpl mod_evasive.c README
LICENSE mod_evasive20.c mod_evasiveNSAPI.c test.pl

[root@station100 mod_evasive]# ls
CHANGELOG Makefile.tmpl mod_evasive.c README
LICENSE mod_evasive20.c mod_evasiveNSAPI.c test.pl

[root@station100 mod_evasive]# locate apxs
/usr/sbin/apxs
/usr/share/man/man8/apxs.8.gz
/var/www/manual/programs/apxs.html
/var_old/www/manual/programs/apxs.html

[root@station100 mod_evasive]# /usr/sbin/apxs -i -a -c mod_evasive20.c
/usr/lib/apr-1/build/libtool –silent –mode=compile gcc -prefer-pic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2
-fexceptions -fstack-protector –param=ssp-buffer-size=4 -m32 -march=i386
-mtune=generic -fasynchronous-unwind-tables -fno-strict-aliasing
-DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -pthread
-I/usr/include/httpd -I/usr/include/apr-1 -I/usr/include/apr-1 -c -o mod_evasive20.lo mod_evasive20.c && touch mod_evasive20.slo
mod_evasive20.c: In function ‘access_checker’:
mod_evasive20.c:212: warning: implicit declaration of function ‘getpid’
mod_evasive20.c:212: warning: format ‘%ld’ expects type ‘long int’, but argument 4 has type ‘int’
mod_evasive20.c:229: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result
mod_evasive20.c: In function ‘destroy_hit_list’:
mod_evasive20.c:301: warning: control reaches end of non-void function
mod_evasive20.c: In function ‘create_hit_list’:
mod_evasive20.c:118: warning: control reaches end of non-void function
/usr/lib/apr-1/build/libtool –silent –mode=link gcc -o mod_evasive20.la -rpath /usr/lib/httpd/modules -module -avoid-version mod_evasive20.lo
/usr/lib/httpd/build/instdso.sh SH_LIBTOOL=’/usr/lib/apr-1/build/libtool’ mod_evasive20.la /usr/lib/httpd/modules
/usr/lib/apr-1/build/libtool –mode=install cp mod_evasive20.la /usr/lib/httpd/modules/
cp .libs/mod_evasive20.so /usr/lib/httpd/modules/mod_evasive20.so
cp .libs/mod_evasive20.lai /usr/lib/httpd/modules/mod_evasive20.la
cp .libs/mod_evasive20.a /usr/lib/httpd/modules/mod_evasive20.a
chmod 644 /usr/lib/httpd/modules/mod_evasive20.a
ranlib /usr/lib/httpd/modules/mod_evasive20.a
PATH=”$PATH:/sbin” ldconfig -n /usr/lib/httpd/modules
———————————————————————-
Libraries have been installed in:
/usr/lib/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/lib/httpd/modules/mod_evasive20.so
[activating module `evasive20' in /etc/httpd/conf/httpd.conf]
[root@station100 mod_evasive]#

[root@station100 mod_evasive]# ls /usr/lib/httpd/modules/
libmodnss.so mod_cgid.so mod_logio.so
libphp5.so mod_cgi.so mod_mem_cache.so
mod_actions.so mod_dav_fs.so mod_mime_magic.so
mod_alias.so mod_dav.so mod_mime.so
mod_asis.so mod_dbd.so mod_negotiation.so
mod_auth_basic.so mod_deflate.so mod_perl.so
mod_auth_digest.so mod_dir.so mod_proxy_ajp.so
mod_authn_alias.so mod_disk_cache.so mod_proxy_balancer.so
mod_authn_anon.so mod_dumpio.so mod_proxy_connect.so
mod_authn_dbd.so mod_env.so mod_proxy_ftp.so
mod_authn_dbm.so mod_evasive20.so mod_proxy_http.so
mod_authn_default.so mod_expires.so mod_proxy.so
mod_authn_file.so mod_ext_filter.so mod_rewrite.so
mod_authnz_ldap.so mod_file_cache.so mod_setenvif.so
mod_authz_dbm.so mod_filter.so mod_speling.so
mod_authz_default.so mod_headers.so mod_ssl.so
mod_authz_groupfile.so mod_ident.so mod_status.so
mod_authz_host.so mod_imagemap.so mod_suexec.so
mod_authz_owner.so mod_include.so mod_unique_id.so
mod_authz_user.so mod_info.so mod_userdir.so
mod_autoindex.so mod_ldap.so mod_usertrack.so
mod_cache.so mod_log_config.so mod_version.so
mod_cern_meta.so mod_log_forensic.so mod_vhost_alias.so
[root@station100 mod_evasive]#

CONFIGURATION

mod_evasive has default options configured, but you may also add the
following block to your httpd.conf:

APACHE v1.3
———–

<ifmodule mod_evasive.c=”"> DOSHashTableSize 3097 DOSPageCount 2 DOSSiteCount 50 DOSPageInterval 1 DOSSiteInterval 1 DOSBlockingPeriod 10</ifmodule>

APACHE v2.0
———–
<ifmodule mod_evasive20.c=”"> DOSHashTableSize 3097 DOSPageCount 2 DOSSiteCount 50 DOSPageInterval 1 DOSSiteInterval 1 DOSBlockingPeriod 10</ifmodule>

DOSHashTableSize
—————-

The hash table size defines the number of top-level nodes for each child’s
hash table. Increasing this number will provide faster performance by
decreasing the number of iterations required to get to the record, but
consume more memory for table space. You should increase this if you have
a busy web server. The value you specify will automatically be tiered up to
the next prime number in the primes list (see mod_evasive.c for a list
of primes used).

DOSPageCount
————

This is the threshhold for the number of requests for the same page (or URI)
per page interval. Once the threshhold for that interval has been exceeded,
the IP address of the client will be added to the blocking list.

DOSSiteCount
————

This is the threshhold for the total number of requests for any object by
the same client on the same listener per site interval. Once the threshhold
for that interval has been exceeded, the IP address of the client will be added
to the blocking list.

DOSPageInterval
—————

The interval for the page count threshhold; defaults to 1 second intervals.

DOSSiteInterval
—————

The interval for the site count threshhold; defaults to 1 second intervals.

DOSBlockingPeriod
—————–

The blocking period is the amount of time (in seconds) that a client will be
blocked for if they are added to the blocking list. During this time, all
subsequent requests from the client will result in a 403 (Forbidden) and
the timer being reset (e.g. another 10 seconds). Since the timer is reset
for every subsequent request, it is not necessary to have a long blocking
period; in the event of a DoS attack, this timer will keep getting reset.

DOSEmailNotify
————–

If this value is set, an email will be sent to the address specified
whenever an IP address becomes blacklisted. A locking mechanism using /tmp
prevents continuous emails from being sent.

NOTE: Be sure MAILER is set correctly in mod_evasive.c
(or mod_evasive20.c). The default is “/bin/mail -t %s” where %s is
used to denote the destination email address set in the configuration.
If you are running on linux or some other operating system with a
different type of mailer, you’ll need to change this.

DOSSystemCommand
—————-

If this value is set, the system command specified will be executed
whenever an IP address becomes blacklisted. This is designed to enable
system calls to ip filter or other tools. A locking mechanism using /tmp
prevents continuous system calls. Use %s to denote the IP address of the
blacklisted IP.

DOSLogDir
———

Choose an alternative temp directory

By default “/tmp” will be used for locking mechanism, which opens some
security issues if your system is open to shell users.

http://security.lss.hr/index.php?page=details&ID=LSS-2005-01-01

In the event you have nonprivileged shell users, you’ll want to create a
directory writable only to the user Apache is running as (usually root),
then set this in your httpd.conf.

WHITELISTING IP ADDRESSES

IP addresses of trusted clients can be whitelisted to insure they are never
denied. The purpose of whitelisting is to protect software, scripts, local
searchbots, or other automated tools from being denied for requesting large
amounts of data from the server. Whitelisting should *not* be used to add
customer lists or anything of the sort, as this will open the server to abuse.
This module is very difficult to trigger without performing some type of
malicious attack, and for that reason it is more appropriate to allow the
module to decide on its own whether or not an individual customer should be
blocked.

To whitelist an address (or range) add an entry to the Apache configuration
in the following fashion:

DOSWhitelist127.0.0.1
DOSWhitelist127.0.0.*

Wildcards can be used on up to the last 3 octets if necessary. Multiple
DOSWhitelist commands may be used in the configuration.


So we will add our network as whitelist ip

#our network is 192.168.2.0/24
DOSWhitelist127.0.0.1
DOSWhitelist192.168.2.*

So finally our entry in httpd.conf is

#mod_evasive20 configuration start here——-
<ifmodule mod_evasive20.c=”"> DOSHashTableSize 3097 DOSPageCount 2 DOSSiteCount 50 DOSPageInterval 1 DOSSiteInterval 1 DOSBlockingPeriod 10</ifmodule>

#our network is 192.168.2.0/24
DOSWhitelist 127.0.0.1
DOSWhitelist 192.168.2.*

#mod_evasive20 configuration end here——–

Let us restart the service

[root@station100 ~]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@station100 ~]#

Before running final test …

[root@station100 mod_evasive]# ls
apachemod_evasive Makefile.tmpl mod_evasive20.o README
apachemod_evasive~ mod_evasive20.c mod_evasive20.slo README~
CHANGELOG mod_evasive20.la mod_evasive.c test.pl
LICENSE mod_evasive20.lo mod_evasiveNSAPI.c

As we had created white list for our network 192.168.2.0/24 we need to use
other computer not added in whitelist.

[root@station100 mod_evasive]# scp test.pl 192.168.0.225:/root
‘s password:
test.pl 100% 406 0.4KB/s 00:00

[root@station100 mod_evasive]#

[root@station100 mod_evasive]# ssh 192.168.0.225
‘s password:
Last login: Tue Apr 12 15:30:33 2011 from 192.168.10.4
[root@instructor ~]#

[root@instructor ~]# ls -l test.pl
-rwxr-xr-x 1 root root 406 Apr 12 16:54 test.pl
[root@instructor ~]#

[root@instructor ~]# cat test.pl
#!/usr/bin/perl

# test.pl: small script to test mod_dosevasive’s effectiveness

use IO::Socket;
use strict;

for(0..100) {
my($response);
my($SOCKET) = new IO::Socket::INET( Proto => “tcp”,
# PeerAddr=> “127.0.0.1:80″);
PeerAddr=> “192.168.0.100:80″);
if (! defined $SOCKET) { die $!; }
print $SOCKET “GET /?$_ HTTP/1.0

“;
$response = <$SOCKET>;
print $response;
close($SOCKET);
}

[root@instructor ~]#

Let us run the test (Remember we are running this test from other computer which is not in
whitelisted, or testing purpose change the whitelist.)

[root@instructor ~]# ./test.pl
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
—–snip———
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden

Let us again run.

[root@instructor ~]# ./test.pl
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
—–snip———-
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
[root@instructor ~]#

From /var/log/httpd/error_log

[Tue Apr 12 17:02:22 2011] [error] [client 192.168.0.225] client denied by server configuration: /var/www/html/
[Tue Apr 12 17:02:22 2011] [error] [client 192.168.0.225] client denied by server configuration: /var/www/html/
[Tue Apr 12 17:02:22 2011] [error] [client 192.168.0.225] client denied by server configuration: /var/www/html/

Again after DOSBlockingPeriod the ip will be unlocked

[root@station100 mod_evasive]# ssh 192.168.0.225
‘s password:
Last login: Tue Apr 12 16:55:18 2011 from station100.example.com
[root@instructor ~]# ./test.pl
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden

And again it will be block !!

DOSLogDir
———

Choose an alternative temp directory

By default “/tmp” will be used for locking mechanism, which opens some
security issues if your system is open to shell users.

http://security.lss.hr/index.php?page=details&ID=LSS-2005-01-01

In the event you have nonprivileged shell users, you’ll want to create a
directory writable only to the user Apache is running as (usually root),
then set this in your httpd.conf.

DOSLogDir /var/log/mod_evasive

[root@station100 mod_evasive]# mkdir /var/log/mod_evasive

[root@station100 mod_evasive]# chmod 770 /var/log/mod_evasive

[root@station100 mod_evasive]# ll /var/log/mod_evasive
total 0

[root@station100 mod_evasive]# ll -d /var/log/mod_evasive
drwxrwx— 2 root root 4096 Apr 12 17:17 /var/log/mod_evasive

[root@station100 mod_evasive]# chgrp apache /var/log/mod_evasive

[root@station100 mod_evasive]#

[root@station100 mod_evasive]# ll -d /var/log/mod_evasive
drwxrwx— 2 root apache 4096 Apr 12 17:17 /var/log/mod_evasive
[root@station100 mod_evasive]#

Now the configuration will be

#mod_evasive configuration
<ifmodule mod_evasive20.c=”"> DOSHashTableSize 3097 DOSPageCount 2 DOSSiteCount 50 DOSPageInterval 1 DOSSiteInterval 1 DOSBlockingPeriod 10</ifmodule>

#our network is 192.168.2.0/24
DOSWhitelist 127.0.0.1
DOSWhitelist 192.168.2.*

DOSLogDir /var/log/mod_evasive

[root@station100 mod_evasive]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@station100 mod_evasive]#

Alternatively you can create dedicated conf file for mod_evasive as below

[root@station100 mod_evasive]# vim /etc/httpd/conf.d/mod_evasive.conf
[root@station100 mod_evasive]# vim /etc/httpd/conf/httpd.conf
[root@station100 mod_evasive]# cat /etc/httpd/conf.d/mod_evasive.conf
#mod_evasive configuration
<ifmodule mod_evasive20.c=”"> DOSHashTableSize 3097 DOSPageCount 2 DOSSiteCount 50 DOSPageInterval 1 DOSSiteInterval 1 DOSBlockingPeriod 10</ifmodule>

#our network is 192.168.2.0/24
DOSWhitelist 127.0.0.1
DOSWhitelist 192.168.2.*

DOSLogDir /var/log/mod_evasive
[root@station100 mod_evasive]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@station100 mod_evasive]#

[root@station100 mod_evasive]# ssh 192.168.0.225
‘s password:
Last login: Tue Apr 12 17:05:56 2011 from station100.example.com
[root@instructor ~]# ./test.pl
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden
HTTP/1.1 403 Forbidden

There are more parameter as below

Optionally you can also add the following directives:

DOSEmailNotify
DOSSystemCommand”su – someuser -c ‘/sbin/… %s …’”

Thats all :)

References
1. http://www.zdziarski.com/blog/?page_id=442
2. README file in mod_evasive_1.10.1.tar.gz

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>