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

# lab-3

## File Path Traversal, Traversal Sequences Stripped Non-Recursively

**Difficulty:** Practitioner

### Lab Description

This lab contains a path traversal vulnerability in the display of product images. The application strips path traversal sequences from the user-supplied filename before using it.

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

### Root Cause

The application implements a blacklist-based defence that strips `../` from the input — for example:

```php
$file = str_replace("../", "", $file);
```

However, this replacement is performed only once and non-recursively. An attacker can bypass it by nesting the traversal sequence: `....//`. When `../` is stripped from the middle of `....//`, the remaining characters collapse back into `../`, reconstructing the traversal sequence after the filter has run.

### Exploitation

1. Access the lab, open any product page, and open the image in a new tab.
2. Capture the request in Burp Suite.
3. Find the URL endpoint: `GET /image?filename=43.jpg`
4. Replace the filename value with the nested traversal payload:

   ```
   ....//....//....//....//etc/passwd
   ```

   After the non-recursive strip removes each `../`, this resolves to `../../../../etc/passwd`.
5. Send the request — the contents of `/etc/passwd` are returned in the response.

### Lesson Learned

Non-recursive stripping of traversal sequences is trivially bypassed using nested sequences. The fix must either apply the strip recursively until no further matches are found, or — better — validate the fully resolved canonical path after any sanitisation to confirm it remains within the intended directory.


---

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