Ethereum ERC20 honeypot contract detector in block chain and detection method thereof

By integrating a honeypot contract detector into the Ethereum execution client, analyzing contract bytecode, and simulating execution control flow, the problem that existing detection methods cannot identify ERC20 honeypot contracts is solved, achieving efficient contract risk identification and security enhancement.

CN118981356APending Publication Date: 2024-11-19HEILONGJIANG UNIV
View PDF 0 Cites 3 Cited by

Patent Information

Application Number
CN202410944520.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-15
Publication Date
2024-11-19

AI Technical Summary

Technical Problem

Existing honeypot systems and detection methods have low support for Ethereum cryptocurrency theft attacks, failing to attract attackers to send substantial attack requests. Furthermore, existing detection methods cannot effectively identify ERC20 contracts with honeypot switching mechanisms, resulting in detection results that are independent of the blockchain and making it impossible to understand contract risks in a timely manner.

Method used

The honeypot contract detector is integrated into the Ethereum execution client. By analyzing the contract bytecode and converting it into opcodes, the execution of the contract control flow is simulated to detect whether the contract conforms to the predefined honeypot pattern. It focuses on the contract's token transfer method to improve detection efficiency.

Benefits of technology

It implements lightweight detection within the Ethereum execution client, enabling timely identification and response to contracts with honeypot risks, enhancing the Ethereum network's ability to identify ERC20 honeypot contracts, and providing stronger security guarantees.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118981356A_ABST
    Figure CN118981356A_ABST
Patent Text Reader

Abstract

The invention discloses an Ethereum ERC20 honeypot contract detector in a block chain and a detection method thereof, and belongs to the block chain technology. The method comprises the steps that firstly, a honeypot contract detector is integrated into an Ethereum client, the honeypot contract detector verifies whether a contract belongs to an ERC20 standard contract or not according to a contract address, and then a contract byte code is obtained and converted into an operation code of an Ethereum virtual machine; then basic blocks are divided, a contract entrance is found in a token transfer mode, an Ethereum virtual machine operation code is simulated and executed, a contract control flow is simulated, and whether the contract conforms to a predefined honeypot contract mode or not is detected. The method is different from an existing honeypot detector, does not depend on a contract interaction record or a source code provided by a contract creator, static data flow analysis is carried out on a contract byte code to deduce a honeypot mechanism in a contract, in addition, the detector only pays attention to a token transfer method control flow of ERC20, and the detection efficiency is improved. Compared with overall analysis of the contract, the method has the advantage of rapid analysis.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of computer network security technology and relates to a detection technology for malicious attacks, specifically an Ethereum ERC20 honeypot contract detector in a blockchain and a detection method thereof. Background Art

[0002] In recent years, digital cryptocurrency technology has received widespread attention around the world. Ethereum is a blockchain platform that can issue encrypted digital currencies, including Ether and tokens created using smart contract technology. The ERC20 token standard is a specification for implementing smart contracts on the Ethereum blockchain, mainly used to issue and manage homogeneous tokens. Homogenous tokens mean that each token is identical and indistinguishable, a feature that makes them widely used in the digital economy, such as representing a currency, a points system, or voting rights.

[0003] The Ethereum network consists of nodes running Ethereum client software, including Go-Ethereum (Geth for short) and Parity, etc. These client software all support remote control of the system's API function modules through the HTTP protocol-based remote procedure call (HTTP JSON-RPC) module.

[0004] When the user starts the client software, the RPC module listens to port 8545 of the host operating system by default. Due to the lack of an effective identity authentication mechanism in the design of the Ethereum RPC protocol, attackers can remotely control the client program through the network and steal Ethereum digital currency. Ethereum digital currency theft attacks usually follow a three-step process: sending a probe request to find an open Ethereum RPC port; sending a request to obtain node information to construct the final attack call request; sending the final attack call request to cause a substantial attack.

[0005] In the Ethereum blockchain, honeypot systems are widely used to collect data on the Internet, capture malicious activities to study and understand the behavior and motivation of attackers, and discover system weaknesses and new attack methods. However, existing honeypot systems and detection methods have low support for Ethereum digital currency theft attacks, and cannot attract attackers who steal Ethereum digital currency to send substantial attack requests to the honeypot system, so it is impossible to detect Ethereum digital currency attacks.

[0006] In order to make native tokens in DEX and tokens implemented by smart contracts interchangeable, Ethereum developers proposed the Ethereum Request for Comment-20 (ERC-20) homogeneous token standard. This standard specifies the properties that tokens must implement so that each token is exactly the same as another token (in type and value), thereby ensuring that different tokens can interact in a consistent and compatible manner within the Ethereum ecosystem. However, DEX (such as the most popular uniswap in Ethereum) does not perform contract audits on the ERC20 tokens deployed in it. Any token that symbolizes the ERC20 standard can be deployed. Developers do not even need to upload the source code of the contract. They only need to deposit a pair of ERC20 tokens into the trading pool to provide liquidity. This has led to the existence of a large number of honeypot ERC20 contracts in DEX that are designed to steal traders' assets.

[0007] The ERC20 token standard is a specification for implementing smart contracts on the Ethereum blockchain, mainly used to issue and manage homogeneous tokens. Homogenous tokens mean that each token is identical and indistinguishable, a feature that makes them widely used in the digital economy, such as representing a currency, a points system, or voting rights.

[0008] At present, the detection methods for closed-source honeypot ERC20 contracts are mainly divided into two categories. One is to use external tools to perform static analysis on the contract bytecode, and the other is to use the estimateGas method provided by the Ethereum execution client (estimate the gas fee required for a transaction) to verify whether the three operations of buying, authorizing and selling the token can be achieved in a transaction. However, existing static analysis tools often focus on whether there are vulnerabilities such as integer overflow and reentrancy in the contract, and there are performance problems caused by the limitation of recursive depth during detection. It often takes several minutes to analyze a contract. The main limitation of using the estimateGas method to check the risk of contract honeypots is that it cannot detect contracts with honeypot switch mechanisms. This detection method is similar to black box testing, which is limited to detecting contracts without honeypot switches. In addition, the detection results of both methods are independent of the blockchain, and other traders cannot immediately understand whether there are risks in the contract. Summary of the invention

[0009] The purpose of the present invention is to provide an Ethereum ERC20 honeypot contract detection method in the blockchain, which improves the efficiency of detection by directly analyzing the operation code in the Ethereum execution client without relying on the on-chain contract interaction record and focusing on the contract's token transfer method.

[0010] Technical solution: A method for detecting Ethereum ERC20 honeypot contracts in a blockchain. The method first integrates a honeypot contract detector into an Ethereum client. The honeypot contract detector verifies whether the contract belongs to the ERC20 standard contract according to the contract address, then obtains the contract bytecode and converts it into the Ethereum virtual machine's operation code; then divides the basic block, finds the entry of the token transfer method in the contract, simulates the execution of the Ethereum virtual machine's operation code and simulates the contract control flow, and detects whether the contract meets the predefined honeypot contract mode;

[0011] The method for judging the ERC20 standard contract is as follows: judging whether the contract bytecode of the contract address contains function signatures corresponding to the ERC20 standard for token transfer, token authorization, and token transfer from a specified address to another address. If the three function signatures are included at the same time, the contract is deemed to be an ERC20 contract, otherwise it is deemed to be a non-ERC20 contract;

[0012] The method divides the basic blocks into the following logical parts:

[0013] The JUMPDEST opcode represents the beginning of a basic block, and the JUMP, JUMPI, STOP, REVERT, RETURN, and SELFDESTRUCT opcodes represent the end of a basic block. In addition, two additional basic block division methods are defined to adapt to special cases:

[0014] When two JUMPDEST opcodes appear consecutively, the first JUMPDEST is composed of a basic block alone;

[0015] Treat the INVALID opcode as a basic block in its own right;

[0016] The predefined honeypot contract modes in the method include blacklist honeypot mode, transaction lock honeypot mode and proxy contract honeypot model, and the judgment method is as follows:

[0017] Blacklist honeypot mode: When executing a token transfer operation, the contract logic is designed to first check whether the token recipient address is a liquidity pool address in a decentralized exchange. If it is a liquidity pool address, check the Boolean mapping corresponding to the token sender address representing the blacklist. If the Boolean value is true, stop executing the token transfer operation and roll back all operations that have been executed;

[0018] Transaction lock honeypot mode: When executing a token transfer operation, the contract logic is designed to check the Boolean mapping corresponding to the token sender address and the token receiver address representing the blacklist. If the Boolean value is true, the token transfer operation is stopped and all operations that have been executed are rolled back;

[0019] Proxy contract honeypot mode: There is an operation of calling an external contract in the control flow corresponding to the token transfer operation.

[0020] Furthermore, the method implements lightweight detection directly on the Ethereum execution client, and then analyzes the contract bytecode without relying on the on-chain contract interaction records or the source code provided by the contract creator. Honeypot contract detection implements static data flow analysis on the contract bytecode to infer the honeypot mechanism in the contract, and implements detection and marking based on the contract-based token transfer method, specifically including:

[0021] The single contract detection algorithm in the honeypot detector first ensures that the client instance has been successfully initialized and can query the contract bytecode of the corresponding address in the Ethereum client; then it generates a log file for recording the detection results and obtains the contract bytecode through the method provided by the Ethereum client; finally, if the bytecode in the contract address is not empty, it enters the analysis of the ERC20 standard contract and the honeypot contract model. After the analysis is completed, the contract detection result recording algorithm is called to write the detection result of the corresponding contract into the log file.

[0022] In the above detection method, the implementation process of the single contract detection algorithm is as follows:

[0023] (1) Determine whether the client instance in the detector structure is initialized successfully, and then generate a log file for recording the detection results;

[0024] (2) Obtain the bytecode of the smart contract through the Ethereum client instance, and determine whether the contract complies with the ERC20 standard through the bytecode;

[0025] Convert bytecode into Ethereum virtual machine opcode and separate into basic blocks, pass all basic blocks into the core method for detection, and each basic block contains at least one opcode;

[0026] (3) Execute a control flow analysis algorithm, the method comprising the following steps:

[0027] (31) Loop through all the opcodes and find the entry for the token transfer method in the ERC20 standard;

[0028] (32) Define and initialize the stack, memory and contract permanent storage space in Ethereum based on pseudo depth-first traversal;

[0029] Each element in the stack, memory and contract permanent storage space is a structure containing a value and a tainted Boolean value to implement taint analysis. The pseudo depth-first traversal method is responsible for determining the direction of the control flow and saving the status of the stack, memory and contract permanent storage space when each basic block is executed;

[0030] (33) Each opcode in the basic block is simulated by executing a simulation algorithm. The algorithm is responsible for updating the status in the stack, memory, and permanent storage space of the contract and for assigning and transmitting tainted information, assigning false values ​​when reading without relying on the data in the real block;

[0031] (34) Call the contract detection result recording algorithm to write the detection result of the corresponding contract into the log file.

[0032] Furthermore, in the method, when executing a token transfer, the contract will send a call message containing the function signature and call parameters of the token transfer method to the contract. Before executing the code logic, the function selector in the function wrapper is first entered. The function selector selects to enter the corresponding control flow according to the function signature in the call message. If the function selector of the contract does not contain the function signature in the call message, it will be regarded as an illegal call and all operations will be rolled back.

[0033] When entering the function selector basic block, the stack only contains the function signature of the token transfer method. In the corresponding basic block, the function signature of the token transfer method is first pushed into the stack, and then the top element of the stack and the function signature are compared. Under the same circumstances, the control flow of the corresponding method is entered, including entering the token transfer method control flow by forging the calling information containing the function signature and calling parameters of the token transfer method in the function selector part.

[0034] Furthermore, the honeypot contract detector creates a full node in the Ethereum client startup process, and then calls custom method APIs to register the application programming interface API of the honeypot detector, wherein the custom method APIs define the API namespace, version, service instance and visibility parameters; subsequently, the single contract detection algorithm, batch contract detection algorithm and stop detection algorithm executed by the honeypot detector are added to the interactive console of the Ethereum client to realize single, batch or stop detection of contract addresses.

[0035] The method satisfies the following two conditions for the ERC20 honeypot contract definition:

[0036] a. Create a liquidity pool in a decentralized exchange and form a trading pair with valuable tokens;

[0037] b. Victims can buy tokens on decentralized exchanges, but cannot sell tokens on decentralized exchanges.

[0038] The honeypot contract detector includes creating a detector instance and registering a detector service in an Ethereum client.

[0039] Furthermore, the detection process of the honeypot contract includes the judgment of the blacklist, transaction lock and proxy contract from the level of simulation execution algorithm and pseudo depth-first traversal method;

[0040] a. Blacklist honeypot mode

[0041] Simulation execution algorithm level: determine whether to enter a comparison between a receiver address and the value stored in the contract permanent storage space, and whether the comparison result will lead to two flows. If so, set the receiver address comparison taint of the jump condition element to true; determine whether to enter a statement that reads a Boolean value stored in the contract permanent storage space with the sender address as an index, and whether the value will lead to two flows. If so, set the sender address of the jump condition element to true.

[0042] At the pseudo-depth-first traversal method level: at the end of the execution of the basic block, if the tainted value of the receiver address of the jump condition element is true, the two flows are traversed separately, and the two flows are recorded as pointing to control flow A and control flow B respectively; at the end of the execution of the basic block, if the Boolean tainted value of the sender address of the jump element is true, the two flows are traversed separately. If one flow causes the execution to terminate and roll back, and the other flow points to A or B, then the contract is considered to have a blacklist honeypot risk;

[0043] b. Transaction lock honeypot mode

[0044] Simulation execution algorithm level: determine whether to enter a statement that reads a Boolean value stored in the contract's permanent storage space with the sender or receiver address as an index, and whether the value will lead to two flows. If so, set the sender or receiver address of the jump condition element to a Boolean value of true;

[0045] At the pseudo-depth-first traversal method level: at the end of the execution of the basic block, if the sender or receiver address of the jump element is a Boolean taint value of true, two flows are traversed respectively. If one flow leads to the termination of execution and rollback, and the other flow points to a basic block that does not contain a rollback opcode, the contract is considered to have a transaction lock type honeypot risk;

