> For the complete documentation index, see [llms.txt](https://ganesha-hk.gitbook.io/cybersecurity-writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ganesha-hk.gitbook.io/cybersecurity-writeups/port-swigger-file-upload-labs/lab-2.md).

# lab-2

## Web Shell Upload via Content-Type Restriction Bypass

**Difficulty:** Apprentice

### Lab Description

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

**Objective:** Upload a basic PHP web shell and use it to exfiltrate the contents of `/home/carlos/secret`. Submit the secret using the button in the lab banner.

**Credentials:** `wiener:peter`

### Root Cause

The server validates the `Content-Type` header of the uploaded file rather than the actual file contents. When a PHP file is uploaded without interception, the browser sets `Content-Type: application/x-php`, which the server rejects. However, because `Content-Type` is a client-controlled header, an attacker can intercept the upload request and change it to `image/jpeg`. The server accepts the file based on the spoofed header and stores the PHP script — which is then executable on the server.

### Exploitation

1. Log in as `wiener:peter` and navigate to the account page.
2. Create a file named `php.php` with the following content:

   ```php
   <?php echo file_get_contents('/home/carlos/secret'); ?>
   ```
3. Upload `php.php` as the avatar and intercept the request in Burp Suite.
4. In the multipart body, find the `Content-Type` header for the file part and change it from `application/x-php` to `image/jpeg`.
5. Forward the request.
6. Right-click the avatar and select **Open image in new tab** — the secret is returned in the response.
7. Submit the secret to complete the lab.

### Lesson Learned

The server must never rely solely on the user-controlled `Content-Type` header to validate uploaded files. File type validation must be based on the actual file contents (e.g. magic bytes) and the file extension, not on headers that the client can freely modify.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ganesha-hk.gitbook.io/cybersecurity-writeups/port-swigger-file-upload-labs/lab-2.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
