Python 打包成 exe 之后,是否能從二進制文件中恢復出源代碼?沒有加密的話是可以的。
首先需要解包。
直接從 github 上下載就行:https://github.com/countercept/python-exe-unpacker
使用也簡單:python pyinstxtractor.py xxx.exe
解包后,得到 xxx.exe_extracted 就是所有的 pyc 文件了。
找到 xxx.exe_extracted\struct 中的 pyc 簽名信息:
然后可以並使用下面的腳本進行拼接(PYZ-00.pyz_extracted 里面的 pyc 只缺中間一部分):
import os import argparse parser =argparse.ArgumentParser() parser.add_argument('--filename', '-f', type=str, help="file name of the file to be modified!") args = parser.parse_args() if args.filename: print("filename: " + args.filename) if os.access(args.filename, os.W_OK): print("- processing...") else: print("- access is denied! exit!") exit(0) else: print("-h for help!") exit(0) structBytes=b'\x70\x79\x69\x30\x10\x01\x00\x00' with open(args.filename, "rb") as f: bytes = f.read() bytes=bytes[:8]+structBytes+bytes[12:] with open(args.filename, "wb") as g: g.write(bytes) print("- successed!")
多個文件的話。。。再疊加個批處理吧,順便直接用 uncompyle6 把 pyc 反編譯成 python 源代碼 = =
set "path" "C:\DevApp\Anaconda3\Scripts;C:\DevApp\Anaconda3;C:\DevApp\Anaconda3\DLLs;C:\DevApp\Anaconda3\\Library\bin;%path%" /m
@echo off set here=%~dp0 pushd %here% cd xxx.exe_extracted\PYZ-00.pyz_extracted for /f "delims=" %%i in ('dir /s/b "*.pyc"') do (
python.exe %here%\cooking.py -f %%i
start uncompyle6 -o . %%i ) popd
這里之所以用 start,是因為有些 pyc 沒法直接反編譯,這樣的話 script 會被卡住。用 start 開始新進程,並行處理,有一兩個卡住也沒關系。
最后發現有 4 個沒法反編譯,從文件大小可以看出來:
還好,可以從文件名猜到用了什么庫。