Contract automatic identification method, system and device for closed-source attack based on fuzz testing
By real-time monitoring and static analysis of closed-source smart contracts, test cases are generated and test assertions are defined, which solves the problem of insufficient ability to identify and intercept closed-source contracts in existing technologies, realizes effective identification and protection of closed-source contracts, and improves the security of blockchain systems.
Patent Information
- Application Number
- CN202310878954.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-07-17
- Publication Date
- 2026-11-20
- Estimated Expiration
- 2043-07-17
AI Technical Summary
Existing fuzz testing-based methods are performed off-chain, making it impossible to obtain on-chain state and data. This makes it difficult to assess the severity and actual impact of potential attacks, and they have a weak ability to identify and intercept closed-source contracts.
This paper presents an automatic identification method for closed-source attack contracts based on fuzz testing. By monitoring the blockchain network in real time, the method obtains the bytecode of closed-source smart contracts, decompiles and performs static analysis to generate test cases, simulates contract execution, defines six test assertions to detect contracts, and adds a blacklist mechanism to intercept suspicious contracts.
It can promptly detect potential attack contracts, improve contract security, effectively identify various attacks, prevent malicious attacks, and enhance the security and reliability of contracts.
Smart Images

Figure CN117118661B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of blockchain smart contract security protection technology, and in particular to a method, system and device for automatic identification of closed-source attack contracts based on fuzz testing. Background Technology
[0002] With the development of blockchain technology, smart contracts have become a key component of blockchain systems. However, with the widespread adoption of smart contract applications, various attack methods targeting smart contracts have emerged, posing a significant security threat to the blockchain ecosystem. Currently, smart contract security protection technologies mainly include auditing, monitoring, firewalls, and fuzzing-based methods. Among these, fuzzing-based methods are dynamic analysis techniques that randomly mutate the contract's inputs and observe its output and execution behavior to discover security vulnerabilities. Examples include pure off-chain fuzzing methods like ContractFuzzer and Echidna. ContractFuzzer uses static analysis at the source code level to generate test cases for testing smart contracts; Echidna uses attribute-based testing to fuzz the compiled contract bytecode. However, because these fuzzing methods are primarily performed off-chain, they cannot access on-chain state and data; they lack effective assertion testing, making it difficult to assess the severity and actual impact of potential attacks; they mainly focus on vulnerability discovery, with weaker capabilities in identifying and intercepting malicious contracts; and they typically require analysis of the contract source code, limiting their applicability when dealing with closed-source contracts. Summary of the Invention
[0003] The purpose of this invention is to effectively identify and analyze newly added closed-source contracts on the blockchain, and to determine whether the protected contract is vulnerable to attack in the early stages of an attack. To achieve the above objective, this invention provides a method, system, and device for automatic identification of closed-source attack contracts based on fuzz testing.
[0004] This invention provides a method for automatically identifying closed-source attack contracts based on fuzz testing, comprising:
[0005] Real-time monitoring and acquisition of the latest closed-source smart contract bytecode deployed on Ethereum;
[0006] After decompiling the closed-source smart contract bytecode into opcodes, static analysis is performed to extract the function signatures defined in the closed-source smart contract.
[0007] The input parameter sequence of the function is obtained by querying the function signature, and test cases are generated based on the input parameter sequence with the protected contract state as the seed.
[0008] The test case is executed by sending a transaction from the address of the creator of the closed-source smart contract.
[0009] The execution results of the test cases are checked according to the test assertions. If an attack is detected in the closed-source smart contract, the address of the closed-source smart contract is added to the blacklist.
[0010] Preferably, the real-time monitoring and acquisition of the latest closed-source smart contract bytecode deployed on Ethereum includes:
[0011] The system monitors newly added smart contracts on the blockchain network in real time, uses a blockchain explorer to query the open-source status of these smart contracts, filters out those that maintain a closed-source status, and directly downloads the bytecode of the closed-source contracts to the local machine.
[0012] Preferably, it further includes:
[0013] Transactions that invoke protected contracts on the blockchain are filtered according to a blacklist, and are automatically blocked when the transaction is found to originate from an address on the blacklist.
[0014] If no attack behavior is detected in the closed-source smart contract, then the number of fuzz tests for the judgment function is determined.
[0015] If the number of fuzz tests is less than a preset threshold, then the control flow diagram of the closed-source smart contract is used to determine whether the data flow of the transaction is fully covered.
[0016] If the data stream is not fully covered, the test cases are mutated and the mutated test cases are executed.
[0017] Preferably, the step of querying the function's input parameter sequence based on the function signature includes:
[0018] A function selector database is pre-built, which stores all function selectors on the blockchain as well as the parameter types and quantities of the functions. The function selectors of the closed-source smart contracts obtained by static analysis are traversed in the function selector database, and the parameter types and quantities of the queried functions are used as the input parameter sequence of the functions of the closed-source smart contracts.
[0019] If the query fails, the maximum number of candidate input parameters for the function is inferred based on the CALLDATALOAD opcode. Then, using a preset function parameter type library as a seed, all possible input parameter type sequences are generated. A transaction is generated based on the input parameter type sequence and sent to the chain for execution by a closed-source smart contract. When the transaction is successfully executed, the function's input parameter sequence is inferred.
[0020] Preferably, the test assertion includes:
[0021] Balance immutability is used to record the asset balance managed by the protected contract that interacts with the closed-source smart contract during the transaction before and after the transaction execution by tracking the execution path of the transaction, and to check whether the asset balance of the protected contract has changed during the transaction.
[0022] No asset theft is detected. This is used to check whether the asset balance of the suspicious address changes before and after the transaction, by recording the asset balance of the suspicious address before and after the transaction, and whether the changes are the same as the changes in the asset balance of the protected contract.
[0023] Role invariance is used to check whether a user's role remains unchanged before and after a transaction.
[0024] The freeze state invariance is used to check whether a user's freeze state remains unchanged before and after a transaction.
[0025] Ownership immutability is used to check whether the owner of the protected contract remains unchanged before and after the transaction;
[0026] Transaction restriction invariance is used to check whether the transaction restrictions of the protected contract remain unchanged before and after the transaction.
[0027] Preferably, determining whether the data flow of the transaction is fully covered based on the control flowchart of the closed-source smart contract includes:
[0028] The data flow of each function is obtained based on the control flow diagram. The variables contained in the function are obtained based on the data flow. The opcodes used in the execution of each transaction are checked. The variables used in the execution of the transaction are obtained based on the opcodes. The variables contained in the function and the variables used in the execution of the transaction are compared to determine whether the data flow is fully covered.
[0029] Preferably, the mutation of the test cases includes:
[0030] Based on the control flow diagram, a solver is used to solve the constraints of each data flow path, find the parameter constraints of each data flow path, and mutate the test cases according to the parameter constraints.
[0031] This invention also provides an automatic identification system for closed-source attack contracts based on fuzz testing, comprising:
[0032] The data acquisition module is used to monitor and acquire the latest closed-source smart contract bytecode deployed on Ethereum in real time.
[0033] The static analysis module is used to decompile the bytecode of the closed-source smart contract into opcodes and then perform static analysis to extract the function signatures defined in the closed-source smart contract.
[0034] The test case generation module is used to query the function signature to obtain the sequence of input parameters of the function, and generate test cases based on the sequence of input parameters with the protected contract state as the seed.
[0035] The test case execution module is used to simulate the address of the creator of the closed-source smart contract to send transactions in order to execute the test cases;
[0036] The test assertion detection module is used to detect the execution results of the test cases based on test assertions. If an attack is detected in the closed-source smart contract, the address of the closed-source smart contract is added to the blacklist.
[0037] This invention also provides a terminal device, including a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor. When the processor executes the computer program, it implements the closed-source attack contract automatic identification method as described above.
[0038] This invention provides a method, system, and device for automatic identification of closed-source attack contracts based on fuzz testing. Compared with existing technologies, its advantages are as follows: by performing fuzz testing on newly added smart contracts, potential attack contracts can be discovered in a timely manner, and security vulnerabilities in protected contracts can be found before the contracts are attacked, thereby improving the security of protected contracts; by defining six assertion tests, various attacks, including reentrancy attacks and flash loan attacks, can be effectively detected, demonstrating good attack identification performance; and by using a blacklist mechanism to intercept suspicious attack contracts, malicious attacks can be effectively prevented. Attached Figure Description
[0039] Figure 1 This is a flowchart illustrating an automatic identification method for closed-source attack contracts based on fuzz testing according to an embodiment of the present invention.
[0040] Figure 2 This is a schematic diagram of a function selector within a smart contract according to an embodiment of the present invention.
[0041] Figure 3 This is a code diagram illustrating six test assertions in embodiments of the present invention.
[0042] Figure 4 This is a flowchart illustrating the test assertion detection steps in an embodiment of the present invention.
[0043] Figure 5 This is a schematic diagram of the control flowchart of an embodiment of the present invention.
[0044] Figure 6 This is a schematic diagram of the structure of an automatic closed-source attack contract identification system based on fuzz testing according to an embodiment of the present invention.
[0045] Figure 7 This is a schematic diagram of the structure of a terminal device according to an embodiment of the present invention. Detailed Implementation
[0046] The specific embodiments of the present invention will be described in further detail below with reference to the accompanying drawings and examples. The following examples are for illustrative purposes only and are not intended to limit the scope of the invention.
[0047] like Figure 1 As shown, this embodiment of the invention provides a method for automatically identifying closed-source attack contracts based on fuzz testing, including the following steps:
[0048] S1. Monitor and obtain the latest closed-source smart contract bytecode deployed on Ethereum in real time;
[0049] Specifically, it monitors newly added smart contracts on the blockchain network in real time, uses the blockchain explorer Etherscan to query the open-source status of smart contracts, filters out smart contracts that maintain a closed-source status, and directly downloads the bytecode of the closed-source contract to the local machine.
[0050] S2. After decompiling the closed-source smart contract bytecode into opcodes, perform static analysis to extract the function signatures defined in the closed-source smart contract.
[0051] S3. Obtain the sequence of input parameters of the function by querying the function signature, and generate test cases based on the sequence of input parameters with the protected contract state as the seed;
[0052] Smart contract functions are divided into two types: those with function parameters and those without. Different test case generation strategies apply to different function types.
[0053] Research into historical attack incidents revealed that many attackers, when invoking smart contracts, simply called the corresponding parameterless attack function without inputting any parameters. Therefore, in such cases, the function call can be performed directly without generating test cases.
[0054] For attack functions with parameters, since the ABI code of closed-source smart contracts is not publicly available, it is impossible to directly obtain the sequence of input parameters to generate test cases. Therefore, the following three methods can be used to generate test cases:
[0055] 1) Pre-build a function selector database. The function selector database stores all function selectors on the blockchain as well as the parameter types and quantities of the functions. Traverse the function selectors of the closed-source smart contracts obtained from static analysis in the function selector database, and use the queried function parameter types and quantities as the input parameter sequence of the functions of the closed-source smart contracts.
[0056] The function selector is calculated from the function signature. The function signature is a string consisting of the function name and function parameter types. After obtaining the function signature through static analysis of the closed-source smart contract bytecode, the function selector is the first four bytes of the Keccak-256 hash result of the function signature. Therefore, the smart contract bytecode can be converted into opcode form, and then filtered according to PUSH4 to obtain all callable public functions within the function, forming the function selector database for that function. The form of all callable public functions is as follows: Figure 2 As shown.
[0057] 2) If the query fails, the Octopus tool is used to perform pattern matching on the bytecode of the smart contract. Since the smart contract needs to obtain data using the CALLDATALOAD opcode when obtaining data in a transaction, the maximum number of candidate input parameters N of the function can be inferred based on the CALLDATALOAD opcode. Then, using a preset function parameter type library (including uint, bytes, address, etc.) as a seed, all possible input parameter type sequences from 0 to N are generated. A transaction is generated based on the input parameter type sequence and sent to the chain for execution by the closed-source smart contract. When the transaction is successfully executed, the input parameter sequence of the function is inferred.
[0058] After obtaining the sequence of input parameters for the function, test cases are generated using the protected contract state as a seed. The protected contract state includes, but is not limited to, the contract address and the managed token address.
[0059] 3) For a few functions whose input parameter sequences cannot be found even after trying the above methods, the function's call status on the mainnet can be continuously monitored over a period of time. If a transaction calling the function appears on the mainnet, then that transaction can be used as a seed to generate test cases.
[0060] Since attack transactions on a contract may require a specific, unpredictable path to trigger, randomly generating test cases can effectively increase the probability of triggering such transactions, thereby assessing whether the protected contract is vulnerable to attack.
[0061] S4. Simulate the address of the creator of the closed-source smart contract to send transactions to execute test cases;
[0062] Specifically, since many attack contracts restrict contract calls to only the attacker, Echidna's built-in functionality is used to simulate accounts and send test cases from the address of the creator of the closed-source smart contract for fuzz testing.
[0063] S5. Detect the execution results of the test cases based on the test assertions. If an attack is detected in the closed-source smart contract, add the address of the closed-source smart contract to the blacklist.
[0064] Specifically, based on different attack types, this invention adds six test assertions to identify potential attack behaviors, such as... Figure 3 As shown, it includes:
[0065] Balance immutability is used to track the execution path of transactions, record the asset balance managed by protected contracts that interact with closed-source smart contracts before and after the transaction execution, and check whether the asset balance of protected contracts has changed during the transaction.
[0066] No asset theft is detected. This is used to check whether the asset balance of a suspicious address changes before and after a transaction, by recording the asset balance of the suspicious address before and after the transaction.
[0067] Role invariance is used to check whether a user's role remains unchanged before and after a transaction.
[0068] The freeze state invariance is used to check whether a user's freeze state remains unchanged before and after a transaction.
[0069] Ownership immutability is used to check whether the owner of a protected contract remains unchanged before and after a transaction.
[0070] Transaction restriction invariance is used to check whether the transaction restrictions of a protected contract remain unchanged before and after a transaction.
[0071] The balance immutability assertion and the no-asset-theft assertion can identify reentrancy attacks and flash loan attacks. The role immutability assertion and the ownership immutability assertion can detect attacks that maliciously change the ownership of smart contracts. The frozen state ownership assertion and the transaction restriction immutability assertion can detect whether the contract's protection mechanisms are subject to malicious attacks. These six test assertions can monitor and identify potential security vulnerabilities during simulated attack testing, helping to improve the security of smart contracts and prevent unauthorized attacks.
[0072] If a closed-source smart contract is found to be engaging in suspicious attack behavior against a protected contract, the contract address of that closed-source smart contract will be added to the blacklist.
[0073] In one specific embodiment, step S5 is as follows: Figure 4 As shown, it also includes:
[0074] S501: Filter transactions that call protected contracts on the blockchain according to a blacklist, and automatically block transactions when they are found to originate from addresses on the blacklist.
[0075] By using a blacklist mechanism to block suspicious attack contracts, malicious attacks can be effectively prevented.
[0076] S502. If no attack behavior is detected in the closed-source smart contract, then the number of fuzz tests of the judgment function is determined.
[0077] Specifically, if the number of fuzz tests is not less than 30, then no further fuzz testing will be performed on the function; otherwise, continue to step 503.
[0078] S503. If the number of fuzz tests is less than the preset threshold, then determine whether the data flow of the transaction is fully covered according to the control flow diagram of the closed-source smart contract.
[0079] Specifically, if the number of fuzz tests is less than 30, the control flow diagram of the closed-source smart contract is used to determine whether the data flow of the transaction is fully covered. Based on the control flow diagram, the data flow of each function is obtained, the variables contained in the function are obtained from the data flow, and the opcodes used in the execution of each transaction are checked. Based on the opcodes, the variables used in the transaction execution are obtained, and the variables contained in the function and the variables used in the transaction execution are compared to determine whether the data flow is fully covered. Octopus is used to convert the bytecode of the closed-source smart contract into opcodes and perform static analysis, which can automatically generate the control flow diagram of the closed-source smart contract, as detailed below. Figure 5 As shown. By determining whether the data flow of a transaction is fully covered, we can determine whether the transaction can test all the functionalities of the function, and thus guide the transaction to mutate in a specific direction, increasing the transaction's code coverage.
[0080] S504. If the data stream is not fully covered, mutate the test cases and execute the mutated test cases.
[0081] Specifically, if the data flow is not fully covered, the Z3 solver is used to solve the constraints of each data flow path based on the control flow diagram, find the parameter constraints of each data flow path, and mutate the test cases according to the parameter constraints so that the test cases can meet the constraints of the test cases, thereby increasing the code coverage of the test.
[0082] This invention provides an automatic identification method for closed-source attack contracts based on fuzz testing. This method performs fuzz testing on newly added smart contracts, enabling timely detection of potential attack contracts and identifying security vulnerabilities in protected contracts before they are attacked, thus improving the security of protected contracts. By defining six assertion tests, it can effectively detect various attacks, including reentrancy attacks and flash loan attacks, demonstrating good attack identification performance. Furthermore, a blacklist mechanism intercepts suspicious attack contracts, effectively preventing malicious attacks.
[0083] like Figure 6 As shown, based on the above-described method for automatically identifying closed-source attack contracts, this embodiment of the invention also provides a fuzz-based system for automatically identifying closed-source attack contracts, comprising:
[0084] Data acquisition module 1 is used to monitor and acquire the latest closed-source smart contract bytecode deployed on Ethereum in real time;
[0085] Static analysis module 2 is used to decompile the bytecode of the closed-source smart contract into opcodes and then perform static analysis to extract the function signatures defined in the closed-source smart contract.
[0086] Test case generation module 3 is used to obtain the input parameter sequence of the function by querying the function signature, and generate test cases based on the input parameter sequence with the protected contract state as the seed;
[0087] Test case execution module 4 is used to simulate the address of the creator of the closed-source smart contract to send a transaction in order to execute the test case;
[0088] Test assertion detection module 5 is used to detect the execution results of the test cases based on test assertions. If an attack is detected in the closed-source smart contract, the address of the closed-source smart contract is added to the blacklist.
[0089] It should be noted that each module in the aforementioned fuzz-based automatic identification system for closed-source attack contracts can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer device, or stored in the memory of a computer device as software, so that the processor can call and execute the corresponding operations of each module. For specific limitations regarding the fuzz-based automatic identification system for closed-source attack contracts, please refer to the limitations of the fuzz-based automatic identification method for closed-source attack contracts described above; both have the same function and role, and will not be repeated here.
[0090] This invention also provides a terminal device, which includes:
[0091] Processor, memory, and bus;
[0092] The bus is used to connect the processor and the memory;
[0093] The memory is used to store operation instructions;
[0094] The processor is configured to execute operations corresponding to the fuzz-based automatic identification method for closed-source attack contracts described above by invoking the operation instructions.
[0095] In one alternative embodiment, a terminal device is provided, such as Figure 7 As shown, Figure 7 The terminal device 5000 shown includes a processor 5001 and a memory 5003. The processor 5001 and the memory 5003 are connected, for example, via a bus 5002. Optionally, the terminal device 5000 may also include a transceiver 5004. It should be noted that in practical applications, the transceiver 5004 is not limited to one type, and the structure of this terminal device 5000 does not constitute a limitation on the embodiments of this application.
[0096] Processor 5001 may be a CPU, a general-purpose processor, a DSP, an ASIC, an FPGA, or other programmable logic device, transistor logic device, hardware component, or any combination thereof. It may implement or execute the various exemplary logic blocks, modules, and circuits described in conjunction with the disclosure of this application. Processor 5001 may also be a combination that implements computational functions, such as including one or more microprocessor combinations, a combination of a DSP and a microprocessor, etc.
[0097] Bus 5002 may include a path for transmitting information between the aforementioned components. Bus 5002 may be a PCI bus or an EISA bus, etc. Bus 5002 can be divided into address bus, data bus, control bus, etc. For ease of representation, Figure 7 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.
[0098] The memory 5003 may be a ROM or other type of static storage device capable of storing static information and instructions, RAM or other type of dynamic storage device capable of storing information and instructions, or it may be an EEPROM, CD-ROM or other optical disc storage, optical disc storage (including compressed optical discs, laser discs, optical discs, digital universal optical discs, Blu-ray discs, etc.), magnetic disk storage media or other magnetic storage devices, or any other medium capable of carrying or storing desired program code in the form of instructions or data structures and accessible by a computer, but is not limited thereto.
[0099] The memory 5003 is used to store application code that executes the scheme of this application, and its execution is controlled by the processor 5001. The processor 5001 is used to execute the application code stored in the memory 5003 to implement the content shown in any of the foregoing method embodiments.
[0100] Terminal devices include, but are not limited to: mobile terminals such as mobile phones, laptops, digital radio receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), and in-vehicle terminals (such as in-vehicle navigation terminals), as well as fixed terminals such as digital TVs and desktop computers.
[0101] In summary, the embodiments of the present invention provide a method, system, and device for automatic identification of closed-source attack contracts based on fuzzing. By performing fuzzing tests on newly added smart contracts, potential attack contracts can be discovered in a timely manner, and security vulnerabilities in protected contracts can be found before the contracts are attacked, thereby improving the security of protected contracts. By defining six assertion tests, various attacks, including reentrancy attacks and flash loan attacks, can be effectively detected, demonstrating good attack identification performance. By intercepting suspicious attack contracts through a blacklist mechanism, malicious attacks can be effectively prevented.
[0102] The various embodiments in this specification are described in a progressive manner. For directly identical or similar parts of the embodiments, refer to each other. Each embodiment focuses on its differences from other embodiments. In particular, the system embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions in the method embodiments. It should be noted that the technical features of the above embodiments can be combined arbitrarily. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as the combination of these technical features does not contradict each other, it should be considered within the scope of this specification.
[0103] The above description is only a preferred embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and substitutions can be made without departing from the technical principles of the present invention, and these improvements and substitutions should also be considered within the scope of protection of the present invention.
Claims
1. A method for automatically identifying closed-source attack contracts based on fuzz testing, characterized in that, include: Real-time monitoring and acquisition of the latest closed-source smart contract bytecode deployed on Ethereum; After decompiling the closed-source smart contract bytecode into opcodes, static analysis is performed to extract the function signatures defined in the closed-source smart contract. The input parameter sequence of the function is obtained by querying the function signature. After obtaining the input parameter sequence of the function, test cases are generated using the protected contract state as a seed. The protected contract state includes the contract address and the managed token address. The test case is executed by sending a transaction from the address of the creator of the closed-source smart contract. The execution results of the test cases are checked based on test assertions. If an attack is detected on the closed-source smart contract, the address of the closed-source smart contract is added to a blacklist. The test assertions include: Balance immutability is used to record the asset balance managed by the protected contract that interacts with the closed-source smart contract during the transaction before and after the transaction execution by tracking the execution path of the transaction, and to check whether the asset balance of the protected contract has changed during the transaction. No asset theft is detected. This is used to check whether the asset balance of the suspicious address changes before and after the transaction, by recording the asset balance of the suspicious address before and after the transaction, and whether the changes are the same as the changes in the asset balance of the protected contract. Role invariance is used to check whether a user's role remains unchanged before and after a transaction. The freeze state invariance is used to check whether a user's freeze state remains unchanged before and after a transaction. Ownership immutability is used to check whether the owner of the protected contract remains unchanged before and after the transaction; Transaction restriction invariance is used to check whether the transaction restrictions of the protected contract remain unchanged before and after the transaction.
2. The method for automatically identifying closed-source attack contracts according to claim 1, characterized in that, The real-time monitoring and acquisition of the latest closed-source smart contract bytecode deployed on Ethereum includes: The system monitors newly added smart contracts on the blockchain network in real time, uses a blockchain explorer to query the open-source status of these smart contracts, filters out those that maintain a closed-source status, and directly downloads the bytecode of the closed-source contracts to the local machine.
3. The method for automatically identifying closed-source attack contracts according to claim 1, characterized in that, Also includes: Transactions that invoke protected contracts on the blockchain are filtered according to a blacklist, and are automatically blocked when the transaction is found to originate from an address on the blacklist. If no attack behavior is detected in the closed-source smart contract, then the number of fuzz tests for the judgment function is determined. If the number of fuzz tests is less than a preset threshold, then the control flow diagram of the closed-source smart contract is used to determine whether the data flow of the transaction is fully covered. If the data stream is not fully covered, the test cases are mutated and the mutated test cases are executed.
4. The method for automatically identifying closed-source attack contracts according to claim 1, characterized in that, The step of querying the function's input parameter sequence based on the function signature includes: A function selector database is pre-built, which stores all function selectors on the blockchain as well as the parameter types and quantities of the functions. The function selectors of the closed-source smart contracts obtained by static analysis are traversed in the function selector database, and the parameter types and quantities of the queried functions are used as the input parameter sequence of the functions of the closed-source smart contracts. If the query fails, the maximum number of candidate input parameters for the function is inferred based on the CALLDATALOAD opcode. Then, using a preset function parameter type library as a seed, all possible input parameter type sequences are generated. A transaction is generated based on the input parameter type sequence and sent to the chain for execution by a closed-source smart contract. When the transaction is successfully executed, the function's input parameter sequence is inferred.
5. The method for automatically identifying closed-source attack contracts according to claim 2, characterized in that, The step of determining whether the data flow of the transaction is fully covered based on the control flow diagram of the closed-source smart contract includes: The data flow of each function is obtained based on the control flow diagram. The variables contained in the function are obtained based on the data flow. The opcodes used in the execution of each transaction are checked. The variables used in the execution of the transaction are obtained based on the opcodes. The variables contained in the function and the variables used in the execution of the transaction are compared to determine whether the data flow is fully covered.
6. The method for automatically identifying closed-source attack contracts according to claim 3, characterized in that, The mutation of the test cases includes: Based on the control flow diagram, a solver is used to solve the constraints of each data flow path, find the parameter constraints of each data flow path, and mutate the test cases according to the parameter constraints.
7. An automatic identification system for closed-source attack contracts based on fuzz testing, characterized in that, include: The data acquisition module is used to monitor and acquire the latest closed-source smart contract bytecode deployed on Ethereum in real time. The static analysis module is used to decompile the bytecode of the closed-source smart contract into opcodes and then perform static analysis to extract the function signatures defined in the closed-source smart contract. The test case generation module is used to query the function signature to obtain the sequence of input parameters of the function. After obtaining the sequence of input parameters of the function, test cases are generated using the protected contract state as a seed. The protected contract state includes the contract address and the managed token address. The test case execution module is used to simulate the address of the creator of the closed-source smart contract to send transactions in order to execute the test cases; The test assertion detection module is used to detect the execution results of the test cases based on test assertions. If an attack is detected in the closed-source smart contract, the address of the closed-source smart contract is added to a blacklist. The test assertions include: Balance immutability is used to record the asset balance managed by the protected contract that interacts with the closed-source smart contract during the transaction before and after the transaction execution by tracking the execution path of the transaction, and to check whether the asset balance of the protected contract has changed during the transaction. No asset theft is detected. This is used to check whether the asset balance of the suspicious address changes before and after the transaction, by recording the asset balance of the suspicious address before and after the transaction, and whether the changes are the same as the changes in the asset balance of the protected contract. Role invariance is used to check whether a user's role remains unchanged before and after a transaction. The freeze state invariance is used to check whether a user's freeze state remains unchanged before and after a transaction. Ownership immutability is used to check whether the owner of the protected contract remains unchanged before and after the transaction; Transaction restriction invariance is used to check whether the transaction restrictions of the protected contract remain unchanged before and after the transaction.
8. A terminal device, characterized in that, The method includes a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, wherein the processor, when executing the computer program, implements the closed-source attack contract automatic identification method as described in any one of claims 1-6.
Citation Information
Patent Citations
Intelligent contract vulnerability detection method based on combination of neural network and dynamic fuzzy test
CN113326050A
Intelligent contract reentry vulnerability detection method based on static and dynamic analysis
CN115098863A
