WordPress

Your WordPress site was hacked: a calm cleanup runbook, step by step

In this article8 sections

You open your site and find spam pages, a redirect to a strange address, a warning from the browser, or an email from your host saying the account has been suspended. Your first instinct is to start deleting things. Resist it for ten minutes. A hacked site is an incident, and the order in which you work decides whether you clean it once or find it infected again next week.

This runbook is the order I follow. It assumes a WordPress site on a server you can reach by SSH and, ideally, WP-CLI. Where it is useful I also say what to do from a hosting control panel.

First: contain, and keep the evidence

  1. Do not panic-delete. The files that look suspicious tell you how they got in. Keep them.
  2. Take a full backup of the current state, files and database, and store it somewhere safe. It is infected, and you still want it: for evidence, and in case your last clean backup turns out to be older than the infection.
  3. Stop the damage. If the site is serving malware or sending spam, put it into maintenance mode, or restrict access to your own address, while you work. A short outage costs less than a blocklisting. In NGINX:
# in the server block, temporarily
allow 203.0.113.10;     # your address
deny all;
  1. Change the passwords that matter now, from a clean computer: hosting panel, SSH/SFTP, database, WordPress administrators, and the email account that receives password resets. Do it early, because an attacker with an active session or an admin account can re-infect you while you clean.
  2. Write down the time the problem was first noticed and what you saw. It helps you pick the right backup later.

Step 1: Find out what is wrong, read-only

Go to the site’s folder and look before you touch.

Do the official core and plugin files still match? WP-CLI can compare them with the originals published by WordPress.org:

cd /var/www/example.com
wp core verify-checksums
wp plugin verify-checksums --all

Any file reported as modified or “should not exist” in WordPress core is a strong signal. (Plugins and themes from outside the official directory cannot be verified this way; compare them with a fresh download from the vendor.)

What changed recently? Files modified in the last two weeks, newest first:

find . -type f -mtime -14 \( -name '*.php' -o -name '*.js' -o -name '.htaccess' \) -printf '%TY-%Tm-%Td %TH:%TM  %p\n' | sort -r | head -40

Legitimate updates show up here too; what you are looking for is a file that does not belong, or a time that does not match any update you made.

PHP files where there should be none. The uploads folder holds images and documents, not code:

find wp-content/uploads -type f -name '*.php'
find wp-content/uploads -type f \( -iname '*.ico' -o -iname '*.jpg' -o -iname '*.png' \) -print0 | xargs -0 grep -l '<?php' 2>/dev/null | head

Typical malware patterns:

grep -rIl --include='*.php' -E 'eval\(base64_decode|gzinflate\(base64_decode|str_rot13\(|assert\(\$_(POST|REQUEST)|preg_replace\(.*/e' . | head -30

Some legitimate software uses these functions, so treat hits as leads rather than verdicts. A file with a long unreadable string and a call to eval in a folder it should not be in is the classic backdoor.

Must-use plugins and the drop-in files are loaded without appearing in the plugin list, which makes them favourites for persistence:

