源码怎么编译exe-PyInstaller将Python文件打包成exe后如何反编译(破解源码)

这里有一些方法和经验与大家分享。 努力分享、转载或引用,请保留作者信息和文章链接。

作者环境:

win7+python3.5(anaconda3)

理论上win7及以上系统以及任何版本的python都可以。

1. 基本脚本

首先我们创建一个简单的脚本,比如输出一串数字、文本等。这里我们输出一串文本,同时估计三次方。

# -*- coding: utf-8 -*-
"""
Created on Wed Aug 29 09:18:13 2018
@author: Li Zeng hai
"""
def test(num):
    print('num={n}, {n}^3={n2}'.format(n=num, n2=num**3))
    
    
if __name__ == '__main__':
    while 1:
        try:
            num = input('提示:输入"q"退出程序。n请输入一个数字:')
            if num.lower() == 'q':
                break
            num = float(num)
            print(num)
        except:
            print('输入的数字不正确!')
            continue
        
        test(num)

将此脚本保存为 mylib.py 并运行此脚本:

源码怎么编译exe-PyInstaller将Python文件打包成exe后如何反编译(破解源码)

可以看到,脚本运行正常。

为了演示源码怎么编译exe,我们在 main.py 脚本文件中创建以下脚本,并从 mylib 导入测试函数。


# -*- coding: utf-8 -*-
from mylib import *
while 1:
    try:
        num = input('提示:输入"q"退出程序。n请输入一个数字:')
        if num.lower() == 'q':
            break
        num = float(num)
        print(num)
    except:
        print('输入的数字不正确!')
        continue
    
    test(num)

2.使用PyInstaller打包为exe

使用以下命令打包成单个exe(去掉-F不是单个exe)

pyinstaller -F main.py

包装流程如下:

E:t>pyinstaller -F main.py
505 INFO: PyInstaller: 3.3.1
505 INFO: Python: 3.5.5
505 INFO: Platform: Windows-7-6.1.7601-SP1
505 INFO: wrote E:tmain.spec
505 INFO: UPX is not available.
505 INFO: Extending PYTHONPATH with paths
['E:\t', 'E:\t']
505 INFO: checking Analysis
505 INFO: Building Analysis because out00-Analysis.toc is non existent
505 INFO: Initializing module dependency graph...
521 INFO: Initializing module graph hooks...
521 INFO: Analyzing base_library.zip ...
6269 INFO: running Analysis out00-Analysis.toc
6269 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by d:anaconda3python.exe
6956 INFO: Caching module hooks...
6956 INFO: Analyzing E:tmain.py
6956 INFO: Loading module hooks...
6956 INFO: Loading module hook "hook-pydoc.py"...
6956 INFO: Loading module hook "hook-xml.py"...
7283 INFO: Loading module hook "hook-encodings.py"...
7533 INFO: Looking for ctypes DLLs
7549 INFO: Analyzing run-time hooks ...
7549 INFO: Looking for dynamic libraries
7720 INFO: Looking for eggs
7720 INFO: Using Python library d:anaconda3python35.dll
7720 INFO: Found binding redirects:
[]
7720 INFO: Warnings written to E:tbuildmainwarnmain.txt
7751 INFO: Graph cross-reference written to E:tbuildmainxref-main.html
7767 INFO: checking PYZ
7767 INFO: Building PYZ because out00-PYZ.toc is non existent
7767 INFO: Building PYZ (ZlibArchive) E:tbuildmainout00-PYZ.pyz
8345 INFO: Building PYZ (ZlibArchive) E:tbuildmainout00-PYZ.pyz completed successfully.
8345 INFO: checking PKG
8345 INFO: Building PKG because out00-PKG.toc is non existent
8345 INFO: Building PKG (CArchive) out00-PKG.pkg
9954 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
9954 INFO: Bootloader d:anaconda3libsite-packagesPyInstallerbootloaderWindows-64bitrun.exe
9954 INFO: checking EXE
9954 INFO: Building EXE because out00-EXE.toc is non existent
9954 INFO: Building EXE from out00-EXE.toc
9954 INFO: Appending archive to EXE E:tdistmain.exe
9954 INFO: Building EXE from out00-EXE.toc completed successfully.
E:t>

最后在目录中生成build和disk文件夹。 exe文件在磁盘中。 运行正常。 如下所示:

3.反编译Pyinstaller打包的exe

这样的exe已经可以迁移到其他笔记本上使用。 如果是为了其他笔记本的方便,这一步就已经结束了。 但有时候,我们可能会为很多人使用这个功能,但如果你不想开源或者被别人破解,这一步是不够的。

因为到此为止的exe就可以被别人反编译生成源代码了。

这里使用的工具:

exe反编译工具:pyinstxttractor.py:点此下载

