A code protection method, device and electronic device

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 higher code security and cracking difficulty is achieved.

CN119293758BActive Publication Date: 2025-05-27BEIJING HOLYZONE TECH CORP CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202410945491.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-07-15
Publication Date
2025-05-27
Estimated Expiration
2044-07-15

AI Technical Summary

Technical Problem

The existing technology is difficult to effectively improve the security of program code developed by Python language, and the existing methods of encrypting and modifying the starting address can only increase the difficulty of cracking to a certain extent.

Method used

By obtaining the source code data of the code to be protected, classifying the import module, generating dynamic link library files, and packaging the new entry file and dynamic link library files into executable code programs to form binary programs to improve security.

Benefits of technology

It significantly improves the security of program code developed by Python language and increases the difficulty of cracking and decompiling.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119293758B_ABST
    Figure CN119293758B_ABST
Patent Text Reader

Abstract

The present invention discloses a code protection method, apparatus and electronic device. The method includes: obtaining source code data of code to be protected; obtaining all code files from the source code data as a set of code files to be processed; determining an entry file; starting from the entry file, classifying import modules in the code files to be processed in the set of code files to be processed according to loading environment feature words and their parameters to obtain a local module dictionary and a third-party library module set; screening out modules to be compiled from the local module dictionary according to an exclusion file list; compiling the modules to be compiled to generate dynamic link library files; writing the import statements in the local module dictionary and the third-party library module set into local and third-party library import statement files respectively; adding import statements for importing the local and third-party library import statement files in the entry file to generate a new entry file; and packaging the new entry file and the dynamic link library files to generate an executable code program.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of data security, and in particular, to a code protection method, apparatus, and electronic device. Background Art

[0002] The Python language is an interpreted language, which means that there is no compilation process during the development of a program using the Python language. This also means that a program developed using the Python language does not require compilation and can be directly run using the Python language interpreter. However, this feature of the Python language leads to the inability to guarantee the security of the program code developed using the Python language. Existing methods for improving the security of program code developed using the Python language usually encrypt the program code or modify the starting address of the program code. However, the above methods can only increase the cracking difficulty of the program code to a certain extent, and have a relatively small effect on improving the security of the program code developed using the Python language. Summary of the Invention

[0003] Embodiments of the present invention provide a code protection method, apparatus, and electronic device for improving the security of program code developed using the 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 including:

[0006] Obtain source code data of the code to be protected;

[0007] Obtain all code files from the source code data as a set of code files to be processed;

[0008] Determine an entry file for the set of code files to be processed;

[0009] Starting from the entry file, classify the import modules in each code file to be processed in the set of code files to be processed according to preset loading environment feature words and parameters of the loading environment feature words, to 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 set of code files 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 set of code files to be processed;

[0010] Screen out modules to be compiled from the local module dictionary according to a preset exclusion file list;

[0011] Compile 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 for importing the local import statement file and the third-party library import statement file to the entry file respectively to generate a new entry file;

[0014] Package the new entry file and the dynamic link library file 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] Obtain the 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 classifying the imported modules in each code file to be processed in the set of code files to be processed based on the preset loading environment feature words and the parameters of the loading environment feature words with the entry file as the starting point to obtain a local module dictionary and a third-party library module set includes:

[0019] Use the root directory corresponding to the code to be protected as the working path and the entry file as the starting point;

[0020] Based on the preset loading environment feature word "import" or "from" and the parameter of the loading environment feature word, determine the type of the imported module in each code file to be processed in the set of code files to be processed; wherein, the parameter of the loading environment feature word is the path of the imported module;

[0021] When the type of the imported module in the code file to be processed is a local module, calculate the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected; generate a new import statement for the imported module 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;

[0022] Write the path of the source code data of the imported module 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 imported module in the to-be-processed code file is a third-party library module, write the import statement corresponding to the imported module into a preset third-party library module set.

[0024] Optionally, the step of determining the type of the imported module in the to-be-processed code file according to the preset loading environment feature word "import" or "from" and the parameters of the loading environment feature word for each to-be-processed code file in the to-be-processed code file set includes:

[0025] For each to-be-processed code file in the to-be-processed code file set, find out the module import statement from the to-be-processed code file according to the loading environment feature word "import" or "from";

[0026] For each module import statement, obtain the parameters in the module import statement; wherein, the parameters in the module import statement are the paths of the imported modules;

[0027] Judge 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 to-be-protected code includes:

[0029] If the parameter in the import statement corresponding to the imported module starts with the string "app", replace all the characters "." in the parameter with the string " / " to obtain the path of the source code data of the imported module relative to the root directory corresponding to the to-be-protected code;

[0030] If the parameter in the import statement corresponding to the imported module starts with the string ".", obtain the current path of the source code data of the imported module relative to the root directory corresponding to the to-be-protected code; delete all the characters after the rightmost character " / " in the current path to obtain a new current path; replace the string "." at the beginning of the parameter with the new current path to obtain the path of the source code data of the imported module relative to the root directory corresponding to the to-be-protected code;

