背景:最近在帮朋友写一款类似抢票的脚本,朋友有好几个脚本了,但是效果不理想,想让我帮忙,由于这种需要对接口进行详细了解,有些事件没有条件去抓包,然后我就对朋友已有的几个脚本动了心思。首先像这种爬虫类小软件八九不离十,都是python写的,然后正好其中一款表现较好的软件打包的时候连应用程序图标都没改,大大的Python Logo,网上参考了文章来对其进行了反编译,得到了源码,然后再在别人的代码基础上进行修改,大大开发降低了难度。
参考文章:谈谈 Pyinstaller 的编译和反编译,如何保护你的代码
首先,简单写一段Python代码。输出0-49的偶数,我知道range可以通过步长的方式实现,只是加多一个判断语句,显得不至于太过简单。

# 这是一段极为简单的测试代码
def foo():for i in range(50):if not i%2:print(i)if __name__ == '__main__':foo()

代码运行结果

PS Python反编译测试> python .\main.py
0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
PS Python反编译测试>

本次为方便使用使用的是conda创建的新环境,没有多余的包。安装pyinstaller

pip install pyinstaller

使用pyinstaller进行打包

(blogTest) PS C:\Users\lkt\Desktop\Python反编译测试> pyinstaller.exe -F .\main.py
674 INFO: PyInstaller: 5.2
674 INFO: Python: 3.8.13 (conda)
690 INFO: Platform: Windows-10-10.0.22000-SP0
692 INFO: wrote C:\Users\lkt\Desktop\Python反编译测试\main.spec
695 INFO: UPX is not available.
705 INFO: Extending PYTHONPATH with paths
['C:\\Users\\lkt\\Desktop\\Python反编译测试']
1101 INFO: checking Analysis
1102 INFO: Building Analysis because Analysis-00.toc is non existent
1102 INFO: Initializing module dependency graph...
1104 INFO: Caching module graph hooks...
1136 INFO: Analyzing base_library.zip ...
5874 INFO: Processing pre-find module path hook distutils from 'C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
5875 INFO: distutils: retargeting to non-venv dir 'C:\\ide\\anaconda3\\envs\\blogTest\\lib'
10915 INFO: Caching module dependency graph...
11135 INFO: running Analysis Analysis-00.toc
11159 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executablerequired by C:\ide\anaconda3\envs\blogTest\python.exe
12028 INFO: Analyzing C:\Users\lkt\Desktop\Python反编译测试\main.py
12031 INFO: Processing module hooks...
12033 INFO: Loading module hook 'hook-difflib.py' from 'C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\hooks'...
12035 INFO: Loading module hook 'hook-distutils.py' from 'C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\hooks'...
12036 INFO: Loading module hook 'hook-distutils.util.py' from 'C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\hooks'...
12038 INFO: Loading module hook 'hook-encodings.py' from 'C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\hooks'...
12552 INFO: Loading module hook 'hook-heapq.py' from 'C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\hooks'...
12555 INFO: Loading module hook 'hook-lib2to3.py' from 'C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\hooks'...
12587 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\hooks'...
12589 INFO: Loading module hook 'hook-pickle.py' from 'C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\hooks'...
12592 INFO: Loading module hook 'hook-platform.py' from 'C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\hooks'...
12594 INFO: Loading module hook 'hook-sysconfig.py' from 'C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\hooks'...
12597 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\hooks'...
12598 INFO: Loading module hook 'hook-xml.py' from 'C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\hooks'...
12718 INFO: Loading module hook 'hook-_tkinter.py' from 'C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\hooks'...
12952 INFO: checking Tree
12952 INFO: Building Tree because Tree-00.toc is non existent
12952 INFO: Building Tree Tree-00.toc
13013 INFO: checking Tree
13013 INFO: Building Tree because Tree-01.toc is non existent
13014 INFO: Building Tree Tree-01.toc
13107 INFO: checking Tree
13108 INFO: Building Tree because Tree-02.toc is non existent
13108 INFO: Building Tree Tree-02.toc
13147 INFO: Looking for ctypes DLLs
13163 INFO: Analyzing run-time hooks ...
13166 INFO: Including run-time hook 'C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_subprocess.py'
13171 INFO: Looking for dynamic libraries
14067 INFO: Looking for eggs
14068 INFO: Using Python library C:\ide\anaconda3\envs\blogTest\python38.dll
14068 INFO: Found binding redirects:
[]
14072 INFO: Warnings written to C:\Users\lkt\Desktop\Python反编译测试\build\main\warn-main.txt
14113 INFO: Graph cross-reference written to C:\Users\lkt\Desktop\Python反编译测试\build\main\xref-main.html
14130 INFO: checking PYZ
14130 INFO: Building PYZ because PYZ-00.toc is non existent
14131 INFO: Building PYZ (ZlibArchive) C:\Users\lkt\Desktop\Python反编译测试\build\main\PYZ-00.pyz
14419 INFO: Building PYZ (ZlibArchive) C:\Users\lkt\Desktop\Python反编译测试\build\main\PYZ-00.pyz completed successfully.
14434 INFO: checking PKG
14434 INFO: Building PKG because PKG-00.toc is non existent
14435 INFO: Building PKG (CArchive) main.pkg
16921 INFO: Building PKG (CArchive) main.pkg completed successfully.
16924 INFO: Bootloader C:\ide\anaconda3\envs\blogTest\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
16924 INFO: checking EXE
16925 INFO: Building EXE because EXE-00.toc is non existent
16925 INFO: Building EXE from EXE-00.toc
16925 INFO: Copying bootloader EXE to C:\Users\lkt\Desktop\Python反编译测试\dist\main.exe.notanexecutable
17084 INFO: Copying icon to EXE
17091 INFO: Copying icons from ['C:\\ide\\anaconda3\\envs\\blogTest\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
17157 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
17158 INFO: Writing RT_ICON 1 resource with 3752 bytes
17158 INFO: Writing RT_ICON 2 resource with 2216 bytes
17158 INFO: Writing RT_ICON 3 resource with 1384 bytes
17159 INFO: Writing RT_ICON 4 resource with 37019 bytes
17159 INFO: Writing RT_ICON 5 resource with 9640 bytes
17159 INFO: Writing RT_ICON 6 resource with 4264 bytes
17159 INFO: Writing RT_ICON 7 resource with 1128 bytes
17164 INFO: Copying 0 resources to EXE
17164 INFO: Embedding manifest in EXE
17165 INFO: Updating manifest in C:\Users\lkt\Desktop\Python反编译测试\dist\main.exe.notanexecutable
17231 INFO: Updating resource type 24 name 1 language 0
17237 INFO: Appending PKG archive to EXE
17248 INFO: Fixing EXE headers
18957 INFO: Building EXE from EXE-00.toc completed successfully.

使用的是单文件打包的形式举例,打包好的文件在dist目录下,运行可执行文件

(blogTest) PS C:\Users\lkt\Desktop\Python反编译测试> cd .\dist\
(blogTest) PS C:\Users\lkt\Desktop\Python反编译测试\dist> .\main.exe
0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
(blogTest) PS C:\Users\lkt\Desktop\Python反编译测试\dist>

成功运行,接下来对可执行文件进行反编译。反编译我使用的是这个库封装的api
https://github.com/countercept/python-exe-unpacker 当然知乎链接里也有推荐另一个,我没有使用过。或许还会更好用,毕竟这个库有些年头了,而且我在使用过程中遇到了反编译的部分代码不对的问题(if else的逻辑和相关的缩进不对,我通过debug一步步改回来了)

将仓库clone下来

git clone git@github.com:countercept/python-exe-unpacker.git

进入仓库文件夹,下载相应的依赖,会有一些报错,可以不Care

pip install -r .\requirements.txt

然后将可执行文件反编译

(blogTest) PS C:\Users\lkt\Desktop\Python反编译测试\python-exe-unpacker> python .\pyinstxtractor.py ..\dist\main.exe
.\pyinstxtractor.py:95: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative usesimport imp
[*] Processing ..\dist\main.exe
[*] Pyinstaller version: 2.1+
[*] Python version: 308
[*] Length of package: 5714476 bytes
[*] Found 58 files in CArchive
[*] Beginning extraction...please standby
[*] Found 78 files in PYZ archive
[*] Successfully extracted pyinstaller archive: ..\dist\main.exeYou can now use a python decompiler on the pyc files within the extracted directory

这样就得到了main.exe_extracted反编译文件

需要注意的是我画框的即为代码文件,当然项目比较大的话,还会有其他的,自己留意一下。其实这个文件是一个pyc文件,pyc文件可以很方便就转成py文件,但是麻烦点在于,他缺少了magic number(魔数),解决方法就是struct这个文件是有魔数的,用能编辑二进制文件的打开main和struct,将struct的第一行,复制粘贴给main即可



将main重命名为main.pyc,使用命令

(blogTest) PS C:\Users\lkt\Desktop\Python反编译测试\python-exe-unpacker> uncompyle6 -o main.py .\main.exe_extracted\main.pyc
.\main.exe_extracted\main.pyc --
# Successfully decompiled file
(blogTest) PS C:\Users\lkt\Desktop\Python反编译测试\python-exe-unpacker>

反编译的结果

# uncompyle6 version 3.8.0
# Python bytecode 3.8.0 (3413)
# Decompiled from: Python 3.8.13 (default, Mar 28 2022, 06:59:08) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: main.py
# Compiled at: 2022-03-16 20:22:54
# Size of source mod 2**32: 257 bytesdef foo():for i in range(50):if not i % 2:print(i)if __name__ == '__main__':foo()

注释没有了,我自己实际使用的时候只有入口文件的注释会消失。其他文件的还在。

我反编译后的那个脚本我就可以随意修改了,脚本本身设置有密钥一定时间后会过期,拿到源码后我一个拿到了密钥的加密钥匙,可以随意生成密钥,另一个也可以修改程序改成自己的密钥,就跟开挂了一样。

最后如果想要加密打包可以在pyinstaller打包的时候加上–key指定密钥进行打包,不过据了解还是能反编译。或者用Cython打包,总的来说是加大反编译的难度,不然自己写的程序就打水漂了~

========================================================
补充:后续发现这个反编译库可以直接加上魔数,不用自己再去加,会更加好用一点
https://github.com/extremecoders-re/pyinstxtractor

python反编译Pyinstaller打包的可执行文件相关推荐

  1. Python反编译pyinstaller打包的exe文件 从0开始(未加密篇)

    因为pyinstaller方便.兼容性相对较好,所以我们会经常见到一些用pyinstaller打包的Python程序,在这里我们了解一下如何对这些打包好的exe文件进行反编译(即反编译出.py文件), ...

  2. 超详细Pyinstaller打包exe+反编译Pyinstaller打包的exe教程+防止反编译教程

    在这里分享一些技巧和经验给大家.辛苦撰文分享,转载或引用请保留本文作者信息及文章链接. 作者的环境: win7+python3.5(anaconda3) 理论上,win7及以上的系统和python任意 ...

  3. Python反编译pyinstaller或py2exe生成的exe可执行文件,获得源码

    目录 一.从exe文件中抽取pyc文件 二.将pyc文件反编译为py源码文件 三.更正.pyc文件的头信息 一.从exe文件中抽取pyc文件 直接到下列网址将代码clone下来: GitHub - c ...

  4. 【原创】用 Python 反编译 Python 软件

    [原创]用 Python 反编译 Python 软件 标 题: [原创]用 Python 反编译 Python 软件 作 者: Ptero 时 间: 2010-04-21,16:28:27 链 接: ...

  5. python 反编译

    python 反编译工具名称: Easy Python Decompiler 工具下载地址:http://sourceforge.net/projects/easypythondecompiler/? ...

  6. Python 反编译:pyinstxtractor工具和uncompyle6库的使用

    uncompyle6 现仅适用于 Python 2.4 到 3.8 版本 Python 3.9 及以上版本请参见我另外一篇博客: Python 反编译:pycdc工具的使用 ✅作者简介:人工智能专业本 ...

  7. Android APK破解、反编译、打包签名基本思路与方法

    Android APK破解.反编译.打包签名基本思路与方法 2013年6月1日星期六                                                           ...

  8. Python文件用pyinstaller打包成.exe文件后如何获取Python源码(Python文件反编译)

    此文章自己做个学习记录,也希望对跟我有一样困扰的同学一些帮助! 使用到的工具下载链接我都已经附上,点击下面蓝色字体可直接下载: 1.pyinstxtractor.py 2.wxMEDIT 3.在线编译 ...

  9. python反编译加密文件_python打包的二进制文件反编译

    简介 Pyhton是一个脚本语言,在运行Python代码时,最终由Python解释器来执行.解释器就是Python的运行环境,但是除了开发人员,大部分人在使用时并不会安装Python.所以官方就提供了 ...

最新文章

  1. 边框画的三角形给shadow
  2. 实践自定义UI—View
  3. linux 配置DNS正反区域,Linux基础服务_DNS原理以及正反向DNS配置
  4. 郑卅航院计算机与科学是一本吗,郑州航空工业管理学院计算机科学与应用系
  5. PHP 基础篇 - PHP 中 DES 加解密详解
  6. python 日志函数参数_将日志信息作为参数传递给函数
  7. nginx 电子书_Nginx最详细的反向代理配置步骤,拿去不谢
  8. 【学习笔记】第二章——信号量机制 用信号量实现互斥、同步
  9. Android模拟器PANIC: Could not open:问题解决方法
  10. 设计模式-模板方法模式(15)
  11. oracle 闩锁(latch)概述
  12. 推荐一个免费超级好用的简历模板网站
  13. 百度网盘使用浏览器下载大文件,无需跳转客户端
  14. linux下迅雷远程下载服务,在 Linux 下使用迅雷的另一种无入侵方式
  15. canvas太极八卦图
  16. 4.网络爬虫—Post请求(实战演示)
  17. 文章伪原创-文章伪原创工具
  18. cisco AP瘦转胖过程,以及“注意点”。
  19. 微信JSSDK的运用
  20. 雷达系列论文翻译(十一):LVI-SAM: Tightly-coupled Lidar-Visual-Inertial Odometryvia Smoothing and Mapping

热门文章

  1. 【django】Ajax Post请求CSRF认证问题
  2. maven 的生命周期
  3. JLink v9 固件修复
  4. 全文搜索技术 Lucene solr es (二)Solr(7.7.1)
  5. html修改th颜色,HTML th 标签 - HTML 颜色参考手册 - 自强学堂
  6. 大学物理实验——固体线胀系数的测定
  7. HP Pavilion 15-bc014tx 拆机教程,惠普加装固态硬盘
  8. 在win7上安装ft232r usb uart 驱动
  9. 第二十七章 日游南斋
  10. 关于python中的变量声明问题