[0046] c. Proxy Contract Honeypot Model

[0047] Simulation execution algorithm level: As long as the control flow contains opcodes involving external calls, it is considered that there is a proxy contract honeypot risk, and further detection is terminated at this time.

[0048] Furthermore, the batch contract detection algorithm parses the string type contract address in the single contract detection algorithm into an address type array, and then loops through the detection logic of the single detection algorithm.

[0049] The present invention also provides an Ethereum ERC20 honeypot contract detector in a blockchain, which is used to execute the above-mentioned detection method and can realize the detection of a blacklist contract honeypot mode, an exchange honeypot mode and a proxy contract honeypot mode.

[0050] Beneficial effect: The present invention integrates the honeypot contract detection system into the Ethereum execution client. The advantage of this integration method is that it can timely identify and respond to contracts with honeypot risks. The detection system verifies that the contract is an ERC20 standard contract based on the contract address, obtains the contract bytecode and converts it into the underlying operation code, then finds the entry of the token transfer method, sequentially simulates the execution of the contract operation code, and detects whether there is a REVERT situation that conforms to the predefined honeypot contract mode. By analyzing the operation code directly in the Ethereum execution client without having to rely on the on-chain contract interaction record, the detection system focuses on the contract's token transfer method, thereby improving the efficiency of detection. In addition, the Ethereum full node running the integrated detection tool can mark contracts with honeypot risks. This method helps to enhance the entire Ethereum network's ability to identify ERC20 honeypot contracts, thereby providing stronger security for the Ethereum blockchain ecosystem. BRIEF DESCRIPTION OF THE DRAWINGS

[0051] Figure 1 It is the fraud process of ERC20 honeypot contract;

[0052] Figure 2 It is a schematic diagram of the workflow of the method of the present invention;

[0053] Figure 3 It is the function selector and function wrapper that implements token transfer;

[0054] Figure 4 It is the legitimacy check opcode for the normal token transfer control flow;

[0055] Figure 5 It is the CPU usage of the detector in working and stopped state;

[0056] Figure 6 It is the time taken by the detector to detect each contract;

[0057] Figure 7 It is the detection result of the basic block traversal depth. DETAILED DESCRIPTION

[0058] To explain the technical solution disclosed by the present invention in detail, further introduction is given below in conjunction with the accompanying drawings.

[0059] Ethereum is an open source public blockchain platform with smart contract functions. It provides a decentralized Ethereum virtual machine to process peer-to-peer contracts through its dedicated cryptocurrency Ethereum. At the same time, Ethereum is regarded as a state machine, and its state changes when the latest block containing transactions is added to the chain. Ethereum uses the Merkle Patricia Trie (MPT) data structure to maintain a state tree that stores all account information, Ethereum balances, and smart contract codes. In order to ensure the consistency of state transitions on all mainnet nodes, transactions are executed based on the state saved in the state tree of the previous block. In the design of Ethereum, any operation that changes the state of the blockchain is considered a transaction. Due to the nature of the blockchain as an immutable ledger, any transaction added to the chain is irreversible.

[0060] Smart contracts are the cornerstone of the Ethereum ecosystem. With the development of decentralized finance (DeFi) and the increase of decentralized exchanges (DEX), the security research of smart contracts and blockchain transactions in Ethereum has attracted more and more attention. With the introduction of ERC20 (homogeneous token standard), a large number of token contracts with honeypot properties have appeared in Ethereum. Since decentralized exchanges (DEX) allow any token that meets the ERC20 token standard to be deployed, there are a large number of honeypot contracts in DEX that are designed to steal traders' assets: users exchange honeypot ERC20 tokens with valuable tokens (such as ETH), but cannot exchange honeypot ERC20 tokens for ETH, which causes traders to lose funds. This study proposes a lightweight honeypot contract detection system that can be integrated into the go-Ethereum client (Geth). Unlike previous work, the detector does not rely on contract interaction records or source code provided by the contract creator. Instead, the detector implements static data flow analysis on the contract bytecode to infer the honeypot mechanism in the contract. In addition, as a lightweight detector integrated into the Ethereum client, the detector only focuses on the control flow of the ERC20 token transfer method, which has the advantage of fast analysis compared to the overall analysis of the contract. It takes an average of 9.74 milliseconds to detect each contract. This embodiment experiments on contracts known to have honeypot behavior and actual token contracts in the real world, proving the effectiveness of the detector and detection method of the present invention in identifying honeypot contracts.

[0061] The core logic of the ERC20 honeypot contract is to prevent victims from selling tokens and extracting valuable tokens, and this logic relies on the special design of the token smart contract. ERC20 honeypot contracts have different design strategies for different user behaviors. In the common implementation methods of ERC20 honeypot contracts, there are multiple code implementations for each type of trap, and attackers may disguise the core code to hide their true intentions.

[0062] The detection method described in the present invention is to integrate the honeypot contract detection system into the Ethereum execution client. The advantage of this integrated method is that it can timely identify and respond to contracts with honeypot risks. The detection system verifies that the contract is an ERC20 standard contract based on the contract address, obtains the contract bytecode and converts it into the Ethereum virtual machine operation code, then finds the entrance of the transfer (transfer token) method, sequentially simulates the execution of the contract operation code, and detects whether there is a predefined honeypot contract mode. By directly analyzing the operation code in the Ethereum execution client without relying on the on-chain contract interaction record, the detection system focuses on the token transfer method of the contract, thereby improving the efficiency of detection. In addition, the Ethereum full node running the integrated detection tool can mark contracts with honeypot risks. This method helps to enhance the entire Ethereum network's ability to identify ERC20 honeypot contracts, thereby providing a stronger security guarantee for the Ethereum blockchain ecosystem.

