How To Completely Clean Your Hacked WordPress Installation
Posted on June 24th, 2008 at 10:11 am by Michael VanDeMar under On The Ball-ness, SEO, blogthropology, coding, how-to, web design
Getting hacked sucks, plain and simple. It can affect your rankings, cause your readership to be exposed to virus and trojan attacks, make you an unwilling promoter to subject material you may not actually endorse, and in many cases cause the loss of valuable content. However, once it happens it is usually best to not procrastinate on the clean up process, since a speedy restore will most times minimize the damage that was caused.
While almost all sources will recommend that you upgrade your WordPress to the latest version, what the majority neglect to tell you is that in most cases simply doing so will not prevent the attackers from getting back in, even if there are no known exploits with the latest version. The hackers may have left a back door file hidden in a directory where it wouldn’t get overwritten with an upgrade, or inserted code into your theme, or simply created an account that they then granted admin privileges to. Any one of those would allow them back in, even after you patched what was wrong the first time. Therefore I am providing this step by step process on how to completely clean out and restore a WordPress installation that has been hacked.
1. Backup the site and the database.
Even a hacked copy of your blog still probably contains valuable information and files. You don’t want to lose this data if something goes wrong with the cleanup process. Worst case scenario you can just restore things back to their hacked state and start over.
2. Make a copy of any uploaded files, such as images, that are referenced.
Images are generally exempt from posing a security risk, and ones that you uploaded yourself (as opposed to ones included with a theme, for instance) will be harder to track down and replace after things are fixed again. Therefore it is usually a good idea to grab a copy of all the images in your upload folder so as to avoid broken images in posts later. If you have any non-image files that could potentially have been compromised, such as zip files, plugins, or php scripts that you were offering people, then it is a good idea to grab fresh copies of those from the original source.
3. Download a fresh version of WP, all of the plugins you need, and a clean template.
Using the WordPress automatic upgrade plugin does make it easier to upgrade every time a new version comes out. However, it only replaces WordPress specific files, and does not delete obsolete ones. It also leaves your current themes and plugins in place, as is. This means that if used to upgrade a blog that has already been compromised, it can very well leave the attackers a way back in. It is best to start over from scratch as far as the files portion of your installation goes. Note that if you use the EasyWP WordPress Installer script that I wrote it saves you from having to download, unzip, and then upload all of the core WordPress files, although you will still need to grab fresh copies of the themes and plugins that you want to use.
4. Delete all of the files and folders in the WP directory, either through FTP (slower) or through cPanel’s File Manager (faster).
Now that you have fresh copies of all the files you need, and copied all of your uploaded images, completely delete the entire directory structure your blog is in. This is the only surefire way to completely remove all possibly infected files. You can do this through FTP, but due to the way that FTP handles folder deletion (ie. it walks the directory structure, stores each and every file name that needs to be deleted, and then sends a delete command for each one), this can be slow and in some instances cause you to get disconnected due to flooding the server with FTP commands. If available it is much faster to do this through either cPanel’s File Manager, or via command line if you happen to have shell access.
5. Re-upload the new fresh copies you just grabbed.
This step should be self explanatory, but I would like to mention that if your FTP client supports it (I use FileZilla, which does) and your host allows it, then increasing the number of simultaneous connections you use to upload can greatly reduce your overall transfer time, especially on servers or ISP’s where latency is more of an issue than bandwidth. In FileZilla this setting is found by going to “Edit -> Settings -> File transfer settings”:

