How to decompile an exe file compiled by py2exe?

86,782

Solution 1

You can use unpy2exe to extract the .pyc and then use pyREtic to get the source code. I guess you can read the HOWTO and understand how to use these programs, but basically you go to the location of unpy2exe.py and run:

unpy2exe.py [-h] [-o OUTPUT_DIR] [-p PYTHON_VERSION] filename

thene go to the location of REpdb.py and run:

REpdb.py

set_project [new project name]

Select the python version

fs_um_decompile [location of pyc file]

The source should be in ...\Projects[new project name]\sourcecode\fs_um\

Solution 2

Another approach is to take the python byte code dump directly from memory, Immunity Inc published a paper about this subject with a toolkit that takes the bytecode from memory and decompile it to .py source code. pyREtic

Solution 3

I have wrote a small tool in C++ which takes a py2exe generated executable and extracts the PYTHONSCRIPT and Library.zip. The Library.zip contains mostly pyc/pyo files, you can decompile them using any decompiler.

Links :

Py2ExeDumper : https://sourceforge.net/projects/py2exedumper/
Easy Python Decompiler : http://sourceforge.net/projects/easypythondecompiler/

Solution 4

Use script provided here. It requires the original py2exe Python module to be installed. Also you must ensure that you use the same Python version originaly used to produce the executable. Otherwise the script will produce invalid magic number for the .pyc. Later you can use uncompyle2 to restore the original .py source.

Share:
86,782

Related videos on Youtube

toontong
Author by

toontong

good at python,golang,nginx,MySQL,redis

Updated on July 09, 2022

Comments

  • toontong
    toontong almost 2 years

    How to decompile an exe file compiled by py2exe?

    just one exe file, didn'n have any zip file.

    how to decompile to pyc or pyo file?

    • Karl Knechtel
      Karl Knechtel about 13 years
      Why are you hoping to be able to do this?
    • Rafe Kettler
      Rafe Kettler about 13 years
      There's very little you can do to reverse it without the library zip archive.
    • motoku
      motoku about 13 years
      As far as I know an exe file is an encapsulation type because my Linux package manager opens them as such.
    • whyoz
      whyoz about 13 years
      py2exe can bundle the library.zip into the .exe. Assuming that is the case here (and that you're not simply missing the library.zip), then you can simply extract the files from the .exe (treat it as a zipfile) and you're left with .pyc files. For decompiling pyc to py, see here: stackoverflow.com/questions/378127/pyc-to-py-files
  • Martin Dorey
    Martin Dorey over 4 years
    unpy2exe worked like a charm for me. pyREtic, however, failed with a TypeError about a string being a list, which I failed to decode on the interwebs. That tool looks like it's designed to undo obfuscation of bytecode. For my problem, uncompyle6 was enough. Linking to the source was the right thing to do but pip install worked for both uncompyle6 and unpy2exe.

Related