Protect WordPress Against Malicious URL Requests
Post #732 categorized as WordPress, last updated on Apr 5, 2010
Tagged with blacklist, htaccess, php, plugins, security, url, WordPress
A few months ago, many WordPress sites were attacked with some extremely malicious code. While searching for a good solution, I discovered the following gem of a plugin in the pastebin repository:
<?php /* Plugin Name: Block Bad Queries */
if (strlen($_SERVER['REQUEST_URI']) > 255 ||
strpos($_SERVER['REQUEST_URI'], "eval(") ||
strpos($_SERVER['REQUEST_URI'], "base64")) {
@header("HTTP/1.1 414 Request-URI Too Long");
@header("Status: 414 Request-URI Too Long");
@header("Connection: Close");
@exit;
} ?>
This script checks for excessively long request strings (i.e., greater than 255 characters), as well as the presence of either “eval(” or “base64” in the request URI. These sorts of nefarious requests were implicated in the September 2009 WordPress attacks.
To protect your site using this lightweight script, save the code as a plugin and activate in the WordPress Admin area. Once active, this plugin will silently and effectively close any connections for these sorts of injection-type attacks.
For further protection against malicious code, automated attacks, and other cracker nonsense, check out my 4G Blacklist.
Update to original method
As is often the case, Perishable Press readers have helped to improve this plugin by leaving comments, asking questions, and recommending changes. Here is the new, recommended version of the plugin:
<?php
/*
Plugin Name: Block Bad Queries
Plugin URI: http://perishablepress.com/press/2009/12/22/protect-wordpress-against-malicious-url-requests/
Description: Protect WordPress Against Malicious URL Requests
Author URI: http://perishablepress.com/
Author: Perishable Press
Version: 1.0
*/
global $user_ID; if($user_ID) {
if(!current_user_can('level_10')) {
if (strlen($_SERVER['REQUEST_URI']) > 255 ||
stripos($_SERVER['REQUEST_URI'], "eval(") ||
stripos($_SERVER['REQUEST_URI'], "CONCAT") ||
stripos($_SERVER['REQUEST_URI'], "UNION+SELECT") ||
stripos($_SERVER['REQUEST_URI'], "base64")) {
@header("HTTP/1.1 414 Request-URI Too Long");
@header("Status: 414 Request-URI Too Long");
@header("Connection: Close");
@exit;
}
}
} ?>
The changes include, in order, proper plugin declaration (thanks Aldo), exclusion of admin pages, and additional protection against CONCAT and UNION+SELECT requests (thanks John Hoff).
Update [March 5th, 2010]: Block Bad Queries is now available at the Plugin Repository.
Update [April 5th, 2010]: Code changed to use stripos instead of strpos. See this comment for details. Thanks to James Wilkes for the tip :)
Share this..
Related articles
- Blacklist Candidate Number 2008-01-02
- Comprehensive Reference for WordPress NoNofollow/Dofollow Plugins
- Optimize WordPress: Pure Code Alternatives for 7 Unnecessary Plugins
- Blacklist Candidate Number 2008-02-10
- Blacklist Candidate Number 2008-03-09
- Three Unsolved WordPress Mysteries
- Blacklist Candidate Number 2008-04-27
#1 — Alex Denning
Great tip Jeff. Thanks for that. Going into my framework :)