Skip to content
Panel Newsletters Mail Account Choose a plan

How to Remove Malware from WordPress: Find and Clean Infected Code

How to Remove Malware from WordPress: Find and Clean Infected Code

To find malware on a WordPress site, compare: check core files against a clean archive of the same version (wp core verify-checksums or md5), the whole site against a backup taken before the breach, and separately look for PHP files in uploads, unknown administrators, cron events, mu-plugins and scripts in the database. Clean up in this order: maintenance mode, clean core with extra files removed, plugins from official sources, manual removal of findings, new keys, passwords and sessions, then repeat the same checks. On a copy of our test bench no single check found every trace of the simulated compromise, so one scanner or one command is not an answer.

How we tested

  • Environment: RichHost test bench, isolated container with 2 vCPU / 8 GB, Ubuntu 24.04, nginx 1.24, PHP 8.3-FPM, MariaDB 10.11, WordPress 6.7.2, WP-CLI 2.12.0, 25 October 2026. These are not RichHost production servers.
  • We worked on a separate copy: its own docroot, tables with the r30_ prefix and its own nginx port. The main bench site was not touched, and the copy was deleted completely after the test.
  • The “infection” used only harmless markers: a comment line added to wp-includes/version.php, a PHP file in uploads that only prints a string, an empty PHP file in wp-includes, an mu-plugin with no code, an administrator wpsupport_tmp, a script tag with a dummy example.invalid URL in a post and in an option, and a cron hook with no callback. No real malware was used.
  • Core reference: the WordPress 6.7.2 archive from the Ubuntu package (6.7.2+dfsg1-1.1ubuntu2) the bench was installed from; its SHA256 matched the official Ubuntu package index. The container has no outbound access to wordpress.org, so wp core verify-checksums and wp core download failed (output below), and we checked core with md5.
  • Sources checked on 25 October 2026: FAQ My site was hacked (WordPress.org, updated 26 July 2026), WP-CLI docs for wp core verify-checksums and wp plugin verify-checksums, Google’s Security issues report help page and Google’s guide How do I know if my site was hacked?.

What each check found on the “infected” copy

We took a clean backup of the copy, planted eight markers and ran the usual checks. The summary is in the graphic, the output of each command follows.

Results of eight WordPress checks on a bench copy with harmless compromise markers
Bench copy with harmless markers: only diff against a clean backup found every file change

Core checksums

wp core verify-checksums downloads md5 sums for the installed version from api.wordpress.org and compares them with the files on disk. According to the WP-CLI docs, a modified file shows up as “File doesn’t verify against checksum: wp-includes/version.php” and the summary as “Error: WordPress installation doesn’t verify against checksums.” On our bench the command never got that far because the server could not reach wordpress.org. Servers with restricted outbound traffic behave the same way, so a fallback is worth knowing.

$ wp core verify-checksums
Error: RuntimeException: Failed to get url 'https://api.wordpress.org/core/checksums/1.0/?version=6.7.2&locale=en_US': cURL error 56: CONNECT tunnel failed, response 403.

# manifest from the clean copy: 2785 core files, without wp-content and wp-config.php
$ find . -type f ! -path "./wp-content/*" ! -name wp-config.php -print0 | sort -z | xargs -0 md5sum > core.md5

$ md5sum -c --quiet core.md5
./wp-includes/version.php: FAILED
md5sum: WARNING: 1 computed checksum did NOT match

# md5sum only checks listed files, so look for new files separately
$ comm -13 <(cut -c35- core.md5 | sort) <(find . -type f ! -path "./wp-content/*" ! -name wp-config.php | sort)
./wp-includes/class-wp-cache-helper.php

Takeaway: md5sum caught the modified file but not the added one, because that file is not in the manifest. WP-CLI also warns about extra files in wp-admin and wp-includes, and with --include-root it checks the site root too.

Plugins: a “Success” that verified nothing

