Code protection method and device, electronic equipment and storage medium

By inserting random global variables generated by the operating system into the code as protection variables, protection code is generated, which solves the problem that existing code protection methods cannot be cross-platform compatible and affect the running logic, thus improving security and compatibility.

CN116628697BActive Publication Date: 2025-11-04TONGDUN NETWORK TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202310062871.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-01-19
Publication Date
2025-11-04
Estimated Expiration
2043-01-19

AI Technical Summary

Technical Problem

Existing code protection methods struggle to improve security without affecting code execution logic and cannot achieve cross-platform protection.

Method used

By creating multiple operating system-generated random global variables as protection variables, protection code is generated and inserted into the original code. This ensures that the original code and the protection code are independent of each other at runtime, and the compiler generates a cross-platform compatible executable file.

Benefits of technology

It improves code security, reduces the difficulty of cracking, and achieves cross-platform protection without affecting the code's execution logic.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116628697B_ABST
    Figure CN116628697B_ABST
Patent Text Reader

Abstract

The present disclosure relates to the technical field of computer, and specifically provides a code protection method and device, electronic equipment and storage medium. The code protection method comprises: creating a plurality of protection variables; wherein the protection variable is a global variable, and the value of the protection variable is a random number value randomly generated by an operating system; generating a protection code according to the protection variable; wherein the protection code is used for protecting an original code, and the protection code contains the protection variable; inserting the protection code into the original code; wherein the original code contains an original variable, and the original variable and the protection variable are different variables; compiling the protection code and the original code to obtain an executable file; wherein the executable file is a program that can be directly run by a computer, and in the process of running the executable file, the compilation logic of the original code and the protection code is independent of each other. The present disclosure provides a code protection method, which solves the problem of low logic security of the original code and easy cracking.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present disclosure relates to the technical field of computer, and in particular, to a code protection method, a code protection device, an electronic device and a storage medium. BACKGROUND

[0002] In recent years, with the development of hacking technology, there are security risks in each link of the design, release and use of software. After stealing the code, the attacker can directly obtain the original logic of the code. Even if the code is compiled into an executable file, the attacker can also obtain the code logic of the original code through decompilation and other means. Therefore, protecting the code security of enterprises and users is an important research direction in the industry.

[0003] In related technologies, code protection methods are generally divided into three types. One is executable file shell, that is, a new execution file is built, and the execution file of the code is compressed and placed at the tail of the new execution file. Another is executable file virtualization, that is, the execution file of the code is translated into virtual instructions that can only be read by a virtual machine through machine instructions. The third is code obfuscation, that is, the code is converted to be functionally equivalent, but the protected code obtained by this method is difficult to read and understand.

[0004] In actual application, the executable file shell method is easy to be exhausted and brute forced, and the protection is weak. The executable file virtualization can only be executed by specific commercial software, which is extremely high in cost and has no compatibility. The code obfuscation may affect the code and bring inconvenience to the coder. At the same time, most of the existing code obfuscation technologies can only be implemented on a single specific platform, and cannot realize cross-platform code protection. How to improve the security of the code while not affecting the running logic of the code and realize cross-platform code protection is a problem faced by those skilled in the art.

[0005] It should be noted that the information disclosed in the above background section is only used to strengthen the understanding of the background of the present disclosure, and therefore can include information that does not constitute prior art known to those of ordinary skill in the art. SUMMARY

[0006] The purpose of the present disclosure is to provide a code protection method, a code protection device, an electronic device and a storage medium, which can improve the security of the code while not affecting the running logic of the code and realize cross-platform code protection.

[0007] According to one aspect of the present disclosure, a code method is provided, which is applied to a compiler end, comprising: creating a plurality of protection variables; wherein the protection variables are global variables, the variable names of the protection variables are strings generated by an operating system, and the values of the protection variables are random values randomly generated by the operating system; generating protection codes according to the protection variables; wherein the protection codes are used to protect original codes, and the protection codes contain the protection variables; inserting the protection codes into the original codes; wherein the original codes contain original variables, and the original variables are different from the protection variables; compiling the protection codes and the original codes to obtain an executable file; wherein the executable file is a program that can be directly run by a computer, and the compiling logics of the original codes and the protection codes are independent of each other in the process of running the executable file.

[0008] In one exemplary embodiment of the present disclosure, the protection codes are generated according to the protection variables, and the method further comprises: defining an empty function; wherein the function name of the empty function is a randomly generated string; adding an operation function to the empty function to obtain a protection function; wherein the operation function is a function of operating the protection variables; constructing a function call instruction through the function name of the protection function; wherein the function call instruction is an instruction of calling the protection function; and taking the code corresponding to the function call instruction as the protection code.

[0009] In one exemplary embodiment of the present disclosure, the operation function comprises: assigning the value of a first variable to a second variable; wherein the first variable is one of the plurality of protection variables, the second variable is one of the plurality of protection variables, and the first variable is different from the second variable.

[0010] In one exemplary embodiment of the present disclosure, the operation function comprises: performing an operation operation on a first variable and a second variable to obtain an operation result; wherein the first variable is one of the plurality of protection variables, the second variable is one of the plurality of protection variables, the first variable is different from the second variable, and the operation operation can be one or more of the four arithmetic operations, logical operations and relational operations; and assigning the operation result to the first variable.

[0011] In one exemplary embodiment of the present disclosure, the protection codes are generated according to the protection variables, and the method further comprises: determining an operation rule; wherein the operation rule comprises one or more of the four arithmetic operations, logical operations and relational operations; constructing a variable operation instruction according to the operation rule and the protection variables; wherein the variable operation instruction is used to perform an operation operation on the protection variables; and taking the code corresponding to the variable operation instruction as the protection code.

[0012] In one example embodiment of the present disclosure, the protection code is generated according to the protection variable, and the method further comprises: constructing a first operation instruction and a second operation instruction according to a preset inequality; wherein, the inequality of the preset inequality is a less than or equal to sign, the first operation instruction is an operation instruction corresponding to the left side of the preset inequality, and the second operation instruction is an operation instruction corresponding to the right side of the preset inequality; substituting the protection variable into the first operation instruction to obtain a first operation result; substituting the protection variable into the second operation instruction to obtain a second operation result; constructing a conditional judgment instruction; wherein, the conditional judgment instruction is used to judge the size relationship between the first operation result and the second operation result; constructing a jump instruction to change the running logic of the original code when it is judged that the first operation result is greater than the second operation result; executing the running logic of the original code when it is judged that the first operation result is less than or equal to the second operation result; and taking the code corresponding to the first operation instruction, the code corresponding to the second operation instruction, the code corresponding to the conditional judgment instruction, and the code corresponding to the jump instruction as the protection code.

