Smart contract vulnerability detection method and device based on execution path and stack events
Through a smart contract vulnerability detection method based on execution paths and stack events, the deficiencies in code coverage and accuracy of existing tools are solved. Through dynamic analysis and symbolic execution processing, high accuracy and low false positive rate vulnerabilities are achieved. Detection effect.
Patent Information
- Application Number
- CN202410749158.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-12
- Publication Date
- 2024-09-13
- Estimated Expiration
- 2044-06-12
AI Technical Summary
Existing smart contract vulnerability detection tools have deficiencies in code coverage and accuracy. In particular, the detection of complex contracts and integer overflow vulnerabilities has false positives and false positives.
Adopt a detection method based on execution paths and stack events, obtain all execution paths through the control flow graph, simulate the execution of EVM instructions to record stack events, perform dynamic analysis based on parameter properties and instruction properties, and use symbolic execution to process potential vulnerability paths to detect integer overflows, Reentrancy and denial of service vulnerabilities.
It improves the accuracy and recall rate of vulnerability detection, reduces the false positive rate, increases the F1 score, and significantly improves the ability to detect smart contract vulnerabilities.
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of electronic digital data processing, and in particular to a method and device for detecting smart contract vulnerabilities based on execution paths and stack events. Background Art
[0002] The immutability of blockchain means that once smart contracts are deployed, they cannot be changed. In addition, the instability of smart contract programming languages and the complexity of their functional logic pose a serious threat to contract security. In the financial field, smart contracts control a large amount of digital assets, which gives hackers a strong motivation to attack contracts for illegal gains. Therefore, it is crucial to conduct effective vulnerability checks before smart contracts are deployed.
[0003] There are currently a variety of tools for detecting vulnerabilities in smart contracts. ContractFuzzer is a dynamic detection tool for smart contract security analysis. It uses fuzz testing technology to detect vulnerabilities. However, since the input test cases of ContractFuzzer are randomly generated, it may not cover all execution paths, resulting in low code coverage and low accuracy of vulnerability detection results. Vandal, a tool based on intermediate representation technology, performs vulnerability analysis by extracting semantic logical relationships from contract bytecodes. However, the intermediate representation relies too much on expert-defined rules and lacks strict theorem proofs, resulting in low accuracy of detection results. Oyente detects contract vulnerabilities through symbolic execution technology, but if it encounters complex contracts, Oyente may have path explosion problems, and its false positive rate for integer overflow vulnerabilities is high, resulting in low accuracy of its vulnerability detection. Summary of the invention
[0004] The purpose of the present invention is to provide a smart contract vulnerability detection method and device based on execution path and stack events.
[0005] The technical solution of the present invention is as follows:
[0006] A smart contract vulnerability detection method based on execution path and stack events includes the following operations:
[0007] S1. Compile the source code of the smart contract to be tested into bytecode, disassemble the bytecode to obtain the opcode; obtain the control flow graph based on the opcode; obtain all execution paths from the head basic block to the tail basic block based on the control flow information in the control flow graph; take the execution paths containing operation instructions and / or call instructions in all execution paths as the execution paths to be tested; simulate the execution of the EVM instructions of the smart contract to be tested, record the interaction information between each EVM instruction and the stack during the simulation execution, and obtain the stack event;
[0008] S2. Dynamically analyze and process the stack event and the execution path to be inspected based on the parameter nature and / or instruction nature to obtain potential vulnerability paths.
[0009] S3. If the potential vulnerability path is related to an integer overflow vulnerability, a reentry vulnerability, or a denial-of-service vulnerability, perform symbolic execution processing on the potential vulnerability path based on the arithmetic instruction operation result, and / or the transfer amount, and / or the cost consumption to obtain an integer overflow vulnerability detection result, a reentry vulnerability detection result, or a denial-of-service vulnerability detection result.
[0010] During the dynamic analysis and processing in S2, if in the execution path to be inspected where the arithmetic instruction is located in the stack event, the judgment operation on whether the call parameter corresponding to the arithmetic instruction is 0 has not been executed, then the execution path to be inspected corresponding to the arithmetic instruction is a potential vulnerability path related to the integer overflow vulnerability.
[0011] During the process of performing symbolic execution processing on the potential vulnerability path related to the integer overflow vulnerability, if in the potential vulnerability path related to the integer overflow vulnerability, the arithmetic instruction operation result is within the corresponding solution range, then the smart contract to be inspected has an integer overflow vulnerability.
[0012] During the dynamic analysis and processing in S2, if in the execution path to be inspected where the CALL instruction is located in the stack event, the purpose of the SLOAD instruction is the same as that of the CALLER instruction, and the address of the CALL instruction < the address of the SSTORE instruction, and the parameter addresses of the SLOAD instruction and the SSTORE instruction are the same, then the execution path to be inspected corresponding to the CALL instruction is a potential vulnerability path related to the reentry vulnerability.
[0013] During the process of performing symbolic execution processing on the potential vulnerability path related to the reentry vulnerability, if in the potential vulnerability path related to the reentry vulnerability, 0 < transfer amount < the balance of the caller's account, or the cost consumption exceeds the consumption threshold, then the smart contract to be inspected has a reentry vulnerability.
[0014] During the dynamic analysis and processing in S2, if in the execution path to be inspected where the DELEGATECALL instruction is located in the stack event, the target address of the DELEGATECALL instruction is the same as the target address of the parameter loaded by the MLOAD instruction, then the execution path to be inspected corresponding to the DELEGATECALL instruction is a potential vulnerability path related to the delegate call vulnerability, and the smart contract to be inspected has a delegate call vulnerability.
[0015] During the dynamic analysis and processing of the smart contract to be tested, if the top element of the stack starting with the CALL instruction in the stack event is in the execution path to be tested, and the top element of the stack starting with the CALL instruction has executed a judgment operation on whether it is 0, then the execution path to be tested where the top element of the stack starting with the CALL instruction is located is a potential vulnerability path related to the denial of service vulnerability. If there is no loop structure in the potential vulnerability path related to the denial of service vulnerability, then the smart contract to be tested has a denial of service vulnerability; if there is a loop structure in the potential vulnerability path related to the denial of service vulnerability, and the loop structure contains a basic block whose number of visits exceeds the access threshold during the symbolic execution processing, and the loop structure contains a basic block whose transfer amount of the CALL instruction is greater than 0, then the smart contract to be tested has a denial of service vulnerability.
[0016] A smart contract vulnerability detection system based on execution path and stack events, comprising:
[0017] The execution path to be checked and the stack event generation module are used to compile the source code of the smart contract to be checked into bytecode, and the bytecode is disassembled to obtain the operation code; based on the operation code, a control flow graph is obtained; based on the control flow information in the control flow graph, all execution paths from the head basic block to the tail basic block are obtained; among all execution paths, the execution path containing operation instructions and / or call instructions is taken as the execution path to be checked; the EVM instructions of the smart contract to be checked are simulated and executed, and the interaction information between each EVM instruction and the stack during the simulation execution process is recorded to obtain the stack event;
[0018] A potential vulnerability path generation module is used to dynamically analyze and process stack events and execution paths to be checked based on parameter properties and / or instruction properties to obtain potential vulnerability paths;
[0019] The vulnerability detection result generation module is used to perform symbolic execution processing on the potential vulnerability path based on the arithmetic instruction operation results, and / or the transfer amount, and / or the fee consumption if the potential vulnerability path is related to the integer overflow vulnerability, reentrancy vulnerability, or denial of service vulnerability, so as to obtain the integer overflow vulnerability detection result, reentrancy vulnerability detection result, or denial of service vulnerability detection result.
[0020] A smart contract vulnerability detection device based on execution path and stack events includes a processor and a memory, wherein the processor implements the above-mentioned smart contract vulnerability detection method based on execution path and stack events when executing a computer program stored in the memory.
[0021] A computer-readable storage medium is used to store a computer program, wherein the computer program, when executed by a processor, implements the above-mentioned smart contract vulnerability detection method based on execution path and stack events.
[0022] The beneficial effects of the present invention are:
[0023] The present invention provides a smart contract vulnerability detection method based on execution path and stack events. First, the execution path is obtained through a control flow graph, and the execution path containing operation instructions and / or call instructions in the execution path is used as the execution path to be detected; at the same time, the stack event is obtained according to the interaction information between the EVM instruction and the stack; then, the execution path to be detected, the stack event, the parameter property and the instruction property are combined, and dynamic analysis processing is performed to obtain a potential vulnerability path and a preliminary vulnerability detection result; finally, symbolic execution processing is performed according to the property of the potential vulnerability path, as well as the arithmetic instruction operation result, the transfer amount and the fee consumption to obtain the vulnerability detection result; the detection method has a low false alarm rate and has a high accuracy, recall rate and F1 score value. DETAILED DESCRIPTION
[0024] This embodiment provides a smart contract vulnerability detection method based on execution path and stack events, including the following operations:
[0025] S1. Compile the source code of the smart contract to be tested into bytecode, disassemble the bytecode to obtain the opcode; obtain the control flow graph based on the opcode; obtain all execution paths from the head basic block to the tail basic block based on the control flow information in the control flow graph; take the execution paths containing operation instructions and / or call instructions in all execution paths as the execution paths to be tested; simulate the execution of the EVM instructions of the smart contract to be tested, record the interaction information between each EVM instruction and the stack during the simulation execution, and obtain the stack event;
[0026] S2. Based on the properties of parameters and / or instructions, dynamically analyze and process the stack events and the execution path to be checked to obtain a potential vulnerability path;
[0027] S3. If the potential vulnerability path is related to an integer overflow vulnerability, a reentrancy vulnerability, or a denial of service vulnerability, symbolic execution is performed on the potential vulnerability path based on the arithmetic instruction operation result, and / or the transfer amount, and / or the fee consumption to obtain an integer overflow vulnerability detection result, a reentrancy vulnerability detection result, or a denial of service vulnerability detection result.
[0028] S1. Compile the source code of the smart contract to be tested into bytecode, disassemble the bytecode to obtain the opcode; obtain the control flow graph based on the opcode; obtain all execution paths from the head basic block to the tail basic block based on the control flow information in the control flow graph; take the execution paths containing operation instructions and / or call instructions in all execution paths as the execution paths to be tested; simulate the execution of the EVM instructions of the smart contract to be tested, record the interaction information between each EVM instruction and the stack during the simulation execution, and obtain the stack event.
[0029] The execution path is obtained through the control flow graph, and the execution path containing operation instructions and / or call instructions in the execution path is used as the execution path to be checked; at the same time, the stack event is obtained according to the interaction information between the EVM instruction and the stack; the execution path to be checked is combined with the stack event to perform vulnerability detection analysis, which can improve the accuracy of vulnerability detection.
[0030] First, compile the source code of the smart contract to be tested into bytecode, and then disassemble the bytecode to obtain the operation code. The above operation is a prior art and will not be repeated here to save space.
[0031] Then, a control flow graph is obtained based on the opcode. Specifically, the opcode is divided into several basic blocks, and the basic blocks are connected according to the jump instructions to form a control flow graph. The control flow information in the control flow graph is the jump information between basic blocks. The jump types are divided into conditional jump, unconditional jump, and fall. For a basic block ending with a JUMPI instruction, the type of the basic block is conditional jump. For a basic block ending with a JUMP instruction, the type of the basic block is unconditional jump, and for a basic block ending with a non-jump or non-termination instruction, the basic jump type is fall.
[0032] Finally, based on the control flow information in the control flow graph, all execution paths from the head basic block to the tail basic block are obtained to clarify the logical order of the smart contract to be checked during execution; all execution paths that contain operation instructions and / or call instructions are taken as execution paths to be checked; at the same time, the EVM instructions of the smart contract to be checked are simulated, and the interaction information between each EVM instruction and the stack during the simulation execution is recorded to obtain stack events. Stack events not only include the interaction information between the instruction EVM and the stack and keep it updated, but also include the parameters, values and addresses of the instructions.
[0033] S2. Based on the properties of parameters and / or instructions, dynamically analyze and process the stack events and the execution path to be checked to obtain a potential vulnerability path.
[0034] Based on the properties of parameters and / or instructions in the smart contract to be checked, dynamic analysis and processing of stack events and execution paths to be checked can screen out suspicious paths related to integer overflow vulnerabilities, reentrancy vulnerabilities, or denial of service vulnerabilities as potential vulnerability paths. It can also directly detect delegate call vulnerabilities, unchecked external call vulnerabilities, and denial of service vulnerabilities that do not contain loop structures.
[0035] S3. If the potential vulnerability path is related to an integer overflow vulnerability, a reentrancy vulnerability, or a denial of service vulnerability, symbolic execution is performed on the potential vulnerability path based on the arithmetic instruction operation result, and / or the transfer amount, and / or the fee consumption to obtain an integer overflow vulnerability detection result, a reentrancy vulnerability detection result, or a denial of service vulnerability detection result.
[0036] After the potential vulnerability path has been determined, in order to obtain accurate vulnerability detection results, symbolic execution processing is performed on the potential vulnerability path based on the arithmetic instruction operation results, and / or transfer amount, and / or fee consumption, and the potential vulnerability path is converted into a set of constraints. By using constraint solving, it is analyzed whether the vulnerability of the smart contract to be tested is triggered by the capability, and the integer overflow vulnerability detection result or the reentrancy vulnerability detection result is obtained.
[0037] In order to facilitate those skilled in the art to quickly understand the vulnerability detection method of this embodiment, an overall technical solution is described for each vulnerability as follows.
[0038] Detection of integer overflow vulnerabilities.
[0039] During the dynamic analysis of the smart contract to be tested, if the arithmetic instruction in the stack event is in the execution path to be tested, and the corresponding call parameter of the arithmetic instruction has not performed the judgment operation of whether it is 0, then the execution path to be tested corresponding to the arithmetic instruction is a potential vulnerability path related to the integer overflow vulnerability. In addition, during the symbolic execution of the potential vulnerability path related to the integer overflow vulnerability, if the arithmetic instruction operation result in the potential vulnerability path related to the integer overflow vulnerability is within the corresponding solution range, then the smart contract to be tested has an integer overflow vulnerability.
[0040] The process of dynamic analysis for the purpose of integer overflow vulnerability detection is shown in the following detection code.
[0041] Input: CFG; SE: Stack Events
[0042] Output: Potential_IOF_Path[]
[0043] 1: Potential_IOF_Path[] ← ⌀
[0044] 2: O_P[] ← ⌀
[0045] 3: Traverse SE
[0046] 4: Arith para ← Extr(ADD / MUL / SUB / DIV)
[0047] 5: O_P[] ← storage Arith paths
[0048] 6: Traverse O_P[]
[0049] 7: if ISZERO(Arith para) then
[0050] 8: Pop(O_P[])
[0051] 9: else if EQ(opcode,ADD) then
[0052] 10: Mark category 1
[0053] 11: else if EQ(opcode,MUL) then
[0054] 12: Mark category 2
[0055] 13: else if EQ(opcode,SUB) then
[0056] 14: Mark category 3
[0057] 15: else if EQ(opcode,DIV) then
[0058] 16: Mark category 4
[0059] 17: Push(Potential_IOF_Path[],O_P[])
[0060] 18: end.
[0061] In the above detection code, the control flow graph CFG and stack event SE are used as input, and the potential vulnerability path set (Potential_IOF_Path[]) is output. First, the call parameters of the arithmetic instructions (ADD / MUL / SUB / DIV operation instructions) are extracted through regular expressions and recorded as Arith para (line 4). Secondly, the execution paths to be tested corresponding to these arithmetic instructions are pushed into the set O_P[] (line 5). Then, traverse the paths in O_P[]. If Arith para passes the ISZERO judgment (line 7), pop up this path and continue traversing (line 8). If Arith para does not pass the ISZERO judgment, the path is recorded as a potential vulnerability path, marked with a serial number, and put into the potential vulnerability path set Potential_IOF_Path[] (line 17).
[0062] The process of symbolic execution for integer overflow vulnerability detection is detailed in the detection code below.
[0063] Input: Potential_IOF_Path[]; S :z3-solver
[0064] Output: IOF[]
[0065] 1: IOF[] ← ⌀
[0066] 2: Traverse Potential_IOF_Path[]
[0067] 3: SYME(Potential_IOF_Path[])
[0068] 4: a1 ← S(ADD_arg(1))
[0069] 5: a2 ← S(ADD_arg(2))
[0070] 6: if EQ(Potential_IOF_Path[],category 1) then
[0071] 7: sum ← a1 + a2
[0072] 8: Add(S,UGT(sum, 2^256-1))
[0073] 9: else if EQ(Potential_IOF_Path[],category 2) then
[0074] 10: product ← a1* a2
[0075] 11: Add(S,UGT(product, 2^256-1))
[0076] 12: else if EQ(Potential_IOF_Path[],category 3) then
[0077] 13: Add(S,ULT(s1, s2))
[0078] 14: else if EQ(Potential_IOF[]_Path,category 4) then
[0079] 15: Add(S,(d2 == 0))
[0080] 16: end
[0081] 17: if EQ(check_sat(S),sat) then
[0082] 18: Push(IOF[], Potential_IOF_Path[])
[0083] 19: end。
[0084] The above detection code takes the set of potential vulnerability paths Potential_IOF_Path[] and S (the z3 solver, which is the solver and judge for the arithmetic instruction operation result) as inputs, and outputs the set of integer overflow vulnerabilities IOF[]. First, traverse the potential vulnerability paths in the set of potential vulnerability paths Potential_IOF_Path[], and perform symbolic execution on them (SYME(Potential_IOF_Path)) (line 3). Second, assign the two parameters of the arithmetic instruction to a1 and a2 respectively through the expressions of the z3 solver (lines 4 - 5). Then, if the path operation type is 1 (line 6), use the solution method of the ADD instruction in the z3 solver (Line 11) for calculation. If the operation result is within the corresponding preset solution range, it proves that there is a solution. Finally, if there is a solution (EQ(check_sat(S), sat)) (Line 17), push the potential vulnerability path into the set IOF[]. Similarly, the overflows of the other 3 arithmetic instruction operations in Table 1 can also be detected.
[0085] Table 1 Summary Table of Operation Types
[0086] 。
[0087] Detection of re - entry vulnerabilities.
[0088] During the dynamic analysis and processing of the smart contract to be detected, if in the execution path to be detected where the CALL instruction is located in the stack event, the SLOAD instruction and the CALLER instruction have the same purpose, and the address of the CALL instruction < the address of the SSTORE instruction, and the parameter addresses of the SLOAD instruction and the SSTORE instruction are the same, then the execution path to be detected where the CALL instruction is located is a potential vulnerability path related to the re - entry vulnerability. Moreover, during the process of performing symbolic execution on the potential vulnerability path related to the re - entry vulnerability, if in the potential vulnerability path related to the re - entry vulnerability, 0 < transfer amount < caller's account balance, or the cost consumption exceeds the consumption threshold, then the smart contract to be detected has a re - entry vulnerability.
[0089] The specific process of dynamic analysis and processing for the purpose of detecting re - entry vulnerabilities is shown in the following detection code.
[0090] Input: CFG; SE; Modifier; BoolLock
[0091] Output: Potential_RE_Path[]
[0092] 1: Potential_RE_Path[] ← ⌀
[0093] 2: R_P[] ← ⌀
[0094] 3: if EQ(Modifier,false) and EQ(BoolLock,false) then
[0095] 4: Traverse SE
[0096] 5: R_P[] ← storage CALL paths
[0097] 6: Traverse R_P[]
[0098] 7: if LT(sloadpc,callpc) and LT(callpc,sstorepc) or LT(callpc,sstorepc) then
[0099] 8: Push(Potential_RE_Path[],C_P[])
[0100] 9: else
[0101] 10: Pop(R_P[])
[0102] 11: end
[0103] 12: end。
[0104] In the above code, the control flow graph CFG, stack events SE, Modifier restrictions, and BoolLock (Boolean lock) are used as inputs, and the set of potential vulnerability paths Potential_RE_Path[] is output. First, traverse SE and push the execution paths to be checked that contain the CALL instruction into the set R_P[] (line 5). Then, traverse the execution paths to be checked in R_P[]. If sload_pc (the address of the SLOAD instruction) < call_pc (the address of the CALL instruction) < sstore_pc (the address of the SSTORE instruction) or CALL instruction's address) < sstore_pc (the address of the SSTORE instruction) (LT(sload_pc, call_pc) and LT(call_pc, sstore_pc) or LT(call_pc, sstore_pc)) (line 7), that is, the execution path to be checked does not include a Boolean lock, and the parameter addresses of the SLOAD instruction and the SSTORE instruction are the same, and the execution path to be checked is not restricted by Modifier either, then the execution path to be checked is a potential vulnerability path related to the reentry vulnerability, and this execution path to be checked is put into the set of potential vulnerability paths Potential_RE_Path[].
[0105] Among them, the operation of whether the execution path to be checked is restricted by Modifier is as follows: If in the execution path to be checked where the CALL instruction is located in the stack events, the address of the current caller is the same as the address from which data is read in the contract storage, that is, the address of the CALLER instruction is the same as the address of the SLOAD instruction, then the execution path to be checked is restricted by Modifier, and the smart contract to be checked does not have a reentry vulnerability.
[0106] The operation of whether the execution path to be checked contains BoolLock (Boolean lock) is as follows: If in the execution path to be checked where the CALL instruction is located in the stack events, the parameter value of the SLOAD instruction starting with the top element of the stack is different from the parameter value of the SSTORE instruction, but the parameter addresses of the SLOAD instruction and the SSTORE instruction are the same, and the address of the SLOAD instruction (sload_pc) is greater than the address of the CALL instruction (call_pc) which is greater than the address of the SSTORE instruction (sstore_pc), then the execution path to be checked contains BoolLock (Boolean lock), and the smart contract to be checked does not have a reentry vulnerability.
[0107] The operations of whether the execution path to be checked is restricted by Modifier and whether it contains BoolLock (Boolean lock) can be implemented through the following detection code:
[0108] Input: CFG; SE :Stack Events
[0109] Output: Bool :Modifier; Bool :BoolLock
[0110] 1: Modifier ← False
[0111] 2: Mod_P[] ← ⌀
[0112] 3: Traverse SE
[0113] 4: Mod_P[] ← storage CALL paths
[0114] 5: Traverse Mod_P[]
[0115] 6: if Cts(block_conexp(block),(EQ(SLOAD,CALLER)) or (EQ(CALLER,SLOAD))) then
[0116] 7: Modifier ← True
[0117] 8: end
[0118] 9: BoolLock ← False
[0119] 10: Traverse SE
[0120] 11: if SE_Top_Stw(SSTORE) then
[0121] 12: record sstore_pc and sstore_para_pc and sstore_para_value
[0122] 13: end
[0123] 14: if SE_Top_Stw(CALL) then
[0124] 15: record call_pc
[0125] 16: end
[0126] 17: if SE_Top_Stw(SLOAD) then
[0127] 18: record sload_pc and sload_para_pc and sload_para_value
[0128] 19: if EQ(sstore_para_value,1) and EQ(sload_para_value,0)
[0129] 20: and EQ(sstore_para_pc,sload_para_pc) and GT(sload_pc,call_pc)
[0130] 21: and GT(call_pc,sstore_pc) then
[0131] 22: BoolLock ← True
[0132] 23: end
[0133] 24: end。
[0134] In the above code to be checked, lines 1-8 are for judging Modifier restrictions, and lines 9-24 are for judging BoolLock (Boolean lock). When judging Modifier restrictions, it takes the control flow graph CFG and stack event SE as input, and outputs whether the execution path to be checked has Modifier restrictions. First, traverse SE and push the execution path to be checked containing the CALL instruction into the set Mod_P[] (line 4). Then, traverse the execution path to be checked in Mod_P[] (line 5). If there is a basic block whose jump expression contains EQ(SLOAD,CALLER or EQ(CALLER,SLOAD) in the execution path to be checked (line 6), it is considered that there is a Modifier restriction and the variable Modifier is set to True. When judging BoolLock, first record the address (sstore_pc) of the SSTORE instruction (SE_Top_Stw(SSTORE)) whose top element of the stack begins with the SSTORE instruction (sstore_para_pc), the parameter address (sstore_para_pc) of the SSTORE instruction, and the parameter value (sstore_para _value) (line 12). Then, record the address of the CALL instruction (call_pc) (line 15). Finally, continue to traverse SE. If the top element of the stack begins with a SLOAD instruction (line 17), and satisfies the following conditions: sstore_para_value is equal to 1, the parameter value of the SLOAD instruction is equal to 0, sstore_para_pc is equal to the address of the parameter of the SLOAD instruction (sload_para_pc), the address of the SLOAD instruction (sload_pc) is greater than call_pc, and call_pc is greater than sstore_pc (lines 19-21), then it is considered that a BoolLock exists and the variable BoolLock is set to True.
[0135] The process of symbolic execution for the purpose of reentrancy vulnerability detection is detailed in the detection code below.
[0136] Input: Potential_RE_Path[]; S: z3-solver
[0137] Output: RE[]
[0138] 1: Trasfer_amount ← False
[0139] 2: Gas_Unlimit ← False
[0140] 3: RE[] ← ⌀
[0141] 4: Traverse Potential_RE_Path[]
[0142] 5: SYME(Potential_RE_Path[])
[0143] 6: tx ← S(CALL.argument(3))
[0144] 7: Add(S,UGT(tx, 0)) and Add(S,ULE(tx, balance_Ia))
[0145] 8: if EQ(check_sat(S),sat) then
[0146] 9: Trasfer_amount ← True
[0147] 10: end
[0148] 11: Traverse Potential_RE_Path[]
[0149] 12: SYME(Potential_RE_Path[])
[0150] 13: gas_total ← Accm(gas_single)
[0151] 14: Ga_c ← S(gas_total)
[0152] 15: Add(S,UGT(Ga_c, 2300))
[0153] 16: if EQ(check_sat(S),sat) then
[0154] 17: Gas_Unlimit ← True
[0155] 18: end
[0156] 19: if EQ(Trasfer_amount,True) and EQ(Gas_Unlimit,True) then
[0157] 20: Push(RE[],Potential_RE_Path[])
[0158] 21: end。
[0159] In the above detection code, the set of potential vulnerability paths Potential_RE_Path[] and S (z3 solver) are used as inputs, and the set of reentrancy vulnerabilities RE[] is output. First, the transfer amount in the potential vulnerability path is modeled using z3, and the modeled expression is assigned to tx (line 6). Second, "0 < tx < caller's account balance (balance_Ia)" is used as the solution method and input into z3 for solving (line 7). If there is a solution, the variable Trasfer_amount is set to True (line 9). Then, the cumulative cost consumption (gas consumption) during the symbolic execution process is assigned to the variable gas_total (line 13), modeled using the z3 solver, and the modeled expression is assigned to Ga_c (line 14). Finally, "Ga_c > 2300" (line 15) is used as the solution method and input into the z3 solver for solving. If there is a solution, Gas_Unlimit is set to True (line 17). If both Trasfer_amount and Gas_Unlimit are True simultaneously, the potential vulnerability path is placed into the set of reentrancy vulnerabilities RE[].
[0160] Detection of delegate call vulnerabilities.
[0161] During the dynamic analysis and processing of the smart contract to be detected, if in the execution path to be detected where the DELEGATECALL instruction is located in the stack event, the target address of the DELEGATECALL instruction is the same as the target address of the parameter loaded by the MLOAD instruction, then the execution path to be detected corresponding to the DELEGATECALL instruction is a potential vulnerability path related to the delegate call vulnerability, and the smart contract to be detected has a delegate call vulnerability.
[0162] The process of dynamic analysis and processing for the purpose of detecting delegate call vulnerabilities can be seen in the following detection code.
[0163] Input: CFG; SE :Stack Events
[0164] Output: DC[]
[0165] 1: DC[] ← ⌀
[0166] 2: DC_P[] ← ⌀
[0167] 3: Traverse SE
[0168] 4: DC_P[] ← storage delegatecall paths
[0169] 5: Traverse DC_R[]
[0170] 6: dele_para ← DELEGATECALL.arg(3)
[0171] 7: if Cts(cond_expr,MLOAD) then
[0172] 8: record mload_para
[0173] 9: if Cts(dele_para,mloadpc) then
[0174] 10: Push(DC[],DC_R[])
[0175] 11: end
[0176] 12: end.
[0177] The above detection code takes the control flow graph CFG and stack event SE as input, and outputs the delegate call vulnerability (delegatecall vulnerability) set DC_R[]. First, traverse SE and push the execution path to be checked containing the DELEGATECALL instruction into the set DC_P[] (Line 4). Then traverse the path in DC_P[], record the target address of the DELEGATECALL instruction, and assign the target address to the variable dele_para (Line 6). Finally, if the jump expression of the basic block contains the instruction MLOAD(Cts(cond_expr,MLOAD)) (Line 7), and dele_para is the same as mload_para (Line 9), the path is pushed into the delegate call vulnerability set DC_R[].
[0178] Detection of unchecked external call vulnerabilities.
[0179] During the dynamic analysis process of the smart contract to be checked, if the top element of the stack starting with the CALL instruction in the execution path to be checked where the CALL instruction is located in the stack event has executed a judgment operation on whether it is 0, then the execution path to be checked corresponding to the CALL instruction is a potential vulnerability path related to the unchecked external call vulnerability, and the smart contract to be checked has an unchecked external call vulnerability.
[0180] The process of dynamic analysis for the purpose of unchecked external call vulnerabilities can be seen in the following detection code.
[0181] Input: CFG; SE: Stack Events
[0182] Output: UEC[]
[0183] 1: UEC[] ← ⌀
[0184] 2: UEc_P[] ← ⌀
[0185] 3: Traverse SE
[0186] 4: Uec_P[] ← storage CALL paths
[0187] 5: Traverse UEc_P[]
[0188] 6: if SE_Top_Stw(CALL) then
[0189] 7: record call_pc
[0190] 8: uec_para ← call_pc
[0191] 9: if !ISZERO(uec_para) then
[0192] 10: Push(UEC[],UEc_P[])
[0193] 11: else
[0194] 10: Pop(UEc_P[])
[0195] 12: end
[0196] 13: end.
[0197] The above detection code uses CFG and SE as input and outputs the unchecked external call vulnerability set UEC[]. First, traverse SE and push the path containing the CALL instruction into the set UEC_P[] (line 4). Then, traverse the path in UEC_P[] and find the top element of the stack that starts with the CALL instruction (SE_Top_Stw(CALL)) (line 6), and assign call_pc to the variable uec_para (line 8). Finally, if call_pc is not judged by ISZERO (line 9), push the path into the set UEC[].
[0198] Detection of denial of service vulnerabilities.
[0199] During the dynamic analysis of the smart contract to be checked, if the top element of the stack starting with the CALL instruction in the stack event has performed a judgment operation on whether it is 0 in the execution path to be checked, then the execution path to be checked where the top element of the stack starting with the CALL instruction is located is a potential vulnerability path related to the denial of service vulnerability.
[0200] If there is no loop structure in the potential vulnerability path related to the denial of service vulnerability, then the smart contract to be tested has a denial of service vulnerability;
[0201] If there is a loop structure in the potential vulnerability path related to the denial of service vulnerability, and during the symbolic execution process, the loop structure contains basic blocks that have been accessed more than the access threshold, and the loop structure contains basic blocks with a transfer amount greater than 0 in the CALL instruction, then the smart contract to be tested has a denial of service vulnerability.
[0202] The specific operation of judging the loop structure is as follows: first, use the depth-first search algorithm to traverse the control flow graph CFG and mark all visited basic blocks. If there is a basic block that has been visited, then the basic block is the starting point of the loop, and the other basic blocks in the loop are the loop body. These basic blocks constitute a loop structure (Loop Path), and all basic blocks in the loop path are loop blocks (Loop Block).
[0203] The process of dynamic analysis for the purpose of denial of service vulnerability can be seen in the following detection code.
[0204] Input: CFG; SE: Stack Events
[0205] Output: DoS[]
[0206] 1: DoS[] ← ⌀
[0207] 2: DoS_P[] ← ⌀
[0208] 3: Traverse SE
[0209] 4: DoS_P[] ← storage CALL paths
[0210] 5: Traverse DoS_P[]
[0211] 6: if SE_Top_Stw(CALL) then
[0212] 7: record call_pc
[0213] 8: dos_para ← call_pc
[0214] 9: if Cts(cond_expr(block),ISZERO(dos_para)) then
[0215] 10: Push(DoS[],DoS_P[])
[0216] 11: else
[0217] 12: Pop(DoS_P[])
[0218] 13: end.
[0219] The above detection code takes the control flow graph CFG and stack events SE as input, and outputs a set of denial of service vulnerabilities (DoS vulnerabilities) (DoS[]). First, traverse SE and push the execution path to be checked containing the CALL instruction into the set DoS_P[] (line 4). Then traverse the execution path to be checked in DoS_P[], find the top element of the stack that starts with the CALL instruction (SE_Top_Stw(CALL)) (line 6), and assign call_pc to the variable dos_para. Finally, if dos_para is judged by ISZERO in the conditional jump expression of the basic block (Cts(cond_expr(block),ISZERO(dos_para)))(Line 9), the execution path to be checked is regarded as a potential vulnerability path related to the denial of service vulnerability and is pushed into the set DoS[].
[0220] The process of symbolic execution for the purpose of denial of service vulnerability can be seen in the detection code below.
[0221] Input: Loop Path; S: z3-solver
[0222] Output: DoS[]
[0223] 1: DoS[] ← ⌀
[0224] 2: SYME (Loop Path)
[0225] 3: Add(S,constraints)
[0226] 4: if visited_times(block)>2 and Cts(Loop Path,Money call block)then
[0227] 5: Push(DoS[],Loop Path)
[0228] 6: end.
[0229] In the above detection code, the loop structure Loop Path and z3 solver collected in the control flow graph CFG construction phase are used as input to output the denial of service vulnerability set DoS[]. First, the loop structure is symbolically executed, and the constraints in the loop structure are put into z3 for solution (line 3). If the number of visits to one of the basic blocks exceeds 2, and the loop structure contains Money_call _block (line 4), the potential vulnerability path corresponding to the loop structure is pushed into the set DoS[]. If the transfer amount of the CALL instruction in the stack event is greater than 0, the basic block where the CALL instruction is located is called Money_call_block.
[0230] In order to verify the detection effect of the smart contract vulnerability detection method of this embodiment (hereinafter referred to as the detection method of this embodiment), the following experiment was conducted.
[0231] Experimental environment. The experiment was conducted on a 64-bit Ubuntu 20.04.4 operating system, running on a VMware Workstation Pro virtual machine with 2 GB of memory and 8 processor cores.
[0232] Datasets. The first dataset comes from the Smartbugs official website, which selects 198 contracts, covering integer overflow vulnerabilities, reentrancy vulnerabilities, delegate call vulnerabilities, unchecked external call vulnerabilities, and denial of service vulnerabilities. The second dataset selects 13,016 verified real smart contracts from Etherscan.
[0233] Evaluation indicators. Seven metrics were obtained in the experiment: True Positive (TP), True Negative (TN), False Positive (FP), False Negative (FN), Precision (P), Recall (R), and F1-score (F). TP means that the smart contract vulnerability is correctly detected, TN means that the smart contract is correctly detected without a vulnerability, FP means that the smart contract vulnerability is incorrectly detected, and FN means that the smart contract vulnerability is not detected. Based on the above metrics, the evaluation indicators are set to Precision, Recall, and F1-score, and the calculation formula is as follows:
[0234] ,
[0235] ,
[0236] .
[0237] Experimental results on the first data set. Table 2 summarizes the number of vulnerabilities detected by the method of this embodiment in the first data set, including the number of TP, TN, FP and FN, as well as the corresponding Precision, Recall and F1-score. Among them, the method of this embodiment detected 23 smart contracts with integer overflow vulnerabilities, 31 smart contracts with reentrancy vulnerabilities, 4 smart contracts with delegated call vulnerabilities, 9 smart contracts with denial of service vulnerabilities, and 58 smart contracts with unchecked external call vulnerabilities. It can be seen that the accuracy of vulnerability detection by the method of this embodiment is relatively high.
[0238] Table 2 Detection results of the method in this embodiment for the first data set
[0239] .
[0240] The detection results of this embodiment are compared with those of other advanced existing tools on the first data set. In the experiment, the F1-score of the method of this embodiment is compared with that of three existing advanced smart contract vulnerability detection methods (Oyente, Mythril, and DefectChecker) on the first data set. The experimental results are shown in Table 3. The results show that the F1-score of the method of this embodiment is higher than that of the other three existing methods, and the accuracy of vulnerability detection by the method of this embodiment is higher.
[0241] Table 3 Detection results of this embodiment and other advanced existing tools on the first dataset
[0242] .
[0243] Experimental results on the second data set. In the experiment, the method of this embodiment was used to detect 13,016 different smart contracts (the second data set) collected on Etherscan. The results are shown in Table 4. The second column of Table 4 indicates the number of contracts with corresponding vulnerabilities. The last column of Table 4 provides the percentage of contracts with vulnerabilities. The integer overflow vulnerability detected in the data set was also verified in the experiment. Among the 304 integer overflow vulnerabilities detected, 283 were true positives. Of the 29 contracts with reentrancy vulnerabilities detected by the method of this embodiment, 21 were true positives, while Oyente could only detect 9 contracts and Mythril could only detect 7 contracts. For the delegate call vulnerability, the method of this embodiment detected a total of 11 contracts with this vulnerability, of which 9 were confirmed as true positives. It can be seen that the detection accuracy of the method of this embodiment is relatively high.
[0244] Table 4 Number of vulnerabilities detected by the method in this embodiment in the second data set
[0245] .
[0246] This embodiment also provides a smart contract vulnerability detection system based on execution path and stack events, including:
[0247] The execution path to be checked and the stack event generation module are used to compile the source code of the smart contract to be checked into bytecode, and the bytecode is disassembled to obtain the operation code; based on the operation code, a control flow graph is obtained; based on the control flow information in the control flow graph, all execution paths from the head basic block to the tail basic block are obtained; among all execution paths, the execution path containing operation instructions and / or call instructions is taken as the execution path to be checked; the EVM instructions of the smart contract to be checked are simulated and executed, and the interaction information between each EVM instruction and the stack during the simulation execution process is recorded to obtain the stack event;
[0248] A potential vulnerability path generation module is used to dynamically analyze and process stack events and execution paths to be checked based on parameter properties and / or instruction properties to obtain potential vulnerability paths;
[0249] The vulnerability detection result generation module is used to perform symbolic execution processing on the potential vulnerability path based on the arithmetic instruction operation results, and / or the transfer amount, and / or the fee consumption if the potential vulnerability path is related to the integer overflow vulnerability, reentrancy vulnerability, or denial of service vulnerability, so as to obtain the integer overflow vulnerability detection result, reentrancy vulnerability detection result, or denial of service vulnerability detection result.
[0250] This embodiment also provides a smart contract vulnerability detection device based on execution path and stack events, including a processor and a memory, wherein the processor implements the above-mentioned smart contract vulnerability detection method based on execution path and stack events when executing a computer program stored in the memory.
[0251] This embodiment also provides 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 execution path and stack events.
[0252] The present embodiment provides a smart contract vulnerability detection method based on execution path and stack events. First, the execution path is obtained through a control flow graph, and the execution path containing operation instructions and / or call instructions in the execution path is used as the execution path to be checked; at the same time, the stack event is obtained according to the interaction information between the EVM instruction and the stack; then, the execution path to be checked, the stack event, the parameter properties and the instruction properties are combined for dynamic analysis and processing to obtain a potential vulnerability path and preliminary vulnerability detection results; finally, symbolic execution processing is performed according to the properties of the potential vulnerability path, as well as the arithmetic instruction operation results, the transfer amount and the cost consumption to obtain the vulnerability detection results; the detection method has a low false alarm rate and has high accuracy, recall rate and F1 score value.
Claims
1. A smart contract vulnerability detection method based on execution path and stack events, characterized in that: It includes the following operations: S1. Compile the source code of the smart contract to be inspected into bytecode, disassemble the bytecode to obtain opcodes, and based on the opcodes, obtain a control flow graph. Specifically, divide the opcodes into several basic blocks, and connect the basic blocks according to jump instructions to form a control flow graph; Based on the control flow information in the control flow graph, obtain all execution paths from the head basic block to the tail basic block; Use the execution paths containing arithmetic instructions and / or call instructions among all execution paths as the execution paths to be inspected; Simulate the execution of the EVM instructions of the smart contract to be inspected, record the interaction information between each EVM instruction and the stack during the simulation execution, and obtain stack events; S2. Based on the parameter nature and / or instruction nature, perform dynamic analysis on the stack events and the execution paths to be inspected to obtain potential vulnerability paths; S3. If the potential vulnerability paths are related to integer overflow vulnerabilities, reentry vulnerabilities, or denial-of-service vulnerabilities, based on the arithmetic instruction operation results, and / or transfer amounts, and / or fee consumption, perform symbolic execution on the potential vulnerability paths to obtain integer overflow vulnerability detection results, reentry vulnerability detection results, or denial-of-service vulnerability detection results; During the dynamic analysis process, if in the execution path to be inspected where the arithmetic instruction in the stack event is located, the arithmetic instruction corresponding call parameter has not performed a judgment operation on whether it is 0, then the execution path to be inspected corresponding to the arithmetic instruction is a potential vulnerability path related to the integer overflow vulnerability; during the process of performing symbolic execution on the potential vulnerability path related to the integer overflow vulnerability, if in the potential vulnerability path related to the integer overflow vulnerability, the arithmetic instruction operation result is within the corresponding solution range, then the smart contract to be inspected has an integer overflow vulnerability; During the dynamic analysis process, if in the execution path to be inspected where the CALL instruction in the stack event is located, the SLOAD instruction and the CALLER instruction have the same purpose, and the address of the SLOAD instruction < the address of the CALL instruction < the address of the SSTORE instruction, and the parameter addresses of the SLOAD instruction and the SSTORE instruction are the same, then the execution path to be inspected where the CALL instruction is located is a potential vulnerability path related to the reentry vulnerability; during the process of performing symbolic execution on the potential vulnerability path related to the reentry vulnerability, if in the potential vulnerability path related to the reentry vulnerability, 0 < transfer amount < the balance of the caller's account, or the fee consumption exceeds the consumption threshold, then the smart contract to be inspected has a reentry vulnerability; During the dynamic analysis process of the smart contract to be inspected, if in the execution path to be inspected where the stack top element starting with the CALL instruction in the stack event is located, the stack top element starting with the CALL instruction has performed a judgment operation on whether it is 0, then the execution path to be inspected where the stack top element starting with the CALL instruction is located is a potential vulnerability path related to the denial-of-service vulnerability; If there is no loop structure in the potential vulnerability path related to the denial-of-service vulnerability, then the smart contract to be inspected has a denial-of-service vulnerability; If there is a loop structure in the potential vulnerability path related to the denial of service vulnerability, and the loop structure contains a basic block whose access times exceed the access threshold during the symbolic execution process, and the loop structure contains a basic block whose transfer amount of the CALL instruction is greater than 0, then the smart contract to be tested has a denial of service vulnerability; During the dynamic analysis and processing of the smart contract to be inspected, if the top element of the stack starting with the CALL instruction in the execution path to be inspected where the CALL instruction is located in the stack event has executed a judgment operation on whether it is 0, then the execution path to be inspected corresponding to the CALL instruction is a potential vulnerability path related to the unchecked external call vulnerability, and the smart contract to be inspected has an unchecked external call vulnerability.
2. The smart contract vulnerability detection method based on execution path and stack events according to claim 1 is characterized in that: During the dynamic analysis process in S2, If the target address of the DELEGATECALL instruction in the execution path to be checked where the DELEGATECALL instruction in the stack event is located is the same as the target address of the MLOAD instruction loading parameter, then the execution path to be checked corresponding to the DELEGATECALL instruction is a potential vulnerability path related to the delegate call vulnerability, and the smart contract to be checked has a delegate call vulnerability.
3. A smart contract vulnerability detection system based on execution path and stack events, characterized in that: include: The execution path and stack event generation module to be checked is used to compile the source code of the smart contract to be checked into bytecode, and the bytecode is decompiled to obtain the operation code; Based on the operation code, a control flow graph is obtained; Specifically, the operation code is divided into several basic blocks, and the basic blocks are connected according to the jump instructions to form a control flow graph; based on the control flow information in the control flow graph, all execution paths from the head basic block to the tail basic block are obtained; among all execution paths, the execution paths containing operation instructions and / or call instructions are taken as the execution paths to be checked; the EVM instructions of the smart contract to be checked are simulated and executed, and the interaction information between each EVM instruction and the stack during the simulation execution process is recorded to obtain the stack event; A potential vulnerability path generation module is used to dynamically analyze and process the stack event and the execution path to be checked based on parameter properties and / or instruction properties to obtain a potential vulnerability path; A vulnerability detection result generation module is used to perform symbolic execution processing on the potential vulnerability path based on the arithmetic instruction operation result, and / or the transfer amount, and / or the fee consumption if the potential vulnerability path is related to an integer overflow vulnerability, a reentrancy vulnerability, or a denial of service vulnerability, so as to obtain an integer overflow vulnerability detection result, a reentrancy vulnerability detection result, or a denial of service vulnerability detection result; During the dynamic analysis process, if the arithmetic instruction in the stack event is in the execution path to be checked, and the arithmetic instruction corresponding to the call parameter has not performed a judgment operation on whether it is 0, then the execution path to be checked corresponding to the arithmetic instruction is a potential vulnerability path related to the integer overflow vulnerability; during the symbolic execution process of the potential vulnerability path related to the integer overflow vulnerability, if the arithmetic instruction operation result in the potential vulnerability path related to the integer overflow vulnerability is within the corresponding solution range, then the smart contract to be checked has an integer overflow vulnerability; During the dynamic analysis process, if in the execution path to be inspected where the CALL instruction is located in the stack event, the SLOAD instruction has the same purpose as the CALLER instruction, and the address of the SLOAD instruction < the address of the CALL instruction < the address of the SSTORE instruction, and the parameter addresses of the SLOAD instruction and the SSTORE instruction are the same, then the execution path to be inspected where the CALL instruction is located is a potential vulnerability path related to the reentrancy vulnerability; during the process of symbolic execution of the potential vulnerability path related to the reentrancy vulnerability, if in the potential vulnerability path related to the reentrancy vulnerability, 0 < transfer amount < caller's account balance, or the cost consumption exceeds the consumption threshold, then the smart contract to be inspected has a reentrancy vulnerability; During the dynamic analysis process of the smart contract to be inspected, if in the execution path to be inspected where the top element of the stack starting with the CALL instruction is located in the stack event, the top element of the stack starting with the CALL instruction has performed a judgment operation on whether it is 0, then the execution path to be inspected where the top element of the stack starting with the CALL instruction is located is a potential vulnerability path related to the denial-of-service vulnerability. If there is no loop structure in the potential vulnerability path related to the denial-of-service vulnerability, then the smart contract to be inspected has a denial-of-service vulnerability; If there is a loop structure in the potential vulnerability path related to the denial-of-service vulnerability, and during the process of symbolic execution of the loop structure, the loop structure contains a basic block whose access count exceeds the access threshold, and the basic block containing the CALL instruction in the loop structure has a transfer amount greater than 0, then the smart contract to be inspected has a denial-of-service vulnerability; During the dynamic analysis process of the smart contract to be inspected, if in the execution path to be inspected where the CALL instruction is located in the stack event, the top element of the stack starting with the CALL instruction has performed a judgment operation on whether it is 0, then the execution path to be inspected corresponding to the CALL instruction is a potential vulnerability path related to the uninspected external call vulnerability, and then the smart contract to be inspected has an uninspected external call vulnerability.
4. A smart contract vulnerability detection device based on execution path and stack events, characterized in that: It includes a processor and a memory. Among them, when the processor executes the computer program stored in the memory, it implements the smart contract vulnerability detection method based on the execution path and stack event as described in any one of claims 1-2.
5. A computer-readable storage medium, characterized in that: It is used to store a computer program. Among them, when the computer program is executed by the processor, it implements the smart contract vulnerability detection method based on the execution path and stack event as described in any one of claims 1-2.
