pyarmor的使用

2023-05-27 16:13:28 浏览数 (1)

作用: 用于python脚本的加密

切换到虚拟环境python3.x下(这里是用的3.6,个人开发环境下推荐使用miniconda3来实现虚拟环境管理)

代码语言:javascript复制
这里演示的脚本叫 diff_demo.py ,用于比较 old和new 这2个文件的差异

0 原始文件和目录如下:
tree
.
├── diff_demo.py
├── new
└── old


1 安装扩展
pip install pyarmor


2 加密文件
pyarmor obfuscate diff_demo.py

3 查看目录结构
tree
.
├── compare.html
├── diff_demo.py
├── dist
│    ├── diff_demo.py
│    └── pytransform
│        ├── __init__.py
│        └── _pytransform.so
├── new
└── old


其中, dist/diff_demo.py  就是加密后的文件 
$ cat dist/diff_demo.py 
from pytransform import pyarmor_runtime
pyarmor_runtime()
__pyarmor__(__name__, __file__, b'x50x59x41x52x4dx4fx52x00x00x03x06x00x33x0dx0dx0ax09x34xe0x02x00x00x00x00 
下面内容省略
...
 
            
            
4 执行加密后的脚本
python dist/diff_demo.py old new
            
5 创建有效期文件
pyarmor  licenses -e 2020-12-21 r001  # 故意设置一个过期的日期

6 查看目录结构      
tree
.
├── compare.html
├── diff_demo.py
├── dist
│    ├── diff_demo.py
│    └── pytransform
│        ├── __init__.py
│        ├── __pycache__
│        │    └── __init__.cpython-36.pyc
│        └── _pytransform.so
├── licenses
│    └── r001
│        ├── license.lic
│        └── license.lic.txt
├── new
└── old


7 执行加密
pyarmor obfuscate --with-license licenses/r001/license.lic diff_demo.py


8 执行测试
python dist/diff_demo.py
这里直接提示 License is expired


另外,我们进到 dist 目录下执行也行。 或者把文件拷贝到其它地方(确保依赖相对路径的一致就行)去执行都可以。



9 此外,还可以绑定到网卡mac, 如果更换机器后 网卡mac不一致就无法执行
pyarmor hdinfo
日志如下 
INFO     PyArmor Trial Version 7.7.4
INFO     Python 3.6.13

Hardware informations got by PyArmor:

Failed to get harddisk information.

Default Mac address: "00:0c:29:4e:e7:f6"

Multiple Mac addresses: "<00:0c:29:4e:e7:f6,02:42:53:d3:63:0c>"

Ip address: "x.x.x.x"

Domain name: "(none)"

Change logs

	v6.2.0(r21): Remove trailing dot from harddisk serial number
	v6.4.2(r34): Support binding multiple mac addresses
	v6.5.3(r37): Support binding named harddisk
	v6.7.5(r45): Support mmc/sd card in Linux



10 这里故意绑定一个错误的mac地址
pyarmor licenses --bind-mac "00:0c:29:4e:f7:ff" r002
日志如下: 
INFO     PyArmor Trial Version 7.7.4
INFO     Python 3.6.13
INFO     Generate licenses with capsule /root/.pyarmor/.pyarmor_capsule.zip ...
INFO     Make output path of licenses: licenses
INFO     The license file is generated in restrict mode
INFO     The license file is generated in period mode disabled
INFO     Make path: licenses/r002
INFO     Generate license: *IFMAC:00:0c:29:4e:f7:ff*CODE:r002
INFO     Write license file: licenses/r002/license.lic
INFO     Write information to licenses/r002/license.lic.txt
INFO     Generate 1 licenses OK.


11 查看下当前目录结构
tree
.
├── compare.html
├── diff_demo.py
├── licenses
│    └── r002
│        ├── license.lic
│        └── license.lic.txt
├── new
└── old

12 使用mac地址绑定方式来加密文件
pyarmor obfuscate --with-license licenses/r002/license.lic diff_demo.py
日志如下:
......
INFO     Start obfuscating the scripts...
INFO     	diff_demo.py -> dist/diff_demo.py
INFO     Insert bootstrap code to entry script dist/diff_demo.py
INFO     Obfuscate 1 scripts OK.


13 执行测试
python dist/diff_demo.py
提示 License is not for this machine




14 此外,还可以打包成独立可执行文件
$ pip install pyinstaller  # 需要确保 pyinstaller已安装

$ pyarmor pack diff_demo.py

$ cd dist/diff_demo
$ ./diff_demo old new  这样可以执行成功

# 如果把 diff_demo 拷贝到其它目录(如/tmp/下,则执行会报错,因为会提示找不到.so文件)下面是拷贝到/tmp下执行的样子
$ ./diff_demo old new
[11552] Error loading Python lib '/tmp/libpython3.6m.so.1.0': dlopen: /tmp/libpython3.6m.so.1.0: cannot open shared object file: No such file or directory


15 开启web页面 (生产上基本用不到,了解下即可)
pip install pyarmor-webui
pyarmor-webui --host 0.0.0.0

0 人点赞