非常感谢RémiMevaere提供的有用的Python文件保护教程!原始教程链接所在:https://www./555d13e4a9b34836bb4753192f14225c 介绍如您所知,保护python代码有点困难。因为它被解释了;稍微修改一下python源代码,您就可以提取所有代码(即使它们被替换了)。 我将在此处说明的方法确实可靠,并使用了所谓的“ The Enigma Protector”的第三方工具。 先决条件
安装和使用Cython“ Cython是一种编程语言,逐步成为Python编程语言的超集,逐步通过主要使用Python编写的代码以及可选的其他受C语言启发的语法来提供替代C的性能。Cython是一种编译语言,通常用于生成CPython扩展模块。” 一句话:Cython可以用您的python代码生成一个在动态链接库(DLL)中编译的c ++文件,该文件可以由您的python代码直接调用。 设定pip install cython 例第一个文件是要以C速度运行的python文件脚本。请注意扩展名.pyx 档案:fib.pyx #cython: language_level=3 def fib(n): """Print the Fibonacci series up to n.""" a, b = 0, 1 while b < n: print(b, end=' ') a, b = b, a + b print() 现在我们需要创建,就像一个python Makefile(有关更多信息,请参见源文件和编译) 文件: from setuptools import setup from Cython.Build import cythonize setup( ext_modules=cythonize("fib.pyx"), ) 现在使用以下命令转换并编译所有这些 python build_ext --inplace 从python调用创建的dll (fib.cp38-win_amd64.pyd)真的很容易。它只是将其视为模块。 import fib fib.fib(50000000) # will give the expected result 保护您的应用目的是保护您的python应用。变量,您将需要:
优点:如您所知,C编译比python快速集成,因为它不处理python对象结构。处理循环时,C也可以转换。由于cython无法处理GIL,因此您可以使用所有CPU内核。 准备编译文件: from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext setup( name = 'Test app', ext_modules=[ Extension('test', sources=['script_test.pyx'], extra_link_args=['/MAP'], libraries = ["enigma_ide64"], language="c++") ], cmdclass = {'build_ext': build_ext} ) 观看API“标记是一个放置在源代码中的字节,可帮助Enigma Protector在标记内查找代码以进行处理。标记包括两部分:开始标记和结束标记。” // Markers API void __declspec(dllimport) __stdcall EP_Marker(char* Name); “ EP_RegHardware函数用于检索唯一的用户PC信息。该函数没有参数。如果函数成功,则返回值是指向以空值终止的ANSI字符串的指针。如果函数失败,则返回值为0。” // Registration API LPCWSTR __declspec(dllimport) __stdcall EP_RegHardwareIDW(); 在目录中添加一些文件(enigma Protector的sdk路径)并且您必须添加到enigma_ide.h的顶部 #include <windows.h> 脚本测试# distutils: language = c++ # cython: language_level=3 # Declare EP_Marker function in enigma_ide64.lib cdef extern from "enigma_ide.h": void EP_Marker(char* Name) # Declare EP_RegHardwareID function in enigma_ide64.lib cdef extern from "enigma_ide.h": char* EP_RegHardwareID() # Declare a trivial function def sum_it(number1,number2): return number1 + number2 # Call and print the EP_RegHardwareID print(EP_RegHardwareID()) # Crypt this stuff which will only be decrypt with registration EP_Marker("reg_crypt_begin1") print("here it's crypted") EP_Marker("reg_crypt_end1") # Protect this with virtualization EP_Marker("vm_risc_begin") a = 4 b = 7 c = a + b print('Virtualized :', c) EP_Marker("vm_risc_end") # Classic python code print("Give me the sum :", sum_it(1,2)) input("End, press key") 建立 python build_ext --inplace 使用enigma protector保护器进行保护 称它为 奖励:使用宽字符wchar_t *from cpython.ref cimport PyObject from libc.stddef cimport wchar_t cdef extern from "Python.h": PyObject* PyUnicode_FromWideChar(wchar_t *w, Py_ssize_t size) cdef extern from "enigma_ide.h": wchar_t* EP_RegHardwareIDW() cdef PyObject * pystr = PyUnicode_FromWideChar(EP_RegHardwareIDW(), -1) print(<object>pystr) |
|