T The Triage ManualTechnical Guides for IT Emergencies
P2 · Cyber Incident Response

De-obfuscating Malicious PHP Code Found on a Compromised Web Server

Attackers who compromise PHP-based web servers commonly plant obfuscated scripts using eval(), preg_replace() with the /e modifier, base64_decode(), hex encoding, and XOR techniques to conceal malicious payloads such as webshells, spam mailers, and backdoors. Resolution requires isolating the server, identifying all suspect files, systematically decoding each obfuscation layer using tools such as UnPHP, PHP Beautifier, and CLI decoders to understand the payload, then cleaning, recredentialling, and hardening the environment. After confirming file integrity and patching the exploited vulnerability, the server can be safely returned to production.

Indicators

Likely causes

Diagnostic steps

  1. Isolate the server or affected virtual host immediately — take it offline or block inbound/outbound web traffic at the firewall to stop active exploitation before investigating.
  2. Identify all recently modified or created PHP files: run 'find /var/www -name "*.php" -newer /var/www/index.php -ls' (adjust path and reference file as appropriate) to build a list of suspect files. Note the modification timestamps for log correlation.
  3. Grep for common malicious function calls across all PHP files: run 'grep -rl "eval(" /var/www', 'grep -rl "preg_replace" /var/www', and 'grep -rl "base64_decode" /var/www' to locate all affected files.
  4. Copy each suspicious PHP file's contents and paste into UnPHP (archived: https://web.archive.org/web/*/https://www.unphp.net/) to attempt automated recursive de-obfuscation. Review the decoded output for the plaintext payload.
  5. If the file is a single long minified line, paste it into PHP Beautifier (https://phpbeautifier.com/) to reformat it with proper indentation before attempting manual layer-by-layer analysis.
  6. If Base64-encoded strings are present, extract the string passed to base64_decode() and decode it using 'echo "<base64string>" | base64 -d' in a terminal, or use a trusted offline Base64 decoder tool.
  7. If hex-encoded sequences (\xNN format) are present, use a hex-to-ASCII converter or confirm the decoded value safely by wrapping the string in an echo statement in PHP Sandbox: 'echo "\x48\x65\x6c\x6c\x6f";'.
  8. For XOR-obfuscated strings (common in preg_replace-based payloads), replace the eval() call with echo() in a PHP Sandbox environment (https://sandbox.onlinephpfunctions.com/) to safely print the decoded payload without executing it.
  9. After revealing the final payload, document what the script does (webshell, spam mailer, backdoor, crypto miner, etc.) and review server access logs (access_log, error_log) around the file's modification timestamp to identify the initial attack vector.

Resolution path

Prevention

Tools

References

phpmalwareobfuscationdeobfuscationwebshellevalbase64preg_replaceincident-responseserver-compromiseweb-applicationcmsforensicscyber-incident