前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pyarmor的使用

pyarmor的使用

作者头像
保持热爱奔赴山海
发布2023-05-27 16:13:28
1K0
发布2023-05-27 16:13:28
举报
文章被收录于专栏:饮水机管理员饮水机管理员

作用: 用于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'\x50\x59\x41\x52\x4d\x4f\x52\x00\x00\x03\x06\x00\x33\x0d\x0d\x0a\x09\x34\xe0\x02\x00\x00\x00\x00 
下面内容省略
...
 
            
            
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
本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-05-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com