Early this week we were hired to remove some malware from a quite popular web site. The malicious code was there for a little while and the site got blacklisted by google. That’s how the owner noticed it.

Everytime someone tried to visit it (either using Chrome or Firefox) or searched for this site on google, that ugly “Report attack Site” message would show up.

Uh-oh, not good for a site owner that makes money with ads and can’t afford losing users. If they had been using our Web-based Integrity monitor, or recognized the early symptoms of a hacked site, that would not have happened, but since they didn’t, now it was time to fix the problem. Remember, no matter what CMS your site runs on, be it WordPress, Joomla, Drupal or something else, we can help fix and prevent malware on your website.

1-Understanding the problem

The first thing we did was to look where and how the code was showing up. We used a simple dump tool to see the source page (lynx is a command-line tool available on most Linux systems):

$lynx –source –dump [siteinquestion]

It shows the whole page source and by analyzing it we saw the following strange javascript (a bit modified to protect the innocent):

We also used our site scanner (free) and it confimed that it was indeed malicious.

2-Analyzing the javascript

There are multiple ways to analyze a malicious Javascript, and we chose the easier one. We see that they added an escaped javascript, unescaped and used the function eval to parse the content. I copied over the javascript to a local file and modified the final “eval” function for the “alert” one. Now, instead of executing the code, it will print it.

So, the unescaped code loads another script from the site martuz.cn. After searching a bit, this seems to be an old attack (from mid-2009), that somehow is still running around. The martuz.cn is now unreachable, so the good news is that the attack is not doing anything against the users.

3-Cleaning up WordPress

Once we found what the code was and what it was doing, now it was time to remove it from the site. That’s what we did:

Back the whole WordPress directory for analysis and removed it from the site

Changed all passwords, unused accounts and services and cleaned up the box

Reinstalled WordPress from scratch (last version), re-imported the database (after checking that it was safe) and reinstalled their theme from scratch (to make sure it was not hacked too).

Worked with Google to get the site removed from their blacklist

4-Analysis of the malware

Once the site was clean and the client happy, we went to do a better analysis of the attack. First, we did a diff between their WordPress version and the original one (they were on version 2.8):

$ diff -r -i –strip-trailing-cr -b -B sitedump/public_html wordPress
Only in sitedump/public_html/wp-content/plugins: multi-level-navigation-plugin1
Only in sitedump/public_html/wp-content/plugins: order-categories
Only in sitedump/public_html/wp-content/plugins: seo-automatic-links
Only in sitedump/public_html/wp-content/plugins: wp-contact-form
Only in sitedump/public_html/wp-content/plugins: wp-db-backup

We also did a diff between the original theme and the one they used and no major changes were found. With that, it was clear to us that the problem was in one of the plugins.

We started by searching for that javascript code in the plugins directory and nothing was returned. That means that the code was probably escaped (hidden) in some way. So we searched for base64_decode or eval (PHP functions generally used by malware authors):

So, these 3 files wp-db-backup/wp-db-backup.php, image.php and gifimg.php had possibly something hidden. To analyze the code, we did the same thing we did with Javascript. We modified the “eval” function for “echo” to see what it was doing. On the wp-db-backup.php we removed the encoded string and decoded it externally using the base64 command line tool:

So, all of them had a backdoor to allow the attacker to execute any PHP script (and command) they wanted on the box (see eval(POST)) and the wp-db-backup.php had this script to create the malicious javascript on all the pages.

Lessons learned

First, always monitor your systems. If they had a HIDS installed (like the open source OSSEC) it would had detected the modification on those files.

Daniel is the Founder & CTO of Sucuri and also the founder of the open source project - OSSEC HIDS. His interests range from intrusion detection, log analysis (log-based intrusion detection), web-based malware research and secure development.

A very nice write up on how to trouble shoot a setup for clients, I would love to learn more on this subject, keep up the good work.Benjamin Straw

http://www.blogger.com/profile/14980808976404159238 http://sucuri.net

Thanks! I appreciate the feedback.

http://www.extremehost.ba/ web hosting

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.

http://blog.avangelistdesign.com Avangelist

I have this issue with a very large news site. I have no problem with getting a clean install of WP going, but it is finding the triggers that must be in the database I am struggling with.

The minute I connect the original db to the new install the same problem occurs. This is even occurring on a local install.

Chris

I stumbled onto this site in my search to find help fixing my wordpress based site. I'm not trying to bash you here in your comments section, but I have to be honest and say that it makes me very nervous for my computer's safety, and perhaps even more so to do business with you because when I arrived at this particular page, my Avast AV security pops up for the first time ever since I've had it (got it in the last 2 weeks or so), and says out loud, "Threat has been detected".

It opens a small notification window that reads:
"TROJAN HORSE BLOCKED"
avast! Web Shield has blocked a threat.
No further action is required.
Object: http://blog.sucuri.net/2010/02/georgia-government…
Infection:PHP:Small-F{Trj}
Action: Connection Aborted
The threat was detected and blocked while downloading an item from the web.

Yikes! What is up with this?

What to do about my site? Ugh…

http://intensedebate.com/people/dremeda dremeda

Thanks for the comment Chris. The warning you're receiving is a false positive.

When we write a post we typically include a sample of what the malicious code may look like (merely showing people what attacked them) just like in the analysis above.

Dre

Chris

Thanks for the reply. Please excuse my ignorance–I suck at anything related to computers. At least the AV thing is working good. Please feel free to remove these messages if you find that to be appropriate.

Now, about my site…

http://blog.sucuri.net dremeda

Chris, no problem and we can appreciate the concern, thank you for bringing up the subject. You can report this to your antivirus provider as a false positive if it alerts you again.

Can you talk a bit to what problems you're encountering on your site? Feel free to contact us off the blog if you'd prefer: support@sucuri.net

Many thanks for this blog post; it was instrumental in finding the malware that infected our WordPress site !

Johnny Hurst

My wordpress site has been taken over and my admin account deleted now I cant get into it. My host restored it to about a month ago but it didn’t do any good I still cant get in. Any one know what I should do?