Code protection method and device and electronic equipment
By obtaining and classifying the source code data of Python code, generating dynamic link library files and packaging them into binary programs, the problem of insufficient security of Python code in the existing technology is solved, and the difficulty of cracking and decompiling of the code is significantly improved.
Patent Information
- Application Number
- CN202410945491.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-15
- Publication Date
- 2025-01-10
- Estimated Expiration
- 2044-07-15
AI Technical Summary
Existing methods to improve the security of Python language development program code, such as encryption and modification of the starting address, are difficult to effectively prevent code cracking and decompilation, and are insufficient security.
By obtaining the source code data of the code to be protected, classifying and compiling local and third-party library modules, generating dynamic link library files, and packaging them with the new entry file into executable code programs to form binary programs to improve the security of the code to improve the code .
It significantly improves the difficulty of cracking and decompiling program code in Python language development and enhances the security of the code.
Smart Images

Figure CN119293758A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data security technology, and in particular to a code protection method, device and electronic equipment. Background Art
[0002] Python is an interpreted language, which means that there is no compilation link in the process of developing programs using Python. This means that programs developed using Python do not need to be compiled and can be directly run using the Python interpreter. However, this feature of Python will result in the security of program codes developed using Python being unable to be guaranteed. Existing methods for improving the security of program codes developed using Python are usually to encrypt the program code or modify the starting address of the program code. However, the above methods can only increase the difficulty of cracking the program code to a certain extent, and have little effect on improving the security of program codes developed using Python. Summary of the invention
[0003] The embodiments of the present invention provide a code protection method, device and electronic device for improving the security of program codes developed in Python language.
[0004] The specific technical solutions are as follows:
[0005] In a first aspect, an embodiment of the present invention provides a code protection method, the method comprising:
[0006] Obtain source code data of the code to be protected;
[0007] Acquire all code files from the source code data as a code file set to be processed;
[0008] Determine an entry file for the code file set to be processed;
[0009] Taking the entry file as the starting point, the import modules in each code file to be processed in the code file set to be processed are classified according to the preset loading environment characteristic words and the parameters of the loading environment characteristic words, and a local module dictionary and a third-party library module set are obtained; wherein the local module dictionary includes: the path of the source code data of each local module in the code file set to be processed relative to the root directory corresponding to the code to be protected, and the import statement corresponding to each local module; the third-party library module set includes: the import statement corresponding to each third-party library module in the code file set to be processed;
[0010] According to a preset exclusion file list, the modules to be compiled are screened out from the local module dictionary;
[0011] Compiling the module to be compiled to generate a dynamic link library file;
[0012] Write the import statements in the local module dictionary and the third-party library module set into a preset local import statement file and a third-party library import statement file respectively;
[0013] Add import statements of the local import statement file and the third-party library import statement file to the entry file to generate a new entry file;
[0014] The new entry file and the dynamic link library file are packaged to generate an executable code program.
[0015] Optionally, the step of determining an entry file for the set of code files to be processed includes:
[0016] Obtaining a main function file from the set of code files to be processed;
[0017] Determine the main function file as the entry file.
[0018] Optionally, the step of taking the entry file as the starting point and classifying the import modules in each of the code files to be processed in the code file set to be processed according to preset loading environment characteristic words and parameters of the loading environment characteristic words to obtain a local module dictionary and a third-party library module set includes:
[0019] Take the root directory corresponding to the code to be protected as the working path and the entry file as the starting point;
[0020] According to a preset loading environment characteristic word import or from, and a parameter of the loading environment characteristic word, for each of the code files to be processed in the set of code files to be processed, determining the type of the import module in the code file to be processed; wherein the parameter of the loading environment characteristic word is the path of the import module;
[0021] When the type of the import module in the code file to be processed is a local module, calculating the path of the source code data of the import module relative to the root directory corresponding to the code to be protected; generating a new import statement for the import module according to the path of the source code data of the import module relative to the root directory corresponding to the code to be protected;
[0022] Write the source code data of the import module, the path relative to the root directory corresponding to the code to be protected, and the new import statement into a preset local module dictionary in the form of key-value pairs;
[0023] When the type of the import module in the code file to be processed is a third-party library module, the import statement corresponding to the import module is written into a preset third-party library module set.
[0024] Optionally, the step of determining the type of the import module in each of the code files to be processed in the code file set to be processed according to a preset loading environment characteristic word import or from and a parameter of the loading environment characteristic word comprises:
[0025] For each of the code files to be processed in the set of code files to be processed, according to the loading environment characteristic word import or from, searching for a module import statement from the code file to be processed;
[0026] For each module import statement, obtain the parameters in the module import statement; wherein the parameters in the module import statement are the path of the imported module;
[0027] Determine whether the parameter in the module import statement starts with the string app or the string . or the string .. If so, confirm that the type of the imported module in the module import statement is a local module; if not, confirm that the type of the imported module in the module import statement is a third-party library module.
[0028] Optionally, under the Linux operating system, the step of calculating the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected includes:
[0029] If the parameter in the import statement corresponding to the import module begins with the string app, replace all characters . in the parameter with the string / to obtain the source code data of the import module, relative to the path of the root directory corresponding to the code to be protected;
[0030] If the parameter in the import statement corresponding to the import module begins with a string., then the source code data of the import module is obtained, relative to the current path of the root directory corresponding to the code to be protected; all characters after the rightmost character / in the current path are deleted to obtain a new current path; the string. at the beginning of the parameter is replaced with the new current path to obtain the source code data of the import module, relative to the path of the root directory corresponding to the code to be protected;
[0031] If the parameter in the import statement corresponding to the import module starts with the string .., then count the number of strings .. in the parameter and record it as the backtracking number; obtain the source code data of the import module, and the current path relative to the root directory corresponding to the code to be protected; delete all characters after the deletion number of characters from the right in the current path, and obtain a new current path; replace the backtracking number of strings .. from the left in the parameter with the new current path, and obtain the source code data of the import module, and the path relative to the root directory corresponding to the code to be protected.
[0032] Optionally, the step of generating a new import statement for the import module according to the source code data of the import module and relative to the path of the root directory corresponding to the code to be protected includes:
[0033] Replace all the strings / in the source code data of the import module in the path corresponding to the root directory of the code to be protected with the string ., delete the string .py in the path and add the string import as a prefix to generate a new import statement for the import module.
[0034] Optionally, the step of filtering out the modules to be compiled from the local module dictionary according to a preset exclusion file list includes:
[0035] From the local module dictionary, obtain the source code data of each local module, relative to the path of the root directory corresponding to the code to be protected;
[0036] For each local module, obtain the file name of the local module from the source code data of the local module in the path relative to the root directory corresponding to the code to be protected;
[0037] The file name of the local module is matched with the file names in the exclusion file list; if the match fails, the local module is confirmed to be a module to be compiled.
[0038] In a second aspect, an embodiment of the present invention provides a code protection device, the device comprising:
[0039] A code acquisition module, used to acquire source code data of the code to be protected;
[0040] A file module, used for acquiring all code files from the source code data as a code file set to be processed;
[0041] A determination module, used to determine an entry file for the set of code files to be processed;
[0042] A classification module is used to classify the import modules in each of the code files to be processed in the code file set based on the preset loading environment characteristic words and the parameters of the loading environment characteristic words, taking the entry file as the starting point, and obtaining a local module dictionary and a third-party library module set; wherein the local module dictionary includes: the path of the source code data of each local module in the code file set relative to the root directory corresponding to the code to be protected, and the import statement corresponding to each local module; the third-party library module set includes: the import statement corresponding to each third-party library module in the code file set;
[0043] A screening module, used for screening out modules to be compiled from the local module dictionary according to a preset exclusion file list;
[0044] A first generating module, used for compiling the module to be compiled to generate a dynamic link library file;
[0045] A writing module, used to write the import statements in the local module dictionary and the third-party library module set into a preset local import statement file and a third-party library import statement file respectively;
[0046] A second generation module is used to add import statements of the local import statement file and the third-party library import statement file to the entry file to generate a new entry file;
[0047] The packaging module is used to package the new entry file and the dynamic link library file to generate an executable code program.
[0048] Optionally, the determining module is specifically used to:
[0049] Obtaining a main function file from the set of code files to be processed;
[0050] Determine the main function file as the entry file.
[0051] Optionally, the classification module is specifically used to:
[0052] Take the root directory corresponding to the code to be protected as the working path and the entry file as the starting point;
[0053] According to a preset loading environment characteristic word import or from, and a parameter of the loading environment characteristic word, for each of the code files to be processed in the set of code files to be processed, determining the type of the import module in the code file to be processed; wherein the parameter of the loading environment characteristic word is the path of the import module;
[0054] When the type of the import module in the code file to be processed is a local module, calculating the path of the source code data of the import module relative to the root directory corresponding to the code to be protected; generating a new import statement for the import module according to the path of the source code data of the import module relative to the root directory corresponding to the code to be protected;
[0055] Write the source code data of the import module, the path relative to the root directory corresponding to the code to be protected, and the new import statement into a preset local module dictionary in the form of key-value pairs;
[0056] When the type of the import module in the code file to be processed is a third-party library module, the import statement corresponding to the import module is written into a preset third-party library module set.
[0057] Optionally, the classification module is specifically used to:
[0058] For each of the code files to be processed in the set of code files to be processed, according to the loading environment characteristic word import or from, searching for a module import statement from the code file to be processed;
[0059] For each module import statement, obtain the parameters in the module import statement; wherein the parameters in the module import statement are the path of the imported module;
[0060] Determine whether the parameter in the module import statement starts with the string app or the string . or the string .. If so, confirm that the type of the imported module in the module import statement is a local module; if not, confirm that the type of the imported module in the module import statement is a third-party library module.
[0061] Optionally, the classification module is specifically used to:
[0062] When the parameter in the import statement corresponding to the import module begins with the string app, all characters . in the parameter are replaced with the string / to obtain the source code data of the import module, relative to the path of the root directory corresponding to the code to be protected;
[0063] When the parameter in the import statement corresponding to the import module begins with a string., the source code data of the import module is obtained, relative to the current path of the root directory corresponding to the code to be protected; all characters after the rightmost character / in the current path are deleted to obtain a new current path; the string. at the beginning of the parameter is replaced with the new current path to obtain the source code data of the import module, relative to the path of the root directory corresponding to the code to be protected;
[0064] When the parameter in the import statement corresponding to the import module starts with a character string .., the number of character strings .. in the parameter is counted and recorded as the backtracking number; the source code data of the import module is obtained, and the current path relative to the root directory corresponding to the code to be protected is obtained; all characters after the deletion number characters from the right in the current path are deleted to obtain a new current path; the backtracking number of character strings .. from the left in the parameter are replaced with the new current path to obtain the source code data of the import module, and the path relative to the root directory corresponding to the code to be protected.
[0065] Optionally, the classification module is specifically used to:
[0066] Replace all the strings / in the source code data of the import module in the path corresponding to the root directory of the code to be protected with the string ., delete the string .py in the path and add the string import as a prefix to generate a new import statement for the import module.
[0067] Optionally, the screening module is specifically used to:
[0068] From the local module dictionary, obtain the source code data of each local module, relative to the path of the root directory corresponding to the code to be protected;
[0069] For each local module, obtain the file name of the local module from the source code data of the local module in the path relative to the root directory corresponding to the code to be protected;
[0070] The file name of the local module is matched with the file names in the exclusion file list; if the match fails, the local module is confirmed to be a module to be compiled.
[0071] In a third aspect, an embodiment of the present invention further provides an electronic device, comprising a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus;
[0072] The memory is used to store computer programs;
[0073] The processor is used to implement the code protection method steps described in the first aspect when executing the program stored in the memory.
[0074] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the code protection method described in the first aspect are implemented.
[0075] It can be seen that the method provided by the embodiment of the present invention can achieve the acquisition of source code data of the code to be protected; acquire all code files from the source code data as a set of code files to be processed; determine an entry file for the set of code files to be processed; take the entry file as the starting point, classify the import modules in each code file to be processed in the set of code files to be processed according to the preset loading environment feature words and the parameters of the loading environment feature words, and obtain a local module dictionary and a third-party library module set; filter out the modules to be compiled from the local module dictionary according to the preset exclusion file list; compile the modules to be compiled to generate a dynamic link library file; write the import statements in the local module dictionary and the third-party library module set into the preset local import statement file and the third-party library import statement file respectively; add import statements of the local import statement file and the third-party library import statement file respectively in the entry file to generate a new entry file; package the new entry file and the dynamic link library file to generate an executable code program. It can be seen that by implementing the method provided by the embodiment of the present invention, the code to be protected, i.e., the code written in Python language, can be converted into C language code, and then the C language code is compiled to generate a dynamic link library file, and the new entry file and the dynamic link library file are further packaged into an executable program, and the executable program generated by packaging is a binary program, thus greatly improving the difficulty of cracking and decompiling the code written in Python language. In summary, the method provided by the embodiment of the present invention can improve the security of the program code developed in Python language compared to the method of encrypting the program code or modifying the starting address of the program code.
[0076] Other features and advantages of the present invention will be described in the following description, and partly become apparent from the description, or understood by practicing the present invention. The purpose and other advantages of the present invention can be realized and obtained by the structures particularly pointed out in the written description, claims, and drawings.
[0077] The technical solution of the present invention is further described in detail below through the accompanying drawings and embodiments. BRIEF DESCRIPTION OF THE DRAWINGS
[0078] The accompanying drawings are used to provide a further understanding of the present invention and constitute a part of the specification. Together with the embodiments of the present invention, they are used to explain the present invention and do not constitute a limitation of the present invention. In the accompanying drawings:
[0079] Figure 1 A schematic diagram of a flow chart of a code protection method provided by an embodiment of the present invention;
[0080] Figure 2 Another flowchart of the code protection method provided by the embodiment of the present invention;
[0081] Figure 3 A schematic diagram of the structure of a code protection device provided by an embodiment of the present invention;
[0082] Figure 4 A schematic diagram of the internal structure of an electronic device provided by an embodiment of the present invention;
[0083] Figure 5 A schematic diagram of the organization of a Python program provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0084] The preferred embodiments of the present invention are described below in conjunction with the accompanying drawings. It should be understood that the preferred embodiments described herein are only used to illustrate and explain the present invention, and are not used to limit the present invention.
[0085] See also Figure 1 The embodiment of the present invention provides a code protection method, the method comprising:
[0086] S101: Obtain source code data of the code to be protected;
[0087] In a specific embodiment, the code to be protected is a code program written in Python language.
[0088] S102: Obtain all code files from the source code data as a code file set to be processed;
[0089] In practical applications, a program written in Python can be divided into multiple Python files. This is because a program written in Python is composed of one or more functional modules, and one functional module corresponds to one Python source file.
[0090] S103: Determine an entry file for the code file set to be processed;
[0091] In a specific embodiment, the step of determining an entry file for the code file set to be processed may include:
[0092] Get the main function file from the set of code files to be processed;
[0093] Determine the main function file as the entry file.
[0094] In practical applications, the main function file is usually used as the entry file of an executable program. In the embodiment of the present invention, the main function file of the code to be protected is first found from the set of code files to be processed, and then the main function file is used as the entry file. Specifically, the main.py file of the python program is first found, and then the main.py function file is used as the entry file.
[0095] S104: Taking the entry file as the starting point, classify the import modules in each of the code files to be processed in the code file set according to the preset loading environment characteristic words and the parameters of the loading environment characteristic words, and obtain a local module dictionary and a third-party library module set; wherein the local module dictionary includes: the path of the source code data of each local module in the code file set relative to the root directory corresponding to the code to be protected, and the import statement corresponding to each local module; the third-party library module set includes: the import statement corresponding to each third-party library module in the code file set;
[0096] In a specific embodiment, starting from the entry file, according to the preset loading environment characteristic words and the parameters of the loading environment characteristic words, the steps of classifying the import modules in each of the code files to be processed in the code file set to obtain a local module dictionary and a third-party library module set may include:
[0097] Take the root directory corresponding to the code to be protected as the working path and the entry file as the starting point;
[0098] According to the preset loading environment characteristic word import or from and the parameter of the loading environment characteristic word, for each of the code files to be processed in the code file set to be processed, the type of the import module in the code file to be processed is determined; wherein the parameter of the loading environment characteristic word is the path of the import module;
[0099] When the type of the imported module in the code file to be processed is a local module, the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected is calculated; according to the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected, a new import statement is generated for the imported module;
[0100] Write the source code data of the imported module, the path relative to the root directory corresponding to the protected code, and the new import statement into the preset local module dictionary in the form of key-value pairs;
[0101] When the type of the import module in the code file to be processed is a third-party library module, the import statement corresponding to the import module is written into a preset third-party library module set.
[0102] Specifically, according to the preset loading environment characteristic word import or from and the parameters of the loading environment characteristic word, for each of the code files to be processed in the code file set to be processed, the step of determining the type of the import module in the code file to be processed may include:
[0103] For each code file to be processed in the set of code files to be processed, according to the loading environment characteristic word import or from, find the module import statement from the code file to be processed;
[0104] For each module import statement, obtain the parameters in the module import statement; wherein the parameters in the module import statement are the path of the imported module;
[0105] Determine whether the parameter in the module import statement starts with the string app or the string . or the string .. If so, confirm that the type of the imported module in the module import statement is a local module; if not, confirm that the type of the imported module in the module import statement is a third-party library module.
[0106] Specifically, taking the Linux operating system as an example, under the Linux operating system, the step of calculating the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected may include:
[0107] If the parameter in the import statement corresponding to the imported module starts with the string app, replace all characters in the parameter with the string / to obtain the source code data of the imported module, relative to the path of the root directory corresponding to the code to be protected;
[0108] If the parameter in the import statement corresponding to the imported module begins with the string ., then obtain the source code data of the imported module, relative to the current path of the root directory corresponding to the code to be protected; delete all characters after the rightmost character / in the current path to obtain the new current path; replace the string . at the beginning of the parameter with the new current path to obtain the source code data of the imported module, relative to the path of the root directory corresponding to the code to be protected;
[0109] If the parameter in the import statement corresponding to the imported module starts with the string .., then count the number of strings .. in the parameter and record it as the backtracking number; obtain the source code data of the imported module, relative to the current path of the root directory corresponding to the code to be protected; delete all characters after the number of characters from the right in the current path to obtain the new current path; replace the number of backtracking strings .. from the left in the parameter with the new current path to obtain the source code data of the imported module, relative to the path of the root directory corresponding to the code to be protected.
[0110] Specifically, the step of generating a new import statement for the import module according to the source code data of the import module and relative to the path of the root directory corresponding to the code to be protected includes:
[0111] Replace all the strings / in the source code data of the imported module with the string . in the path corresponding to the root directory of the code to be protected, delete the string .py in the path and add the string import as a prefix to generate a new import statement for the imported module.
[0112] S105: Filtering the modules to be compiled from the local module dictionary according to the preset exclusion file list;
[0113] Specifically, the step of filtering the modules to be compiled from the local module dictionary according to the preset exclusion file list may include:
[0114] From the local module dictionary, get the source code data of each local module, relative to the path of the root directory corresponding to the code to be protected;
[0115] For each local module, obtain the file name of the local module from the source code data of the local module, relative to the path of the root directory corresponding to the code to be protected;
[0116] The file name of the local module is matched with the file names in the exclusion file list; if the match fails, the local module is confirmed as a module to be compiled.
[0117] The files in the exclusion file list are files that do not need to be compiled into .so files, such as dbutils.py files, etcdutils.py files, etc. Since not all code files in the code file set to be processed need to be compiled into .so files for protection, excluding these code files that do not need to be compiled can reduce the compilation workload and increase the speed. Although the excluded files will not be compiled, they will still be packaged normally.
[0118] Of course, in some embodiments, it is also possible not to filter the local modules in the local module dictionary, but to directly compile all the local modules in the local module dictionary, and then the compiler determines which module needs to be compiled and which module does not need to be compiled. However, this method has a large workload of compilation and will reduce the speed of the algorithm.
[0119] The .so file is a file formed by converting Python language code to C language code through the programming language converter cython on Linux system and then compiling the C language code. On Windows system, the .pyd file is a file formed by converting Python language code to C language code through the programming language converter cython and then compiling the C language code. Cython is a language that combines Python language syntax and C language syntax.
[0120] S106: Compile the module to be compiled to generate a dynamic link library file;
[0121] In the Linux operating system, the process of compiling the module to be compiled is to convert the Python language program to be protected into a C language program, and then compile the converted C language program into a .so file. The compiled .so file is a dynamic link library file.
[0122] S107: writing the import statements in the local module dictionary and the third-party library module set into a preset local import statement file and a preset third-party library import statement file respectively;
[0123] S108: Add import statements of the local import statement file and the third-party library import statement file to the import file to generate a new import file;
[0124] For example, the local import statement file is local_import, and the third-party library import statement file is third_import. After the Python language program is compiled into a .so file, the packaging program pyinstaller cannot obtain the import information of the module to be imported from the .so file. Therefore, it is necessary to add two statements, importthird_import and import local_import, to the top of the original entry file main.py. In this way, the packaging module (pyinstaller, a functional module in the third-party library in the Python language, used to package files into executable programs) is informed of which modules need to be imported.
[0125] S109: Packaging the new entry file and the dynamic link library file to generate an executable code program.
[0126] Specifically, the packaging process is to add the two statements importthird_import and import local_import to the top of the original entry file main.py, and then package the new entry file and the .so file generated by compiling the Python language program into an executable program.
[0127] Depend on Figure 1 As can be seen from the illustrated embodiment, the method provided by the embodiment of the present invention can realize the acquisition of source code data of the code to be protected; the acquisition of all code files from the source code data as a set of code files to be processed; the determination of an entry file for the set of code files to be processed; taking the entry file as the starting point, the import modules in each code file to be processed in the set of code files to be processed are classified according to the preset loading environment feature words and the parameters of the loading environment feature words to obtain a local module dictionary and a third-party library module set; according to the preset exclusion file list, the modules to be compiled are screened out from the local module dictionary; the modules to be compiled are compiled to generate a dynamic link library file; the import statements in the local module dictionary and the set of third-party library modules are written into the preset local import statement file and the third-party library import statement file respectively; the import statements of the local import statement file and the third-party library import statement file are added to the entry file respectively to generate a new entry file; the new entry file and the dynamic link library file are packaged to generate an executable code program. It can be seen that by implementing the method provided by the embodiment of the present invention, the code to be protected, i.e., the code written in Python language, can be converted into C language code, and then the C language code is compiled to generate a dynamic link library file, and the new entry file and the dynamic link library file are further packaged into an executable program, and the executable program generated by packaging is a binary program, thus greatly improving the difficulty of cracking and decompiling the code written in Python language. In summary, the method provided by the embodiment of the present invention can improve the security of the program code developed in Python language compared to the method of encrypting the program code or modifying the starting address of the program code.
[0128] The following is a detailed example to further illustrate the method provided by the embodiment of the present invention. In this embodiment, the operating system is a Linux operating system, and the code to be protected is a code program written in Python language.
[0129] Python programs are organized like Figure 5 shown.
[0130] See also Figure 2 , the specific process of this embodiment is as follows:
[0131] S201: Obtain source code data of the Python language code to be protected;
[0132] S202: Obtain all Python files from the source code data as a Python file set to be processed;
[0133] S203: Obtain the main.py file from the set of python files to be processed;
[0134] In fact, the main.py file is the entry file of the Python language code to be protected.
[0135] S204: taking the root directory of the Python language code to be protected as the working path $proj_path and main.py as the entry file, for each Python file to be processed in the Python file set to be processed, according to the loading environment feature word import or from, find the module import statement from the Python file to be processed;
[0136] Python is a high-level programming language. Its power lies in the fact that it can implement various functions by importing different modules. The method of importing modules in Python is: import module path, or from path import module.
[0137] S205: For each module import statement, obtain the parameters in the module import statement;
[0138] The parameter in the module import statement is the path of the imported module.
[0139] S206: Determine whether the parameter in the module import statement starts with the string app or the string . or the string .. If yes, confirm that the type of the imported module in the module import statement is a local module; and when the parameter in the module import statement starts with the string app, execute step S207; when the parameter in the module import statement starts with the string ., execute step S208; when the parameter in the module import statement starts with the string .., execute step S209; if no, confirm that the type of the imported module in the module import statement is a third-party library module; and execute step S218;
[0140] In the embodiment of the present invention, all source code data of the python language code are placed in the app directory of the root directory of the python language code, so the parameter in the module import statement (the parameter in the module import statement is the path corresponding to the imported module) is a path starting with the string app, and the module corresponding to the path must be the current module. The parameter in the module import statement is a path starting with the string . and the string .. It is a relative path relative to the path of the current python file, so the module corresponding to the path must also be a local module. In addition, the modules corresponding to the paths in the module import statement are all third-party library modules.
[0141] Modules in Python are divided into standard modules, also known as local modules, and third-party library modules. Local modules in Python are the default modules that come with Python when it is installed. Third-party modules in Python need to be downloaded and installed in the Python installation directory. Different third-party modules have different installation and usage methods, but their calling methods are the same and all need to be called using the import statement.
[0142] S207: Replace all characters in the parameter of the module import statement with the string / to obtain the path $mod_path of the source code data of the imported module relative to $proj_path;
[0143] S208: Obtain the current path $cur_path of the source code data of the imported module relative to $proj_path; delete all characters after the rightmost character / in $cur_path to obtain a new current path $base_path; replace the string . at the beginning of the parameter in the module import statement with the new current path $base_path to obtain the path $mod_path of the source code data of the imported module relative to $proj_path;
[0144] For example, if the module import statement is import.aaa / bbb.py, then the parameter in the module import statement is .aaa / bbb.py. The current path $cur_path of the source code data of the imported module relative to the working path $proj_path is C: / myprogram / python_project / app / mod1 / fun1.py, then the string at the beginning of the parameter .aaa / bbb.py refers to the directory C: / myprogram / python_project / app / mod1 / . Therefore, to obtain the path of bbb.py (that is, the path $mod_path of the source code data of the imported module relative to $proj_path), you need to remove C: / myprogram / python_ All characters after the last character / in project / app / mod1 / fun1.py (that is, the file name fun1.py) are replaced to get the new current path $base_path: C: / myprogram / python_project / app / mod1 / , and then the string . at the beginning of the parameter .aaa / bbb.py is replaced with the new current path $base_path, so as to get the path $mod_path of the source code data of the imported module relative to $proj_path: C: / myprogram / python_project / app / mod1 / aaa / bbb.py.
[0145] S209: Count the number of character strings .. in the parameters of the module import statement, recorded as the backtracking number $back_count; obtain the current path $cur_path of the source code data of the imported module relative to $proj_path; delete all characters after the $back_count character from the right in $cur_path, and obtain the new current path $base_path; replace the first $back_count character strings .. from the left in the parameters of the module import statement with the new current path $base_path, and obtain the path $mod_path of the source code data of the imported module relative to $proj_path;
[0146] For example: the module import statement is import...ccc / ddd.py, then the parameter in the module import statement is ....ccc / ddd.py. Count the number of strings .. in the parameters of the module import statement, that is, the backtracking number $back_count is 2, and the current path $cur_path of the imported module source data relative to the working path $proj_path is C: / myprogram / python_project / app / mod1 / fun1.py. Delete all characters after the $back_count character / from the right in $cur_path, that is, delete all characters after the 2nd character / from the right in $cur_path to get the new current path $base_path: C: / myprogram / python_project / app / , and replace the first $back_count characters, that is, the first two strings .. from the left in the parameter ....ccc / ddd.py in the module import statement with the new current path $base_path, and get the path $mod_path of the imported module source code data relative to $proj_path: C: / myprogram / python_project / app / ccc / ddd.py.
[0147] S210: Replace all the strings / in the $mod_path path with the string . and delete the string .py at the end of the $mod_path path and add the string import as a prefix to the $mod_path path, generating a new import statement $local_import corresponding to the imported module;
[0148] S211: using the $mod_path path as a key and the new import statement $local_import corresponding to the $mod_path path as a value, writing the $mod_path path and the new import statement $local_import corresponding to the $mod_path path into the local module dictionary local_dict in the form of a key-value pair;
[0149] S212: Obtain a path $mod_path of the source code data of a local module relative to $proj_path from the local module dictionary local_dict; obtain all characters after the rightmost character / in the $mod_path path, record them as $mod_name, and match the $mod_name with the file names in the exclusion file list; if the match fails, confirm that the local module is a module to be compiled;
[0150] If the match succeeds, it is confirmed that the local module is a module that does not need to be compiled into a .so file. Excluding these code files that do not need to be compiled can reduce the compilation workload and increase the algorithm speed. Although the excluded files will not be compiled, they will still be packaged normally.
[0151] S213: Determine whether, for each local module in the local module dictionary local_dict, the $mod_name in the $mod_path path is matched with the file name in the exclusion file list; if yes, execute step S214; if not, return to continue to execute step S212;
[0152] S214: construct an extension module according to the $mod_path path and $mod_name of the module to be compiled;
[0153] Among them, one module to be compiled corresponds to one extension function.
[0154] In a specific embodiment, the process of constructing an extension module according to the $mod_path and $mod_name of the module to be compiled can be: constructing an Extension object, i.e., constructing an extension module, by using the $mod_path and $mod_name of the module to be compiled as the Extension class instantiation parameters; and saving the constructed extension module distutils.extension to the extension module list extension_list. Among them, distutils is a tool module for constructing extension modules in the Python language. distutils.extension is a built-in function in distutils, which is called in this embodiment to pass parameters to its Extension class, and the parameters passed in are all the modules to be compiled that need to be compiled.
[0155] S215: Compile each extension module to generate a .so file;
[0156] In a specific embodiment, for each extension module, the process of compiling and generating a .so file may be: calling setup of distutils.core, executing cythonize, and passing extension_list as a parameter to start compiling. Distutils.core is a function in distutils (used to build extension modules in Python language), and cythonize is a compiler in the programming language converter cython.
[0157] The .so file is a file formed after the Python language code is converted to the C language code through the programming language converter cython on the Linux system, and then the C language code is compiled. On the Windows system, the file formed after the Python language code is converted to the C language code through the programming language converter cython and then the C language code is compiled is a .pyd file. The method provided in the embodiment of the present invention can also be used on the Windows system. The character / in this embodiment refers to the path separator. The path separator on the Linux system is a forward slash / , and the path separator on the Windows system is a backslash \. When the method provided in the embodiment of the present invention is applied to the Windows system, it is necessary to pay attention to the direction of the path separator, that is, the path separator in this embodiment needs to be replaced with a backslash \.
[0158] S216: Store the compiled .so file in the result directory in the working path;
[0159] The directory structure of the subdirectory app in the result directory is exactly the same as the directory structure of app in the working path $proj_path.
[0160] S217: Replace the app directory in the working path $proj_path with the app directory in result;
[0161] S218: Write the import statement corresponding to the import module into the third-party library module set $third_set;
[0162] S219: Write all import statements in the third-party library module set $third_set into the third-party library import statement file third_import.py;
[0163] S220: Write all import statements in the local module dictionary local_dict into the local import statement file local_import.py;
[0164] S221: Add two import statements, import third_import and importlocal_import, to the top of the entry file main.py to generate a new entry file;
[0165] The import statements corresponding to the local modules constitute the local import statement file local_import; the import statements corresponding to the third-party library modules constitute the third-party library import statement file third_import; because after the Python language program is compiled into a .so file, the packaging program pyinstaller cannot obtain the import information of the module to be imported from the .so file, therefore, it is necessary to add import third_import and importlocal_import statements at the top of the original entry file main.py to inform the packaging module which modules need to be imported.
[0166] S222: Packaging the new entry file and the compiled .so file to generate an executable code program.
[0167] In a specific embodiment, the new entry file and the compiled .so file are packaged to generate an executable code program, which can be: executing the pyinstaller -F main.py statement to generate an executable program named main, where the executable program is located in the dist directory under the working path.
[0168] Depend on Figure 2 It can be seen from the embodiment shown that by implementing the method provided by the embodiment of the present invention, the code to be protected, i.e., the code written in Python language, can be converted into C language code, and then the C language code can be compiled to generate a dynamic link library file, and the new entry file and the dynamic link library file can be further packaged into an executable program, and the executable program generated by packaging is a binary program, thus greatly improving the difficulty of cracking and decompiling the code written in Python language. In summary, it can be seen that the method provided by the embodiment of the present invention can improve the security of the program code developed in Python language compared to the method of encrypting the program code or modifying the starting address of the program code.
[0169] and Figure 1 Corresponding to the embodiment shown in FIG. 1 , the embodiment of the present invention further provides a code protection device. Figure 3As shown, the device includes: a code acquisition module 301, a file module 302, a determination module 303, a classification module 304, a screening module 305, a first generation module 306, a writing module 307, a second generation module 308, and a packaging module 309, wherein:
[0170] The code acquisition module 301 is used to acquire source code data of the code to be protected;
[0171] A file module 302 is used to obtain all code files from the source code data as a code file set to be processed;
[0172] The determination module 303 is used to determine an entry file for the code file set to be processed;
[0173] The classification module 304 is used to classify the import modules in each of the code files to be processed in the code file set based on the preset loading environment characteristic words and the parameters of the loading environment characteristic words, taking the entry file as the starting point, and obtain a local module dictionary and a third-party library module set; wherein the local module dictionary includes: the path of the source code data of each local module in the code file set relative to the root directory corresponding to the code to be protected, and the import statement corresponding to each local module; the third-party library module set includes: the import statement corresponding to each third-party library module in the code file set;
[0174] The screening module 305 is used to screen out the modules to be compiled from the local module dictionary according to the preset exclusion file list;
[0175] The first generating module 306 is used to compile the module to be compiled and generate a dynamic link library file;
[0176] A writing module 307 is used to write the import statements in the local module dictionary and the third-party library module set into a preset local import statement file and a third-party library import statement file respectively;
[0177] The second generation module 308 is used to add import statements of the local import statement file and the third-party library import statement file to the entry file to generate a new entry file;
[0178] The packaging module 309 is used to package the new entry file and the dynamic link library file to generate an executable code program.
[0179] Optionally, the determination module 303 is specifically configured to:
[0180] Get the main function file from the set of code files to be processed;
[0181] Determine the main function file as the entry file.
[0182] Optionally, the classification module 304 is specifically used for:
[0183] Take the root directory corresponding to the code to be protected as the working path and the entry file as the starting point;
[0184] According to the preset loading environment characteristic word import or from and the parameter of the loading environment characteristic word, for each of the code files to be processed in the code file set to be processed, the type of the import module in the code file to be processed is determined; wherein the parameter of the loading environment characteristic word is the path of the import module;
[0185] When the type of the imported module in the code file to be processed is a local module, the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected is calculated; according to the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected, a new import statement is generated for the imported module;
[0186] Write the source code data of the imported module, the path relative to the root directory corresponding to the protected code, and the new import statement into the preset local module dictionary in the form of key-value pairs;
[0187] When the type of the import module in the code file to be processed is a third-party library module, the import statement corresponding to the import module is written into a preset third-party library module set.
[0188] Optionally, the classification module 304 is specifically used for:
[0189] For each code file to be processed in the set of code files to be processed, according to the loading environment characteristic word import or from, find the module import statement from the code file to be processed;
[0190] For each module import statement, obtain the parameters in the module import statement; wherein the parameters in the module import statement are the path of the imported module;
[0191] Determine whether the parameter in the module import statement starts with the string app or the string . or the string .. If so, confirm that the type of the imported module in the module import statement is a local module; if not, confirm that the type of the imported module in the module import statement is a third-party library module.
[0192] Optionally, the classification module 304 is specifically used for:
[0193] When the parameter in the import statement corresponding to the imported module starts with the string app, replace all characters in the parameter with the string / to obtain the source code data of the imported module, relative to the path of the root directory corresponding to the code to be protected;
[0194] When the parameter in the import statement corresponding to the imported module begins with the string ., the source code data of the imported module is obtained, relative to the current path of the root directory corresponding to the code to be protected; all characters after the rightmost character / in the current path are deleted to obtain the new current path; the string . at the beginning of the parameter is replaced with the new current path to obtain the source code data of the imported module, relative to the path of the root directory corresponding to the code to be protected;
[0195] When the parameter in the import statement corresponding to the imported module starts with the string .., the number of strings .. in the parameter is counted and recorded as the backtracking number; the source code data of the imported module is obtained, relative to the current path of the root directory corresponding to the code to be protected; all characters after the number of characters deleted from the right in the current path are deleted, and the new current path is obtained; the backtracking number of strings .. from the left in the parameter are replaced with the new current path, and the source code data of the imported module is obtained, relative to the path of the root directory corresponding to the code to be protected.
[0196] Optionally, the classification module 304 is specifically used for:
[0197] Replace all the strings / in the source code data of the imported module with the string . in the path corresponding to the root directory of the code to be protected, delete the string .py in the path and add the string import as a prefix to generate a new import statement for the imported module.
[0198] Optionally, the screening module 305 is specifically used for:
[0199] From the local module dictionary, get the source code data of each local module, relative to the path of the root directory corresponding to the code to be protected;
[0200] For each local module, obtain the file name of the local module from the source code data of the local module, relative to the path of the root directory corresponding to the code to be protected;
[0201] The file name of the local module is matched with the file names in the exclusion file list; if the match fails, the local module is confirmed as a module to be compiled.
[0202] It can be seen that by implementing the device provided by the embodiment of the present invention, the code to be protected, i.e., the code written in Python language, can be converted into C language code, and then the C language code is compiled to generate a dynamic link library file, and the new entry file and the dynamic link library file are further packaged into an executable program, and the executable program generated by packaging is a binary program, thus greatly improving the difficulty of cracking and decompiling the code written in Python language. In summary, it can be seen that the device provided by the embodiment of the present invention can improve the security of program codes developed in Python language compared with the prior art.
[0203] and Figure 1 Corresponding to the embodiment shown, the embodiment of the present invention further provides an electronic device, see Figure 4 , including a processor 401, a communication interface 402, a memory 403 and a communication bus 404, wherein the processor 401, the communication interface 402, and the memory 403 communicate with each other through the communication bus 404;
[0204] Memory 403, used for storing computer programs;
[0205] The processor 401 is used to implement any code protection method step described in the above embodiments when executing the program stored in the memory.
[0206] It can be seen that the electronic device provided by the embodiment of the present invention can convert the code to be protected, that is, the code written in Python language, into C language code, and then compile the C language code to generate a dynamic link library file, and further package the new entry file and the dynamic link library file into an executable program, and the executable program generated by packaging is a binary program, so that the difficulty of cracking and decompiling the code written in Python language is greatly improved. In summary, the electronic device provided by the embodiment of the present invention can improve the security of the program code developed in Python language compared with the prior art.
[0207] and Figure 1 Corresponding to the embodiment shown, an embodiment of the present invention further provides a computer-readable storage medium, in which a computer program is stored. When the computer program is executed by a processor, the method steps of any code protection described in the above embodiments are implemented.
[0208] It can be seen that by implementing the storage medium provided in the embodiment of the present invention, the code to be protected, i.e., the code written in Python language, can be converted into C language code, and then the C language code is compiled to generate a dynamic link library file, and the new entry file and the dynamic link library file are further packaged into an executable program, and the executable program generated by packaging is a binary program, thus greatly improving the difficulty of cracking and decompiling the code written in Python language. In summary, the storage medium provided in the embodiment of the present invention can improve the security of the program code developed in Python language compared with the prior art.
[0209] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage and optical storage, etc.) containing computer-usable program code.
[0210] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0211] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0212] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The present invention is a block or a plurality of blocks. Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.
Claims
1. A code protection method, characterized in that: The method comprises: Obtain source code data of the code to be protected; Acquire all code files from the source code data as a code file set to be processed; Determine an entry file for the code file set to be processed; Taking the entry file as the starting point, the import modules in each code file to be processed in the code file set to be processed are classified according to the preset loading environment characteristic words and the parameters of the loading environment characteristic words, and a local module dictionary and a third-party library module set are obtained; wherein the local module dictionary includes: the path of the source code data of each local module in the code file set to be processed relative to the root directory corresponding to the code to be protected, and the import statement corresponding to each local module; the third-party library module set includes: the import statement corresponding to each third-party library module in the code file set to be processed; According to a preset exclusion file list, the modules to be compiled are screened out from the local module dictionary; Compiling the module to be compiled to generate a dynamic link library file; Write the import statements in the local module dictionary and the third-party library module set into a preset local import statement file and a third-party library import statement file respectively; Add import statements of the local import statement file and the third-party library import statement file to the entry file to generate a new entry file; The new entry file and the dynamic link library file are packaged to generate an executable code program.
2. The method according to claim 1, characterized in that The step of determining an entry file for the code file set to be processed includes: Obtaining a main function file from the set of code files to be processed; Determine the main function file as the entry file.
3. The method according to claim 1, characterized in that The step of taking the entry file as the starting point and classifying the import modules in each of the code files to be processed according to the preset loading environment characteristic words and the parameters of the loading environment characteristic words to obtain a local module dictionary and a third-party library module set includes: Take the root directory corresponding to the code to be protected as the working path and the entry file as the starting point; According to a preset loading environment characteristic word import or from, and a parameter of the loading environment characteristic word, for each of the code files to be processed in the set of code files to be processed, determining the type of the import module in the code file to be processed; wherein the parameter of the loading environment characteristic word is the path of the import module; When the type of the import module in the code file to be processed is a local module, calculating the path of the source code data of the import module relative to the root directory corresponding to the code to be protected; generating a new import statement for the import module according to the path of the source code data of the import module relative to the root directory corresponding to the code to be protected; Write the source code data of the import module, the path relative to the root directory corresponding to the code to be protected, and the new import statement into a preset local module dictionary in the form of key-value pairs; When the type of the import module in the code file to be processed is a third-party library module, the import statement corresponding to the import module is written into a preset third-party library module set.
4. The method according to claim 3, characterized in that The step of determining the type of the import module in each of the code files to be processed in the code file set according to the preset loading environment characteristic word import or from and the parameter of the loading environment characteristic word comprises: For each of the code files to be processed in the set of code files to be processed, according to the loading environment characteristic word import or from, searching for a module import statement from the code file to be processed; For each module import statement, obtain the parameters in the module import statement; wherein the parameters in the module import statement are the path of the imported module; Determine whether the parameter in the module import statement starts with the string app or the string . or the string .. If so, confirm that the type of the imported module in the module import statement is a local module; if not, confirm that the type of the imported module in the module import statement is a third-party library module.
5. The method according to claim 4, characterized in that Under the Linux operating system, the step of calculating the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected includes: If the parameter in the import statement corresponding to the import module begins with the string app, replace all characters . in the parameter with the string / to obtain the source code data of the import module, relative to the path of the root directory corresponding to the code to be protected; If the parameter in the import statement corresponding to the import module begins with a string., then the source code data of the import module is obtained, relative to the current path of the root directory corresponding to the code to be protected; all characters after the rightmost character / in the current path are deleted to obtain a new current path; the string. at the beginning of the parameter is replaced with the new current path to obtain the source code data of the import module, relative to the path of the root directory corresponding to the code to be protected; If the parameter in the import statement corresponding to the import module starts with the string .., then count the number of strings .. in the parameter and record it as the backtracking number; obtain the source code data of the import module, and the current path relative to the root directory corresponding to the code to be protected; delete all characters after the deletion number of characters from the right in the current path, and obtain a new current path; replace the backtracking number of strings .. from the left in the parameter with the new current path, and obtain the source code data of the import module, and the path relative to the root directory corresponding to the code to be protected.
6. The method according to claim 5, characterized in that The step of generating a new import statement for the import module according to the source code data of the import module and relative to the path of the root directory corresponding to the code to be protected comprises: Replace all the strings / in the source code data of the import module in the path corresponding to the root directory of the code to be protected with the string ., delete the string .py in the path and add the string import as a prefix to generate a new import statement for the import module.
7. The method according to claim 1, characterized in that The step of filtering out the modules to be compiled from the local module dictionary according to the preset exclusion file list includes: From the local module dictionary, obtain the source code data of each local module, relative to the path of the root directory corresponding to the code to be protected; For each local module, obtain the file name of the local module from the source code data of the local module in the path relative to the root directory corresponding to the code to be protected; The file name of the local module is matched with the file names in the exclusion file list; if the match fails, the local module is confirmed to be a module to be compiled.
8. A code protection device, characterized in that: The device comprises: A code acquisition module, used to acquire source code data of the code to be protected; A file module, used for acquiring all code files from the source code data as a code file set to be processed; A determination module, used to determine an entry file for the set of code files to be processed; A classification module is used to classify the import modules in each of the code files to be processed in the code file set based on the preset loading environment characteristic words and the parameters of the loading environment characteristic words, taking the entry file as the starting point, and obtaining a local module dictionary and a third-party library module set; wherein the local module dictionary includes: the path of the source code data of each local module in the code file set relative to the root directory corresponding to the code to be protected, and the import statement corresponding to each local module; the third-party library module set includes: the import statement corresponding to each third-party library module in the code file set; A screening module, used for screening out modules to be compiled from the local module dictionary according to a preset exclusion file list; A first generating module, used for compiling the module to be compiled to generate a dynamic link library file; A writing module, used to write the import statements in the local module dictionary and the third-party library module set into a preset local import statement file and a third-party library import statement file respectively; A second generation module is used to add import statements of the local import statement file and the third-party library import statement file to the entry file to generate a new entry file; The packaging module is used to package the new entry file and the dynamic link library file to generate an executable code program.
9. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; The memory is used to store computer programs; The processor is used to implement the method steps described in any one of claims 1-7 when executing the program stored in the memory.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method steps described in any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Multi-process concurrence difference calculation server system and implementation method thereof
CN106095601A
Java source code protection method based on dynamic link library
CN107992723A
Class file protection and operation method and device, equipment and storage medium
CN112395564A
Python code decompiling method and system and readable storage medium
CN114721668A
Apparatus, system, and method for trace insertion
US20060200806A1