githubEdit

File upload

What is it?

File Inclusion vulnerabilities allow an attacker to include files on a server through the web browser. This can occur in two forms: Local File Inclusion (LFI) and Remote File Inclusion (RFI). LFI exploits enable attackers to read files on the server, while RFI allows attackers to execute arbitrary code by including remote files over the internet.

A simple example

  • A vulnerable web application has the endpoint /page?file={filename}

  • When a request is made, the application dynamically includes the content of the file specified in the query parameter, for example, PHP's include() function: include($filename);

  • If an attacker modifies {filename} to a path such as ../../etc/passwd or a remote URL http://attacker.com/malicious.php, they can read sensitive files or execute malicious code.

It's important to note that the specific impact and exploitation techniques can vary depending on server configuration, programming language, and application logic. File Inclusion vulnerabilities can lead to:

  • Sensitive data exposure

  • Remote code execution

  • Cross-site scripting

Other learning resources:

  • [To be updated]

Writeups:

Have a good writeup & want to share it here? Drop me a message on LinkedIn.arrow-up-right

Checklist

Exploitation// Some code

Insecure file upload

What is it?

Insecure File Upload vulnerability is when an application allows uncontrolled and unvalidated upload of files. An attacker can exploit this vulnerability to upload malicious files, like web shells, which can lead to code execution, data leakage, or other types of attacks.

A simple example

An application allows users to upload profile pictures without validating the file type and content, or without properly handling the file storage. An attacker can upload a PHP shell script disguised as an image file. When this file is served by the server, the malicious script can be executed.

The impact of insecure file uploads includes:

  • Remote Code Execution (RCE)

  • Data Leakage

  • Server Compromise

Other learning resources:

Checklist

Exploitation

File upload

Cheatsheet

File Upload Attack

It is often used for gaining access to the target shell using Reverse Shell, or getting sensitive information using Remote Code Execution (RCE).

Check Allowed File Extensions

First off, we need to know what file types are allowed to be uploaded in target website. Try to upload any formats.

Create Blank Files for Each Format

To create a blank file for the checking purpose, execute the following command.

  • jpg, png

  • pdf

Bypass File Extension Validation

We might be able to bypass file extension validation by modifying the filename. For example, if we cannot upload a pure .php file extension, tweak the filename a bit as below.

Bypass Content-Type Validation

We might be able to bypass the content type validation by modifying that. For example, assume that we want to upload PHP file to execute webshell or reverse shell, but PHP files are rejected by the website. In this situation, we might be able to bypass the validation by modifying the "Content-Type" from "application/x-php" to other types such as "image/jpeg", "plain/text" etc. Here is the example.

Change Upload Location by Filename

We might be able to upload our file to unintended location by path traversing with filename e.g. ../example.php or ..%2Fexample.php.

Do not forget to test bypass techniques as below:

Overwrite Server Configuration

We might be able to overwrite the web server configuration file such as ".htaccess", ".htpasswd" by specifying the filename to the name of the config file and write desired contents of that.

Magic Bytes

Reference: Wikipediaarrow-up-right

If the website checks the magic byte of the uploaded file for allowing only image files to be uploaded, we might be able to bypass this validation by adding magic bytes before the actual payload.

The exif_imagetype() PHP function is likely to be used for such validation. In addition, we may need to change other values such as "Content-Type" depending on the situation.

PNG

Hex Signature
ISO 8859-1

89 50 4E 47 0D 0A 1A 0A

‰PNG␍␊␚␊

Payload example:

JPG/JPEG

Hex Signature
ISO 8859-1

FF D8 FF EE

ÿØÿî

FF D8 FF E0

ÿØÿà

FF D8 FF E0 00 10 4A 46 49 46 00 01

ÿØÿà␀␐JFIF␀␁

Payload example:

PDF

Hex Signature
ISO 8859-1

25 50 44 46 2D

%PDF-

Payload example:

GIF

Reference: Web Design in a Nutshellarrow-up-right

  • GIF87a: The original format for indexed color images. It uses LZW compression and has the option of being interlaced.

  • GIF89a: Is the same as GIF87a but also includes transparancy and animation capabilities.

Hex Signature
ISO 8859-1

47 49 46 38 37 61

GIF87a

47 49 46 38 39 61

GIF89a

Payload example:

RIFF WAVE

Hex Signature
ISO 8859-1

52 49 46 46 ?? ?? ?? ?? 57 41 56 45

RIFF????WAVE

Payload example:

Combine payload into image file

The payload can be executed by combining into an image file data. For example, generate a blank image file at first:

And then we can put our payload to the end of the image data:

Zip

If target website restricts uploads to zip files only, the website (server) may unzip uploaded files internally and displays the result of decompressed file somewhere e.g. /upload/example.txt.

Zip Slip

Create a file containing ‘../’ in the filename, and compress this file.

After uploading the zip file, target website (server) will decompress this file and may store the test.php file into unexpected directory.

In local machine, create a symbolic link for a sensitive file. Then compress the symlink with zip.

When we upload the zip file to target website, the website decompress the zip file and may display the file of the symbolic link (/etc/passwd, etc.). In short, we may be able to see the contents of the sensitive file.

JPEG Polyglot XSS

Reference: https://infosecwriteups.com/exploiting-xss-with-javascript-jpeg-polyglot-4cff06f8201aarrow-up-right

We may be able to inject XSS by inserting arbitrary JavaScript code into a JPEG file. We can generate automatically a polyglot JPEG with imgjs_polygloterarrow-up-right.

Below is the manual exploitation flow.

1. Prepare JPEG Image

Here we create a blank JPEG image using convert command.

2. Create XSS Payload in Hex

We will insert the following JavaScript code.

To insert it into JPEG file, we need to convert this to HEX as below:

3. Start Hex Editor

Start Hex editor to modify our evil.jpg. We use ghex here but you can use your favorite HEX editor.

The original hex representation of the evil.jpg is as such follow:

4. Insert Our JavaScript into JPEG File

Now start inserting our payload. First we replace 00 10 after FF D8 FF E0 with our 2F 2A (/*).

By this, the length of the JPEG header is 12074 bytes (0x2F2A in decimal).

Since the size of our payload is 19 bytes as below:

We need to pad out the remaining 12042 bytes (12074 - 16 - 19) with nulls. Below is the Python code example to generate a polyglot JPEG file, which refers to https://github.com/simplylu/jpeg_polyglot_xss/blob/main/exploit.pyarrow-up-right.

5. XSS with this JPEG

Inject XSS to make our JPEG to execute JavaScript code. We need to set charset to ISO-8859-1 for executing that.

Malicious Filnames

Command Injection

PHP Injection

XSS

### SSTI

### Truncation

### HTML Injection Try to upload the file which includes **HTML tags** in the filename. It may affect the web page.

### SQL Injection Try to upload the file which includes **SQL command** in the filename. It may execute **SQL Injection** when uploading or other situations.

## Race Condition Attack We might be able to bypass/execute payload using race condition. We can easily achieve that with **Turbo Intruder** in **Burp Suite**.

## PHP Payloads After finding vulnerability, we can create a payload for exploiting the website. Here is the example payloads of **web shell** and **reverse shell** to compromise target system. ### Web Shell For example, the file name is "exploit.php".

### Reverse Shell for Linux

Or we might be able to use the following simple script.

Then open listener for getting a shell.

After uploading it, reload the page in which the payload uploaded e.g. `"/upload/shell.php"`. ### Reverse Shell for Windows First create a malicious executable file using msfvenom. Replace **10.0.0.1** with your local ip.

Next start a listener for getting the target shell.

After getting the shell, we will get in the meterpreter, so we need to know the meterpreter’s commands. To get a list of command, run the following in the meterpreter.

## Other Tips ### Craft Upload Request If website does not display the upload page but such functionality exists on the website, we can manually create the uploading request. First off, we need to set **`multipart/form-data; boundary=`** to **Content-Type** in **HTTP headers** as below. Of course we need to specify the **POST** method at the top of the header.

Then we can surround **POST body (to upload file)** with this boundary as the following.

## References -

Last updated