Clean up after a hack
A site of yours has been hacked, and you need it clean and safe again.
This article describes how to restore from a backup, what to do when restoring isn't an option, and when you have to report the hack to the authorities.
The order to work in
Start by stopping the malicious code that's still running. Until you've done that, anything else you fix is undone within seconds.
Then:
- Restore from a backup taken before the hack, both the files and the database.
- Update the CMS, plugins and themes. A restored site still has the hole the attacker came in through.
- Replace every secret stored in the site.
- Remove users you don't recognise, in the CMS and in Core.
If the site stores personal data, the hack is also a data breach, and you may have 72 hours to report it. See Reporting a data breach.
Restoring is safer than cleaning by hand
An attacker almost never leaves one file behind. A typical hack leaves several: a backdoor inside a plugin directory, a second one in the uploads folder, a modified CMS core file, an extra administrator account, and sometimes a scheduled task that downloads everything again.
Cleaning by hand means finding every one of those yourself. Miss a single file and the attacker still has a way back in, and the site is hacked again within days.
Restoring from a backup replaces the whole site at once with a copy from before the hack. Everything the attacker added is gone, whether you found it or not.
Clean by hand only when restoring isn't realistic — for example a webshop, where going back to yesterday's backup would throw away a day of orders, customer accounts and stock changes.
1. Stop the malicious code that's still running
Do this first, on both routes.
Malicious code often runs inside a PHP-FPM worker that stays alive in memory. While that worker runs, it rewrites files the moment you replace them, so your cleanup or restore is undone within seconds.
Restart the FPM pool that serves the site. A restart kills every worker for the pool and starts fresh ones.
'Reload' is not enough: it lets the current request finish first, giving the malicious code time to write the files back.
See Restart and reload and File permissions keep reverting after a hack.
2. Restore from a backup
Restoring is done on the 'Backups' page of the UNIX user, the database, or the project. See Working with your backups.
Three things matter when the reason is a hack.
Pick an archive from before the hack
Not sure when the site was first compromised? Choose an older archive rather than a newer one. A backup taken after the attacker got in contains the attacker's files too, and restoring it leaves you where you started.
You can browse a files archive before restoring it, so you can check whether the backdoor is already in there.
Restore both the files and the database
Attackers plant things in both. A backdoor sits in the files; an extra administrator account, or injected content such as spam links, sits in the database.
Putting back only one leaves half the hack in place.
There may be nothing to restore from
Restoring only works for a site or database that has a Borg repository. Without one, the 'Backups' item is disabled and shows 'No backup repositories configured'.
In that case: ask Cyberfusion support whether a restore from their own backups is possible, and continue with the manual cleanup below meanwhile.
3. Update the CMS and everything around it
A restored site runs the same software version it ran before the hack, so it still has the same hole. Do not skip this step.
Update the CMS core, all plugins, all themes, and any framework or library your application uses, to a version that still gets security updates.
An outdated CMS or plugin is the most common way in. Attackers scan the internet for sites running versions with a publicly known weakness, so a site usually gets hit because of the version it runs, not because someone picked it out.
The 'CMS Versions' page under 'WebOps' lists every WordPress installation Cyberfusion found on your clusters and marks the ones past their security support. See CMS versions.
4. Replace the sensitive information stored in the site
Anyone who could read your files could read every secret in them. Treat all of them as known to the attacker:
- The database password, usually in a
.envfile or a configuration file such aswp-config.php. Change it under 'Advanced' > 'Database Users': select the database user, click 'Change' in the 'Password' panel. Then set the new password in the site's configuration file as well, otherwise the site cannot connect to its database. - API keys and tokens of external services — payment providers, mail services, shipping, CRM. Generate new ones with the service and revoke the old ones.
- Mailbox passwords, if the site sends mail with a mailbox password in its configuration.
The UNIX user's own password
Usually does not need changing. The attacker almost always gets in through outdated website software, not by logging in with that password.
Change it anyway if there is an indication the login itself was used. Every SSH login is recorded: the time, the account logged in to, the IP address, and the SSH key fingerprint. A login you cannot account for, or one from an unexpected country, is a reason to change the password and remove SSH keys you do not recognise.
See SSH logins on a node and Change or disable a UNIX user's password.
5. Remove users you don't recognise
Attackers create an extra account so they can get back in after you have patched the hole.
In the CMS: delete every administrator you did not create. Check the ordinary user accounts too — an attacker can raise a normal account's permissions later.
In Core:
- The site's FTP users. See All about FTP users.
- The site's SSH keys. See All about UNIX users.
- The site's crons. A cron that downloads the backdoor again is a common way for a hack to come back. See All about crons. If the site has shell access, also run
crontab -lover SSH.
WordPress: two extra steps
Remove plugins you didn't install
Delete every plugin you do not recognise — including the ones in wp-content/mu-plugins/.
Those are 'must-use' plugins. WordPress loads every PHP file in that directory on every single request, and there is no way to deactivate them from the WordPress interface — you have to delete the file. A backdoor placed there keeps running after you have deactivated everything else, which is exactly why attackers put it there.
Rotate the salt keys
The salt keys in wp-config.php are the secret values WordPress uses to sign login sessions. Replacing them makes every existing session invalid, so everyone logged in is logged out immediately — including an administrator account the attacker created, and the attacker's own browser session.
- Generate a fresh set at api.wordpress.org/secret-key/1.1/salt.
- Replace the corresponding lines in
wp-config.php. - Log in again yourself.
Check the malware list a day later
The cluster runs a daily malware scan (maldet) across UNIX user home directories. Hits appear under 'Malware' on the UNIX user page and the project page, with the file path and a 'Last Seen' timestamp. See Malware scanning.
Use the list to find malicious files you have not cleaned up yet. A record disappears once the file is gone from disk, so an entry that stays is a file that is still there.
The scan covers files modified in the last day, so give it a day after your cleanup and check again. An entry that reappears means the attacker still has a way in.
Reporting a data breach
If the site or application stores personal data, a hack is a data breach, and you have legal obligations beyond cleaning up.
Under the GDPR you must report the breach to the data protection authority without undue delay, and where possible within 72 hours of finding out about it. In the Netherlands that is the Autoriteit Persoonsgegevens; in other EU countries, that country's own authority.
You must also inform the people whose data was involved when the breach is likely to put them at high risk — for example when passwords, addresses or payment details were exposed.
This is the site owner's obligation, not the hosting provider's. If you build and manage sites for clients, agree in advance who reports what.
Organisations under NIS2 have further obligations. See NIS2 voor webbureaus, tech agencies en SaaS (in Dutch).
Related
- File permissions keep reverting after a hack — why your uploads and permission changes are undone within seconds.
- All about backups — the three backup tracks, and which one you can restore from yourself.
- CMS versions — which of your WordPress installations are past their security support.
- Security and auditing — what the server records about SSH logins, and what Core records about every change.
- Staging environments and cloning — a second copy of the site, for testing an update before putting it on production.