How to Clean a Hacked WordPress Site from SSH

Scanner plugins run inside WordPress, so they miss anything WordPress does not load and can be disabled by the malware itself. Cleaning from SSH gives you the whole picture. This is the procedure we follow. Work on a copy where you can, and keep the infected snapshot for later analysis.

1. Contain

cd ~/public_html
# Snapshot everything before you touch it
tar czf ~/infected-$(date +%F).tgz .
wp db export ~/infected-db-$(date +%F).sql
# Optional: maintenance mode while you work
wp maintenance-mode activate

Rotate credentials now: hosting panel, SFTP/SSH, database user and every WordPress administrator. Assume they are all known to the attacker.

2. Verify core and plugin files against clean sources

wp core verify-checksums
wp plugin verify-checksums --all

Any modified or extra file in core is suspect. The fastest fix for core is to replace it entirely:

wp core download --force --skip-content --version=$(wp core version)

Reinstall every plugin and theme from the official source rather than cleaning them file by file. Remove anything nulled, abandoned or unused.

wp plugin install $(wp plugin list --field=name --status=active) --force

Premium plugins that are not on wordpress.org need fresh copies from the vendor.

3. Hunt for files that should not exist

# PHP in uploads is almost never legitimate
find wp-content/uploads -type f -name '*.php'

# Recently modified PHP files
find . -type f -name '*.php' -mtime -14 -printf '%TY-%Tm-%Td %p\n' | sort

# Common obfuscation patterns (review matches by hand; some plugins use these legitimately)
grep -rlE --include='*.php' 'eval\(base64_decode|gzinflate\(base64|str_rot13\(|assert\(\$_|preg_replace\(.*/e' .

# Must-use plugins and drop-ins are a favourite hiding place
ls -la wp-content/mu-plugins wp-content/*.php

Also check .htaccess files in every directory for injected redirects, and wp-config.php for added code at the top or bottom.

4. Clean the database

# Administrators you did not create
wp user list --role=administrator --fields=ID,user_login,user_email,user_registered

# Injected scripts in content and options
wp db query "SELECT ID, post_title FROM wp_posts WHERE post_content LIKE '%<script%' AND post_status='publish';"
wp db query "SELECT option_name FROM wp_options WHERE option_value LIKE '%<script%' OR option_value LIKE '%eval(%';"

# Siteurl/home hijack
wp option get siteurl && wp option get home

Delete unknown admins and their content, then reset salts so every session is logged out:

wp config shuffle-salts

5. Check scheduled tasks

Malware often re-infects from cron.

wp cron event list
crontab -l

Remove anything unfamiliar, especially events calling remote URLs.

6. Find the entry point

Without this the infection comes back. Check:

  • Web server access logs around the first modified file’s timestamp, looking for POST requests to unusual PHP files or plugin endpoints.
  • Plugins with known vulnerabilities: wp plugin list --update=available, then check each against a vulnerability database (Wordfence Intelligence, WPScan, Patchstack).
  • Other sites in the same hosting account. Cross-contamination is common on shared accounts without isolation.

7. Harden and request review

wp maintenance-mode deactivate
  • Update everything, remove unused plugins and themes, enforce 2FA for admins.
  • Add a WAF (server-level ModSecurity/Imunify360 or Cloudflare).
  • If Google flagged the site, request a review in Search Console under Security & Manual Actions once clean.

8. Monitor

Watch for new files, new admins and outbound spam for at least 30 days. A file integrity monitor alerting on changes in wp-content catches most reinfections within hours.

If the site keeps getting reinfected, the cause is usually persistence outside WordPress (cron, other accounts, compromised credentials) or the original vulnerability is still open. That is when a server-side investigation pays off.