break
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading...

How to install mod_evasive on a cPanel server

We are going to install mod_evasive to help protect our cPanel server from low end ddos attacks.  The installation is really quite simple.So, what is this mod_evasive you speak of…?  Here is a quote directly from the creators site:

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 and network management tool, and can be easily configured to talk to ipchains, firewalls, routers, and etcetera. mod_evasive presently reports abuses via email and syslog facilities.

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 for maximum protection.

This module instantiates for each listener individually, and therefore has a built-in cleanup mechanism and scaling capabilities. Because of this per-child design, legitimate requests are never compromised (even from proxies and NAT addresses) but only scripted attacks. Even a user repeatedly clicking on ‘reload’ should not be affected unless they do it maliciously. mod_evasive is fully tweakable through the Apache configuration file, easy to incorporate into your web server, and easy to us.

OK, now that’s done, how do we install this thing?  Pretty simple, lets log into a terminal session as root.

I always download everything to my downloads folder, so, we change to it:

[email protected] [~] # cd downloads

Now, we need to grab the file (this is the latest version as of this post):

[email protected] [~/downloads] # wget http://www.zdziarski.com/projects/mod_evasive/mod_evasive_1.10.1.tar.gz

Of course, we now need to extract them:

[email protected] [~/downloads] # tar zxvf mod_evasive_1.10.1.tar.gz

Now, change into the directory that was just created:

[email protected] [~/downloads] #cd mod_evasive

Now, you can view the README file for information on how to install this on a couple of different types of servers, however, we are going to focus on installing mod_evasive on an Apache2x server.  We also have cPanel running on this box, so, to install, we run the following:

[email protected] [~/downloads] #/usr/local/apache/bin/apxs -i -a -c mod_evasive20.c

Now, that will create an entry in the httpd.conf file, and, if we want to retain that after an upgrade/rebuild, we need to tell cPanel not to take it out!  Do do this, we now run this:

[email protected] [~/downloads] # /usr/local/cpanel/bin/apache_conf_distiller –update

Now, to change the settings for mod_evasive, we need to add them in some place.  All we have done so far, is install the actually module into apache, and, even with a restart, it would not be using it.  So, I like to add things into my includes files through either WHM, or, directly through the terminal.  To do this, we run the following:

[email protected] [~/downloads] # vim /usr/local/apache/conf/includes/post_virtualhost_2.conf

Once the file is open, lets add in the following lines to the bottom of the file:

<IfModule mod_evasive20.c>
DOSHashTableSize    3097
DOSPageCount        2
DOSSiteCount        50
DOSPageInterval     1
DOSSiteInterval     1
DOSBlockingPeriod   3600
DOSEmailNotify      root
</IfModule>

Once done, save the file, this varies depending on your editor of choice, in VI and VIM, its :wq enter.

Here is a copy from the readme file for the variables that can be configured :

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:

DOSWhitelist    127.0.0.1
DOSWhitelist    127.0.0.*

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

You will notice that I have a couple of different settings in there than the default.  I ban the IP for 1 hour, thus the 3600 seconds, and, I also send an email to the root user.  I have this email account forwarded as well, so I receive the email off the server.  You can place your [email protected] address in that spot if you prefer.

That’s all folks!  This will help to lesson the load on your server under a mild ddos attack, or, someone just being silly and trying to increase the load on your server.

10 responses to “How to install mod_evasive on a cPanel server”

  1. i must admit this is one of the most informative posts i have read today. kudos to the author

  2. Dayna Schutze says:

    Just landed on this place via Google lookup. I love it. This post switch my perceptual experience and I am bringing the RSS feeds. Cheers Up.

  3. Security says:

    Hi there I like your post

  4. Dalton Lopey says:

    This is a good piece of writing, I was wondering if I could use this summary on my website, I will link it back to your website though. If this is a problem please let me know and I will take it down right away.

  5. Jack Morris says:

    Thank you for the great post. I’m bookmarking your post!

  6. dika says:

    Nice post,

    I’m bookmarking your post

Leave a Reply

Your email address will not be published. Required fields are marked *