Last active
April 19, 2024 21:22
-
-
Save Joshix-1/9e1c3481562a2873a94d12c1548dcc67 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import annotations | |
| import base64 | |
| import lzma | |
| import marshal | |
| import sys | |
| import traceback | |
| import types | |
| from collections.abc import Callable | |
| from functools import partial | |
| from pathlib import Path | |
| def m_dumps(o: types.CodeType | str, version: int) -> bytes: | |
| return marshal.dumps(o, version) | |
| def dumps_code(code_obj: types.CodeType | str) -> bytes: | |
| if isinstance(code_obj, str): | |
| return repr(code_obj).encode() | |
| arguments = ( | |
| "argcount", "posonlyargcount", "kwonlyargcount", "nlocals", "stacksize", "flags", "code", "consts", "names", "varnames", | |
| "filename", "name", | |
| "qualname", | |
| "firstlineno", "linetable", "exceptiontable", "freevars", "cellvars" | |
| ) | |
| data = ( | |
| (key, getattr(code_obj, f"co_{key}")) | |
| for key in arguments | |
| ) | |
| args: list[bytes] = [ | |
| ( | |
| (b"__import__('marshal').loads(__import__('base64').b85decode(" + repr(base64.b85encode(marshal.dumps(val))).encode() + b"))") | |
| if name == "consts" | |
| else repr(val).encode() | |
| ) | |
| for name, val in data | |
| ] | |
| return b"type((lambda:0).__code__)(" + b",".join(args) + b")" | |
| COMPRESSIONS: list[tuple[Callable[[bytes], bytes], str]] = [ | |
| (__import__("bz2").compress, "__import__('bz2').decompress"), | |
| (__import__("gzip").compress, "__import__('gzip').decompress"), | |
| (__import__("lzma").compress, "__import__('lzma').decompress"), | |
| (partial(lzma.compress, preset=9 | lzma.PRESET_EXTREME), "__import__('lzma').decompress"), | |
| (lambda _: _, ""), | |
| ] | |
| CONVERTS: list[tuple[Callable[[types.CodeType | str], bytes], str]] = [ | |
| (marshal.dumps, "__import__('marshal').loads"), | |
| *( | |
| (partial(m_dumps, version=v), "__import__('marshal').loads") | |
| for v in range(marshal.version) | |
| ), | |
| (dumps_code, "eval"), | |
| ] | |
| text = Path(__file__).read_text() | |
| compiled: types.CodeType = compile( | |
| text, | |
| "__main__", | |
| "exec", | |
| flags=annotations.compiler_flag, | |
| dont_inherit=True, | |
| optimize=2 | |
| ) | |
| compressed: list[tuple[bytes, str, str]] = [] | |
| for code in (compiled, text): | |
| for convert, deconvert in CONVERTS: | |
| for compress, decompress in COMPRESSIONS: | |
| try: | |
| compressed.append( | |
| (compress(convert(code)), deconvert, decompress) | |
| ) | |
| except Exception: | |
| pass # traceback.print_exc() | |
| compressed.sort(key=lambda t: len(t[0])) | |
| c, deconvert, decompress = compressed[0] | |
| del compressed | |
| b = base64.b85encode(c) | |
| print(f"exec({deconvert}({decompress}(__import__('base64').b85decode({b!r}))))") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment