Smart contract vulnerability detection method based on the combination of static analysis and fuzz testing

By combining static analysis and fuzzy testing methods, the key paths and function call sequences of smart contracts are extracted and high-quality test cases are generated, which solves the problem of insufficient coverage and accuracy of existing tools, and achieves higher vulnerability detection Effect.

CN117951712BActive Publication Date: 2024-11-05YANTAI UNIV
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202410090931.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-01-23
Publication Date
2024-11-05
Estimated Expiration
2044-01-23

AI Technical Summary

Technical Problem

The existing smart contract vulnerability detection tools have shortcomings in path coverage and vulnerability detection accuracy, especially the fuzzing tools sFuzz, Harvey and ContractFuzzer have low path coverage and low accuracy of vulnerability detection results.

Method used

Using a method based on static analysis and fuzzy testing, the control flowchart of the smart contract is obtained, the critical path and function call sequence are extracted, the initial test cases are generated, and the high coverage mutation test cases are generated through cross-transformation and mutation processing. Testing prophecy traversing the smart contract execution path to detect vulnerabilities.

Benefits of technology

The code coverage and accuracy of vulnerability detection have been improved, with an average accuracy of about 10%, and a code coverage of about 14% higher than existing fuzz testing tools, allowing more vulnerabilities to be detected and improved detection results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117951712B_ABST
    Figure CN117951712B_ABST
Patent Text Reader

Abstract

The present invention relates to the field of network security technology, and specifically to a smart contract vulnerability detection method based on the combination of static analysis and fuzz testing; the vulnerability detection method first extracts a function call sequence based on key variables to guide the generation of test cases that conform to the transaction sequence; then, an initial test case is generated based on the function call sequence, and then the test case is cross-transformed and subjected to mutation processing at the function level and mutation processing at the transaction level to generate high-quality mutation test cases; finally, according to the test prediction, the smart contract execution path is traversed using the mutation test case to obtain the vulnerability detection result; compared with the existing tools, the detection method has a higher code coverage rate, can detect a larger number of vulnerabilities, and has a more accurate detection effect, with an average accuracy rate increased by about 10%, and an average code coverage rate increased by about 14% compared with the current more advanced fuzz testing tools.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of network security technology, and specifically to a smart contract vulnerability detection method based on the combination of static analysis and fuzzy testing. Background Art

[0002] A smart contract is an automated contract that runs on the blockchain and contains logic related to digital assets. In recent years, with the widespread application of digital assets and the continuous maturity of blockchain technology, the number of smart contracts deployed has exploded. This not only highlights that smart contracts are an indispensable part of the blockchain ecosystem, but also makes it more critical to ensure the security of contracts. Similar to traditional programs, smart contracts are inevitably prone to potential vulnerabilities due to negligence in the design and development process. Due to the characteristics of blockchain technology itself, such as the immutability of the contract, once it is deployed on the chain, it cannot be changed and the execution process cannot be interrupted, which means that once the assets are lost, they cannot be recovered. Therefore, it is essential to conduct a comprehensive security review of the contract before going on the chain.

[0003] At present, most vulnerability detection tools use static analysis methods, such as formal verification, symbolic execution, and intermediate representation. Formal verification is based on mathematical techniques, using formal language to transform the concepts, judgments, and reasoning in the contract into formal models, and using mathematical reasoning logic and proofs to perform deterministic verification of the contract. However, this method requires domain experts to participate in modeling, is difficult to use, and has low efficiency. Symbolic execution is a way to discover vulnerability security issues with symbolic values ​​replacing program variables as the main idea. It explores all executable paths in the program by solving a set of path constraints generated by symbolic program input. However, this method is prone to path explosion when processing complex programs, which greatly reduces its efficiency. The intermediate representation is to convert the smart contract source code or bytecode into a form that is easier to analyze and has a higher semantic expression.

[0004] Dynamic analysis is to detect whether there are security issues in a specific program by running it. Fuzz testing is a mainstream dynamic vulnerability detection technology. It is fast and not affected by the internal structure of the target being tested, and has good reusability. It builds a large number of unpredictable inputs and monitors the errors and exceptions that occur during the execution of the program being tested to discover potential vulnerabilities. Many experts have tried to introduce fuzz testing into the field of smart contract vulnerability detection, but the existing fuzz testing tools sFuzz, Harvey and ContractFuzzer have low path coverage and low accuracy of vulnerability detection results. Summary of the invention

[0005] The purpose of the present invention is to provide a smart contract vulnerability detection method based on the combination of static analysis and fuzzy testing, which has high path coverage and high vulnerability detection result accuracy.

[0006] The technical solution of the present invention is as follows:

[0007] A smart contract vulnerability detection method based on the combination of static analysis and fuzz testing includes the following operations:

[0008] S1. Obtain a control flow chart of the smart contract to be inspected, and take the basic block containing the key instructions in the control flow chart as the key block; obtain the path containing the key block in the control flow chart to obtain the key path; all the key paths form a key path set;

[0009] S2, traverse all basic blocks on each critical path in the critical path set, obtain the operand of the PUSH4 instruction in the current basic block, and obtain the current function selector; arrange all function selectors according to the function call order to obtain a function call sequence;

[0010] S3. Based on the input parameter type and input parameter size of the function corresponding to the current function selector in the function call sequence, the current initial test case is obtained; all the initial test cases form an initial test case set; the code and branch coverage of each initial test case in the initial test case set is obtained; in the order of code and branch coverage from large to small, the adjacent initial test cases are cross-interchange processed at the same position to obtain a cross test case group; all the cross test case groups form a cross test case set; in the cross test case set, each cross test case is subjected to mutation processing at the function level and mutation processing at the transaction level to obtain a mutation test case set;

[0011] S4. Based on the test prediction, in the mutation test case set, all mutation test cases traverse the execution path of the smart contract to be tested to obtain vulnerability detection results.

[0012] The test oracle in S4 includes:

[0013] If the smart contract to be checked contains a CALL instruction, and there is gas that satisfies the caller's repeated calls, and the value of the virtual currency sent is greater than zero, and the value of the storage location of the same instruction is changed, then there is a reentrancy vulnerability in the smart contract to be checked;

[0014] In the smart contract to be checked, if, when a current arithmetic instruction is detected, the two most recently executed operands in the stack of the previous arithmetic instruction, the operation result obtained by executing the current arithmetic instruction does not conform to the standard value, and there is no exception handling mechanism in the smart contract to be checked, then there is an integer overflow vulnerability in the smart contract to be checked;

[0015] If during the execution of the smart contract to be checked, the TIMESTAMP opcode or the NUMBER opcode is called, and there is a virtual currency transfer greater than zero, then there is a block state dependency vulnerability in the smart contract to be checked;

[0016] If delegatecall() is called during the execution of the smart contract to be checked, and the parameters passed in by delegatecall() can be exploited by an attacker, then an unsafe delegate call vulnerability exists in the smart contract to be checked;

[0017] If the smart contract to be checked can receive virtual currency, but the smart contract to be checked does not contain a transfer function, then there is a freezing vulnerability in the smart contract to be checked.

[0018] After the operation of S2, the function call sequence is also filtered to obtain a simplified function call sequence for executing the operation in S3; the filtering operation is specifically: deleting the function selectors in the function call sequence whose corresponding functions are not externally accessible and / or whose corresponding functions cannot modify the global variable state, to obtain the simplified function call sequence.

[0019] After the operation of forming the test case set in S3, it also includes performing coverage screening processing on the test case set to obtain an optimized test case set, which is used to execute the operation of obtaining the cross test case set; the operation of the coverage screening processing is specifically: sorting all the test cases in the test case set in order of code and branch coverage from large to small to obtain a sorted test case set; retaining the test cases in the sorted test case set that are in the first number of sorting positions to obtain the optimized test case set.

[0020] After the operation of forming the cross-test case set in S3, it also includes: judging whether the code and branch coverage of the current cross-test case in the cross-test case group exceeds the code and branch coverage of the corresponding initial test case; if exceeded, the current cross-test case is used to form a cross-test case set; if not exceeded, the current cross-test case continues to perform the cross-interchange processing operation.

[0021] The code and branch coverage is obtained based on the pcs value and jumpis value of the test case.

[0022] In S3, the mutation processing at the function level can be achieved by changing the input parameter value of the calling function in the transaction sequence of the cross-test case; the mutation processing at the transaction level can be achieved by changing the transaction information of the cross-test case.

[0023] A smart contract vulnerability detection system based on the combination of static analysis and fuzz testing, including:

[0024] The critical path set generation module is used to obtain the control flow chart of the smart contract to be inspected, and take the basic block containing the key instructions in the control flow chart as the key block; obtain the path containing the key block in the control flow chart to obtain the critical path; all the critical paths form a critical path set;

[0025] A function call sequence generation module is used to traverse all basic blocks on each key path in the key path set, obtain the operand of the PUSH4 instruction in the current basic block, and obtain the current function selector; all function selectors are arranged in a function call order to obtain a function call sequence;

[0026] A mutation test case set generation module is used to obtain the current initial test case based on the input parameter type and input parameter size of the function corresponding to the current function selector in the function call sequence; all the initial test cases form an initial test case set; obtain the code and branch coverage of each initial test case in the initial test case set; cross-exchange the content at the same position of adjacent initial test cases in the order of code and branch coverage from large to small to obtain a cross-test case group; all cross-test case groups form a cross-test case set; in the cross-test case set, each cross-test case is subjected to mutation processing at the function level and mutation processing at the transaction level to obtain a mutation test case set;

[0027] The vulnerability detection result generation module is used to obtain the vulnerability detection result based on the test prediction and the mutation test case set, in which all the mutation test cases traverse the execution path of the smart contract to be tested.

[0028] A smart contract vulnerability detection device based on a combination of static analysis and fuzzy testing includes a processor and a memory, wherein the processor implements the above-mentioned smart contract vulnerability detection method based on a combination of static analysis and fuzzy testing when executing a computer program stored in the memory.

[0029] A computer-readable storage medium for storing a computer program, wherein the computer program, when executed by a processor, implements the above-mentioned smart contract vulnerability detection method based on the combination of static analysis and fuzz testing.

[0030] The beneficial effects of the present invention are:

[0031] The present invention provides a smart contract vulnerability detection method based on the combination of static analysis and fuzz testing. First, a function call sequence is statically extracted based on key variables to guide the generation of test cases that conform to the transaction sequence, so as to explore richer smart contract states; then, an initial test case is generated based on the function call sequence, and then the test case is cross-transformed and subjected to mutation processing at the function level and mutation processing at the transaction level, and high-quality mutation test cases are generated with high coverage as an indicator, which is conducive to exploring more code branches in a limited time and improving the possibility of discovering potential vulnerabilities; finally, according to the test prediction, the smart contract execution path is traversed using the mutation test case to obtain the vulnerability detection result; compared with the existing tools, the detection method has a higher code coverage rate, can detect a larger number of vulnerabilities, and has a more accurate detection effect, with an average accuracy rate increased by about 10%, and an average code coverage rate increased by about 14% compared with the current more advanced fuzzy testing tools. BRIEF DESCRIPTION OF THE DRAWINGS

[0032] By reading the detailed description of the preferred embodiment below, the scheme and advantages of the present application will become clear to those skilled in the art. The accompanying drawings are only for the purpose of illustrating the preferred embodiment and are not to be considered as limiting the present invention.

[0033] In the attached picture:

[0034] Figure 1 : is a schematic diagram of the flow of the vulnerability detection method of this embodiment;

[0035] Figure 2 In the embodiment, a process code diagram of obtaining a critical path set is obtained;

[0036] Figure 3 Schematic diagram of the process of extracting the critical path in the embodiment; (a) is the marked part of the control flow chart; (b) is the critical path; (c) is the operation code content of the key block N1,

[0037] Figure 4 A process code diagram of filtering and processing function call sequences in an embodiment;

[0038] Figure 5 In the embodiment, the method of this embodiment and other detection tools respectively detect the code coverage results of large-scale smart contracts and small-scale smart contracts;

[0039] Figure 6 This is a code diagram of smart contract example 1 in the embodiment;

[0040] Figure 7 This is a code diagram of smart contract example 2 in the embodiment. DETAILED DESCRIPTION

[0041] Exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings.

[0042] This embodiment provides a smart contract vulnerability detection method based on the combination of static analysis and fuzz testing, see Figure 1 , including the following operations:

