All ArticlesSecurity2026-11-21T15:10:19+08:00

Found a Webshell on Production? Deciding Whether to Clean the Server or Rebuild It

After finding a webshell, admins often delete the file and keep running without knowing whether the attacker left other access behind. This teardown covers the questions that settle clean versus rebuild: how the attacker got in, how far privileges reached, whether file integrity can be proven, and whether clean backups exist.

Someone finds a PHP file in /wp-content/uploads/ that should not be there. It accepts POST requests, runs arbitrary commands, and has been sitting on the production server for an unknown amount of time. The instinct is to delete it, run a malware scanner, and move on. That instinct is wrong more often than it is right. The file you found is almost never the only thing the attacker left behind.

This teardown walks through the investigation that should happen after discovering a webshell, and how the answers determine whether you can safely clean the server or whether you need to rebuild it from scratch.

Initial impact: what a webshell actually means

A webshell is not the attack itself. It is the persistence mechanism the attacker installed after the attack succeeded. The attacker already exploited something — an unpatched plugin, a file upload vulnerability, SQL injection, weak FTP or SSH credentials — and then dropped the webshell so they could come back later through normal HTTP traffic without needing the original vulnerability again.

This distinction matters because deleting the webshell does not close the door the attacker originally walked through. And if they had enough access to drop one backdoor, they likely had enough access to drop others.

Investigation phase: four questions that decide everything

Before touching the webshell or making any changes, the investigation needs to answer four questions. The answers, taken together, tell you whether cleanup is viable or whether rebuild is the only safe option.

Question 1: How did the attacker get in?

Start with web server access logs. Search for requests to the webshell file path to find when it first appeared and what IP addresses accessed it.

# Search for requests to the suspected webshell path
grep 'uploads/cache-widget.php' /var/log/apache2/access.log | head -20

The earliest request to that file gives you a rough timestamp for when the shell was planted. From there, look at what else that IP address did around the same time. Common entry vectors:

  • POST requests to plugin or theme upload endpoints (often without prior authentication)
  • Requests to known vulnerable plugin paths
  • Successful logins from unusual IPs in auth logs
  • FTP or SSH sessions that coincide with the file creation time

If you can identify the entry vector, you know what to patch. If you cannot identify it, that is a strong argument for rebuilding — you cannot secure a door you have not found.

For more on tracing webshell uploads through access logs when the filename does not appear directly, see Webshell Uploaded but No Suspicious File in access_log? Search POST Requests to Upload Endpoints.

Question 2: How far did privileges reach?

A webshell running as the web server user (typically www-data or apache or nobody) has limited damage potential compared to one that achieved root. Check:

  • What user owns the webshell file? If it is owned by root or a system user rather than the web server user, privilege escalation happened.
  • Are there entries in /var/log/auth.log or /var/log/secure showing su or sudo usage from unexpected sources around the time of compromise?
  • Did any new user accounts appear in /etc/passwd?
  • Were any SSH authorized_keys files modified?
# Check for recently modified authorized_keys files across all users
find /home /root -name 'authorized_keys' -mtime -90 -ls 2>/dev/null

If the attacker stayed at web-server-user level and the entry vector is confirmed, cleanup is plausible. If they reached root, rebuild. A root-level attacker can modify system binaries, install kernel modules, and alter logs to hide their tracks. You cannot trust anything on that filesystem anymore.

Question 3: Can file integrity be proven?

This is where most cleanup attempts fall apart. You need to know which files were modified, and you need to trust the tools telling you.

On Debian/Ubuntu systems, debsums can verify installed package files against known checksums. On RHEL/CentOS, rpm -Va does something similar. If system binaries have been modified, that is a root compromise and the answer is rebuild.

For application files (WordPress, custom code), compare the current filesystem against a known-good source. For WordPress core, wp-cli can verify checksums:

# Verify WordPress core file integrity
wp core verify-checksums --path=/var/www/html

But this only covers core files. Plugins, themes, uploaded content, and custom code have no canonical checksum to compare against unless you have your own baseline or version control.

Attackers also tamper with file timestamps using touch to make modified files blend in. Do not trust mtime alone. If you have a file integrity monitoring tool that was running before the compromise and storing hashes externally, that data is gold. If you do not, you are guessing about what changed.

