目测python3.0+环境首发(手动滑稽)

今天试着反编译一个群友用python写的.exe,上流程。

该网友做了一个搜索引擎,功能如图:

app 反编译python python3反编译_搜索引擎


然后 自然是第一步上搜索引擎搜一下如何反编译,大概总结一下:

获取python的.pyc(http://blog.sina.com.cn/s/blog_17bce02530102ya3k.html)

这里用到的工具是:pyinstxtractor.py(下面链接有下载)

将pyinstxtractor.py与目标exe放置在同一个文件夹

python pyinstxtractor.py 搜索引擎.exe

得到:

(test) H:\>python pyinstxtractor.py 搜索引擎.exe
pyinstxtractor.py:86: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
[*] Processing 搜索引擎.exe
[*] Pyinstaller version: 2.1+
[*] Python version: 37
[*] Length of package: 5751671 bytes
[*] Found 64 files in CArchive
[*] Beginning extraction...please standby
[+] Possible entry point: pyiboot01_bootstrap
[+] Possible entry point: pyi_rth_certifi
[+] Possible entry point: 搜索引擎
[*] Found 248 files in PYZ archive
[*] Successfully extracted pyinstaller archive: 搜索引擎.exe

这里可以获得该程序用的pyinstaller版本和python版本
注意如果你当前python版本与目标程序版本不同,会是这样的:

[*] Processing 搜索引擎.exe
[*] Pyinstaller version: 2.1+
[*] Python version: 37
[*] Length of package: 5751671 bytes
[*] Found 64 files in CArchive
[*] Begining extraction...please standby
[*] Found a PYC object inside of pyiboot01_bootstrap... Unmarshalling it to fix MAGIC headers.
[*] Found a PYC object inside of pyi_rth_certifi... Unmarshalling it to fix MAGIC headers.
[*] Found a PYC object inside of 搜索引擎... Unmarshalling it to fix MAGIC headers.
[!] Warning: The script is running in a different python version than the one used to build the executable
    Run this script in Python37 to prevent extraction errors(if any) during unmarshalling**
[*] Found 248 files in PYZ archive
[*] Successfully extracted pyinstaller archive: 搜索引擎.exe

[!] Warning: The script is running in a different python version than the one used to build the executable,Run this script in Python37 to prevent extraction errors(if any) during unmarshalling**

提示与你当前的版本不同可能会有一些未知问题,所以这里我用anconda新建了一个python3.7的环境。

然后获得一个文件夹:

app 反编译python python3反编译_搜索引擎_02


在文件夹下找到主函数:

app 反编译python python3反编译_搜索引擎_03


这里文件格式没有显示为.pyc 查了一下可能是pyinstxtractor的问题,这不重要。

然后用16进制编辑器,打开搜素引擎和struct ,这里网上几乎所有的教程(墙内)都是从stract文件中获取前8个字节(magic)补充到目标文件,我照抄不误

然后利用Easy Python Decompiler v1.3.2进行反编译遇到以下错误:

Invalid pyc/pyo file - Magic value mismatch!

使用uncompyle进行反编译:

<class 'ValueError'>; bad marshal data (unknown type code)
ValueError: bad marshal data (unknown type code)

这应该就是magic码没有正确还原。
key:由于每个pyc文件都有一个magic head,pyinstaller生成exe的时候会把pyc的magic部分去掉,在反编译的时候需要自己补齐。python2,需要补8个字节,后面的4个字节是时间戳,前面的
4个字节是python编译的版本。

以上key作者是在python2环境下,可能由于python版本的不同,字节的数量可能有影响,因为前4个字节是代表版本的意思,所以我大胆的尝试,根据16进制分析:

app 反编译python python3反编译_搜索引擎_04


app 反编译python python3反编译_hive_05


可以看出在struct和搜素引擎中都有E3,struct中E3位于17字节,在搜索引擎中位于9字节,所以大胆将struct中前16字节添加到搜索引擎中(这里上边KEY说了这是版本和时间戳的表示,所以在这两个文件中要求应该一样),修改保存,并将搜索引擎改为.pyc

然后我使用了uncompyle进行py还原:

pip install uncompyle
uncompyle6 搜索引擎.pyc > 搜索引擎.py

app 反编译python python3反编译_hive_06


没有报错,成功生成:

app 反编译python python3反编译_反编译_07


app 反编译python python3反编译_反编译_08


乱码部分是code问题,且都是中文,不影响。

完美!

Q:expecting code indicator ‘c’; got ’
这是当前python版本和目标程序python版本不同报错

如果文章对你有所帮助,那就点个赞吧!

协助文章:
https://www.lizenghai.com/archives/898.html
上述文章也介绍了如何保护源码