[0013] In one example embodiment of the present disclosure, the protection code is inserted into the original code, and the method further comprises: traversing the original code to obtain a protection position in the original code; wherein, the protection position is a position that does not change the running logic of the original code after the protection code is inserted; recording the number of function call instructions; and inserting the protection code into the protection position when the number of function call instructions is greater than a preset value.

[0014] In one example embodiment of the present disclosure, after the protection code is generated according to the protection variable, the method further comprises: converting the protection code into a preset format; wherein, the preset format is a format that can be directly read by the end of the compiler.

[0015] According to one aspect of the present disclosure, a code protection device is provided, comprising a protection variable creation module for creating a plurality of protection variables; wherein, the protection variable is a global variable, the variable name of the protection variable is a string generated by an operating system, and the value of the protection variable is a random number value randomly generated by the operating system; a protection code generation module for generating a protection code according to the protection variable; wherein, the protection code is used to protect an original code, and the protection code contains the protection variable; a protection code insertion module for inserting the protection code into the original code; wherein, the original code contains an original variable, and the original variable and the protection variable are different variables; and a code running module for running the protection code and the original code to obtain an executable file; wherein, the executable file is a program that can be directly run by a computer, and the original code and the protection code are independent of each other in the process of running the executable file.

[0016] According to one aspect of the present disclosure, an electronic device is provided, comprising:

[0017] a processor; and a memory for storing executable instructions of the processor; wherein the processor is configured to execute the code protection method of any one of the above embodiments via execution of the executable instructions.

[0018] According to an aspect of the present disclosure, there is provided a computer-readable storage medium storing instructions that, when executed by a processor of an electronic device, cause the electronic device to perform the code protection method of any one of the above embodiments.

[0019] The exemplary embodiments of the present disclosure can have the following partial or all beneficial effects:

[0020] In the code protection method provided by the exemplary embodiments of the present disclosure, a plurality of protection variables are created, a protection code is generated according to the protection variables, the protection code is inserted into the original code, and an executable file is obtained by running the protection code and the original code. On the one hand, by inserting the protection code into the original code, the readability of the original code is reduced, the cracking difficulty of the attacker is increased, and the security of the original code is improved. On the other hand, the protection variables are independent of the variables in the original code, and inserting the protection code related to the protection variables does not change the running logic of the original code, thereby reducing the impact on the coder. On the other hand, the code protection method provided by the exemplary embodiments of the present disclosure is applied to the middle end of the compiler, and the protection code and the original code are converted into an intermediate language independent of the architecture platform by the middle end of the compiler, which can be used on any platform, thereby realizing cross-platform protection code, improving the security of the code without affecting the running logic of the code, and realizing cross-platform protection code.

[0021] It should be understood that the foregoing general description and the following detailed description are only exemplary and explanatory, and cannot limit the present disclosure. BRIEF DESCRIPTION OF DRAWINGS

[0022] The accompanying drawings, which are incorporated into and form part of the specification, illustrate one embodiment consistent with the present disclosure and, together with the description, serve to explain the principles of the disclosure. Obviously, the drawings in the following description are only some embodiments of the present disclosure, and other drawings can be obtained by those skilled in the art without creative labor on the basis of these drawings.

[0023] Figure 1 A schematic diagram of the system architecture of the code protection method according to an embodiment of the present disclosure is shown schematically.

[0024] Figure 2 A flowchart of the code protection method according to an embodiment of the present disclosure is shown schematically.

[0025] Figure 3A flowchart of a method of generating function call code according to a protection variable is shown schematically in accordance with one embodiment of the present disclosure.

[0026] Figure 4 A flowchart of a method of adding an operation function to an empty function is shown schematically in accordance with one embodiment of the present disclosure.

[0027] Figure 5 A flowchart of a method of generating variable operation code according to a protection variable is shown schematically in accordance with one embodiment of the present disclosure.

[0028] Figure 6 A flowchart of a method of generating conditional judgment and jump code according to a protection variable is shown schematically in accordance with one embodiment of the present disclosure.

[0029] Figure 7 A flowchart of a method of inserting protection code into original code is shown schematically in accordance with one embodiment of the present disclosure.

[0030] Figure 8 A block diagram of a code protection apparatus in accordance with one embodiment of the present disclosure is shown schematically.

[0031] Figure 9 A structural schematic diagram of a computer system of an electronic device suitable for implementing embodiments of the present disclosure is shown. DETAILED DESCRIPTION

[0032] Example implementations will now be described more fully with reference to the accompanying drawings. Example implementations can be implemented in any

[0033] The computer program product of the present disclosure can be a computer program embodied on a non-transitory computer readable medium. When the program runs on a computer, the process executed by the computer can be as shown in flow charts and / or block diagrams in the present disclosure. In this regard, each block in the flow charts and / or block diagrams can represent a module, a procedure, or a part of code which comprises one or more executable instructions for implementing the specific logical functions or operations specified for the block. It should also be noted that in some alternative implementations, the functions shown in the blocks can occur in a different order than that which is depicted in the flow charts and / or block diagrams. For example, two blocks shown in succession can in fact be executed substantially concurrently or in the reverse order that is shown. This depiction in no way implies that the described functions are to be executed by separate hardware or software modules. It will also be appreciated that each of the blocks can be implemented by hardware, software, or any combination of the two.

[0034] The units described in the embodiments of the present disclosure can be implemented by software, or by hardware, or combinations of the two. The units described can also be located in a single processor. In some cases, the names of the units do not limit the units themselves.

[0035] Figure 1 A schematic diagram of a system architecture of an exemplary application environment in which a code protection method and apparatus according to embodiments of the present disclosure can be applied is shown.

[0036] As shown in Figure 1 The system architecture 100 can include one or more of terminal devices 101, 102, 103, a network 104, and a server 105. The network 104 serves as a medium to provide communication links between the terminal devices 101, 102, 103 and the server 105. The network 104 can include various connection types, such as wired, wireless communication links, or fiber optic cables, etc. The terminal devices 101, 102, 103 can be various electronic devices with display screens, including but not limited to desktop computers, portable computers, smart phones, and tablet computers, etc. It should be understood that Figure 1 The number of terminal devices, networks, and servers in the system architecture 100 is merely illustrative. Depending on the implementation needs, there can be any number of terminal devices, networks, and servers. For example, the server 105 can be a server cluster composed of multiple servers.

