> 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-path-traversal-labs/lab-6.md).

# lab-6

## File Path Traversal, Validation of File Extension with Null Byte Bypass

**Difficulty:** Practitioner

### Lab Description

This lab contains a path traversal vulnerability in the display of product images. The application validates that the supplied filename ends with the expected file extension.

**Objective:** Retrieve the contents of the `/etc/passwd` file.

### Root Cause

There are two security flaws working together.

**Flaw 1 — User input is used directly as a file path:**

```php
$file = $_GET['filename'];
readfile($file);
```

The application allows the user to decide which file to open.

**Flaw 2 — The extension check is performed on the raw input:**

```php
if (endsWith($filename, ".png")) {
    openFile($filename);
}
```

The developer validates that the filename ends with `.png` before opening it. The vulnerable assumption is:

```
Validation
    │
filename ends with .png
    │
    ▼
Filesystem opens exactly that filename
```

But that assumption is false when a null byte is involved.

### Exploitation

**Payload:**

```
../../../etc/passwd%00.png
```

**Step 1 — URL decoding:**

`%00` decodes to `\x00` (the null byte). The string becomes:

```
../../../etc/passwd\0.png
```

**Step 2 — Extension validation:**

The application checks: *does it end with `.png`?* Yes — the `.png` suffix is present and the check passes.

**Step 3 — Native filesystem/API call:**

Historically, many C libraries and older language runtimes treat the null byte as the string terminator. The OS or underlying C function receives the path up to the `\0` and ignores everything after it:

```
../../../etc/passwd\0.png  →  ../../../etc/passwd
```

The file that actually gets opened is `/etc/passwd`.

**Visual flow:**

```
Attacker
    │
    ▼
../../../etc/passwd%00.png
    │
    ▼
URL Decode
    │
    ▼
../../../etc/passwd\0.png
    │
    ▼
Extension Check
Ends with ".png" ✔
    │
    ▼
Native C API
Stops at null byte
    │
    ▼
../../../etc/passwd
    │
    ▼
OS opens /etc/passwd
```

### Lesson Learned

* **Never trust user-controlled paths.** Use allowlists of expected filenames or opaque identifiers (e.g. an image ID mapped server-side to a real path) instead of accepting arbitrary path input from the user.
* **Validate after canonicalisation.** Resolve the full canonical path first, then verify it remains inside the intended directory before opening any file.
* The null byte bypass works because the validation layer and the filesystem layer interpret the string differently. Ensuring a single, consistent parsing step for both validation and file access eliminates this class of mismatch.


---

# 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-path-traversal-labs/lab-6.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.