Also, if not using the EasyWP WordPress Installer script, don’t forget to edit and rename your wp-config.php file (when freshly unzipped this is named wp-config-sample.php).
6. Run the database upgrade (point your browser at /wp-admin/upgrade.php).
This will make any necessary changes to your database structure to support the newest version of WordPress.
7. Immediately change your admin password.
If you have more than one admin (meaning any user with editing capabilities), and cannot get the others to change their passwords right then, I would change their user levels until they can change their passwords as well. If there is anyone in your user list that has editing capabilities, and you do not recognize them, it’s probably best to just delete them altogether. If changing passwords is something you hate doing, then maybe my new memorable password generator can make that a little less stressful for you.
8. Go through the posts and repair any damage in the posts themselves.
Delete any links or iframes that were inserted, and restore any lost content. Google and Yahoo’s caches are often a good source of what used to be there if anything got overwritten. The following query run against the database can help you isolate which posts you want to look at:
SELECT * FROM wp_posts WHERE post_content LIKE '%<iframe%' UNION SELECT * FROM wp_posts WHERE post_content LIKE '%<noscript%' UNION SELECT * FROM wp_posts WHERE post_content LIKE '%display:%'
If you did not change the default prefix for WordPress tables, than you can copy and paste that directly into a query window and run it, and it should pull up any posts that have been modified to hide content using any of the methods I have come across so far (iframes, noscript tags, and display:none style attributes). To get to a query window in cPanel, you would click on the MySQL® Databases icon, scroll to the bottom of the page, and then click on phpMyAdmin. Once the new window or tab opens, you would click on the database in the left hand side that your blog was in, and then in the right side at the top click on the SQL tab. Then just paste the query into the large text area and hit the Go button.
Note, however, that there may be other types of injected content that I haven’t seen yet, and that a manual inspection looking for the types of patterns that first alerted you to the fact that your blog was hacked is always a good idea.
If you have enjoyed what you read here, please consider subscribing to my feed.


