Advertisement
MSM

Exe2Py source code mirrpr

MSM
Aug 7th, 2012
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. import dis, imp, struct, os, sys
  2. import marshal
  3. import win32api
  4.  
  5. def exe2py(ifi):
  6.     """py2exe stores the main script (among other things) as resource
  7.   in the image.  This script prints some information, and extracts
  8.   the code objects into the current directory."""
  9.  
  10.     handle = win32api.LoadLibrary(ifi)
  11.     data = win32api.LoadResource(handle, "PYTHONSCRIPT", 1, 0)
  12.  
  13.     # The resource data has 3 parts:
  14.     # 1. header: magic number (0x78563412), optimize flag, unbuffered flag, script length in bytes
  15.     magic, optimize, unbuffered, script_len = struct.unpack("iiii", data[:4*4])
  16.     print "HEADER:", hex(magic), optimize, unbuffered, script_len
  17.     if magic != 0x78563412:
  18.         print "Warning: magic number incompatible"
  19.  
  20.     data = data[4*4:]
  21.     # 2. name of the zip-archive (relative path, NUL terminated)
  22.     zipname, data = data.split("\0", 1)
  23.     print "ZipArchive:", zipname
  24.  
  25.     # 3. a sequence of marshaled code objects.  Typically, the boot
  26.     # script plus the main script; but there may be more.
  27.     code_objects = marshal.loads(data + "foo bar spam")
  28.     for co in code_objects:
  29.         print "Found code object:", co.co_filename
  30.         fname = os.path.basename(co.co_filename)
  31.         if os.path.splitext(fname)[1]:
  32.             if optimize:
  33.                 fname = fname + 'o'
  34.             else:
  35.                fname = fname + 'c'
  36.             print "\tExtracting to:", fname
  37.             # a .pyc or .pyo file is a magic number, a timestamp, plus the
  38.             # marshaled code object
  39.             open(fname, "wb").write(imp.get_magic() + "\0\0\0\0" + marshal.dumps(co))
  40.         else:
  41.             print "\tDisassembly:"
  42.             dis.dis(co)
  43.             continue
  44.  
  45. if __name__ == "__main__":
  46.     exe2py(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement