将 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
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 个没法反编译,从文件大小可以看出来:

 

 还好,可以从文件名猜到用了什么库。

posted @ 2021-04-20 11:11  Biiigfish  阅读(2115)  评论(0编辑  收藏  举报