# 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`. ![](https://i.imgur.com/0hExEoM.png) 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). ![](https://i.imgur.com/6BQR64T.png) Thay đổi tham số `filename` thành `../../../etc/passwd` để traverse về thư mục root và truy cập file `/etc/passwd`. ![](https://i.imgur.com/8ZL5dDv.png) Đọ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. ![](https://i.imgur.com/HwWRDFX.png) Khi traverse bằng `../../../etc/passwd` thì bị trả về `400 bad request` &rarr; Có vẻ như server đã chặn `../`. ![](https://i.imgur.com/RUCHqIX.png) 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. ![](https://i.imgur.com/rJklauQ.png) 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. ![](https://i.imgur.com/1hFAk3R.png) 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`. ![](https://i.imgur.com/Ut4wMo6.png) 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 `../` &rarr; 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. ![](https://i.imgur.com/DtCGZkd.png) ### 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. ![](https://i.imgur.com/DqC5AA6.png) Khi traverse bằng `../../../etc/passwd` thì bị trả về `400 bad request` &rarr; ứng dụng chặn `../` để traverse. ![](https://i.imgur.com/XgeVHet.png) 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à `/` ![](https://i.imgur.com/EGxg6pI.png) Nếu đọc kĩ mô tả, có vẻ như server còn có 1 bước URL decode nữa sau khi check `../` &rarr; 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 `../` &rarr; Ta bypass thành công và xem được nội dung file `/etc/passwd` với payload như hình dưới. ![](https://i.imgur.com/RuyXIdb.png) ### 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`. ![](https://i.imgur.com/BgxcYQe.png) 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/` ![](https://i.imgur.com/c8JF8zY.png) 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. ![](https://i.imgur.com/1cyPJwW.png) ### 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`. ![](https://i.imgur.com/RmTFV2I.png) Theo mô tả, nó yêu cầu `filename` phải chứa extension `.jpg`. ![](https://i.imgur.com/Mckh0UG.png) 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`. ![](https://i.imgur.com/hgMW8aF.png) ###### tags: `portswigger`, `directory-traversal`