[0043] S1. Obtain a control flow chart of the smart contract to be inspected, and take the basic block containing the key instructions in the control flow chart as the key block; obtain the path containing the key block in the control flow chart to obtain the key path; all the key paths form a key path set;

[0044] S2, traverse all basic blocks on each critical path in the critical path set, obtain the operand of the PUSH4 instruction in the current basic block, and obtain the current function selector; arrange all function selectors according to the function call order to obtain a function call sequence;

[0045] S3. Based on the input parameter type and input parameter size of the function corresponding to the current function selector in the function call sequence, obtain the current initial test case; obtain the code and branch coverage of each initial test case in the initial test case set; cross-exchange the contents at the same position of adjacent initial test cases in the order of code and branch coverage from large to small to obtain the current cross-test case group; all cross-test case groups form a cross-test case set; in the cross-test case set, each cross-test case is subjected to mutation processing at the function level and mutation processing at the transaction level to obtain a mutation test case set;

[0046] S4. Based on the test prediction, in the mutation test case set, all mutation test cases traverse the execution path of the smart contract to be tested to obtain vulnerability detection results.

[0047] S1. Obtain the control flow chart of the smart contract to be inspected, and take the basic block containing the key instructions in the control flow chart as the key block; obtain the path containing the key block in the control flow chart to obtain the key path; all the key paths form a key path set.

[0048] First, the smart contract to be tested is converted into a control flow graph (CFG). The operation of converting the smart contract into a control flow graph is an existing technology. In order to save space, it will not be described in detail here. Then, the instructions of each basic block in the control flow graph are traversed, and the instructions related to the vulnerability (for example, arithmetic instructions such as CALL, ADD, SUB, MUL, etc. are prone to integer overflow) are taken as key instructions, and the basic blocks containing key instructions are taken as key basic blocks. Then, all the paths in the control flow graph are traversed, and the paths containing key blocks are taken as key paths. Finally, after traversing the control flow graph, all the key paths obtained form a key path set.

[0049] For specific operations on obtaining the critical path set, see Figure 2 The code graph in takes CFG and key instruction set K as input, and finally outputs all paths containing key blocks, that is, the output key path set. Figure 2 The variable Paths is initialized in line 1, the variable I in line 2 represents the instructions contained in the basic block, and the critical blocks are marked in lines 3-11. If the basic block in CFG contains the critical instructions defined in the set K, it is marked as a critical block, and the critical path is extracted in lines 13-20. If the current basic block is a critical block, it is added to the path list, and the depth-first method is used to continue exploring its adjacent basic blocks.

[0050] Figure 3 The process of extracting the critical path is intuitively demonstrated. Figure 3 (a) is a marked partial control flow graph, where the key block is basic block N1; Figure 3 (b) in the figure is to extract the path containing the basic block N1 in the control flow graph, that is, to extract the critical path. Each critical path represents a different situation during the execution of the program, that is, represents a different transaction scenario. Figure 3 (c) in Fig. 1 is the opcode content of key block N1. Each basic block in the critical path contains a set of opcodes. The extraction of the critical path reduces the information irrelevant to vulnerability detection to a certain extent.

[0051] S2. Traverse all basic blocks on each critical path in the critical path set, obtain the operand of the PUSH4 instruction in the current basic block, and obtain the current function selector; all function selectors are arranged in the function call order to obtain the function call sequence.

[0052] Traverse the critical path set, each instruction of all basic blocks on each critical path, use the operand in the PUSH4 instruction (an instruction that pushes a 4-byte constant value onto the stack) as a function selector to show which interface the user called during the execution of the smart contract. Use the function selector to generate test cases to ensure that the test cases meet the transaction order. Arrange all function selectors obtained after traversing all basic blocks according to the order of the corresponding functions in the function call sequence to obtain the function call sequence. The function call order is determined by the function call-related instructions of the opcode.

[0053] Not all functions can be targeted by attackers. To improve the efficiency of vulnerability detection, the function call sequence is filtered to obtain a simplified function call sequence for executing operations in S3.

[0054] The filtering operation is specifically as follows: deleting function selectors in the function call sequence whose corresponding functions are not externally accessible and / or whose corresponding functions cannot modify the state of global variables, to obtain a simplified function call sequence.

[0055] Specifically, the Abstract Syntax Tree (AST) of the smart contract to be tested is obtained, and the function properties corresponding to each function selector are analyzed by traversing the abstract syntax tree to determine whether the function is externally accessible and whether it can modify the state of global variables. If the function is externally accessible, attackers can use it. If the function can modify the state of global variables, attackers can use the function to change the internal state of the smart contract memory and damage the value of the contract. Therefore, in order to generate high-quality test cases, the function selectors in the function call sequence whose corresponding functions are externally accessible and / or whose corresponding functions can modify the state of global variables should be retained. On the contrary, the function selectors whose corresponding functions are not externally accessible and / or whose corresponding functions cannot modify the state of global variables should be deleted, because the functions corresponding to these function selectors are relatively safe. If they are used to generate test cases, it will only increase the amount of calculation and reduce the detection efficiency.

[0056] The specific process of filtering and processing function call sequences can be found in Figure 4 . Set F stores modifiers that represent filterable semantics (modifiers corresponding to functions that are not externally accessible and / or that cannot modify the state of global variables). Use sha3_256() to calculate the hash value of the PUSH4 instruction function signature in each basic block in the critical path, and use the first four bytes of the PUSH4 instruction as the function selector stored in set S. Take all the extracted critical paths, set S, and the ABI file (Application Binary Interface) of the smart contract as input to finally generate a function call sequence list. First, Figure 4In the code, all function selectors set to S in line 2 are initialized to F filtered_selectors, and line 3 initializes an empty list Sequences to store the filtered function call sequence. Next, the first loop from line 4 to line 7 filters functions according to modifiers with special meanings. Line 5 is used to evaluate whether the relevant parameter values ​​in the ABI of this function belong to the set F. Finally, the second loop from line 8 to line 16 executes the function of extracting the function call sequence from the input P path. OpC and OpN represent the opcode and operand of the instruction in the path node, respectively. If OpC is PUSH4 and its corresponding operand exists in F filtered_selectors, the operand, that is, the function selector, will be added to the function call sequence.