[0037] The code protection method provided in the embodiments of the present disclosure can be executed by the server 105. Specifically, a plurality of protection variables are created, protection code is generated according to the protection variables, the protection code is inserted into the original code, and the protection code and the original code are compiled to obtain an executable file. The code protection method provided in the embodiments of the present disclosure can also be executed by the terminal device 101, 102 or 103. The code protection method provided in the embodiments of the present disclosure can also be executed by the terminal device 101, 102 or 103 and the server 105 together. This is not specially limited in the example embodiment.

[0038] With the development of the information age, the software industry has become an indispensable part of the social economy, and its security problem has become increasingly prominent. Criminals mainly use software piracy, tampering and reverse analysis and other means to attack and steal the code logic of various software, so that code protection has gradually developed into an important part of computer security.

[0039] Code obfuscation is a technique of transforming the flow structure and data relationship of code, which does not change the syntax and semantic rules of the original code. The main purpose of the first proposed code obfuscation technology is to protect the security of software code. Since programs of various languages can be easily decompiled or attacked by malicious reverse engineering, by obfuscating the program, the program source code becomes more difficult to understand and is not easy to be cracked by a decompilation tool, so as to achieve the purpose of not stealing intellectual property rights and confidential information.

[0040] In an example embodiment of the present disclosure, a code protection method is provided, which is applied to the end of a compiler. As shown in FIG. 1, the code protection method can include the following steps: Figure 2

[0041] In step S210, a plurality of protection variables are created. The protection variable is a global variable, the variable name of the protection variable is a string generated by an operating system, and the value of the protection variable is a random number value randomly generated by the operating system.

[0042] In step S220, protection code is generated according to the protection variable. The protection code is used to protect the original code, and the protection code contains the protection variable.

[0043] In step S230, the protection code is inserted into the original code. The original code contains an original variable, and the original variable and the protection variable are different variables.

[0044] In step S230, the protection code and the original code are compiled to obtain an executable file. The executable file is a program that can be directly run by a computer. In the process of running the executable file, the compilation logic of the original code and the protection code is independent of each other.

[0045] The above steps will be described in more detail below.​

[0046] In an example embodiment of the present disclosure, a code protection method is provided, referring to Figure 2 As shown in the figure, the code protection method comprises the following steps S210-S240:

[0047] In step S210, a plurality of protection variables are created.

[0048] In an example embodiment of the present disclosure, a plurality of protection variables are created. The protection variables are global variables, the variable names of the protection variables are strings generated by the operating system, and the values of the protection variables are random numbers generated by the operating system. For example, the plurality of protection variables can be global variables X, Y, and Z, and the values of the respective protection variables are random numbers generated by the operating system.

[0049] Specifically, a plurality of random numbers are obtained by a random number generator of the operating system and stored in the memory, and a plurality of global variable names are randomly defined by the operating system, and the plurality of random numbers in the memory are randomly assigned to the plurality of variable names to obtain a plurality of random global variables as protection variables.

[0050] For example, the protection variables are global variables X, Y, and Z, a plurality of random numbers such as 1, 2, and 3 are obtained by a random number generator of the operating system and stored in the memory, and a plurality of global variable names such as X, Y, and Z are randomly defined by the operating system, and the plurality of random numbers in the memory are randomly assigned to the plurality of global variable names to obtain a plurality of protection variables.

[0051] In step S220, a protection code is generated according to the protection variables.

[0052] In an example embodiment of the present disclosure, a protection code is generated according to the protection variables. The protection code is used to protect the original code, and the protection code contains the protection variables. The original code is a code that has been converted into an intermediate language by a compiler front end and can be directly read by a compiler. The intermediate language is a language with a complexity between high-level language and machine, and each compiler has a specific format of intermediate language.

[0053] Specifically, a plurality of variable operation instructions related only to the protection variables and not affecting the original code are created according to the protection variables. These operation instructions only participate in the calculation related to the protection variables and are not related to the variables in the original code, avoiding interference with the running logic of the original code. The code related to the above operation instructions is taken as the protection code.

[0054] In step S230, the protection code is inserted into the original code.

[0055] In an example embodiment of the present disclosure, the protection code is inserted into the original code. In the example, the original code includes original variables, and the original variables are different from the protection variables. For example, the original variables can be a, b, and g, and the protection variables can be X, Y, and Z.

[0056] Specifically, after obtaining the protection code, a position for inserting the protection code in the original code is determined, and the protection code is inserted into the original code at the corresponding position, so as to reduce the readability of the original code.

[0057] For example, the original code is a code including a plurality of functions for calculating original variables, and the protection code is a code including a plurality of functions for calculating protection variables. After obtaining the protection code, a position in the original code is searched, at which the protection code is inserted without affecting the running logic of the original code. Since the functions in the protection code are all used for calculating the protection variables, the original variables in the original code are not affected, and the running logic of the original code is not affected.

[0058] In step S240, the protection code and the original code are compiled to obtain an executable file.

[0059] In an example embodiment of the present disclosure, the protection code and the original code are compiled to obtain an executable file. In the example, the executable file is a program that can be directly run by a computer, and the compilation logic of the original code and the protection code is independent of each other in the process of running the executable file. For example, the executable file can be an exe (a file format) file.

[0060] Specifically, after the protection code is inserted into the original code, the mixed code including the original code and the protection code is compiled by a back end of a compiler to obtain an executable file. Since the variables in the protection code do not participate in the related calculation process of the variables in the original code, the compilation logic of the original code and the protection code is independent of each other, that is, the running result of the protection code does not affect the running result of the original code. The function of the executable file is consistent with the function of the executable file obtained by running the original code alone.

[0061] For example, the executable file can be an exe file. The original code and the protection code are compiled into a plurality of obj (a file format) files by a compiler, and the plurality of obj files are connected into an exe file by a linker.

[0062] In an example embodiment of the present disclosure, an empty function is defined, an operation function is added to the empty function to obtain a protection function, a function call instruction is constructed by using the function name of the protection function, and the code corresponding to the function call instruction is used as the protection code. For details, refer to Figure 3As shown, according to the function call code generated according to the protection variable, the following steps S310-S340 can be included:

[0063] In step S310, an empty function is defined.

[0064] In an example embodiment of the present disclosure, an empty function is defined. Wherein, the function name of the empty function is a randomly generated string. For example, the function name of the empty function can be protect.

[0065] Specifically, through the interface at the end of the compiler, a plurality of empty functions are dynamically created, and the empty functions are randomly assigned function names, which are randomly generated strings.

[0066] For example, the function name of the empty function is protect, that is, through the interface at the end of the compiler, an empty function with the function name protect is defined.

[0067] In step S320, an operation function is added to the empty function to obtain a protection function.

[0068] In an example embodiment of the present disclosure, an operation function is added to the empty function to obtain a protection function. Wherein, the operation function is a function of operating on the protection variable. For example, the operation function can be one or more of the four arithmetic operations, logical operations or relational operations.

[0069] Specifically, an operation function for operating on the protection variable is added to the empty function.

[0070] For example, the function name of the empty function is protect, the protection variable is the global variable X, Y and Z, and the operation function is the addition assignment operation. Specifically, the operation function X=X+Y+Z is added to the protect function.

[0071] In step S330, a function call instruction is constructed through the function name of the protection function.

[0072] In an example embodiment of the present disclosure, a function call instruction is constructed through the function name of the protection function. Wherein, the function call instruction is an instruction for calling the protection function. For example, the function call instruction can be protect().

[0073] Specifically, through the operating system and the compiler, an instruction for calling the protect function is generated according to the function name of the protect.

[0074] For example, through the operating system and the compiler, an instruction for calling the protect function protect() is generated according to the function name of the protect.

[0075] In step S340, the code corresponding to the function call instruction is taken as the protection code.

[0076] Through the above steps S310-S340, the empty function is defined, the operation function is added to the empty function to obtain the protection function, the function call instruction is constructed through the function name of the protection function, and the code corresponding to the function call instruction is taken as the protection code. The code containing the randomly generated variable and function is meaningless and cannot be understood, which increases the cracking difficulty of the attacker and improves the protection strength of the protection code to the original code.

[0077] In an example embodiment of the present disclosure, the value of the first variable is assigned to the second variable. Among them, the first variable is one of the plurality of protection variables, the second variable is one of the plurality of protection variables, and the first variable and the second variable are different variables. For example, the protection variables can be global variables X, Y, and Z, the first variable can be a global variable X, and the second variable can be any protection variable other than X.

[0078] For example, the protection variables are global variables X, Y, and Z, the first variable is a global variable X, and the second variable is a global variable Y, wherein the value of X is 1 and the value of Y is 2. The value of the first variable is assigned to the second variable, that is, X=Y, X=2.

[0079] Through the above steps, the operation function of the protection function is enriched, and the protection strength of the protection code is further increased.

[0080] In an example embodiment of the present disclosure, an operation is performed on the first variable and the second variable to obtain an operation result, and the operation result is assigned to the first variable. Specifically, referring to FIG. 4, the operation function is added to the empty function, which can include the following steps S410-S420: Figure 4

[0081] In step S410, an operation is performed on the first variable and the second variable to obtain an operation result.

[0082] In an example embodiment of the present disclosure, an operation is performed on the first variable and the second variable to obtain an operation result. Among them, the first variable is one of the plurality of protection variables, the second variable is one of the plurality of protection variables, and the first variable and the second variable are different variables. The operation can be one or more of the four arithmetic operations, logical operations, and relational operations. For example, the protection variables can be global variables X, Y, and Z, the first variable can be a global variable X, and the second variable can be any protection variable other than X, and the operation result can be a defined intermediate variable temp.

[0083] ​For example, the protected variables are global variables X, Y and Z, the first variable is the global variable X, the second variable is the global variable Y, the value of X is 1, the value of Y is 2, the operation operation is addition operation in the four arithmetic operations, and the operation operation is performed on the first variable and the second variable to obtain the operation result, that is, X+Y=3, and 3 is the operation result.

[0084] In step S420, the operation result is assigned to the first variable.

[0085] In an example embodiment of the present disclosure, the operation result is assigned to the first variable. Wherein the operation result is the result of performing the operation operation on the first variable and the second variable. For example, the first variable is the global variable X, the second variable is the global variable Y, the operation operation is the addition operation X+Y in the four arithmetic operations, the operation result is 3, and the operation result is assigned to the first variable, at which time X=3.

[0086] It can be understood that the operation operation is not only the simple operation in the above example, but also multiple invocations of the four arithmetic operations, logical operations and relational operations to form a complex operation.

[0087] Through the above steps S410-S420, the operation operation is performed on the first variable and the second variable to obtain the operation result, and the operation result is assigned to the first variable. The operation function of the protection function is enriched, and the protection strength of the protection code is further increased.

[0088] In an example embodiment of the present disclosure, the operation rule is determined, the variable operation instruction is constructed according to the operation rule and the protected variable, and the code corresponding to the variable operation instruction is taken as the protection code. Specifically, referring to Figure 5 As shown, the variable operation code is generated according to the protected variable, which can include the following steps S510-S530:

[0089] In step S510, the operation rule is determined.

[0090] In an example embodiment of the present disclosure, the operation rule is determined. Wherein the operation rule includes one or more of the four arithmetic operations, logical operations and relational operations.

[0091] Specifically, one or more operation rules are randomly selected from the four arithmetic operations, logical operations and relational operations.

[0092] In step S520, the variable operation instruction is constructed according to the operation rule and the protected variable.

[0093] In an example embodiment of the present disclosure, a variable operation instruction is constructed according to an operation rule and a protection variable. The variable operation instruction is used to perform an operation on the protection variable. For example, the operation can be a four-operation, a logic operation, or a relation operation, or a mixed operation of multiple operation types.

[0094] Specifically, the compiler and the operating system generate a variable operation instruction for the protection variable according to the operation rule and the protection variable.

[0095] In step S530, the code corresponding to the variable operation instruction is used as the protection code.

[0096] In an example embodiment of the present disclosure, the code corresponding to the variable operation instruction is used as the protection code. Specifically, all the codes related to the variable operation instruction are used as the protection code.

[0097] For example, the protection variables are global variables X, Y, and Z, the value of X is 1, the value of Y is 2, and the value of Z is 3. The operation rule is to assign the result of X+Y-Z to X, i.e., X=X+Y-Z. Specifically, the operation rule is determined from the four operations, the logic operation, and the relation operation, which is to first sum, then subtract, and finally assign. The protection variables X, Y, and Z are obtained, and the variable operation instruction X=X+Y-Z is obtained according to the protection variables and the operation rule. All the codes related to the variable operation instruction are used as the protection code.

[0098] Through the above steps S510-S530, the operation rule is determined, the variable operation instruction is constructed according to the operation rule and the protection variable, and the code corresponding to the variable operation instruction is used as the protection code. Multiple complex variable operation instructions can be randomly generated, which are meaningless and difficult to understand, increasing the difficulty of cracking for attackers. As the protection code, the protection code can further improve the protection of the original code.

[0099] In an example embodiment of the present disclosure, a first operation instruction and a second operation instruction are constructed according to a preset inequality, a first operation result is obtained by substituting the protection variable into the first operation instruction, a second operation result is obtained by substituting the protection variable into the second operation instruction, a condition judgment instruction is constructed, a jump instruction is constructed to change the running logic of the original code when the first operation result is greater than the second operation result, the running logic of the original code is executed when the first operation result is less than or equal to the second operation result, and the code corresponding to the first operation instruction, the code corresponding to the second operation instruction, the code corresponding to the condition judgment instruction, and the code corresponding to the jump instruction are used as the protection code. Specifically, refer to Figure 6 As shown in FIG. 6, the condition judgment and jump code is generated according to the protection variable, which can include the following steps S610-S670:

[0100] In step S610, the first operation instruction and the second operation instruction are constructed according to the preset inequality.

[0101] In an example embodiment of the present disclosure, the first operation instruction and the second operation instruction are constructed according to the preset inequality. Wherein, the inequality sign of the preset inequality is a less than or equal to sign, the first operation instruction is an operation instruction corresponding to the left side of the preset inequality, the first operation instruction is used to indicate the expression on the left side of the inequality, the second operation instruction is an operation instruction corresponding to the right side of the preset inequality, and the second operation instruction is used to indicate the expression on the right side of the inequality. For example, the preset inequality can be Jensen's inequality, Cauchy's inequality or other inequalities.

[0102] In an example embodiment of the present disclosure, for example, the preset inequality is f(x)≤g(x), the first operation instruction is an operation instruction for calculating y=f(x), and the second operation instruction is an operation instruction for calculating y=g(x).

[0103] Specifically, the format of the preset inequality is that, under the preset condition, the calculation result of the expression on the left side is less than or equal to the calculation result of the expression on the right side when the same variable is brought in, the operation instruction corresponding to the expression on the left side is constructed as the first operation instruction according to the expression on the left side, and the operation instruction corresponding to the expression on the right side is constructed as the second operation instruction according to the expression on the right side.

[0104] For example, the preset inequality is Jensen's inequality, which specifically includes:

[0105] If f is a convex function on [a, b], then for any x i ∈[a, b], λ i >0(i=1, 2,..., n), and ∑λ i =1 (preset condition), there is

[0106]

[0107] According to the first operation instruction is constructed according to (the expression on the left side), which is first defined by an operating system and a compiler that a convex function f has a domain [a, b], and then the Jensen coefficient λi is defined to construct the first operation instruction y=∑λi f(x). Similarly, the second operation instruction can be constructed as y=g(x).

[0108] In step S620, the first operation result is obtained by substituting the protection variable into the first operation instruction.

[0109] In an example embodiment of the present disclosure, the first operation result is obtained by substituting the protection variable into the first operation instruction. The first operation instruction is an operation instruction corresponding to the left side of the preset inequality. For example, the first operation instruction can be

[0110] For example, the protection variable is X, the value of which is 1, and the first operation instruction is The protection variable X is selected and a modulo (MOD) operation is performed, so that the value of X always falls within [a, b]. The modulo value of the protection variable is substituted into to obtain the corresponding first operation result.

[0111] In step S630, the second operation result is obtained by substituting the protection variable into the second operation instruction.

[0112] In an example embodiment of the present disclosure, the second operation result is obtained by substituting the protection variable into the second operation instruction. The second operation instruction is an operation instruction corresponding to the right side of the preset inequality, and the protection variable is the same variable as that in step S620. For example, the second operation instruction can be

[0113] For example, the protection variable is X, the value of which is 1, and the second operation instruction is The protection variable X is selected and a modulo operation is performed, so that the value of X always falls within [a, b]. The modulo value of the protection variable is substituted into to obtain the corresponding second operation result.

[0114] In step S640, the condition judgment instruction is constructed.

[0115] In an example embodiment of the present disclosure, the condition judgment instruction is constructed. The condition judgment instruction is used to judge the size relationship between the first operation result and the second operation result. Specifically, after the first operation result and the second operation result are obtained, the condition judgment instruction for comparing the numerical values of the first operation result and the second operation result is constructed.

[0116] In step S650, when it is judged that the first operation result is greater than the second operation result, a jump instruction is constructed to change the running logic of the original code. For example, the jump instruction can be to change the running logic of the original code to jump from the current position to the end.

[0117] In step S660, when it is judged that the first operation result is less than or equal to the second operation result, the running logic of the original code is executed.

[0118] Specifically, after obtaining the first operation result and the second operation result, a conditional judgment instruction for comparing the numerical values of the first operation result and the second operation result is constructed. The conditional judgment instruction includes that when the first operation result is greater than the second operation result, a jump instruction for changing the running logic of the original code is constructed and corresponding jump is performed, and when the first operation result is less than or equal to the second operation result, the running logic of the original code is executed.

[0119] For example, the first operation result is a result of substituting the modulus value of X into the result, and the second operation result is a result of substituting the modulus value of X into the result. The conditional judgment instruction is an if statement, and the jump instruction is a break statement. After obtaining the first operation result and the second operation result, an if statement for comparing the numerical values of the first operation result and the second operation result is constructed by using an operating system compiler. The content of the if statement includes that when the first operation result is greater than the second operation result, a break instruction for changing the running logic of the original code is constructed and the break instruction is executed, so that the code ends running, and when the first operation result is less than or equal to the second operation result, the running logic of the original code is executed. Since the size relationship between the first operation instruction and the second operation instruction always satisfies the Jensen inequality, that is, in the case of substituting the modulus value of X into the first operation result, the first operation result is always less than the second operation result, that is, the jump instruction is always not executed, so that the attacker is confused and the running logic of the original code is not disturbed.

[0120] In step S670, the code corresponding to the first operation instruction, the code corresponding to the second operation instruction, the code corresponding to the conditional judgment instruction, and the code corresponding to the jump instruction are taken as the protection code.

