Restoring a Clean Copy of a Hacked Website
Restoring a backup can be the quickest way to undo a hack. But it only works if the backup is clean, and if you also close the hole the hacker used. Otherwise you've restored a site that can be hacked the same way again.
What a backup is
A backup is a digital copy of your website, kept so you can restore it if data is lost.1 It can sit in the cloud or on an external hard drive. Storing backups offsite is described as a more secure method.1
If you have none, you may not be able to recover your data after an attack.1 That's one reason this page has a route for sites without any backup.
How to know a backup is clean
Google's first test is plain. Check that the backup was created before your site was hacked.2
That means finding out when the hack began. Your notes from finding malware and unknown files help, such as the date of the oldest changed file or the first unknown account. A backup made after that date may carry the infection with it.
Restored, and the same problems came back? Then the backup wasn't clean enough, or the way in is still open. Check both.
Copy the infected site first
Before you restore anything, keep a copy of the current site, even though it's still infected. Google says to mark that copy as infected so nobody mixes it up with the others.2 You may need it to rescue recent content or to see how the attack worked.
Route one: clean and current
If your backup is both clean and recent, Google's steps are short.2
- Restore your backup.
- Install any software upgrades, updates and patches available. That covers the content management system, shop software, plugins and templates.
- Correct the vulnerability, which means closing the way the hacker got in.
- Make sure everything found while assessing the damage has been dealt with.
- Change the passwords again for every account tied to the site.
If the database was changed too, you can update each infected record or restore your last known database backup.3
Route two: clean but old
An older backup lacks your newest pages and posts. Google suggests restoring the older copy and updating the software and fixing the vulnerability as above.2
Then compare the clean backup with the infected copy using a site diff. That's a comparison, by hand or by tool, that lists what differs. You can upload new content you want to keep from the infected copy, after you've checked that it's clean. Finish with another round of password changes.2
Route three: no backup
Google says to make backups of the infected site, labelled as such, and then clean a copy rather than the live server.2 The steps run in this order:
- On the backup copy, not the server, fix file permissions if they were too lenient.
- Still on the copy, clean the files found to be compromised, and remove the new files the hacker created.
- Correct the vulnerability if it exists in your code or cracked passwords. Google adds that input validation libraries or security audits may help.
- Clean the hacker-modified database records on the copy.
- Change the passwords for all accounts related to the site.
- Remove software the site no longer uses.
- Do a clean installation, not just an upgrade. Google warns that an upgrade can leave infected files behind, which makes a repeat hack more likely.
- Transfer in only known clean files or database from the cleaned copy, and don't overwrite the freshly installed system files.
- Change the passwords one last time.
This is slow work. It's also the point where asking a qualified professional for help is worth considering.
After the restore
Restoring doesn't finish the job. Check the pages, change the passwords, and only then go back online. The full order of work is on the checklist page. Then make regular backups part of your routine, with automatic backups turned on if your host offers them.1
Where the facts come from
- Secure your website, Australian Signals Directorate (cyber.gov.au)
- Clean and maintain your site, web.dev (Google)
- Hacked with malware, web.dev (Google)