[0057] S3. Based on the function input parameter type and input parameter size corresponding to the current function selector in the function call sequence, the current initial test case is obtained; all the initial test cases form an initial test case set; the code and branch coverage of each initial test case in the initial test case set is obtained; in the order of code and branch coverage from large to small, the adjacent initial test cases are cross-interchange processed at the same position to obtain the current cross-test case group; all the cross-test case groups form a cross-test case set; in the cross-test case set, each cross-test case is subjected to mutation processing at the function level and mutation processing at the transaction level to obtain a mutation test case set.

[0058] First, the input parameter type and input parameter size of each function selector in the function call sequence are input into the fuzzifier to obtain a set of initial test cases corresponding to each function selector; after all function selectors are processed by the fuzzifier, all the initial test cases obtained form the initial test case set. The initial test case set forms a seed pool, and each seed is an initial test case.

[0059] To obtain high-quality test cases and improve the accuracy of vulnerability detection. After the operation of forming the initial test case set, it also includes coverage screening processing of the initial test case set to obtain an optimized test case set, which is used to perform the operation of obtaining a cross test case set. The coverage screening operation is: sort all the initial test cases in the initial test case set in order from large to small code and branch coverage to obtain a sorted initial test case set; retain the initial test cases in the sorted initial test case set that are in the first position to obtain an optimized test case set.

[0060] Then, the code and branch coverage of each initial test case or optimized test case in the initial test case set or the optimized test case set is obtained; in the order of code and branch coverage from large to small, the adjacent initial test cases or optimized test cases are cross-interchange processed at the same position to obtain the current cross-test case group or the current improved cross-test case group; all cross-test case groups or the current improved cross-test case groups form a cross-test case set or an improved cross-test case, respectively.

[0061] Specifically, taking the initial test case corresponding to the maximum code and branch coverage as an example, recorded as the current initial test case, at the same selected position, the current initial test case and the adjacent initial test case (the initial test case whose code and branch coverage is only less than the current initial test case) are respectively divided into two parts to obtain the first part of the current initial test case, the second part of the current initial test case, the first part of the adjacent initial test case, and the second part of the adjacent initial test case; the first part of the current initial test case is spliced ​​with the second part of the adjacent initial test case, and the first part of the adjacent initial test case is spliced ​​with the second part of the current initial test case to obtain the first cross test case and the second cross test case, and these two cross test cases form the current cross test case group; after all test cases are cross-interchange processed, all cross test case groups obtained form a cross test case set.

[0062] Alternatively, the initial test case corresponding to the maximum code and branch coverage is taken as an example, which is recorded as the current initial test case. First, at the same selected position, the current initial test case, the adjacent first initial test case (the initial test case whose code and branch coverage is only less than that of the current initial test case), and the adjacent second initial test case (the initial test case whose code and branch coverage is only less than that of the current initial test case and the adjacent first initial test case) are respectively divided into two parts to obtain the first part of the current initial test case, the second part of the current initial test case, the first part of the adjacent first initial test case, the second part of the adjacent first initial test case, the first part of the adjacent second initial test case, and the second part of the adjacent second initial test case; then, the first part of the current initial test case and the adjacent first initial test case are separated. The second part of the trial case is spliced, the first part of the adjacent first initial test case is spliced ​​with the second part of the adjacent second initial test case, the first part of the adjacent second initial test case is spliced ​​with the second part of the current initial test case, or the first part of the current initial test case is spliced ​​with the second part of the adjacent second initial test case, the first part of the adjacent first initial test case is spliced ​​with the current initial test case, and the first part of the adjacent second initial test case is spliced ​​with the second part of the adjacent first initial test case to obtain a cross test case group containing 3 cross test cases; after all test cases are cross-interchange processed, all cross test case groups obtained form a cross test case set.

[0063] In this embodiment, the number of adjacent initial test cases for cross-exchange processing is not limited to the 2 or 3 mentioned above, and can be adjusted according to actual needs.

[0064] The above code and branch coverage is based on the pcs value and jumpis value of the test case. The two indicators of pcs and jumpis are used to record the number of instructions and branches covered during the execution of the seed (test case), and the code and branch coverage is calculated after the execution. The appropriate test case is selected for the next operation based on pcs and jumpis.

[0065] In order to increase the number of cross-test cases and further improve the accuracy of vulnerability detection. After obtaining the cross-test case set, it also includes: judging whether the code and branch coverage of the current cross-test case in the cross-test case group exceeds the code and branch coverage of the corresponding initial test case; if it exceeds, the current cross-test case is used to form a cross-test case set; if it does not exceed, the current cross-test case continues to perform the cross-interchange processing operation. This ensures that each cross-test case output can meet the standard for generating mutation test cases, ensuring that each mutation test case obtained has a high code and branch coverage.

[0066] Finally, in order to increase the diversity of test cases, in the cross test case set, each cross test case undergoes mutation processing at the function level and mutation processing at the transaction level to obtain a mutation test case set.

[0067] Among them, the mutation processing at the function level can be implemented by changing the input parameter value of the function called in the transaction sequence of the cross-test case. The operation of changing the input parameter value of the function called in the transaction sequence of the cross-test case can be achieved by replacing the input parameter value with a random value, or using the bit flipping method to process the input parameter value. The mutation processing at the transaction level can be implemented by changing the transaction information of the cross-test case, such as changing the transaction sender address, transaction amount, gas price and gas upper limit information in the cross-test case. The operation of changing the input parameter value of the function called in the transaction sequence of the cross-test case can be achieved by replacing the transaction information with a random value, or using the bit flipping method to process the transaction information.

[0068] S4. Based on the test prediction, all mutation test cases in the mutation test case set traverse the execution path of the smart contract to be tested to obtain the vulnerability detection results.

[0069] Use all the mutation test cases in the mutation test case set to traverse the execution path of the smart contract to be tested. When the execution logic of the execution path meets the logical rules of the test prediction, the smart contract to be tested has a vulnerability.

[0070] Test oracles include:

[0071] Reentrancy vulnerability (hereinafter referred to as RE): If there is a CALL instruction in the smart contract to be checked, and there is gas to satisfy the caller's repeated calls, and the virtual currency value sent is greater than zero, and the value of the same variable storage location is changed, then there is a reentrancy vulnerability in the smart contract to be checked.

