Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Tool:py2exe

From ReverseEngineering.wiki
Tool
Developer py2exe contributors
License MIT, MPL-2.0 (MemoryModule)
Website https://www.py2exe.org
On this wiki Facts · Code · Notes · Topics

Windows Python freezer: the executable is a prebuilt run stub (named for the Python version) holding a PYTHONSCRIPT resource with a marshalled list of code objects, and the modules live in library.zip as pyc files with a zeroed timestamp and size.

Identification

  • The PE has a resource of custom type PYTHONSCRIPT, name 1. Its first dword is the tag 0x78563412 (bytes 12 34 56 78), the program refuses to start with 'Bug: Invalid script resource' otherwise (Fact:py2exe/struct/PYTHONSCRIPT resource).
  • It imports/loads pythonXY.dll (or carries it: with bundle_files 1 the DLL is stored as a resource of library.zip and loaded from memory with the MemoryModule loader). Modules are in library.zip (a ZIP next to the exe, or appended to / embedded in the exe when no name is given), read through a custom zipextimporter.
  • Stub file names in the package are run-pyMAJ.MIN-win32.exe-style, so the Python version is the one the stub was built for; read it from the python DLL name or the stub import table.

PYTHONSCRIPT layout

Struct scriptinfo: int tag = 0x78563412; int optimize; int unbuffered; int data_bytes; followed by the NUL-terminated name of the library zip relative to the exe (empty means the exe itself is the library), then data_bytes of marshal.dumps(list of code objects) and a NUL. The first code objects are py2exe boot code, the last is the main script (Format:Marshal (Python)); unmarshal with the matching Python minor version and decompile the last objects.

library.zip

Each module is a .pyc whose header is the interpreter's magic number followed by zero flags (3.7+), zero timestamp and zero size, then the marshalled code (Format:PYC (Python compiled bytecode)); the magic number tells the Python version. Standard ZIP tools list and extract it. Extensions (.pyd) may be loaded from memory.

Related

Format:PyInstaller archive (CArchive and PYZ), Tool:Nuitka, Technique:Bundled-runtime applications.

Code & repositories

  • https://github.com/py2exe/py2exe @ 1be98bd71ac737f73aa146631ab902b2b1cc43f7 · license: MIT AND MPL-2.0 (declared by the contributor; GitHub's detector did not recognize LICENSE.txt)
    py2exe (MIT; MemoryModule under MPL-2.0): runtime.py builds the exe and library.zip, source/start.c is the run stub.
    Tested on: source read, not run: Read the source at this commit; not built or run. · checked 2026-10-18; last activity 2026-06-21

Evidence & references

  • py2exe/py2exe commit 1be98bd71ac737f73aa146631ab902b2b1cc43f7: py2exe/runtime.py lines 326-375 (stub names, scriptinfo pack, PYTHONSCRIPT), 396-445 (pythonXY.dll as resource, pyc header), source/start.c lines 30-145 (struct and tag check).