Post Pic

Protect your WordPress blog using .htaccess

The .htaccess file have lots of possibilities. Today, let’s see how we can protect our WordPress blog from scripts injection, and unwanted modification of _REQUEST and/or GLOBALS.

Simply paste the following lines into your .htaccess file. This file is located at the root of your WordPress install.
Remember to always make a backup of your .htaccess file before editing it so, you'll be able to restore it if something went wrong.

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]

Thanks to Oussama for those simple, but efficient lines of code!

24 Responses

Jan 25 2010 08:54

Thx for linking :)

Jan 25 2010 09:49

Hi

Would be great if this tip could be accompanied by some more explanation, since i don’t feel very comfortable just adding this to my .htaccess file without knowing what it does.

Gabrie

Jan 25 2010 12:03

Glad that you’re sharing the trick but I just wish you briefly describe what it does. As of now it looks greek to me. Going to Oussama’s site is no help, the language is also greek to me :)

Jan 25 2010 12:46

@Hilmy: I came here for the same question (or to verify that my assumption on what the code does was right). I now noticed that info below the headline wasn’t visible in the RSS feed, which more and less explains it:

This .htaccess rule protects the blog from “scripts injection”, and unwanted modification of _REQUEST and/or GLOBALS.

I’ve seen attempts like this on my blog, blocked by WordPress Firewall -plugin, but .htaccess is always better.

Jan 25 2010 14:56

Here is the translation of the original article from French to English ala Google

The majority of developers still think to protect their POST and GET requests (obviously) but in my opinion this is not always enough: we must also remember to protect themselves against the injection script, changing _REQUEST, changing PHP GLOBALS variable .. And to do this we simply add (create?) A few lines in his file. Htaccess

Warning: Incorrect use of. Htaccess files can cause the blockage of its website. Use sparingly! (The example I give is tested and is functional).

If after the implementation of the file. Htaccess you have problems accessing the site, simply delete this file or delete the lines you’ve added.

As we all know (not you?) File. Htacces must always start with these two lines:

Options + FollowSymLinks # To enable tracing of symbolic links
RewriteEngine On # To start the engine of url_rewriting

We begin by putting the conditional redirection (url_rewriting):

# It blocks all the links containing :

RewriteCond% (QUERY_STRING) (\ |% 3E) [NC, OR]

# It blocks all scripts that try to modify a PHP GLOBALS variable:

RewriteCond% (QUERY_STRING) GLOBALS (= | \ [| \% [0-9A-Z] (0,2)) [OR]

# It blocks all scripts that try to modify a _REQUEST variable:

RewriteCond% (QUERY_STRING) _REQUEST (= | \ [| \% [0-9A-Z] (0,2))

# Everything is good! We sent it all to the homepage with 403 Forbidden error:

RewriteRule ^(.*)$ index.php [F, L]

So to summarize: To better protect your website, you can add these lines in his file. Htacces (or create one. Htaccess file containing these lines):

Jan 25 2010 18:10

I use the Su.pr short URL service on my domain and my .htacess file includes the line:

RewriteRule ^[a-zA-Z0-9]{4}$ supr.php?supr=$0

Would these scripts conflict?

Jan 25 2010 20:05

nice snippet there Jean-Baptiste, will definitely be useful in the future!

Jan 26 2010 03:10

Looks nice, but… What it does? ;)

Jan 26 2010 22:01

yea, explanation?

thanks!!!

Jan 28 2010 15:08

Author fail!

Anyone who adds ANY code to any part of their site without the author explaining what it does is ignorant.

Jan 28 2010 16:53

@Rick : Stop reading my blog then. It’s pretty simple.

Jan 30 2010 12:40

great tip and follow-up info. I’ve been lucky so far but you never know when a hacker is going to find your site.

Feb 01 2010 00:26

Sounds good, but how do you use it along with a custom permalink structure : /%category%/%postname%.php

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Feb 01 2010 19:21

Not too happy adding these lines without any commenting on it. Would be nice to have more detailed explanation about what each line does.

Feb 02 2010 18:52

Nice tip Jean-Baptiste.

@Jeevan – Check out comment number 5 on this post.
Or you could learn French and check out the source.

Feb 04 2010 10:08

sorry … but this code broke my rss feed … so I removed this

May 28 2010 01:11

I think this is a useful hack, but I need to know what it means to each line. How about an explanation?

Jul 02 2010 04:51

Okay, here is the explanation:

The first line checks if the URL contains a script tag (e.g. …) – this is important to check for because some pages just print out parameters from the url directly into the code () and this would lead to the JavaScript code being executed.

The second and third line check if the URL contain the strings _REQUEST[...] or GLOBAL to prevent access to these global PHP variables.

If one of these conditions are met, the user is redirected to the websites frontpage.

Jul 02 2010 04:52

Okay, my code examples have been cut out – that’s good, because that means that my text has been filtered for malicious code :) .

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required