[0121] Through the above steps S610-S670, the first operation instruction and the second operation instruction are constructed according to the preset inequality. The protection variable is substituted into the first operation instruction to obtain the first operation result, the protection variable is substituted into the second operation instruction to obtain the second operation result, the conditional judgment instruction is constructed, the jump instruction is constructed to change the running logic of the original code when it is judged that the first operation result is greater than the second operation result, and the running logic of the original code is executed when it is judged that the first operation result is less than or equal to the second operation result. The code corresponding to the first operation instruction, the code corresponding to the second operation instruction, the code corresponding to the conditional judgment instruction, and the code corresponding to the jump instruction are taken as the protection code. The code condition branch that is not reachable at runtime is constructed by skillfully using mathematical inequality, so that the attacker is confused and the original code running logic is not disturbed.

[0122] In an example embodiment of the present disclosure, the original code is traversed, a protection position in the original code is obtained, the number of function call instructions is recorded, and the protection code is inserted into the protection position when the number of function call instructions is greater than a preset value. Specifically, reference is made toFigure 7 As shown, inserting the protection code into the original code can include the following steps S710-S730:

[0123] In step S710, the original code is traversed to obtain a protection position in the original code.

[0124] In an example embodiment of the present disclosure, the original code is traversed to obtain a protection position in the original code. The protection position is a position that does not change the running logic of the original code after the protection code is inserted. For example, the protection position can be a position between different logic blocks, and a logic block is a code block that implements a certain function and can be regarded as a whole in terms of function.

[0125] Specifically, the original code is traversed to analyze the structure of the original code and find a position that does not change the running logic of the original code after the protection code is inserted.

[0126] For example, the protection position is a position between different logic blocks, the original code is traversed to analyze the structure of the original code, and all logic blocks of the original code are determined. The position between the logic blocks is taken as the protection position.

[0127] In step S720, the number of function call instructions is recorded.

[0128] In an example embodiment of the present disclosure, the number of function call instructions is recorded. Specifically, the number of generated function call instructions is counted by using a counter of the operating system.

[0129] For example, the initial value of the counter of the operating system is set to 0, and the counter is incremented by 1 each time the operating system generates a function call instruction.

[0130] In step S730, when the number of function call instructions is greater than a preset value, the protection code is inserted into the protection position.

[0131] In an example embodiment of the present disclosure, when the number of function call instructions is greater than a preset value, the protection code is inserted into the protection position. The preset value is a value set by a developer and is used to specify the number of function call instructions.

[0132] Specifically, when it is detected that the number of function call instructions is greater than the preset value, the generation of function call instructions is stopped, and the protection code is inserted into the protection position.

[0133] For example, the preset value is 20, and when the operating system generates 21 function call instructions, the number of function call instructions is greater than the preset value, the generation of function call instructions is stopped, and the protection code is inserted into the protection position.

[0134] In an example embodiment of the present disclosure, the sum of the number of function call instructions, variable operation instructions, first operation instructions, second operation instructions, conditional judgment instructions, and jump instructions can be counted by a counter. When the sum of the number of instructions is greater than a preset value, the generation of each instruction is stopped and the protection code is inserted into the protection position.

[0135] Through the above steps S710-S730, the original code is traversed, the protection position in the original code is obtained, the number of function call instructions is recorded, and when the number of function call instructions is greater than a preset value, the protection code is inserted into the protection position. The insertion process is applied to the compiler end, and there is no invasion to the high-level language code written by the developer, reducing the influence on the developer.

[0136] In an example embodiment of the present disclosure, the protection code is converted into a preset format. Wherein, the preset format is an intermediate language format directly read by the compiler end.

[0137] Specifically, the protection code is converted into a sequence of triple expression to generate RTL (register transfer language) and subsequent machine code generation by the compilation process.

[0138] The triple expression sequence refers to an abstract structure containing at most two operands and one operator. For example, if the original code is "int a=(b<<2+7)*(x++)", the converted result is:

[0139] (t1)(<<, b, 2)

[0140] (t2)(+, t1, 7)

[0141] (t3)(*, t2, c)

[0142] (t4)(++, x,).

[0143] RTL is used to describe the data flow on the register transfer level in an architecture, and is an architecture-independent intermediate language.

[0144] Through the above steps, the protection code is converted into a format independent of the CPU architecture and the language, and the code protection can be implemented on various platforms of the CPU architecture.

[0145] In the code protection method provided by the example embodiment of the present disclosure, a plurality of protection variables are created, protection code is generated according to the protection variables, the protection code is inserted into the original code, and the protection code and the original code are compiled to obtain an executable file. On the one hand, by inserting the protection code into the original code, the readability of the logic of the original code is reduced, the cracking difficulty of the attacker is increased, and the security of the original code is improved. On the other hand, the protection variables are independent of the variables in the original code, and inserting the protection code related to the protection variables does not change the running logic of the original code, thereby reducing the impact on the coder. On the other hand, the code protection method provided by the example embodiment of the present disclosure is applied to the end of the compiler, and the protection code and the original code are converted into an intermediate language independent of the architecture platform through the end of the compiler, so that the protection code can be used on any platform, thereby realizing cross-platform protection code.

[0146] Figure 8 is a code protection device block diagram according to an example embodiment. Referring to Figure 8 The code protection device 800 includes a protection variable creation module 810, a protection code generation module 820, a protection code insertion module 830, and a running module 840. Wherein:

[0147] The protection variable creation module 810 is configured to create a plurality of protection variables. The protection variable is a global variable, the variable name of the protection variable is a string generated by an operating system, and the value of the protection variable is a random number generated by the operating system. The protection code generation module 820 is configured to generate protection code according to the protection variable. The protection code is used to protect the original code, and the protection code includes the protection variable. The protection code insertion module 830 is configured to insert the protection code into the original code. The original code includes original variables, and the original variables and the protection variables are different variables. The running module 840 is configured to run the protection code and the original code to obtain an executable file. The executable file is a program that can be directly run by a computer. In the process of running the executable file, the original code and the protection code are independent of each other.

[0148] In an example embodiment of the present disclosure, based on the foregoing scheme, the protection code generation module 820 further includes a definition unit configured to define an empty function, wherein the function name of the empty function is a randomly generated string; an operation function unit configured to add an operation function to the empty function to obtain a protection function, wherein the operation function is a function of operating the protection variable; a function call instruction construction unit configured to construct a function call instruction through the function name of the protection function, wherein the function call instruction is an instruction for calling the protection function; and a first protection code unit configured to take the code corresponding to the function call instruction as the protection code.

[0149] In an example embodiment of the present disclosure, based on the foregoing scheme, the protection code generation module 820 further includes a direct assignment unit configured to assign a value of a first variable to a second variable, wherein the first variable is one of the plurality of protection variables, the second variable is one of the plurality of protection variables, and the first variable is different from the second variable.

[0150] In an example embodiment of the present disclosure, based on the foregoing scheme, the protection code generation module 820 further includes a mathematical operation unit configured to perform an operation on a first variable and a second variable to obtain an operation result, wherein the first variable is one of the plurality of protection variables, the second variable is one of the plurality of protection variables, the first variable is different from the second variable, and the operation can be one or more of arithmetic operation, logical operation, and relational operation; and a result assignment unit configured to assign the operation result to the first variable.

[0151] In an example embodiment of the present disclosure, based on the foregoing scheme, the protection code generation module 820 further includes an operation rule unit configured to determine an operation rule, wherein the operation rule includes one or more of arithmetic operation, logical operation, and relational operation; a variable operation instruction construction unit configured to construct a variable operation instruction according to the operation rule and the protection variable, wherein the variable operation instruction is used to perform an operation on the protection variable; and a second protection code unit configured to use the code corresponding to the variable operation instruction as the protection code.

[0152] In an example embodiment of the present disclosure, based on the foregoing scheme, the protection code generation module 820 further includes an inequality instruction construction unit configured to construct a first operation instruction and a second operation instruction according to a preset inequality, wherein the inequality of the preset inequality is a less than or equal to sign, the first operation instruction is an operation instruction corresponding to the left side of the preset inequality, and the second operation instruction is an operation instruction corresponding to the right side of the preset inequality; a left side calculation unit configured to substitute the protection variable into the first operation instruction to obtain a first operation result; a right side calculation unit configured to substitute the protection variable into the second operation instruction to obtain a second operation result; a conditional judgment instruction construction unit configured to construct a conditional judgment instruction, wherein the conditional judgment instruction is used to judge the size relationship between the first operation result and the second operation result; a jump logic unit configured to construct a jump instruction to change the running logic of the original code when the first operation result is greater than the second operation result; a sequential logic unit configured to execute the running logic of the original code when the first operation result is less than or equal to the second operation result; and a third protection code unit configured to use the code corresponding to the first operation instruction, the code corresponding to the second operation instruction, the code corresponding to the conditional judgment instruction, and the code corresponding to the jump instruction as the protection code.

[0153] In an example embodiment of the present disclosure, based on the foregoing scheme, the running module 840 further comprises: a traversing unit, configured to traverse the original code to obtain a protection position in the original code; wherein the protection position is a position after the protection code is inserted without changing the running logic of the original code; a counting unit, configured to record the number of function call instructions; and an inserting unit, configured to insert the protection code into the protection position when the number of function call instructions is greater than a preset value.

[0154] In an example embodiment of the present disclosure, based on the foregoing scheme, the apparatus further comprises: a format converting unit, configured to convert the protection code into a preset format; wherein the preset format is a format directly read by a compiler.

[0155] Since each functional module of the code protection apparatus of the example embodiments of the present disclosure corresponds to the steps of the example embodiments of the code protection method described above, for details not disclosed in the apparatus embodiments of the present disclosure, please refer to the code protection method embodiments of the present disclosure described above.

[0156] It should be noted that although several modules or units of the device for action execution are mentioned in the foregoing detailed description, such division is not mandatory. In fact, according to the embodiments of the present disclosure, the features and functions of two or more modules or units described above can be embodied in one module or unit. Conversely, the features and functions of one module or unit described above can be further divided into embodied by multiple modules or units.

[0157] Figure 9 A structural schematic diagram of a computer system of an electronic device suitable for implementing the embodiments of the present disclosure is shown.

[0158] It should be noted that, Figure 9 The computer system 900 of the electronic device shown is only an example and should not impose any limitation on the functions and use range of the embodiments of the present disclosure.

[0159] As Figure 9 shown, the computer system 900 comprises a central processing unit (CPU) 901, which can perform various appropriate actions and processes according to programs stored in a read-only memory (ROM) 902 or programs loaded from a storage portion 908 into a random access memory (RAM) 903. In the RAM 903, various programs and data required for system operation are also stored. The CPU 901, the ROM 902, and the RAM 903 are connected to each other through a bus 904. An I / O interface 905 is also connected to the bus 904.

[0160] The following components are connected to the I / O interface 905: an input part 906 including a keyboard, a mouse, etc.; an output part 907 including a display such as a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage part 908 including a hard disk, etc.; and a communication part 909 including a network interface card such as a LAN card, a modem, etc. The communication part 909 performs communication processing via a network such as the Internet. A drive 910 is also connected to the I / O interface 905 as necessary. A removable medium 911 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc. is attached to the drive 910 as necessary, so that a computer program read out therefrom is installed in the storage part 908 as necessary.

[0161] In particular, according to embodiments of the present disclosure, the processes described below with reference to the flowcharts can be implemented as a computer software program. For example, embodiments of the present disclosure include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for executing the methods illustrated in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network by the communication part 909, and / or installed from the removable medium 911. When the computer program is executed by the central processing unit (CPU) 901, various functions defined in the methods and apparatuses of the present application are executed.

[0162] In exemplary embodiments of the present disclosure, a computer-readable storage medium having stored thereon a program product capable of implementing the above-described methods of the present specification is also provided. In some possible implementations, various aspects of the present disclosure can also be implemented in the form of a program product, which includes program code for causing an end device to perform the various exemplary embodiments and steps according to the present disclosure described in the above "Exemplary Methods" section of the present specification, when the program product is run on the end device.

[0163] The program product for implementing the above-described methods according to embodiments of the present disclosure can take the form of a portable compact disc read-only memory (CD-ROM) and include program code, and can be run on an end device such as a personal computer. However, the program product of the present disclosure is not limited to this, and the readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device.

[0164] The program product can employ any combination of one or more readable media. The readable media can be readable signal media or readable storage media. The readable media, for example, can include, but is not limited to, electrical, magnetic, optical, electromagnetic, infrared, or semiconductor technology, or any suitable combination thereof. More specific examples (a non-exhaustive list) of readable storage media include an electrical connection having one or more wires, a portable disc, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or Flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof.

[0165] The computer readable signal media can include a data signal traveling in baseband or aliened as part of a carrier wave traveling along a wiring, or wireless, medium. Such a propagated signal can take any number of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium can be any computer readable medium that is not a storage medium or is not a medium of transmission. Thus the computer readable signal medium can include the propagated signal as the computer readable code embodied in the propagated signal.

[0166] The program code embodied on the computer readable media can be transmitted using any appropriate medium, including but not limited to wireless, wired, optical fiber cable, RF, and the like, or any suitable combination thereof.

[0167] In addition, the above-described flowcharts are merely illustrative examples of the processes included in the method according to the exemplary embodiments of the present disclosure, and are not intended to limit the present disclosure. It is readily understood that the processes shown in the above-described flowcharts do not indicate or limit the time sequence of the processes. In addition, it is readily understood that the processes can be executed synchronously or asynchronously, for example, in a plurality of modules.

[0168] Other embodiments of the present disclosure will be readily apparent to those skilled in the art upon considering the specification and practice of the disclosed application. The present application is intended to cover any variations, uses, or adaptations of the present disclosure following, in general, the principles of the present disclosure and including such departures from the present disclosure that come within known or customary practice in the art to which the present disclosure pertains. The specification and examples are to be regarded as exemplary only, and the true scope and spirit of the present disclosure are indicated by the claims.

Claims

1. A code protection method, characterized by, The method is applied to the end of a compiler, and comprises the following steps: creating a plurality of protection variables; wherein the protection variables are global variables, the variable names of the protection variables are strings generated by an operating system, and the values of the protection variables are random values randomly generated by the operating system; generating protection code according to the protection variables; wherein the protection code is used for protecting original code, and the protection code contains the protection variables; inserting the protection code into the original code; wherein the original code contains original variables, and the original variables and the protection variables are different variables; compiling the protection code and the original code to obtain an executable file; wherein the executable file is a program that can be directly run by a computer, and the compiling logics of the original code and the protection code are independent of each other in the process of running the executable file; generating protection code according to the protection variables, comprising: defining an empty function; wherein the function name of the empty function is a randomly generated string; adding an operation function to the empty function to obtain a protection function; wherein the operation function is a function of operating the protection variables; constructing a function call instruction through the function name of the protection function; wherein the function call instruction is an instruction of calling the protection function; taking the code corresponding to the function call instruction as the protection code; the inserting of the protection code into the original code comprises: traversing the original code to obtain a protection position in the original code; wherein the protection position is a position that does not change the running logic of the original code after the protection code is inserted; recording the number of the function call instructions; when the number of the function call instructions is greater than a preset value, inserting the protection code into the protection position.

2. The method of claim 1, wherein, the operation function comprises: assigning the value of a first variable to a second variable; wherein the first variable is one of the plurality of protection variables, the second variable is one of the plurality of protection variables, and the first variable and the second variable are different variables.

3. The method of claim 1, wherein, the operation function comprises: performing an operation operation on a first variable and a second variable to obtain an operation result; wherein the first variable is one of the plurality of protection variables, the second variable is one of the plurality of protection variables, the first variable and the second variable are different variables, and the operation operation is one or more of four arithmetic operations, logical operations and relational operations; assigning the operation result to the first variable.

4. The method of claim 1, wherein, the generating of the protection code according to the protection variables comprises: determining an operation rule; wherein the operation rule comprises one or more of four arithmetic operations, logical operations and relational operations; constructing a variable operation instruction according to the operation rule and the protection variables; wherein the variable operation instruction is used for performing an operation operation on the protection variables; taking the code corresponding to the variable operation instruction as the protection code.

5. The method of claim 1, wherein, the generating of the protection code according to the protection variables comprises: constructing a first operation instruction and a second operation instruction according to a preset inequality; wherein, the inequality of the preset inequality is a less than or equal to sign, the first operation instruction is an operation instruction corresponding to a left side of the preset inequality, and the second operation instruction is an operation instruction corresponding to a right side of the preset inequality; substituting the protection variable into the first operation instruction to obtain a first operation result; substituting the protection variable into the second operation instruction to obtain a second operation result; constructing a condition judgment instruction; wherein, the condition judgment instruction is used to judge a size relation between the first operation result and the second operation result; when judging that the first operation result is greater than the second operation result, constructing a jump instruction to change a running logic of the original code; when judging that the first operation result is less than or equal to the second operation result, executing the running logic of the original code; taking the code corresponding to the first operation instruction, the code corresponding to the second operation instruction, the code corresponding to the condition judgment instruction, and the code corresponding to the jump instruction as the protection code.

6. The method of claim 1, wherein, After the protection code is generated according to the protection variable, the method further comprises: converting the protection code into a preset format; wherein, the preset format is a format directly read by a compiler end.

7. A code protection device, characterized by comprise: a protection variable creating module, used to create a plurality of protection variables; wherein, the protection variable is a global variable, a variable name of the protection variable is a string generated through an operating system, and a value of the protection variable is a random number value randomly generated through the operating system; a protection code generating module, used to generate a protection code according to the protection variable; wherein, the protection code is used to protect an original code, and the protection code contains the protection variable; a protection code inserting module, used to insert the protection code into the original code; wherein, the original code contains an original variable, and the original variable and the protection variable are different variables; a code compiling module, used to compile the protection code and the original code to obtain an executable file; wherein, the executable file is a program directly executable by a computer, and in a process of running the executable file, a compiling logic of the original code and a compiling logic of the protection code are independent of each other; the protection code generating module further comprises: a defining unit, used to define an empty function; wherein, a function name of the empty function is a string randomly generated; an operation function unit, used to add an operation function to the empty function to obtain a protection function; wherein, the operation function is a function of performing operation on the protection variable; a function call instruction constructing unit, used to construct a function call instruction through the function name of the protection function; wherein, the function call instruction is an instruction of calling the protection function; and a first protection code unit, used to take a code corresponding to the function call instruction as the protection code. The running module further includes: a traversing unit, configured to traverse the original code to obtain a protection position in the original code; wherein the protection position is a position after the protection code is inserted, which does not change the running logic of the original code; a counting unit, configured to record the number of function call instructions; and an inserting unit, configured to insert the protection code into the protection position when the number of function call instructions is greater than a preset value.

8. An electronic device, comprising: Comprise: a processor; and a memory for storing executable instructions of the processor; wherein the processor is configured to execute the code protection method of any one of claims 1-6 by executing the executable instructions.

9. A non-transitory computer-readable storage medium, comprising: When the instructions in the storage medium are executed by the processor of the electronic device, the electronic device can execute the code protection method of any one of claims 1-6.

Citation Information

Patent Citations

  • Protecting an item of software

    US20170213027A1