ERC777 contract re-entry attack detection method and system
Through the combination of static analysis and fuzz testing, the ability to detect the reentry attack of ERC777 contracts has been solved, and the problem of the existing technology being unable to detect the reentry attack of newly deployed contracts on existing contracts has been achieved, achieving accurate and cost-effective detection effects.
Patent Information
- Application Number
- CN202411266350.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-10
- Publication Date
- 2024-12-06
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The existing smart contract reentry attack detection method cannot effectively detect whether the newly deployed contract can launch reentry attacks on existing contracts, resulting in the owner of target contracts on the chain being unable to take targeted defense measures.
Use static analysis to obtain the ERC777 contract that can initiate reentry attacks as the contract to be deployed, and deploy the contract to be deployed and the target contracts based on the contract dependencies. Use fuzzy tests to generate test cases and analyze information during the execution process to determine whether the reentry attack is successful. and whether the attack is harmful.
Through the combination of static analysis and fuzz testing, we ensure the accuracy of reentry attack detection of ERC777 contracts, reduce the detection set of dynamic tests, reduce labor costs, and allow target contract users to take targeted defense measures.
Smart Images

Figure CN119094213A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of blockchain reentry attack detection, and specifically to an ERC777 contract reentry attack detection method and system. Background Art
[0002] The ERC777 standard is an improvement and expansion of the ERC20 standard, which introduces more advanced functions and security measures. Among them, the ERC777 standard proposes a callback function called Hook, which enables the contract to meet more complex needs and scenarios. However, since the function of the Hook function is uncertain and depends on the implementation and timing of the external caller, this allows the ERC777 contract to launch a reentrancy attack on on-chain smart contracts with specific structures.
[0003] Traditional smart contract vulnerability detection methods are mainly based on static analysis or dynamic testing. However, these methods have certain limitations. First, static analysis methods have two major problems: unreachable paths and path explosion. Although dynamic testing can ensure a sufficiently low false alarm rate, this method cannot guarantee the coverage of the contract execution path, resulting in a high false alarm rate. The existing smart contract reentrancy defect detection method based on dynamic execution information analysis cannot guarantee the path coverage of randomly generated test cases, has a low false alarm rate, and this method uses a random generation method to generate test cases. The random generation method is only applicable to some simple scenarios. For complex scenarios such as reentrancy vulnerability detection, it may lead to an increase in false alarm rate. Although the blockchain smart contract vulnerability detection method based on hybrid fuzzy testing combines static analysis and dynamic testing methods, it can be seen from the method execution steps that it is not suitable for reentrancy vulnerability detection. First, reentrancy attacks require interaction between contracts, but this method does not deploy the attack contract. In addition, there are certain dependencies between contract deployments, which are not considered by this method. Therefore, this method cannot detect reentrancy vulnerabilities. For this reason, we propose an ERC777 contract reentrancy attack detection method and system to solve the above problems. Summary of the invention
[0004] The three methods mentioned in the background technology only perform vulnerability detection on target contracts with reentrancy vulnerabilities. A large number of smart contracts with reentrancy vulnerabilities have been deployed on the current blockchain. The above methods cannot detect whether the newly deployed contract can launch a reentrancy attack on the existing contract; therefore, the owner of the target contract on the chain cannot take targeted defensive measures, such as building a contract call blacklist. In response to the above problems, the present invention proposes an ERC777 contract reentrancy attack detection method and system, which uses static analysis to obtain an ERC777 contract that can launch a reentrancy attack as a contract to be deployed and a target contract with a specific structure on the chain, and then deploys the contract to be deployed and the target contract according to the contract dependency relationship. Finally, fuzz testing is used to generate test cases, execute the test cases and analyze the information during the execution process to determine whether the reentrancy attack is successful and whether the attack is harmful, so as to determine whether the contract to be deployed can launch a harmful reentrancy attack.
[0005] To achieve the above object, the present invention provides the following technical solutions: a method and system for detecting reentrancy attacks on ERC777 contracts, comprising a contract to be tested and a target contract, wherein the contract to be tested is an ERC777 contract containing a hook function in the contract to be deployed, and the target contract is a contract that interacts with the contract to be tested and may have a reentrancy vulnerability, and the method comprises the following steps:
[0006] Step S1, construct an abstract syntax tree of the contract to be deployed, identify the ERC777 contract containing the hook function according to the function definition node in the syntax tree, load the corresponding contract into the contract pool to be tested, and add the corresponding hook function in the contract to the first function candidate pool, and at the same time, construct the dependency relationship between the contract to be tested and the deployed contract on the chain according to the abstract syntax tree, and construct a target contract set, perform static analysis on the code of the called function in the set to determine whether there is a possibility of reentrancy attack, and determine that the contract does not have a mechanism to prevent attacks, and add the functions that meet both conditions to the second function candidate pool;
[0007] Step S2: Determine the deployment order of the contract to be tested and the target contract according to the dependency relationship between the contract to be tested and the deployed contract on the chain, deploy the contract to be tested and the target contract in the test network respectively, use an automated script or automated deployment tool to generate a contract deployment transaction, randomly select a candidate function from the first function candidate pool and the second function candidate pool respectively, randomly generate a test case according to the parameter type of the candidate function, execute the corresponding transaction according to the test case and record the address information generated during the execution process, collect the execution information of the opcode in the Ethereum virtual machine, and record the corresponding contract state variable information after each opcode is executed and received;
[0008] Step S3: Analyze whether the transaction in step S2 is successfully executed, generate a call graph based on the opcode information in the Ethereum virtual machine to analyze whether a reentrancy attack occurs, and analyze whether the contract under test is aggressive to the target contract based on the changes in the corresponding contract state variables.
[0009] Preferably, the specific operations of step S1 are as follows:
[0010] Step S11: construct an abstract syntax tree of the contract to be deployed, and use regular expressions to delete irrelevant information in the contract code to be tested;
[0011] Step S12: traverse the abstract syntax tree of the contract to be deployed, identify the ERC777 contract containing the Hook function, load the corresponding contract into the contract pool to be tested, and add the corresponding hook function in the contract to the first function candidate pool;
[0012] Step S12: traverse the abstract syntax tree of the contract to be tested, and construct a set of dependencies between the contract to be tested and the deployed contracts on the chain;
[0013] Step S13, mark the contract that interacts with the hook function among the deployed contracts on the chain as the target contract, and construct the target contract set based on it;
[0014] Step S14, statically analyze the code of the called function in the target contract to determine whether it is at risk of reentrancy attack, and determine whether the contract does not have a mechanism to prevent attacks, and add functions that meet both conditions to the second function candidate pool.
[0015] Preferably, step S13 also includes the following specific steps of screening the target contract: filtering functions with private, view, pure and constant modifiers. When there is no callable function in the target contract, remove it from the target contract set and cancel the mark of the target contract.
[0016] Preferably, the specific operation of step S2 is as follows:
[0017] Step S21: Determine the deployment order of the contract to be tested and the target contract according to the dependency set, write an automated script or use an automated deployment tool to generate a contract deployment transaction, deploy the contract to be tested and the target contract in the test network respectively, and obtain the address of the contract to be tested and the target contract through the transaction receipt or transaction hash;
[0018] Step S22: Select a first function from the first function candidate pool, select a second function from the second function candidate pool, and randomly generate test cases according to the parameter types of the candidate functions.
[0019] Step S23: Execute the corresponding transaction according to the test case and record the address information generated during the execution process, collect the execution information of the operation code in the Ethereum virtual machine, and record the corresponding contract state variable information after each operation code is executed and received.
[0020] Preferably, the address information includes the calling contract address and the called contract address;
[0021] Execution information includes arithmetic operations, logical operations, and I / O operations;
[0022] State variable information includes persistent data declared in an Ethereum smart contract, which is saved on the blockchain and remains unchanged between different function calls of the contract.
[0023] Preferably, whenever a transaction is sent on the Ethereum network, the blockchain executes the transaction and generates a transaction receipt for providing detailed information on the transaction execution result. The specific operations of step S3 are as follows:
[0024] Step S31: Determine whether the transaction is successfully executed by checking the status field in the transaction receipt;
[0025] Step S32, generating a call graph according to the address information generated during the execution of step S2;
[0026] Step S33: Determine whether the address of the calling contract and the address of the called contract appear repeatedly during the execution process through the call graph;
[0027] Step S34: through the execution information of the operation code in step S2, determine whether the transfer operation occurs repeatedly, and the calling contract address is the same as the called contract address during the transfer process. If multiple transfer operations occur, analyze whether the corresponding contract state variable information is abnormal. If a change in the state variable occurs, it can be determined that the reentry attack is harmful, that is, the contract under test has the ability to attack the target contract.
[0028] An ERC777 contract reentry attack detection system, the system comprising the following modules;
[0029] Contract static analysis module: constructs the abstract syntax tree of the contract to be deployed, identifies the ERC777 contract containing the hook function according to the function definition node in the syntax tree, loads the corresponding contract into the contract pool to be tested, and adds the corresponding hook function in the contract to the first function candidate pool. At the same time, the dependency relationship between the contract to be tested and the deployed contract on the chain is constructed according to the abstract syntax tree, and the target contract set is constructed. The code of the called function in the set is statically analyzed to determine whether there is a possibility of reentrancy attack, and whether the contract does not have a mechanism to prevent attacks. Functions that meet both conditions are added to the second function candidate pool;
[0030] Fuzz testing module: Determine the deployment order of the contract to be tested and the target contract according to the dependency relationship between the contract to be tested and the deployed contracts on the chain, deploy the contract to be tested and the target contract in the test network respectively, use automated scripts or automated deployment tools to generate contract deployment transactions, randomly select a candidate function from the first function candidate pool and the second function candidate pool respectively, randomly generate test cases according to the parameter types of the candidate functions, execute the corresponding transactions according to the test cases and record the address information generated during the execution process, collect the execution information of the opcodes in the Ethereum virtual machine, and record the corresponding contract state variable information after each opcode is executed and received;
[0031] Vulnerability detection module: Whether the transaction is executed successfully, generate a call graph based on the opcode information in the Ethereum virtual machine to analyze whether a reentrancy attack has occurred, and analyze whether the contract under test is aggressive to the target contract based on the changes in the corresponding contract state variables.
[0032] Compared with the prior art, the present invention has the following beneficial effects:
[0033] First, the present invention uses static analysis to detect ERC777 contracts and related on-chain contracts with attack capabilities, reducing the detection set of dynamic testing.
[0034] Second, the present invention reduces labor costs by automatically deploying contracts through contract dependencies while ensuring the normal execution of contracts.
[0035] Third, the present invention uses fuzz testing to test the results of static analysis to ensure that the corresponding ERC777 contract has the ability to launch harmful reentry attacks, so that the target contract users can take targeted defensive measures. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] Figure 1 It is a schematic diagram of the process of the present invention;
[0037] Figure 2 It is the contract static analysis framework of the present invention;
[0038] Figure 3 It is the fuzzy testing framework of the present invention;
[0039] Figure 4 It is the vulnerability detection framework of the present invention. DETAILED DESCRIPTION
[0040] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0041] The Hook function is a concept introduced by the ERC777 standard. Through the Hook function, the contract can customize the behavior of sending and receiving rewards as needed, which makes ERC777 rewards more flexible and applicable to a wider range of use cases. There are two important Hook functions in this standard, namely tokensToSend and tokensToReceived. The former is a function triggered during the reward sending process. When the reward holder tries to send a reward, the contract can call this function to check and modify the parameters of the transaction. The latter is triggered by the reward receiving. These Hook functions allow contracts to insert custom logic in the token sending and receiving process to achieve more flexible interactions and functions.
[0042] like Figure 1 As shown, a method for detecting reentry attacks on ERC777 contracts includes the following steps:
[0043] Step S1, construct an abstract syntax tree of the contract to be deployed, identify the ERC777 contract containing the hook function according to the function definition node in the syntax tree, load the corresponding contract into the contract pool to be tested, and add the corresponding hook function in the contract to the first function candidate pool, and at the same time, construct the dependency relationship between the contract to be tested and the deployed contract on the chain according to the abstract syntax tree, and construct a target contract set, perform static analysis on the code of the called function in the set to determine whether there is a possibility of reentrancy attack, and determine that the contract does not have a mechanism to prevent attacks, and add the functions that meet both conditions to the second function candidate pool;
[0044] Step S2: Determine the deployment order of the contract to be tested and the target contract according to the dependency relationship between the contract to be tested and the deployed contract on the chain, deploy the contract to be tested and the target contract in the test network respectively, use an automated script or automated deployment tool to generate a contract deployment transaction, randomly select a candidate function from the first function candidate pool and the second function candidate pool respectively, randomly generate a test case according to the parameter type of the candidate function, execute the corresponding transaction according to the test case and record the address information generated during the execution process, collect the execution information of the opcode in the Ethereum virtual machine, and record the corresponding contract state variable information after each opcode is executed and received;
[0045] Step S3: Analyze whether the transaction in step S2 is successfully executed, generate a call graph based on the opcode information in the Ethereum virtual machine to analyze whether a reentrancy attack occurs, and analyze whether the contract under test is aggressive to the target contract based on the changes in the corresponding contract state variables.
[0046] Specifically, the contract to be tested is the ERC777 contract containing the hook function in the contract to be deployed, and the target contract is the contract in the deployed contract on the chain that interacts with the hook function in the ERC777 contract. The present invention first performs static analysis on the smart contract code to obtain the contract to be deployed of type ERC777 and with the Hook function, and then obtains the dependencies between the contracts according to the abstract syntax tree, obtains the target contract with a specific structure on the chain according to the contract dependency, and completes the automatic deployment of the contract to be deployed and the target contract. The contract to be deployed attacks the target contract according to the test case generated by the fuzzy test, and finally determines whether there is a reentrancy vulnerability according to the contract execution information generated during the attack.
[0047] like Figure 2 As shown, the specific operations of step S1 are as follows:
[0048] Step S11: construct an abstract syntax tree of the contract to be deployed, and use regular expressions to delete irrelevant information in the contract code to be tested;
[0049] Step S12: traverse the abstract syntax tree of the contract to be deployed, identify the ERC777 contract containing the Hook function, load the corresponding contract into the contract pool to be tested, and add the corresponding hook function in the contract to the first function candidate pool;
[0050] Step S12: traverse the abstract syntax tree of the contract to be tested, and construct a set of dependencies between the contract to be tested and the deployed contracts on the chain;
[0051] Step S13, mark the contract that interacts with the hook function among the deployed contracts on the chain as the target contract, and construct the target contract set based on it;
[0052] Step S14, statically analyze the code of the called function in the target contract to determine whether it is at risk of reentrancy attack, and determine whether the contract does not have a mechanism to prevent attacks, and add functions that meet both conditions to the second function candidate pool.
[0053] Specifically, step S12 traverses the contract abstract syntax tree, determines whether the contract to be deployed is an ERC777 contract according to whether ERC777 is included in the contract inheritance relationship, identifies the function definition node in the abstract syntax tree, determines whether the contract contains hook functions such as tokensToSend and tokensToRecived, and adds the corresponding hook function to the first function candidate pool, which records the function selector and parameter list respectively. The former is used to uniquely identify a function, and the latter helps fuzz testing generate test cases by recording function parameter types.
[0054] Specifically, in the process of traversing the abstract syntax tree of the contract to be tested, contract references are identified to build dependencies between contracts. Dependencies include function calls, contract instantiations, inheritance relationships, etc., and a dependency set between the contract to be tested and the deployed contracts on the chain is built. Then, the contracts that interact with the hook function in the deployed contracts on the chain are marked as target contracts, and the target contract set is built accordingly.
[0055] Specifically, the implementation method of step S14 is to perform static analysis on the code of the called function in the target contract to determine whether it has specific characteristics:
[0056] Feature 1: The ability to transfer money first and then receive rewards;
[0057] Feature 2: There is no mutex lock;
[0058] Structure 1 is used to determine whether the function is subject to a reentrancy attack, because the ERC777 contract can decide which attack to launch through the hook function. Structure 2 is used to determine whether the function already has a mechanism to prevent attacks. The target contract function information that meets both features 1 and 2 is added to the second function candidate pool.
[0059] Existing fuzz testing aims to discover potential defects and security vulnerabilities in applications or systems. It is an automated testing method that triggers abnormal situations by inputting a large amount of random, invalid or abnormal data into the target program to explore the boundary conditions and unstable behaviors of the application; fuzz testing generally includes the following steps: selecting targets, generating test data, sending test data, monitoring target programs, collecting feedback, and optimizing tests; the simplest test case generation method is random mutation, which is suitable for simple test scenarios, but may not be effective for complex scenarios. Mutation generation refers to generating new test data by modifying and mutating existing test data. This method helps to explore the diversity of inputs; for specific input formats, syntax-aware generation can generate test data that conforms to the format according to grammatical rules; symbolic execution is an advanced fuzz testing technology that belongs to static analysis technology. It does not directly use specific input data during testing, but uses symbols to represent variables and symbolizes the execution path of the program; through the constraint solver executed on the symbolic path, symbolic execution can automatically generate input to achieve the purpose of exploring as many execution paths and test situations as possible; however, the computational complexity of this method is too high and the degree of automation is relatively low. The present invention reduces labor costs by automatically deploying contracts through contract dependencies while ensuring the normal execution of contracts.
[0060] like Figure 3 As shown, the specific operation of step S2 is as follows;
[0061] Step S21: Determine the deployment order of the contract to be tested and the target contract according to the dependency set, write an automated script or use an automated deployment tool to generate a contract deployment transaction, deploy the contract to be tested and the target contract in the test network respectively, and obtain the address of the contract to be tested and the target contract through the transaction receipt or transaction hash;
[0062] Step S22: Select a first function from the first function candidate pool, select a second function from the second function candidate pool, and randomly generate test cases according to the parameter types of the candidate functions.
[0063] Step S23: Execute the corresponding transaction according to the test case and record the address information generated during the execution process, collect the execution information of the operation code in the Ethereum virtual machine, and record the corresponding contract state variable information after each operation code is executed and received.
[0064] Specifically, step S22 randomly generates corresponding parameter values to form test cases. Since the coverage rate of the random generation method is low, it can be supplemented by a more targeted grammar-based test case generation method, such as a mutation generation method that makes slight modifications to existing valid input data, and a combination generation method that combines multiple input data to generate complex test cases. In order to ensure that the contract to be deployed can initiate transactions for reentrancy attacks, a certain amount of rewards need to be transferred to the contract to be deployed for initiating transactions.
[0065] Specifically, the address information includes the calling contract address and the called contract address;
[0066] Execution information includes arithmetic operations, logical operations, and I / O operations;
[0067] State variable information includes persistent data declared in an Ethereum smart contract, which is saved on the blockchain and remains unchanged between different function calls of the contract.
[0068] like Figure 4 As shown, the specific operations of step S3 are as follows:
[0069] Step S31: Determine whether the transaction is successfully executed by checking the status field in the transaction receipt;
[0070] Step S32, generating a call graph according to the address information generated during the execution of step S2;
[0071] Step S33: Determine whether the address of the calling contract and the address of the called contract appear repeatedly during the execution process through the call graph;
[0072] Step S34: through the execution information of the operation code in step S2, determine whether the transfer operation occurs repeatedly, and the calling contract address is the same as the called contract address during the transfer process. If multiple transfer operations occur, analyze whether the corresponding contract state variable information is abnormal. If a change in the state variable occurs, it can be determined that the reentry attack is harmful, that is, the contract under test has the ability to attack the target contract.
[0073] Specifically, an Ethereum transaction receipt is a data record generated after a transaction is executed on the Ethereum blockchain. Whenever a transaction is sent on the Ethereum network, the blockchain executes the transaction and generates a transaction receipt that provides detailed information on the transaction execution results; including transaction hash, transaction index, block number, etc.
[0074] An ERC777 contract reentry attack detection system is used to implement a contract static analysis method, a fuzzy testing method, and a vulnerability detection method. The system includes the following modules;
[0075] Contract static analysis module: constructs the abstract syntax tree of the contract to be deployed, identifies the ERC777 contract containing the hook function according to the function definition node in the syntax tree, loads the corresponding contract into the contract pool to be tested, and adds the corresponding hook function in the contract to the first function candidate pool. At the same time, the dependency relationship between the contract to be tested and the deployed contract on the chain is constructed according to the abstract syntax tree, and the target contract set is constructed. The code of the called function in the set is statically analyzed to determine whether there is a possibility of reentrancy attack, and whether the contract does not have a mechanism to prevent attacks. Functions that meet both conditions are added to the second function candidate pool;
[0076] Fuzz testing module: Determine the deployment order of the contract to be tested and the target contract according to the dependency relationship between the contract to be tested and the deployed contracts on the chain, deploy the contract to be tested and the target contract in the test network respectively, use automated scripts or automated deployment tools to generate contract deployment transactions, randomly select a candidate function from the first function candidate pool and the second function candidate pool respectively, randomly generate test cases according to the parameter types of the candidate functions, execute the corresponding transactions according to the test cases and record the address information generated during the execution process, collect the execution information of the opcodes in the Ethereum virtual machine, and record the corresponding contract state variable information after each opcode is executed and received;
[0077] Vulnerability detection module: Whether the transaction is executed successfully, generate a call graph based on the opcode information in the Ethereum virtual machine to analyze whether a reentrancy attack has occurred, and analyze whether the contract under test is aggressive to the target contract based on the changes in the corresponding contract state variables.
[0078] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.
Claims
1. A method for detecting reentrancy attacks on ERC777 contracts, characterized by: Including a contract to be tested and a target contract, the contract to be tested is an ERC777 contract containing a hook function in the contract to be deployed, and the target contract is a contract that interacts with the contract to be tested and may have a reentrancy vulnerability. The method includes the following steps: Step S1, construct an abstract syntax tree of the contract to be deployed, identify the ERC777 contract containing the hook function according to the function definition node in the syntax tree, load the corresponding contract into the contract pool to be tested, and add the corresponding hook function in the contract to the first function candidate pool, and at the same time, construct the dependency relationship between the contract to be tested and the deployed contract on the chain according to the abstract syntax tree, and construct a target contract set, perform static analysis on the code of the called function in the set to determine whether there is a possibility of reentrancy attack, and determine that the contract does not have a mechanism to prevent attacks, and add the functions that meet both conditions to the second function candidate pool; Step S2: Determine the deployment order of the contract to be tested and the target contract according to the dependency relationship between the contract to be tested and the deployed contract on the chain, deploy the contract to be tested and the target contract in the test network respectively, use an automated script or automated deployment tool to generate a contract deployment transaction, randomly select a candidate function from the first function candidate pool and the second function candidate pool respectively, randomly generate a test case according to the parameter type of the candidate function, execute the corresponding transaction according to the test case and record the address information generated during the execution process, collect the execution information of the opcode in the Ethereum virtual machine, and record the corresponding contract state variable information after each opcode is executed and received; Step S3: Analyze whether the transaction in step S2 is successfully executed, generate a call graph based on the opcode information in the Ethereum virtual machine to analyze whether a reentrancy attack occurs, and analyze whether the contract under test is aggressive to the target contract based on the changes in the corresponding contract state variables.
2. A method for detecting reentrancy attacks on ERC777 contracts according to claim 1, characterized in that: The specific operations of step S1 are as follows: Step S11: construct an abstract syntax tree of the contract to be deployed, and use regular expressions to delete irrelevant information in the contract code to be tested; Step S12: traverse the abstract syntax tree of the contract to be deployed, identify the ERC777 contract containing the Hook function, load the corresponding contract into the contract pool to be tested, and add the corresponding hook function in the contract to the first function candidate pool; Step S12: traverse the abstract syntax tree of the contract to be tested, and construct a set of dependencies between the contract to be tested and the deployed contracts on the chain; Step S13, mark the contract that interacts with the hook function among the deployed contracts on the chain as the target contract, and construct the target contract set based on it; Step S14, statically analyze the code of the called function in the target contract to determine whether it is at risk of being attacked by a reentrancy attack, and determine whether the contract does not have a mechanism to prevent attacks, and add functions that meet both conditions to the second function candidate pool.
3. An ERC777 contract reentry attack detection method and system according to claim 2, characterized in that: Step S13 also includes the following specific steps for screening the target contract: filtering functions with private, view, pure and constant modifiers. When there is no callable function in the target contract, it is removed from the target contract set and the target contract is unmarked.
4. A method for detecting reentrancy attacks on ERC777 contracts according to claim 1, characterized in that: The specific operation of step S2 is as follows: Step S21: Determine the deployment order of the contract to be tested and the target contract according to the dependency set, write an automated script or use an automated deployment tool to generate a contract deployment transaction, deploy the contract to be tested and the target contract in the test network respectively, and obtain the address of the contract to be tested and the target contract through the transaction receipt or transaction hash; Step S22: Select a first function from the first function candidate pool, select a second function from the second function candidate pool, and randomly generate test cases according to the parameter types of the candidate functions. Step S23: Execute the corresponding transaction according to the test case and record the address information generated during the execution process, collect the execution information of the operation code in the Ethereum virtual machine, and record the contract state variable information after each operation code is executed and received.
5. A method for detecting reentrancy attacks on ERC777 contracts according to claim 4, characterized in that: The address information includes the calling contract address and the called contract address; Execution information includes arithmetic operations, logical operations, and I / O operations; State variable information includes persistent data declared in Ethereum smart contracts, which will be saved on the blockchain and remain unchanged between different function calls of the contract.
6. A method for detecting reentrancy attacks on ERC777 contracts according to claim 1, characterized in that: Whenever a transaction is sent on the Ethereum network, the blockchain executes the transaction and generates a transaction receipt to provide detailed information on the transaction execution results. The specific operations of step S3 are as follows: Step S31: Determine whether the transaction is successfully executed by checking the status field in the transaction receipt; Step S32, generating a call graph according to the address information generated during the execution of step S2; Step S33: Determine whether the address of the calling contract and the address of the called contract appear repeatedly during the execution process through the call graph; Step S34: through the execution information of the operation code in step S2, determine whether the transfer operation occurs repeatedly, and the calling contract address is the same as the called contract address during the transfer process. If multiple transfer operations occur, analyze whether the corresponding contract state variable information is abnormal. If a change in the state variable occurs, it can be determined that the reentry attack is harmful, that is, the contract under test has the ability to attack the target contract.
7. An ERC777 contract reentry attack detection system, characterized by: For implementing any one of claims 1-6, the system comprises the following modules; Contract static analysis module: constructs the abstract syntax tree of the contract to be deployed, identifies the ERC777 contract containing the hook function according to the function definition node in the syntax tree, loads the corresponding contract into the contract pool to be tested, and adds the corresponding hook function in the contract to the first function candidate pool. At the same time, the dependency relationship between the contract to be tested and the deployed contract on the chain is constructed according to the abstract syntax tree, and the target contract set is constructed. The code of the called function in the set is statically analyzed to determine whether there is a possibility of reentrancy attack, and whether the contract does not have a mechanism to prevent attacks. Functions that meet both conditions are added to the second function candidate pool; Fuzz testing module: Determine the deployment order of the contract to be tested and the target contract according to the dependency relationship between the contract to be tested and the deployed contracts on the chain, deploy the contract to be tested and the target contract in the test network respectively, use automated scripts or automated deployment tools to generate contract deployment transactions, randomly select a candidate function from the first function candidate pool and the second function candidate pool respectively, randomly generate test cases according to the parameter types of the candidate functions, execute the corresponding transactions according to the test cases and record the address information generated during the execution process, collect the execution information of the opcodes in the Ethereum virtual machine, and record the corresponding contract state variable information after each opcode is executed and received; Vulnerability detection module: Whether the transaction is executed successfully, generate a call graph based on the opcode information in the Ethereum virtual machine to analyze whether a reentrancy attack has occurred, and combine the changes in the corresponding contract state variables to analyze whether the contract under test is aggressive to the target contract.
Citation Information
Patent Citations
Intelligent contract transaction sequence dependent vulnerability variation fuzzy test method
CN111563742A
Intelligent contract reentry defect detection method based on dynamic execution information analysis
CN113448870A
Ethereum intelligent contract vulnerability real-time detection method, terminal and storage medium
CN115174279A
Method and contract rewriting framework system for supporting smart contracts in a blockchain network
WO2020249572A1
