WordPress Quickie: Ban Malicious Visitors

July 11, 2006 · 29 comments

Over the past few weeks I have been receiving spam comments from one spammer in Abu Dhabi. The comments were not picked up by Akismet, nor did Akismet learn to recognize them as spam after I flagged them as such. Yesterday I started receiving tons (a few every minute) of the same spam with the guy leaving links to his splog. At first I disabled comments on the current post but then he started spamming huge comments all over the place. Unfortunately WordPress does not currently possess the ability to ban a commenter based on his IP address.

I had attempted to perform a deny with htaccess rules but my foo was lacking and I kept getting Internal Server 500 errors. (Update: The WordPress Codex expands upon denying access via htaccess.) So I decided for a quick, yet effective PHP tactic as follows:

if($_SERVER['REMOTE_ADDR'] == '0.0.0.0') { echo 'Spam is not appreciated!'; exit; }

If loaded at the top of a primary WordPress file such as wp-config.php, the spammer won’t even be able to access the site. In place of the site, the spammer receives a lovely message. You can get as creative as you like with the message. For this to work, you must replace 0.0.0.0 with the IP address of the spammer which WordPress provides with every comment.

If you have multiple IPs to ban just follow the same format except with the code below:

if($_SERVER['REMOTE_ADDR'] == '0.0.0.0'
|| $_SERVER['REMOTE_ADDR'] == '0.0.0.0'
|| $_SERVER['REMOTE_ADDR'] == '0.0.0.0')
{
	echo 'Spam is not appreciated!'; exit;
}

This method of banning visitors obviously only works with static IPs. There’s nothing stopping the spammer/irate kid from coming back in a few days after his dynamic IP has changed. I have yet to find any effective WordPress plugins that can do simple user banning, so if you know of anything please let me know as I bet it’s something on many peoples’ minds. If nothing exists, I might just have to start writing a WP-Ban plugin, heh. Update: This plugin looks promising.

PaulStamatiou.com runs on the Thesis Theme for WordPress

How smart is your Theme?  How good is your support? Check out ThesisTheme for WordPress.

Thesis is the search engine optimized WordPress theme of choice for serious online publishers. If you’re a blogger who doesn’t understand a lot of PHP, Thesis will give a ton of functionality without having to alter any code. For the advanced, Thesis has incredible customization possibilities via Thesis hooks.

With so many design options, you can use the template over and over and never have it look like the same site. The theme is robust and flexible enough not only to accommodate a site like PaulStamatiou.com, but also to enable the site to run far more efficiently than it ever has before.

{ 3 trackbacks }

Gary Slinger » links for 2006-07-20
July 20, 2006 at 11:59 am
Blogging Explorer-The Blog Bible
January 14, 2007 at 8:48 pm
Reno and Its Discontents»Blog Archive » Will “The Man” Help Me Troubleshoot Comments?
March 9, 2007 at 8:37 pm

{ 26 comments… read them below or add one }

1 Jonathan July 11, 2006 at 12:44 pm

What do you mean your foo was lacking? I had no problems using htaccess to deny IPs for spam reasons when I had a blog. Then I moved to a captcha method instead which instantly removed all spam. Though I forgot what it was called, it was excellent, so much in fact, not all characters were human readable. But of course I like to respond via lynx so i couldnt post back (if i didnt login that is).

But the deny method should work, as I have used it for several months in WP…

Reply

2 alvin July 11, 2006 at 12:47 pm

this is interesting. Akismet missed it more than one time? It has worked perfectly so far for me. Might want to talk to akismet developers about this. I can see swarms of spammers asking tips from that dude on how he bypasses the system.

Reply

3 cavemonkey50 July 11, 2006 at 12:50 pm

WordPress does support IP blocking. Just enter the spammer’s IP address in the blacklist field on the Discussion Option page. I’ve been using that feature for years and have never had a problem.

Reply

4 Paul Stamatiou July 11, 2006 at 12:56 pm

Oohh. I thought the blacklist was only for words. Well anyways, this method is still useful for making sure that someone can’t even load up your site.

Reply

5 Dave Goodman July 11, 2006 at 12:58 pm

One thing I’ve been having lately is spammers who are leaving comments without registering, which is weird, as I have registration enabled. Askimet stops them nicely but I feel uneasy that there’s a potential security hole there – any experience of this Paul, or anyone else?

Reply

6 cavemonkey50 July 11, 2006 at 12:59 pm

Nope, it acts just like the moderation list, except it kills the comments without you knowing. Just make sure you try out anything you add to the blocklist in the moderation list first. You don’t want to be blocking legitimate users’ comments.

Reply

7 franky July 11, 2006 at 1:33 pm

I use Spam Karma 2. Since I installed the newest version of Bad Behavior 2, which now also works with the wp-advanced-cache plug-in (small change required in one of the files, everything nicely described in the readme file) Spam Karma only logged 2 spomments anymore in almost 72 hours.

Reply

8 Paul Stamatiou July 11, 2006 at 1:37 pm

Be careful with Bad Behavior.. Brian told me it almost killed his database. But maybe the new version is different.

Reply

9 Jonathan July 11, 2006 at 1:50 pm

I still prefer htaccess

Reply

10 franky July 11, 2006 at 1:59 pm