$ wp plugin verify-checksums --all
Warning: Failed to get url 'https://downloads.wordpress.org/plugin-checksums/akismet/5.3.6.json': cURL error 56: CONNECT tunnel failed, response 403.
Warning: Could not retrieve the checksums for version 5.3.6 of plugin akismet, skipping.
Success: Verified 0 of 1 plugins (1 skipped).

The command exited with code 0 and the word Success without verifying a single plugin. Paid and custom plugins, which have no checksums on wordpress.org, are skipped the same way. Neither mu-plugin was counted, including our unknown one: “0 of 1” refers to Akismet only. WP-CLI has no checksum command for themes at all. If you run the check from a script, look for “skipped”, not at the exit code.

New files: uploads, mtime and ctime

$ find wp-content/uploads -type f -name "*.php"
wp-content/uploads/2026/10/cache-3f9a1.php

$ curl -s http://127.0.0.1:8130/wp-content/uploads/2026/10/cache-3f9a1.php
R30 test marker

$ find . -type f -newer "$STAMP"          # STAMP created right after the backup
./wp-content/mu-plugins/wp-sys-cache.php
./wp-content/uploads/2026/10/cache-3f9a1.php
./wp-includes/version.php

$ touch -r wp-includes/plugin.php wp-includes/version.php   # how a change gets hidden
$ find . -type f -newer "$STAMP"
./wp-content/mu-plugins/wp-sys-cache.php
./wp-content/uploads/2026/10/cache-3f9a1.php

$ find . -path ./wp-content -prune -o -type f -cnewer "$STAMP" -print
./wp-includes/version.php

Three observations. Without an explicit deny rule nginx executed the PHP file from uploads (HTTP 200). The modification time (mtime) can be faked with one command, after which the modified file drops out of the “new” list. The change time (ctime) cannot be set that way, but it is noisy: after chown -R on wp-content, 272 files looked “new”. And find -mtime -1 returned 5 files, 2 of them legitimate (the bench’s own mu-plugin and wp-config.php). Treat timestamps as a hint, not as proof.

Users, cron, mu-plugins and the database

$ wp user list --role=administrator --fields=ID,user_login,user_email,user_registered
ID  user_login     user_email               user_registered
1   admin          admin@example.test       2026-10-25 14:34:52
3   wpsupport_tmp  wpsupport@example.test   2026-10-25 14:36:13

$ wp cron event list --fields=hook,next_run_relative,recurrence
wp_version_check     58 minutes 30 seconds   12 hours
r30_dummy_sync       59 minutes 52 seconds   1 hour
...

$ wp plugin list --status=must-use --fields=name,status
bench-heavy    must-use
wp-sys-cache   must-use

$ wp db search '<script' --all-tables-with-prefix
r30_options:option_value
130:<script src="https://example.invalid/r30-test.j
r30_posts:post_content
1:tart writing!</p> <!-- /wp:paragraph --><script src="https://example.invalid/r30-test.j

All four database markers were found, but only because we knew what to look for. A search for a script tag misses encoded injections, and a cron hook with a bland name is easy to overlook among the system ones. An unknown mu-plugin shows up in the must-use list: there is no button in the admin to deactivate it, and it loads on every request.

Comparing with a clean backup

$ diff -rq clean/files /var/www/R30
Only in /var/www/R30/wp-content/mu-plugins: wp-sys-cache.php
Only in /var/www/R30/wp-content/uploads/2026/10: cache-3f9a1.php
Only in /var/www/R30/wp-includes: class-wp-cache-helper.php
Files clean/files/wp-includes/version.php and /var/www/R30/wp-includes/version.php differ

Only diff -r found all four file markers, regardless of the faked mtime. There is one condition: the backup must predate the breach and be stored outside the site.

Cleanup: what we ran and what happened

The full sequence took 5.8 seconds on a copy with 22 posts. On a real site the time goes into analysing findings and locating a clean backup, not into the commands.

1. Core: overwriting does not remove extra files

