• python学习笔记12: python程序的打包/破解/加密


    1. 使用pyintaller打包代码

    pyinstaller abc.py # 要打包的top代码
        -F         # 只生成单个的exe文件, 缺点是运行exe时要先解压.
        -w         # 不生成命令行窗口.
        --debug    # 生成带debug信息的exe文件.
        --clean    # 运行前清理编译的临时文件;
        --icon     # 工具的top图标文件
        --distpath # 输出的exe存放的目录
        --workpath # 编译的临时文件存放的目录
        --hidden-import # 隐含import的模块, 比如自定义/第三方的import模块等
        --key      # 加密密钥
    

    2. pyc文件的破解

    pyinstaller打包的py文件会转为pyc格式, 但这种文件很容易破解出源码.
    操作如下:

    1. 下载pyinstxtractor.py;
    2. 从exe中获取pyc文件: python pyinstxtractor.py ../dist/xx.exe;
    3. 在生成的xx.exe_extracted目录中找到需要破解的pyc文件: yy.pyc;
    4. 使用在线反编译工具https://tool.lu/pyc读入pyc, 得到源码.

    3. 加密方法1: 将py文件转为pyd格式

    pyd是动态链接库文件, 本质上与dll文件相同, 安全性比pyc文件高很多. 所以这种方式不算真正意义上的加密, 而是一个编译过程.
    操作如下:

    1. 安装easycython模块 pip install easycython
    2. 使用easycython命令将py转pyd: easycython xx.py, 会在同一目录生成xx.pyd(如果是64位系统生成的文件名为xx.cp36-win_amd64.pyd);
    3. 将xx.cp36-win_amd64.pyd重命名为xx.pyd;
    4. 使用pyinstaller打包(由于xx.py和xx.pyd在同一目录, pyinstaller会优先打包pyd), 打包时添加—hidden-import xx选项.
    5. 生成的打包文件中会包含xx.pyd;
    6. 注意: 如果打包时使用了—key选项, 但打包的模块是pyd格式的, 则pyd文件不会被加密, 只是打包.

    4. 加密方法2: 使用pyinstaller的—key选项

    操作如下:

    1. 安装Visual Studio 2017 Community, 需要用到它的c语言编译功能;
    2. 安装PyCrypto模块: pip install PyCrypto –i https://pypi.douban.com/simple, 需要调用该模块;
    3. 运行pyinstaller --key 0123456789 –F [other options]
    4. 对生成的exe破解时, 会报告Fail to decompress xx, probably encrypted. 破解输出的目录中生成的是xx.pyc.encrypted文件, 不再是xx.pyc.
    5. 注意: 只能加密py文件, 如果加密的模块是pyd文件, 则pyd文件会直接打包, 不会被加密.
    6. 听说密钥也一并打包到输出文件中了, 所以好像也不安全. 感觉还是pyd靠谱些, 至少差不多是C编译后的文件.

    安装或使用过程可能遇到的问题:

    1. 安装PyCrypto模块时, 报告错误:
    C:Program Files (x86)Windows Kits10include10.0.17763.0ucrtinttypes.h(27): error C2061: 语法错误: 标识符"intmax_t"  
    	......
    C:Program Files (x86)Windows Kits10include10.0.17763.0ucrtinttypes.h(96): error C2143: 语法错误: 缺少"{"(在"__cdecl"的前面)  
    ----------------------------------------  
    

    解决方法:

    1)	将Microsoft Visual Studio2017CommunityVCToolsMSVC14.16.27023includestdint.h复制到C:Program Files (x86)Windows Kits10Include10.0.17763.0ucrtstdint.h;
    2)	将C:Program Files (x86)Windows Kits10Include10.0.17763.0ucrtinttypes.h, 第14行, #include <stdint.h>修改为#include "stdint.h"
    3)	重新安装PyCrypto.
    
    1. pycryptodome模块引起的问题. 网上有帖子说PyCrypto模块已经停止维护, 推荐安装pycryptodome模块, 但这个模块安装后可能会引发一些错误.
    1)	两个模块全都安装, 加密/运行exe都没问题;
    2)	两个模块全都安装, 然后卸载掉PyCrypto, 保留pycryptodome, 加密时报告: Crypto has not attribute '__version__';
    3)	两个模块全都安装, 然后卸载掉pycryptodome, 保留PyCrypto, 加密时报告: Crypto has not attribute '__version__';
    4)	两个模块全都卸载, 然后单独安装pycryptodome, 加密成功, 但运行生成的exe, 但错误内容与第2)条又不相同: 
    
    1.	$ ./hardware_debug_tool.exe  
    2.	[27076] Failed to execute script pyiboot01_bootstrap  
    3.	......  
    4.	zlib.error: Error -3 while decompressing data: incorrect header check  
    5.	  
    6.	During handling of the above exception, another exception occurred:  
    7.	  
    8.	Traceback (most recent call last):  
    9.	......
    10.	ImportError: Loader FrozenImporter cannot handle module os  
    

    解决方法:

    1. 只安装PyCrypto模块;
    2. 或者PyCrypto模块和pycryptodome都安装;
    3. 两个模块都安装后, 想卸载某一个模块时, 不能直接卸载其中一个, 只能两个全部卸载, 然后重新安装需要保留的模块.

    加密效果如这个网址上说的https://blog.csdn.net/mutex86/article/details/45367143:

  • 相关阅读:
    九个令人兴奋的新功能将与Java 9 展示两点
    自学前端开发 新版css时钟效果图
    自学前端,你要的学习资料到了~~~~~~
    Angularjs中ng-repeat与移动端滑动插件iScroll的冲突
    计蒜客学习记录
    明明的随机数
    模板题
    泉州一中复赛模拟
    快速幂模板
    NOIP2013
  • 原文地址:https://www.cnblogs.com/gaiqingfeng/p/13229278.html
Copyright © 2020-2023  润新知