[0072] Reentrancy vulnerabilities in smart contracts usually refer to the situation where an attacker causes the contract state to be inconsistent or produces unexpected results by repeatedly calling a function. Therefore, the test oracle for reentrancy vulnerabilities is defined as follows: ExistCALL∧ReeStore. The first sub-oracle ExistCALL is used to check the following three conditions: (1) whether there is an instruction CALL call; (2) whether there is enough gas to satisfy the caller's repeated calls; and (3) whether the sent virtual currency is greater than zero. The second sub-oracle ReeStore is used to determine whether the value of the same variable storage location has been changed. For example, during the detection process, the SLOAD instruction is first detected, and its current storage location is recorded. Then, it is checked whether there is an SSTORE storage operation after the CALL operation, and whether the SSTORE storage location is the same as the storage location read by the previous SLOAD. If the same, the value of the storage location of the SLOAD instruction will be changed, because the SSTORE instruction can change the internal state of the memory. When it changes the value of the same location, it will cause the contract to have different states before and after.

[0073] When the two sub-prophecies ExistCALL and ReeStore are both true, that is, there is a CALL instruction in the smart contract to be tested, and there is gas to satisfy the caller's repeated calls, and the virtual currency value sent is greater than zero, and the value of the same variable storage location is changed, then the smart contract to be tested has a reentrancy vulnerability.

[0074] Integer overflow vulnerability (hereinafter detectable OF). In the smart contract to be tested, if when the current arithmetic instruction is detected, the two most recently executed operands in the stack of the previous arithmetic instruction, the calculation result obtained by executing the current arithmetic instruction does not match the standard value, and there is no exception handling mechanism in the smart contract to be tested, then there is an integer overflow vulnerability in the smart contract to be tested.

[0075] In smart contracts, integer variables are generally defined as fixed-size, unsigned integer types, which means that integer variables can only have values ​​within a certain range. When the result of an arithmetic operation exceeds this range, an integer overflow or integer underflow occurs, so the test prediction for integer overflow vulnerabilities is defined as follows: ExistArith∧ErrorResult∧NoExceptiont.

[0076] When an arithmetic instruction is detected, such as ADD, MUL, and MUL, the two most recently executed operands are obtained from the stack of the previous instruction, and the operation corresponding to the current instruction is performed. If the operation result does not meet the expectation (standard value) and there is no exception handling mechanism set in the smart contract (for example, require assertion is not used), the smart contract to be checked has an integer overflow.

[0077] Block state dependency vulnerability (hereinafter referred to as BD). If the TIMESTAMP opcode or NUMBER opcode is called during the execution of the smart contract to be tested, and there is a virtual currency transfer greater than zero, then a block state dependency vulnerability exists in the smart contract to be tested.

[0078] The operations involving virtual currency transfer in smart contracts are closely related to the properties of the block state, so the test prediction for the block state dependency vulnerability is defined as follows: (TimestampOp∨BlockNumberOp)∧TransferEther.

[0079] TimestampOp and BlockNumberOp are used to determine whether the TIMESTAMP and NUMBER opcodes are called during the execution of the contract. The third sub-prophecy TransferEther determines whether there is a virtual currency transfer greater than zero. When the TIMESTAMP opcode or NUMBER opcode is called during the execution of the smart contract to be checked, and there is a virtual currency transfer greater than zero, it is considered that there are block-related operations in the smart contract to be checked, and the transfer operation is involved in its execution process. The smart contract to be checked has a block state dependency vulnerability.

[0080] Unsafe delegate call vulnerability (hereinafter referred to as UC). If delegatecall() is called during the execution of the smart contract to be checked, and the parameters passed in by delegatecall() can be exploited by attackers, then an unsafe delegate call vulnerability exists in the smart contract to be checked.

[0081] The test prophecy for the unsafe delegate call vulnerability is defined as ExistDelegateCALL∧OrAttacker. The two sub-prophecies ExistDelegateCALL and OrAttacker respectively check whether delegatecall() is called during the execution of the smart contract, and whether the parameters passed in delegatecall() can be exploited by the attacker. If delegatecall() is called during the execution of the smart contract to be tested, and the parameters passed in delegatecall() can be exploited by the attacker, then the smart contract to be tested has an unsafe delegate call vulnerability. For example, an attacker can control the victim contract to call an external malicious function through msg.data to modify the state of the current contract.

[0082] Freezing vulnerability (hereinafter referred to as EF). If the smart contract to be tested can receive virtual currency, but the smart contract to be tested does not contain a transfer function, then there is a freezing vulnerability in the smart contract to be tested.

[0083] The test prediction for the freezing vulnerability is defined as CanReceive∧NotTransfer. CanReceive is used to check whether the contract can receive virtual currency, and NotTransfer is used to check whether the contract contains a transfer function. The operation code called during the execution of the smart contract to be tested is used to determine whether it can transfer money. When the smart contract to be tested can receive virtual currency, but the smart contract to be tested does not contain a transfer function, it is considered that the smart contract to be tested has a freezing vulnerability.

[0084] In order to verify the effect of the vulnerability detection method (hereinafter referred to as CSAFuzzer) provided in this embodiment, the following experiment was conducted.

[0085] The experiments were conducted to answer the following three questions about the performance of CSA Fuzzer:

[0086] Question 1: Can CSAFuzzer achieve higher code coverage than existing tools?

[0087] Question 2: Can CSAFuzzer detect smart contract vulnerabilities more effectively than existing tools?

[0088] Question 3: Can CSAFuzzer distinguish between vulnerabilities that appear to be vulnerable and those that are actually vulnerable?

[0089] Experimental environment. The experiment runs CSAFuzzer with python on Ubuntu 20.04, and the virtual machine used is equipped with an 8-core processor and 4GB of memory. CSAFuzzer is compared with the existing tools listed in Table 1. These existing tools all use mainstream vulnerability detection methods and have been made public on GitHub. For static analysis tools, three static analysis tools, Oyente, Osiris, and Slither, were selected. The first two use the symbolic execution method (hereinafter referred to as Symbolic) and the latter uses the intermediate representation method (hereinafter referred to as IR). The fuzz testing tool sFuzz, which has been popular in recent years, was also selected. In the experiment, the code coverage and effectiveness of CSAFuzzer and existing tools were compared under the same configuration environment. The tools involved in the experiment, their respective detection vulnerability types, and the methods used are shown in Table 1.

