Skip to content

Cleaning a malware-infected WordPress install on cPanel

How to actually clean a malware-infected WordPress install on cPanel — what to scan, what to replace, what to rotate, and how to know you finished.

A compromised WordPress install is rarely cleaned by running a scanner and clicking “remove threats.” Real cleanup is a forensic exercise: identify the entry point, replace everything attacker-touched from clean source, rotate every credential the attacker could have seen, and put monitoring in place so you’d notice the next attempt. Anything less and the site reinfects within days.

What’s happening

A compromised WordPress typically has: PHP files in wp-content/uploads/, modified core files, mu-plugins that don’t appear in the plugins list, backdoors in .htaccess, scheduled cron jobs that fetch and execute remote payloads, modified theme functions.php, and new administrator accounts. Scanners catch some of this. The attacker who’s been on your server for two weeks has had time to bury things scanners don’t catch.

How to verify

Inventory the surface:

find ~/public_html/wp-content/uploads -name '*.php' -ls
find ~/public_html -name '.htaccess' -newer /etc/hostname
find ~/public_html -name '*.php' -newer /etc/hostname | head -30
ls -la ~/public_html/wp-content/mu-plugins/
crontab -l
wp user list --fields=ID,user_login,user_registered,user_email,roles

PHP files in uploads/ are nearly always malicious. Recently modified .htaccess files are suspicious. mu-plugins you don’t recognize are suspicious. Unknown admin accounts are conclusive.

The fix

  1. Isolate first. Take the site offline via .htaccess:

    Header set X-Robots-Tag "noindex"
    ErrorDocument 503 "Under maintenance"
    RewriteEngine On
    RewriteCond %{REMOTE_ADDR} !^203\.0\.113\.42$
    RewriteRule . - [R=503,L]

    Replace the IP with yours. Visitors get 503; you can still work on the site. Buys you time to investigate without serving malware.

  2. Snapshot for forensics. Tar the entire account and the database before you clean — you may want to look at the malware later.

  3. Replace core. Don’t try to clean modified core files — replace them. Download a fresh copy of the exact WordPress version, overwrite everything except wp-content/ and wp-config.php:

    cd /tmp
    curl -O https://wordpress.org/wordpress-6.5.tar.gz
    tar -xzf wordpress-6.5.tar.gz
    rsync -av --exclude=wp-content/ --exclude=wp-config.php /tmp/wordpress/ ~/public_html/
  4. Replace all plugins from source. Don’t trust any plugin file on disk — reinstall each one from the WordPress.org repo or the developer’s site. Same for themes:

    wp plugin list --field=name | while read p; do
      wp plugin uninstall $p --deactivate
      wp plugin install $p --activate
    done
  5. Audit mu-plugins. Anything in wp-content/mu-plugins/ that you didn’t write is suspicious. Move them out of the directory; the site won’t load them; investigate, then put them back only if you confirm provenance.

  6. Audit uploads for PHP files. They have no business being there:

    find ~/public_html/wp-content/uploads -name '*.php' -delete

    Also block PHP execution in uploads via .htaccess:

    <FilesMatch "\.php$">
        Require all denied
    </FilesMatch>
  7. Reset every credential and key:

    wp user reset-password $(wp user list --role=administrator --field=ID)
    wp user reset-password $(wp user list --role=editor --field=ID)

    Rotate wp-config.php salts (covered separately). Rotate the database user password. Rotate any plugin API keys (Stripe, PayPal, anything that takes a key from the WordPress admin).

  8. Delete unknown user accounts. Any admin account you don’t recognize, especially one created during the compromise window, gets deleted with content reassigned to your real admin:

    wp user delete <suspicious_id> --reassign=<your_id>
  9. Audit cron entries. cPanel > Cron Jobs. Anything fetching a URL or running a script you didn’t add is a backdoor. Remove.

  10. Scan with multiple tools — they catch different things. ImunifyAV (often available on cPanel), Wordfence Premium, MalCare, Sucuri Site Check. Run all, address every finding.

  11. After cleanup, the question is whether you missed something. The honest answer is “probably.” Watch logs aggressively for 30 days post-cleanup. Any modified file under wp-content/ or wp-includes/ outside a deploy window is investigated immediately.

Cleanup is best done as a fixed-scope engagement with someone who’s done it before. Stack Harbor handles this under managed enterprise WordPress hosting.