How to blacklist an IP Address in Apache

April 27th, 2009 by bodhi.zazen

As my blog has become more popular, I am collecting my share of “spam-boys” and was looking for an easy way to black list IP addresses.

Sure you can use iptables , but it would be nice if Apache used tcpwrapper (host.deny), but alas that requires Apache to be re-compiled.

As it turns out there is an easy way using features built right into Apache – deny from – sweet

Edit your main Apache configuration file, and at the bottom add (assume you want to black list 111.222.33.444 or it’s subnet).

< Location />
< Limit GET POST PUT>
order allow,deny
allow from all
deny from 111.222.33.444
deny from 111.222.33
< /Limit>
< /Location>

Note: I had to put spaces at the front of the < Location> and < Limit> tags for them to show-up on this blog entry, remove them or Apache will give you error messages.

Rules explained :

order allow,deny
allow from all

Allows from all ip unless an ip address is specifically denied.

deny from 111.222.33.444
deny from 111.222.33

This is your blacklist, by ip address.

111.222.33 == 111.222.33.** (the entire 111.222.33 subnet, helpful from spammers who change IP, mean otherwise ;) )

After making your edits, restart Apache :

Debian / Ubuntu :

sudo /etc/init.d/apache2 restart

RHEL / Centos / Fedora :

service httpd restart

What I like about this technique, more then anything else, is the “Permission Denied” message Apache gives. Just my way of giving back to the spamming community (iptables is just too impersonal that way).

Another thing, this seems to cover all virtual hosts in a single location (ie this does not need to be configured per VirtualHost, sweet).

StumbleUpon

Posted in Linux

3 Comments »

  1. He he he …

    56 “Forbidden” pages served since posting this blog (4 hours ago).

    Yum , spam :)

    Comment by bodhi.zazen — April 27, 2009 @ 4:34 pm

  2. [...] posted here: Shadows of epiphany » Blog Archive » How to blacklist an IP … Posted in: How [...]

    Pingback by Shadows of epiphany » Blog Archive » How to blacklist an IP … | Linux Affinity — April 27, 2009 @ 7:22 pm

  3. Perfect!
    Exactly what I was looking for.
    Yet another worthy contribution by bodhi.zazen to the FOSS community.
    I plan to use this to help keep my blog and wiki spam free.

    Additionally I am going to forward this link to the maintainer of the Tulsa LUG wiki. We have been having a problem with lots of Japanese spam lately.

    Comment by duanedesign — July 2, 2009 @ 3:51 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment