Share |

How to Find a Hacked WordPress Plugin

March 23rd, 2011 by Eric Giguere Leave a reply »

When your WordPress blog suddenly starts displaying ads for various pharmaceuticals, chances are good that the site’s been hacked. A quick check using the “View Source” option on your favorite browser will probably reveal that a large list of links to dubious sites has been inserted somewhere into the HTML generated by WordPress, probably wrapped in a <div> tag that shifts it off the displayable area of the screen. This lets Google and other search engines see the links but ensures that the viewers of the site don’t — this kind of hacking is done specifically to get some link juice flowing to those dubious sites.

If you have a pre-hacked backup of your system, the easiest fix is to revert to that backup. But that’s not always an option. At this point you’ll need to start looking at the PHP files that run your WordPress blog, looking for hacked files. (If you’re not techie, this is the time to engage the help of one of your techie friends.)

Check the Theme

Start by looking at the files included with your current WordPress theme. Hacking the footer.php file is a favorite option, for example, or the sidebar.php. You can quickly look through these files directly from the WordPress dashboard using the Editor option under Appearance.

Not There? Check the Plugins

If you can’t find anything in the theme, it’s probably a problem with a plugin. WordPress has a nice extension mechanism that lets plugins add content directly to the generated HTML. Chances are good that one of your plugins has been hacked and is injecting its links via one of these “hooks”.

Grab a copy of the wp-includes/plugin.php file from your site and stash it somewhere safe as a backup. Then make a second copy. Now open that copy with your favorite text editor and look for a PHP function called add_action that looks like this:

function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
	return add_filter($tag, $function_to_add, $priority, $accepted_args);
}

Insert three lines so it looks like this:

function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
      echo "\n\n";
      return add_filter($tag, $function_to_add, $priority, $accepted_args);
}

Save the changes and upload the modified file to your blog, overwriting the existing plugin.php. Now reload one of the pages in your browser and view the source to the page. You’ll see a bunch of HTML comments inserted into the generated HTML, like so:

<!-- add_action
Adding akismet_cron_recheck
#0  add_action(akismet_schedule_cron_recheck, akismet_cron_recheck) called at [/home/memwg/public_html/wp-content/plugins/akismet/akismet.php:495]
#1  include_once(/home/memwg/public_html/wp-content/plugins/akismet/akismet.php) called at [/home/memwg/public_html/wp-settings.php:190]
#2  require_once(/home/memwg/public_html/wp-settings.php) called at [/home/memwg/public_html/wp-config.php:77]
#3  require_once(/home/memwg/public_html/wp-config.php) called at [/home/memwg/public_html/wp-load.php:30]
#4  require_once(/home/memwg/public_html/wp-load.php) called at [/home/memwg/public_html/wp-blog-header.php:12]
#5  require(/home/memwg/public_html/wp-blog-header.php) called at [/home/memwg/public_html/index.php:17]
-->

This shows you exactly who is adding a specific hook into the system. The example above shows the Akismet plugin adding a hook. It’s probably OK. There are a lot of hooks that get added, so it may take a while to find the offending plugin. You’re looking for something suspicious like this:

<!-- add_action
Adding echo_links
#0  add_action(wp_footer, echo_links) called at [/home/memwg/public_html/wp-content/plugins/duplicate-content-cure/duplicate-content-cure.php(79) : runtime-created function:402]
#1  __lambda_func() called at [/home/memwg/public_html/wp-content/plugins/duplicate-content-cure/duplicate-content-cure.php:80]
#2  include_once(/home/memwg/public_html/wp-content/plugins/duplicate-content-cure/duplicate-content-cure.php) called at [/home/memwg/public_html/wp-settings.php:190]
#3  require_once(/home/memwg/public_html/wp-settings.php) called at [/home/memwg/public_html/wp-config.php:77]
#4  require_once(/home/memwg/public_html/wp-config.php) called at [/home/memwg/public_html/wp-load.php:30]
#5  require_once(/home/memwg/public_html/wp-load.php) called at [/home/memwg/public_html/wp-blog-header.php:12]
#6  require(/home/memwg/public_html/wp-blog-header.php) called at [/home/memwg/public_html/index.php:17]
-->

What’s suspicious about this? First of all, the name of the hook is “echo_links”. Second, it’s coming from the Duplicate Content Cure plugin. Third, it’s a “runtime-created function”, which is definitely unusual. So it’s worth a look.

Luckily, the backtrace dump above — the list of all files and functions that were called by PHP in order to add the hook — points me to the file duplicate-content-cure.php on line 80.

To quickly see if this is the culprit, restore the plugin.php to its original state (upload the original you had stashed away somewhere safe) and then disable the plugin in question from the WordPress dashboard and see if the offending content disappears from your site.

Secure Your Site

Once you’ve found the bad plugin, delete it from your site. You may want to install a replacement version of the plugin or find an alternative plugin with similar functionality. Only install plugins (and themes) from sites you trust.

After cleaning your site, be sure to harden your WordPress installation in order to prevent this kind of hacking from occurring in the future.

7 comments

  1. Jan Smith says:

    Hi Eric,
    Another great post but it all seems so hard for a non-techie person. Many of us work alone and in solitude so when this happens, where is the best place (that you would recommend) to find a techie-friend to remove the hack please?

  2. Mike says:

    Hi Eric,

    Nice post!

    I had one of my sites hacked once and I’ll tell you that it isn’t something you want to see when you bring it up. That was real noticeable because it was blatant and you could see that it was hacked.

    But there are the hacks that you can’t see that is bothersome because you really don’t know they’re there until you have some reason to investigate a problem.

    Thanks fro this post. I will bookmark this one for sure.

    Mike

  3. Eric Giguere says:

    @Jan:

    Just look around in your circle of friends. Or, better yet, start learning how to do these things yourself. It’s not that hard, really!

  4. Spanie says:

    Hello Eric,

    I was wondering could you reccomend some wp plugins which will help us all to prevent our blogs from beeing hacked? There are many people who doesn’t know php at all and it’s really difficult for them to backup their sites.

    Cheers.

  5. I’m seeing this more and more with not just WordPress sites. Even when I check the sites, it’s getting more and more difficult to find the source and the hacks are getting more difficult to remove.

    I’ve checked the access logs of some of the affected sites and there is no sign of any access reported. All plug-ins are updated, server is secured and changing passwords seems to secure things temporarily, but they keep popping up.

    It’s just frustrating.

  6. There are also plugins that can perform a vulnerability search for you. These will save you quite some time.

    Most of the time, you’ll see the malicious code hidden in the theme files, as the rest will not persist after a reinstall.

  7. Brian Maher says:

    Google will not be too sympathetic to companies who do not secure their website – get blacklisted and pay the price

Leave a Reply

*