Web 101: Fixed a hacked site and prevent it from happening again – Part 1

A client and Swank hostee emailed me a few days ago because her browser had notified her that her blog had been marked as suspicious by Google and she didn’t know how or why that had happened.  Further checking revealed that google had found malicious software being downloaded and installed without user consent.  Her site had been hacked and the hacker had added malicious code to her blog template.

Unfortunately, this can happen to anyone and there are a myriad of ways that a hacker can get into your site.  I believe in this instance that the hacker was able to guess her password, which was a very simple name.  So what is one to do if your site is hacked?  My client had no clue and I know that not everyone has a trusted designer or tech support that they can email with problems such as these (plus speed is key, so waiting around for help can be frustrating), so I thought I’d write up a checklist of things you should do to remove malicious code from your hacked site and prevent it from happening again (or ever if it hasn’t happened yet).  I’m specifically going to be using WordPress blogs as an example since almost my entire clientele uses WordPress, but most of these things can be applied to all content management systems.

CHANGE YOUR PASSWORDS. First things first when you discover your site has been hacked is to change your passwords FOR EVERYTHING.  You’ll absolutely want to change your blog password and your FTP/control panel passwords (if they aren’t the same).  But if you use the same or similar password for your email or anywhere else, you are going to want to change them as well.  Even if you’ve never been hacked, it’s good practice to change your passwords regularly, at the very least yearly.  Make your passwords as secure as possible and try not to use the same password for everything:

  • Use a combination of letters and numbers as well as lowercase and uppercase and possibly even some symbols
  • Try not to use recognizable names or dates/numbers
  • 6-8 characters is a good length (though the longer the better)

Here’s a password trick I learned a while back that has been invaluable to me.  It’s a way to make every password for every site you visit different, but also something that you can remember.  First think of a good base using the rules I mentioned above, like: Xd5ye8*K

It may be hard to remember at first, but you are going to be typing it over and over so you should have no problem memorizing it eventually.  Next, you’re going to add an identifier of the site your password is for, so you need to come up with a system.  Examples of this could be the first four letters of the site name or the first two and last two.  It doesn’t really matter how you want to do it, just come up with a rule that can be applied to all sites.  Once you’ve done that add the letters to front or back of the base password you already came up with.

So for flickr, you password would be: flicXd5ye8*K.  For gmail it would be gmaiXd5ye8*K. For facebook faceXd5ye8*K, and so on.

DELETE/CHANGE YOUR USERS’ PASSWORDS. The hacker could have registered themselves as a user on you blog so that they could get in again.  Or they could have changed the password for one of your users so they could log in under that username.  Click on Users in your WP admin and look over the list of registered users.  If you have too many users and don’t want to have to change them all, you might consider deleting them all (except for yourself, of course).  People can always re-register.

Look for users with suspicious or spammy looking emails and delete them.  Many of my blogs have been getting a lot of registration spam lately.  Delete these users immediately, specifically if any of the users have an email address like xzy@mail.ru or anything else that looks random or generic.

If you don’t have a lot of users, then if may just be better in the long run to turn that option off altogether.  Go to Settings and under Membership, un-check the box next to “Anyone can register”.

SEARCH YOUR THEME FILES.  Next you’ll need to find whatever the hacker added and take it out.  The first place to look, especially if you are a WordPress user, is your template.  In your WP admin, click on Design and then Theme Editor.  A list of all your theme files will be down the right side.  The main ones you want to check are header.php, sidebar.php, footer.php and index.php, but you will want to check every single file listed for anything suspicious.

So what is suspicious code?  Look for anything that looks like a bunch of garbled text/code, or maybe a bunch of links to spammy-looking sites.  Specifically look for anything that uses the eval() command, base64_decode(), k1b0rg or keymachine.de and delete these lines of code. (You may want to back up your theme files before doing this in case you accidentally remove something important.)

