Security

Protect your Apache from spam IP attack

Besides mod_evasive, there exists another spam deference module for Apache2 in Debian unstable - libapach2-mod-spamhaus.

Again mod-spamhaus is a configuration-free package. Just install it with apt-get:

apt-get -t unstable install libapache2-mod-spamhaus

Double check the installation with:

find /etc/apache2 -name '*spamhaus*'

Also remember to restart Apache in order to get it active:

/etc/init.d/apache2 restart

P.S. Note that this package is now only available in Debian unstable.


Protect your Apache from DDoS attack - mod_evasive

DDoS attack (http://en.wikipedia.org/wiki/Denial-of-service_attack) is all around the Internet and no one can escape from it. What we can do is trying to protect ourself whenever happened. On the other hand, DDoS attack to Apache is also very common so what can we do for it? Let's try mod_evasive (http://www.zdziarski.com/projects/mod_evasive/).

The installation of mod_evasive under Debian is very simple:

apt-get install libapache2-mod-evasive

After install try this command and you will find that Debian have already active mod_evasive for us by default:

find /etc/apache2/ | xargs fgrep -nH evasive

So what we only need to do is restart the Apache and let mod_evasive active:

/etc/init.d/apache2 restart

For testing the effect of mod_evasive, you can try this command (you will have HTTP/1.1 302 Found at beginning but soon become HTTP/1.1 403 Forbidden):

perl /usr/share/doc/libapache2-mod-evasive/examples/test.pl


Filter spam or bad robot visit your Apache with Fail2ban

Since a long days before I keep on using Apache's mod_access for spam or bad robot filtering (http://edin.no-ip.com/content/block-apache-visiting-abnormal-user-agent). It is quite handy and simple; BTW, you need to configure it manually. The benefit of the model is you only need to have a functional Apache installed then you can set it up without any special difficult and dependence; and the drawback is simple that it is not flexible.

As Debian's Fail2ban already come with apache-badbots.conf, why not utilize it? As it will function in firewall level, rather than application level (Apache), using this model would be more secure and stable, too.

Setting this up is very simple. In case of Debian, install Fail2ban with:

apt-get install fail2ban

Then check /etc/fail2ban/filter.d/apache-badbots.conf and you will find a well pre-defined blocking list, which fetched from http://www.user-agents.org. What you need to do is active this filtering rule within your Fail2ban configuration. As mentioned in the header section of /etc/fail2ban/jail.conf, we should create a file called as /etc/fail2ban/jail.local which contain our changes for override, e.g.:

[apache-badbots]
enabled = true
port    = http,https
filter  = apache-badbots
logpath = /var/log/apache*/*access.log
maxretry = 2

After restart Fail2ban with /etc/init.d/fail2ban restart, check your iptables with iptables -nvL and you should have similar result:

Chain INPUT (policy DROP 55369 packets, 7683K bytes)
4650  824K fail2ban-apache-badbots  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           multiport dports 80,443

In order to check for ban/unban record, try cat /var/log/fail2ban.log | grep WARNING. E.g. soon after I have install Fail2ban it catch 1 IP for ban, due to rule of apache-overflows:

2008-05-18 18:03:11,702 fail2ban.actions: WARNING [apache-badbots] Ban 119.30.120.220
2008-05-18 18:13:11,817 fail2ban.actions: WARNING [apache-badbots] Unban 119.30.120.220


Block Apache visiting from abnormal User-Agent?

My site is being attack by some abnormal client, with some invalid user-agent. They keep on scanning my server, and just keep on pushing up the server loading.

There is a lot of different defense method, and here is one of the simple solution (http://httpd.apache.org/docs/2.0/mod/mod_access.html). Just add the following lines (I just list those are required):

<VirtualHost *>
        # You need to add these line in regex formatting
        # Check your access log to find out those abnormal User-Agent
        SetEnvIf User-Agent .*www\.pingdom\.com.* robot
        SetEnvIf User-Agent .*blogspot\.com.* robot
        SetEnvIf User-Agent ^$ robot

        <Directory /var/www/>
                AllowOverride None
                Order allow,deny
                allow from all
                # Here we get the defined variable, and block them
                deny from env=robot
        </Directory>
</VirtualHost>

And so Apache will happily block those unfriendly user agents ;-)


Syndicate content