PortGate

Guides · WordPress · 3 min

How to Find and Remove a Webshell on a WordPress Site

A step-by-step way to find PHP backdoors in uploads, plugins and core, quarantine them safely, and close the hole they came through.

A webshell is a small PHP file that lets someone run commands on your server through a web request. On WordPress it usually lands in wp-content/uploads/, a cache folder, or inside a plugin that has not been updated. The site often keeps working normally, which is why it can sit there for months.

This guide is the order we use: confirm, find everything, quarantine, close the entry point, then check for the ways attackers come back.

1. Take a snapshot before you touch anything

Before you delete or move a single file, copy the site so you can compare later and so you do not destroy evidence you may need.

sudo tar czf /root/site-$(date +%F).tar.gz -C /var/www example.com
sudo cp /var/log/nginx/access.log* /root/   # or /var/log/apache2/

If the site is on shared hosting, use the host's backup tool or download the files over SFTP.

2. Look for PHP where PHP should never be

WordPress never needs PHP files in the uploads folder. Any you find there are suspicious.

cd /var/www/example.com
find wp-content/uploads -type f -name '*.php*'
find wp-content -type f -name '*.php' -newer wp-config.php -mtime -30

The second command lists PHP files changed in the last 30 days that are newer than wp-config.php. Attackers often copy timestamps from nearby files, so treat this as a hint, not proof.

3. Let WordPress compare its own files

WP-CLI can check core and plugins against the official checksums from wordpress.org:

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

Any core file that "doesn't verify" or any file that "should not exist" deserves a look. Premium plugins that are not on wordpress.org cannot be verified this way; compare them with a fresh download from the vendor instead.

4. Search for the patterns webshells share

Most webshells take input from the request and pass it to something that runs code. Search for those combinations rather than single words:

grep -rlE --include='*.php' 'eval\s*\(\s*(base64_decode|gzinflate|str_rot13|gzuncompress)' wp-content
grep -rlE --include='*.php' '(system|shell_exec|passthru|proc_open|popen)\s*\(\s*\$_(GET|POST|REQUEST|COOKIE)' wp-content
grep -rlE --include='*.php' 'preg_replace\s*\(.*/e' wp-content

Expect some false positives. Legitimate plugins use base64_decode and eval occasionally. What makes a file dangerous is several signals together: hidden code, request input, a code-running function, and a location like uploads. That is how the free webshell analyzer scores a file, and you can paste a suspicious one there to get a second opinion.

5. Quarantine, do not delete

Move suspicious files out of the web root and make them unreadable. If you were wrong, you can put them back.

sudo mkdir -p /root/quarantine
sudo mv wp-content/uploads/2026/12/cache.php /root/quarantine/
sudo chmod 000 /root/quarantine/cache.php

6. Check for the ways back in

Removing the file is only half the job. Attackers usually leave a second door:

  • Admin users you do not recognise: wp user list --role=administrator
  • Cron jobs that download the shell again: crontab -l -u www-data, ls /etc/cron.d, and wp cron event list for WordPress's own scheduler
  • Code in the database, often a script tag in wp_options or post content: wp db search '<script' --all-tables
  • Must-use plugins in wp-content/mu-plugins, which load without appearing in the plugin list

7. Close the hole and reset secrets

Find the entry point in your access logs. Search for POST requests to the file you found, and look at what happened just before it first appeared:

sudo grep -h 'cache.php' /root/access.log* | head

Then:

  1. Update or remove the vulnerable plugin or theme.
  2. Change every admin password and the database password in wp-config.php.
  3. Replace the salts so all sessions are logged out: wp config shuffle-salts.
  4. Stop PHP from running in uploads. On nginx, inside the server block:
location ~* /wp-content/uploads/.*\.php$ {
    deny all;
}

On Apache, put this in wp-content/uploads/.htaccess:

<FilesMatch "\.php$">
    Require all denied
</FilesMatch>

When to rebuild instead

If you find a webshell plus a new system user, changed SSH keys, or a process running as root that you cannot explain, the attacker likely had more than web access. Cleaning in place is then a guess. Build a fresh server, restore the database and wp-content after checking them, and move the domain over.

How PortGate helps

The Malware & Backdoor Guard runs these checks on a schedule on every server you install it on. It scores PHP files on several signals, flags PHP in upload folders, watches cron and systemd for jobs that download and run code, and gives you a quarantine command that never deletes anything. Findings stay open until the file is gone, so a shell that comes back is reported again.