[0031] If the parameter in the import statement corresponding to the import module starts with the string "..", count the number of occurrences of the string ".." in the parameter, which is denoted as the backtracking count; obtain the source code data of the import module, which is the current path relative to the root directory corresponding to the code to be protected; delete all characters after the character " / " at the position of the deletion count from the right in the current path, to obtain a new current path; replace the first backtracking count number of strings ".." from the left in the parameter with the new current path, to obtain the path of the source code data of the import module 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 path of the source code data of the import module relative to the root directory corresponding to the code to be protected includes:

[0033] Replace all strings " / " in the path of the source code data of the import module relative to the root directory corresponding to 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 the new import statement of the import module.

[0034] Optionally, the step of screening out the modules to be compiled from the local module dictionary according to the preset exclusion file list includes:

[0035] Obtain the source code data of each local module, which is the path relative to the root directory corresponding to the code to be protected, from the local module dictionary;

[0036] For each local module, obtain the file name of the local module from the path of the source code data of the local module relative to the root directory corresponding to the code to be protected;

[0037] Match the file name of the local module with the file names in the exclusion file list; if the match fails, confirm that the local module is a module to be compiled.

[0038] In a second aspect, an embodiment of the present invention provides a code protection device, the device includes:

[0039] A code acquisition module, configured to acquire the source code data of the code to be protected;

[0040] A file module, configured to acquire all code files from the source code data as a set of code files to be processed;

[0041] A determination module, configured to determine an entry file for the set of code files to be processed;

[0042] A classification module, which is used to start from the entry file and classify the import modules in each to-be-processed code file in the to-be-processed code file set according to the preset loading environment feature words and the parameters of the loading environment feature words, so as to 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 to-be-processed 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 to-be-processed code file set;

[0043] A screening module, which is used to screen out the to-be-compiled modules from the local module dictionary according to the preset exclusion file list;

[0044] A first generation module, which is used to compile the to-be-compiled modules to generate dynamic link library files;

[0045] A writing module, which 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;

[0046] A second generation module, which is used to add the import statements for importing the local import statement file and the third-party library import statement file to the entry file respectively to generate a new entry file;

[0047] A packaging module, which is used to package the new entry file and the dynamic link library file to generate an executable code program.

[0048] Optionally, the determination module is specifically used for:

[0049] Obtain the main function file from the to-be-processed code file set;

[0050] Determine the main function file as the entry file.

[0051] Optionally, the classification module is specifically used for:

[0052] Use 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 the preset loading environment feature word "import" or "from" and the parameters of the loading environment feature word, for each to-be-processed code file in the to-be-processed code file set, determine the type of the import module in the to-be-processed code file; wherein, the parameter of the loading environment feature word is the path of the import module;

[0054] When the type of the imported module in the code file to be processed is a local module, calculate the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected; generate a new import statement for the imported module 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.

[0055] Write the source code data of the imported 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 imported module in the code file to be processed is a third-party library module, write the import statement corresponding to the imported module into a preset third-party library module set.

[0057] Optionally, the classification module is specifically used for:

[0058] For each code file to be processed in the code file set to be processed, find the module import statement from the code file to be processed according to the loading environment feature words "import" or "from".

[0059] For each module import statement, obtain the parameters in the module import statement; wherein, the parameters in the module import statement are the paths of the imported modules.

[0060] Judge 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 for:

[0062] When the parameter in the import statement corresponding to the imported module starts with the string "app", replace all the characters "." in the parameter with the string " / " to obtain the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected.

[0063] When the parameter in the import statement corresponding to the imported module starts with the string ".", obtain the current path of the source code data of the imported module relative to the root directory corresponding to the code to be protected; delete all the characters after the rightmost character " / " in the current path to obtain a new current path; replace the starting string "." in the parameter with the new current path to obtain the path of the source code data of the imported module relative to 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 the string '..', count the number of occurrences of the string '..' in the parameter, which is denoted as the backtracking count; obtain the source code data of the import module, which is the current path relative to the root directory corresponding to the code to be protected; delete all characters after the character at the position of the deletion count from the right in the current path, to obtain a new current path; replace the first backtracking count number of strings '..' from the left in the parameter with the new current path, to obtain the path of the source code data of the import module relative to the root directory corresponding to the code to be protected.

[0065] Optionally, the classification module is specifically configured to:

[0066] Replace all strings ' / ' in the path of the source code data of the import module relative to the root directory corresponding to 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 configured to:

[0068] Obtain the source code data of each local module, which is the path relative to the root directory corresponding to the code to be protected, from the local module dictionary;

[0069] For each local module, obtain the file name of the local module from the path of the source code data of the local module relative to the root directory corresponding to the code to be protected;

[0070] Match the file name of the local module with the file names in the excluded file list; if the match fails, confirm that the local module is a module to be compiled.

[0071] In a third aspect, an embodiment of the present invention further provides an electronic device, including a processor, a communication interface, a memory, and a communication bus, where the processor, the communication interface, and the memory communicate with each other through the communication bus;

[0072] The memory is used to store a computer program;

[0073] When the processor executes the program stored on the memory, it implements the steps of the code protection method described in the first aspect above.

[0074] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, where the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, it implements the steps of the code protection method described in the first aspect above.

[0075] It can be seen that the method provided by the embodiments of the present invention can achieve obtaining the source code data of the code to be protected; obtaining all code files from the source code data as the set of code files to be processed; determining an entry file for the set of code files to be processed; starting from the entry file, classifying the imported 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, to obtain a local module dictionary and a third-party library module set; screening out the modules to be compiled from the local module dictionary according to the preset exclusion file list; compiling the modules to be compiled to generate a dynamic link library file; writing the import statements in the local module dictionary and the third-party library module set into the preset local import statement file and third-party library import statement file respectively; adding the import statements for importing the local import statement file and the third-party library import statement file to the entry file respectively to generate a new entry file; and packaging 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 embodiments of the present invention, the code to be protected, i.e., the code written in the 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. Further, the new entry file and the dynamic link library file can be packaged into an executable program, and the generated executable program is a binary program, thus greatly improving the difficulty of cracking and decompiling the code written in the Python language. In summary, the method provided by the embodiments of the present invention can improve the security of the program code developed in the Python language more than the methods 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 specification, and in part will be obvious from the specification, or will be understood by implementing the present invention. The objectives and other advantages of the present invention can be realized and obtained by the structures specifically pointed out in the written specification, claims, and drawings.

[0077] The technical solutions of the present invention will be further described in detail below through the drawings and embodiments. Description of the Drawings

[0078] The drawings are used to provide a further understanding of the present invention, and constitute a part of the specification. They are used together with the embodiments of the present invention to explain the present invention, and do not constitute a limitation to the present invention. In the drawings:

[0079] Figure 1 is a schematic flowchart of a code protection method provided by an embodiment of the present invention;

[0080] Figure 2 is another schematic flowchart of a code protection method provided by an embodiment of the present invention;

[0081] Figure 3 A structural schematic diagram of the code protection device provided by an embodiment of the present invention;

[0082] Figure 4 An internal structural schematic diagram of the electronic device provided by an embodiment of the present invention;

[0083] Figure 5 A schematic diagram of the organization form of the Python program provided by an embodiment of the present invention. Specific implementation manners

[0084] The following describes the preferred embodiments of the present invention with reference to the accompanying drawings. It should be understood that the preferred embodiments described herein are only for the purpose of illustrating and explaining the present invention, and are not used to limit the present invention.

[0085] See Figure 1 , an embodiment of the present invention provides a code protection method, which includes:

[0086] S101: Obtain the 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 the Python language.

[0088] S102: Obtain all code files from the source code data as the set of code files to be processed;

[0089] In practical applications, a program written in the Python language can be divided into multiple Python files. This is because a program written in the Python language 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 set of code files to be processed;

[0091] In a specific embodiment, the step of determining an entry file for the set of code files to be processed may include:

[0092] Obtain 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 embodiments of the present invention, first, the main function file of the code to be protected is found from the set of code files to be processed, and then this main function file is used as the entry file. Specifically, that is, first find the main.py file of the Python program, and then use this main.py function file as the entry file.

[0095] S104: Starting from the entry file, according to the preset loading environment feature words and the parameters of the loading environment feature words, classify the imported modules in each code file to be processed in the set of code files to be processed, 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 set of code files 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 set of code files to be processed.

[0096] In a specific embodiment, the step of classifying the imported 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 starting from the entry file to obtain a local module dictionary and a third-party library module set may include:

[0097] Use the root directory corresponding to the code to be protected as the working path, and start from the entry file;

[0098] According to the preset loading environment feature word "import" or "from", and the parameter of the loading environment feature word, for each code file to be processed in the set of code files to be processed, determine the type of the imported module in the code file to be processed; wherein, the parameter of the loading environment feature word is the path of the imported module.

[0099] When the type of the imported module in the code file to be processed is a local module, calculate the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected; 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, generate a new import statement for the imported module.

[0100] Write the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected and the new import statement into the preset local module dictionary in the form of key-value pairs.

[0101] When the type of the imported module in the code file to be processed is a third-party library module, write the import statement corresponding to the imported module into the preset third-party library module set.

[0102] Specifically, according to the preset loading environment feature words "import" or "from", and the parameters of the loading environment feature words, for each code file to be processed in the code file set to be processed, the steps to determine the type of imported module in the code file to be processed may include:

[0103] For each code file to be processed in the code file set to be processed, according to the loading environment feature words "import" or "from", find out the module import statements from the code file to be processed;

[0104] For each module import statement, obtain the parameters in the module import statement; where the parameters in the module import statement are the paths of the imported modules;

[0105] Judge 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 steps to calculate 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 the characters "." in the parameter with the string " / " to obtain the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected;

[0108] If the parameter in the import statement corresponding to the imported module starts with the string ".", 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 the characters after the rightmost character " / " in the current path to obtain a new current path; replace the leading string "." in the parameter with the new current path to obtain the path of the source code data of the imported module relative to 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 "..", count the number of the string ".." in the parameter, denoted 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 the characters after the character " / " at the delete number from the right in the current path to obtain a new current path; replace the first backtracking number of the string ".." from the left in the parameter with the new current path to obtain the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected.

[0110] Specifically, the steps of generating a new import statement for an imported module according to the source code data of the imported module and the path relative to the root directory corresponding to the code to be protected include:

[0111] Replace all occurrences of the string / in the path of the source code data of the imported module relative to the root directory corresponding to 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 imported module.

[0112] S105: Screen out the modules to be compiled from the local module dictionary according to the preset excluded file list;

[0113] Specifically, the steps of screening out the modules to be compiled from the local module dictionary according to the preset excluded file list may include:

[0114] Obtain the source code data of each local module and the path relative to the root directory corresponding to the code to be protected from the local module dictionary;

[0115] For each local module, obtain the file name of the local module from the path of the source code data of the local module relative to the root directory corresponding to the code to be protected;

[0116] Match the file name of the local module with the file names in the excluded file list; if the match fails, confirm that the local module is a module to be compiled.

[0117] The files in the excluded file list are files that do not need to be compiled into.so files. For example, files such as dbutils.py and etcdutils.py. Since not all the 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 improve the speed. Although the excluded files will not be compiled, they will still be normally packaged.

[0118] Of course, in some embodiments, it may also be that the local modules in the local module dictionary are not screened, but rather, all the local modules in the local module dictionary are directly compiled, and then the compilation program determines which modules need to be compiled and which modules do not need to be compiled. However, this method has a larger compilation workload and will reduce the speed of the algorithm.

[0119] .so files are files formed after converting Python language code into C language code through the programming language converter Cython on the Linux system and then compiling the C language code. On the Windows system, the file formed after converting Python language code into C language code through the programming language converter Cython and then compiling the C language code is a.pyd file. Cython is a language that combines the syntax of Python language and C language.

[0120] S106: Compile the module to be compiled to generate a dynamic link library file;

[0121] Under the Linux operating system, the process of compiling the module to be compiled is to convert the Python language program of the code to be protected into a C language program, and then immediately compile the converted C language program into a.so file. The.so file generated by compilation is the dynamic link library file.

[0122] S107: 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;

[0123] S108: Add import statements for importing the local import statement file and the third-party library import statement file in the entry file respectively to generate a new entry file;

[0124] For example, the local import statement file is local_import, and the third-party library import statement file is third_import. Since the pyinstaller packaging program cannot obtain the import information of the modules to be imported from the.so file after compiling the Python language program into a.so file, it is necessary to add two statements, import third_import and import local_import, at the top of the original entry file main.py. In this way, the packaging module (pyinstaller, which is a functional module in the third-party library of the Python language and is used to package files into executable programs) is informed of which modules need to be imported.

[0125] S109: Package the new entry file and the dynamic link library file to generate an executable code program.

[0126] Specifically, the packaging process is to package the new entry file obtained by adding two statements, import third_import and import local_import, at the top of the original entry file main.py and the.so file generated by compiling the Python language program into an executable program.

[0127] As can be seen from the embodiments shown Figure 1 , the method provided by the embodiments of the present invention can achieve obtaining the source code data of the code to be protected; obtaining all code files from the source code data as the set of code files to be processed; determining an entry file for the set of code files to be processed; starting from the entry file, classifying 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, obtaining a local module dictionary and a third-party library module set; screening out the modules to be compiled from the local module dictionary according to the preset exclusion file list; compiling the modules to be compiled to generate dynamic link library files; writing the import statements in the local module dictionary and the third-party library module set into the preset local import statement file and third-party library import statement file respectively; adding the import statements for importing the local import statement file and the third-party library import statement file into the entry file respectively to generate a new entry file; and performing packaging processing on the new entry file and the dynamic link library files to generate an executable code program. It can be seen that by implementing the method provided by the embodiments of the present invention, the code written in Python can be converted into C code, and then the C code can be compiled to generate dynamic link library files. Further, the new entry file and the dynamic link library files are packaged into an executable program, and the generated executable program is a binary program, thus greatly increasing the difficulty of cracking and decompiling the code written in Python. In summary, the method provided by the embodiments of the present invention can improve the security of the program code developed in Python more than the methods of encrypting the program code or modifying the starting address of the program code.

[0128] The following gives a detailed example to further elaborate on the method provided by the embodiments of the present invention. In this embodiment, the operating system is the Linux operating system, and the code to be protected is a code program written in Python.

[0129] The organization form of the Python program is as Figure 5 shown.

[0130] Referring to Figure 2 , the specific process of this embodiment is as follows:

[0131] S201: Obtain the source code data of the Python code to be protected;

[0132] S202: Obtain all Python files from the source code data as the set of Python files 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 for the Python language code to be protected.

[0135] S204: Use the root directory of the Python language code to be protected as the working path $proj_path, and use main.py as the entry file. For each Python file to be processed in the set of Python files to be processed, find the module import statements from the Python file to be processed according to the loading environment feature words import or from;

[0136] The Python language is a high-level programming language, and its strength lies in the ability to implement various functions by importing different modules. The methods for importing modules in the Python language are: import module path, or from path import module.

[0137] S205: For each module import statement, obtain the parameters in the module import statement;

[0138] Among them, the parameters in the module import statement are the paths of the imported modules.

[0139] S206: 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; 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 not, 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 embodiments of the present invention, all the source code data of the Python language code are placed in the app directory under the root directory of the Python language code. Therefore, 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 this module. The paths starting with the strings. and.. in the parameter of the module import statement are relative paths relative to the current Python file, so the modules corresponding to the paths must also be local modules. In addition, the modules corresponding to the paths in the module import statement are all third-party library modules.

[0141] Modules in the Python language are divided into standard modules, also known as local modules, and third-party library modules. Local modules in Python are modules that come with Python by default when it is installed. Third-party modules in Python need to be downloaded and installed in the Python installation directory. The installation and usage methods of different third-party modules are different, but their calling methods are the same, and they all need to be called using the import statement.

[0142] S207: Replace all characters '.' in the parameters of the module import statement with the string ' / ', and 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 the new current path $base_path; Replace the leading string '.' in the parameters of 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: 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 (i.e., the path $mod_path of the source code data of the imported module relative to $proj_path), it is necessary to first remove all characters after the last character ' / ' in C: / myprogram / python_project / app / mod1 / fun1.py (i.e., the file name fun1.py) to obtain the new current path $base_path: C: / myprogram / python_project / app / mod1 / , and then replace the leading string '.' in the parameter.aaa / bbb.py with the new current path $base_path, so as to obtain 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 strings '..' in the parameters of the module import statement, denoted as the backtracking count $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-th character from the right in $cur_path to obtain the new current path $base_path; Replace the first $back_count strings '..' from the left in the parameters of 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;

[0146] For example: If 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 parameter of this module import statement, that is, the backtracking count $back_count is 2. The obtained 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. Delete all characters after the $back_count-th character from the right in $cur_path, that is, delete all characters after the second character ' / ' from the right in $cur_path to obtain the new current path $base_path: C: / myprogram / python_project / app / . Replace the first $back_count, that is, the first 2 strings '..' from the left in the parameter....ccc / ddd.py of 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: C: / myprogram / python_project / app / ccc / ddd.py.

[0147] S210: Replace all strings ' / ' in the $mod_path with the string '.' in the $mod_path, delete the string '.py' at the end of the $mod_path, and add the string 'import' as a prefix to the $mod_path to generate the new import statement $local_import corresponding to the imported module;

[0148] S211: Using the $mod_path path as the key, and the new import statement $local_import corresponding to this $mod_path path as the value, write the $mod_path path and the new import statement $local_import corresponding to this $mod_path path into the local module dictionary local_dict in the form of key-value pairs;

[0149] S212: From the local module dictionary local_dict, obtain the path $mod_path of the source code data of a local module relative to $proj_path; Obtain all characters after the rightmost character / in this $mod_path path, denoted as $mod_name, and match this $mod_name with the file names in the exclusion file list; If the match fails, then confirm that this local module is the module to be compiled;

[0150] If the match is successful, then confirm that this 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 improve the algorithm speed. Although the excluded files will not be compiled, they will still be normally packaged.

[0151] S213: Determine whether the $mod_name in the $mod_path path of each local module in the local module dictionary local_dict has been matched with the file names in the exclusion file list; If so, execute step S214; If not, return and continue to execute step S212;

[0152] S214: Build 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 building an extension module according to the $mod_path path and $mod_name of the module to be compiled can be: Using the $mod_path path and $mod_name of the module to be compiled as the instantiation parameters of the Extension class to build an Extension object, that is, build an extension module; And save the built extension module distutils.extension to the extension module list extension_list. Among them, distutils is a tool module used to build extension modules in the Python language. distutils.extension is a built-in function in distutils. In this embodiment, it is called to pass parameters to its Extension class, and the passed parameters are all 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 to generate a.so file can be: call setup of distutils.core, execute cythonize, and pass extension_list as a parameter to start compilation. Among them, Distutils.core is a function in distutils (used to build extension modules in the Python language), and cythonize is a compiler in the programming language converter Cython.

[0157] .so file is a file formed after converting Python language code to C language code through the programming language converter Cython on the Linux system and then compiling the C language code. On the Windows system, the file formed after converting Python language code to C language code through the programming language converter Cython and then compiling the C language code is a.pyd file. The method provided in the embodiments 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 the forward slash / , and the path separator on the Windows system is the backslash \. When applying the method provided in the embodiments of the present invention to the Windows system, attention needs to be paid to the direction of the path separator, that is, the path separator in this embodiment needs to be replaced with the backslash \.

[0158] S216: Store the compiled.so file in the result directory in the working path;

[0159] Among them, 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 imported module into the third-party library module set $third_set;

[0162] S219: Write all the 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 to the local import statement file local_import.py;

[0164] S221: Add two import statements, import third_import and import local_import, at the top of the entry file main.py to generate a new entry file;

[0165] The import statements corresponding to the local modules form the local import statement file local_import; the import statements corresponding to the third-party library modules form the third-party library import statement file third_import. Since after compiling the Python language program into a.so file, the packaging program pyinstaller cannot obtain the import information of the modules to be imported from the.so file, therefore, two statements, import third_import and import local_import, need to be added at the top of the original entry file main.py to inform the packaging module which modules need to be imported in this way.

[0166] S222: Package the new entry file and the compiled.so file to generate an executable code program.

[0167] In a specific embodiment, the process of packaging the new entry file and the compiled.so file to generate an executable code program can be: execute the pyinstaller -F main.py statement to generate an executable program named main, where this executable program is located in the dist directory under the working path.

[0168] As can be seen from the Figure 2 embodiment shown, by implementing the method provided in the embodiments of the present invention, the code to be protected, that is, the code written in the Python language, can be converted into C language code, and then this C language code is compiled to generate a dynamic link library file. Further, the new entry file and the dynamic link library file are packaged into an executable program. And the generated executable program is a binary program, thus greatly improving the difficulty of cracking and decompiling the code written in the Python language. In summary, the method provided in the embodiments of the present invention can improve the security of the program code developed in the Python language more than the methods of encrypting the program code or modifying the starting address of the program code.

[0169] Corresponding to the Figure 1 embodiment shown, the embodiments of the present invention also provide a code protection device. As Figure 3As shown in the figure, 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. Among them,

[0170] The code acquisition module 301 is used to acquire the source code data of the code to be protected;

[0171] The file module 302 is used to acquire all code files from the source code data as the set of code files to be processed;

[0172] The determination module 303 is used to determine an entry file for the set of code files to be processed;

[0173] The classification module 304 is used to classify the import modules in each code file to be processed in the set of code files to be processed starting from the entry file 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; among them, the local module dictionary includes: the path of the source code data of each local module in the set of code files 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 set of code files to be processed;

