Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decompile Python 3.3 .pyc using unpyc3

Accedentely I lost all my project source code. But I still have my .pyc files. I need help decompiling them. I downloaded unpyc3 script that can decompile python 3.2 files. And made a change to allow it read python 3.3 pyc files correctly:

def read_code(stream): 
# This helper is needed in order for the PEP 302 emulation to 
# correctly handle compiled files
# Note: stream must be opened in "rb" mode
import marshal 
magic = stream.read(4) 
if magic != imp.get_magic(): 
    print("*** Warning: file has wrong magic number ***")
stream.read(8) # Skip timestamp and additional 4 bytes for python 3.3
return marshal.load(stream)         

By running this code, I've got following error: "'str' object has no attribute 'co_cellvars'" here:

class Code:
    def __init__(self, code_obj, parent=None):
        self.code_obj = code_obj
        self.parent = parent
        self.derefnames = [PyName(v)
                       for v in code_obj.co_cellvars + code_obj.co_freevars]

I happens when instead of code object, code_obj, string appears when code class is initializing. I need help figuring out why is this happen and how to fix it. If there is anybody know how unpyc3 works and can help, please write me. I can send .pyc example.

like image 263
Michael M. Avatar asked Feb 28 '14 13:02

Michael M.


1 Answers

After some serious debugging, I found that modifying the code from MAKE_FUNCTION should do the trick :

def MAKE_FUNCTION(self, addr, argc, is_closure=False):
    testType = self.stack.pop().val
    if isinstance(testType, str) :
        code = Code(self.stack.pop().val, self.code)
    else :
        code = Code(testType, self.code)

Hope this helps!

like image 135
MadeCoder Avatar answered Oct 17 '22 04:10

MadeCoder