[0090] Table 1 Summary of different tool types and detected vulnerability types

[0091]

[0092] Dataset. In the experiment, two datasets of different numbers are used to demonstrate the experimental effect of CSAFuzzer. The purpose of Dataset 1 is to test its coverage of contract code. Dataset 1 selects 15,000 smart contracts that have been made public by previous scholars. They are all real-world smart contracts from Etherscan and their source code is public. After screening out the smart contracts that cannot be compiled, a total of 12,351 smart contracts were obtained. Considering the impact of the scale of smart contracts on code coverage, smart contracts are classified according to their scale. Among them, there are 2,285 large-scale smart contracts (greater than 500 lines), which usually involve complex code paths, branches and execution logic, and 10,066 small-scale smart contracts (less than or equal to 500 lines). The purpose of Dataset 2 is to test the effect of CSAFuzzer on smart contract vulnerability detection. The experiment selected 505 contracts from Etherscan for comprehensive manual labeling and testing. The experiment focuses on five vulnerabilities in the final result report, namely block state dependency vulnerability BD, reentrancy vulnerability RE, integer overflow vulnerability OF, unsafe delegate call vulnerability UC and freezing vulnerability FE. After counting the detection results of each tool, all smart contract detection results are checked and analyzed in combination with vulnerability labels to ensure that both missed and false positives are taken into account.

[0093] Answer to question 1 about code coverage. For experimental results, see Figure 5 ,Since the intermediate representation method usually does not use coverage as an ,evaluation metric, only the existing tools Oyente, Osiris, and sFuzz are compared with CSAFuzzer.,In the experiment, the running time of CSAFuzzer and sFuzz is set to 60s, and the number of ,instructions covered by the test cases generated by CSAFuzzer is recorded and ,compared with this metric of the existing tools, and the running results are counted and the ,average is calculated. Figure 5 What is shown is the calculation status of each tool for smart contracts of different sizes, where complex represents large-scale smart contracts and simple represents small-scale smart contracts.

[0094] Figure 5 The experimental results show that CSAFuzzer's code coverage for small-scale smart contracts and large-scale smart contracts reached 84.20% and 78.72% respectively. Obviously, compared with sFuzz, which is also a fuzz testing tool, CSAFuzzer's code coverage for large-scale and small-scale smart contracts is 15.83% (large-scale smart contracts) and 12.75% (small-scale smart contracts) higher respectively; compared with Osiris and Oyente, CSAFuzzer is 3.76% and 2.63% higher in large-scale contracts.

[0095] In particular, for small-scale smart contracts, the branches covered by CSAFuzzer are about 2% lower than those of Osiris. As one of the advanced symbolic execution detection tools, Osiris can theoretically achieve more comprehensive path coverage by symbolically calculating the program path and assigning arbitrary values ​​to state variables (if special constraints such as initial values ​​are not considered) to traverse multiple branches in the path. However, when smart contracts are actually executed, not all paths are executable. In other words, in real scenarios, there may be some infeasible paths in Osiris's high coverage results. For example, Figure 6 Given smart contract example 1, for smart contract example 1 FalseOverflow, Osiris' code coverage reached 99.2%, and reported that it contained an integer overflow vulnerability. According to the report, the integer overflow vulnerability occurred in lines 7-8. But in fact, since there is no function in the smart contract to set or change the value of balances[msg.sender], the condition in the require statement on line 6 cannot be met, and Osiris tends to directly assign an arbitrary value to balances[msg.sender] to meet the conditional constraints, thereby covering paths that have no practical significance, which also affects the correctness of the vulnerability detection results.

[0096] In general, CSAFuzzer improves the code coverage of contracts to a great extent, which can be attributed to the use of static analysis methods to obtain more accurate function call sequences and the application of an improved genetic algorithm guided by coverage.

[0097] Answer to the effectiveness in question 2. According to the reported vulnerabilities of each tool detecting the same data set, the accuracy of its detection is calculated in the experiment and summarized in Table 2. The accuracy can show the correctness of the overall sample. From Table 2, we can observe that compared with the three static analysis tools, CSAFuzzer can detect more types of vulnerabilities, and for all five vulnerabilities, CSAFuzzer also achieves higher accuracy. For example, for block state dependency vulnerabilities (BD), CSAFuzzer is 17.23%, 12.48%, 12.28% and 12.08% higher than Slither, Oyente, Osiris and sFuzz respectively. At the same time, CSAFuzzer's detection effect on reentrancy vulnerabilities and integer overflow vulnerabilities is particularly obvious. Compared with some other tools, for example, Slither has the lowest accuracy for RE detection, which is only 55.25%, and Oyente has the lowest accuracy for IO detection, which is only 64.16%, while CSAFuzzer is 42.77% and 34.65% higher than Slither and Oyente in these two aspects respectively.

[0098] Table 2 Comparison of the accuracy of different tools. “-” indicates that this tool cannot detect this vulnerability.

[0099]

[0100] Overall, CSAFuzzer can detect smart contract vulnerabilities more effectively than other existing tools (with an average accuracy improvement of about 10%). This is because CSAFuzzer combines static analysis and guided genetic algorithms to improve code coverage, so that it can capture more vulnerabilities, and CSAFuzzer designs better test predictions to reduce false reports.

[0101] Answer to question 3 about true positives and false positives. This section will discuss whether CSAFuzzer can effectively detect seemingly vulnerable and actually vulnerable contract vulnerabilities, that is, the false positive (FP) and true positive (TP) levels of the vulnerability results. At the same time, some false positive cases are analyzed and explained. The experiment manually reviewed the smart contracts in Dataset 2 and calculated the number of vulnerabilities identified by each tool. Table 3 shows the number of vulnerabilities found by different tools, as well as the corresponding true positives and false positives. Total represents the number of true positive vulnerabilities reported by each tool.

[0102] Table 3 True positives and false positives detected by different tools for five vulnerabilities

[0103]

