Last active
May 14, 2025 18:16
-
-
Save siddolo/59a8ce248cb0afe705989715cf62090f to your computer and use it in GitHub Desktop.
Dump all memory-loaded modules with accessible bytecode (.pyc)
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
# pip install uncompyle6 | |
# cd /tmp/python_memory_dump | |
find ./ -name '*.pyc' -print0 | xargs -0 -n1 -I{} sh -c 'uncompyle6 "{}" > "$(dirname "{}")/$(basename "{}" .pyc).py"' |
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
import sys | |
import os | |
import marshal | |
import imp | |
OUTPUT_DIR = '/tmp/python_memory_dump' | |
MAGIC = b'\x03\xf3\x0d\x0a' # Python 2.7 magic number | |
TIMESTAMP = b'\x00\x00\x00\x00' # placeholder timestamp | |
def dump_module(modname, module): | |
if not hasattr(module, '__file__'): | |
return | |
if not hasattr(module, '__loader__'): | |
return | |
loader = module.__loader__ | |
if loader is None: | |
return | |
if not hasattr(loader, 'get_code'): | |
return | |
code = loader.get_code(modname) | |
if code is None: | |
return | |
relpath = modname.replace('.', '/') + '.pyc' | |
outpath = os.path.join(OUTPUT_DIR, relpath) | |
outdir = os.path.dirname(outpath) | |
if not os.path.exists(outdir): | |
os.makedirs(outdir) | |
f = open(outpath, 'wb') | |
f.write(MAGIC) | |
f.write(TIMESTAMP) | |
marshal.dump(code, f) | |
f.close() | |
print("Wrote:", outpath) | |
print("Dumping all memory-loaded modules with accessible bytecode...") | |
for modname in sys.modules: | |
module = sys.modules[modname] | |
if module is not None: | |
dump_module(modname, module) | |
print("Done.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dump single module