$ wp maintenance-mode activate
Success: Activated Maintenance mode.          # front page returns 503

$ wp core download --force --skip-content --version=6.7.2
Error: Failed to get url 'https://wordpress.org/wordpress-6.7.2.zip': cURL error 56: CONNECT tunnel failed, response 403.

# instead: a clean archive of the same version, without wp-content and wp-config.php
$ rsync -rc --no-links --itemize-changes --exclude=wp-content --exclude=wp-config.php wordpress/ ./
>fcsT...... wp-includes/version.php
$ md5sum -c --quiet core.md5 && echo OK
OK
$ comm -13 <(cut -c35- core.md5 | sort) <(find . -type f ! -path "./wp-content/*" ! -name wp-config.php | sort)
./wp-includes/class-wp-cache-helper.php

# with --delete the extra file goes away
$ rsync -a --delete --itemize-changes clean/wp-includes/ wp-includes/
*deleting   class-wp-cache-helper.php
>f.st...... version.php

The overwrite restored the original version.php, but the added file stayed. WordPress.org says the same: reinstalling from the admin only overwrites existing files and leaves injected new ones, so replace wp-admin and wp-includes entirely. wp core download --force on a host that can reach wordpress.org also unpacks on top of existing files, so check for extra files afterwards or sync the folders with --delete.

2. Plugins, files and database records

$ wp plugin install akismet --force
Error: No plugins installed.                  # wordpress.org unreachable; folder taken from the clean backup

$ rm wp-content/uploads/2026/10/cache-3f9a1.php wp-content/mu-plugins/wp-sys-cache.php
$ wp user delete 3 --reassign=1
Success: Removed user 3 from http://127.0.0.1:8130.
$ wp search-replace '<script src="https://example.invalid/r30-test.js"></script>' '' r30_posts --dry-run
r30_posts  post_content  1  SQL
Success: 1 replacement to be made.
$ wp option delete r30_widget_cache
$ wp cron event delete r30_dummy_sync
Success: Deleted a total of 1 cron event.

Reinstall free plugins from wordpress.org and paid ones from an archive downloaded from the vendor’s account, not from a copy on the infected server. --reassign hands the deleted user’s posts to another user; without it they are deleted too.

3. Keys, passwords and sessions

$ wp config shuffle-salts
Success: Shuffled the salt keys.
# old admin cookie -> /wp-admin/
302 http://127.0.0.1:8130/wp-login.php?redirect_to=...&reauth=1
$ wp user session list admin --format=count
3
$ for u in $(wp user list --field=user_login); do wp user session destroy $u --all; done
Success: Destroyed all sessions.
$ wp user session list admin --format=count
0

# separate test: password reset only, keys unchanged
$ wp user reset-password admin --skip-email
Success: Password reset for 1 user.
# fresh cookie: 200 before the reset, 302 after

New keys in the wp-config.php settings immediately invalidated every user’s cookies, but the session records stayed in the database, so we removed them separately. A password reset also killed that user’s cookie, because the cookie is tied to the password hash, and again the session record stayed. --skip-email notifies nobody, so users will need “Lost your password”. Change the database password in wp-config.php, the hosting panel, FTP and SSH passwords separately, preferably from a computer you have checked.

4. Re-check

$ md5sum -c --quiet core.md5 && echo OK
OK
$ find wp-content/uploads -type f -name "*.php"      # nothing
$ wp user list --role=administrator --fields=ID,user_login
1   admin
$ wp plugin list --status=must-use --fields=name
bench-heavy
$ wp db search '<script' --all-tables-with-prefix   # nothing
$ diff -rq clean/files /var/www/R30
Files clean/files/wp-config.php and /var/www/R30/wp-config.php differ    # new keys

Symptom: where to start

