Intelligent contract vulnerability detection method based on static profit analysis
Through the static profit analysis method, we identify the paths through which callers in smart contracts can make profits without permission checks, which solves the problems of high false positive rate and low coverage rate of existing tool detection, and achieves accurate vulnerability detection of smart contract codes. detection.
Patent Information
- Application Number
- CN202410914117.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-09
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2044-07-09
AI Technical Summary
Existing smart contract static detection tools cannot effectively detect code vulnerabilities, have a high false positive rate, and have limited detection capabilities for large batches of contracts, making them unable to meet smart contract security requirements.
Using a method based on static profit analysis, by compiling the Solidity contract into Yul intermediate language and converting it into a control flow chart, path-sensitive symbolic execution analysis is performed to identify the caller's profit and expenditure behavior and permission checking behavior, and determine whether the contract path is There are loopholes.
It achieves accurate vulnerability detection of smart contract code, improves detection coverage and accuracy, and can effectively identify vulnerabilities in contract code.
Smart Images

Figure CN118862087A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of smart contract security and static analysis, and in particular to a smart contract vulnerability detection method based on static profit analysis. Background Art
[0002] Blockchain technology is developing rapidly. Since the launch of Ethereum 2.0, smart contracts, as a program code deployed on the chain, have the characteristics of being unmodifiable once on the chain, helping decentralized finance to develop rapidly. However, due to the complexity of on-chain business and the difficulty of development of smart contracts, attacks on smart contract code vulnerabilities often occur, causing huge losses to ordinary users and project parties. The security of smart contracts on the blockchain is facing a huge test.
[0003] In order to solve these security issues, accurate and effective smart contract vulnerability detection methods are urgently needed. Although the currently commonly used manual auditing method can accurately and effectively discover code problems, it requires a large amount of work and a large number of domain expert resources, and cannot perform vulnerability detection on a large number of contracts. The static detection tools currently available for automated vulnerability detection often only focus on the suspicious vulnerability code sentences themselves, and do not pay attention to the logical semantic information of the vulnerability code context, and lack perfect and effective detection rules for existing vulnerability types. This leads to problems such as high false positive detection rate and limited vulnerability detection capabilities. Summary of the invention
[0004] In view of the shortcomings of the existing smart contract static detection technology, the present invention provides a smart contract vulnerability detection method based on static profit analysis.
[0005] The core idea of this smart contract vulnerability detection method is that for each transaction call of the smart contract, when the caller is a non-privileged address, the called contract should ensure its own balance of income and expenditure. Therefore, for the caller as a non-privileged address, if after completing the contract call, only benefits are obtained without spending costs, it is considered that there is a code vulnerability. The definition of a vulnerability is: in a program path where there is no caller permission check behavior, there is profit behavior, there is no corresponding caller expenditure behavior, and there is no check behavior for the profiteer in this path.
[0006] The purpose of the present invention is achieved through the following technical solutions:
[0007] A smart contract vulnerability detection method based on static profit analysis includes the following steps:
[0008] Step 1: Use the solc compiler to compile the smart contract written in Solidity into the Yul intermediate language and convert it into a control flow graph;
[0009] Step 2: Perform path-sensitive symbolic execution analysis on the control flow graph, and collect path information during the symbolic execution analysis process to identify the following four types of behaviors: possible profit behaviors of the caller and the corresponding beneficiaries, possible expenditure behaviors of the caller, caller permission check behaviors, and beneficiary check behaviors;
[0010] Step 3: When the symbolic execution analysis encounters the end of each path, all collected information is combined to determine whether the path may have a vulnerability;
[0011] Step 4: After analyzing all possible paths or reaching the preset program running time, organize and output the vulnerability detection results.
[0012] Furthermore, the possible profit-making behaviors of the caller include profit-making behaviors brought about by internal ledger modifications, profit-making behaviors brought about by external contract calls based on EIP standards including ERC20, and profit-making behaviors of native tokens.
[0013] Furthermore, the profit-making behavior brought about by the modification of the internal ledger includes: the destruction of tokens of the address controlled by the callee, the destruction of tokens of the transaction pool address, and the minting of tokens of the address controlled by the callee; the beneficiary of the profit-making behavior brought about by the modification of the internal ledger is the owner address variable of the modified internal ledger.
[0014] Furthermore, the profit behavior brought about by external contract calls based on EIP standards including ERC20 is the transfer behavior or authorization behavior to the address controlled by the callee, that is, searching whether there is a transfer or approve function, and the first parameter of the corresponding function call is the address controlled by the callee; the beneficiary of the profit behavior brought about by external contract calls based on EIP standards including ERC20 is the first parameter variable of the function call, that is, the address variable that is transferred or authorized.
[0015] Furthermore, the possible spending behaviors of the caller include spending behaviors caused by internal ledger modifications, spending behaviors caused by external contract calls based on EIP standards including ERC20, and spending behaviors of the caller's native tokens.
[0016] Furthermore, the caller's spending behavior caused by the internal ledger modification includes: the contract under test reduces the spending behavior of the storage variable "_allowances" authorized to the caller, and the contract under test reduces the spending behavior of any storage variable under the name of the caller except "_allowances".
[0017] Furthermore, the caller spending behavior caused by external contract calls based on EIP standards including ERC20 includes the external function call behavior of the contract under test calling the transferFrom / safeTransferFrom function, and the first parameter is the caller's external function call behavior, and through the previous and next ledgers, that is, using the balanceOf external call or address(this).balance to view the token balance including ERC20 or the native token balance, and compare them to determine the behavior of increasing the balance of this contract.
[0018] Furthermore, when the symbolic execution analysis encounters a conditional branch, the caller permission check behavior and the variable check behavior are identified in combination with the actual branch path selection of the current symbolic execution analysis; the variable check behavior is matched with the beneficiary information of the profit behavior record, thereby identifying the beneficiary check behavior;
[0019] The caller's permissions are checked when the conditional branch meets the following two points:
[0020] (1) This conditional branch means that the caller compares the contract's built-in data, which includes storage type data, constant type data, and immutable type data;
[0021] (2) The value selected by the conditional branch in the current execution path, which means that the conditional branch restricts the caller to the value range built into the contract;
[0022] The variable is checked when the conditional branch meets the following two conditions:
[0023] (1) This conditional branch means that the variable is compared with the contract's built-in data, which includes storage type data, constant type data, and immutable type data;
[0024] (2) The value selected by the conditional branch in the current execution path indicates that the conditional branch restricts the variable to the value range built into the contract.
[0025] Furthermore, the step three includes the following sub-steps:
[0026] (3.1) Determine whether the path ends with revert. If so, it means that the path cannot be successfully executed and the path analysis ends; otherwise, continue to execute (3.2);
[0027] (3.2) Is there any caller permission check in the path? If yes, it is considered that there is no vulnerability and the path analysis ends. Otherwise, continue to execute (3.3);
[0028] (3.3) Traverse and analyze each caller profit behavior collected in step 2 to determine whether it is "token destruction behavior of the transaction pool address". If so, directly execute (3.4); otherwise, determine whether there is a profit check behavior corresponding to the profit behavior in the path. If so, exclude the profit behavior; otherwise, continue to execute (3.4);
[0029] (3.4) Determine whether there is an expenditure behavior that needs to be considered for the profit behavior in the current execution path. If so, it is considered that there is no vulnerability and the profit behavior is excluded; otherwise, it is considered that there is a vulnerability in the path, and the profit point brought by the vulnerability is the corresponding code of the profit behavior, and the vulnerability detection result is recorded.
[0030] A smart contract vulnerability detection device based on static profit analysis includes one or more processors for implementing a smart contract vulnerability detection method based on static profit analysis.
[0031] The beneficial effects of the present invention are as follows:
[0032] The smart contract vulnerability detection method of the present invention is based on the static analysis level of smart contracts, and uses path-sensitive symbolic execution to simulate the possible execution paths of contracts, and find program execution paths that callers can profit without permission checks and without spending any expenses. This detection method fits the business semantics of smart contracts, can more effectively identify contract code vulnerabilities, and has accurate identification effects and high identification coverage. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] Figure 1 It is an overall flow chart of the smart contract vulnerability detection method based on static profit analysis according to an embodiment of the present invention.
[0034] Figure 2 It is a flow chart that determines whether there is a vulnerability in the path at the end of the path.
[0035] Figure 3 Schematic diagram of a smart contract vulnerability detection device based on static profit analysis according to an embodiment. DETAILED DESCRIPTION
[0036] The present invention will be described in detail below based on the accompanying drawings and preferred embodiments, and the purpose and effects of the present invention will become more clear. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0037] As one of the implementation methods, Figure 1 As shown, the smart contract vulnerability detection method based on static profit analysis of this embodiment includes the following steps:
[0038] Step 1: Use the solc compiler to compile the smart contract written in Solidity into the Yul intermediate language and convert it into a control flow graph.
[0039] Step 2: Perform path-sensitive symbolic execution analysis on the control flow graph (hereinafter referred to as CFG), and collect path information during the symbolic execution analysis process to identify the following four types of behaviors: possible profit behaviors of the caller and the corresponding beneficiaries, possible expenditure behaviors of the caller, caller permission check behaviors, and beneficiary check behaviors.
[0040] (2.1) Possible profit-making behaviors of the caller and the corresponding beneficiaries
[0041] The possible profit-making behaviors of the caller include: profit-making behaviors brought about by internal ledger modifications, profit-making behaviors brought about by external contract calls based on EIP standards including ERC20, and profit-making behaviors brought about by native tokens.
[0042] (2.1.1) Profit-making activities resulting from internal ledger modification include: destroying tokens of addresses controlled by the callee, destroying tokens of transaction pool addresses, and minting tokens of addresses controlled by the callee.
[0043] The beneficiary of the profit behavior brought about by the modification of the internal ledger is the owner address variable of the modified internal ledger.
[0044] Although the token destruction behavior here does not make any profit for the address of the destroyed token, it is actually an indirect profit behavior for the caller. In the detection rules, the address of the destroyed token is set as the beneficiary here.
[0045] (2.1.2) Profitable behavior brought by external contract calls based on EIP standards including ERC20
[0046] This behavior is a transfer or authorization behavior to the address controlled by the callee, that is, searching for a transfer or approve function, and the first parameter of the corresponding function call is the address controlled by the callee. The beneficiary of this behavior is the first parameter variable of the function call, that is, the address variable that is transferred or authorized.
[0047] (2.1.3) Profit-making behavior of native tokens
[0048] This behavior is the act of transferring native tokens to the address controlled by the callee. The beneficiary of this behavior is the address variable that receives the native token transfer.
[0049] The "address controlled by the callee" in (2.1) above means: the address directly derived from the calldata parameter variable when calling an external function, or the new variable obtained by calculating the calldata parameter.
[0050] (2.2) Possible spending behavior of the caller (i.e. msg.sender)
[0051] This behavior includes three categories: caller spending behavior caused by internal ledger modifications, caller spending behavior caused by external contract calls based on EIP standards including ERC20, and caller native token spending behavior.
[0052] (2.2.1) Caller spending behavior caused by internal ledger modification
[0053] Depending on the storage variable being modified, two modification behaviors are identified:
[0054] 1. The contract under test reduces the spending behavior of authorizing the caller's "_allowances" storage variable;
[0055] 2. The contract under test reduces the spending behavior of any storage variables under the caller's name except "_allowances".
[0056] (2.2.2) Caller spending behavior caused by external contract calls based on EIP standards including ERC20
[0057] This includes the contract under test calling the transferFrom / safeTransferFrom function, with the first parameter being the caller's external function call behavior, and checking the previous and next ledgers, that is, using the balanceOf external call or address(this).balance to check the token balance including ERC20 or the native token balance, and comparing them to determine the behavior of increasing the balance of this contract.
[0058] (2.2.3) Native Token Spending Behavior
[0059] That is, the code behavior of the contract under test using msg.value (msg.value represents the number of native tokens received by the contract under test in the current transaction. In this method, it means that in this program path, the contract under test requires the caller to pay the native token).
[0060] (2.3) Caller permission check behavior
[0061] When the symbolic execution analysis encounters a conditional branch, the actual branch path selection of the current symbolic execution analysis and the condition of the conditional branch are combined to identify the caller permission check behavior. Specifically:
[0062] There is a behavior where the caller's permissions are checked, which requires the conditional branch to meet the following two points:
[0063] (a) This conditional branch means that the caller (msg.sender) is compared with the contract's built-in data, which includes storage type data, constant type data, and immutable type data.
[0064] (b) The value selected by the conditional branch in the current execution path indicates that the conditional branch restricts the caller to the value range built into the contract.
[0065] For example: Assuming that the owner variable is the owner address of this contract, which is the storage address variable in the contract, for the if (msg.sender == owner) judgment, when the if conditional branch takes the false branch, the check does not limit the caller to the value within the contract built-in range. For if taking the true branch, the check limits the caller to the value within the contract built-in range.
[0066] If both of the above points are met, it is considered that the caller permissions on this path are checked and there is a caller permission check behavior.
[0067] (2.4) Profit-seeker inspection behavior
[0068] In order to collect the profit check behavior, it is necessary to collect all variable check behaviors in the path analysis. Specifically, when the symbolic execution analysis encounters a conditional branch, the variable check behavior is identified by combining the actual branch path selection of the current symbolic execution analysis and the condition of the conditional branch. A variable is checked in the current analysis path if the following two points are met:
[0069] (a) This conditional branch means that the variable is compared with the contract's built-in data, which includes storage type data, constant type data, and immutable type data.
[0070] (b) The value selected by the conditional branch in the current execution path indicates that the conditional branch restricts the variable to the value range built into the contract.
[0071] If both of the above conditions are met, the variable is checked. For checked variables, if the variable is calculated by keccak256, abi.encode, abi.encodePacked, then the components of keccak256, abi.encode, abi.encodePacked are also checked.
[0072] After obtaining the variable inspection behavior information of the path, it is matched with the beneficiary information recorded in step (2.1) to collect the beneficiary inspection behavior information.
[0073] Step 3: When the symbolic execution analysis encounters the end of each path, all the collected information is combined to determine whether the path may have a vulnerability, as follows: Figure 2 As shown, this is achieved through the following sub-steps:
[0074] (3.1) Determine whether the path ends with revert. If so, it means that the path cannot be successfully executed and the path analysis ends; otherwise, continue to execute (3.2);
[0075] (3.2) Is there any caller permission check in the path? If yes, it is considered that there is no vulnerability and the path analysis ends. Otherwise, continue to execute (3.3);
[0076] (3.3) Traverse and analyze each caller profit behavior collected in step 2 to determine whether it is "token destruction behavior of the transaction pool address". If so, directly execute (3.4); otherwise, determine whether there is a profit check behavior corresponding to the profit behavior in the path. If so, exclude the profit behavior; otherwise, continue to execute (3.4);
[0077] (3.4) Determine whether there is an expenditure behavior that needs to be considered for the profit behavior in the current execution path. If so, it is considered that there is no vulnerability and the profit behavior is excluded; otherwise, it is considered that there is a vulnerability in the path, and the profit point brought by the vulnerability is the corresponding code of the profit behavior, and the vulnerability detection result is recorded.
[0078] Step 4: After analyzing all possible paths or reaching the preset program running time, organize and output the vulnerability detection results.
[0079] On the other hand, corresponding to the aforementioned embodiment of the smart contract vulnerability detection method based on static profit analysis, the present invention also provides an embodiment of a smart contract vulnerability detection device based on static profit analysis.
[0080] See also Figure 3, an embodiment of the present invention provides a smart contract vulnerability detection device based on static profit analysis, including one or more processors, which are used to implement the smart contract vulnerability detection method based on static profit analysis in the above embodiment.
[0081] The embodiments of the smart contract vulnerability detection device based on static profit analysis of the present invention can be applied to any device with data processing capabilities, and the device with data processing capabilities can be a device or apparatus such as a computer. The device embodiment can be implemented through software, or through hardware or a combination of software and hardware. Taking software implementation as an example, as a device in a logical sense, it is formed by the processor of any device with data processing capabilities in which it is located reading the corresponding computer program instructions in the non-volatile memory into the memory for execution. From the hardware level, if Figure 3 As shown, it is a hardware structure diagram of any device with data processing capability where the smart contract vulnerability detection device based on static profit analysis of the present invention is located, except Figure 3 In addition to the processor, memory, network interface, and non-volatile memory shown, any device with data processing capabilities in which the apparatus in the embodiments is located may also include other hardware, generally based on the actual functions of the device with data processing capabilities, which will not be described in detail.
[0082] The implementation process of the functions and effects of each unit in the above-mentioned device is specifically described in the implementation process of the corresponding steps in the above-mentioned method, and will not be repeated here.
[0083] For the device embodiment, since it basically corresponds to the method embodiment, the relevant parts can refer to the partial description of the method embodiment. The device embodiment described above is only schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of the present invention. Ordinary technicians in this field can understand and implement it without paying creative work.
[0084] An embodiment of the present invention also provides a computer-readable storage medium on which a program is stored. When the program is executed by a processor, the general image target detection method based on the self-attention mechanism in the above embodiment is implemented.
[0085] The computer-readable storage medium may be an internal storage unit of any device with data processing capability described in any of the aforementioned embodiments, such as a hard disk or a memory. The computer-readable storage medium may also be an external storage device, such as a plug-in hard disk, a smart media card (SMC), an SD card, a flash card, etc. equipped on the device. Furthermore, the computer-readable storage medium may also include both an internal storage unit of any device with data processing capability and an external storage device. The computer-readable storage medium is used to store the computer program and other programs and data required by any device with data processing capability, and may also be used to temporarily store data that has been output or is to be output.
[0086] Those skilled in the art can understand that the above are only preferred examples of the invention and are not intended to limit the invention. Although the invention is described in detail with reference to the above examples, those skilled in the art can still modify the technical solutions recorded in the above examples or replace some of the technical features therein with equivalents. Any modification, equivalent replacement, etc. made within the spirit and principle of the invention shall be included in the protection scope of the invention.
Claims
1. A smart contract vulnerability detection method based on static profit analysis, characterized in that: The following steps are involved: Step 1: Use the solc compiler to compile the smart contract written in Solidity into the Yul intermediate language and convert it into a control flow graph; Step 2: Perform path-sensitive symbolic execution analysis on the control flow graph, and collect path information during the symbolic execution analysis process to identify the following four types of behaviors: possible profit behaviors of the caller and the corresponding beneficiaries, possible expenditure behaviors of the caller, caller permission check behaviors, and beneficiary check behaviors; Step 3: When the symbolic execution analysis encounters the end of each path, all collected information is combined to determine whether the path may have a vulnerability; Step 4: After analyzing all possible paths or reaching the preset program running time, organize and output the vulnerability detection results.
2. The method for detecting smart contract vulnerabilities based on static profit analysis according to claim 1, characterized in that: The possible profit-making behaviors of the caller include profit-making behaviors brought about by internal ledger modifications, profit-making behaviors brought about by external contract calls based on EIP standards including ERC20, and profit-making behaviors brought about by native tokens.
3. The method for detecting smart contract vulnerabilities based on static profit analysis according to claim 2, characterized in that: The profit-making behavior brought about by the modification of the internal ledger includes: the destruction of tokens of the address controlled by the callee, the destruction of tokens of the transaction pool address, and the minting of tokens of the address controlled by the callee; the beneficiary of the profit-making behavior brought about by the modification of the internal ledger is the owner address variable of the modified internal ledger.
4. The method for detecting smart contract vulnerabilities based on static profit analysis according to claim 2, characterized in that: The profit behavior brought about by external contract calls based on EIP standards including ERC20 is the transfer behavior or authorization behavior to the address controlled by the callee, that is, searching whether there is a transfer or approve function, and the first parameter of the corresponding function call is the address controlled by the callee; the beneficiary of the profit behavior brought about by external contract calls based on EIP standards including ERC20 is the first parameter variable of the function call, that is, the address variable that is transferred or authorized.
5. The method for detecting smart contract vulnerabilities based on static profit analysis according to claim 1, characterized in that: The possible spending behaviors of the caller include spending behaviors caused by internal ledger modifications, spending behaviors caused by external contract calls based on EIP standards including ERC20, and spending behaviors of the caller's native tokens.
6. The method for detecting smart contract vulnerabilities based on static profit analysis according to claim 5, characterized in that: The caller's spending behaviors caused by the internal ledger modification include: the contract under test reduces the spending behavior of the storage variable "_allowances" authorized to the caller, and the contract under test reduces the spending behavior of any storage variable under the caller's name except "_allowances".
7. The method for detecting smart contract vulnerabilities based on static profit analysis according to claim 5, characterized in that: The caller spending behavior caused by external contract calls based on EIP standards including ERC20, including the test contract calling the transferFrom / safeTransferFrom function, and the first parameter is the caller's external function call behavior, and through the previous and next ledgers, that is, using the balanceOf external call or address(this).balance to view the token balance including ERC20 or the native token balance, and compare them to determine the behavior of increasing the balance of this contract.
8. The method for detecting smart contract vulnerabilities based on static profit analysis according to claim 1, characterized in that: When the symbolic execution analysis encounters a conditional branch, the caller permission check behavior and variable check behavior are identified based on the actual branch path selection of the current symbolic execution analysis; Matching the variable checking behavior with the information of the winners recorded by the profit behavior, thereby identifying the winner checking behavior; The caller's permissions are checked when the conditional branch meets the following two points: (1) This conditional branch means that the caller compares the contract's built-in data, which includes storage type data, constant type data, and immutable type data; (2) The value selected by the conditional branch in the current execution path, which means that the conditional branch restricts the caller to the value range built into the contract; The variable is checked when the conditional branch meets the following two conditions: (1) This conditional branch means that the variable is compared with the contract's built-in data, which includes storage type data, constant type data, and immutable type data; (2) The value selected by the conditional branch in the current execution path indicates that the conditional branch restricts the variable to the value range built into the contract.
9. The method for detecting smart contract vulnerabilities based on static profit analysis according to claim 1, characterized in that: The step three includes the following sub-steps: (3.1) Determine whether the path ends with revert. If so, it means that the path cannot be successfully executed and the path analysis ends; otherwise, continue to execute (3.2); (3.2) Is there any caller permission check in the path? If yes, it is considered that there is no vulnerability and the path analysis ends. Otherwise, continue to execute (3.3); (3.3) Traverse and analyze each profit-making behavior of the caller collected in step 2 to determine whether it is "token destruction behavior of the transaction pool address". If so, directly execute (3.4); otherwise, determine whether there is a profit-making check behavior corresponding to the profit-making behavior in the path. If so, exclude the profit-making behavior; otherwise, continue to execute (3.4); (3.4) Determine whether there is an expenditure behavior that needs to be considered for the profit behavior in the current execution path. If so, it is considered that there is no vulnerability and the profit behavior is excluded; otherwise, it is considered that there is a vulnerability in the path, and the profit point brought by the vulnerability is the corresponding code of the profit behavior, and the vulnerability detection result is recorded.
10. A smart contract vulnerability detection device based on static profit analysis, characterized in that: It includes one or more processors for implementing the smart contract vulnerability detection method based on static profit analysis as described in any one of claims 1 to 9.
Citation Information
Patent Citations
Intelligent contract vulnerability checking method, system and medium
CN110399730A
Abnormal virtual currency wallet address detection method based on graph neural network
CN115375480A
Transaction blocking method and device aiming at intelligent contract vulnerability attack
CN115442380A
Method and system for detecting ERC20 token contract authentication vulnerability and storage medium
CN115510448A
KR20200094618A
