Exiting the vm2 sandbox – HTB Codify: Hacker’s Journey

In this writeup I will show you how to escape from vm2 sandbox. Then we will get the RCE on the server, find the database and extract the credentials from it. And to increase privileges, we have to exploit the vulnerability in a user script in Bash.

Our goal is to obtain superuser rights on the Codify machine from the training site Hack The Box. The difficulty level of the car is easy.

warning

It is recommended to connect to machines with HTB only via VPN. Do not do this from computers that contain data that is important to you, as you will end up on a shared network with other participants.

Intelligence service

Port scanning

Add the machine's IP address to /etc/hosts:

И запус­каем ска­ниро­вание пор­тов.

Справка: сканирование портов

Ска­ниро­вание пор­тов — стан­дар­тный пер­вый шаг при любой ата­ке. Он поз­воля­ет ата­кующе­му узнать, какие служ­бы на хос­те при­нима­ют соеди­нение. На осно­ве этой информа­ции выбира­ется сле­дующий шаг к получе­нию точ­ки вхо­да.

На­ибо­лее извес­тный инс­тру­мент для ска­ниро­вания — это Nmap. Улуч­шить резуль­таты его работы ты можешь при помощи сле­дующе­го скрип­та:

#!/bin/bash

ports=$(nmap -p- --min-rate=500 $1 | grep ^(0-9) | cut -d '/' -f 1 | tr 'n' ',' | sed s/,$//)

nmap -p$ports -A $1

It works in two stages. The first one performs a regular quick scan, the second one performs a more thorough scan using the available scripts (option -A).

The result of the script

The scanner found three open ports:

  • port 22 - OpenSSH 8.9p1 service;
  • port 80 - Apache web server 2.4.52;
  • port 3000 - Node.js Express application.

Based on the list of open ports, it is clear that in order to gain access to the server, you need to find and exploit a vulnerability on the site (it is not accepted to brute force the SSH password on HTB).

Home page of the site codify.htb
Home page of the site codify.htb

The site allows you to test Node.js code in an isolated environment directly on the site.

Node.js code editor
Node.js code editor

Point of entry

The About us page has information about the code editor. It uses a JavaScript sandbox for execution vm2.

Contents of the About us page
Contents of the About us page

The link on the site does not lead to the latest version of the vm2 library, but to version 3.9.16, which is probably used on the server.