[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 generation module 306 is used to compile the modules to be compiled to generate dynamic link library files;

[0176] The writing module 307 is used to 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;

[0177] The second generation module 308 is used to add the import statements for importing the local import statement file and the third-party library import statement file to the entry file respectively 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 used for:

[0180] Acquire 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 configured to:

[0183] Use 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 feature words "import" or "from", and the parameters of the loading environment feature words, for each code file to be processed in the code file set to be processed, determine the type of the imported module in the code file to be processed; wherein, the parameter of the loading environment feature word is the path of the imported module;

[0185] When the type of the imported module in the code file to be processed is a local module, calculate the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected; 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, generate a new import statement for the imported module;

[0186] Write the source code data of the imported 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;

[0187] When the type of the imported module in the code file to be processed is a third-party library module, write the import statement corresponding to the imported module into a preset third-party library module set.

[0188] Optionally, the classification module 304 is specifically configured to:

[0189] For each code file to be processed in the code file set to be processed, find out the module import statement from the code file to be processed according to the loading environment feature words "import" or "from";

[0190] For each module import statement, obtain the parameter in the module import statement; wherein, the parameter in the module import statement is the path of the imported module;

[0191] Judge 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 configured to:

[0193] When the parameter in the import statement corresponding to the imported module starts with the string "app", replace all the characters "." in the parameter with the string " / " to obtain the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected;

[0194] When the parameter in the import statement corresponding to the imported module starts with the string '.', obtain the source code data of the imported module, which is the current path relative to 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 leading string '.' in the parameter with the new current path to obtain the path of the source code data of the imported module relative to 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 '..', count the number of the string '..' in the parameter, which is recorded as the backtracking number; obtain the source code data of the imported module, which is the current path relative to the root directory corresponding to the code to be protected; delete all characters after the character ' / ' at the delete number counted from the right in the current path to obtain the new current path; replace the first backtracking number of the string '..' from the left in the parameter with the new current path to obtain the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected.

[0196] Optionally, the classification module 304 is specifically used for:

[0197] Replace all strings ' / ' in the path of the source code data of the imported module relative to the root directory corresponding to 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 imported module.

[0198] Optionally, the screening module 305 is specifically used for:

[0199] Obtain the source code data of each local module from the local module dictionary, which is the path relative to 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 path of the source code data of the local module relative to the root directory corresponding to the code to be protected;

[0201] Match the file name of the local module with the file names in the exclusion file list; if the match fails, confirm that the local module is the module to be compiled.

[0202] It can be seen that by implementing the device provided by the embodiments of the present invention, the code written in the Python language, that is, the code to be protected, can be converted into C language code, and then the C language code can be compiled to generate a dynamic link library file. Further, the new entry file and the dynamic link library file can be packaged into an executable program. The packaged executable program is a binary program, thus greatly improving the difficulty of cracking and decompiling the code written in the Python language. In summary, the device provided by the embodiments of the present invention can improve the security of the program code developed in the Python language more than the prior art.

[0203] corresponding to the embodiment shown, an embodiment of the present invention further provides an electronic device. Refer to Figure 1 , including a processor 401, a communication interface 402, a memory 403, and a communication bus 404. Among them, the processor 401, the communication interface 402, and the memory 403 complete mutual communication through the communication bus 404; Figure 4

[0204] The memory 403 is used to store a computer program;

[0205] When the processor 401 is used to execute the program stored on the memory, it implements any of the method steps of code protection described in the above embodiments.

[0206] It can be seen that by implementing the electronic device provided by the embodiment of the present invention, the code to be protected, that is, the code written in the 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. Further, the new entry file and the dynamic link library file are packaged into an executable program, and the packaged executable program is a binary program. Therefore, the difficulty of cracking and decompiling the code written in the 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 the Python language more than the prior art.

[0207] Figure 1 corresponding to the embodiment shown, an embodiment of the present invention further provides a computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, it implements any of the method steps of code protection described in the above embodiments.

[0208]

[0209] It can be seen that by implementing the storage medium provided by the embodiment of the present invention, the code to be protected, that is, the code written in the 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. Further, the new entry file and the dynamic link library file are packaged into an executable program, and the packaged executable program is a binary program. Therefore, the difficulty of cracking and decompiling the code written in the Python language is greatly improved. In summary, the storage medium provided by the embodiment of the present invention can improve the security of the program code developed in the Python language more than the prior art.

[0209] Those skilled in the art should understand that the embodiments of the present invention can be provided as a method, a system, or a computer program product. Therefore, the present invention can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present invention can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk memories and optical memories, etc.) containing computer-usable program code.

[0210] The present invention is described with reference to the flowcharts and / or block diagrams of methods, apparatuses (systems), and computer program products according to embodiments of the present invention. It should be understood that each flow and / or block in the flowchart and / or block diagram, as well as the combination of flows and / or blocks in the flowchart and / or block diagram, can be realized by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing devices to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing devices generate means for realizing the functions specified in one Figure 1 flow or multiple flows and / or blocks Figure 1 block or multiple blocks.

[0211] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer-readable memory generate a manufactured article including instruction means, and the instruction means realizes the functions specified in one Figure 1 flow or multiple flows and / or blocks Figure 1 block or multiple blocks.

[0212] These computer program instructions can also be loaded onto a computer or other programmable data processing device, so that a series of operation steps are executed on the computer or other programmable device to generate a computer-implemented process, and thus the instructions executed on the computer or other programmable device provide steps for realizing the functions specified in one Figure 1 flow or multiple flows and / or blocks Figure 1 block or multiple 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 equivalent technologies, the present invention is also intended to include these changes and modifications.

Claims

1. A code protection method, characterized in that, the method includes: Obtain the source code data of the code to be protected; Obtain all code files from the source code data as the set of code files to be processed; Determine an entry file for the set of code files to be processed; Taking 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; wherein, the local module dictionary includes: the path of the source code data of each local module in the set of code files 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 set of code files to be processed; Screen 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 dynamic link library files; Write the import statements in the local module dictionary and the third-party library module set into the preset local import statement file and third-party library import statement file respectively; Add import statements for importing the local import statement file and the third-party library import statement file in the entry file respectively to generate a new entry file; Package the new entry file and the dynamic link library file to generate an executable code program; Taking the root directory corresponding to the code to be protected as the working path and the entry file as the starting point; According to the preset loading environment feature word "import" or "from", and the parameters of the loading environment feature word, determine the type of the import module in each code file to be processed in the set of code files to be processed; wherein, the parameter of the loading environment feature 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, calculate the path of the source code data of the import module relative to the root directory corresponding to the code to be protected; generate 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 path of the source code data of the import module relative to the root directory corresponding to the code to be protected and the new import statement into the 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, write the import statement corresponding to the import module into the preset third-party library module set; For each code file to be processed in the set of code files to be processed, find out the module import statement from the code file to be processed according to the loading environment feature word "import" or "from"; For each module import statement, obtain the parameter in the module import statement; wherein, the parameter in the module import statement is the path of the import module; Determine whether the parameter in the module import statement starts with the string "app" or the character "." 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.

