# KCSC CTF 2023

------
## Tin học văn phòng

Đề cho ta một file docm, bên trong có macro.
Vậy mình sẽ dùng olevba để xem thử trong đó có gì.

Ta có được đoạn code trên vói flag = `KCSC{H1_1m_sUcky_+$phlac}`
đề cho flag có 2 phần nên $phlac sẽ là phần thứ 2
Đoạn code tìm một Shape có màu RGB(13,3,7) để chạy payload trong shape đó.
Vậy extract file docm ra, vào `document.xml` tìm shape có màu rgb(13,3,7) / #0d0307.

Đoạn code cũng có thêm, cmdParams sẽ là phần text, và trong document phần text cũng chứa đoạn base64 khá sus, nên mình decode nó mình được:

đã thấy biến $phlac
Thử in nó ra xem được gì:

flag:
```
KCSC{H1_1m_sUcky_Tr0ll_m4lw@r3_y0u_g0t_m3_ah1Hi}
```
## Dropper

Đề cho 1 file disk, mở bằng FTK thấy được:

Đầu tiên mình xem thử trong các User chứa gì, và ta thấy trong user Public, có file readme.txt nhưng có vẻ có symlink đến một file khác, twenty.ps1, khá là sus.

export nó ra, ta thấy một powershell script khá dài:

Nhìn dài vậy nhưng thực ra chỉ có vài dòng:

Có vẻ như script này sử dụng để decrypt một cái gì đó sử dụng AES.
Mình sẽ thử decrypt nó xem được gì:

lại là một script khác, có lẽ nó sẽ decrypt nhiều lần lồng với nhau và tên biến khác nhau; vị trí key, ciphertext thay đổi với từng script; mode cũng thay đổi. Vậy mình phải viết một script để decrypt nó nhiều lần:
```python=
from Crypto.Cipher import AES
import base64
import re
import os
def extract_encrypted_data(file_path):
pattern = r"FromBase64String\(\".*?\"\)"
with open(file_path, 'r') as f:
content = f.read()
res = re.findall(pattern, content)
key = res[1].replace("FromBase64String(\"", "").strip(res[1][-2:])
text = res[0].replace("FromBase64String(\"", "").strip(res[0][-2:])
return base64.b64decode(key), base64.b64decode(text)
def decrypt_data(key, text, mode, iv=None):
header = bytes.fromhex('1F8B0800000000000400')
if mode == 'CBC':
cipher = AES.new(key, AES.MODE_CBC, iv)
elif mode == 'ECB':
cipher = AES.new(key, AES.MODE_ECB)
data = cipher.decrypt(text)
if header not in data:
data = header + data
return data
def write_to_file(file_path, data):
with open(file_path, 'wb') as f:
f.write(data)
def decompress_file(file_path):
output_path = file_path[:-3]
os.system(f'gzip -dc < {file_path} > {output_path}')
for idx in range(1, 20):
print(f'Attempt: {idx}')
key, text = extract_encrypted_data(f"test-{idx}")
print(key, end=' ')
print(len(key))
iv = text[:16]
ct = text[16:]
if len(key) < len(text):
key, text = text, key
if '[System.Security.Cryptography.CipherMode]::CBC' in text:
print('CBC')
decrypted_data = decrypt_data(key, ct, mode='CBC', iv=iv)
elif '[System.Security.Cryptography.CipherMode]::ECB' in text:
print('ECB')
decrypted_data = decrypt_data(key, ct, mode='ECB')
write_to_file(f'test-{idx+1}.gz', decrypted_data)
decompress_file(f'test-{idx+1}.gz')
```
Sau khi decrypt 20 lần, mình được file:

thử decode src1 mình được một file jpg chứa flag:

flag:
```
KCSC{Som3one's_th0ugh1s_KMA_don't_have_researcher?}
```
## Action Capture

Đề cho file pcap:

Mình thấy có protocol tcp và có gửi data nên mình follow thử có gì hot:

thấy được bquaman sử dụng output của xinput để ping qua 192.168.253.27


xinput test 8 tương ứng với keyboard và 10 tương ứng với mouse.
Vì sử dụng ping nên sẽ liên quan đến protocol ICMP và ở file pcap này có rất nhiều.

```python=
#tshark -r ActionCapture.pcapng -T fields -e data.data -Y "(icmp) && (icmp.type == 8) && (frame.number < 869)" > kb.txt
#tshark -r ActionCapture.pcapng -T fields -e data.data -Y "(icmp) && (icmp.type == 8) && (frame.number >= 869)" > mouse.txt
with open('kb.txt','r') as f:
kb = f.readlines()
with open('mouse.txt','r') as f:
mouse = f.readlines()
with open('kbx.txt','w') as w:
a = 0
for i in kb:
if a % 2:
w.write(bytes.fromhex(i)[12:17].decode())
# print(bytes.fromhex(i)[12:17])
else:
w.write(bytes.fromhex(i)[12:22].decode())
# print(bytes.fromhex(i)[12:22])
a += 1
# print('====================')
with open('mousex.txt','w') as w:
for i in mouse:
w.write(bytes.fromhex(i)[12:22].decode())
import matplotlib.pyplot as plt
x = []
y = []
with open('mousex.txt','r') as f:
a = f.readlines()
for i in a:
i = i.split(' ')
# print(i)
x.append(int(i[1].split('=')[1]))
y.append(-int(i[-2].split('=')[1]))
print(x,y)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.scatter(x,y,c='r',marker='o')
plt.show()
```

Đối với kbx.txt, mình có được tool trên github để parse sang ký tự:
https://github.com/Wh1t3Rh1n0/xinput-keylog-decoder/blob/master/xinput-keylog-decoder.py
flag:
```
KCSC{g00d_luck_have_fun_1337}
```