[0063] The ERC20 token standard is a specification for implementing smart contracts on the Ethereum blockchain, mainly used to issue and manage homogeneous tokens. Homogenous tokens mean that each token is identical and indistinguishable, a feature that makes them widely used in the digital economy, such as representing a currency, a points system, or voting rights. The standard defines a series of API interfaces that must be implemented, including transferring tokens (transfer), viewing the balance of the token at any address (balanceOf), the total supply of tokens (totalSupply), two functions for authorizing third parties to transfer tokens on behalf of token holders (approve and transferFrom), and two event declaration methods (Transfer and Approval. ERC20 requires that corresponding events should occur whenever the transfer and approve methods occur. The standardization of these interfaces ensures the interoperability of various smart contracts between different applications and wallets, enabling developers to create and deploy new digital assets while ensuring interoperability and compatibility.

[0064] The present invention defines the following two requirements for the ERC20 honeypot contract:

[0065] 1. Create a liquidity pool in a decentralized exchange and form a trading pair with valuable tokens.

[0066] 2. Victims can buy tokens on decentralized exchanges, but cannot sell tokens on decentralized exchanges.

[0067] Combination Figure 1The process of fraud implemented by the ERC20 honeypot contract shown. First, the attacker deploys a smart contract with special logic to prevent buyers from selling and meets the ERC20 standard, so that the contract can be deployed in a decentralized exchange and wallet software (such as Metamask) can recognize it as an ERC20 token. Next, the attacker pairs the token with a valuable token (ETH in most cases) and creates a liquidity pool. Subsequently, the attacker generates fake transactions to create the illusion of increased trading volume and rising token prices, thereby attracting automated arbitrage robots that create sandwich attacks and victims looking for profit opportunities to buy tokens. Finally, the victims cannot sell tokens in exchange for ETH in the liquidity pool. Once enough victims buy tokens, the attacker can withdraw funds from the liquidity pool by removing liquidity to make a profit. The core logic of the ERC20 honeypot contract is to prevent victims from selling tokens and withdrawing valuable tokens, and this logic relies on the special design of the token smart contract. ERC20 honeypot contracts have different design strategies for different user behaviors. The present invention provides three common implementation methods of ERC20 honeypot contracts. There are multiple code implementations for each type of trap, and attackers may disguise the core code to hide their true intentions.

[0068] The ERC20 honeypot detector of the present invention is integrated into a server running an Ethereum full node. Compared with the existing detection method for ERC20 honeypot contracts, this method significantly improves the detection efficiency, and miners of Ethereum full nodes running integrated detection tools can detect whether newly created ERC20 contracts in blocks have honeypot risks when packaging blocks, and can mark contracts as honeypots during the process of packaging blocks.

[0069] Combination Figure 2As shown, first create a full node in the Ethereum client (geth) startup process to register the ERC20 honeypot detector service, including building the structure of the detection component. Then call APIs to register the application programming interface (API) of this tool. The API namespace, version, service instance, and visibility parameters are defined. Considering that not all users running full nodes need to detect ERC20 honeypot contracts, in order to avoid unnecessary memory overhead affecting the efficiency of the full node, only the shutdown function of the detection component and the instance (contract address) of the necessary parameters are registered, without registering the complete Ethereum client instance. Subsequently, in the geth interactive console, the mapping of three methods in the detector is added, including a single contract detection algorithm (detect.DetectSingle), a batch contract detection algorithm (detect.DetectBatch), and a stop detection algorithm (detect.StopDetectHoneyPots). These three methods are used to detect whether it is a honeypot ERC20 contract based on the contract address (the parameter is the contract address), detect whether these addresses are honeypot ERC20 contracts based on the batch contract address (the parameter is the contract address of the string type separated by commas), and stop the honeypot ERC20 contract detection service.

[0070] The DetectSingle method and the DetectBatch method are used to detect ERC20 honeypot contracts individually and in batches respectively. Compared with the DetectSingle method, the DetectBatch method parses the string type contract address into an address type array, and then loops through the detection logic in DetectSingle. Therefore, you only need to focus on the implementation logic of DetectSingle.

[0071] The DetectSingle method first ensures that the client instance has been successfully initialized (checked) to facilitate the query of the contract bytecode of the corresponding address in the Ethereum client. The contract bytecode of the corresponding address is queried through the client instance; then the inspection result log of this method call is generated, and then the CodeAt method provided by the Ethereum client is called to obtain the contract bytecode. If the bytecode in the contract address is not empty, the core detection logic controlFlowAnalyze method is entered for further analysis. After the analysis is completed, the logHoneyPots method is called to print the contract detection results.

[0072] Since the implementation method of the honeypot is mainly based on the Transfer method in ERC20, in order to detect whether there is a honeypot risk in the contract, the detector implements the control flow of the Transfer method in ERC20 in the analyzeControlFlow method. Before explaining the implementation logic of the analyzeControlFlow method in detail, the present invention needs to clarify the difference between the honeypot logic and the normal logic at the opcode level.

[0073] Combination Figure 3 and Figure 4 As shown, when the contract calls the transfer method to implement token transfer, it will send a call data (call information) containing the signature and parameters of the Transfer method to the contract. Before executing the code logic, it will first enter the function selector in the function wrapper. The function of the function selector is to select which control flow to enter according to the function signature in the call information. If the function selector of the contract does not contain the function signature in the call information, it will be regarded as an illegal call and enter the basic block containing the REVERT opcode and then roll back all operations. When entering the function selector basic block, the stack only contains the function signature of the Transfer method. In this basic block, the signature of the Transfer method is first pushed into the stack, and then the top element of the stack and the method signature are compared. In the same case, the control flow of the corresponding method is entered. Therefore, the control flow of the Transfer method can be forged by forging the Calldata containing the signature of the Transfer method in the function selector part.

[0074] After entering the Transfer method control flow, the value of the call information is checked. Since the token transfer method does not need to accept ETH, the call information containing ETH is considered an illegal call. In this basic block, the ETH value contained in the call information is first obtained. If the value is 0, enter the next basic block. If it is not 0, enter the basic block containing the REVERT opcode to roll back all operations.

[0075] After passing the value test, the call information length test begins. In Ethereum, the function signature occupies 4 bytes, and each parameter contained in the method occupies 32 bytes. Since the token transfer method contains two parameters, the call information length of this method must be at least 68 bytes. Figure 3 The simplified opcode shown in the figure first pushes 0x04 into the basic block, then pushes the call information length, executes the SUB operation to obtain the parameter length of the call information and compares it with 0x40 (the length of two parameters), and if it is greater than the length of two parameters, jumps out of the function wrapper and enters the token transfer method function body. If the parameter length is less than the length of two parameters, enters the basic block containing the REVERT opcode to roll back all operations.

[0076] The following takes a single contract detection algorithm as an example to introduce the implementation process of the solution of the present invention for basic blocks and detection:

[0077] (1) Determine whether the client instance in the detector structure is initialized successfully, and then generate a log file for recording the detection results;

[0078] (2) Obtain the bytecode of the smart contract through the Ethereum client instance, and determine whether the contract complies with the ERC20 standard through the bytecode;

[0079] Convert bytecode into Ethereum virtual machine opcode and separate into basic blocks, pass all basic blocks into the core method for detection, and each basic block contains at least one opcode;

[0080] Among them, the basic block division logic is:

[0081] The JUMPDEST opcode represents the beginning of a basic block. The JUMP, JUMPI, STOP, REVERT, RETURN, and SELFDESTRUCT opcodes represent the end of a basic block. In addition, two additional basic block division methods are defined to adapt to special cases:

[0082] 1. When two JUMPDEST opcodes appear consecutively, the first JUMPDEST is composed into a basic block alone.

[0083] 2. Treat the INVALID opcode as a basic block in its own right.

[0084] (3) Execute a control flow analysis algorithm, the method comprising the following steps:

[0085] (31) Loop through all the opcodes and find the entry for the token transfer method in the ERC20 standard;

[0086] (32) The pseudo depth-first traversal method defines and initializes the stack, memory, and contract permanent storage space in Ethereum;

[0087] Each element in the stack, memory and contract permanent storage space is a structure containing a value and a tainted Boolean value to implement taint analysis. The pseudo depth-first traversal method is responsible for determining the direction of the control flow and saving the status of the stack, memory and contract permanent storage space when each basic block is executed;

[0088] When the STOP or SELFDESTRUCT opcode is executed, the simulation control flow ends.

[0089] When the JUMP or RETURN opcode is executed, the program jumps to the top element of the stack and continues executing.

[0090] When executing the JUMPI opcode, if the value of the second element on the top of the stack is 1, jump to the value of the top element on the stack and continue execution. Otherwise, execute the first opcode in the next basic block in sequence. By default, execute according to the code flow. When a honeypot risk is detected, save the status of the stack, memory, and permanent storage space of the contract at this time and execute two flows separately to detect whether it is a honeypot contract;

[0091] When executing to the REVERT and ASSERT opcodes, read the status of the stack, memory, and permanent storage space of the contract saved by the most recent JUMPI and execute another control flow of the previous basic block. When it can execute normally to the next JUMPI opcode, mark the contract as a honeypot contract.

[0092] (33) Each opcode in the basic block is simulated by executing a simulation algorithm. The algorithm is responsible for updating the status in the stack, memory, and permanent storage space of the contract and for assigning and transmitting tainted information, assigning false values ​​when reading without relying on the data in the real block;

[0093] When the CALLER opcode is executed, the fake token sender address is pushed into the stack and the Boolean value of the token sender taint field is set to true.

[0094] When the CALLDATALOAD opcode is executed, when the value of the top element of the stack is 0x04, the forged token recipient address is pushed into the stack and the Boolean value of the token recipient taint field is set to true. When the value of the top element of the stack is 0x24, the forged token transfer amount value is pushed into the stack and the taint value of the token transfer value taint field is set to true.

[0095] Execution reaches CALLDATASIZE and pushes 0x68 into the stack.

[0096] Execute to MSTORE, pop the top two elements of the stack and store the top element of the stack in the specified memory location. The value and taint of the element are retained in the memory.

[0097] When MLOAD is executed, the top element of the stack is popped and the element at the specified location in the memory is pushed into the stack. The value and taint of the element are retained in the stack.

[0098] Execute to SSTORE pops the top two elements of the stack and stores the top element in the specified location of the contract's permanent storage space. The value and taint of the element are retained in the stack.

[0099] Execute until SLOAD pops an element from the top of the stack and pushes the element at the specified position in the permanent storage space of the contract into the stack. When the top element of the stack only satisfies the token recipient taint field and the taint field of pushing 1 byte of information into the stack is true, it is considered to read the balance of the token recipient address. At this time, the token balance of the simulated token recipient address is pushed into the stack as the value of the element.

[0100] When the top element of the stack only satisfies the token sender taint field and the taint field of 1-byte information pushed into the stack, it is considered to read the balance of the token sender address. At this time, the token balance of the simulated token sender address is pushed into the stack as the value of the element. When the token sender taint field, the 1-byte information pushed into the stack, and the Boolean taint field in the contract permanent storage space are all true at the same time, it is considered to read the Boolean mapping of the token sender address. When the token receiver taint field, the 1-byte information pushed into the stack, and the Boolean taint field in the contract permanent storage space are all true at the same time, it is considered to read the Boolean mapping of the token receiver address. In the latter two cases, the honeypot taint value is set to true, and the two branches of JUMPI will be traversed in the outer method for honeypot analysis.

[0101] The execution merges the values ​​of the elements in the specified length storage space in the KECCAK256 memory, takes the hash and pushes it into the stack, pops the top two elements of the stack and pushes an element into the stack. The stack retains the taints of all elements in the corresponding memory length.

[0102] Execute PUSH1-PUSH32 to push an element of the specified length into the stack. In the case of PUSH1, set the push 1-byte information taint field of the element to true.

[0103] Execute DUP1-DUP16 to copy the element at the corresponding position in the stack to the top of the stack. The value and taint of the element are retained in the stack.

[0104] Execute SWAP1-SWAP16 to swap the elements at the corresponding positions in the stack with the top element of the stack, and retain the value and taint of the element in the stack.

[0105] Execute ADD, SUB, MUL, DIV, AND, LT, GT, EQ, SGT, SLT, EXP to pop the top two elements of the stack and push the result of the operation into the stack. If the taint of any of the two elements is true, the corresponding taint of the pushed element is true.

[0106] If one element satisfies the token receiver taint field or the token sender taint field is true, the other element satisfies the push 1-byte information taint field into the stack is true, and the Boolean taint field in the contract permanent storage space is set to true. When processing the AND opcode, if one of the elements is 0xff, the Boolean taint field in the contract permanent storage space is set to true. When processing the EQ opcode, if one of the elements has a token receiver taint field that is true, the other element pushes a 1-byte information taint field into the stack that is true, and the token receiver taint field taint value is set to true.

[0107] Execute to ISZERO pops the top element of the stack and pushes 0 or 1. If the taint of either element is true, the corresponding taint of the pushed element is true

[0108] Execute to POP to pop the top element of the stack

[0109] Execute to JUMPI pop two elements from the stack

[0110] Execute until JUMP pops an element from the stack

[0111] Execute DELEGATECALL, CALLCODE, STATICCALL, CALL and immediately stop the simulation execution control flow

[0112] (34) Call the contract detection result recording algorithm to write the detection result of the corresponding contract into the log file.

[0113] The present invention only needs to pay attention to whether the jump condition of the JUMPI opcode is related to the storage element at the corresponding position in the contract permanent storage space with the value of PUSH1 as the index in the storage (contract permanent storage space) and the hash as the index. Because the element is directly executed and operated with 0xff as the condition of JUMPI, it means that the element can only be 0 or 1, that is, the element is a Boolean value. If it is related, it means that when the jump condition has the token sender\token receiver address taint, PUSH1data taint and storageIsBool taint at the same time, it will lead to two control flows. When one control flow points to REVERT and the other control flow is executed normally backward, it means that the contract has Transfer lock (transaction lock) logic. The key logic to realize the honeypot is to call external contracts, so it only needs to find whether there are DELEGATECALL, CALLCODE, STATICCALL, and CALL opcodes in the token transfer method control flow. If any one of them exists, it means that the contract has an external call. Since external contracts are usually closed source and do not follow the ERC20 standard, it is impossible to detect external contracts. Therefore, only when these four opcodes are detected will the contract be considered to have a honeypot risk.

[0114] In order to realize the detection of three honeypot contract modes, namely blacklist contract honeypot mode, transaction lock honeypot mode and proxy contract honeypot mode, the present invention designs a pseudo-depth-first traversal method to explore the control flow of the token transfer method, and combines the taint analysis method to simulate the execution of the stack, memory and storage (contract permanent storage space) in the contract.

[0115] First, make sure that the contract bytecode contains the six function signatures of the ERC20 standard. If so, compile the bytecode into EVM opcodes and split them into different basic blocks.

[0116] Table 1 shows the opcodes that represent the start and end of a basic block. The JUMPDEST opcode is used to mark the JUMP or JUMPI target location. JUMP represents an unconditional jump to the first element at the top of the stack to continue execution. JUMPI represents a conditional jump. When the second element at the top of the stack is a non-zero value, it jumps to the first element at the top of the stack to continue execution, otherwise the next opcode is executed sequentially. STOP means stop execution. REVERT means roll back all states. RETURN means stop execution and return the data in memory, which is usually used to return the result after the function call is completed. SELFDESTRUCT is used to destroy the contract and remove the code and storage from the blockchain. In addition, we have specified two additional basic blocks:

[0117] 1. When there are two JUMPDEST opcodes in succession, the first opcode is considered as a basic block alone.

[0118] 2. Treat the INVALID opcode as a basic block. This opcode is used to indicate illegal operations during program execution, which means the abnormal termination of the program.

[0119] The present invention designs a BasicBlockAnalysis structure for each basic block, which includes all the operation codes of the basic block, the program counter of the first operation code, the stack, memory and storage status when the basic block is executed, and the basic block depth.

[0120] The elements in the stack, memory, and storage are defined with the TaintedItem structure, the first field of which is the value, and all other fields are Boolean values ​​related to the taint. The stack is initialized to an empty array of TaintedItem structures, the memory is initialized to a mapping from uint64 type to TaintedItem type elements, and the storage is initialized to a mapping from string type to TaintedItem type elements.

[0121] It is further found that only the JUMPI opcode in the control flow will cause the control flow to fork, and only when the control flow terminates or REVERT will the next opcode of the JUMPI opcode be executed sequentially. In order to avoid invalid depth-first traversal, in the case of non-honeypot logic, only jump to the top element of the stack to continue execution, and only when there is honeypot logic will the two control flows be traversed separately. For this purpose, a pseudo-depth-first traversal dfs method is designed. First, the entrance of the Transfer method is found to enter the Transfer method control flow and simulate the execution of the opcode. When entering the control flow, the simulated stack contains only one element: the Transfer method function signature without any taint, and the memory and storage are empty.

[0122] Table 1. Relationship between the last opcode of a basic block and the direction of control flow

[0123]

[0124] The Dfs method only determines the direction of control flow at the last opcode of the basic block. As shown in Table 2:

[0125] When the last opcode of a basic block is STOP or SELFDESTRUCT, it indicates the end of the program.

[0126] When the last opcode of the basic block is RETURN, it means the method call ends, and the program jumps to the top element of the stack to continue execution.

[0127] When the last opcode of a basic block is JUMP, it represents a conditional jump, jumping to the basic block corresponding to the program counter position of the value of the top element of the stack.

[0128] When the last opcode of a basic block is JUMPI, it represents a conditional jump. In the default state, it jumps to the basic block of the program counter position corresponding to the value of the top element of the stack. When the tainted value of the jump condition element contains an address check and the address mapping is a Boolean value, the stack, memory, and contract permanent storage space status and the jump address by default are saved, and then the next opcode is executed sequentially.

[0129] When the last opcode of the basic block is REVERT or ASSERT, it means that the program ends immediately and all operations of the call are rolled back. This is also the key opcode for the implementation of honeypot logic. When jumping to these two opcode basic blocks, first determine whether the jump condition is suspected of being a honeypot. If the jump condition is suspected of being a honeypot, mark the control flow branch as rollback. Finally, read the stack, memory, contract permanent storage space status saved by the most recent JUMPI and the jump address by default to execute the control flow. When the next JUMPI opcode can be executed normally, mark the contract as a honeypot contract for the corresponding situation.

[0130] This study designs the opcodeexecution method to simulate the execution of opcodes to simulate the status of the stack, memory, and permanent storage space of the contract:

[0131] The CALLER opcode pushes the sender address onto the stack, so in the simulated execution of the CALLER opcode, a fake sender address is pushed onto the stack and the tainted field IsCallerTainted is set to true.

[0132] The CALLDATALOAD opcode adds the parameters of the call information to the stack according to the top element of the stack. When the top of the stack is 0x04, a fake to address is pushed into the stack and the tainted field IsToTainted is set to true. When the top of the stack is 0x24, a fake amount value is pushed into the stack and the tainted field IsTransferValueTainted is set to true.

[0133] The CALLDATASIZE opcode returns the length of the call information, which is the function signature plus the length of the two parameters 0x68

[0134] The MSTORE opcode means storing the top element of the stack in the specified memory location, popping the top two elements of the stack and storing the top element of the stack in the specified memory location, and retaining the value and taint of the element in the memory.

[0135] The MLOAD opcode means reading the element at the specified memory location into the stack, popping the top element of the stack and merging it into the element at the specified memory location on the stack, and retaining the value and taint of the element in the stack.

[0136] The SSTORE opcode means storing the top element of the stack into the specified location of the contract's permanent storage space, popping the two top elements of the stack and storing the top element of the stack into the specified location of the contract's permanent storage space. The value and taint of the element are retained in the contract's permanent storage space.

[0137] The SLOAD opcode means reading the element at the specified position of the contract's permanent storage space into the stack, popping the top element of the stack and pushing it into the element at the specified position of the contract's permanent storage space, and retaining the value and taint of the element in the stack. When the top element of the stack only satisfies the IsToAddressTainted and IsPush1data taint fields to be true, it is considered to read the balance of the to address, and the token balance of the simulated to address is used as the value of the element. When the top element of the stack only satisfies the IsCallerTainted taint field to be true, it is considered to read the balance of the sender address, and the token balance of the simulated sender address is used as the value of the element. When the IsCallerTainted, IsPUSH1Data, and StorageValueIsBool taint fields are true at the same time, it is considered to read the Boolean mapping of the sender address. When the IsToAddressTainted, IsPUSH1Data, and StorageValueIsBool taint fields are true at the same time, it is considered to read the bool mapping of the to address. In the latter two cases, the honeypot taint value is set to true.

[0138] The KECCAK256 opcode represents adding up the values ​​of the elements of the specified length storage space in the memory, taking the hash and pushing it into the stack, popping the top two elements of the stack and pushing an element into the stack, which retains the taint of all elements in the corresponding memory length.

[0139] The PUSH1-32 opcode pushes an element of the specified length into the stack, and in the case of PUSH1 sets the isPUSH1Data taint value of the element to true.

[0140] The DUP1-16 opcode means copying the element at the corresponding position in the stack to the top of the stack and retaining the taint.

[0141] The SWAP1-16 opcode swaps the elements at the corresponding positions in the stack with the top element and retains the taint.

[0142] The ADD, SUB, MUL, DIV, AND, LT, GT, EQ, SGT, SLT, and EXP opcodes represent operations on the first and second elements at the top of the stack, popping the two elements at the top of the stack and pushing one element into the stack. If the taint of any of the two elements is true, the corresponding taint of the pushed element is true. When processing the AND opcode, if one of the elements is 0xff, the StorageValueIsBool taint value is set to true.

[0143] The ISZERO opcode is used to determine whether the top element of the stack is 0. If it is 0, it returns 1, otherwise it returns 0, pops the top two elements of the stack and pushes 0 or 1 into the stack. If the taint of either element is true, the corresponding taint of the pushed element is true.

[0144] The POP opcode is used to pop the top element of the stack.

[0145] The JUMPI opcode is used for conditional jumps. In the simulated execution stack method, only two elements are popped out and no control flow decisions are made.

[0146] The JUMP opcode is used for unconditional jumps. In the simulated execution stack method, only one element is popped and it does not participate in the control flow decision.

[0147] The DELEGATECALL, CALLCODE, STATICCALL, and CALL opcodes represent external calls. When these opcodes are executed, the simulation execution control flow is stopped immediately and a report is returned that a proxy contract exists.

[0148] This study implemented a lightweight ERC20 honeypot contract detector based on Geth v1.13.4, with about 1200 lines of Golang code. All tests were performed on a desktop computer equipped with Intel Core i7 10700, 16GB main memory and 2TB hard disk. The detector has been successfully applied to full nodes using snap synchronization mode and can effectively detect ERC20 honeypot contracts. Considering that Ethereum is one of the most widely used EVM (Ethereum Virtual Machine) compatible chains, the detector can be easily integrated into other EVM compatible chains. As far as we know, there is no research in the existing work on the control flow analysis of token transfer methods based on contract bytecode to detect honeypot contracts.

[0149] UniswapV2 is currently the most popular DEX in Ethereum. Since the publisher of the honeypot contract usually uses a small amount of funds to add liquidity to the liquidity pool, small funds can easily create the illusion of rising token prices. Therefore, this study randomly selected tokens with liquidity of less than 1 ETH in 5031 liquidity pools in the uniswapV2 factory contract. There are 17 honeypot contracts with blacklist mode, 2183 honeypot contracts with transaction lock mode, and 3 honeypot contracts with proxy contracts.

[0150] The present invention downloads the bytecode of contracts that are known to have problems such as GenerateToken, FreezeAccount and DisableTransfer. These problems are similar to the blacklist and transferlock problems we found. There are a total of 189 honeypot contracts with different types of problems, of which 65 contracts have honeypot logic that the contract owner can trade other people's tokens, issue tokens unlimitedly and destroy tokens at will. Since these three honeypot contract implementation methods do not involve ERC20's token transfer method, this detector cannot detect these types of honeypot logic. After detecting the remaining 124 contracts that designed token transfer methods, 109 honeypot contracts were detected. After manually analyzing the undetected attacks, this study found that the honeypot implementation of such contracts has both a Boolean global lock that controls the token transaction and a restricted transaction lock for a single address Boolean mapping, which increases the complexity of the recursion. These findings emphasize the importance of further optimizing the detection algorithm to enhance the ability to identify complex contract logic.

[0151] Table 2. Number of detected contracts and number of different types of honeypot contracts detected

[0152] Number of contracts tested Blacklisted TransferLocked ProxyContract 5031 17 2183 3 124 0 109 0

[0153] Combination Figure 5 As shown in the figure, the peak value of the full node when only syncing the node is almost the same as the peak value when the detection is turned on. The average CPU usage is 21 when only syncing the node, and the average CPU usage is 104 when the detection is turned on. Considering that a block contains less than 200 transactions on average, and most transactions are just ordinary transfer transactions and do not involve contracts, this shows that it is acceptable to integrate the detector into the full node to monitor newly created contracts in the block. This confirms that the performance overhead introduced by our detection system is negligible. The detector enhances security without placing a significant burden on system resources.

[0154] It took a total of 49 seconds to test 5031 contracts. Figure 6The distribution range of the time taken to detect a single contract is shown. Among them, there are 100 contracts with a detection time of 6ms, accounting for 1.99%. There are 855 contracts with a detection time of 7ms, accounting for 17.00%. There are 187 contracts with a detection time of 8ms, accounting for 3.72%. There are 1157 contracts with a detection time of 9ms, accounting for 23.00%. There are 2359 contracts with a detection time of 10ms, accounting for 46.89%. There are 372 contracts with a detection time greater than 10ms, accounting for 7.39%. The average time taken per contract is 9.74 milliseconds. These data emphasize the significant advantages of the detector designed in this study in terms of processing speed, proving its practicality and reliability in efficient contract detection.

[0155] Figure 7 The results of basic block traversal depth detection for 5031 contracts are shown. Among them, there are 100 contracts with a traversal depth of 0. Figure 6 The 100 contracts that took 6ms to detect were matched. After manual analysis, it was found that these 100 contracts did not fully comply with the ERC20 standard, resulting in the detector being unable to locate the entry point of the transfer method. The number of contracts with a traversal depth of 1 was 855, which was also the same as Figure 6 Although these contracts declare the Transfer method, they do not implement specific logic in the method, causing the control flow to stop after entering the Transfer method entrance. The reason why traversal depths of 2 and 3 do not appear is that the second and third basic blocks are mainly used to verify the legitimacy of Calldata. Most of the honeypot logic is executed first after verifying Calldata, which explains the phenomenon that traversal depths of 5 and 6 account for a large proportion. There are 305 contracts with a traversal depth greater than 36, and the depths of these contracts are all 500, which indicates that the detector may enter an infinite loop during the detection process. To prevent excessive consumption of system resources, the detector will actively terminate further detection of these contracts. This phenomenon indicates that the detector still has shortcomings in handling complex logic.

[0156] The experimental results of this example not only demonstrate the accuracy and efficiency of the detection system of the present invention, but also highlight its practicality in enhancing the security of the Ethereum ecosystem. Our ability to systematically, accurately, and in real time detect and analyze ERC20 honeypot contracts has proven to be a powerful supplement to the security of the Ethereum ecosystem. The detection system does not require extraordinary computing power or specialized hardware, thus promoting its adoption in a wider range of Ethereum nodes. This study validates that our detection system is a viable solution to the security challenges of ERC20 honeypot contracts.

Claims

1. A method for detecting Ethereum ERC20 honeypot contracts in blockchain, characterized in that: The method first integrates a honeypot contract detector into an Ethereum client, and the honeypot contract detector verifies whether the contract belongs to the ERC20 standard contract according to the contract address, and then obtains the contract bytecode and converts it into an Ethereum virtual machine operation code; then divides the basic block, finds the entry of the token transfer method in the contract, simulates the execution of the Ethereum virtual machine operation code and simulates the contract control flow, and detects whether the contract meets the predefined honeypot contract mode; The method for judging the ERC20 standard contract is as follows: judging whether the contract bytecode of the contract address contains function signatures corresponding to the ERC20 standard for token transfer, token authorization, and token transfer from a specified address to another address. If the three function signatures are included at the same time, the contract is considered to be an ERC20 contract, otherwise it is considered to be a non-ERC20 contract; The method divides the basic blocks into the following logical parts: The JUMPDEST opcode represents the beginning of a basic block, and the JUMP, JUMPI, STOP, REVERT, RETURN, and SELFDESTRUCT opcodes represent the end of a basic block. In addition, two additional basic block division methods are defined to adapt to special cases: When two JUMPDEST opcodes appear consecutively, the first JUMPDEST is composed of a basic block alone; Treat the INVALID opcode as a basic block in its own right; The predefined honeypot contract modes in the method include blacklist honeypot mode, transaction lock honeypot mode and proxy contract honeypot model, and the judgment method is as follows: Blacklist honeypot mode: When executing a token transfer operation, the contract logic is designed to first check whether the token recipient address is a liquidity pool address in a decentralized exchange. If it is a liquidity pool address, check the Boolean mapping corresponding to the token sender address representing the blacklist. If the Boolean value is true, stop executing the token transfer operation and roll back all operations that have been executed; Transaction lock honeypot mode: When executing a token transfer operation, the contract logic is designed to check the Boolean mapping corresponding to the token sender address and the token receiver address representing the blacklist. If the Boolean value is true, the token transfer operation is stopped and all operations that have been executed are rolled back; Proxy contract honeypot mode: There is an operation of calling an external contract in the control flow corresponding to the token transfer operation.

2. The Ethereum ERC20 honeypot contract detection method in the blockchain according to claim 1 is characterized in that: This method implements lightweight detection directly on the Ethereum execution client, and then analyzes the contract bytecode without relying on the on-chain contract interaction records or the source code provided by the contract creator. Honeypot contract detection implements static data flow analysis on the contract bytecode to infer the honeypot mechanism in the contract, and implements detection and marking based on the contract-based token transfer method, including: The single contract detection algorithm in the honeypot detector first ensures that the client instance has been successfully initialized and can query the contract bytecode of the corresponding address in the Ethereum client; then it generates a log file for recording the detection results and obtains the contract bytecode through the method provided by the Ethereum client; finally, if the bytecode in the contract address is not empty, it enters the analysis of the ERC20 standard contract and the honeypot contract model. After the analysis is completed, the contract detection result recording algorithm is called to write the detection result of the corresponding contract into the log file.

3. The Ethereum ERC20 honeypot contract detection method in the blockchain according to claim 1, characterized in that: The implementation process of a single contract detection algorithm is as follows: (1) Determine whether the client instance in the detector structure is initialized successfully, and then generate a log file for recording the detection results; (2) Obtain the bytecode of the smart contract through the Ethereum client instance, and determine whether the contract complies with the ERC20 standard through the bytecode; Convert bytecode into Ethereum virtual machine opcode and separate into basic blocks, pass all basic blocks into the core method for detection, and each basic block contains at least one opcode; (3) Execute a control flow analysis algorithm, the method comprising the following steps: (31) Loop through all the opcodes and find the entry for the token transfer method in the ERC20 standard; (32) Define and initialize the stack, memory and contract permanent storage space in Ethereum based on pseudo depth-first traversal; Each element in the stack, memory and contract permanent storage space is a structure containing a value and a tainted Boolean value to implement taint analysis. The pseudo depth-first traversal method is responsible for determining the direction of the control flow and saving the status of the stack, memory and contract permanent storage space when each basic block is executed; (33) Each opcode in the basic block is simulated by executing a simulation algorithm. The algorithm is responsible for updating the status in the stack, memory, and permanent storage space of the contract and for assigning and transmitting tainted information, assigning false values ​​when reading without relying on the data in the real block; (34) Call the contract detection result recording algorithm to write the detection result of the corresponding contract into the log file.

4. The Ethereum ERC20 honeypot contract detection method in the blockchain according to claim 1, characterized in that: In the method, when executing token transfer, the contract will send a call message containing the function signature and call parameters of the token transfer method to the contract. Before executing the code logic, the function selector in the function wrapper is first entered. The function selector selects to enter the corresponding control flow according to the function signature in the call message. If the function selector of the contract does not contain the function signature in the call message, it will be regarded as an illegal call and all operations will be rolled back. When entering the function selector basic block, the stack only contains the function signature of the token transfer method. In the corresponding basic block, the function signature of the token transfer method is first pushed into the stack, and then the top element of the stack and the function signature are compared. Under the same circumstances, the control flow of the corresponding method is entered, including entering the token transfer method control flow by forging the calling information containing the function signature and calling parameters of the token transfer method in the function selector part.

5. The Ethereum ERC20 honeypot contract detection method in the blockchain according to claim 1, characterized in that: The honeypot contract detector creates a full node in the Ethereum client startup process, and then calls custom method APIs to register the application programming interface API of the honeypot detector. The custom method APIs define the API namespace, version, service instance and visibility parameters; then the single contract detection algorithm, batch contract detection algorithm and stop detection algorithm executed by the honeypot detector are added to the interactive console of the Ethereum client to realize single, batch or stop detection of contract addresses.

6. The Ethereum ERC20 honeypot contract detection method in the blockchain according to claim 1, characterized in that: The method satisfies the following two conditions for the ERC20 honeypot contract definition: a. Create a liquidity pool in a decentralized exchange and form a trading pair with valuable tokens; b. Victims can buy tokens on decentralized exchanges, but cannot sell tokens on decentralized exchanges.

7. The Ethereum ERC20 honeypot contract detection method in the blockchain according to claim 1, characterized in that: The honeypot contract detector includes creating a detector instance and registering a detector service in an Ethereum client.

8. The Ethereum ERC20 honeypot contract detection method in the blockchain according to claim 1, characterized in that: The detection process of the honeypot contract includes the judgment of the blacklist, transaction lock and proxy contract from the level of simulation execution algorithm and pseudo depth-first traversal method; a. Blacklist honeypot mode Simulation execution algorithm level: determine whether to enter a comparison between a receiver address and the value stored in the contract permanent storage space, and whether the comparison result will lead to two flows. If so, set the receiver address comparison taint of the jump condition element to true; determine whether to enter a statement that reads a Boolean value stored in the contract permanent storage space with the sender address as an index, and whether the value will lead to two flows. If so, set the sender address of the jump condition element to true. At the pseudo-depth-first traversal method level: at the end of the execution of the basic block, if the tainted value of the receiver address of the jump condition element is true, the two flows are traversed separately, and the two flows are recorded as pointing to control flow A and control flow B respectively; at the end of the execution of the basic block, if the Boolean tainted value of the sender address of the jump element is true, the two flows are traversed separately. If one flow causes the execution to terminate and roll back, and the other flow points to A or B, then the contract is considered to have a blacklist honeypot risk; b. Transaction lock honeypot mode Simulation execution algorithm level: determine whether to enter a statement that reads a Boolean value stored in the contract's permanent storage space with the sender or receiver address as an index, and whether the value will lead to two flows. If so, set the sender or receiver address of the jump condition element to a Boolean value of true; At the pseudo-depth-first traversal method level: at the end of the execution of the basic block, if the sender or receiver address of the jump element is a Boolean taint value of true, two flows are traversed respectively. If one flow leads to the termination of execution and rollback, and the other flow points to a basic block that does not contain a rollback opcode, the contract is considered to have a transaction lock type honeypot risk; c. Proxy Contract Honeypot Model Simulation execution algorithm level: As long as the control flow contains opcodes involving external calls, it is considered that there is a proxy contract honeypot risk, and further detection is terminated at this time.

9. The Ethereum ERC20 honeypot contract detection method in the blockchain according to claim 1, characterized in that: The batch contract detection algorithm parses the string type contract address in a single contract detection algorithm into an address type array, and then loops through the detection logic of a single detection algorithm.

10. An Ethereum ERC20 honeypot contract detector in a blockchain, characterized in that: Used to execute the detection method as described in claim 1, it can realize the detection of blacklist contract honeypot mode, exchange honeypot mode and proxy contract honeypot mode.

Citation Information

Cited By

  • Transaction simulation execution-based Ethereum phishing fraud contract detection method

    CN119991134A

  • An Ethereum Phishing Scam Contract Detection Method Based on Transaction Simulation Execution

    CN119991134B

  • Wave field currency (TRX) and Taida currency (USDT-TRC20) exchange (mixed currency) transaction identification method based on wave field chain (TRON)

    CN120070057A