加密Python源碼方案 PyArmor

WBOY
發布: 2023-05-05 15:01:06
轉載
2307 人瀏覽過

加密Python源码方案 PyArmor

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, --versionshow program's version number and exit
-q, --silent Suppress all normal output
-d, --debugPrint exception traceback and debugging message
--home HOMEChange pyarmor home path
--boot BOOTChange 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
checkCheck consistency of project
hdinfo Show all available hardware information
benchmarkRun benchmark test in current machine
register Make registration keyfile work
download Download platform-dependent dynamic libraries
runtimeGenerate 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'x50x59x41x52x4dx4fx52x00x00x03x08x00x55x0dx0dx0ax09x33xe0x02x00x00x00x00x01x00x00x00x40x00x00x00x63x01x00x00x00x00x00x18x26xa4x75x12x0cx32x8fxd9xa6xf8x0ax0bx17x1exc7xfex00x00x00x00x00x00xx22x0cxa0x75x0cx1ax13x8ax26xb4x02x46x1bx8cx5dxafxd0x81xc2x22x59xc0x1dxd2x83x99x01x09xb1x78x4cxdcx58x9bxdfx17xd9xe9x07xa4xa7xc6x51xdexaax20xbfx7cx43xbbx83x87xafx82x9ex65x2dxaexb9x5bx14xfcxf8x1dxc7x09xe5x65xa7x8dx5ax62x9dx78xa3x82x4cx53x17xc9x3ax15xa4xe7x66xdax3fxf2x9d', 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 .licensestest01license.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/。

以上是加密Python源碼方案 PyArmor的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:51cto.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!