A method, system, device and storage medium for preventing decompilation
By adding preset member variables to the Java source code and replacing and simulating the process, the generated Class file can be run in the virtual machine but cannot be decompiled into readable source code, which solves the problem of decompiling the Java source code and improves security.
Patent Information
- Application Number
- CN202210322713.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-30
- Publication Date
- 2025-07-29
- Estimated Expiration
- 2042-03-30
AI Technical Summary
The existing technology cannot effectively prevent Java source code from being decompiled, causing criminals to steal or destroy server information by decompiling the program package into source code.
Add the first preset member variable and the second preset member variable to the initial source code, identify the Boolean expression of the judging logical code block, and replace it through the preset replacement rules, and perform simultaneous processing after compilation to generate the target Class file.
It improves the difficulty of understanding the source code during decompilation, so that the decompiled code does not meet the Java language specifications and cannot read specific logic, thereby preventing the Java source code from being decompiled.
Smart Images

Figure CN114707125B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer security, and particularly to a method, a system, a device and a storage medium for preventing decompilation. Background Art
[0002] As a computer language for object-oriented programming, Java has been widely used due to its characteristics such as high efficiency, open source, and easy maintenance. However, due to the above characteristics of the Java language, there is a risk that Java Class files can be decompiled. Lawbreakers can decompile the program package into source code by obtaining the program package, and it is easy to discover the internal interfaces of the software. Through this internal interface, illegal means are used to bypass the software's verification mechanism, thereby realizing behaviors such as stealing or destroying information in the server.
[0003] Currently, the technical means for preventing decompilation mainly involve encrypting Java Class files or obfuscating variable names. However, since Java Class files need to run in a virtual machine to achieve the established functions, and the Java Class files that can be recognized by the virtual machine must be decrypted or restored to the normal state of normal variables. Therefore, lawbreakers can dump the decrypted or restored normal state Class files to the local through the virtual machine, thereby realizing decompilation based on the Class files. Therefore, how to prevent Java source code from being decompiled has become an urgent problem to be solved. Summary of the Invention
[0004] The purpose of the embodiments of the present invention is to provide a method, a system, a device and a storage medium for preventing decompilation to prevent Java source code from being decompiled. The specific technical solutions are as follows:
[0005] A method for preventing decompilation, the method comprising:
[0006] Adding a first preset member variable and a second preset member variable to the initial source code to obtain a first target source code, wherein the first preset member variable and the second preset member variable are different.
[0007] Identifying the Boolean expressions of each judgment logic code block in the first target source code.
[0008] Using the first preset member variable and the second preset member variable, performing a replacement operation on the Boolean expressions according to a preset replacement rule to obtain a second target source code.
[0009] Compiling the second target source code to obtain an initial Class file.
[0010] According to the preset rule of homogenizing the same name, perform homogenization processing on the initial Class file to obtain the target Class file.
[0011] Optionally, the Boolean expressions for identifying each judgment logic code block in the first target source code include:
[0012] Traverse the first target source code, and for each code block in the first target source code: According to the preset target code block identification rule, determine whether there is judgment logic code in the code block. If so, determine the code block as the judgment logic code block.
[0013] Identify the Boolean expressions of each judgment logic code block.
[0014] Optionally, the Boolean expressions are divided into a first type and a second type. The Boolean expressions of the first type are Boolean expressions that cannot be split into sub-expressions. The second type is different from the first type. Using the first preset member variable and the second preset member variable, perform a replacement operation on the Boolean expression according to the preset replacement rule to obtain the second target source code, including:
[0015] When the type of the Boolean expression is the first type, according to the preset replacement rule, assign the first Boolean expression to the first preset member variable, and replace the first Boolean expression with the variable name of the first preset member variable. Assign the second Boolean expression to the second preset member variable, and replace the second Boolean expression with the variable name of the second preset member variable to obtain the second target source code, where the first Boolean expression and the second Boolean expression are different, and the first Boolean expression and the second Boolean expression are not in the same judgment logic code block.
[0016] Optionally, the Boolean expressions are divided into a first type and a second type. The Boolean expressions of the first type are Boolean expressions that cannot be split into sub-expressions. The second type is different from the first type. Using the first preset member variable and the second preset member variable, perform a replacement operation on the Boolean expression according to the preset replacement rule to obtain the second target source code, further including:
[0017] When the type of the Boolean expression is the second type, split the Boolean expression into a first sub-Boolean expression and a second sub-Boolean expression.
[0018] According to the preset replacement rule, assign the first sub-Boolean expression to the first preset member variable, and replace the first sub-Boolean expression with the variable name of the first preset member variable. After performing a negation operation on the second sub-Boolean expression, assign it to the second preset member variable, and replace the second sub-Boolean expression with the variable name of the second preset member variable after the negation operation to obtain the second target source code, where the first sub-Boolean expression and the second sub-Boolean expression are in the same judgment logic code block.
[0019] Optionally, perform a same-name processing on the initial Class file according to the preset constant same-name rule to obtain a target Class file, including:
[0020] According to the variable name of the first preset member variable, search for a string constant that matches the variable name of the first preset member variable in the constant pool of the initial Class file, and replace the string content of the string constant with the variable name of the second preset member variable.
[0021] Or, according to the variable name of the second preset member variable, search for a string constant that matches the variable name of the second preset member variable in the constant pool of the initial Class file, and replace the string content of the string constant with the variable name of the first preset member variable.
[0022] A system for preventing decompilation, the system includes:
[0023] A variable addition module, configured to add a first preset member variable and a second preset member variable to the initial source code to obtain a first target source code, where the first preset member variable and the second preset member variable are different.
[0024] An expression acquisition module, configured to identify the Boolean expressions in each judgment logic code block of the first target source code.
[0025] An expression replacement module, configured to use the first preset member variable and the second preset member variable to perform a replacement operation on the Boolean expression according to the preset replacement rule to obtain a second target source code.
[0026] A compilation module, configured to compile the second target source code to obtain an initial Class file.
[0027] A same-name processing module, configured to perform a same-name processing on the initial Class file according to the preset constant same-name rule to obtain a target Class file.
[0028] Optionally, the expression acquisition module is set to:
[0029] Traverse the first target source code, and for each code block in the first target source code: According to the preset target code block recognition rule, determine whether there is judgment logic code in the code block. If so, determine the code block as the judgment logic code block.
[0030] Identify the Boolean expressions of each judgment logic code block.
[0031] Optionally, the expression replacement module is set to:
[0032] When the type of the Boolean expression is the first type, according to the preset replacement rule, assign the first Boolean expression to the first preset member variable, and replace the first Boolean expression with the variable name of the first preset member variable. Assign the second Boolean expression to the second preset member variable, and replace the second Boolean expression with the variable name of the second preset member variable to obtain the second target source code, where the first Boolean expression and the second Boolean expression are different, and the first Boolean expression and the second Boolean expression are not in the same judgment logic code block.
[0033] Optionally, the expression replacement module is further set to:
[0034] When the type of the Boolean expression is the second type, split the Boolean expression into a first sub-Boolean expression and a second sub-Boolean expression.
[0035] According to the preset replacement rule, assign the first sub-Boolean expression to the first preset member variable, and replace the first sub-Boolean expression with the variable name of the first preset member variable. After taking the inverse operation on the second sub-Boolean expression, assign it to the second preset member variable, and replace the second sub-Boolean expression with the variable name of the second preset member variable after the inverse operation to obtain the second target source code, where the first sub-Boolean expression and the second sub-Boolean expression are in the same judgment logic code block.
[0036] Optionally, the homonym processing module is set to:
[0037] According to the variable name of the first preset member variable, search for a string constant matching the variable name of the first preset member variable in the constant pool of the initial Class file, and replace the string content of the string constant with the variable name of the second preset member variable.
[0038] Alternatively, according to the variable name of the second preset member variable, look up the string constant that matches the variable name of the second preset member variable in the constant pool of the initial Class file, and replace the string content of the string constant with the variable name of the first preset member variable.
[0039] An anti-decompilation device, the device includes: [[ID={4}]]
[0040] A processor;
[0041] A memory for storing executable instructions of the processor.
[0042] Wherein, the processor is configured to execute the instructions to implement the anti-decompilation method as described in any one of the above.
[0043] A computer-readable storage medium, when the instructions in the computer-readable storage medium are executed by the processor of the anti-decompilation device, the anti-decompilation device can execute the anti-decompilation method as described in any one of the above.
[0044] An anti-decompilation method, system, device and storage medium provided by an embodiment of the present invention use preset member variables to replace boolean expressions, realizing the obfuscation of judgment logic in source code. Compared with the existing method of obfuscating variable names, the present invention improves the difficulty of understanding the source code during decompilation without affecting the normal operation of the source code. At the same time, based on the differences between the Java language specification and the virtual machine specification, by performing a homonymization process on the initial Class file obtained after the replacement operation, the obtained target Class file can meet the virtual machine specification and be normally run. However, the source code obtained after decompiling it does not meet the Java language specification and is not readable. This makes the present invention, compared with the prior art, when the Class file is sent to the virtual machine for running, it can realize the normal operation of the Class file without restoring the Class file to a Java syntax standard readable state, thereby reducing the risk of being decompiled. It can be seen that the present invention can prevent Java source code from being decompiled.
[0045] Of course, it is not necessary for any product or method implementing the present invention to achieve all the above-mentioned advantages at the same time. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for the description of the embodiments or the prior art. Obviously, the following drawings are only some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can be obtained based on these drawings.
[0047] Figure 1 A flowchart of a method for preventing decompilation provided by an embodiment of the present invention;
[0048] Figure 2 A block diagram of a system for preventing decompilation provided by an embodiment of the present invention;
[0049] Figure 3 A block diagram of a device for preventing decompilation provided by an embodiment of the present invention. Detailed implementation manners
[0050] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.
[0051] An embodiment of the present invention provides a method for preventing decompilation. As Figure 1 shown, the method includes:
[0052] S101. Add a first preset member variable and a second preset member variable to the initial source code to obtain a first target source code, where the first preset member variable and the second preset member variable are different.
[0053] Optionally, in an optional embodiment of the present invention, the above initial source code may be an uncompiled text file edited by a programmer in accordance with the programming language specification. Among them, the above programming language specification may be the Java language specification. Optionally, in another optional embodiment of the present invention, the above preset member variable may be a variable that defines the variable content of the data type used to execute the judgment logic in the Java language. Among them, the above data type for executing the judgment logic may be the boolean type. Since the Java language specification only allows compatibility with two boolean types, and only a quantity greater than one boolean type can achieve the effect of obfuscating the judgment logic, the number of the above preset member variables is exactly two.
[0054] Specifically, in practical applications, the specific implementation manner of adding the first preset member variable and the second preset member variable to the initial source code may be to use a preset code addition algorithm to add the first preset member variable and the second preset member variable represented in the form of static fields to the class of the initial source code. The present invention does not overly limit the specific addition position of the above preset member variable, and it shall be subject to the actual application scenario.
[0055] Those skilled in the art can understand that, according to the Java language specification, the types of member variables in the same class in the source code can be the same, but the names cannot be the same. Therefore, when setting the above first preset member variable and second preset member variable, their data types can be boolean and Boolean respectively, and their data names are different. Among them, boolean is the boolean data type in Java, and Boolean is the wrapper class of boolean. Through the above setting, the first preset member variable and the second preset member variable set by the present invention are two boolean value types that meet the Java language specification and do not affect the normal operation of the source code.
[0056] S102. Identify the boolean expressions in each judgment logic code block of the first target source code.
[0057] Among them, a code block is an independent code area in the source code that encapsulates multiple lines of code together to implement a specific function.
[0058] Optionally, in an alternative embodiment of the present invention, the above judgment logic code block may be a code block in the above first target source code that implements branch process control based on boolean values. For example, code blocks with keywords such as if, while, and for for judgment logic are determined as judgment logic code blocks.
[0059] Optionally, in another alternative embodiment of the present invention, the above boolean expression (Boolean expression) in Java is a code declaration where the judgment logic value can only take a true value or a false value. The judgment logic value taken by the boolean expression represents the judgment logic of the code.
[0060] S103. Use the first preset member variable and the second preset member variable to perform a replacement operation on the boolean expression according to a preset replacement rule to obtain a second target source code.
[0061] Optionally, in an alternative embodiment of the present invention, the specific implementation manner of the above step S103, as Figure 1 shown, can adopt an assignment method to assign the judgment logic value of the boolean expression to the corresponding preset member variable, so that in the subsequent operation of the program, the preset member variable is used to replace the boolean expression to complete the logical judgment of the judgment logic code block. Since the variable name of the preset member variable can be set by itself and does not directly reflect the content of the judgment logic condition.
[0062] Therefore, by using the method of replacing the Boolean expression with a preset member variable, the present invention can achieve the obfuscation of the judgment logic in the source code. Even if the operator obtains the source code through decompilation, the obtained is only the second target source code after the replacement operation, and the judgment logic value therein is only the variable name of the preset member variable set by itself. And after the subsequent homonym processing, the operator still cannot obtain the specific judgment logic, thereby improving the security of protecting the Java source code.
[0063] S104. Compile the second target source code to obtain an initial Class file.
[0064] Optionally, in an alternative embodiment of the present invention, the above-mentioned Figure 1 step S104 as shown can be implemented by a preset compiler or by a compiler. The present invention does not make too many limitations on this.
[0065] S105. Perform homonym processing on the initial Class file according to the preset constant homonym rule to obtain a target Class file.
[0066] Optionally, in an alternative embodiment of the present invention, the above-mentioned homonym processing may be to unify the constant names of the judgment logic constants in the constant pool of the initial Class file.
[0067] For example, assume that there are two judgment logic constants in the constant pool of the initial Class file: A and B. Now perform the above-mentioned homonym processing on A and B, and set the constant names to be unified as "A". Then, in the target Class file obtained after the above-mentioned homonym processing, the judgment logic constant names are all "A".
[0068] At this time, when the operator decompiles according to the target Class file, since decompiling to obtain the source code needs to meet the Java language specification. And the Java language specification stipulates that the names of member variables in the source code cannot be the same. Obviously, the source code obtained by decompiling the target Class file does not meet the Java language specification, which causes the compiler to prompt an error during decompilation and must correct the error before normal compilation. However, since Figure 1The target Class file in step S105 shown is obtained by making the name of the initial Class file the same. The initial Class file is compiled from the second target source code, and the judgment logic values in the second target source code have been replaced by the first preset member variable and the second preset member variable, which causes the operator to be unable to distinguish in the second target source code from which Boolean expression the judgment logic value of the logical code block is derived. Furthermore, the operator is unable to confirm the truth or falsehood of the judgment logic value and is also unable to confirm the business logic of each logical code block, thus preventing the source code from being decompiled.
[0069] Optionally, in another optional embodiment of the present invention, since the Class file needs to run in the virtual machine to achieve the set function, the Class file needs to meet the virtual machine specification to be run. Since the virtual machine specification stipulates that as long as one of the constant types or constant names in the Class file is different, the Class file meets the specification and can be run by the virtual machine. For the above-mentioned target Class file, although the constant names of the judgment logic constants in it are the same, their constant types are different. It can be seen that the above-mentioned target Class file meets the virtual machine specification and can be run normally.
[0070] The present invention uses preset member variables to replace Boolean expressions to achieve the obfuscation of the judgment logic in the source code. Compared with the existing method of obfuscating variable names, the present invention improves the difficulty of understanding the source code during decompilation without affecting the normal operation of the source code. At the same time, based on the difference between the Java language specification and the virtual machine specification, by making the name of the initial Class file obtained after the replacement operation the same, the obtained target Class file can meet the virtual machine specification and be run normally. However, the source code that meets the Java language specification cannot be obtained after decompiling it, and the obtained source code is not readable. This makes the present invention, compared with the prior art, when the Class file is sent to the virtual machine for running, it is not necessary to restore the Class file to the Java syntax standard readable state to achieve the normal operation of the Class file, thereby reducing the risk of being decompiled. It can be seen that the present invention can prevent the Java source code from being decompiled.
[0071] Optionally, the obtaining of the Boolean expressions of the judgment logic code blocks in the first target source code includes:
[0072] Traverse the first target source code, and for each code block in the first target source code: according to the preset target code block recognition rule, judge whether there is judgment logic code in the code block. If so, determine the code block as a judgment logic code block.
[0073] Identify the Boolean expressions of the judgment logic code blocks.
[0074] It should be noted that in practical applications, there are multiple implementation methods for the above-mentioned Boolean expressions that identify each judgment logic code block. Here, one is provided exemplarily:
[0075] According to the keyword of the determined judgment logic code block, search for the Java syntax structure of this keyword. And according to this syntax structure, determine the position of the Boolean expression in the above-mentioned judgment logic code block.
[0076] For example: when the keyword of the judgment logic code block is "if", search for the syntax structure of the "if" statement from the preset database. The syntax structure of the "if" statement is: if (Boolean expression) {business logic}. Then, from this syntax structure, it can be located that the content within the parentheses is the Boolean expression, where the Boolean expression represents the judgment logic of this judgment code block.
[0077] Optionally, the above-mentioned judgment logic code can be a keyword for judgment logic. Such as "if", "while", and "for".
[0078] Optionally, Boolean expressions are divided into a first type and a second type. The Boolean expressions of the first type are those that cannot be split into sub-expressions. Different from the first type, the second type uses a first preset member variable and a second preset member variable, and performs a replacement operation on the Boolean expression according to a preset replacement rule to obtain a second target source code, including:
[0079] When the type of the Boolean expression is the first type, according to the preset replacement rule, assign the first Boolean expression to the first preset member variable, and replace the first Boolean expression with the variable name of the first preset member variable. Assign the second Boolean expression to the second preset member variable, and replace the second Boolean expression with the variable name of the second preset member variable to obtain the second target source code, where the first Boolean expression and the second Boolean expression are different, and the first Boolean expression and the second Boolean expression are not in the same judgment logic code block.
[0080] Optionally, in an alternative embodiment of the present invention, the above-mentioned method for judging the Boolean expressions of the first type and the second type can be implemented by judging whether a specific Java logical operator is included in the Boolean expression. If a specific Java logical operator is included, then determine that the Boolean expression is a Boolean expression of the second type. If a specific Java logical operator is not included, then determine that the Boolean expression is a Boolean expression of the first type.
[0081] It should be noted that the above-mentioned specific Java logical operators are limited to: "&&", "&", "|", and "||". Among them, both "&&" and "&" represent logical "AND", and "|" and "||" represent logical "OR".
[0082] Optionally, in another alternative embodiment of the present invention, the preset member variable may be composed of a variable type and a variable name. The variable types of the first preset member variable and the second preset member variable are different, and the variable names of the first preset member variable and the second preset member variable are also different.
[0083] For the convenience of understanding the implementation process of the above, when the type of the Boolean expression is the first type, the first preset member variable and the second preset member variable are used, and according to the preset replacement rule, the first Boolean expression is assigned to the first preset member variable, and the first Boolean expression is replaced with the variable name of the first preset member variable, the second Boolean expression is assigned to the second preset member variable, and the second Boolean expression is replaced with the variable name of the second preset member variable to obtain the second target source code. Here, a specific alternative embodiment of the present invention is used for illustration:
[0084] Suppose the existing first target source code includes two judgment logic code blocks with the keyword "if":
[0085] if(old == true); if(man == true).
[0086] Among them, "old == true" is Boolean expression 1, and the code logic of the judgment logic code block where this Boolean expression 1 is located is "judging whether it is an old person". "man == true" is Boolean expression 2, and the code logic of the judgment logic code block where this Boolean expression 2 is located is "whether it is a man".
[0087] Set the variable type of the first preset member variable to boolean and the variable name to x. Set the variable type of the second preset member variable to Boolean and the variable name to y.
[0088] Then, according to the preset replacement rule, Boolean expression 1 is replaced with the variable name of the first preset member variable, and Boolean expression 2 is replaced with the variable name of the second preset member variable. The specific implementation method of this replacement operation can be by means of assignment: let "x = old == true, and replace Boolean expression 1: old == true with x", let "y = man == true, and replace Boolean expression 2: man == true with y". Then the second target source code obtained after the above replacement operation is:
[0089] x = old == true; y = man == true; if(x); if(y).
[0090] Optionally, for the second target source code obtained after the above replacement operation, after the second target source code passes through such as Figure 1After the processing of step S104 and step S105 shown, the source code of the obtained target Class file can be modified to:
[0091] x = old == true; x = man == true; if(x); if(x).
[0092] Or: y = old == true; y = man == true; if(y); if(y).
[0093] It can be seen that through the above operations, the judgment logic in the initial source code is hidden. Even if the operator forcefully decompiles the Class file after the homonymization process and obtains the above second target source code, it is still impossible to determine the judgment logic of the initial source code. Thus, the operator cannot understand the source code obtained after forced decompilation.
[0094] Optionally, the above-mentioned use of the first preset member variable and the second preset member variable to perform a replacement operation on the boolean expression according to the preset replacement rule to obtain the second target source code further includes:
[0095] When the type of the boolean expression is the second type, the boolean expression is split into a first sub-boolean expression and a second sub-boolean expression.
[0096] According to the preset replacement rule, the first sub-boolean expression is assigned to the first preset member variable, and the first sub-boolean expression is replaced with the variable name of the first preset member variable. The second sub-boolean expression is negated and then assigned to the second preset member variable, and the second sub-expression is replaced with the variable name of the second preset member variable after the negation operation to obtain the second target source code, where the first sub-boolean expression and the second sub-boolean expression are in the same judgment logic code block.
[0097] Optionally, in an alternative embodiment of the present invention, the specific implementation of splitting the boolean expression into a first sub-boolean expression and a second sub-boolean expression when the type of the boolean expression is the second type may be to locate a specific Java logical operator, and then determine the expressions on both sides of the logical operator as the first sub-boolean expression and the second sub-boolean expression respectively. It should be noted that the present invention does not limit which side of the logical operator the expression is determined as the first sub-boolean expression and which side is determined as the second sub-boolean expression too much, and it depends on the actual application scenario.
[0098] Optionally, in another alternative embodiment of the present invention, the above-mentioned negation operation can be achieved by adding the logical operator "!" before the sub-boolean expression and the variable name. Through the above negation operation, while ensuring the correct code logic, it can increase the difficulty for the person performing the decompilation to understand the code.
[0099] To facilitate the understanding of the implementation process of the above-mentioned replacement operation on the Boolean expression when the type of the Boolean expression is the second type, using the first preset member variable and the second preset member variable according to the preset replacement rule to obtain the second target source code, a preferred embodiment of the present invention is hereby described for illustration:
[0100] Assume that the existing first target source code includes a judgment logic code block with a keyword "if":
[0101] boolean old = is old; Boolean man = is man; if(man && old).
[0102] Among them, && represents the logical "AND". Then the code logic of the Boolean expression "man && old" in this judgment logic code block is "is a man and is an old person".
[0103] It can be judged from this that the Boolean expression "man && old" is the above-mentioned second type of Boolean expression, and it needs to be split into a first sub-Boolean expression "man" and a second sub-Boolean expression "old".
[0104] According to the preset replacement rule, assign the first sub-Boolean expression to x: x = man. And replace the first sub-Boolean expression with the variable name of the first preset member variable. After taking the negation operation on the second sub-Boolean expression, assign it to y: y =!(old). And replace the second sub-Boolean expression with the variable name of the second preset member variable after the negation operation. Then, after the above operations, the obtained second target source code is:
[0105] x = man; y =!(old); if(x &&!y).
[0106] Optionally, for the second target source code obtained after the above replacement operation, after the second target source code is processed through steps S104 and S105 as shown in Figure 1 the source code of the obtained target Class file can be modified to:
[0107] if(x &&!x), or if(y &&!y).
[0108] Thereby hiding the judgment logic in the initial source code, so that the operator cannot understand the source code obtained after forced decompilation.
[0109] Optionally, according to the preset constant homonymization rule, perform homonymization processing on the initial Class file to obtain the target Class file, including:
[0110] According to the variable name of the first preset member variable, search for a string constant that matches the variable name of the first preset member variable in the constant pool of the initial Class file, and replace the string content of the string constant with the variable name of the second preset member variable;
[0111] Alternatively, according to the variable name of the second preset member variable, search for a string constant that matches the variable name of the second preset member variable in the constant pool of the initial Class file, and replace the string content of the string constant with the variable name of the first preset member variable.
[0112] It should be noted that in practical applications, there are various implementation methods for the above-mentioned homogenization processing of the initial Class file according to the preset constant homogenization rule. Here, an example is provided:
[0113] Now it is set to replace the variable name of the second preset member variable with the variable name of the first preset member variable.
[0114] Based on the setting of the Class file format in the Java Virtual Machine Specification, obtain an identifier that matches the variable type of the second preset member variable.
[0115] According to this identifier, traverse the constant pool of the initial Class file to find a constant that matches this identifier and whose data type is string type.
[0116] Replace the string content of this string constant with the variable name of the first preset member variable and save it.
[0117] Determine the initial Class file after the above-mentioned homogenization processing as the target Class file.
[0118] After the above-mentioned homogenization processing, in the target Class file obtained, the variable names of the boolean expressions in the source code are the same, but the variable types are different. Therefore, although this target Class file can be normally run by the virtual machine, its source code no longer meets the requirements of the Java language specification. When decompiling the target Class file, a compilation error prompt will be generated, resulting in decompilation failure. Moreover, due to the judgment logic expressed by the boolean expressions in the source code of the target Class file being confused as the variable names of the preset member variables through the replacement operation, the decompiled code has poor readability. When there are a large number of classes or a large amount of complex judgment logic in the target Class file, the operator cannot infer the correct judgment logic based on the decompiled partial code, thus preventing decompilation.
[0119] Corresponding to the above method embodiment, the present invention also provides a system for preventing decompilation, as Figure 2 shown, this system includes:
[0120] A variable addition module 201, configured to add a first preset member variable and a second preset member variable to an initial source code to obtain a first target source code, where the first preset member variable and the second preset member variable are different.
[0121] An expression acquisition module 202, configured to identify Boolean expressions of each judgment logic code block in the first target source code.
[0122] An expression replacement module 203, configured to perform a replacement operation on the Boolean expressions according to a preset replacement rule by using the first preset member variable and the second preset member variable to obtain a second target source code.
[0123] A compilation module 204, configured to compile the second target source code to obtain an initial Class file.
[0124] A homonym processing module 205, configured to perform homonym processing on the initial Class file according to a preset constant homonym rule to obtain a target Class file.
[0125] Optionally, the above-mentioned expression acquisition module 202 is configured as follows:
[0126] Traverse the first target source code, and for each code block in the first target source code: according to a preset target code block identification rule, determine whether there is judgment logic code in the code block. If so, determine the code block as a judgment logic code block.
[0127] Identify the Boolean expressions of each judgment logic code block.
[0128] Optionally, the above-mentioned expression replacement module 203 is configured as follows:
[0129] When the type of the Boolean expression is a first type, according to a preset replacement rule, assign the first Boolean expression to the first preset member variable, replace the first Boolean expression with the variable name of the first preset member variable, assign the second Boolean expression to the second preset member variable, and replace the second Boolean expression with the variable name of the second preset member variable to obtain a second target source code, where the first Boolean expression and the second Boolean expression are different, and the first Boolean expression and the second Boolean expression are not in the same judgment logic code block.
[0130] Optionally, the above-mentioned expression replacement module 203 is further configured as follows:
[0131] When the type of the Boolean expression is a second type, split the Boolean expression into a first sub-Boolean expression and a second sub-Boolean expression.
[0132] According to the preset replacement rule, assign the first sub-boolean expression to the first preset member variable, replace the first sub-boolean expression with the variable name of the first preset member variable, perform a negation operation on the second sub-boolean expression and then assign it to the second preset member variable, and replace the second sub-expression with the variable name of the second preset member variable after the negation operation to obtain the second target source code, where the first sub-boolean expression and the second sub-boolean expression are in the same judgment logic code block.
[0133] Optionally, the above-mentioned homonym processing module 205 is set to:
[0134] According to the variable name of the first preset member variable, search for the string constant matching the variable name of the first preset member variable in the constant pool of the initial Class file, and replace the string content of the string constant with the variable name of the second preset member variable.
[0135] Or, according to the variable name of the second preset member variable, search for the string constant matching the variable name of the second preset member variable in the constant pool of the initial Class file, and replace the string content of the string constant with the variable name of the first preset member variable.
[0136] An embodiment of the present invention provides an anti-decompilation device, as Figure 3 shown, the device includes:
[0137] A processor 301;
[0138] A memory 302 for storing executable instructions of the processor 301.
[0139] Wherein, the processor 301 is configured to execute instructions to implement the above-mentioned anti-decompilation method as Figure 1 shown.
[0140] An embodiment of the present invention provides a computer-readable storage medium. When the instructions in the computer-readable storage medium are executed by the processor of the anti-decompilation device, the anti-decompilation device can execute the above-mentioned anti-decompilation method as Figure 1 shown.
[0141] The memory may include non-permanent memory in the computer-readable medium, forms such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash memory (flash RAM), and the memory includes at least one storage chip. The memory is an example of a computer-readable medium.
[0142] A computer-readable medium includes both permanent and non-permanent, removable and non-removable media that can store information by any method or technology. The information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices, or any other non-transitory medium that can be used to store information accessible by a computing device. As defined herein, a computer-readable medium does not include transitory computer-readable media such as modulated data signals and carrier waves.
[0143] Those skilled in the art will understand that the embodiments of the present application can be provided as a method, a system, or a computer program product. Therefore, the present application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present application can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.
[0144] It should be noted that, in this document, relational terms such as "first" and "second" are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. It should also be noted that the term "comprising", "including" or any other variation thereof is intended to cover non-exclusive inclusion, so that a process, method, commodity or device comprising a series of elements not only includes those elements, but also includes other elements not expressly listed, or elements inherent to such process, method, commodity or device. Without further limitation, an element defined by the statement "comprising an..." does not exclude the presence of additional identical elements in the process, method, commodity or device comprising the element.
[0145] Each embodiment in this specification is described in a related manner. The same or similar parts among the embodiments can be referred to each other, and the differences between each embodiment and other embodiments are emphasized. In particular, for system embodiments, since they are basically similar to method embodiments, they are described relatively simply, and the relevant parts can be referred to the description of the method embodiments.
[0146] The above are only embodiments of the present application and are not intended to limit the present application. For those skilled in the art, various changes and modifications can be made to the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included within the scope of the claims of the present application.
Claims
1. A method for preventing decompilation, characterized in that, The method comprises: Adding a first preset member variable and a second preset member variable to the initial source code to obtain a first target source code, wherein the first preset member variable and the second preset member variable are different; Traversing the first target source code, for each code block in the first target source code: determining whether there is a judgment logic code in the code block according to a preset target code block identification rule, and if so, determining the code block as a judgment logic code block; Identify a Boolean expression of each judgment logic code block, where the Boolean expression is divided into a first type and a second type, where the first type of Boolean expression is a Boolean expression that cannot be split into sub-expressions, and the second type is different from the first type; When the type of the Boolean expression is the first type, according to a preset replacement rule, assigning the first Boolean expression to the first preset member variable and replacing the first Boolean expression with the variable name of the first preset member variable, assigning the second Boolean expression to the second preset member variable and replacing the second Boolean expression with the variable name of the second preset member variable, to obtain a second target source code, wherein the first Boolean expression and the second Boolean expression are different, and the first Boolean expression and the second Boolean expression are not in the same judgment logic code block; Compile the second target source code to obtain an initial Class file; According to the preset constant homonymization rule, the initial Class file is homonymized to obtain the target Class file.
2. The method according to claim 1, characterized in that, Also includes: When the type of the Boolean expression is the second type, splitting the Boolean expression into a first sub-Boolean expression and a second sub-Boolean expression; According to the preset replacement rule, the first sub-Boolean expression is assigned to the first preset member variable, and the first sub-Boolean expression is replaced with the variable name of the first preset member variable; the second sub-Boolean expression is negated and assigned to the second preset member variable, and the second sub-Boolean expression is replaced with the variable name of the second preset member variable after the negation operation, to obtain the second target source code, wherein the first sub-Boolean expression and the second sub-Boolean expression are in the same judgment logic code block.
3. The method according to claim 1 or 2, characterized in that: According to the preset constant homonymization rule, the initial Class file is homonymized to obtain the target Class file, including: According to the variable name of the first preset member variable, search the constant pool of the initial Class file for a string constant that matches the variable name of the first preset member variable, and replace the string content of the string constant with the variable name of the second preset member variable; Or, according to the variable name of the second preset member variable, search the constant pool of the initial Class file for a string constant that matches the variable name of the second preset member variable, and replace the string content of the string constant with the variable name of the first preset member variable.
4. A system for preventing decompilation, characterized in that, The system comprises: A variable addition module for adding a first preset member variable and a second preset member variable to an initial source code to obtain a first target source code, where the first preset member variable and the second preset member variable are different; An expression acquisition module for traversing the first target source code and, for each code block in the first target source code: according to a preset target code block recognition rule, determining whether there is judgment logic code in the code block. If so, determining the code block as a judgment logic code block and identifying the Boolean expressions of each judgment logic code block. The Boolean expressions are divided into a first type and a second type. The Boolean expressions of the first type are Boolean expressions that cannot be split into sub-expressions, and the second type is different from the first type; An expression replacement module for, when the type of the Boolean expression is the first type, according to a preset replacement rule, assigning a first Boolean expression to the first preset member variable, replacing the first Boolean expression with the variable name of the first preset member variable, assigning a second Boolean expression to the second preset member variable, and replacing the second Boolean expression with the variable name of the second preset member variable to obtain a second target source code, where the first Boolean expression and the second Boolean expression are different, and the first Boolean expression and the second Boolean expression are not in the same judgment logic code block; A compilation module for compiling the second target source code to obtain an initial Class file; A homonym processing module for performing homonym processing on the initial Class file according to a preset constant homonym rule to obtain a target Class file.
5. A device for preventing decompilation, characterized in that: The device includes: A processor; A memory for storing executable instructions of the processor; Wherein, the processor is configured to execute the instructions to implement the anti-decompilation method according to any one of claims 1 to 3.
6. A computer-readable storage medium, characterized in that, When the instructions in the computer-readable storage medium are executed by the processor of the anti-decompilation device, the anti-decompilation device is enabled to execute the anti-decompilation method according to any one of claims 1 to 3.
Citation Information
Patent Citations
Opaque predicate and finite state machine-based source code enhancement method and apparatus
CN108416191A
Source code processing method and device, storage medium and electronic equipment
CN110058861A