加密Python源码方案 PyArmor

发表于:2022-9-13 09:27

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:魔法小木瓜    来源:今日头条

  1、PyArmor介绍
  PyArmor是一个用来混淆python脚本的命令行工具,将混淆脚本绑定到固定的机器上,或到一定的时间让混淆的脚本过期不能用。
  可通过pyarmor -h查看其用法,以下只截取部分进行说明:
  (tt) PS C:\test> pyarmor -h    
  usage: pyarmor [-h] [-v] [-q] [-d] [--home HOME] [--boot BOOT]  ...
  optional arguments:
    -h, --help     show this help message and exit
    -v, --version  show program's version number and exit
    -q, --silent   Suppress all normal output
    -d, --debug    Print exception traceback and debugging message
    --home HOME    Change pyarmor home path
    --boot BOOT    Change boot platform
  The most commonly used pyarmor commands are:
      obfuscate (o) Obfuscate python scripts
      licenses (l) Generate new licenses for obfuscated scripts
      pack (p)     Pack obfuscated scripts to one bundle
      init (i)     Create a project to manage obfuscated scripts
      config (c)   Update project settings
      build (b)    Obfuscate all the scripts in the project
      info         Show project information
      check        Check consistency of project
      hdinfo       Show all available hardware information
      benchmark    Run benchmark test in current machine
      register     Make registration keyfile work
      download     Download platform-dependent dynamic libraries
      runtime      Generate runtime package separately
      help         Display online documentation
  See "pyarmor <command> -h" for more information on a specific command.
  More usage refer to https://pyarmor.readthedocs.io
  (1)pyarmor主要功能
  1. 使用命令 obfuscate 来加密脚本。
  2. 使用命令 licenses 为加密脚本生成新的许可文件 license.lic,如果需要设置加密脚本的使用期限或者限制脚本在特定的机器使用,需要生成新的许可文件, 并使用新的许可文件加密脚本。
  3. 可以加密整个python包。
  4. 可以配合pyinstaller对python工程打包成一个独立可运行的安装包。
  5. 还可以利用其提供的一些方案,进一步提升加密脚本的安全性,具体在实际项目需要时,再查阅其官网即可。
  2、使用示例
  (1)安装
  pip install pyarmor -i https://pypi.douban.com/simple/
  -i https://pypi.douban.com/simple/ 是使用国内的豆瓣源,提升安装速度,具体可参见我的其他文章
  (2)一般加密
  # module1.py内容如下
  def module1_func1():
      print("I'm module1.py")
      return   
  # main.py内容如下
  import module1
  print("I'm main.py")
  module1.module1_func1()。
  运行命令进行加密:pyarmor o main.py。
  PyArmor会加密main.py和相同目录下面的所有*.py 文件,会生成dist文件夹,其包含运行加密脚本所需要的全部文件,基本过程:
  1. 创建输出子目录 dist。
  2. 生成加密的主脚本 main.py 保存在输出目录 dist。
  3. 加密相同目录下其他所有 *.py 文件,保存到输出目录 dist。
  4. 生成运行加密脚本所需要的全部辅助文件,保存到输出目录 dist。
  验证:到dist目录下,python main.py。
  加密后的文件是这样的:
  from pytransform import pyarmor_runtime
  pyarmor_runtime()
  __pyarmor__(__name__, __file__, b'\x50\x59\x41\x52\x4d\x4f\x52\x00\x00\x03\x08\x00\x55\x0d\x0d\x0a\x09\x33\xe0\x02\x00\x00\x00\x00\x01\x00\x00\x00\x40\x00\x00\x00\x63\x01\x00\x00\x00\x00\x00\x18\x26\xa4\x75\x12\x0c\x32\x8f\xd9\xa6\xf8\x0a\x0b\x17\x1e\xc7\xfe\x00\x00\x00\x00\x00\x00\x\x22\x0c\xa0\x75\x0c\x1a\x13\x8a\x26\xb4\x02\x46\x1b\x8c\x5d\xaf\xd0\x81\xc2\x22\x59\xc0\x1d\xd2\x83\x99\x01\x09\xb1\x78\x4c\xdc\x58\x9b\xdf\x17\xd9\xe9\x07\xa4\xa7\xc6\x51\xde\xaa\x20\xbf\x7c\x43\xbb\x83\x87\xaf\x82\x9e\x65\x2d\xae\xb9\x5b\x14\xfc\xf8\x1d\xc7\x09\xe5\x65\xa7\x8d\x5a\x62\x9d\x78\xa3\x82\x4c\x53\x17\xc9\x3a\x15\xa4\xe7\x66\xda\x3f\xf2\x9d', 2)
  还会根据不同平台生成相关依赖:在pytransform文件夹下,例如windows生成相关.dll,linux生成.so等。
  (3)带licenses的加密
  1.生成新的许可文件
    pyarmor l -e 2022-09-09 test01
    执行这条命令会生成一个带有效期的认证文件: 创建license.lic与license.lic.txt,保存在licenses/test01目录下
  2.使用新生成的许可文件加密脚本
    pyarmor o --with-license .\licenses\test01\license.lic main.py
  3.同样可以在dist目录下找到相关,此时如果过期了,则会提示:License is expired
  (4)也可以绑定在固定机器上
  1.在该机器上运行命令获取硬件信息
    pyarmor hdinfo
  2.然后生成绑定的固定机器的许可文件
    pyarmor l --bind-disk "100304PBN2081SF3NJ5T" --bind-mac "20:c1:d2:2f:a0:96" code-002
  3.使用这个许可文件加密脚本,加密脚本就只能在指定机器上运行
    pyarmor o --with-license licenses/code-002/license.lic main.py
  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号