加密Python源码方案 PyArmor

开发 后端
PyArmor是一个用来混淆Python脚本的命令行工具,将混淆脚本绑定到固定的机器上,或到一定的时间让混淆的脚本过期不能用。

python源码的加密方案有很多,现在我们来了解PyArmor如何给python加密。

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

其他更多使用方式可以参考其官网:
https://pyarmor.readthedocs.io/en/latest/。

责任编辑:姜华 来源: 今日头条
相关推荐

2023-08-31 09:00:00

PythonPyarmor

2010-06-17 22:16:48

2010-03-22 12:40:48

Python代码加密

2022-06-01 07:33:29

数据存储加密

2020-10-25 09:04:46

数据加密数据泄露攻击

2009-06-30 10:05:24

MD5加密JSP源码

2013-04-10 14:21:35

2021-08-05 10:40:37

加密方案Spring

2019-05-13 09:11:41

加密解密Python攻击

2010-03-23 13:16:14

Python代码加密

2019-05-09 10:25:46

Python加密库编程语言

2013-09-09 09:19:32

2009-11-20 09:56:09

Check Point全盘加密

2010-09-14 10:55:21

EAL4+认证端点安全Check Point

2010-09-27 15:12:02

Check Point

2013-11-05 10:22:20

浏览器加密

2021-05-14 14:33:07

Python加密货币

2021-04-15 09:02:33

Python加密解密

2010-06-30 17:56:06

2009-05-26 11:24:00

点赞
收藏

51CTO技术栈公众号