A clean malware scan does not prove integrity either. Scanners rely on signatures and heuristics. Obfuscated backdoors — especially short ones using eval(), assert(), preg_replace() with the e modifier, or variable function calls — routinely evade them. For more on this gap, see Thinking a Clean Malware Scan Means No Backdoor? Check for PHP eval in Upload Directories.

Question 4: Do clean backups exist?

Whether you clean or rebuild, you need a known-good state to restore to or compare against. Check:

  • Do backups exist from before the earliest evidence of compromise?
  • Were backups stored somewhere the attacker could not reach? (If backups live on the same server, assume they may be compromised too.)
  • Can you verify backup integrity? (Checksums, backup tool logs showing successful completion.)

If no clean backup exists, rebuilding means rebuilding from scratch — fresh OS, fresh application install, manual data migration. That is expensive but sometimes the only honest option.

The critical discovery: it is rarely just one file

The pattern that pushes most incidents from "maybe we can clean this" to "we need to rebuild" is finding additional persistence. After the initial webshell discovery, a thorough scan typically reveals:

  • Additional webshells in other directories, sometimes with different filenames and different obfuscation
  • Modified legitimate files with injected code (a single eval(base64_decode(...)) line added to a theme's functions.php)
  • New cron jobs that re-download the webshell if it gets deleted
  • Modified .htaccess files that redirect traffic or proxy requests
  • SSH keys added to ~/.ssh/authorized_keys for direct shell access

Check cron jobs for all users, not just root:

# List cron jobs for all users
for user in (cut -f1 -d: /etc/passwd); do echo "---user ---"; crontab -l -u "$user" 2>/dev/null; done

Each additional backdoor you find reduces confidence that you have found them all. If you find three, assume there are more you have not found.

Temporary mitigation while you decide

While investigating, the server is still potentially under attacker control. Immediate steps:

  1. Do not delete the webshell yet. Rename it or move it to a quarantine directory. You need it for forensic analysis — what functions it uses, what it logs, what other files it references.
  2. Block the attacker's known IPs at the firewall level, not just the application level.
  3. Rotate all credentials — database passwords, CMS admin accounts, FTP/SSH passwords, API keys. Do this even if you plan to rebuild, because credentials may have been exfiltrated.
  4. Take a full disk image or snapshot before making any changes. If this turns into a legal matter or you need deeper forensic analysis later, you need the original state preserved.
  5. Isolate the server from other systems on the same network if possible. Lateral movement — the attacker pivoting from one compromised server to others — is a real risk.

The decision framework: clean or rebuild

Cleanup is defensible only when ALL of these conditions are true:

  • Entry vector identified and patched
  • Attacker stayed at web-server-user privilege level (no root)
  • File integrity verified against a trusted baseline
  • No evidence of additional backdoors beyond the initial webshell
  • Clean backup available for comparison or restoration
  • Logs are intact and show no signs of tampering

Rebuild is the safer choice when ANY of these are true:

  • Entry vector unknown
  • Root access achieved or suspected
  • No file integrity baseline exists
  • Multiple backdoors found
  • No clean pre-compromise backup available
  • Logs are missing, truncated, or show signs of tampering
  • Time since compromise is unknown or long (weeks/months)

In practice, most webshell incidents on shared hosting or unmanaged VPS environments land in the rebuild column. The typical setup lacks file integrity monitoring, has no off-server log aggregation, and runs backups on the same machine. Without those foundations, you cannot prove the server is clean, and "probably clean" is not a standard you want to bet production on.

After the rebuild

Rebuilding is not the end. If you rebuild without addressing the entry vector, the attacker walks back in through the same door. The rebuild should include:

  • Fresh OS installation, not a snapshot restore
  • Application installed from verified sources, not copied from the compromised filesystem
  • Database imported from backup after inspection (look for injected admin users, modified content containing JavaScript, stored procedures that should not exist)
  • All credentials regenerated
  • The original vulnerability patched before the application goes live
  • File integrity monitoring and off-server log shipping set up this time, so the next incident has better forensic data

The uncomfortable truth about webshell incidents is that deleting the file and moving on feels productive but proves nothing. The investigation is the work. If the investigation cannot definitively answer how far the attacker got and what they changed, the only honest response is to stop trusting that filesystem and start over.