內容介紹
PyInstaller封裝程序的反編譯:
- 使用PyInstaller Extractor 提取由PyiIstaller生成的可執行文件內容
- 使用Uncompyle6進行反編譯
PyiIstaller生成的可執行文件
如果要建立獨立應用,那么需要把python的依賴包全部打包。
安裝:
pip install pyinstaller
打包:
pyinstaller -F -w XXX.py
打包完成后,會建立兩個文件夾,分別是build
和dist
,可執行文件就在dist
下面,同時需要把程序用到的文件放到同一個目錄(如果用的是相對路徑的話)
build
下面有一個warn-UI.txt
文件,如果打包完成后無法運行文件可以在此查看問題
以下是pyinstaller
的常用選項
-h,--help 查看該模塊的幫助信息
-F,-onefile 產生單個的可執行文件
-a,--ascii 不包含 Unicode 字符集支持
-d,--debug 產生 debug 版本的可執行文件
-w,--windowed,--noconsolc 指定程序運行時不顯示命令行窗口(僅對 Windows 有效)
-c,--nowindowed,--console 指定使用命令行窗口運行程序(僅對 Windows 有效)
-o DIR,--out=DIR 指定 spec 文件的生成目錄。如果沒有指定,則默認使用當前目錄來生成 spec 文件
-p DIR,--path=DIR 設置 Python 導入模塊的路徑(和設置 PYTHONPATH 環境變量的作用相似)。也可使用路徑分隔符(Windows 使用分號,Linux 使用冒號)來分隔多個路徑
-n NAME,--name=NAME 指定項目(產生的 spec)名字。如果省略該選項,那么第一個腳本的主文件名將作為 spec 的名字
-D,--onedir 產生一個目錄(包含多個文件)作為可執行程序
使用PyInstxtractor提取 *.pyc 文件
工具准備:
PyInstaller Extractor 項目地址:https://github.com/extremecoders-re/pyinstxtractor
工作目錄:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/3/24 15:37 45937021 Main_cfg_editor.exe
-a---- 2020/3/25 14:33 13194 pyinstxtractor.py
提取文件:
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
使用Uncompyle反編譯 *.pyc 文件
工作准備:
Uncompyle6 庫的安裝:
pip install uncompyle6
反編譯:
uncompyle6 Main_cfg_editor.pyc > Main_cfg_editor.py
常見錯誤信息
Invalid pyc/pyo file - Magic value mismatch!
原因:由於每個 *.pyc 文件都有一個magic head,PyInstaller 生成 .exe 的時候會把.pyc 的 magic 部分去掉,在反編譯的時候需要補齊,高版本 PyInstxtractor 2.0 已經解決這個問題。
如果需要手動補齊 magic head 的情況下:
使用16進制模式查看主文件與主文件目錄下的 struct 文件,需要在主文件頭插入16個字節與 struct文件保持一致(其中前4個字節是Python編譯版本,要完全一致)
注意模板文件僅需要插入8個字節,與 struct 文件保持一致
ValueError: source code string cannot contain null bytes
通常在反編譯后執行 *.py 文件時發生,原因時反編譯后的文件中存在一些空字符。最簡單的解決辦法是把文本復制到其他文本編輯器(例如UltraEdit),然后復制回來。
如使用python 3.9 報錯
Error: uncompyle6 requires Python 2.6-3.8
我們只需要給uncompile
包加一個3.9支持
.pyc文件首部損壞
struct.pyc
文件包含一個完整可用的首部,可以用來修復.pyc文件.