Skip to content

Rebuilding WordPress from a confirmed compromise to verified-clean prod

The site is compromised, the customer wants it back online. Rebuilding on the same host with the same database is the most common way to ship reinfection. Here is the ordered path that does not.

The forensics call is over. The site is compromised — administrator user injected three weeks ago, six PHP backdoors across uploads and plugins, a database row in wp_options containing obfuscated JS that injects into every page. The customer wants the site back online by close of business. The shortest path from here to “back online” is reinfection in 48 hours. The actual path — disposable hostname, clean install, selective re-import, verified clean before DNS flip — is longer but it is the only one that ends with a site you can hand back.

What’s happening

Operators rebuild WordPress sites three ways after a compromise, and only one of them actually clears the infection:

  1. In place “cleanup.” Delete the obvious malicious files, change a few passwords, claim done. The backdoor that survives is the one that wrote a scheduled task into wp_options or a hook into wp_cron, which re-creates the injection on first cron tick.
  2. Restore from yesterday’s backup. The compromise has been there for three weeks. Yesterday’s backup contains the same backdoor. You restored to the same compromised state on a fresh disk.
  3. Rebuild on a parallel hostname from clean sources, then cut over. This is the only sequence that actually removes the attacker’s persistence.

The attacker’s persistence layer is what you have to break, and it lives in more places than the files. A list that holds for nearly every WordPress compromise we have seen:

  • Modified core files (wp-login.php, wp-includes/load.php, wp-config.php)
  • Backdoors in plugin/theme PHP, frequently in wp-content/uploads/<year>/<month>/
  • Rogue administrator users in wp_users / wp_usermeta
  • Hostile rows in wp_options — active_plugins, siteurl, custom autoload options carrying base64
  • Scheduled tasks in wp_options('cron') that re-create modified files on a timer
  • Modified .htaccess and nginx.conf includes
  • A second site (or subdirectory) installed in wp-content/uploads/ that the attacker uses as a fallback
  • Reused passwords for the database, the admin user, and any reused-elsewhere service account

What does NOT carry the persistence: clean content. Posts, pages, products, the media library’s image files — these can be re-imported. The trick is selectively re-importing content without re-importing the backdoors that lived next to it.

How to verify

You are verifying two things separately: the scope of compromise on the old environment, and the cleanness of the new environment before cutover.

Scope, on the compromised box:

# Modified core files vs upstream
wp core verify-checksums --skip-themes --skip-plugins 2>&1 \
  | tee /tmp/core-modified.txt

# Modified plugin/theme files vs the WP.org tarballs
wp plugin verify-checksums --all 2>&1 | tee /tmp/plugin-modified.txt
wp theme verify-checksums --all 2>&1 | tee /tmp/theme-modified.txt

# PHP under uploads/ (uploads should never contain executable code)
find /home/<account>/public_html/wp-content/uploads \
  -type f \( -name '*.php' -o -name '*.phtml' -o -name '*.php5' \) \
  | tee /tmp/uploads-php.txt

# Recently created/modified files, last 30 days
find /home/<account>/public_html -type f -mtime -30 \
  -not -path '*/cache/*' -ls | tee /tmp/recent-files.txt

For the database, the rows that hide attacker persistence:

-- Recently created administrators
SELECT u.ID, u.user_login, u.user_registered, u.user_email
FROM wp_users u
JOIN wp_usermeta m ON m.user_id = u.ID
WHERE m.meta_key = 'wp_capabilities'
  AND m.meta_value LIKE '%administrator%'
ORDER BY u.user_registered DESC LIMIT 20;

-- Autoload options carrying suspicious payloads
SELECT option_name, LENGTH(option_value) AS len
FROM wp_options
WHERE autoload = 'yes' AND LENGTH(option_value) > 8192
ORDER BY len DESC LIMIT 30;

-- Decode cron, look for unfamiliar hook names
SELECT option_value FROM wp_options WHERE option_name = 'cron'\G

Anything that came back unexpectedly in those queries is part of the scope. Write it down before you touch anything.

Cleanness, on the new environment, before cutover:

# Clean WP core, no bonus files
wp core verify-checksums --quiet && echo CLEAN