UPGRADE/REPLACE YOUR WORDPRESS FILES.  If you don’t have the most recent version of WordPress, upgrade immediately.  Here’s a tutorial I wrote if you need help. Even if you are current, you should replace all your files with a fresh install in case the hacker modified any of the files or added any new files to your WP folders.  This means completely removing your wp-admin and wp-includes folders and all of the wp-something.php files that are in the main WP directory.  DO NOT remove wp-config.php or the wp-content folder. Everything else is replaceable, though.

CHECK FOR SUSPICIOUS FILES IN (AND AROUND) YOUR WORDPRESS DIRECTORY.  You can access this via FTP or through your control panel file manager.  First look at your .htaccess file, which is in the main WP directory (or root as it’s called).  If you have nice permalinks (links to posts look like http://yoursite.com/2008/09/08/post-title/) it should look like this:

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

There probably won’t be much else in there unless you’ve specifically added something.  Though some plugins like WP-Super Cache or feed/site redirecting plugins do add things to this file, so just be careful about if you delete anything from this (again make sure you have a backup).

Since you didn’t delete your wp-content folder, you will need to check this for suspicious files as well.  The main place to look will be your uploads folder, where the pictures you upload to your blog are stored.  For most WP blogs this is in wp-content>uploads though some older blogs may not have the uploads folder.  They might also be separated into year and month folders.  Look through all the files and make sure they are the right file extensions.  Picture file extensions are .jpg, .gif, .png and bmp.  Delete anything that isn’t one of these extensions unless you uploaded it yourself.  Nothing with a .php file extension should be in your uploads folder.  Those kinds of files are most certainly bad.  Besides looking for weird file extensions, look for files that have strange/random names that you know you did not upload.  If you see a folder called js_cache with a file in it starting with tinymce_, that is supposed to be there, so don’t delete that.

After checking your uploads folder go through and also check your plugins and themes folders for the same kind of files.  Make sure to check the images folder in your themes.  You may want to reinstall all your plugins as well to make sure none of them had been modified either. (I’ve never seen that happen, but you never know).  I have seen and heard of files being added the cache folder if you have WP Super Cache or the image-headlines folder if you using that plugin (or ones like it), so those are definitely ones to delete and reinstall if you have them. Also, be sure to check any and all other folders and files in your root directory for anything suspicious that you know you did not put there.

Checking all these things may sound tedious, but if you know the exact day the hack happened, you can look for things that were added/modified on that day.

CHECK AGAIN. So you went through all your files and you think you got everything bad removed, but how can you be sure?  That’s where these handy plugins come in:

WordPress Exploit Scanner – This will scan your files and your database for suspicious activity.

WP Security Scan – This will scan your site and show you any vulnerabilities you have.  This is for more advanced users, as it doesn’t always tell you how to fix these vulnerabilities (it assumes you already know).  I’ll talk more in my next post about some of the simpler things that you can do to make your site more secure.

Also check out this article which goes into a bit more depth about some of the specific hacks that can happen to WordPress users and some fixes for them. (There’s some good tips in the comments as well.)

NOTIFY GOOGLE THAT YOUR SITE IS SAFE. Now that you’ve gotten everything removed and your site is safe again, how do you get google to remove that warning?  When I visited my client’s hacked site in Firefox 3, it wouldn’t let me view it.  It gave me a big red screen saying the site was dangerous.  Unfortunately, this doesn’t automatically go away once you remove the malicious files.  You need to notify google to re-scan your site and verify that it is safe again.  You can do this by putting in your URL here: //www.google.com/safebrowsing/report_error/?tpl=mozilla

You can also request a review using Google Webmaster Tools.  You have to register and verify your site first, so it’s a little more involved, but I think you may get faster results if you go through the effort. Here’s some more info about that straight from Google.

In my next post I will talk about some of the things you can do to keep your site secure and prevent it from being hacked.

Go to Part 2.

Comments

  1. This is a great resource for anyone who has a site that has been under attack from hackers. There is nothing more of a pain then getting your site hacked and having to fix it.

Leave a Reply