将Python打包的exe进⾏反编译
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 offset here=%~dp0pushd %here%
cd xxx.exe_extracted\\PYZ-00.pyz_extractedfor /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 个没法反编译,从⽂件⼤⼩可以看出来:
还好,可以从⽂件名猜到⽤了什么库。