ls -la wp-content/mu-plugins 2>/dev/null
ls -la wp-content/*.php wp-content/advanced-cache.php wp-content/object-cache.php 2>/dev/null

Users and settings:

wp user list --role=administrator --fields=ID,user_login,user_email,user_registered
wp option get siteurl
wp option get home
wp option get active_plugins --format=json
wp cron event list

An administrator you do not recognise, a changed siteurl, or a scheduled event you cannot explain are all findings.

The database. Injected content is often stored in posts and options:

wp db query "SELECT ID, post_title FROM wp_posts WHERE post_content LIKE '%<script%' OR post_content LIKE '%<iframe%' LIMIT 20;"
wp db query "SELECT option_name FROM wp_options WHERE option_value LIKE '%eval(%' OR option_value LIKE '%base64_decode%' LIMIT 20;"

(If your table prefix is not wp_, use yours.)

The web server’s own files. On Apache hosting, look at .htaccess in every folder for rules you did not write: redirects to unknown sites, or php_value auto_prepend_file, which runs a script before every page:

find . -name '.htaccess' -exec grep -lE 'auto_prepend|RewriteRule.*http' {} \;

Step 2: Understand how they got in

If you only remove the malware, you will get it back. Look for the entry point:

  • An outdated plugin or theme with a known vulnerability. This is the most common cause. Compare the plugins you run with their current versions and the vendors’ security notices. Note the plugin that was abandoned or removed from the directory.
  • A stolen or guessed password. Check the web server’s access log for repeated requests to wp-login.php or xmlrpc.php around the time of the first changes, and for logins from unfamiliar addresses.
  • Another site on the same hosting account. On shared hosting, one infected site can write into its neighbours. If you run several sites in one account, check all of them, including old test installations. This is the cause people miss.
  • A compromised developer machine or a leaked key. Passwords saved in a file manager, an FTP client or a repository.
  • A nulled (pirated) theme or plugin. They very often ship with a backdoor.

Look at the logs for the first suspicious request:

sudo grep -E 'POST /wp-content/(uploads|plugins)/.*\.php|POST /wp-login.php|POST /xmlrpc.php' /var/log/nginx/access.log | head -30

Step 3: Clean

Prefer replacing over repairing. Cleaning one infected file by hand is a poor way to be sure you got everything.

  1. Reinstall WordPress core over the top, without touching your content:
wp core download --force --skip-content --version=$(wp core version)
wp core verify-checksums
  1. Reinstall every plugin and theme from a trusted source:
wp plugin install <slug> --force
wp theme install <slug> --force

For premium or custom plugins, upload a fresh copy from the vendor. Delete plugins and themes you do not use.

  1. Remove what should not exist: the PHP files in uploads, unknown files in the web root, unknown must-use plugins and rogue .htaccess rules. List first, delete exactly what you listed.
  1. Clean the database of injected posts and options you found, and remove unknown administrators.
  1. Rotate secrets. Generate new keys and salts (this logs everyone out), then give every real administrator a new password:
wp config shuffle-salts
wp user list --role=administrator --field=ID
wp user reset-password <ID> --skip-email

Run the last command for each administrator ID from the list; it prints the new password once, so store it in your password manager straight away. Do not reset every user on the site unless you must (customers will be locked out and will write to you). Make sure two-factor authentication is on for administrators, then change the database password and update wp-config.php.

  1. Check the permissions so that PHP cannot overwrite your code: files 644, folders 755, wp-config.php 640 or 600, owned by the deploy user, writable by the web user only where uploads need it.

Step 4: Verify and re-open

  1. Run the verification again: wp core verify-checksums, wp plugin verify-checksums --all, the find and grep commands from Step 1. They should come back clean.
  2. Scan from outside. Use your browser, curl -A "Googlebot" https://example.com/ (some malware shows spam only to search engines), and site:example.com in a search engine to look for indexed spam pages.
  3. Check blocklist status in Google Search Console (Security issues) and request a review if the site was flagged. Check your email-sending domain on a blocklist checker if spam was sent.
  4. Remove the maintenance restriction and watch the access and error logs for the next few days.

Step 5: Stop it happening again

  • Update on a schedule, core, plugins and themes, and delete what you do not use.
  • Limit who can log in: fewer administrators, unique strong passwords, two-factor authentication, and a rate limit or country restriction on wp-login.php.
  • Disable file editing from the admin panel (define('DISALLOW_FILE_EDIT', true);).
  • Block PHP execution in uploads: in NGINX, location ~* ^/wp-content/uploads/.*\.php$ { return 404; }.
  • Keep sites separate. One site per account or per PHP pool where you can.
  • Back up off the server, encrypted, and test a restore. The backup you can restore is the one that saves you.
  • Monitor for file changes and for uptime, so the next problem reaches you before a customer.

If the site handles customer data

If the site takes payments, stores personal data or sends email to customers, a compromise can carry legal and contractual duties to tell the people affected, depending on where you and they are. Keep your evidence and logs, and take advice on what you must report. This is a place where a short conversation with a professional is worth more than a clever script.

When it is worth getting help

If the site is a business asset, if you cannot find the entry point, or if it keeps coming back after you clean it, hand it over. A careful cleanup is a few hours of methodical work with logs, checksums and a clean rebuild, followed by hardening so that it does not repeat.

I do this work: contain, find the cause, rebuild from trusted code, and harden. You can see my profile on Upwork and send me a short description, or read about WordPress and Laravel care. To keep a site on a hardened stack from the start, see SiteHarbour’s WordPress hosting, and the guide on securing WordPress on cPanel hosting.