June 25th, 2008 at 6:43 am
Very nice post bro, I hope you could have posted it lill before to help me out of this situation… lol
Anyhow, I am sure it will be very helpful to some else who is stucked in such situation. Thanks for the useful post
June 25th, 2008 at 2:57 pm
Image files can be used to carry both hidden information and executable code (malware). While I agree the risk is not great, you should be careful about everything you port forward from a hacked installation to a new installation, including images. If you have pre-hack copies, certainly use those instead. Also, the existence of many images you did not put up yourself may be evidence of the intent of the hacker. Your site may have been intended to be used as a distribution point or hub… and you should ask your host to help make sure the new setup is protected against that sort of abuse.
June 25th, 2008 at 3:14 pm
Actually, no, you cannot infect someone with a virus or other malware through an image. At worst a hacked blog might have their images defaced or destroyed, but that doesn’t actually pose a danger to the readers.
June 26th, 2008 at 7:38 am
Excellent post. Definitely worth my Digg.
There is one thing I would add to the list – use a vulnerability scanner on a regular basis.
Spend $50 a month and have someone scan your site every day so you know when is the next time you are vulnerable. I just thought about how much this hack had cost me including downtime when I make no sales, loosing customers and reputation, and the cost of recovery, assuming I have an updated backup…
It is definately helpful to know how to recover but it is more important to make sure you are not falling again.
June 26th, 2008 at 7:55 am
Great post! Unfortunately most folks don’t heed the advice to prepare and backup until something happens to them. Just like backing up a hard drive. But once it happens, they realize how important it is and never forget. It’s good to have the peace of mind that you have a plan in place to deal with something like this when it happens. I recommend printing up the post and keeping it as a Standard Operating Procedure for dealing with the possibility of being hacked.
Sounds like Atlaf just went through the same thing..I’m sure he’ll be prepared if it happens again thanks to your informative post. Keep up the good work! Enjoyable and informative blog on what can sometimes be a boring subject.
June 26th, 2008 at 2:46 pm
You might be getting hundreds of “thank you”s by the end of the year with this post. Thanks for laying out everything step by step. I have dozens of people forward this post to
June 26th, 2008 at 8:03 pm
@Michael VanDeMar there have been malware vectors abusing images files since 2002, and stegonographic manipulations since before that.
You misread me – I never said you would get a virus from the image. I said the image can carry malware – payload code, which can be executed if you have any of several existing Windows viruses on your system. Search perrun for an example.
In this context it was said that you can safely keep your images after you’ve been hacked. My caution was that they may have been modified, so if you have your originals, you are better off resoring them because images are not 100% “safe”. It is not best-practice to retain anything after a hack if you have other options.
July 1st, 2008 at 6:46 am
[...] been attacked, it’s time for the cleanup process. Smackdown has a comprehensive post about cleaning your hacked WordPress blog, so I’ll just link to it here. Obviously, I hope you never have to use that post, but if you [...]
July 3rd, 2008 at 6:22 am
Just say “wush wush go away hacker!” and there they fly away
No sorry, it is a pain in the ass, you should always have the last version and a managed server that is up to date, for my situation that would be sufficient. good luck! Aislin
July 3rd, 2008 at 8:46 am
Actually, Hypotheek, one of the reasons they keep coming out with new versions is because the older ones have security holes in them. Just having the latest version is never a guarantee that it is safe. The most you can say is that having an older version is pretty much a guarantee that you are not safe.
July 8th, 2008 at 1:13 am
Great info, who knows it might be coming handy one day (hopefully not)
Another way to prevent security loophole in WP or any other PHP based CMS is to tighten the security at the server level.
October 22nd, 2008 at 12:42 pm
Thats great info. I’ll try to run those SQL in my wp sites now and check them.
Thanks for the tips
November 28th, 2008 at 12:22 pm
Good info. Does anyone have some great plugin or tool to backup your WP a little faster?
Thanks, Kopen
January 15th, 2009 at 10:57 pm
I have had to deal with four or five wp hacks in the past year, so this post helps alot. Sometimes I dont know where to start.
January 20th, 2009 at 10:56 am
[...] Har du allikevel vært så uheldig å få WordPress bloggen din hacket, så fins det råd. Her er en fin gjennomgang av hva du bør gjøre. [...]
June 5th, 2009 at 12:01 pm
[...] vil være sikker på at du har fjernet alt som ikke skal være på bloggen din, kan du gå igjennom denne listen. Vil du vite hvordan en hacker jobber, er denne siden litt grei å lese, men dog veldig [...]
June 10th, 2009 at 8:16 am
Great post! i had a lot of hackproblems the past couple of years so this would help me a lot.
July 24th, 2009 at 3:52 pm
[...] LIKE ‘%noscript%’ UNION SELECT * FROM wp_posts WHERE post_content LIKE ‘%display:%’ (Thanks to Smackdown) 8. Download and install Secure plugin and Security scan plugin from [...]
August 25th, 2009 at 5:13 pm
This is really good to mentione! Most of the times this advice is for many too late. Thank you for sharing. Greetz, Jasper
August 26th, 2009 at 8:16 am
[...] Også Smackdown har en glimrende artikel om emnet. [...]
September 3rd, 2009 at 12:45 pm
Wow, great reference article, worked great right away.
September 4th, 2009 at 5:33 pm
[...] “How To Completely Clean Your Hacked WordPress Installation” by Smackdown is a good article on how to reinstall WordPress after being hacked, but take care to keep your export limited to the post content and comments (and Pages), not the entire database as the hack goes into the database. [...]
September 5th, 2009 at 12:55 am
[...] Baca How To Completely Clean Your Hacked WordPress Installation oleh Smackdown [...]
September 5th, 2009 at 3:41 am
[...] SmackDown – How to Completely Clean Your Hacked WordPress Installation [...]
September 5th, 2009 at 6:08 am
[...] If you've been hit with this already, then copying your posts and comments into a completely clean installation of WordPress seems to be the best way to deal with it. Simply upgrading now will most likely not deal with this (hackers know how WordPress upgrades work, and make the compromised files ones which are not over-written in an upgrade). Smackdown has more advice. [...]
September 5th, 2009 at 10:44 am
[...] innehåller dina inlägg, sidor, kommentarer och förhoppningsvis ingen hackad kod. Artikeln ”How To Completely Clean Your Hacked WordPress Installation” av Smackdown, är en bra artikel om hur du installerar WordPress efter att ha blivit hackad, [...]
September 5th, 2009 at 1:38 pm
[...] “How To Completely Clean Your Hacked WordPress Installation” by Smackdown is a good article on how to reinstall WordPress after being hacked, but take care to keep your export limited to the post content and comments (and Pages), not the entire database as the hack goes into the database. [...]
September 5th, 2009 at 1:42 pm
I’m going to do all this, thanks.
But while preparing all this, I at least cleaned my unauthorized admins out of my db with a few queries: http://spacefem.livejournal.com/555475.html
September 5th, 2009 at 3:02 pm
[...] Passwörter zu verwenden. Sollte es bereits zu spät sein, könnte dieser Artikel hilfreich sein: How to completely Clean Your Hacked Wordpress Installation Share this on del.icio.usDigg this!Stumble upon something good? Share it on StumbleUponShare this [...]
September 5th, 2009 at 5:03 pm
[...] my man Michael over at Smackdown has a great post on how to completely clean your WordPress installation if you’ve suffered an attack, however, with this particular attack you need to be sure that [...]
September 5th, 2009 at 5:51 pm
[...] How To Completely Clean Your Hacked WordPress Installation [...]
September 5th, 2009 at 7:36 pm
[...] How To Completely Clean Your Hacked WordPress Installation | Smackdown! (tags: wordpress security hack restore) This was written by andy. Posted on Sunday, September 6, 2009, at 1:35 am. Filed under Delicious. Bookmark the permalink. Follow comments here with the RSS feed. Post a comment or leave a trackback. [...]
September 5th, 2009 at 8:58 pm
[...] “How To Completely Clean Your Hacked WordPress Installation” by Smackdown is a good article on how to reinstall WordPress after being hacked, but take care to keep your export limited to the post content and comments (and Pages), not the entire database as the hack goes into the database. [...]
September 6th, 2009 at 12:36 am
[...] the “whole” database since the hack has gone deep into the WordPress DB. You can view, “How To Completely Clean Your Hacked WordPress Installation” by Smackdown for additional tips. It is a good article on how to reinstall WordPress after being [...]
September 6th, 2009 at 9:15 am
[...] “How To Completely Clean Your Hacked WordPress Installation” by Smackdown is a good article on how to reinstall WordPress after being hacked, but take care to keep your export limited to the post content and comments (and Pages), not the entire database as the hack goes into the database. [...]
September 6th, 2009 at 10:00 am
[...] http://smackdown.blogsblogsblo.....s-instal... [...]
September 6th, 2009 at 3:32 pm
[...] start, review Michael VanDeMar’s post on How to Completely Clean Your Hacked WordPress Installation. Much good info [...]
September 6th, 2009 at 4:36 pm
[...] Comment nettoyer complètement une installation WordPress contaminée [...]
September 7th, 2009 at 5:01 am
[...] Smackdown [...]
September 7th, 2009 at 10:52 am
[...] Da bør du ta en backup av alle bilder, videoer etc., så kjøre en eksport av brukerdata i XML-formatet til WordPress. Så innstallere WordPress på nytt. Så importere XML tilbake. Ja, dette var kortversjonen. Før du setter igang så anbefaler jeg deg å lese How To Completely Clean Your Hacked WordPress Installation. [...]
September 8th, 2009 at 6:45 am
Thanks for this great article. I’ve had to deal with three wordpress-hacks this year, so this post helps me a lot!
September 9th, 2009 at 8:15 am
I have been hacked and just found out. I know it was my fault as I did not update because of problems my daughter had when she updated, and I felt like things were going fine for me so did not do it.
I am sorry now, but also confused as to what to do. I am not as young as many of you, and so sometimes things scare me a bit!
I exported my XML that wordpress makes for you as well as all my images, but now do not know what I should do. I also forget how to install wordpress from the beginning which is what I think I need to do now, wipe out all the old and then put in a new, right?
Then, is it not safe to just import my old posts? I hate to lose everything, but do I need to start over and let the two or 3 years of posts get dumped?
UGH, I am upset.
September 10th, 2009 at 3:03 pm
[...] you have been hit, I feel for you. The fixes look aggravating. 2009 10 [...]
September 11th, 2009 at 12:26 pm
Hey
My site just went down in a way that has never happened before. I was not working on it either plus I had done some major marketing. It was a strange coincidence that I had emailed someone who deals with security, just before it happened and obviously competitors may have done it.
The site is http://www.csv2post.com now I was not too bothered to be honest its very new, not a lot of traffic but I’m dead certain it was a hack, even hosting thinks it was.
Anyone good at hacking want to offer a fee to fix the issue or are these hacker plugins around working enough and giving enough security to not bother paying?
Ryan
September 14th, 2009 at 4:44 pm
Thank you Michael for all these useful details. Unfortunately, most of people become wise just after a hacker attack… The main rule remain a constantly backup of your databases.
By upgrading your blog to the last version – and for the plugins also – it`s a way to keep away the common hackers attacks; but don`t forget, never you can`t be sure, this is the nature of the web and only with an early backup you keep in sure the most of your important dates.
September 15th, 2009 at 11:41 am
I cant thank you enough for helping me rescue several of my wordpress sites that were hacked.
Once again thanks
September 15th, 2009 at 2:23 pm
[...] http://smackdown.blogsblogsblo.....s-instal... http://codex.wordpress.org/FAQ_My_site_was_hacked http://www.journeyetc.com/2009.....-problems/ [...]
September 16th, 2009 at 9:58 pm
[...] in a very specific way to avoid updating with the worm’s ‘backdoor’ in tact. “How To Completely Clean Your Hacked WordPress Installation” by Smackdown is a good article on how to reinstall WordPress after being hacked, but take care [...]
September 17th, 2009 at 6:36 pm
I have a photography blog at http://shoots.wedding-photography-melbourne.com.au – I have just recovered from a hack that went deep into the database. I had to export my posts as an xml file from the hacked Wordpress site and import that file into a new, freshly created database… Arrgh. New passwords, redownload of the plugins and template files – absolutely everything new and clean. The call across the board is to stay current with your Wordpress installation and you’ll have less chance of problems.
September 24th, 2009 at 1:55 pm
[...] Cleaning: http://smackdown.blogsblogsblo.....s-instal... September 24, 2009 – 10:55 am | By Brian Thomas Clark | Posted in General, Site News | [...]
September 29th, 2009 at 10:03 pm
Before switching to 2.8.4, our site was compromised. The @*%$! spammers deployed two files to our system /wp-admin/fotter.php and /wp-admin/inclode.php (note the purposeful misspellings). These were encrypted files that were web-based backdoors. These were causing our theme footer to be overwritten nightly.
October 2nd, 2009 at 7:27 am
[...] http://www.spam-whackers.com/b.....rame-hack/ http://smackdown.blogsblogsblo.....s-instal... [...]
October 4th, 2009 at 10:02 am
[...] Lorelle on WordPress: Old WordPress Versions Under Attack Smackdown: How To Completely Clean Your Hacked WordPress Installation Wordpress Blog: How to Keep WordPress [...]
October 9th, 2009 at 7:10 am
Thanks very much for your help.
October 11th, 2009 at 6:30 am
I found a Virus that links to try-your-destiny.cn that was hiding in the file wp-content\uploads\js.cache\tinymce_f299bb0eff6f5bf98754a5f09bd63ddf.gz !
(eval(…) was hidden in that zip).
Deleting all the Wordpress Content didn’t helped, as I kept the wp-config and my upload folder!
So it is important to make step 2. as described above…
Best regards
Kfx
October 14th, 2009 at 10:24 am
[...] How To Completely Clear Your Hacked WordPress Installation Hardening WordPress Did Your WordPress Site Get Hacked? 20 WordPress Security Plugins (don’t [...]
October 15th, 2009 at 6:21 am
[...] How To Completely Clean Your Hacked WordPress Installation – ??? ?? ???????? WordPress ?? ??????????? ???????? ?? ??????. ????? ?? ???????? ?? ????? ??????. ?????? ?? ????? ????, ????? ?? ?????… [...]
October 25th, 2009 at 11:42 am
bad luck for me
my site hacked
i can’t open and redirect into another
and i can’t log in into my cpanel too
October 26th, 2009 at 8:32 pm
[...] How To Clean Up After A Hacker Attack [...]
November 2nd, 2009 at 4:12 pm
[...] you think you’ve been hacked, I’ve spotted a couple of useful guides to dealing with the aftermath. Wordpress, php hack, php, security, [...]
November 18th, 2009 at 6:01 pm
Mine was not hacked, but rather, the whole wp blog seems to have a lot of errors in it. Probably plugin incompatibility issues. Some plugins I was using before doesn’t seem to work anymore. And some features of the dashboard doesn’t work either, like it just shows a white, blank space in there, especially if I am installing themes, plugins.
So I think I’d just clean up my whole root folder, reinstall wordpress and upload my backup database. what do you think? will that solve the issues surrounding plugins, themes etc?
November 19th, 2009 at 3:05 am
[...] Note: If your blog has been hacked, you might want to consider having a look at this blog post; How To Completely Clean Your Hacked WordPress Installation. [...]
November 19th, 2009 at 12:21 pm
kristine, there is no way of knowing if that would fix it without knowing the errors or simply trying. It can’t hurt.
The thing is, if you are having incompatibility issues with the plugins, and you just reinstall the same ones, then you will most likely have the same issues. If you’re not hacked, then what you might want to do is simply deactivate all of the plugins first, and then slowly turn them back on, one by one, checking for the errors you are getting each time. That will help you isolate what is causing the problem (assuming that it is in fact related to your plugins). You should also go to Wordpress and check each plugin’s compatibility (ie. which versions they are supposed to work with).
November 25th, 2009 at 3:45 am
Good info bro to share. Recently, I have just cleaned up my blog folder after backed up my database. All plugin were lost then need to install one by one. It’s so mess. Any plugins management that you can propose? TQ.
December 7th, 2009 at 12:01 am
Thank you so much for this post. My sites got hacked and I did not know what to do. I went through all your steps and was able to almost restore my site. The last think I needed to do was update my .htaccess file and when I did that I got back all my posts….WAHHOOOOOOOOOOOOO
I have worked on one site for 3 years and had close to 600 posts on that site…I thought it was all gone. I literally worked on restoring this site for 12 hours today…and thanks to you and the steps above, I finally did it.
Thank you again
Sam
December 23rd, 2009 at 6:26 pm
Hello,
I need someone to help me explain this in more details and step by step. I’m completely newbie in this.
Thanks for your time
January 3rd, 2010 at 1:26 am
I will pay someone $250 to perform this as a service for me… Cleaning or getting rid of all of the bad coding and scripts on my blogs.
It is unfortunate that hackers and virus creators do things like this to intentionally and maliciously destroy other peoples hard worked for content. These individuals are like the scum of the earth. Who sits around all day creating malicious codes and scripts just to mess with people?? It’s people like this that end up seriously HAUNTED at the end of their lives by all of the wrong doing that they have done throughout their life. People that intentionally harm or wrong do others are like satans little lovely beings. Heartless. Soulless. Friendless. Must be a wonderfull world they live in…
There is a thing called benevolence, which holds the meaning of: possessing that in which an individual truly cares about the health and well being of other people. Not to sound “tree huggerish”. But seriously, come on, get a life and go do something significant rather than sitting in your cave all day and dying a slow, timely death. There is so much more to life than thinking that you are doing something cool by “being a hacker and ILLEGALLY getting into or intentionally destroying other peoples stuff”. Back in the old days you would have had the privilege of getting a bag put over your head with a noose around your neck and getting the stool kicked out from under you. It’s called “eye for eye”.
My apologies to everyone that reads this that are searching out how to fix your blog. It is obviously meant for the little, no good, no life having, soulless human maggot out there that creates viruses, malicious scripts and hacks other peoples stuff. YOU SUCK BIG MOOSE C#&@!!
Nick
January 24th, 2010 at 6:48 am
1, Only use plugin from wordpress.org unless you feel confident about the security of third-party plugins.
2, Use Secure WordPress plugin.
3, Remove the wp version from php files of theme. If possible, directly use static javascript file location instead of invoking php function since wp will automatically add the version number at the tail.
4, Have a nice neighbor on your hosting.
Just my 2 cents hehe
January 31st, 2010 at 3:56 am
thank yu so much for the article, few days ago all of my sites got hacked. I was lucky it was not xss. Bad part is got a trojan from a hacked advertiser on a very reputable network, when it installed it compromised smart ftp. All index,home and .jv got a malicious script inserted after page code. Took me days to clean up, going one by one. I wish I came across your post earlier
February 9th, 2010 at 2:33 pm
urrrgh!!!! Looks like we got our blog hit too X(
This was NOT on the schedule for today, but thanks for the layout of how to handle this hacked WP issue. It should go much smoother with this
February 14th, 2010 at 4:18 pm
Fantastic post. The SQL stuff saved me!
February 26th, 2010 at 10:34 am
[...] how to completely clean your hacked wordpress [...]
February 28th, 2010 at 6:25 pm
[...] http://smackdown.blogsblogsblo.....s-instal... [...]
March 3rd, 2010 at 11:21 am
[...] following “guest post” was a comment left on “How To Completely Clean Your Wordpress Installation” by a gentleman named Daniel J. Dick. He makes some excellent points, and due to it’s [...]
March 13th, 2010 at 11:27 am
[...] physically or virtually (I get a 404 trying to access it on the web), which makes sense since I did completely wipe and reinstall Wordpress several times last year. I also always check the wp_posts, wp_users, and wp_options (especially the [...]