Solidity language source code obfuscation method, system, device and storage medium
By constructing an abstract syntax tree and control flow graph, replacing variable names, disrupting the layout, generating opaque predicates, and flattening the code, the problem of Solidity language contracts being easily reverse-engineered is solved, improving contract security and complexity.
Patent Information
- Application Number
- CN202210230894.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-09
- Publication Date
- 2025-12-30
- Estimated Expiration
- 2042-03-09
AI Technical Summary
Existing technologies cannot effectively protect Ethereum smart contracts written in Solidity from reverse engineering. The rich source code information in the compiled binary files is easily exploited by reverse engineers, resulting in insufficient contract security.
By constructing an abstract syntax tree and control flow graph, replacing variable names, disrupting the layout, and removing auxiliary information, and by using an improved Chebyshev-PWLCM chaotic mapping to generate opaque predicates, contract logic is hidden, and constants are converted into function calls or arithmetic expressions, thus achieving code flattening and enhancing contract complexity.
It significantly improves the security of Ethereum smart contracts, enhances their ability to resist reverse engineering, and avoids unnecessary financial losses.
Smart Images

Figure CN114611074B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to a method, system, device, and storage medium for obfuscating Solidity language source code, specifically for Ethereum smart contracts, and falls under the field of blockchain security. Background Technology
[0002] Ethereum is an open-source public blockchain platform with smart contract functionality, addressing the scalability limitations of the Bitcoin network. Smart contracts are autonomous programs that run on a blockchain platform. They are typically developed in Solidity and then compiled into binary files. Once deployed to the corresponding blockchain, anyone can publicly view the contents of the smart contract's binary file and execute it by calling it, but cannot modify it. Therefore, ensuring that deployed smart contracts have sufficient complexity to effectively resist reverse engineering is a crucial issue.
[0003] Solidity is a statically typed, compiled high-level language specifically designed for smart contract development. It supports inheritance, various libraries, and user-defined types. It is well-suited for developing various functionalities such as voting, crowdfunding, auctions, and multi-signature wallets. Because Solidity is a compiled language, the compiled binary file contains rich source code information. The strings and symbols used in the binary file can assist reverse engineers in cracking the program.
[0004] Compared to other languages, such as interpreted languages like Python, JavaScript, PHP, Shell, and MATLAB, programs are translated during execution, involving intermediate bytecode transformation. Obfuscation techniques used for these languages obfuscate this intermediate bytecode. However, Solidity, as a compiled language, does not have intermediate bytecode, rendering obfuscation ineffective. Furthermore, other compiled languages, including C, C++, and Go, have drastically different syntax structures and lack special features like contracts or transfers. Therefore, obfuscation techniques used for C, C++, and Go cannot be applied to Solidity. Summary of the Invention
[0005] Purpose of the invention: To address the shortcomings of the existing technology, the purpose of this invention is to provide a Solidity language source code obfuscation method. By making the code difficult to read, modifying the data fields of the smart contract, and hiding the internal logic of the smart contract, the obfuscated smart contract is generated, thereby enhancing the smart contract's ability to resist reverse engineering.
[0006] Technical solution: To achieve the above-mentioned objectives, the present invention provides a Solidity language source code obfuscation method, comprising the following steps:
[0007] Step 1: Read the source code and construct the abstract syntax tree and control flow graph of the source code;
[0008] Step 2: Replace variable names in the source code, disrupt the source code layout, and remove auxiliary information;
[0009] Step 3: Transform the variable state in the source code, converting constants into function calls or arithmetic expressions to modify data fields in the smart contract;
[0010] Step 4: Flatten the global code control flow, function body control flow, and basic loop or branch blocks using switch-case statements. In the loop or branch statements, generate and introduce a chaotic permaliterate opaque predicate based on a one-dimensional chaotic map (Chebyshev-PWLCM Map, CPM) that combines Chebyshev and PWLCM (piecewise linear chaotic map) to hide the internal logic of the original program.
[0011] Step 5: Determine whether the functionality of the smart contract is consistent before and after obfuscation. If the functionality is consistent, complete the source code obfuscation of the Solidity language.
[0012] Preferably, step 2 further includes:
[0013] Step 21: Locate the name and corresponding location of each variable in the source code, generate the corresponding encrypted value using an encryption algorithm, and then replace it;
[0014] Step 22: Use regular expressions to replace newline and space characters with single quotes (''), and replace consecutive whitespace characters with single whitespace characters;
[0015] Step 23: Use regular expressions to replace all single-line and multi-line comments in the source code with single quotes ('') and remove auxiliary information from the source code.
[0016] Preferably, step 3 further includes:
[0017] Step 31: Find all declared local variables in the source code and replace the found local variables with the newly declared global variables.
[0018] Step 32: Locate the position and value of all type constants in the source code, declare an array based on the constant value, and then replace the constant with the corresponding function call to realize the transformation of static data into dynamic generation;
[0019] Step 33: Find all Boolean variables in the source code. If a Boolean variable is true, connect it to the subsequent expression using the "OR" operator; if a Boolean variable is false, connect it to the subsequent expression using the "AND" operator. The subsequent expression is a Boolean expression, an arithmetic expression, or a combination thereof.
[0020] Step 34: Locate all state variables in the source code, group them into a structure, and call them through the structure member variables to convert the scalar into a vector.
[0021] Preferably, the code implementation for global code control flow flattening and function body control flow flattening in step 4 is the same; both are flattened using a switch-case approach. The only difference is that function body control flow flattening involves an additional step of extracting the function shell. Loop or branch block flattening goes further, requiring parsing the expressions in the loop or branch statements and flattening based on the truth value of the expressions. If the expression is true, a switch-case branch is generated; if the expression is false, another switch-case branch is generated.
[0022] In order to achieve a good obfuscation effect when opaque predicates are inserted into smart contracts, the process of selecting a suitable chaotic system focuses more on the sensitivity, randomness and timeliness of the chaotic system.
[0023] The method for generating opaque predicates is to combine Chebyshev and PWLCM chaotic maps, proposing an improved one-dimensional chaotic map (Chebyshev-PWLCM Map, CPM). The main design direction of CPM is to retain the good sensitivity and uncertainty of Chebyshev and PWLCM chaotic maps, while minimizing and eliminating the problems of small chaotic range in Chebyshev chaotic maps and discontinuities in PWLCM chaotic maps. This increases the safety, continuity, and uniformity of distribution of the improved chaotic map. The definition of the improved chaotic map is as follows:
[0024]
[0025] Given an initial value x0, x1 is iterated from x0, and then x is obtained. n x n ∈[-1, 1], by x n Generate x n+1 μ and p are both control parameters of this chaotic mapping, where μ∈Z * , p∈(0,0.5); F is the iterative formula for this one-dimensional chaotic mapping, when 0.5≤x n When <1, the variable x in the function n =1-xn The remaining parameters μ and p remain unchanged; when μ≥2 and p∈(0,0.5), the CPM chaotic mapping is in a chaotic state; M is a component of the key in this chaotic mapping and is the perturbation parameter of the chaotic mapping. While retaining the good initial value sensitivity and randomness of Chebyshev and PWLCM chaotic mappings, the CPM chaotic mapping also removes the zero-point problem of PWLCM, expands the chaotic range, and increases the security and continuity of the chaotic mapping. Furthermore, CPM further mixes the chaotic trajectories more uniformly, improving the performance of the chaotic mapping.
[0026] The method of introducing opaque predicates is as follows: First, select the expression Exp into which the opaque predicate will be inserted, observe its structure and decompose it according to the rules, and then generate a number of opaque predicates P based on the decomposition results. The generated opaque predicates and the decomposition results are merged. If the generated opaque predicate is always true, it is Exp&P; if the generated opaque predicate is always false, it is Exp||P and is shifted to generate a new expression, which increases the complexity of the obfuscation process.
[0027] Preferably, if any failure occurs in steps 2 to 4, the failed part is skipped, the next step of obfuscation is performed, an error report is returned, and the corresponding Solidity source code is generated. If no failure occurs in the above functions, a success message is returned, and the corresponding Solidity source code is generated.
[0028] Based on the same inventive concept, this invention provides a Solidity language source code obfuscation system, comprising the following parts:
[0029] The target building unit is used to read source code and construct the abstract syntax tree and control flow graph of the source code;
[0030] The first processing unit is used to replace variable names in the source code, disrupt the source code layout, and delete auxiliary information.
[0031] The second processing unit is used to transform the state of variables in the source code and convert constants into function calls or arithmetic expressions to modify data fields in the smart contract.
[0032] The third processing unit is used to flatten the global code control flow, function body control flow, and loop or branch basic blocks through switch-case, and to generate and introduce chaotic eternal true opaque predicates based on the one-dimensional chaotic mapping CPM that combines Chebyshev and PWLCM in the loop or branch statements to hide the internal logic of the original program.
[0033] The target generation unit is used to determine whether the functionality of the smart contract is consistent before and after obfuscation. If the functionality is consistent, it completes the source code obfuscation of the Solidity language.
[0034] Based on the same inventive concept, the present invention provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program implements the Solidity language source code obfuscation method when loaded onto the processor.
[0035] Based on the same inventive concept, the present invention provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the Solidity language source code obfuscation method.
[0036] Beneficial Effects: This invention provides a Solidity language source code obfuscation method targeting Ethereum smart contracts developed using the Solidity language. The input to this method is an Ethereum smart contract developed using Solidity. It utilizes variable name replacement, source code layout disruption, and removal of auxiliary techniques to make the code more difficult for attackers to read and understand; it modifies data fields in the smart contract by transforming variable states and types in the source code; it hides the internal logic of the original program using code flattening and improved opaque predicate generation and introduction techniques; and it achieves Solidity language source code obfuscation by using difficult-to-read code, modifying smart contract data fields, and hiding the internal logic of the smart contract, while maintaining functional consistency before and after obfuscation. This invention significantly increases the complexity of Ethereum smart contracts developed using the Solidity language, effectively enhances the smart contract's resistance to reverse engineering, improves smart contract security, and avoids unnecessary financial losses. Attached Figure Description
[0037] Figure 1 This is a flowchart illustrating the overall steps of an embodiment of the present invention.
[0038] Figure 2 This is a flowchart illustrating a specific example of the method of the present invention.
[0039] Figure 3 This is a schematic block diagram of a system according to an embodiment of the present invention. Detailed Implementation
[0040] The present invention will be further illustrated below with reference to specific embodiments. It should be understood that these embodiments are for illustrative purposes only and are not intended to limit the scope of the invention. After reading the present invention, any modifications of the present invention in various equivalent forms by those skilled in the art will fall within the scope defined by the appended claims.
[0041] Given the immutable nature of data on the blockchain and the fact that most Ethereum smart contracts involve monetary transactions, the losses caused by successful reverse engineering of smart contracts are far more severe than in other areas. This invention provides a method for obfuscating Solidity language source code, such as... Figure 1 As shown, it mainly includes 5 steps:
[0042] Step 1: Read the source code and construct the abstract syntax tree and control flow graph of the source code;
[0043] Step 2: Replace variable names in the source code, disrupt the source code layout, and remove auxiliary information;
[0044] Step 3: Transform the variable state in the source code, converting constants into function calls or arithmetic expressions to modify data fields in the smart contract;
[0045] Step 4: Flatten the global code control flow, function body control flow, and basic loop or branch blocks using switch-case statements, and generate and introduce chaotic perpetual true opaque predicates based on the improved one-dimensional chaotic mapping CPM in loop or branch statements to hide the internal logic of the original program.
[0046] Step 5: Determine whether the functionality of the smart contract is consistent before and after obfuscation. If the functionality is consistent, complete the source code obfuscation of the Solidity language.
[0047] like Figure 2 As shown, this embodiment of the invention uses any publicly available Ethereum smart contract written in Solidity from an Ethereum browser as an example to illustrate the detailed steps of a Solidity source code obfuscation method disclosed in this embodiment of the invention, as follows:
[0048] The Ethereum smart contract source code in step 1 needs to be written using Solidity, the high-level programming language for Ethereum smart contracts. This step involves reading the source code and constructing its abstract syntax tree and control flow graph. Step 1 is further described as follows:
[0049] Step 11: From the Ethereum Explorer website (https: / / etherscan.io / verifiedContracts / ), select any Ethereum smart contract written in Solidity, copy the contract's source code, and save it locally as a .sol file.
[0050] Step 12: Use the Solidity command-line compiler, solc, to read the source code. Use the command solc-ooutputDirectory--ast sourceFile.sol to parse the source code. If there are errors in the source code, return error information. If the source code is parsed successfully, return the structure of the abstract syntax tree as a file. The abstract syntax tree structure contains detailed package information, the detailed path in the current compilation environment, and the structural information of each syntax node.
[0051] Step 13: Read the generated Abstract Syntax Tree (ABS) file line by line, storing the ABS content in a string array. Traverse the array line by line, creating basic block objects to reuse the ABS content as much as possible. Collect all ABS nodes of a basic block into the array; for example, locate and record the "name" of a block as a basic block. Record the incoming and outgoing edges of the control flow graph. For example, use "FunctionDefinition" and "FunctionCall" from the ABS to generate function call relationships, and use "IfStatement" and "WhileStatement" as keywords for branching and looping statements to define the scope of the loop body. Create control flow graph edge objects to represent the edges of the control flow graph, connecting source and target basic blocks, and record the conditions for jumping to the target. For example, use "BinaryOperation" to determine the range of conditions, "operator" to determine the operator, and find the left and right operands in the "attributes" above and below the condition range. Create data flow objects to record variable assignments; each data flow object records only one assignment of a variable.
[0052] Step 14: Combine the basic block objects, control flow graph edge objects, and data flow objects to generate a complete control flow graph. Use the graph drawing tool Graphviz to generate the corresponding control flow graph with basic blocks as nodes and corresponding control flow graph edges. The naming rule is "original filename_ControlFlow.dot".
[0053] Step 2 utilizes variable name replacement, source code layout disruption, and removal of auxiliary information techniques to make the code more difficult for attackers to read and understand. Step 2 is further described as follows:
[0054] Step 21: Locate the name and corresponding location of each variable in the source code, generate the corresponding encrypted value using an encryption algorithm, and then replace it.
[0055] Step 22: Use the regular expression "\\s*|\t|\r|\n" to replace newline and space characters with single quotes ('') and consecutive whitespace characters with single whitespace characters, thereby disrupting the layout of the source code.
[0056] Step 23: Use the regular expression "\\ / \\ / [^\\n]*|\\ / \\*([^\\*^\\ / ]*|[\\*^\\ / *]*|[^\\**\\ / ]*)*\\*+\\ / " to replace all single-line and multi-line comments in the source code with '', and delete auxiliary information in the source code.
[0057] In step 3, we use the information perturbation technique completed in step 2 to modify the data fields in the smart contract by parsing the abstract syntax tree to transform the variable states and types in the source code. Step 3 can be further divided into:
[0058] Step 31: Locate all declared local variables in the source code, and then handle any existing local variables with the same name. Record the location of each local variable in the source code, and replace each found local variable one by one with a newly declared global variable. For example, by parsing the abstract syntax tree file, determine the location of elements such as "VariableDeclaration" and "referencedDeclaration", and establish the replacement scope based on their scope in "src". Redeclare the local variables and replace them.
[0059] Step 32: Solidity contains four types of constants—integer, boolean, string, and hexadecimal string, which correspond to the types of "type" in "attributes". By parsing the abstract syntax tree file, the location and corresponding values of the first three types of constants are obtained (the value of the last type cannot be obtained). Then, arrays are declared based on the constant values, and the constant values are declared in the arrays. Next, functions are inserted to return the elements corresponding to different indices in the arrays. Finally, the constants are replaced with the corresponding function calls.
[0060] Furthermore, integer constants can be replaced with expressions. Locate all integer constants in the source code, i.e., those with "type = uint256 or other bit depth" in "attributes". Record the location of each constant based on its scope in "src". Convert it into a more complex arithmetic expression that generates the original value. Due to Solidity's lack of support for floating-point numbers, the generated arithmetic expression does not contain floating-point numbers or use division.
[0061] Step 33: Locate all Boolean variables in the source code, specifically those with "type = bool" in "attributes". Record the location of each constant based on its scope in "src". If the Boolean variable is true, connect it to the subsequent expression using the "OR" operator. If the Boolean variable is false, connect it to the subsequent expression using the "AND" operator. The subsequent expression may be a Boolean expression or an arithmetic expression. For example, for the Boolean variable b, if the variable is true, split b = b||q; if the variable is false, split b = b&&q, where q may be a Boolean expression or an arithmetic expression.
[0062] Step 34: Locate all state variables in the source code, i.e., "stateVariable" in "attributes". Organize them into a structure and call them through structure member variables, i.e., "MemberAccess" in "attributes". The location of the call is determined by the scope in their "src" attribute. This converts scalars into vectors.
[0063] In step 4, we use the data modification completed in step 3, and hide the internal logic of the original program by using code flattening and opaque predicate techniques. Step 4 can be further divided into:
[0064] Step 41: The control flow graph information of the source code is first input, and then the integrity of the input parameters is checked. If parameters are missing, the following obfuscation steps cannot be performed. If the parameter form is correct, opaque predicates are generated and inserted, and the control flow is flattened. The generation and insertion of opaque predicates and the flattening of the control flow will be performed.
[0065] Step 42: Based on the constructed control flow graph, identify loop statements, branch statements, and sequential statements in the code. Since basic blocks are executed sequentially, control flow flattening can be categorized into three types: global code control flow flattening, function body control flow flattening, and loop or branch basic block flattening. The specific flattening is shown in the following code:
[0066]
[0067] The implementation of global code control flow flattening and function body control flow flattening is the same; both use a switch-case approach. The only difference is that function body control flow flattening involves an additional step of extracting the function shell. Loop or branch block flattening goes a step further, requiring parsing the expressions in the loop or branch statements and flattening based on the truth value of the expressions. If the expression is true, a switch-case branch is generated; if the expression is false, another switch-case branch is generated.
[0068] Step 43: The method for generating opaque predicates is to combine Chebyshev and PWLCM chaotic maps to propose an improved one-dimensional chaotic map (Chebyshev-PWLCM Map, CPM). The main design direction of CPM is to retain the good sensitivity and uncertainty of Chebyshev and PWLCM chaotic maps, while minimizing and eliminating the problems of small chaotic range in Chebyshev chaotic maps and discontinuities in PWLCM chaotic maps, thus increasing the safety, continuity, and uniformity of the improved chaotic map. The definition of the improved chaotic map is as follows:
[0069]
[0070] Given an initial value x0, x1 is iterated from x0, and then x is obtained. n x n ∈[-1, 1], by x n Generate x n+1 μ and p are both control parameters of this chaotic mapping, where μ∈Z * , p∈(0,0.5); F is the iterative formula for this one-dimensional chaotic mapping, when 0.5≤x n When <1, the variable x in the function n =1-x n The remaining parameters μ and p remain unchanged; when μ≥2 and p∈(0,0.5), the CPM chaotic mapping is in a chaotic state; M is a component of the key in this chaotic mapping and is the perturbation parameter of the chaotic mapping. While retaining the good initial value sensitivity and randomness of Chebyshev and PWLCM chaotic mappings, the CPM chaotic mapping also removes the zero-point problem of PWLCM, expands the chaotic range, and increases the security and continuity of the chaotic mapping. Furthermore, CPM further mixes the chaotic trajectories more uniformly, improving the performance of the chaotic mapping.
[0071] Step 44: Inserting an opaque predicate: Step 1: Locate the expression Exp to which the opaque predicate will be inserted and decompose it. The decomposition rule is: if the logical operator lp exists in Exp, decompose it into Exp_1, lp, and Exp_2 with lp as the boundary; if it does not exist, proceed to Step 2; Step 2: if the logical operator lp exists, decompose each subexpression separately. Taking Exp_1 as an example, decompose it into Exp_11, op, and Exp_12 with the comparison operator op as the boundary. If the logical operator lp does not exist, consider the expression Exp as the subexpression Exp_1 and perform the above operation; Step 3: Count the number of subexpressions N and generate the opaque predicate P. The number of always true opaque predicates generated based on CPM chaotic mapping is 2N, and the number of always false opaque predicates is N. For example, P_1 and P_2 are always true opaque predicates, and P_3 is always false opaque predicate. Fourth step: Combine the opaque predicate P with the decomposed expression Exp to construct a new predicate. If the generated predicate is always true opaque, it is Exp&P. If the generated predicate is always false opaque, it is Exp||P. And the predicate is shifted.
[0072] Because P_1 and P_2 are always true opaque predicates and P_3 is always false opaque predicate, the truth value of the new predicate depends entirely on the input value of Exp. Therefore, its truth table is exactly the same as the truth table of Exp, which complicates the expression. On the one hand, it is difficult to locate the insertion position of the opaque predicate in reverse engineering, and on the other hand, it increases the difficulty of cracking the opaque predicate, resulting in better obfuscation effect and increasing the cost of reverse engineering.
[0073] Step 5 utilizes unreadable code, modifies smart contract data fields, and hides the smart contract's internal logic to achieve Solidity source code obfuscation while maintaining functional consistency before and after obfuscation. If steps 2-4 fail, the failed parts are skipped, the next obfuscation step is performed, an error report is returned, and the corresponding Solidity source code is generated. If no failures occur in the above functions, a success message is returned, and the corresponding Solidity source code is generated.
[0074] To verify the effectiveness of the method of this invention, an obfuscated Ethereum smart contract can be generated using this method. First, the original smart contract and the obfuscated smart contract are examined to ensure that the functionality of the smart contract remains unchanged before and after obfuscation. Second, a smart contract reverse engineering tool is used to decompile the smart contracts before and after obfuscation, and the ability of the smart contracts to resist reverse engineering is compared to evaluate the obfuscation effect of the method of this invention.
[0075] Obfuscated smart contracts can provide smart contract developers with test cases containing vulnerabilities of a certain complexity, enabling their developed tools to perform well when faced with complex smart contracts, and also effectively supplementing the amount of data in the smart contract test dataset.
[0076] Based on the same inventive concept, this invention provides a Solidity language source code obfuscation system, comprising: a target construction unit for reading source code and constructing an abstract syntax tree and control flow graph of the source code; a first processing unit for replacing variable names in the source code, disrupting the source code layout, and deleting auxiliary information; a second processing unit for converting variable states in the source code and converting constants into function calls or arithmetic expressions to modify data fields in the smart contract; a third processing unit for flattening the global code control flow, function body control flow, and loop or branch basic blocks using a switch-case approach, and generating and introducing chaotic perpetually true opaque predicates in loop or branch statements based on an improved one-dimensional chaotic mapping CPM to hide the internal logic of the original program; and a target generation unit for determining whether the functionality of the smart contract is consistent before and after obfuscation, and completing the Solidity language source code obfuscation under the premise of functional consistency.
[0077] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of each unit described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here. The division of units is merely a logical functional division, and in actual implementation, there may be other division methods, such as multiple units being combined or integrated into another system.
[0078] Based on the same inventive concept, embodiments of the present invention provide an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the computer program is loaded onto the processor, it implements the Solidity language source code obfuscation method.
[0079] Those skilled in the art will understand that the technical solution of this invention, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause an electronic device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the method described in the embodiments of this invention. The storage medium includes various media capable of storing computer programs, such as a USB flash drive, portable hard drive, read-only memory (ROM), random access memory (RAM), magnetic disk, or optical disk.
[0080] Based on the same inventive concept, embodiments of the present invention also provide a computer-readable storage medium, wherein the computer-readable storage medium may store a program, which, when executed, may include some or all of the steps provided in the embodiments of the present invention.
Claims
1. A Solidity language source code obfuscation method, characterized in that, The method comprises the following steps: Step 1: reading source code, building an abstract syntax tree and a control flow graph of the source code; Step 2: replacing variable names in the source code, disturbing the layout of the source code and deleting auxiliary information; Step 3: converting variable states in the source code, converting constants into function calls or arithmetic expressions to modify data fields in the smart contract; comprising: Step 31: finding all declared local variables in the source code, replacing the found local variables with newly declared global variables; Step 32: finding the positions and values of all type constants in the source code, declaring arrays according to the constant values, and then replacing the constants with corresponding function calls to realize the conversion of static data into dynamically generated data; Step 33: finding all Boolean variables in the source code, if the Boolean variable is true, connecting the subsequent expression with an "or" operator; if the Boolean variable is false, connecting the subsequent expression with an "and" operator; the subsequent expression is a Boolean operation expression or an arithmetic expression, or a combination thereof; Step 34: finding all state variables in the source code, centralizing them in a structure, and calling them through structure member variables, so as to convert scalars to vectors; Step 4: flattening the global code control flow, the control flow in the function body, and the loop or branch basic block through switch-case, and generating and introducing a chaotic always-true opaque predicate based on a combination of Chebyshev and PWLCM one-dimensional chaotic mapping CPM to hide the internal logic of the original program; Step 5: judging whether the functions of the smart contract before and after the obfuscation are consistent, and completing the source code obfuscation of the Solidity language on the premise that the functions are consistent.
2. The Solidity language source code obfuscation method of claim 1, wherein, The step 2 comprises the following steps: Step 21: finding the name and corresponding position of each variable in the source code, and replacing it with a corresponding encrypted value generated by using an encryption algorithm; Step 22: using a regular expression to replace the line feed, space character with'', and replacing the continuous white space with a single white space; Step 23: using a regular expression to replace the single-line comment and multi-line comment in the source code with'', and deleting the auxiliary information in the source code.
3. The Solidity language source code obfuscation method of claim 1, wherein, The code implementation of the global code control flow flattening and the function body control flow flattening in the step 4 is the same, both of which are flattened by switch-case, except that the function body control flow flattening has an additional step of extracting the function shell; the loop or branch basic block flattening further needs to parse the expressions in the loop or branch statement, and perform flattening according to the truth value of the expression, if the truth value of the expression is true, a switch-case branch is generated, if the truth value of the expression is false, another swith-case branch is generated.
4. The Solidity language source code obfuscation method of claim 1, wherein, The definition of the improved one-dimensional chaotic mapping is as follows: Wherein, given an initial value x0, according to x0 iteration x1, then x n , x n ∈[-1,1], x n is generated by x n+1 ; μ, p are control parameters of the chaotic mapping, μ∈Z * , p∈(0,0.5); F is the iteration formula of the one-dimensional chaotic mapping, when 0.5≤x n <1, the variable x n =1-x n in the function, the rest of the parameters μ, p are unchanged; when μ≥2, p∈(0,0.5), the CPM chaotic mapping is in a chaotic state; M belongs to part of the key composition of the chaotic mapping, and is a perturbation parameter of the chaotic mapping.
5. The Solidity language source code obfuscation method of claim 1, wherein, The selected opaque predicate will be inserted into the expression Exp, the structure of which is observed and decomposed according to the rules, and then a number of opaque predicates P are generated according to the decomposition result, the generated opaque predicates and the decomposition result are combined, if the generated opaque predicate is a tautology, Exp&P is generated, if the generated opaque predicate is a contradiction, Exp||P is generated and is translated, a new expression is generated, which increases the complexity of the obfuscation process.
6. The Solidity language source code obfuscation method of claim 1, characterized in that, if there is a failure in steps 2 to 4, the failed part is skipped, the next step of obfuscation is performed, an error report is returned, and the corresponding Solidity source code is generated; if there is no failure, a success message is returned, and the corresponding Solidity source code is generated.
7. A Solidity language source code obfuscation system, characterized in that, It comprises: a target construction unit for reading source code, constructing an abstract syntax tree and a control flow graph of the source code; a first processing unit for replacing variable names in the source code, scrambling the layout of the source code, and deleting auxiliary information; a second processing unit for converting variable states in the source code, converting constants into function calls or arithmetic expressions to modify data fields in the smart contract; comprising: finding all declared local variables in the source code, replacing the found local variables with newly declared global variables; finding the location and value of all type constants in the source code, declaring an array according to the constant value, and then replacing the constant with a corresponding function call to achieve static data conversion to dynamic generation; finding all Boolean variables in the source code, if the Boolean variable is true, it is connected with the subsequent expression using the "or" operator; if the Boolean variable is false, it is connected with the subsequent expression using the "and" operator; the subsequent expression is a Boolean operation expression or an arithmetic expression, or a combination thereof; finding all state variables in the source code, centralizing them in a structure body, and calling them through structure member variables, thereby converting scalars to vectors; a third processing unit for flattening global code control flow, control flow within function body, and loop or branch basic blocks through switch-case, and generating and introducing chaotic tautology opaque predicates based on a one-dimensional chaotic mapping CPM combined with Chebyshev and PWLCM in loop or branch statements to hide the internal logic of the original program; a target generation unit for judging whether the functions of the smart contract before and after obfuscation are consistent, and completing the obfuscation of the Solidity language source code on the premise that the functions are consistent.
8. An electronic device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, The computer program is loaded into the processor to implement the Solidity language source code obfuscation method according to any one of claims 1-6.
9. A computer-readable storage medium storing a computer program, the computer program comprising instructions that, when executed by a computer, cause the computer to perform the method of any one of claims 1 to 8. The computer program is executed by the processor to implement the Solidity language source code obfuscation method according to any one of claims 1-6.
Citation Information
Patent Citations
High-safety biological hash ciphertext voice retrieval method based on feature fusion
CN112883207A
Code obfuscation method based on parent fusion
CN113158147A