# Portswigger Directory Traversal 🥵
<style>body {text-align: justify}</style>
Hi, dưới đây là writeup của 6/6 bài lab về lỗ hổng [Directory Traversal](https://portswigger.net/web-security/file-path-traversal) mình đã solve trong quá trình ôn tập thi chứng chỉ của Portswigger.
### 1. File path traversal, simple case
##### Description
> This lab contains a file path traversal vulnerability in the display of product images.
>
> To solve the lab, retrieve the contents of the `/etc/passwd` file.
##### Writeup
Ứng dụng web load ảnh của các post thông qua tham số `filename`.

Truy cập đường dẫn load ảnh của post bất kì. Ảnh này có vẻ nằm ở đường dẫn `/var/www/html` (web root directory của Linux).

Thay đổi tham số `filename` thành `../../../etc/passwd` để traverse về thư mục root và truy cập file `/etc/passwd`.

Đọc file `/etc/passwd` thành công và ta solve được challenge.
### 2. File path traversal, traversal sequences blocked with absolute path bypass
##### Description
> This lab contains a file path traversal vulnerability in the display of product images.
>
> The application blocks traversal sequences but treats the supplied filename as being relative to a default working directory.
>
> To solve the lab, retrieve the contents of the `/etc/passwd` file.
##### Writeup
Ứng dụng web load ảnh của các post thông qua tham số `filename` và ta lại khai thác lỗ hổng File path traversal ở tham số này.

Khi traverse bằng `../../../etc/passwd` thì bị trả về `400 bad request` → Có vẻ như server đã chặn `../`.

Tuy nhiên, khi truy cập bằng đường dẫn tuyệt đối `/etc/passwd` thì server trả về nội dung file thành công.

Như vậy, ta solve được challenge.
### 3. File path traversal, traversal sequences stripped non-recursively
##### Description
> This lab contains a file path traversal vulnerability in the display of product images.
>
> The application strips path traversal sequences from the user-supplied filename before using it.
>
> To solve the lab, retrieve the contents of the `/etc/passwd` file.
##### Writeup
Ứng dụng web load ảnh của các post thông qua tham số `filename` và ta lại khai thác lỗ hổng File path traversal ở tham số này.

Khi traverse bằng `../../../etc/passwd` thì bị trả về `400 bad request`. Lí do là do ứng dụng đã xóa `../` nếu nó xuất hiện trong param `filename`.

Tuy nhiên, theo mô tả thì nó làm việc đó không triệt để. Cụ thể, với `....//` thì nó sẽ chỉ xóa `../` ở giữa, và kết quả vẫn còn lại `../` → ta có thể bypass để traverse. Sử dụng payload `....//....//....//etc/passwd` ta sẽ đọc được nội dung file `/etc/passwd` thành công.

### 4. File path traversal, traversal sequences stripped with superfluous URL-decode
##### Description
> This lab contains a file path traversal vulnerability in the display of product images.
>
> The application blocks input containing path traversal sequences. It then performs a URL-decode of the input before using it.
>
> To solve the lab, retrieve the contents of the `/etc/passwd` file.
##### Writeup
Ứng dụng web load ảnh của các post thông qua tham số `filename` và ta lại khai thác lỗ hổng File path traversal ở tham số này.

Khi traverse bằng `../../../etc/passwd` thì bị trả về `400 bad request` → ứng dụng chặn `../` để traverse.

Thử encode URL kí tự `/` thành `%2f`, kết quả cũng trả về 400 do có thể web browser đã decode trước và server vẫn hiểu đó là `/`

Nếu đọc kĩ mô tả, có vẻ như server còn có 1 bước URL decode nữa sau khi check `../` → Nếu ta double encoding `../ -> ..%2f -> ..%252f` thì khi submit request, server sẽ hiểu `..%252f` thành `..%2f`, và cái này trải qua một bước URL decode nữa sẽ thành `../` → Ta bypass thành công và xem được nội dung file `/etc/passwd` với payload như hình dưới.

### 5. File path traversal, validation of start of path
##### Description
> This lab contains a file path traversal vulnerability in the display of product images.
>
> The application transmits the full file path via a request parameter, and validates that the supplied path starts with the expected folder.
>
> To solve the lab, retrieve the contents of the `/etc/passwd` file
##### Writeup
Ứng dụng web load ảnh của các post thông qua tham số `filename` với đường dẫn tuyệt đối tại `/var/www/html/<file-name>.jpg`.

Thử truyền vào `filename` giá trị `/etc/passwd` thì không thành công. Theo mô tả thì server sẽ validate `filename` phải bắt đầu bằng `/var/ww/html/`

Như vậy chỉ cần bypass bằng cách traverse dựa trên folder `/var/ww/html/` với payload `/var/www/html/../../../etc/passwd`, ta sẽ solve được challenge.

### 6. File path traversal, validation of file extension with null byte bypass
##### Description
> This lab contains a file path traversal vulnerability in the display of product images.
>
> The application validates that the supplied filename ends with the expected file extension.
>
> To solve the lab, retrieve the contents of the `/etc/passwd` file.
##### Writeup
Ứng dụng web load ảnh của các post thông qua tham số `filename`.

Theo mô tả, nó yêu cầu `filename` phải chứa extension `.jpg`.

Do đó, để đọc được file `/etc/passwd` ta sẽ traverse và dùng null byte theo cách sau để bypass `../../../etc/passwd%00.jpg`. Khi đó payload sẽ thỏa điều kiện của ứng dụng và từ đó ta đọc được nội dụng `/etc/passwd`.

###### tags: `portswigger`, `directory-traversal`