What WAF Signature Matching Actually Sees
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']); ?>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.
How Obfuscation Defeats Pattern Matching
PHP
<?php
$f = 'sys' . 'tem';
f(_GET['c']);
?>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.
Why Tuning WAF Rules Is Not Enough
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.
Server-Side Controls That Actually Limit Webshell Damage
Restrict upload directories from executing PHP
/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>.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.
Use disable_functions in php.ini
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.
Monitor upload directories for new PHP files
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.php files appearing in upload directories is almost always suspicious and worth investigating immediately.
Validate uploads at the application level
- 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-Typeheader the client sends (which is trivially spoofed) - File content for PHP opening tags (
<?php,<?=,<script language="php") if the upload should never contain code
Webshells That Are Already There
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.
The Correct Production Pattern
- WAF rules as a first filter for known, unobfuscated payloads
- Web server configuration that prevents PHP execution in upload directories
disable_functionsinphp.inito neutralize command execution even if a shell runs- Application-level upload validation with extension and content-type allowlists
- File integrity monitoring on directories where new PHP files should never appear