[0104] The experimental results in Table 3 show that the three static analysis methods (Oyente, Osiris, and Slither) have a high number of false positives for vulnerabilities. In particular, for integer overflow vulnerabilities, Oyente and Osiris have 176 and 114 false positives, respectively. After analysis, we believe that there are two reasons for this: on the one hand, they define the scope of vulnerabilities too large and do not consider the processing of special types of variables such as strings and arrays; on the other hand, as mentioned above, symbolic execution may cover the defect of infeasible paths, which will also affect the effect of contract vulnerability detection. Similarly, Figure 7 In the smart contract example 2 shown, the detection results of Oyente and Osiris are both false positives, because the value of balances cannot be initialized, which causes the judgment statement in line 6 to never be satisfied, so the subsequent code cannot be executed. This means that this contract will not overflow. For reentrancy vulnerabilities, Slither has the most serious false positives, with more than 200 false positives. It can be considered that Slither uses a more relaxed mode to capture as many vulnerabilities as possible, but at the same time it has to pay the price of more serious false positives. Figure 7The following is a snippet from the contract Send_or_Transfer that was misreported by Slither. The user can call the _withdraw() function to withdraw funds through transfer(). Slither ignores the gas restrictions of the send() and transfer() functions themselves, resulting in a misreport.

[0105] At the same time, it can be found in Table 3 that both sFuzz and CSAFuzzer use dynamic detection methods, which greatly reduces the false positive rate of various vulnerabilities. However, it is worth noting that the final total number of true positive vulnerabilities shows that sFuzz has a large degree of underreporting. For example, for block state dependency vulnerabilities, sFuzz only reported 29, which is 51 less than CSAFuzzer. It is speculated that this may be because it tends to randomly generate transaction sequences, resulting in the inability to trigger vulnerabilities hidden in deeper states.

[0106] Overall, CSAFuzzer reported 166 true positives, twice the number detected by sFuzz, also a fuzz testing tool. CSAFuzzer's better vulnerability detection results come from its integration of three key technologies. First, CSAFuzzer obtains a more accurate function call sequence through static analysis, which can explore deeper and richer contract states when fuzzing. Second, CSAFuzzer uses coverage as an optimization indicator, and continuously selects higher-quality test cases during seed iteration to achieve the goal of covering more branches; third, CSAFuzzer designs more complete test predictions based on the characteristics of the vulnerability. They can complement each other to detect contract vulnerabilities more accurately.

[0107] In summary, CSAFuzzer can effectively detect smart contract vulnerabilities that appear to be vulnerable and are actually vulnerable.

[0108] This embodiment also provides a smart contract vulnerability detection system based on a combination of static analysis and fuzz testing, including:

[0109] The critical path set generation module is used to obtain the control flow chart of the smart contract to be inspected, and take the basic block containing the key instructions in the control flow chart as the key block; obtain the path containing the key block in the control flow chart to obtain the critical path; all the critical paths form a critical path set;

[0110] The function call sequence generation module is used to traverse all basic blocks on each critical path in the critical path set, obtain the operand of the PUSH4 instruction in the current basic block, and obtain the current function selector; all function selectors are arranged in the function call order to obtain the function call sequence;

[0111] The mutation test case set generation module is used to obtain the current test case based on the function input parameter type and function size corresponding to the current function selector in the function call sequence; all test cases form a test case set; in the test case set, the current test case and any other test case are cross-interchange processed at the same position to obtain the current cross test case group; all cross test case groups form a cross test case set; in the cross test case set, each cross test case is subjected to mutation processing at the function level and mutation processing at the transaction level to obtain a mutation test case set;

[0112] The vulnerability detection result generation module is used to obtain the vulnerability detection results based on the test prediction and the mutation test case set. All mutation test cases traverse the execution path of the smart contract to be tested.

[0113] This embodiment also provides a smart contract vulnerability detection device based on the combination of static analysis and fuzzy testing, including a processor and a memory, wherein the processor implements the above-mentioned smart contract vulnerability detection method based on the combination of static analysis and fuzzy testing when executing a computer program stored in the memory.

[0114] This embodiment also provides a computer-readable storage medium for storing a computer program, wherein when the computer program is executed by a processor, the above-mentioned smart contract vulnerability detection method based on the combination of static analysis and fuzzy testing is implemented.

[0115] The present embodiment provides a smart contract vulnerability detection method based on the combination of static analysis and fuzz testing. First, a function call sequence is statically extracted based on key variables to guide the generation of test cases that conform to the transaction sequence, thereby exploring a richer state of the smart contract; then, an initial test case is generated based on the function call sequence, and then the test case is cross-transformed and subjected to mutation processing at the function level and mutation processing at the transaction level, and high-quality mutation test cases are generated with high coverage as an indicator, which is conducive to exploring more code branches in a limited time and improving the possibility of discovering potential vulnerabilities; finally, according to the test prediction, the smart contract execution path is traversed using mutation test cases to obtain vulnerability detection results; compared with existing tools, the detection method has a higher code coverage rate, can detect a larger number of vulnerabilities, and has a more accurate detection effect, with an average accuracy rate increased by about 10%, and an average code coverage rate increased by about 14% compared with the current more advanced fuzz testing tools.

Claims

1. A smart contract vulnerability detection method based on the combination of static analysis and fuzz testing, characterized in that: The following operations are included: S1. Obtain a control flow chart of the smart contract to be inspected, and use a basic block containing a key instruction in the control flow chart as a key block; obtain a path containing the key block in the control flow chart to obtain a key path; All critical paths form a critical path set; S2, traverse all basic blocks on each critical path in the critical path set, obtain the operand of the PUSH4 instruction in the current basic block, and obtain the current function selector; arrange all function selectors according to the function call order to obtain a function call sequence; S3, obtaining a current initial test case based on the input parameter type and input parameter size of the function corresponding to the current function selector in the function call sequence; All initial test cases form the initial test case set; Obtaining code and branch coverage of each initial test case in the initial test case set; According to the order of code and branch coverage from large to small, the adjacent initial test cases are cross-interchange processed at the same position to obtain a cross-test case group; all cross-test case groups form a cross-test case set; In the cross test case set, each cross test case is subjected to mutation processing at the function level and mutation processing at the transaction level to obtain a mutation test case set; S4. Based on the test prediction, in the mutation test case set, all mutation test cases traverse the execution path of the smart contract to be tested to obtain vulnerability detection results; The test oracle in S4 includes: If the smart contract to be checked contains a CALL instruction, and there is gas that satisfies the caller's repeated calls, and the value of the virtual currency sent is greater than zero, and the value of the storage location of the same instruction is changed, then there is a reentrancy vulnerability in the smart contract to be checked; In the smart contract to be checked, if, when a current arithmetic instruction is detected, the two most recently executed operands in the stack of the previous arithmetic instruction, the operation result obtained by executing the current arithmetic instruction does not conform to the standard value, and there is no exception handling mechanism in the smart contract to be checked, then there is an integer overflow vulnerability in the smart contract to be checked; If during the execution of the smart contract to be checked, the TIMESTAMP opcode or the NUMBER opcode is called, and there is a virtual currency transfer greater than zero, then there is a block state dependency vulnerability in the smart contract to be checked; If delegatecall() is called during the execution of the smart contract to be checked, and the parameters passed in by delegatecall() can be exploited by an attacker, then an unsafe delegate call vulnerability exists in the smart contract to be checked; If the smart contract to be checked can receive virtual currency, but the smart contract to be checked does not contain a transfer function, then there is a freezing vulnerability in the smart contract to be checked.