Symptom First check Action
Spam redirects only for mobile visitors or visitors from Google curl -s -o /dev/null -w "%{http_code} %{redirect_url}" -A "Mozilla/5.0 (Linux; Android 14) Mobile" -e https://www.google.com/ https://your-site/ and URL Inspection in Search Console Do not trust your own logged-in browser. Look for HTTP_USER_AGENT and HTTP_REFERER conditions in .htaccess, theme files and options, then run the full set of checks
Google shows “This site may be hacked” Search Console, Security issues: sample URLs; a site:your-domain search Remove the spam pages and the cause first, then request a review. Google’s help says it takes from several days to several weeks
Unknown administrator wp user list --role=administrator with user_registered, access log for that time Delete with --reassign, find the file or plugin that created the account, rotate keys and passwords
Host suspended the account Ask the host for the list of flagged files, processes or messages and the detection date Get a copy for analysis, clean by that list plus diff against a backup, tell the host after the re-check
Sudden CPU or outgoing mail spike PHP processes and the mail queue; find -newer, wp cron event list, PHP files in uploads Stop the source with maintenance mode, then the full cycle. For the server side, go through the common server security holes

What no single check can guarantee

  • A comparison only shows differences from the reference. If the backup was taken after the breach, diff finds nothing.
  • Checksums cover core and wordpress.org plugins. Themes, paid plugins, mu-plugins, uploads and the database are outside them, and when wordpress.org is unreachable plugins are skipped with a Success message.
  • File modification times can be faked, and ctime is cluttered by legitimate chmod and chown.
  • Real malware is rarely as obvious as our markers: chains of eval and base64_decode, randomly named files in uploads, code before the opening PHP tag, .htaccess rules keyed on User-Agent and Referer, auto-loading through PHP settings in .user.ini. A single-string search only finds what you guessed.
  • Outside WordPress there are the system crontab, other sites in the same account and the computers used to log in to the admin.
  • A clean site does not mean a closed vulnerability. Without access logs and an answer to “how did they get in”, infections often come back.

After cleanup

Close the way in: update everything, remove unused plugins and administrators and block PHP execution in uploads. On our copy nginx ran such a file without complaint. The full list is in our guide to WordPress security settings, and what a host should provide for WordPress is covered in the WordPress hosting guide. If you are moving the cleaned site to a new server, see the RichHost hosting plans: NVMe disks, our own control panel with an AI assistant and free Let’s Encrypt SSL.

FAQ

Is wp core verify-checksums enough?

No. It only checks core. On our copy two of the four file markers and all four database markers were outside core.

Can I just restore a backup?

Yes, if it predates the breach. Right after restoring, update core, plugins and themes and rotate keys and passwords: the vulnerability is still in the backup.

Why delete sessions if the keys have already changed?

Old cookies stop working after a key change, but session records remain in the database. Clearing them with wp user session destroy leaves a clean list in which new logins are easy to spot.

Why does the malware come back after cleanup?

Usually something was left: a file outside core, a cron event, a hidden administrator, an extra file after a core overwrite, or the vulnerability itself. Repeat every check and use the logs to find how they got in.

More on this topic

All articles
US Web Hosting: a Server in the USA for North American Visitors
Hosting Types22.11.2026

US Web Hosting: a Server in the USA for North American Visitors

US web hosting from RichHost: one of four Tier III data centers, the same plans from $8 a month, NVMe, free SSL,…

Python Hosting for Django and Flask: SSH, Cron, NVMe from $8
Hosting Types22.11.2026

Python Hosting for Django and Flask: SSH, Cron, NVMe from $8

Python hosting at RichHost: Python on shared hosting plans, Django and Flask with MariaDB 10.11, SSH on request, cron every minute, Redis,…

Node.js Hosting: SSH, npm, Cron and NVMe from $8 a Month
Hosting Types22.11.2026

Node.js Hosting: SSH, npm, Cron and NVMe from $8 a Month

Node.js hosting at RichHost: Node.js on shared hosting plans with NVMe, SSH on request, npm via SSH, cron every minute, plans A3…