# Cybersecurity Student Contest Vietnam 2025
## DNS Exfill
### Phân tích tổng quát
**Description:**

* Challenge cho mình file zip, unzip thì được 3 file (1 file pcap và 2 file log)
* Đầu tiên mình sẽ đi phân tích file pcap trước, ở đây ta thấy chỉ có một phương thức DNS, trong đó chứa nhiều **DNS query names**, trong đó có labels dạng hex thì sau một hồi mình lướt xuống phía dưới thì có các dòng hex chứa tên miền `hex.cloudflar3.com`. Mình sẽ đi liệt kê các dòng hex có chứa tên miền này theo thứ tự.
```
┌──(kali㉿kali)-[~/Desktop/dns_exfil/dnsexfil]
└─$ tshark -r 10.10.0.53_ns_capture.pcap -Y "dns.qry.name contains hex.cloudflar3.com" -T fields -e frame.time_epoch -e ip.src -e dns.qry.name
1760509440.192821000 10.10.5.80 p.c7aec5d0d81ba8748acac6931e5add6c24b635181443d0b9d2.hex.cloudflar3.com
1760509440.212821000 10.10.0.53 p.c7aec5d0d81ba8748acac6931e5add6c24b635181443d0b9d2.hex.cloudflar3.com
1760509440.426899000 10.10.5.80 p.f8aad90d5fc7774c1e7ee451e755831cd02bfaac3204aed8a4.hex.cloudflar3.com
1760509440.446899000 10.10.0.53 p.f8aad90d5fc7774c1e7ee451e755831cd02bfaac3204aed8a4.hex.cloudflar3.com
1760509440.497508000 10.10.5.80 p.3dfec8a22cde4db4463db2c35742062a415441f526daecb59b.hex.cloudflar3.com
1760509440.517508000 10.10.0.53 p.3dfec8a22cde4db4463db2c35742062a415441f526daecb59b.hex.cloudflar3.com
1760509440.599459000 10.10.5.80 p.f6af1ecb8cc9827a259401e850e5e07fdc3c1137f1.hex.cloudflar3.com
1760509440.619459000 10.10.0.53 p.f6af1ecb8cc9827a259401e850e5e07fdc3c1137f1.hex.cloudflar3.com
1760509443.714885000 10.10.5.80 f.6837abc6655c12c454abe0ca85a596e98473172829581235dd.hex.cloudflar3.com
1760509443.734885000 10.10.0.53 f.6837abc6655c12c454abe0ca85a596e98473172829581235dd.hex.cloudflar3.com
1760509443.769962000 10.10.5.80 f.95380b06bf6dd06b89118b0003ea044700a5f2c4c106c3.hex.cloudflar3.com
1760509443.789962000 10.10.0.53 f.95380b06bf6dd06b89118b0003ea044700a5f2c4c106c3.hex.cloudflar3.com
```
* Ta thấy nếu nối hai phần hex theo thứ tự này ta được chuỗi hex có độ dài 96 hex characters → 48 bytes. Mà 48 bytes là bội của 16 (AES block = 16 bytes) — khả năng cho thấy có thể là ciphertext AES, tức là dữ liệu đã được mã hoá bằng AES-CBC/AES-256-CBC hoặc tương tự.
Nên là để giải được nội dung /flag thì cần khóa (key) để giải AES — khả năng key này nằm trong file .env (thường tên APP_KEY, ENCRYPTION_KEY, APP_SECRET,...)
* File pcap không còn gì để mình phân tích cả. Mình sẽ chuyển qua 2 file log phân tích.
* Mình sẽ qua file `access.log` thì sau một hồi mình căng mắt ra coi thì mình thấy có các dòng.
```
192.168.13.37 - - [10/Oct/2025:09:26:10 +0700] "GET /media/getfile.php?file=L2V0Yy9wYXNzd2Q%3D&prefix=p HTTP/1.1" 200 832 "-" "curl/7.88.1"
192.168.13.37 - - [10/Oct/2025:09:26:12 +0700] "GET /media/getfile.php?file=L2ZsYWc%3D&prefix=f HTTP/1.1" 200 118 "-" "curl/7.88.1"
192.168.13.37 - - [10/Oct/2025:09:26:14 +0700] "GET /media/getfile.php?file=L3Zhci93d3cvaHRtbC8uZW52&prefix=e HTTP/1.1" 404 651 "-" "curl/7.88.1"
```
* Lên Cyberchef giải mã URL Decode --> From Base64 ta được lần lượt.
```
/etc/passwd
/flag
/var/www/html/.env
```
* File access.log cho thấy attacker IP đã gọi getfile.php?file=<base64>&prefix=..., endpoint dùng để yêu cầu server đọc file. `file=` chứa dữ liệu được mã hóa (URL-encoded, Base64).
* Ta thấy:
Server trả nội dung (200 + kích thước). Thay vì attacker tải trực tiếp file qua HTTP response, mã độc hoặc hàm trên server chia nội dung file thành các chunk, mã hoá hoặc chuyển sang hex, và gửi từng chunk đi bằng DNS queries với labels như p.<hex>... hoặc f.<hex>....
* prefix=p tương ứng stream p → thường map tới một file (ở đây /etc/passwd),
* prefix=f tương ứng stream f → map tới /flag.
* Qua tiếp file errol.log (là log của nginx/php-fpm/nginx error stream) là file lưu các thông báo lỗi, cảnh báo và thông điệp debug được sinh ra bởi webserver (nginx), PHP-FPM hoặc ứng dụng PHP.
* File thường ghi:
* PHP notices / warnings / errors
* Lỗi liên quan FastCGI / upstream
* Các thông điệp debug (nếu dev bật error_log() hoặc var_dump() vào stderr)
* Một số thông tin runtime hữu ích
* Ở challege này file error.log tiết lộ APP_SECRET và cả công thức derive key/iv — chính điều này biến dữ liệu exfil (dù mã hoá) thành dữ liệu có thể giải mã.
```
2025/10/10 09:26:03 [error] 1224#0: *5210 FastCGI sent in stderr: "PHP Fatal error: Uncaught Exception: Debug mode enabled in /var/www/html/media/getfile.php:41\nStack trace:\n#0 /var/www/html/media/getfile.php(21): build_key()\n#1 {main}\n thrown" while reading response header from upstream, client: 192.168.13.37, server: portal.local, request: "GET /media/getfile.php?debug=true HTTP/1.1", upstream: "fastcgi://unix:/run/php/php8.2-fpm.sock:", host: "intra.portal.local"
2025/10/10 09:26:03 [error] 1224#0: *5210 FastCGI sent in stderr: "PHP Notice: DEBUG VARS: APP_SECRET=F0r3ns1c-2025-CSCV; DATE_UTC=20251010" while reading response header from upstream, client: 192.168.13.37, server: portal.local, request: "GET /media/getfile.php?debug=true HTTP/1.1", upstream: "fastcgi://unix:/run/php/php8.2-fpm.sock:", host: "intra.portal.local"
2025/10/10 09:26:03 [error] 1224#0: *5210 FastCGI sent in stderr: "PHP Notice: H=SHA256(APP_SECRET); AES_KEY=H[0..15]; AES_IV=H[16..31]" while reading response header from upstream, client: 192.168.13.37, server: portal.local, request: "GET /media/getfile.php?debug=true HTTP/1.1", upstream: "fastcgi://unix:/run/php/php8.2-fpm.sock:", host: "intra.portal.local"
2025/10/10 09:26:08 [warn] 1224#0: *5300 FastCGI sent in stderr: "PHP Deprecated: Required parameter $request follows optional parameter $opts in /var/www/html/vendor/guzzlehttp/guzzle/src/Client.php on line 112" while reading response header from upstream, client: 172.20.9.10, server: portal.local, request: "GET /api/v1/messages HTTP/1.1", upstream: "fastcgi://unix:/run/php/php8.2-fpm.sock:", host: "intra.portal.local"
```
### Solution
* Ta đã trích DNS queries dạng hex theo thời gian ở trên kia rồi ghép các chunk hex (theo thứ tự thời gian), giải AES-CBC (16-byte key/IV) rồi lược padding PKCS#7
* Tới đây thì mình nhờ ChatGPT giải giúp và thu được flag.
Flag: **`CSCV2025{DnS_Exf1ltr4ti0nnnnnnnnnnNN!!}`**
###