LOL, thanks for the tip ;)
Anyway always backup before installing plug-ins, especially if they make new db-tables. That point were maybe the ‘only’ critic concerning Bad Behavior, although the installations instructions clearly state to delete previous installations. I must admit I didn’t check if the table structure changed in the new version, which would obviously create problems since there is no update version.

What I do know is that BB doesn’t work with GoDaddy, but also that is stated in the guidelines.
Using *looks around* Google’s Web Accelerator there are no problems, unlike with EE.

Reply

11 cavemonkey50 July 11, 2006 at 2:38 pm

I tried Bad Behavior once. It thought I was a spammer and it blocked me from my own site. Several of my users also had major problems getting to my site. I would never install that piece of crap again.

Reply

12 Derek Punsalan July 11, 2006 at 2:43 pm

Bad Behavior is running smoothly on my page. While I completely support the hard work devs put into public plugins, watch your footer as this plugin is one to insert an extra line of code to pages. Jacked my page up. :( Great plugin though. Akismet was beginning to fall short of expectations and manually blacklisting IPs was becoming way too tedious.

Reply

13 franky July 11, 2006 at 2:58 pm

Just like Spam Karma punishes users without javascript, Bad Behavior also checks the signature of browsers. Open proxies might cause problems here, just as too severe security settings.
I guess AOL users might have problems with Bad Behavior.

Derek, the footer message can be unchecked in the Bad Behavior options. ;)
Manually blocking IP’s is good for quick operations like in Paul’s case, although I personally prefer .htaccess. Mid 2003 I started a disallow, deny htaccess, after 3 months I gave up. If I remember well all the rules where 30kB without different mod_rewrites rules. The server performance obviously suffered under this.

Personally I wouldn’t mind to pay for my ISP for an international service maintaining professional lists allowing to block blacklisted IPs immediately at the backbones. Imagine every flatrater pays monthly $0.10 for this. Enough of money to maintain such a service worldwide. And the internet would become faster again.

Reply

14 Derek Punsalan July 11, 2006 at 3:30 pm

Yes I noticed the footer option after a little exploring. I would pay for a ISP service to stop bastards at the source.

Reply

15 Hasse R. Hansen July 12, 2006 at 2:59 pm

Hey,

If you have multiple ip’s it’s nicer to use an array in this way. And more easy to add more ipadresses.

$iparray = array(“0.0.0.0″, “1.1.1.1″, “2.2.2.2″);
if (in_array($_SERVER['REMOTE_ADDR'], $iparray))
{
echo “spam isn’t appreciated!”;
exit;
}

Reply

16 kobak July 15, 2006 at 1:35 pm

Hi, I use boakes akismet htaccess extension and it does just what You need. Give it a try!

Reply

17 kobak July 15, 2006 at 1:39 pm

Or another nice plugin is: bannage.

Reply

18 Justin July 24, 2006 at 1:53 am

Appreciate the linkback. Bannage was started while I was vacationing in Japan. I was getting masses amounts of spam and Akismet was just letting it ride on into the list. I ventured out to write something that would do any good it could.

Although the plugin is still beta, it does its job. It is still under development as individuals have began to post some bugs. None-the-less I appreciate any and all support towards the cause of Bannage.

Reply

19 Thilak August 12, 2006 at 8:45 am

Try ‘Bannage’, its a wordpress plugin which does the same job pretty easily.

Reply

20 Danila September 8, 2006 at 8:32 am

hi

Perfectly idea.

:)

Reply

21 Thilak September 19, 2006 at 11:29 am

Well Paul,
Bannage Plugin can block visitors using cookies, So this will also block visitors with dynamic IP.

But I use both methods, Just for extra Protection ;)

Reply

22 marion January 11, 2007 at 5:37 pm

I keep getting the same thing as Dave Goodman mentioned. I also thought the blacklist was for words only.

Thanks for the feed back.

Reply

23 Godzilla Blitz February 23, 2007 at 3:18 pm

I’m wondering if anyone can confirm that entering the IP address in the Comment Blacklist field in WordPress actually works in nuking comments from that particular IP address.

I’ve got one particularly persistent spammer. I entered his IP address in the Comment Blacklist field, but the spam from that address keeps ending up in my Akismet spam folder. In other words, despite having his/her IP address in the comment blacklist field, spam from that IP address is getting through.

Is there something obvious that I’m missing? Do we need to preface the IP address with some sort of code?

Reply

24 Justin Shattuck March 5, 2007 at 11:34 am

Bannage has wildcards integrated so you can block entire subnets, domains, and prefixes for names etc.

I appreciate the mention Paul.

Reply

25 Bob Campbell January 24, 2009 at 4:31 am

I like nailing them right at the server.. the spammers don’t even get to the directory that Wordpress is located in..

Change your httpd.conf file in this area of the file:

# Controls who can get stuff from this server.
#
# below is a specific block of a spammer ip
Order allow,deny
Allow from all
deny from xxx.xxx.xxx.xxx
deny from xxx.xxx.xxx.
# the deny part is the ip you are blocking
# the deny part with the last group of x’s removed blocks a whole ton # of ip’s.. [smile] – but some of these big spammer have many ip’s

Don’t forget to stop and restart your httpd daemon
> service httpd stop
> service httpd start

Note this is the same as putting the deny directive in an .htaccess file (if you don’t have access to the Apache configuation httpd.conf file)

Cheers,
Bob

Reply

26 sabres678 April 1, 2009 at 8:39 pm

How do you open up that config thing?

Reply

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: