# Portswigger File Upload Vulnerability 🤪
<style>body {text-align: justify}</style>
Hi, dưới đây là writeup của 6/7 bài lab về lỗ hổng [File Upload Vulnerability](https://portswigger.net/web-security/file-upload) mình đã solve trong quá trình ôn tập thi chứng chỉ của Portswigger.
### 1. Remote code execution via web shell upload
##### Description
> This lab contains a vulnerable image upload function. It doesn't perform any validation on the files users upload before storing them on the server's filesystem.
>
> To solve the lab, upload a basic PHP web shell and use it to exfiltrate the contents of the file `/home/carlos/secret`. Submit this secret using the button provided in the lab banner.
>
> You can log in to your own account using the following credentials: `wiener:peter`
##### Writeup
Bài lab cho phép các user cập nhật avatar bằng cách upload ảnh lên. Tuy nhiên, vì không có bất kì cơ chế validate file user upload lên nên ứng dụng có thể bị attacker upload php webshell lên để thực thi code từ xa.

Thực hiện upload một file `exploit.php` có chức năng in ra nội dung file `/home/carlos/secret`.

Quay lại trang thông tin xem source load ảnh avatar, chính là đường dẫn đến file `exploit.php` vừa upload.

Truy cập đường dẫn `/files/avatars/exploit.php` ta thấy server đã thực thi đoạn webshell và trả về nội dung của file `/home/carlos/secret`.

Lấy đoạn secret trả về và submit. Ta solve được challenge thành công.

### 2. Web shell upload via Content-Type restriction bypass
##### Description
> This lab contains a vulnerable image upload function. It attempts to prevent users from uploading unexpected file types, but relies on checking user-controllable input to verify this.
>
> To solve the lab, upload a basic PHP web shell and use it to exfiltrate the contents of the file `/home/carlos/secret`. Submit this secret using the button provided in the lab banner.
>
> You can log in to your own account using the following credentials: `wiener:peter`
##### Writeup
Bài lab này tiếp tục khai thác lỗ hổng File Upload Vulnerability tại chức năng upload avatar.

Hình dưới là request và response khi upload ảnh thành công.

Tuy nhiên, khi upload webshell php có chức năng in ra nội dung file `/home/carlos/secret` thì trường `Content-Type` đã bị check. Cụ thể, server validate `Content-Type: application/octet-stream` từ request là không được phép và chỉ cho `image/jpeg` hoặc `image/png`.

Như vậy, mình chỉ cần sửa `Content-Type` thành `image/png` thì sẽ upload được webshell.

Lấy đường dẫn file vừa upload.

truy cập đường dẫn trên và ta lấy được secret của `carlos`.

Submit secret trên và ta solve được challenge.

### 3. Web shell upload via path traversal
##### Description
> This lab contains a vulnerable image upload function. The server is configured to prevent execution of user-supplied files, but this restriction can be bypassed by exploiting a secondary vulnerability.
>
> To solve the lab, upload a basic PHP web shell and use it to exfiltrate the contents of the file `/home/carlos/secret`. Submit this secret using the button provided in the lab banner.
>
> You can log in to your own account using the following credentials: `wiener:peter`
##### Writeup
Tương tự các bài lab trên, ta sẽ upload webshell php lên bằng chức năng upload avatar.

Tuy nhiên, lúc này có vẻ như server đã không thực thi code của webshell như các bài lab trên. có thể suy ra rằng, server đã cấu hình không cho thực thi code php tại thư mục upload.

Nhưng mà chắc gì server chặn hết các thư mục khác. Mình có thể tận dụng directory traversal tại trường `filename` để lưu file upload vào một thư mục khác. Cụ thể, nếu `filename=../exploit.php` thì rất có thể file upload sẽ được lưu vào thư mục cha của thư mục upload.

Tuy nhiên, kết quả không như mong muốn. Có thể server đã validate chỗ `../` này. Ta thử encode thành `..%2f` thì thấy server đã trả về file lưu vào `avatars/../exploit.php` thành công.

Lúc này chỉ cần truy cập đường dẫn `/files/exploit.php`, ta sẽ nhận được nội dung của file `/hme/carlos/secret` → server chỉ cấu hình cấm thực thi php tại thư mục `files/avatars/` còn `files/` thì không.

Submit secret trên và ta solve được challenge.

### 4. Web shell upload via extension blacklist bypass
##### Description
> This lab contains a vulnerable image upload function. Certain file extensions are blacklisted, but this defense can be bypassed due to a fundamental flaw in the configuration of this blacklist.
>
> To solve the lab, upload a basic PHP web shell, then use it to exfiltrate the contents of the file `/home/carlos/secret`. Submit this secret using the button provided in the lab banner.
>
> You can log in to your own account using the following credentials: `wiener:peter`
##### Writeup
Tương tự các bài lab trên, ta sẽ upload webshell php lên bằng chức năng upload avatar. Tuy nhiên, server đã cấu hình chặn upload file php.

Thử đổi extension từ `exploit.php` thành `exploit.php.jpg` thì ta đã bypass thành công.

Tuy nhiên khi truy cập đường dẫn xem file thì code trong webshell đã không được thực thi. Dựa vào response trả về, ta biết được server đang sử dụng Apache → Có thể server chặn thực thi php tại thư mục `avatars/` này bằng file `.htaccess`. Và tất nhiên nó cũng chặn Directory Traversal nếu không ta sẽ exploit theo bài lab 4.

Như vậy, ta hoàn toàn có thể ghi đè file .htaccess bằng cách upload file `.htaccess` mới. Cụ thể, nó có nội dung:
```
AddType application/x-httd-php .pwned
```
Câu config trên có nghĩa rằng, nếu có file nào chứa extension `.pwned` trong thư mục hiện tại, server thực thi code php trong file đó nếu có.

Bay giờ ta sẽ upload webshell với filename là `exploit.pwned` thay vì `exploit.php`.

Truy cập đường dẫn chứa file tại `/files/avatars/exploit.pwned`, ta lấy được secret của `carlos`.

Submit secret đó và ta solve được challenge.

### 5. Web shell upload via obfuscated file extension
##### Description
> This lab contains a vulnerable image upload function. Certain file extensions are blacklisted, but this defense can be bypassed using a classic obfuscation technique.
>
> To solve the lab, upload a basic PHP web shell and use it to exfiltrate the contents of the file `/home/carlos/secret`. Submit this secret using the button provided in the lab banner.
>
> You can log in to your own account using the following credentials: `wiener:peter`
##### Writeup
Đối với bài lab này, server thực hiện check extension của file upload lên. Khi upload file `exploit.php` thì bị chặn.

Thử obfuscate từ `exploit.php` thành `exploit.pHp` cũng không thành công.

Có vẻ, server chỉ cho phép upload `.jpg` hoặc `.png`. Sử dụng các cách sau: `exploit.php.jpg`, `exploit.php.png` thì upload thành công. Tuy nhiên, code trong webshell không được thi.

Ta sẽ bypass bằng cách chèn null byte vào sau `.php` để trở thành `exploit.php%00.jpg`. Lúc này server vẫn check file là `.jpg`. Tuy nhiên, khi lưu file thì hệ thống sẽ thấy null byte và ngắt chuỗi → file được lưu là `exploit.php`.

Truy cập đường dẫn chứa file upload và ta nhận được secret cần tìm.

Submit secret trên và ta solve được challenge.

### 6. Remote code execution via polyglot web shell upload
##### Description
> This lab contains a vulnerable image upload function. Although it checks the contents of the file to verify that it is a genuine image, it is still possible to upload and execute server-side code.
>
> To solve the lab, upload a basic PHP web shell and use it to exfiltrate the contents of the file `/home/carlos/secret`. Submit this secret using the button provided in the lab banner.
>
> You can log in to your own account using the following credentials: `wiener:peter`
##### Writeup
Bài lab này sử dụng cơ chế validate ảnh dựa trên đặc trưng của file ảnh chứ không theo `Content-Type` hay extension từ người dùng. Khi upload webshell sẽ báo `Error: file is not a valid image`.

Có một cơ chế validate đó là kiểm tra file signature bằng magic bytes trên header của file. File PNG sẽ luôn bắt đầu bằng `89 50 4E 47 0D 0A 1A 0A`. Do đó khi thêm magic bytes này vào trước file php chứa webshell, server sẽ bị bypass.

Upload thành công file webshell sau khi thêm magic bytes.

Truy cập đường dẫn chứa file upload và ta có được secret cần tìm.

Submit secret và ta solve được challenge.

### 7. Web shell upload via race condition
##### Description
> This lab contains a vulnerable image upload function. Although it performs robust validation on any files that are uploaded, it is possible to bypass this validation entirely by exploiting a race condition in the way it processes them.
>
> To solve the lab, upload a basic PHP web shell and use it to exfiltrate the contents of the file `/home/carlos/secret`. Submit this secret using the button provided in the lab banner.
>
> You can log in to your own account using the following credentials: `wiener:peter`
##### Writeup
###### tags: `portswigger`, `file-upload`