# No PHP under uploads
find /home/<account>/public_html/wp-content/uploads -name '*.php' -ls | grep . && echo DIRTY || echo CLEAN

# No unexpected admins
wp user list --role=administrator --fields=ID,user_login,user_email,user_registered

# No suspicious autoloaded options
wp option list --autoload=on --format=count

The fix

This is the ordered sequence. Skipping a step is what makes the rebuild fail.

# 1. Preserve evidence. Snapshot the compromised filesystem and DB before
#    touching either. You will want this for the post-mortem and for
#    proving what was and was not exfiltrated.
tar czf /backup/forensic/<account>-fs-$(date +%F).tar.gz /home/<account>/public_html
mysqldump --single-transaction --quick <db> | gzip > /backup/forensic/<account>-db-$(date +%F).sql.gz

# 2. Take the site offline cleanly. A maintenance page on the live hostname
#    while you work prevents the attacker from observing the rebuild and
#    re-injecting through a backdoor you haven't found yet.
#    (Easiest: redirect at the edge / web server, NOT in WordPress.)

# 3. Provision a fresh environment on a disposable hostname:
#    rebuild.acme.com, NOT www.acme.com. Fresh OS user, fresh DB user,
#    fresh credentials end to end. Do NOT reuse the old DB user
#    even with a rotated password — the user grants are tainted.

The new environment gets a clean install:

wp core download --path=/home/<account>/public_html
wp config create --dbname=<new-db> --dbuser=<new-user> --dbpass=<new-pass>
wp core install --url=https://rebuild.acme.com --title="ACME" \
  --admin_user=<new-admin> --admin_email=ops@example.com

Plugins and themes get reinstalled fresh from official sources — WP.org, the vendor’s authenticated download, or your private git mirror. You do not copy plugin or theme files from the compromised filesystem. That is the single most common rebuild mistake; even one carryover plugin can carry one backdoor.

# From the original plugin/theme inventory you captured earlier
for slug in $(cat /tmp/clean-plugin-list.txt); do
  wp plugin install $slug --activate
done

Content re-import, the careful version. Posts, pages, products from a clean dump:

# On the OLD compromised DB, export only content tables, schema-stripped
mysqldump --no-create-info --skip-triggers --skip-add-locks \
  --where="post_status NOT IN ('auto-draft')" \
  <old-db> wp_posts wp_postmeta wp_terms wp_term_taxonomy wp_term_relationships \
  > /tmp/content.sql

# Sanitize before importing — strip the script/iframe injections
sed -i -E 's#<script[^<]*</script>##g; s#<iframe[^<]*</iframe>##g' /tmp/content.sql

# Import into the NEW database
wp db import /tmp/content.sql

For uploads (media library files), copy only non-executable content:

rsync -av --exclude='*.php' --exclude='*.phtml' --exclude='*.php5' \
  --exclude='*.htaccess' \
  /home/<account>/public_html/wp-content/uploads/ \
  /home/<account-new>/public_html/wp-content/uploads/

Do NOT copy back: wp-config.php, .htaccess, wp_options.cron, wp_options.active_plugins, any custom mu-plugins (audit those manually first), or the wp_users / wp_usermeta tables. Recreate users from a known-good list — typically just the staff who actually need access — and force password resets and session invalidation on day one.

Verification before cutover:

  1. wp core verify-checksums clean
  2. wp plugin verify-checksums --all clean
  3. No PHP in uploads (find returns nothing)
  4. Admin user list matches the known-good list
  5. Frontend rendering on rebuild.acme.com matches expected (compare against a clean staging if available)
  6. Re-scan with the same tool that flagged the original compromise — Wordfence/Patchstack CLI scan, or your file-integrity baseline

Only after all six pass do you flip DNS. After cutover, leave the maintenance redirect off but keep the old environment paused — not deleted — for two weeks. If an indicator surfaces post-cutover, you still have the forensic copy and the paused old box for comparison.


Stack Harbor runs post-compromise rebuilds end to end — disposable hostname, clean re-install, selective content import, verified clean before cutover — under enterprise WordPress operations. If you are in hour two of an incident and need a second set of eyes, reach the team.