pyc反编译工具:Easy Python Decompiler或者在线反编译pyc。

将pyinstxttractor.py放在exe文件同级目录下,执行以下cmd命令:

源码怎么编译exe-PyInstaller将Python文件打包成exe后如何反编译(破解源码)

python pyinstxttractor.py main.exe

如果执行成功,会在同目录下生成一个新的反编译文件夹——main.exe_extracted,如下图:

同时我们可以在右边圈出的路径下找到我们的exe当时引入的mylib模块,是pyc格式的。

反编译pyc格式的文件非常简单。 使用上面提供的工具或在几分钟内找到在线反编译器。 我们看一下反编译的结果:

#!/usr/bin/env python
# visit http://tool.lu/pyc/ for more information
'''
Created on Wed Aug 29 09:18:13 2018
@author: Li Zeng hai
'''
def test(num):
    print('num={n}, {n}^3={n2}'.format(n=num, n2=num ** 3))
if __name__ == '__main__':
    while None:
        try:
            num = input(
                'xe6x8fx90xe7xa4xbaxefxbcx9axe8xbex93xe5x85xa5"q"xe9x80x80xe5x87xbaxe7xa8x8bxe5xbax8fxe3x80x82nxe8xafxb7xe8xbex93xe5x85xa5xe4xb8x80xe4xb8xaaxe6x95xb0xe5xadx97xefxbcx9a')
            if num.lower() == 'q':
                break
            num = float(num)
            print(num)
        except:
            None
            None
            None
            print(
                'xe8xbex93xe5x85xa5xe7x9ax84xe6x95xb0xe5xadx97xe4xb8x8dxe6xadxa3xe7xa1xaexefxbcx81')
            continue

可以看到源码基本上已经完美反编译了。 涉及英文的地方,会因为编码问题而改变。 但非英语部分几乎一模一样。

就问你怕不怕! ! !

4.将脚本编译为pyd,避免反编译

好害怕,哈哈。

怎么解决呢,可以考虑将模块py文件编译成动态链接库,这样破解难度就会大大降低。 其中,在python中,pyd格式就是动态链接库。 使用cython编译,如果anaconda自带,python可能没有cython,安装即可:

pip 安装 Cython

我们在main.py所在的文件夹中新建一个py文件,这里我将其命名为build_pyd.py,其内容如下:

# -*- coding: utf-8 -*-
"""
Created on Wed Aug 29 13:33:20 2018
@author: Li Zeng hai
"""
from distutils.core import setup
from Cython.Build import cythonize
 
setup(
  name = 'any words.....',
  ext_modules = cythonize(["mylib.py",
                           
                           ]
  ),
)

然后执行以下cmd命令:

python build_pyd.py build_ext --inplace

运行过程和生成结果如下图所示,其中红框内的pyd文件是编译出来的。 因为我是64位系统和python,所以会生成amd64后缀。 我们可以将其删除并重命名为 mylib.pyd。

注意:当mylib.pyd和mylib.py同时存在时,导入优先级为pyd>py源码怎么编译exe,所以不需要删除py文件,默认导入为pyd。

此时,我们删除build和disk文件夹,重复步骤2,再次编译成exe。

注:编译需要相关的VC环境,因为python3.5是基于VS14版本的,所以我这里安装的也是。 不安装很难编译。

您可以验证一下:

再次反编译main.exe后,在原来的路径E:tdistmain.exe_extractedout00-PYZ.pyz_extracted下不再找到mylib.pyc。

因为它不再是一个可以直接反编译的文件了。

那么他在哪里? 他以 pyd 的形式存在于下层目录中。 如下所示:

反编译pyd的难度相当高,至此,你就完成了!

3.猜你喜欢 Python 条件理解 Python 列表理解 Python 字典理解 Python 函数声明与调用 Python 变长参数 *argc/**kargcsPython 匿名函数 lambdaPython 返回逻辑决策表达式Python 字符串/列表/元组/字典 Python 局部变量与全局变量Python类型函数与isinstance函数Python is与的区别==Python变量数据类型与不可变数据类型的区别Python浅拷贝与深拷贝Python文件读写操作Python异常处理Python模块importPython __name__ == '__main__' Python线程创建与详解参数传递 Python 线程互斥锁 LockPython 线程时间 EventPython 线程条件变量 ConditionPython 线程计时器 TimerPython 线程信号量 SemaphorePython 线程屏障对象 BarrierPython 线程队列 Queue – FIFOPython 线程队列 LifoQueue – LIFOPython 线程优先级队列 PriorityQueue

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悟空资源网 源码编译 源码怎么编译exe-PyInstaller将Python文件打包成exe后如何反编译(破解源码) https://www.wkzy.net/game/166607.html

常见问题

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务