python源码保护
文章目录
- 代码混淆
- 打包exe
- 编译为字节码
- 源码加密
项目发布部署时,为防止python源码泄漏,可以通过几种方式进行处理
代码混淆
修改函数、变量名
打包exe
通过pyinstaller 将项目打包为exe可执行程序,不过容易被反编译。
编译为字节码
py_compile
compileall
模块
容易被反编译
源码加密
- pyarmor 加密
官网
官方文档
- 源码加密;
- 设置程序的许可方式,如设置使用期限、绑定环境设备(磁盘、mac/ip、网卡)等;
安装
:
pip install pyarmor
加密单个py文件
:
# 终端命令
# 加密一个文件
pyarmor obfuscate index.py
加密后的文件:
dist / index.py 加密文件,内部的函数、类等仍可以像python源码一样导入,前提是要记得里面都有哪些类。
dist / pytransform包 后续需要放入python搜索路径或者能被搜索找到
加密整个项目的py文件
:
# 递归地加密,项目中的每个包都会进行加密
pyarmor obfuscate --recursive index.py
加密结果:
加密后的py文件能像正常源码一样使用,前提是pytransform包可以被搜索到。
试用版加密整个项目时,有最大字节限制(32768)
!!
生成许可文件
,并使用:
# 生成许可, 放入licenses目录
pyarmor licenses --expired "2025-01-01" --bind-disk "100304xxxx" --bind-mac "70:f1:a1:23:f0:94" --bind-ipv4 "200.10.139.23"# 使用许可 加密
pyarmor obfuscate --with-license licenses/regcode-01/license.lic index.py
默认安装的pyarmor是试用版,有些限制,如单次加密最多32768字节。
购买地址
2. Nuitka 将python转为C,再编译为二进制文件(操作系统的动态链接库),反编译难度大。
linux- .so
win- dll/pyd
方法实现:
jmpy3 库
参考
# -*- coding: utf-8 -*-
"""
@summary: 加密python代码为pyd/so
"""
import os
import re
import shutil
import tempfile
from distutils.command.build_py import build_py
from distutils.core import setup
from typing import Union, Listfrom Cython.Build import cythonizefrom jmpy.log import loggerdef get_package_dir(*args, **kwargs):return ""# 重写get_package_dir, 否者生成的so文件路径有问题
build_py.get_package_dir = get_package_dirclass TemporaryDirectory(object):def __enter__(self):self.name = tempfile.mkdtemp()return self.namedef __exit__(self, exc_type, exc_value, traceback):shutil.rmtree(self.name)def search(content, regexs):if isinstance(regexs, str):return re.search(regexs, content)for regex in regexs:if re.search(regex, content):return Truedef walk_file(file_path):if os.path.isdir(file_path):for current_path, sub_folders, files_name in os.walk(file_path):for file in files_name:file_path = os.path.join(current_path, file)yield file_pathelse:yield file_pathdef copy_files(src_path, dst_path):if os.path.isdir(src_path):if os.path.exists(dst_path):shutil.rmtree(dst_path)def callable(src, names: list):if search(src, dst_path):return namesreturn ["dist", ".git", "venv", ".idea", "__pycache__"]shutil.copytree(src_path, dst_path, ignore=callable)else:if not os.path.exists(dst_path):os.makedirs(dst_path)shutil.copyfile(src_path, os.path.join(dst_path, os.path.basename(src_path)))def get_py_files(files, ignore_files: Union[List, str, None] = None):"""@summary:---------@param files: 文件列表#param ignore_files: 忽略的文件,支持正则---------@result:"""for file in files:if file.endswith(".py"):if ignore_files and search(file, regexs=ignore_files): # 该文件是忽略的文件passelse:yield filedef filter_cannot_encrypted_py(files, except_main_file):"""过滤掉不能加密的文件,如 log.py __main__.py 以及包含 if __name__ == "__main__": 的文件Args:files:Returns:"""_files = []for file in files:if search(file, regexs="__.*?.py"):continueif except_main_file:with open(file, "r", encoding="utf-8") as f:content = f.read()if search(content, regexs="__main__"):continue_files.append(file)return _filesdef encrypt_py(py_files: list):encrypted_py = []with TemporaryDirectory() as td:total_count = len(py_files)for i, py_file in enumerate(py_files):try:dir_name = os.path.dirname(py_file)file_name = os.path.basename(py_file)os.chdir(dir_name)logger.debug("正在加密 {}/{}, {}".format(i + 1, total_count, file_name))setup(ext_modules=cythonize([file_name], quiet=True, language_level=3),script_args=["build_ext", "-t", td, "--inplace"],)encrypted_py.append(py_file)logger.debug("加密成功 {}".format(file_name))except Exception as e:logger.exception("加密失败 {} , error {}".format(py_file, e))temp_c = py_file.replace(".py", ".c")if os.path.exists(temp_c):os.remove(temp_c)return encrypted_pydef delete_files(files_path):"""@summary: 删除文件---------@param files_path: 文件路径 py 及 c 文件---------@result:"""try:# 删除python文件及c文件for file in files_path:os.remove(file) # py文件os.remove(file.replace(".py", ".c")) # c文件except Exception as e:passdef rename_excrypted_file(output_file_path):files = walk_file(output_file_path)for file in files:if file.endswith(".pyd") or file.endswith(".so"):new_filename = re.sub("(.*)\..*\.(.*)", r"\1.\2", file)os.rename(file, new_filename)def start_encrypt(input_file_path,output_file_path: str = None,ignore_files: Union[List, str, None] = None,except_main_file: int = 1,
):assert input_file_path, "input_file_path cannot be null"assert (input_file_path != output_file_path), "output_file_path must be diffent with input_file_path"if output_file_path and os.path.isfile(output_file_path):raise ValueError("output_file_path need a dir path")input_file_path = os.path.abspath(input_file_path)if not output_file_path: # 无输出路径if os.path.isdir(input_file_path): # 如果输入路径是文件夹 则输出路径为input_file_path/dist/project_nameoutput_file_path = os.path.join(input_file_path, "dist", os.path.basename(input_file_path))else:output_file_path = os.path.join(os.path.dirname(input_file_path), "dist")else:output_file_path = os.path.abspath(output_file_path)# 拷贝原文件到目标文件copy_files(input_file_path, output_file_path)files = walk_file(output_file_path)py_files = get_py_files(files, ignore_files)# 过滤掉不需要加密的文件need_encrypted_py = filter_cannot_encrypted_py(py_files, except_main_file)encrypted_py = encrypt_py(need_encrypted_py)delete_files(encrypted_py)rename_excrypted_file(output_file_path)logger.debug("加密完成 total_count={}, success_count={}, 生成到 {}".format(len(need_encrypted_py), len(encrypted_py), output_file_path))