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

# lab-4

## Web Shell Upload via Extension Blacklist Bypass

**Difficulty:** Practitioner

### Lab Description

This lab contains a vulnerable image upload function. Certain file extensions are blacklisted, but the defence can be bypassed due to a fundamental flaw in the blacklist configuration.

**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 application blacklists common PHP extensions (e.g. `.php`) but still allows users to upload server configuration files such as `.htaccess`. By uploading a malicious `.htaccess` file, an attacker can instruct Apache to treat a custom file extension (e.g. `.evil`) as executable PHP. Once the mapping is in place, a script with that extension is uploaded and executed.

```
Application
    │
    ▼
Allows uploading .htaccess (configuration file)
    │
    ▼
Apache honours the uploaded configuration
    │
    ▼
Custom extension treated as PHP
    │
    ▼
Code execution
```

### Exploitation

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

   ```
   AddType application/x-httpd-php .evil
   ```

   This tells Apache to execute files with the `.evil` extension as PHP.
3. Upload `.htaccess` as the avatar.
4. Create a second file named `php.evil` with the following content:

   ```php
   <?php echo file_get_contents('/home/carlos/secret'); ?>
   ```
5. Upload `php.evil` as the avatar.
6. Open the avatar in a new tab — Apache executes the `.evil` file as PHP and the secret is returned in the response.
7. Submit the secret to complete the lab.

### Lesson Learned

Never rely on extension blacklisting alone. Attackers can use alternate or custom extensions, or upload server configuration files like `.htaccess` to modify execution rules and bypass the blacklist entirely. Secure file upload requires: strict extension allowlisting, content-based file type validation, and ensuring the server never executes files from the upload directory regardless of extension.


---

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