PyInstaller封装程序反编译 PyInstxtractor + Uncompale

内容介绍

PyInstaller封装程序的反编译:python

  1. 使用PyInstaller Extractor 提取由PyiIstaller生成的可执行文件内容
  2. 使用Uncompyle进行反编译

软件版本:
Python 3.7.3
PyInstaller 3.6
PyInstxtractor 2.0
Uncompyle 3.6.4git

使用PyInstxtractor提取 *.pyc 文件

工具准备:
PyInstaller Extractor 项目地址:https://github.com/extremecoders-re/pyinstxtractor
CSDN本地下载:https://download.csdn.net/download/ZH013/12284418github

工做目录:web

目录: D:\Python_Project\CFG\dist

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        2020/3/24     15:37       45937021 Main_cfg_editor.exe
-a----        2020/3/25     14:33          13194 pyinstxtractor.py

提取文件:bootstrap

(base) PS D:\Python_Project\CFG\dist> python pyinstxtractor.py Main_cfg_editor.exe
[+] Processing Main_cfg_editor.exe
[+] Pyinstaller version: 2.1+
[+] Python version: 37
[+] Length of package: 45659005 bytes
[+] Found 2823 files in CArchive
[+] Beginning extraction...please standby
[+] Possible entry point: pyiboot01_bootstrap.pyc
[+] Possible entry point: pyi_rth_pkgres.pyc
[+] Possible entry point: pyi_rth_win32comgenpy.pyc
[+] Possible entry point: pyi_rth_multiprocessing.pyc
[+] Possible entry point: pyi_rth_traitlets.pyc
[+] Possible entry point: pyi_rth__tkinter.pyc
[+] Possible entry point: Main_cfg_editor.pyc
[+] Found 1627 files in PYZ archive
[+] Successfully extracted pyinstaller archive: Main_cfg_editor.exe

You can now use a python decompiler on the pyc files within the extracted directory
(base) PS D:\Python_Project\CFG\dist>

使用Uncompyle反编译 *.pyc 文件

工做准备:segmentfault

Uncompyle 库的安装:pip install uncompyle编辑器

工做目录:svg

请注意主文件和主文件中调用的模块文件在不一样的目录下,主文件目录:工具

(base) PS D:\Python_Project\CFG\dist\Main_cfg_editor.exe_extracted>

模块文件目录:.net

(base) PS D:\Python_Project\CFG\dist\Main_cfg_editor.exe_extracted\PYZ-00.pyz_extracted>

反编译:

(base) PS D:\Python_Project\CFG\dist\Main_cfg_editor.exe_extracted> uncompyle6 Main_cfg_editor.pyc > Main_cfg_editor.py
(base) PS D:\Python_Project\CFG\dist\Main_cfg_editor.exe_extracted\PYZ-00.pyz_extracted> uncompyle6 create_cfg.pyc > create_cfg.py

常见错误信息

Invalid pyc/pyo file - Magic value mismatch!
缘由:因为每一个 *.pyc 文件都有一个magic head,PyInstaller 生成 *.exe 的时候会把*.pyc 的 magic 部分去掉,在反编译的时候须要补齐,高版本 PyInstxtractor 2.0 已经解决这个问题。

若是须要手动补齐 magic head 的状况下:

  1. 使用16进制模式查看主文件与主文件目录下的 struct 文件,须要在主文件头插入16个字节与 struct文件保持一致(其中前4个字节是Python编译版本,要彻底一致)
  2. 注意模板文件仅须要插入8个字节,与 struct 文件保持一致

ValueError: source code string cannot contain null bytes
一般在反编译后执行 *.py 文件时发生,缘由时反编译后的文件中存在一些空字符。最简单的解决办法是把文本复制到其余文本编辑器(例如UltraEdit),而后复制回来。

参考文献

致谢:
[1]: PyInstaller将Python文件打包为exe后如何反编译(破解源码)以及防止反编译
[2]: python3.7反编译生成的.exe
[3]: python3.7.4反编译生成的.exe
[4]: ValueError: source code string cannot contain null bytes
[5]: Python 2.6.2的.pyc文件格式