Insights

Thinking a WAF Blocks All Webshells? Obfuscated PHP Shells Bypass Pattern Matching

November 20, 2026
0:00
You deployed a WAF in front of your web server, enabled its default ruleset, and now you assume any uploaded PHP webshell will get caught before it executes. That assumption is wrong in a way that matters. WAFs — whether ModSecurity, cloud-based offerings, or LiteSpeed's built-in rules — rely heavily on pattern matching against known signatures. Attackers know exactly what those signatures look for, and they have a long list of tricks to produce functionally identical PHP code that looks nothing like the patterns in your ruleset.
A WAF inspects HTTP request and response bodies looking for strings and patterns that match known malicious payloads. For PHP webshells, that typically means rules that trigger on function names like eval(), system(), exec(), passthru(), shell_exec(), and base64_decode() appearing in uploaded file content or POST bodies. This works against the most basic webshell — the kind you find in tutorials:
PHP
<?php eval($_POST['cmd']); ?>
Any WAF with a remotely current ruleset blocks that. The string eval next to $_POST is a textbook signature match. The problem is that PHP is an extraordinarily flexible language when it comes to calling functions indirectly, and attackers exploit that flexibility.
PHP lets you call functions using variable variables, string concatenation, and encoding in ways that produce no recognizable signature at the text level. Here are patterns that commonly slip past WAF rules. Variable function calls. PHP allows calling a function whose name is stored in a variable:
PHP
<?php
$f = 'sys' . 'tem';
f(_GET['c']);
?>
No rule scanning for the literal string system will match 'sys' . 'tem'. The concatenation happens at runtime, not in the source text the WAF inspects. Encoding and decoding. Base64-encoding the function name and decoding it at runtime is common. But WAFs often flag base64_decode itself, so attackers go one step further — they use str_rot13, gzinflate, convert_uuencode, or even custom XOR routines that have no signature at all. Dynamic invocation via call_user_func. Even if the WAF flags eval, an attacker can use call_user_func or call_user_func_array with an encoded callback name. Or they use preg_replace with the now-deprecated /e modifier (still works on older PHP versions) to execute arbitrary code through a regex replacement. Character-level construction. PHP's chr() function builds strings character by character from ASCII codes. A shell that constructs system as chr(115).chr(121).chr(115).chr(116).chr(101).chr(109) contains none of the target strings a WAF rule would match. The core issue: WAF rules are static text patterns. PHP execution is dynamic. Any technique that defers the construction of dangerous strings to runtime will bypass text-based inspection. This is not a theoretical gap — it is the standard approach used by publicly available webshell generators and obfuscation tools.
The natural response is "add more rules." Block call_user_func, block chr(, block str_rot13. But each of those functions has legitimate uses in normal PHP applications. WordPress core, for example, uses call_user_func extensively. Blocking it at the WAF level breaks the application. You can increase the paranoia level in rulesets like the OWASP Core Rule Set (CRS) for ModSecurity, which adds broader pattern matching. Higher paranoia levels do catch more obfuscation — but they also produce significantly more false positives on legitimate traffic. In practice, many operators lower the paranoia level or disable specific rules to keep their applications working, which reopens the gaps. WAF tuning is a constant arms race. It has value as one layer, but treating it as the only layer is where the misconception causes real damage.
Since you cannot reliably prevent every obfuscated shell from reaching disk through the WAF alone, you need controls at the server level that either prevent upload, prevent execution, or limit what a shell can do even if it runs.
This is the single highest-impact control. If your application accepts file uploads into /uploads/ or /wp-content/uploads/, configure your web server to refuse PHP execution in that directory entirely. For Apache or LiteSpeed with .htaccess:
APACHE
<Directory "/var/www/html/wp-content/uploads">
  php_admin_flag engine off
  <FilesMatch "\\.ph(p[0-9]?|tml|ps)$">
    Require all denied
  </FilesMatch>
</Directory>
For Nginx, deny PHP execution in the location block for the uploads path. The exact directive depends on how your PHP-FPM is configured, but the principle is the same: never pass .php files from upload directories to the PHP handler. Even if an attacker uploads a perfectly obfuscated shell into /uploads/evil.php, the web server returns a 403 or serves it as plain text. The shell never executes.
PHP's disable_functions directive prevents specific functions from being called regardless of how they are invoked — variable functions, call_user_func, encoding tricks, none of it matters. If system is in disable_functions, calling it through f = 'system';f('whoami'); still fails. A typical hardened configuration disables functions that a normal web application should never need: exec, passthru, shell_exec, system, proc_open, popen, pcntl_exec, putenv This does not prevent all webshell functionality — an attacker can still read and write files using PHP's file functions — but it removes the ability to spawn system commands, which is what most webshells exist to do.
File integrity monitoring catches what prevention misses. On Linux, inotifywait (from the inotify-tools package) can watch a directory for new file creation in real time:
BASH
inotifywait -m -r -e create -e moved_to /var/www/html/wp-content/uploads/ \\
  --format '%w%f %e' | while read file event; do
  echo "[ALERT] New file detected: file (event)"
done
This is a basic example. In production, you would pipe alerts to a logging system or trigger an automated scan. The point is that new .php files appearing in upload directories is almost always suspicious and worth investigating immediately.
WAF inspection happens at the HTTP layer. Application-level validation happens after the file is received but before it is written to its final location. Proper validation checks:
  • File extension against an allowlist (not a blocklist — allow .jpg, .png, .pdf, reject everything else)
  • MIME type from the file header, not from the Content-Type header the client sends (which is trivially spoofed)
  • File content for PHP opening tags (<?php, <?=, <script language="php") if the upload should never contain code
WordPress and most CMS platforms do some of this, but plugins that handle uploads often implement their own logic with gaps. Checking what your specific upload handlers actually validate is worth the time.
If you are reading this after a compromise rather than before one, finding obfuscated shells on disk requires different tools than WAF rules. Searching for eval in PHP files misses the same obfuscation that the WAF missed. Look for PHP files in directories where they should not exist (upload directories, cache directories, temp directories). Look for recently modified PHP files outside of known update windows. Look for files with high entropy — heavily encoded content has a measurably different character distribution than normal PHP source code. For more on tracing how a webshell arrived on your server in the first place, the process of searching POST requests to upload endpoints is covered in Webshell Uploaded but No Suspicious File in access_log? Search POST Requests to Upload Endpoints.
A WAF is a useful layer. Keep it. But treat it as one input filter among several, not as a guarantee. The production pattern that actually limits webshell risk combines:
  1. WAF rules as a first filter for known, unobfuscated payloads
  2. Web server configuration that prevents PHP execution in upload directories
  3. disable_functions in php.ini to neutralize command execution even if a shell runs
  4. Application-level upload validation with extension and content-type allowlists
  5. File integrity monitoring on directories where new PHP files should never appear
No single layer catches everything. Obfuscated PHP shells exist specifically because WAFs work well enough against the obvious ones that attackers had to adapt. Your defenses need to adapt the same way — not by writing better regex, but by making execution impossible even when detection fails.

Join My Newsletter & Get Updates!

Get notified whenever a new technical guide, server infrastructure breakdown, or personal reflection goes live, plus a curated weekly digest.