2. The smart contract vulnerability detection method based on the combination of static analysis and fuzz testing according to claim 1 is characterized in that: After the operation in S2, the function call sequence is filtered to obtain a simplified function call sequence for executing the operation in S3; The filtering process is specifically performed as follows: deleting function selectors in the function call sequence whose corresponding functions are not externally accessible and / or whose corresponding functions cannot modify the state of global variables, to obtain the simplified function call sequence.

3. The smart contract vulnerability detection method based on the combination of static analysis and fuzzy testing according to claim 1 is characterized in that: After the operation of forming the initial test case set in S3, the process further includes performing coverage screening processing on the initial test case set to obtain an optimized test case set for executing the operation of forming the cross test case set; The operation of the coverage screening process is specifically as follows: sorting all the initial test cases in the initial test case set in descending order of code and branch coverage to obtain a sorted initial test case set; The initial test cases whose sorting positions are in the front of the first number in the sorted initial test case set are retained to obtain the optimized test case set.

4. The smart contract vulnerability detection method based on the combination of static analysis and fuzz testing according to claim 1 is characterized in that: After the operation of obtaining the cross-test case group in S3, the following steps are also included: Determine whether the code and branch coverage of the current cross test case in the cross test case group exceeds the code and branch coverage of the corresponding initial test case; If it exceeds, the current cross test case is used to form a cross test case set; If not exceeded, the current cross-test case continues to perform cross-interchange processing operations.

5. The smart contract vulnerability detection method based on the combination of static analysis and fuzzy testing according to claim 3 or 4 is characterized in that: The code and branch coverage is obtained based on the pcs value and jumpis value of the test case.

6. The smart contract vulnerability detection method based on the combination of static analysis and fuzz testing according to claim 1 is characterized in that: In S3, The mutation processing at the function level can be achieved by changing the input parameter value of the calling function in the transaction sequence of the cross test case; The transaction-level mutation processing can be achieved by changing the transaction information of the cross-test case.

7. A smart contract vulnerability detection system based on the combination of static analysis and fuzz testing, characterized in that: include: A critical path set generation module is used to obtain a control flow graph of the smart contract to be inspected, and take the basic blocks containing key instructions in the control flow graph as key blocks; Obtaining a path including the key block in the control flow graph to obtain a key path; All critical paths form a critical path set; A function call sequence generation module is used to traverse all basic blocks on each key path in the key path set, obtain the operand of the PUSH4 instruction in the current basic block, and obtain the current function selector; all function selectors are arranged in a function call order to obtain a function call sequence; A mutation test case set generation module, used to obtain a current initial test case based on the input parameter type and input parameter size of the function corresponding to the current function selector in the function call sequence; All initial test cases form an initial test case set; obtain the code and branch coverage of each initial test case in the initial test case set; In the order of code and branch coverage from large to small, adjacent initial test cases are cross-interchange processed at the same position to obtain the current cross-test case group; all cross-test case groups form a cross-test case set; in the cross-test case set, each cross-test case is subjected to mutation processing at the function level and mutation processing at the transaction level to obtain a mutation test case set; A vulnerability detection result generation module is used to obtain vulnerability detection results based on the test prediction, in the mutation test case set, all mutation test cases traverse the execution path of the smart contract to be tested; The test prediction in the vulnerability detection result generation module includes: if there is a CALL instruction in the smart contract to be tested, and there is gas that satisfies the repeated calls of the caller, and the sent virtual currency value is greater than zero, and the value of the same instruction storage location is changed, then there is a reentrancy vulnerability in the smart contract to be tested; In the smart contract to be checked, if when the current arithmetic instruction is detected, the two most recently executed operands in the stack of the previous arithmetic instruction, the calculation result obtained by executing the current arithmetic instruction does not conform to the standard value, and there is no exception handling mechanism in the smart contract to be checked, then there is an integer overflow vulnerability in the smart contract to be checked; if during the execution of the smart contract to be checked, the TIMESTAMP operation code or the NUMBER operation code is called, and there is a virtual currency transfer greater than zero, then there is a block state dependency vulnerability in the smart contract to be checked; if during the execution of the smart contract to be checked, delegatecall() is called, and the parameters passed in by delegatecall() can be exploited by attackers, then there is an unsafe delegate call vulnerability in the smart contract to be checked; if the smart contract to be checked can receive virtual currency, but the smart contract to be checked does not contain a transfer function, then there is a freezing vulnerability in the smart contract to be checked.

8. A smart contract vulnerability detection device based on the combination of static analysis and fuzz testing, characterized in that: It includes a processor and a memory, wherein when the processor executes the computer program stored in the memory, it implements the smart contract vulnerability detection method based on the combination of static analysis and fuzzy testing as described in any one of claims 1 to 6.

9. A computer-readable storage medium, characterized in that: Used to store a computer program, wherein when the computer program is executed by a processor, the smart contract vulnerability detection method based on the combination of static analysis and fuzzy testing as described in any one of claims 1 to 6 is implemented.

Citation Information

Cited By

  • Transaction sequence dependency vulnerability detection system and method based on function dependency analysis

    CN120372620A