2. The method according to claim 1, wherein, the step of determining an entry file for the set of code files to be processed includes: obtain the 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, wherein, 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 imported module starts with the string "app", then replace all the characters "." in the parameter with the character " / ", and obtain the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected; If the parameter in the import statement corresponding to the imported module starts with the character ".", then obtain the current path of the source code data of the imported module relative to the root directory corresponding to the code to be protected; delete all the characters after the rightmost character " / " in the current path to obtain a new current path; replace the starting character "." in the parameter with the new current path to obtain the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected; If the parameter in the import statement corresponding to the imported module starts with the string "..", then count the number of the string ".." in the parameter, denoted as the backtracking number; obtain the current path of the source code data of the imported module relative to the root directory corresponding to the code to be protected; delete all the characters after the character " / " at the backtracking number position from the right in the current path to obtain a new current path; replace the first backtracking number of strings ".." from the left in the parameter with the new current path to obtain the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected.

4. The method according to claim 3, wherein, the step of generating a new import statement for the imported module 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 includes: Replace all the characters " / " in the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected with the character ".", delete the string ".py" in the path, and add the string "import" as a prefix to generate the new import statement of the imported module.

5. The method according to claim 1, wherein, the step of screening out the modules to be compiled from the local module dictionary according to the preset exclusion file list includes: obtain the path of the source code data of each local module relative to the root directory corresponding to the code to be protected from the local module dictionary; For each local module, obtain the file name of the local module from the path of the source code data of the local module relative to the root directory corresponding to the code to be protected; Match the file name of the local module with the file names in the exclusion file list; if the match fails, confirm that the local module is a module to be compiled.

6. A code protection device, characterized in that, the device includes: a code acquisition module for acquiring the source code data of the code to be protected; a file module for obtaining all code files from the source code data as a set of code files to be processed; a determination module for determining an entry file for the set of code files to be processed; a classification module for classifying the imported modules in each code file to be processed in the set of code files to be processed starting from the entry file according to 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; wherein, the local module dictionary includes: the path of the source code data of each local module in the set of code files 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 statements corresponding to each third-party library module in the set of code files to be processed; a screening module for screening out modules to be compiled from the local module dictionary according to a preset exclusion file list; a first generation module for compiling the modules to be compiled to generate dynamic link library files; a writing module for 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 third-party library import statement file respectively; a second generation module for respectively adding import statements for importing the local import statement file and the third-party library import statement file in the entry file to generate a new entry file; a packaging module for performing a packaging process on the new entry file and the dynamic link library files to generate an executable code program; The step of classifying the imported modules in each code file to be processed in the set of code files to be processed starting from the entry file according to 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 includes: using the root directory corresponding to the code to be protected as the working path and the entry file as the starting point; According to the preset loading environment feature word "import" or "from", and the parameter of the loading environment feature word, for each code file to be processed in the set of code files to be processed, determine the type of the imported module in the code file to be processed; wherein, the parameter of the loading environment feature word is the path of the imported module; When the type of the imported module in the code file to be processed is a local module, calculate the path of the source code data of the imported module relative to the root directory corresponding to the code to be protected; generate a new import statement for the imported module 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. Write the source code data of the imported 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 imported module in the code file to be processed is a third-party library module, write the import statement corresponding to the imported module into a preset third-party library module set. The step of determining the type of the imported module in the code file to be processed according to a preset loading environment feature word "import" or "from" and the parameter of the loading environment feature word includes: For each code file to be processed in the code file set to be processed, find out the module import statement from the code file to be processed according to the loading environment feature word "import" or "from". For each module import statement, obtain the parameter in the module import statement; wherein, the parameter in the module import statement is the path of the imported module. Judge whether the parameter in the module import statement starts with the string "app" or the character "." 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.

7. An electronic device Characterized in that It includes a processor, a communication interface, a memory and a communication bus. Among them, the processor, the communication interface, and the memory complete mutual communication through the communication bus. The memory is used to store a computer program. When the processor is used to execute the program stored on the memory, it implements the method steps of any one of claims 1-5.

8. 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, it implements the method steps of any one of claims 1-5.

Citation Information

Patent Citations

  • Multi-process concurrence difference calculation server system and implementation method thereof

    CN106095601A

  • Python code decompiling method and system and readable storage medium

    CN114721668A