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.
UPDATE: 9. (still valid in 2015) If you are having issues cleaning the installation yourself
When I wrote this post back in 2008 I intended it to be a do it yourself guide for the non-techie. However, I do realize that some people would still rather a professional programmer perform many of the steps I outlined here. If anyone has had their WordPress installation hacked, and either is uncomfortable attempting to clean it on their own, or has tried to do so with no success, I am available on a case by case basis. Most cleanings can be performed in about one hour, two at the most. The time can vary depending on the size of the blog, the amount of customization to the original theme, and the number of plugins installed. Feel free to contact me here if you feel like you could benefit from my help. Please include the site and any details that you think might be relevant (pro theme, anything you may have tried on your own, etc.) in the contact form.
UPDATE #2: 10. A note on hosting.
This past year (2010) has seen multiple waves of attacks on people’s websites that happened not due to insecurities within the WordPress platform itself, as has historically been the issue, but rather due to vulnerabilities with the actual hosts. Some of the bigger names that were hit include GoDaddy, Rackspace Cloud, MediaTemple, and Network Solutions, for instance. It is very important that you use a host that is not only well versed in security, but one that is stable and has knowledgeable tech support as well.
Update #3 11/14/2012:
Please note: if you are currently hosting with either HostPapa or Netregistry and you are here because you were hacked then the following tutorial may not be sufficient. Please see this post for more details:Hosting with HostPapa or Netregistry and Hacked? Switch Hosts Now. (hacked by hacker)
My personal recommendation for shared hosting is Hostgator. It is where this blog and many other sites of mine are currently hosted. Yes, that is an aff. link, but I would recommend them even if it wasn’t. For a dedicated solution that is both affordable and robust I use The Planet, which is where I host Bad Neighborhood. Both companies are ones that I have been using for years without issues, and that I do recommend to my own clients when they find themselves dissatisfied with their current hosts. If you were hacked, and your WordPress was up to date when it happened, then a change of hosts is something you should consider looking into.
232 thoughts on “How To Completely Clean Your Hacked WordPress Installation”
So, my site is hacked and redirecting. When I export my wp data to xml I can see the offending hack. At the very end of the xml, is:
It is the script which is the problem.
problem is, I cannot figure out how to remove it from the existing site! I could rebuild the site the scratch, which seems like overkill. I would rather just find and remove that one line of code, but I don’t know how to find it in the database, and it doesn’t show up in any greps of the source files, etc.
shoot, that didn’t work, the code block I just posted didn’t show….
lets try this:
vytas – if that is showing in your xml export then odds are the injection would be in your database, not your files (although there may be a back door injected into your files as well). Try running this inside of a query window in phpmyadmin and see what comes up:
SELECT * from wp_posts WHERE post_content LIKE ‘%rr.nu%’
See if that helps narrow it down.
Thank you so much for this,working on hack attempts which effect wordpress blogs is very frustrating and now thanks to your amazing post which is really helpful it is now much easier.
MICHAEL! IVE been hacked and have no idea how to fix it within my server or anywhere else. I need your help! I read a thread about a guy named lixation that you helped fix the same issue. Please help in any way you can. TY
Hi. I was the administrator of a site that got hit by malicious script and has been named as an attack site by Google about 2 months ago. (www.pasmi.org) I copied the pages and posts to my new blog (above) that I am building and when I try to get into the text now it won’t let me open up the edit pages as they are connected to the pasmi site (ie attack site). How can I get access to this text now? I am no longer administrator of http://www.pasmi.org, so I can’t fix that site directly.
Appreciate your help. Cheers
@Penelope – I am unsure, because I can’t tell exactly what you mean when you say that the pages are “connected” to the old site, but my guess is that if you were to use a Search and Replace plugin to change all instances of the old domain to the new one in your blog then you would no longer have those issues.
I am not sure what is happening with mu URL. My database was hacked so after much deliberating I decided on a fresh re-install. However after 2 days of being clear of all WP files, Goolge are still redirecting my url to a malware prevention site. Is it possible that there are still files on my account I can’t find?
I am at the end of my tether now and I really don’t know what to do!
@Laura – it is possible that you missed something, but it is also possible that you caught everything and Google just hasn’t noticed yet. The results in the search engines are not live, and a good portion of your site needs to be re-crawled by Google before thy will remove the malware warning on their own. However, if you sign up for a Google Webmaster Tools account, add and verify your site through there, and then submit a Google Reconsideration Request through that control panel, in most cases Google will check your site within 12 hours and either clear the flag or let you know that you missed something somewhere. If you would like help with all of that it usually only take me about 15 minutes at most to do the whole process, feel free to hit me up via my contact form.
I appreciate your article. My wordpress site was hacked recently and I still haven’t been able to find a solution despite following all the instructions on this page. I started a help thread on wordpress.org with still no solution: http://wordpress.org/support/topic/pharma-hack-variant-anyone-experienced-it?replies=3#post-2932557
I’m not sure what to do! I’ve deleted my entire wordpress site and uploaded a fresh new copy of wordpress, 2012 theme, plugins.. but 2 hours later a “wp-main.php” file pops up in my folder which I cannot remove. If I delete it, it will reappear almost instantly.
I’m suspecting maybe my database is hacked, but I’m not sure what to do 🙁
Hoping you can provide some assistance.
Thanks for reading
I am looking for an expert to clean my server, a few of my sites are infected. Are you able to help please?
Thanks for your detailed tutorial. It really helped me a lot 🙂
thanks for this great information. i was looking for it since my wordpress dashboard is acting weirdo. i will be reinstalling fresh WordPress script. thanks for providing the right procedure. 🙂
Hi Michael, I contacted you via email yesterday but thought I would post this question on here for others benefit. Having had my website hacked by a malware that deleted all of the files on my server, when I come to: 4. Delete all of the files and folders in the WP directory, either through FTP (slower) or through cPanel’s File Manager (faster). – Should I delete all of the files in the public_html folder?
Right now my site is hacked! 🙁 – reseller host on webhosting buzz
Can anyone help me?
@Olly – it depends. If you have more than just WordPress in your public_html then everything needs to be examined one way or another. If you have other scripts, such as Joomla or a forum, then those too might have back doors on them.
I am hacked also to Russian porn site! Help! I have reseller hosting with certifiedhosting.com
Oh man. Is there any way you’d be willing to do these steps for a nonprofit that’s been hacked? I’d totally pay you, this sounds like gibberish to a non-savvy person like me….my nonprofit supports deployed troops, so if you’re interested in assisting….I’d give you a huge cyberhug. And the going rate for your time. Please please please…..
@Sandy – I emailed you. If you didn’t get it check your spam folder.
awesome article man ! well written and superb guidelines 😀
I tried to manually clean the php files from Filezilla, and like so many others, within hours they were modified again. Does anyone know if there is a plugin that can helps me to remove or check the wordpress installation ?
Please someone help me my website http://www.zombieinfoguide on a blue razor server was hacked by someone in Turkey. Blue razor has no back up because the hack is older then the back up. Blue razor told me that they files were in the server and to find a word press expert to try to get the site back. If anyone can help me please call me at toll free 1-888-578-7324