> 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-3.md).

# lab-3

## Web Shell Upload via Path Traversal

**Difficulty:** Practitioner

### Lab Description

This lab contains a vulnerable image upload function. The server is configured to prevent execution of user-supplied files within the upload directory, but this restriction can be bypassed by exploiting a secondary vulnerability.

**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 correctly prevents PHP scripts from executing inside the upload directory — any `.php` file stored there is served as plain text. However, the `filename` parameter in the multipart upload request is vulnerable to path traversal. By supplying `../php.php` as the filename, the attacker can cause the server to store the script one directory above the upload folder, where execution restrictions may not apply. The script is then directly accessible and executable, leading to arbitrary command execution and remote code execution.

### 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 capture the request in Burp Suite.
4. Find the `POST /my-account/avatar` request and send it to Repeater.
5. In the multipart body, change the `filename` parameter from `php.php` to `../php.php`.
6. Send the request — the file is stored in the parent directory where scripts can be executed.
7. Navigate to the account page, open the avatar in a new tab, and adjust the URL path to point to the uploaded file's location in the parent directory. The secret is returned in the response.
8. Submit the secret to complete the lab.

### Lesson Learned

A restricted upload directory is only effective if the `filename` parameter is also validated. User-controlled filenames must be sanitised to remove path traversal sequences before the file is stored. Even if the upload directory blocks execution, path traversal can move a malicious file to a directory where execution is permitted.


---

# 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-3.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.
