Before you start
This guide assumes the site is confirmed infected and you have SSH or SFTP access. If you are still working out whether it was hacked, start with what to do when a WordPress site was hacked: it covers keeping a copy, locking attackers out and finding how they got in.
Have these ready:
- A copy of the infected site (files and database), kept off the server. You may need to compare against it.
- A maintenance page if the site is redirecting or infecting visitors.
- A list of installed plugins and their versions, so you can reinstall the same versions:
wp plugin list --fields=name,status,version > ../plugins-before.txt
wp theme list --fields=name,status,version > ../themes-before.txt
(The ../ keeps the lists outside the web folder on most hosts.)
- Fresh zips of premium plugins and themes from the vendors’ sites, never from the infected server.
WP-CLI makes this much faster. Every step below also works with SFTP and a file manager, only slower.
Step 1: replace WordPress core
Compare core files with WordPress.org’s checksums:
wp core verify-checksums
It lists every core file that differs (“File doesn’t verify against checksum”) and extra files inside wp-admin and wp-includes (“File should not exist”). Add --include-root to have it warn about anything in the root folder that is not WordPress’s. Then rewrite core from a fresh download of the same version:
wp core download --force --skip-content --version=$(wp core version)
--skip-content leaves wp-content alone, and wp-config.php is never part of the download. The command overwrites changed files but does not delete extra ones, so delete the files verify-checksums said should not exist, and look at any unknown PHP file in the root folder (odd names such as wp-l0gin.php or random letters are a giveaway).
Without SSH: download the same version from WordPress.org, delete the site’s wp-admin and wp-includes folders, upload the fresh ones, and overwrite the root files except wp-config.php.
Step 2: replace plugins and themes
For plugins from WordPress.org, checksums tell you which ones were changed:
wp plugin verify-checksums --all
Reinstall each changed plugin at the version it had:
wp plugin install wordpress-seo --version=24.1 --force
Premium plugins have no public checksums: delete the folder and upload the vendor’s fresh zip. Do the same for the theme; a child theme has no clean copy anywhere, so read its functions.php and other PHP files by hand.
Then compare what is on disk with what WordPress lists. Some malware hides its own plugin from the Plugins screen:
ls wp-content/plugins
wp plugin list --field=name
A folder in the first list but not the second needs a look. Delete plugins and themes nobody uses: they can still be attacked while switched off.
Step 3: hunt what cannot be replaced
PHP in the uploads folder
find wp-content/uploads -type f -name "*.php"
grep -rl "<?php" wp-content/uploads --include=*.jpg --include=*.png --include=*.gif --include=*.ico
The second command finds “images” that are really code. A few plugins keep small PHP files in their own uploads subfolder; everything else should go.
Must-use plugins, drop-ins and hidden folders
Files in wp-content/mu-plugins/ run on every page and never appear in the normal plugin list. Drop-ins (advanced-cache.php, object-cache.php, db.php and similar in wp-content/) also load early. Check that each one belongs to a cache or host tool you know. Then look for hidden folders, a favorite hiding place:
find wp-content -type d -name ".*"
wp-config.php
Read it top to bottom. It should hold database settings, keys and salts, a few define() lines and the line that loads wp-settings.php. Anything with eval(, base64_decode(, gzinflate(, an include of another file, or a very long string is suspect. Compare with wp-config-sample.php if you are unsure what belongs.
.htaccess, .user.ini and php.ini
Look in the root folder and in wp-content for:
auto_prepend_filepointing at a file you do not recognize (it runs that file before WordPress on every request; security plugins use it too, so check whose it is);- rewrite rules that send visitors from search engines or phones to another site;
- rules that let images or text files run as PHP.
Recently changed files and known patterns
find . -type f -name "*.php" -mtime -30 -not -path "./wp-content/cache/*"
grep -rlE "eval\(base64_decode|eval\(gzinflate|eval\(str_rot13" --include=*.php wp-content
Modification times can be faked, and honest libraries sometimes use these functions, so treat matches as leads, not verdicts. Compare a suspect file with the same file in a clean copy of that plugin.
Step 4: clean the database
Search the tables where injected scripts usually sit:
wp db search "<script" wp_options --one_line
wp db search "eval(" wp_options --one_line
wp db search "fromCharCode" wp_posts --one_line
Change wp_ if the site uses another table prefix. Posts legitimately contain embeds and some scripts; look for code that loads from unfamiliar domains or is obfuscated. Fix it in WordPress (edit the post or setting) or with a targeted SQL update.
Then check people and schedules:
wp db query "SELECT ID, user_login, user_email, user_registered FROM wp_users ORDER BY user_registered DESC LIMIT 10;"
wp cron event list --fields=hook,next_run_relative,recurrence
The query reads the users table directly, so it shows the newest accounts even when malware hides them from the Users screen. A scheduled task with an unknown hook name may be what keeps putting files back.
Step 5: lock the doors behind you
- Change every password again: hosting, SFTP and SSH, database, every WordPress administrator.
- Sign everyone out with new keys:
wp config shuffle-salts. - Revoke unknown application passwords.
- Update every plugin, theme and core, and close the hole you found (an outdated plugin, a weak password, a neighbor site).
Step 6: scan again and watch for it coming back
Run your checks again an hour later and the next day. If files come back within minutes of deleting them, something is still running: another copy, a scheduled task, code in wp-config.php, or a different site on the same server. Check every site in the hosting account, not only the one that showed symptoms. If Google flagged the site, request a review once it is clean; our guide on what to do after a hack covers that.
Malware removal plugins and services
Security plugins can scan for known malware and some quarantine or repair files; many security companies and some hosts sell a cleanup service on top. They save time on a badly infected site. Whichever you use, ask the same questions: does it compare files with clean copies or only look for signatures, does it check the database and uploads, and does it tell you how the attacker got in? A cleanup that skips the last part tends to be repeated.
How to explain it to the client
Tell the client what was found, what you removed, how it got in and what changed so it cannot happen the same way. For example:
We found and removed malicious code in three places on harbourbakery.example: a fake plugin, a file in the image uploads and a script added to a site setting. It came in through an out-of-date gallery plugin, which we have replaced. All passwords have been changed and we will check the site daily for two weeks.
If customer data may have been reached, say so and suggest they get advice on what they must do next.
How Keptstone handles it
Keptstone gives agencies malware scanning and repair tools; it is not a done-for-you malware removal service. The malware scanner compares WordPress core with WordPress.org’s checksums, WordPress.org plugins and themes with their official copies, and premium plugins with the agency’s own Library zip or the agency’s other sites running the same version. It looks for PHP in uploads and hidden folders, odd statements in wp-config.php, risky server settings, scripts in the database and code around WordPress; files it cannot verify are read by Keptstone’s own malware engine on its server (wp-config.php never leaves the site).
Each finding has a repair where one is safe: restore a changed WordPress file from WordPress.org, reinstall WordPress core in one go, reinstall a changed plugin with a restore point first, or quarantine an unknown file (moved out of reach, and it can be put back). Database and server findings show the matching code to fix by hand. Known malware in uploads is quarantined by itself. Scans run daily, after anything critical such as a new administrator, and a file watch looks every 15 minutes in between.
The scanner is part of Care + Protect + Speed, $4 a site a month, with the firewall and virtual patches for holes found on your sites. Hardening, two-step login and the emergency lockdown are in both plans. Keptstone does not run backups; it records the host’s.
Questions
Can a plugin remove malware from WordPress?
A security plugin can find much of it and some can quarantine or replace infected files. It runs inside the infected site, so the malware can interfere with it, and it cannot tell you how the attacker got in. Use it as one check, then replace files from clean copies and close the hole yourself.
Why does malware come back after I delete it?
Something you did not remove puts it back: a second copy in another folder, a scheduled task, code in wp-config.php or a must-use plugin, or an infected neighbor site on the same hosting account. Or the hole it came through is still open. Find the source before deleting files one by one.
Do I need to reinstall WordPress to remove malware?
Reinstalling core files is the quickest way to be sure they are clean, and it leaves wp-content and wp-config.php alone. It is not enough by itself: most malware lives in plugins, uploads, the database or wp-config.php.
Is it safe to delete PHP files in the uploads folder?
Usually, because WordPress keeps only media there. A few plugins write small PHP files into their own uploads subfolder (caches, logs, empty index files). Check which plugin owns the folder before deleting, and move files aside rather than deleting them if you are unsure.