/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
Investigation phase: four questions that decide everything
Question 1: How did the attacker get in?
BASH
# Search for requests to the suspected webshell path
grep 'uploads/cache-widget.php' /var/log/apache2/access.log | head -20- 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
Question 2: How far did privileges reach?
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.logor/var/log/secureshowingsuorsudousage from unexpected sources around the time of compromise? - Did any new user accounts appear in
/etc/passwd? - Were any SSH authorized_keys files modified?
BASH
# Check for recently modified authorized_keys files across all users
find /home /root -name 'authorized_keys' -mtime -90 -ls 2>/dev/nullQuestion 3: Can file integrity be proven?
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:
BASH
# Verify WordPress core file integrity
wp core verify-checksums --path=/var/www/htmltouch 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?
- 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.)
The critical discovery: it is rarely just one file
- 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'sfunctions.php) - New cron jobs that re-download the webshell if it gets deleted
- Modified
.htaccessfiles that redirect traffic or proxy requests - SSH keys added to
~/.ssh/authorized_keysfor direct shell access
BASH
# List cron jobs for all users
for user in (cut -f1 -d: /etc/passwd); do echo "---user ---"; crontab -l -u "$user" 2>/dev/null; doneTemporary mitigation while you decide
- 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.
- Block the attacker's known IPs at the firewall level, not just the application level.
- 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.
- 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.
- 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
- 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
- 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)
After the rebuild
- 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
