加密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主要功能
- 使用命令 obfuscate 來加密腳本。
- 使用命令 licenses 為加密腳本生成新的許可文件 license.lic,如果需要設置加密腳本的使用期限或者限制腳本在特定的機器使用,需要生成新的許可文件, 并使用新的許可文件加密腳本。
- 可以加密整個python包。
- 可以配合pyinstaller對python工程打包成一個獨立可運行的安裝包。
- 還可以利用其提供的一些方案,進一步提升加密腳本的安全性,具體在實際項目需要時,再查閱其官網即可。
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文件夾,其包含運行加密腳本所需要的全部文件,基本過程:
- 創建輸出子目錄 dist。
- 生成加密的主腳本 main.py 保存在輸出目錄 dist。
- 加密相同目錄下其他所有 *.py 文件,保存到輸出目錄 dist。
- 生成運行加密腳本所需要的全部輔助文件,保存到輸出目錄 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/。
責任編輯:姜華
來源:
今日頭條