Vulnerability detection method and device for smart contract on block chain
Through fuzzy testing and dynamic analysis, the profit-making vulnerabilities of smart contracts on the blockchain are identified, which solves the problems that are difficult to detect in the existing technology, improves the security and detection efficiency of smart contracts, and prevents economic losses.
Patent Information
- Application Number
- CN202411046450.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-31
- Publication Date
- 2024-11-26
- Estimated Expiration
- 2044-07-31
AI Technical Summary
The existing technology is difficult to quickly and effectively detect profit-making vulnerabilities in smart contracts on the blockchain, especially in complex on-chain smart contract calls, which makes it difficult to guarantee the security of smart contracts, which are vulnerable to attacks and economic losses.
Using the fuzzy testing method, we form an initial seed pool by collecting historical transactions of smart contracts, performing mutation operations and checkpoint evaluation, identifying transactions that generate new test paths and asset token circulation, dynamic analysis and identification of profit-making vulnerabilities, and improving detection efficiency and Validity.
Effectively identify profit-making loopholes in smart contracts, improve detection efficiency, avoid the problem of low flexibility, enhance the security of smart contracts, and prevent economic losses.
Smart Images

Figure CN119026134A_ABST
Abstract
Description
Technical Field
[0001] One or more embodiments of this specification relate to the field of software testing, and more particularly, to a method and device for detecting vulnerabilities in smart contracts on a blockchain. Background Art
[0002] In today’s blockchain environment, in order to complete a variety of complex functions, on-chain smart contracts will collaborate and cooperate with each other. For example, financial smart contracts and exchange rate smart contracts call each other to obtain the latest asset value of financial products.
[0003] With the rapid development of decentralized applications (DApp), a series of on-chain smart contracts are connected to each other and generate calling relationships. For example, in order to realize the basic asset exchange function between two different asset tokens, it is necessary to integrate multiple on-chain smart contracts, introduce extremely complex function calling relationships, increase the possibility of smart contracts being attacked, and also bring major challenges to the security of smart contracts.
[0004] Therefore, while promoting the development of smart contracts, it is also particularly important to ensure the security of smart contract programs on the chain. Unlike traditional vulnerabilities that cause program crashes, attacks on smart contracts or illegal exploitation of defects in smart contracts often lead to economic losses (by calling smart contracts to build seemingly normal transactions, thereby obtaining additional economic benefits, or causing losses to the economic interests of others). This type of defect in smart contracts is called a profit vulnerability.
[0005] Therefore, we hope to have a solution that can use technical means to quickly and effectively detect profit vulnerabilities in highly complex on-chain smart contract calls. Summary of the invention
[0006] One or more embodiments of this specification describe a vulnerability detection method for smart contracts on a blockchain. Based on fuzz testing, a feedback mechanism is introduced to screen effective test seeds and drive the test program to efficiently detect profitable vulnerabilities in smart contracts on the chain.
[0007] According to a first aspect, a vulnerability detection method for a smart contract on a blockchain is provided, comprising:
[0008] Collect multiple historical transactions that call several smart contracts on the blockchain to form an initial seed pool.
[0009] A mutation operation is performed based on the seed transaction in the seed pool to obtain a generated transaction.
[0010] A checkpoint evaluation is performed by executing the generated transaction, and the generated transaction that passes the evaluation is added to the seed pool as a seed transaction. The checkpoint evaluation is used to eliminate invalid transactions and / or identify transactions related to the circulation of asset tokens.
[0011] According to a first asset state after a first transaction is executed in the seed pool, and a second asset state after a second transaction is executed, profit vulnerabilities of the plurality of smart contracts are identified, wherein the first transaction is a historical transaction, and the second transaction is a generated transaction related to the first transaction.
[0012] In one implementation, performing the checkpoint evaluation by executing the generated transaction includes:
[0013] Extracting first track information from the execution track of executing the generated transaction, wherein path jump related information is recorded;
[0014] According to the first trace information, it is determined whether the generated transaction generates a new test path and whether it is rolled back by the program.
[0015] If it generates a new test path and is not rolled back, it is determined that the generated transaction passes the evaluation.
[0016] In one scenario of the above implementation, the first trace information includes a first count value of a program counter corresponding to the execution of a jump-related instruction and a first destination of the jump. Determining whether the generating transaction generates a new test path includes:
[0017] If the first count value is not covered in the global count storage set, it is determined that a new test path is generated, and the first count value is added to the global count storage set.
[0018] If the global count storage set has covered the first count value, and the global destination storage set has not covered the first destination, it is determined to generate a new test path, and the first destination is added to the global destination storage set.
[0019] According to one embodiment, performing the checkpoint evaluation by executing the generating transaction includes:
[0020] The second trace information is extracted from the execution trace of executing the generated transaction, wherein the function call information related to the token circulation is recorded.
[0021] According to the second track information, determine whether the generated transaction generates new asset token circulation information.
[0022] If it generates new asset token transfer information, it is determined that the generated transaction has passed the evaluation.
[0023] In one scenario of the above implementation, extracting second track information from the execution track of executing the generated transaction includes:
[0024] The function call instruction is extracted from the execution trace, and the target instruction and the target location of the called function as the target function related to the token transfer are determined.
[0025] The first token transfer information is read from the target location in the storage space, including the starting point, target, and quantity of this token transfer.
[0026] In one scenario of the above implementation, the opcode of the function call instruction includes CALL and DELEGATECALL; the target function includes transfer and transferFrom.
[0027] In one scenario of the above implementation, determining whether the generation transaction generates new asset token transfer information includes:
[0028] If the storage information at the target location in the global token circulation storage set does not cover the first token circulation information, it is determined to generate new asset token circulation information, and the first token circulation information is added to the global token circulation storage set.
[0029] According to one embodiment, the first transaction is initiated by a first user, the second transaction is initiated by a second user, the first transaction and the second transaction are executed based on the same blockchain state, and the identifying of the profit vulnerabilities of the plurality of smart contracts includes:
[0030] If the assets of the second user in the second asset state are greater than the assets of the first user in the first asset state, it is determined that the plurality of smart contracts have profit loopholes.
[0031] According to one embodiment, the first transaction is initiated by a first user and executed based on a first historical state; the second transaction is initiated by a second user and executed after simulating the execution of the first transaction in a test environment, and the identifying of the profit vulnerabilities of the plurality of smart contracts includes:
[0032] If the assets of the first user in the second asset state are less than the assets of the first user in the first asset state, it is determined that the plurality of smart contracts have profit loopholes.
[0033] In one scenario of the above implementation, the second asset status is obtained in the following manner:
[0034] The generated third transaction is executed to obtain a first intermediate state, where the third transaction is initiated by the second user.
[0035] The first transaction is simulated and executed based on the first intermediate state to obtain a second intermediate state.
[0036] The second transaction is executed based on the second intermediate state to obtain the second asset state.
[0037] According to one embodiment, the mutation operation based on the seed transaction in the seed pool includes:
[0038] Mutate the parameters contained in any single seed transaction; or, mutate the combination of two seed transactions.
[0039] According to one embodiment, collecting multiple historical transactions of calling multiple smart contracts on the blockchain also includes:
[0040] For any target historical transaction, the block number and block timestamp of the block where the transaction is located are obtained, and they are stored in the seed pool corresponding to the target historical transaction for re-execution of the transaction.
[0041] According to one implementation, the multiple historical transactions do not include invalid transactions, and the invalid transactions are transactions that trigger a revert rollback operation.
[0042] According to a second aspect, a vulnerability detection device for a smart contract on a blockchain is provided, comprising:
[0043] The collection module is configured to collect multiple historical transactions of calling several smart contracts on the blockchain to form an initial seed pool.
[0044] The seed mutation module is configured to perform a mutation operation based on the seed transactions in the seed pool to obtain generated transactions.
[0045] The seed evaluation module is configured to perform a checkpoint evaluation by executing the generated transaction, and add the generated transaction that passes the evaluation as a seed transaction to the seed pool, wherein the checkpoint evaluation is used to eliminate invalid transactions and / or identify transactions related to the circulation of asset tokens.
[0046] The identification module is configured to identify profit vulnerabilities of the plurality of smart contracts based on a first asset state after a first transaction is executed in the seed pool, and a second asset state after a second transaction is executed, wherein the first transaction is a historical transaction and the second transaction is a generated transaction related to the first transaction.
[0047] According to a third aspect, a computer program product is provided, comprising a computer program / instruction, which, when executed by a processor, implements the steps of the method described in the first aspect.
[0048] According to a fourth aspect, a computing device is provided, comprising a memory and a processor, wherein an executable code is stored in the memory, and when the processor executes the executable code, the method described in the first aspect is implemented.
[0049] In the method and apparatus provided in the embodiments of the present specification, by introducing two modules responsible for evaluating the validity of test seeds and responsible for identifying profitable transactions, on the one hand, invalid test seeds that cause rollback transactions can be eliminated in fuzzy testing, thereby improving the efficiency of fuzzy testing; on the other hand, test seeds that generate asset token circulation transactions can be effectively identified, thereby avoiding the problem of missed detection in fuzzy testing that only considers coverage; on the other hand, profitable vulnerabilities can be efficiently identified through dynamic difference analysis, thereby solving the problem of low flexibility brought about by identifying profitable vulnerabilities through preset vulnerability predictions, thereby improving the effectiveness of detecting profitable vulnerabilities in smart contracts. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention, and for ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0051] Figure 1A A schematic diagram of an exemplary profit-making vulnerability situation disclosed in the embodiments of this specification;
[0052] Figure 1B A schematic diagram of an exemplary profit-making vulnerability situation disclosed in the embodiments of this specification;
[0053] Figure 1C A schematic diagram of an exemplary profit-making vulnerability situation disclosed in the embodiments of this specification;
[0054] Figure 2 This is a schematic diagram of the implementation architecture disclosed in the embodiments of this specification;
[0055] Figure 3 A flowchart of a vulnerability detection method for smart contracts on a blockchain provided according to an embodiment of this specification;
[0056] Figure 4 A schematic diagram of a seed pool initialization scenario provided according to an embodiment of this specification;
[0057] Figure 5A A checkpoint instrumentation pseudo code is provided according to an embodiment of this specification;
[0058] Figure 5B A pseudo code for checkpoint evaluation provided according to an embodiment of this specification;
[0059] Figure 5C A checkpoint instrumentation pseudo code is provided according to an embodiment of this specification;
[0060] Figure 5D A pseudo code for checkpoint evaluation provided according to an embodiment of this specification;
[0061] Figure 6 Schematic diagram of a vulnerability detection device for smart contracts on a blockchain according to an embodiment of the present specification. DETAILED DESCRIPTION
[0062] The solution provided by the embodiments of this specification is described below in conjunction with the accompanying drawings.
[0063] As mentioned above, there are various profit-making behaviors on the blockchain, which use profit-making vulnerabilities to attack smart contracts. Compared with traditional vulnerabilities that cause program crashes, attacks on smart contracts usually cause asset losses, so they are called profit-making behaviors. Profit-making behaviors obtain additional benefits through seemingly normal transaction behaviors. Specifically, the adversary obtains additional improper benefits for himself or causes damage to the assets of other users by implementing profit-making behaviors (for example, submitting carefully designed transaction calls to smart contracts).
[0064] In fact, attacks that lead to significant asset losses are usually caused by attacks on profit loopholes in on-chain smart contracts. For example, in one example, a normal transaction in the asset token trading market involves the payment of appropriate fees. However, if there is incomplete access control in the smart contract, it will give the adversary the possibility of circumventing the fees, which may cause significant losses to the project. In another example, a normal transaction requires borrowing from an application and depositing sufficient collateral assets. However, due to the insecure data dependency in the smart contract, the adversary can conduct price manipulation attacks, manipulating the value of the collateral assets so that it can obtain more loans than normal through the collateral assets.
[0065] Two common profit-making behavior scenarios are listed below for detailed explanation.
[0066] In one case, if Figure 1AAs shown in the figure, an exemplary scenario of using the profit vulnerability to attack is provided. In this scenario, the adversary uses the profit vulnerability to obtain additional benefits for himself by implementing transaction calls that are not expected by the smart contract developer. Specifically, the user hopes to borrow a certain amount of asset tokens (named USDC) through an application called Unipound (reflected as a contract on the chain). Under normal circumstances, the user only needs to call the borrow function of the smart contract Unipound (the transaction is marked with TX2 in the figure) to lend the USDC asset token. As can be seen from the figure, while the asset is being lent, the real-time price of the lent asset token will be queried in real time (the getPrice function call marked with "STATICCALL" in TX2 in the figure), that is, the lending of USDC asset tokens in the Unipound application will depend on the real-time price of the USDC asset token in Uniswap. At this time, in order to obtain additional benefits, the adversary may take advantage of this unsafe dependency and increase the number of USDC asset tokens in the asset token trading market through an asset token swap transaction (indicated by TX1 in the figure) before the USDC asset token lending operation (when the number of an asset token in the trading market increases, its price will decrease), thereby manipulating the price of the USDC asset token. After that, when the adversary lends USDC asset tokens through TX2, he can pay the same amount of money but lend more USDC asset tokens. Finally, the adversary restores the price of the USDC asset token through an asset token swap (indicated by TX3 in the figure) that is opposite to the previous swap transaction. After this operation, the adversary takes advantage of the unsafe dependency in the smart contract and obtains more asset tokens than can be obtained through regular exchanges with equivalent capital investment, thereby achieving the purpose of obtaining additional benefits.
[0067] In another case, if Figure 1BAs shown in the figure, an exemplary scenario of attacking by exploiting the profit vulnerability is provided. In this scenario, the adversary exploits the profit vulnerability by implementing additional smart contract transaction calls to cause other users to suffer asset losses. Specifically, the user attempts to purchase an NFT (Non-Fungible Token) asset token from a trading market called Opensea. Under normal circumstances, the user only needs to purchase the NFT asset token (NFT_ID in the figure) by calling the buy transaction function of the smart contract (the transaction is marked with TX2 in the figure). As can be seen from the figure, when the user makes a purchase, the real-time price of the purchased NFT asset token will be queried in real time (the getPrice function call marked with "STATICCALL" in TX2 in the figure), and then the funds required to purchase the NFT asset token will be transferred from the user account to the account of the counterparty at the queried price (the transfer function call marked with "CALL" in TX2 in the figure). However, when the adversary (who may be the counterparty of this purchase transaction) recognizes that the user intends to purchase the NFT asset token, it will initiate an additional transaction call (the transaction is marked with TX1 in the figure) before the user's purchase transaction to increase the price of the target NFT asset token, so that when the user makes the purchase, he will purchase the NFT asset token at a manipulated price and suffer additional asset losses.
[0068] It should be noted that the above is an example of a profit-making vulnerability in a smart contract on the chain. In real scenarios, the types of profit-making vulnerabilities are not limited to this. In other cases, profit-making vulnerabilities may also be caused by reentry attacks, unauthorized access, etc. These are not introduced one by one in the embodiments of this specification.
[0069] Through the above examples, it is not difficult to find that attacks on profit vulnerabilities of on-chain smart contracts are usually intertwined with normal transaction calls, and will not cause explicit program crashes. By making some additional normal transaction calls, the purpose of profit can be achieved. It has strong concealment and correlation with contextual transaction calls. In order to detect profit vulnerabilities of on-chain smart contracts, it is necessary to design a set of test methods that can accurately restore the transaction call context.
[0070] Since the profit-making vulnerabilities of on-chain smart contracts have the characteristics described above, it poses a challenge to the methods of detecting profit-making vulnerabilities in existing technologies.
[0071] In some existing technologies, vulnerability detection is performed on the target smart contract in an off-chain manner, that is, the target smart contract is deployed locally, and transaction calls are continuously sent to it to try to reproduce the vulnerability. However, profit vulnerabilities often arise from logical errors in the mutual calls between multiple smart contracts on the chain. The off-chain method lacks a comprehensive simulation of the on-chain environment, and cannot identify and reproduce the transaction call context related to the profit vulnerability, and cannot detect profit vulnerabilities with complex transaction call relationships.
[0072] In other existing technologies, since profit-making vulnerabilities in smart contracts do not cause system downtime or program crashes like traditional vulnerabilities, and profit-making behaviors come from the exploitation of smart contract logic vulnerabilities, profit-making vulnerabilities are identified by presetting customized vulnerability predictions in the test based on the prediction of possible profit-making behaviors. However, this method is difficult to identify new profit-making behaviors, which affects the effectiveness of profit-making vulnerability detection and is not very flexible.
[0073] There are also some existing technologies that, in the test of profit vulnerability detection, focus on fuzz testing of transaction calls that are not covered by the test, and no longer test the transaction calls that have been tested. However, in the test of some transaction calls, due to the existence of security checks (for example, parameter validity checks, permission checks, etc.), the calls to the functions in the test transactions are rolled back because they do not meet the verification rules, resulting in a large number of test transactions generated that only test the shallow logic of the smart contract and are marked as covered. They are no longer tested in subsequent tests, and the core logic of the smart contract is not tested. In addition, some key functions must be called repeatedly in a specific context to trigger profit vulnerabilities. In this case, if the key function is marked as covered when it is first called and is no longer paid attention to in subsequent tests, it is impossible to effectively identify profit vulnerabilities. Figure 1CAs shown in the figure, an exemplary scenario of using the profit-taking vulnerability for attack is provided. In this scenario, the user expects to exchange an asset token (as shown in the transaction call in line 1 in the figure). After the user initiates the transaction call, the exchange of the asset token will be implemented in the last two transaction calls (the transaction calls in lines 11 and 12 in the figure), and the handling fee required for the exchange is obtained from the transaction call initiated by the user (as shown by the arrow in the figure). In this way, a logical vulnerability is generated in the transaction call context. The adversary can use some technical means to adjust the handling fee parameter to zero in the transaction call initiated by the user to avoid being charged. The negative consequences of such a profit-taking vulnerability will only appear when testing to line 11. In this process, the key function is the transferFrom function, which is repeatedly called many times in the context of this transaction (for example, lines 4, 9, and 11). Among them, only the transferFrom function call in line 11 will trigger the profit-taking vulnerability. If the test direction is determined only by coverage, the test of the transferFrom function ends when the transferFrom function is called for the first time on line 4. In subsequent tests, the call to this key function will be ignored, and the profit vulnerability cannot be identified.
[0074] In order to solve the above problems, a method and device for detecting vulnerabilities in smart contracts on the blockchain are proposed in the embodiments of this specification. By introducing two modules responsible for evaluating the validity of test seeds and identifying profitable transactions, the detection efficiency of profitable vulnerabilities in smart contracts on the chain can be improved. By using a dynamic difference analysis method, profitable vulnerabilities can be efficiently identified, solving the problem of low flexibility caused by identifying profitable vulnerabilities through preset vulnerability predictions, and improving the effectiveness of detecting profitable vulnerabilities in smart contracts.
[0075] Figure 2 An implementation framework disclosed in the embodiment of this specification is shown. In this embodiment, the detection of profit vulnerabilities of smart contracts on the chain is divided into three stages.
[0076] In phase 1, for multiple smart contracts being tested, historical transactions that call these smart contracts are collected from blockchain nodes. In a specific scenario, the historical data on the blockchain nodes are all publicly queryable. The JSON-RPC API interface is used to initiate historical transaction data queries on each blockchain node, collect historical transaction data, and store it in JSON format. In addition, in this phase, the metadata of each historical transaction will be extracted, the execution trajectory will be checked, and the seed pool will be initialized using the collected data.
[0077] Thus, a seed pool is obtained which stores several historical transactions as test seeds. In stage 2, any historical transaction is selected from the seed pool and mutated to obtain the generated transaction. The historical transaction and the generated transaction are simulated and executed separately to obtain the corresponding asset states after the simulated execution is completed. The asset state corresponding to the historical transaction is marked as the first asset state in the figure, and the asset state corresponding to the generated transaction is marked as the second asset state in the figure. During the execution of the generated transaction, the execution trajectory is monitored, and the generated transaction is evaluated according to the screening rules based on the features contained in the execution trajectory, and the generated transaction that passes the evaluation is added to the seed pool as a seed. The specific content of the screening rule will be elaborated in detail below.
[0078] By simulating the execution of historical transactions and generated transactions, two corresponding asset states are obtained. In Phase 3, differential analysis will be performed to identify profit loopholes in the smart contract.
[0079] The above is an overview of the technical concept in an exemplary embodiment of this specification. Figure 3 In the figure, a flow chart of a vulnerability detection method for smart contracts on a blockchain according to an embodiment of this specification is shown. It can be understood that the method can be executed by any device, equipment, platform, or device cluster with computing and processing capabilities. Figure 3 , in this embodiment, the method includes at least the following steps: Step S301: Collect multiple historical transactions that call several smart contracts on the blockchain to form an initial seed pool. Step S303: Perform mutation operations based on the seed transactions in the seed pool to obtain generated transactions. Step S305: Perform checkpoint evaluation by executing the generated transactions, and add the generated transactions that pass the evaluation to the seed pool as seed transactions. The checkpoint evaluation is used to eliminate invalid transactions and / or identify transactions related to the circulation of asset tokens. Step S307: According to the first asset state after the execution of the first transaction in the seed pool, and the second asset state after the execution of the second transaction, identify the profit loopholes of the several smart contracts, wherein the first transaction is a historical transaction and the second transaction is a generated transaction related to the first transaction.
[0080] The specific implementation methods of the above steps will be described in detail below with reference to the accompanying drawings.
[0081] In step S301, multiple historical transactions of calling several smart contracts on the blockchain are collected to form an initial seed pool.
[0082] Figure 4The following is a schematic diagram of a seed pool initialization scenario provided according to an embodiment. It can be understood that in any node of the blockchain, each block that has been consensus-linked to the blockchain will be recorded, and each block will record some transactions that have been linked to the blockchain, which can be called historical transactions. Referring to the historical transaction part in the figure, it can be seen that the historical transactions on the blockchain can include transaction tx0 executed in a certain blockchain state (taking state S0 in the figure as an example), where executing transaction tx0 in state S0 can change the blockchain state to S1. Each transaction tx executed in sequence i , change the blockchain state from S i-1 Change to S i .
[0083] By sampling the real on-chain transactions recorded in the blockchain, the collected historical transactions can be obtained. In one embodiment, since the main focus is on the profit loopholes of smart contracts, only transactions that call the target smart contract can be collected, and ordinary transfer transactions, for example, are not concerned. The target smart contract can be all contracts on the chain, or several smart contracts of interest.
[0084] According to one implementation, only necessary transaction metadata is collected for historical transactions. Specifically, for any target historical transaction, the block number and block timestamp of the block where the historical transaction occurred, as well as the address of the transaction participant, transaction body and other information data of the historical transaction are obtained. These data are stored in the seed pool corresponding to the target historical transaction for re-execution of the transaction. In one example, referring to the transaction metadata part in the figure, for any historical transaction tx i , capture the address of the participants in the transaction (marked by From and To in the figure), the transaction body (marked by Data in the figure), and the block information corresponding to the transaction (marked by Env in the figure), including the block number (marked by BlockNumber) and the timestamp (marked by Timestamp). In other examples, the ABI interface information of the smart contract related to the historical transaction will also be analyzed. The ABI contains the function interface information exposed by the tested smart contract. Saving this information will help call the smart contract function according to the ABI interface information during the test execution transaction.
[0085] According to one implementation, in the process of collecting historical transactions, the execution track information of each transaction is captured. See the seed pool initialization part in the figure to generate state transition S i →S i+1 Historical transactions tx i For example, the execution trace part of the dotted line box shows an example of capturing transaction tx iThe execution trace data contains a series of instructions obtained based on bytecode parsing. Each instruction includes data such as program counter (PC), operation code (opcode) and other data (such as operands). For example, in the first line of instructions, the value of the program counter is 0x5, the operation code is PUSH1, and the operand is 0X73. In addition, the asset tokens involved in the transaction will also be collected and saved. For example, in the transaction tx i In the case of conversion between WETH and USDC asset tokens, this information will be collected (the asset token part outlined by the dotted line in the figure). i Execute tx i Transaction, generating a new state S i+1 In this new state, the initiator’s asset information can be marked as V (From, S i+1 ), where From is the transaction initiator, which corresponds to the transaction initiator (From) information in the transaction metadata described above.
[0086] The multiple historical transactions that call several smart contracts collected from the blockchain are collected according to the above rules, and each historical transaction is used as a seed transaction and stored in the seed pool to complete the initialization of the seed pool.
[0087] In one implementation, invalid transactions are not included in the collected historical transactions. The invalid transactions are transactions that trigger the revert rollback operation. That is, in the captured transaction track of the transaction, if there is an instruction of the revert operation code, it means that the transaction is rejected by the smart contract for some reason, resulting in a function call rollback operation. Such transactions will not be effectively used in the subsequent profit vulnerability detection, so such historical transactions can be omitted from collection.
[0088] After the seed pool is initialized, in step S303, a mutation operation is performed based on the seed transactions in the seed pool to obtain a generated transaction.
[0089] In one implementation, the mutation operation is to mutate the parameters included in any single seed transaction in the seed pool to generate a transaction.
[0090] In another implementation, the mutation operation is to perform a combined mutation on two or more seed transactions in the seed pool to obtain a generated transaction. According to a specific scenario, the seed transactions are combined and mutated based on the havoc method.
[0091] Next, in step S305, a checkpoint evaluation is performed by executing the generated transaction, and the generated transaction that passes the evaluation is added to the seed pool as a seed transaction. The checkpoint evaluation is used to eliminate invalid transactions and / or identify transactions related to the circulation of asset tokens.
[0092] In order to avoid over-exploration of useless codes / generated transactions and increase the diversity of seed transactions in the seed pool, a checkpoint (waypoint) evaluation is performed during the execution of generated transactions. On the one hand, invalid transactions are eliminated, and generated transactions that generate invalid transactions will not be added to the seed pool as seed transactions; on the other hand, transactions related to the circulation of asset tokens generated during the execution of generated transactions are identified, and based on this, it is determined whether the generated transactions can be added to the seed pool as seed transactions.
[0093] Generally speaking, checkpoint evaluation includes two parts of operations. The first part is to insert the monitored opcodes for the generated transaction of the current simulation execution, and count the inserted opcodes hit in the current simulation execution. During the simulation execution, it can be executed based on the starting state of the original historical transaction corresponding to the generated transaction. For example, if the current generated transaction tx is based on Figure 4 The historical transaction tx0 in the block is obtained by mutation, and the block information corresponding to the historical transaction tx0 can be obtained, and then the blockchain state S0 when executing tx0 is obtained from the blockchain node, and the generated transaction tx is executed based on the state S0. Alternatively, the generated transaction can also be executed in a specified state of interest. The second part is to determine whether the generated transaction of the current simulated execution has generated a new execution trajectory, whether it has been rolled back, or whether it involves a new asset token transfer based on the plug-in hit statistics and global hit statistics of the current simulated execution (that is, the plug-in hit statistics in the entire profit vulnerability detection process), so as to make a checkpoint evaluation result. Next, the checkpoint evaluation method will be further introduced in conjunction with the embodiments and drawings.
[0094] According to one implementation, in order to avoid the possible over-exploration of useless code / generated transactions, the validity of the generated transaction is judged. Specifically, the first track information is extracted from the execution track of the generated transaction, in which the path jump related information is recorded. According to the first track information, it is determined whether the generated transaction generates a new test path and whether it is rolled back by the program. If it generates a new test path and is not rolled back, it is determined that the generated transaction passes the evaluation, and the generated transaction is added to the seed pool as a seed transaction.
[0095] Among them, extracting the first trace information from the execution trace of the generated transaction includes inserting the operation code that generates the path jump and obtaining the path jump related information as the first trace information. Typically, the inserted operation code that generates the path jump may include JUMP and JUMPI. Correspondingly, the obtained first trace information may include the first count value of the program counter corresponding to the execution of the above jump related instruction, and the first destination of the jump. Based on this information, it can be determined whether the generated transaction generates a new test path.
[0096] Specifically, if the first count value is not covered in the global count storage set, it is determined that a new test path is generated, and the first count value is added to the global count storage set. If the global count storage set has covered the first count value, but the global destination storage set does not cover the first destination, it is also determined that a new test path is generated. In this case, the first destination can be added to the global destination storage set.
[0097] In addition, it is also necessary to determine whether the generated transaction generates a rollback, which can be determined based on whether there is an opcode or instruction indicating a rollback in its trajectory information. For example, if there is an instruction with the opcode revert, it means that the generated transaction was rejected by the smart contract for some reason, resulting in a rollback operation. If such a generated transaction is added to the seed pool as a seed transaction, and is mutated and continuously tested and executed based on the transaction in subsequent tests, it will be continuously rejected by the smart contract for the same reason, resulting in a waste of resources and reducing the efficiency of profit vulnerability detection.
[0098] by Figure 5A Taking the pseudo code shown as an example, a checkpoint stub according to an embodiment is provided. In the simulated execution of the generated transaction, the execution trace data of the generated transaction is collected (the variable trace shown in the figure). The opcode is detected for each instruction in the execution trace data (the for loop in lines 1-7 shown in the figure). If the instruction contains path jump related information, that is, the opcode is one of JUMP and JUMPI (the judgment shown in the second line of code in the figure), the count value (first count value) of the program counter (PC) of the instruction is stored in the count storage set (Br local ), as shown in the third line of code in the figure; in addition, the destination (first destination) pointed to by the jump contained in the instruction is stored in the destination storage set (C local ), as shown in line 5 of the figure.
[0099] Next, Figure 5BTaking the pseudo code shown as an example, a checkpoint evaluation method in this embodiment is provided. After each generated transaction simulation is executed, the collected count storage set and destination storage set are compared with the global count storage set (Br) and the global destination storage set (C) respectively. Different from the count storage set and the destination storage set, the global count storage set and the global destination storage set store the program counters of the instructions related to the path jump that have been executed during the overall process of this profit vulnerability detection and the information of the jump destination. See Figure 5B , this pseudo code will output the checkpoint evaluation result (is_waypoint), true means passing the evaluation, false means failing the evaluation. During the checkpoint evaluation process, each program count and jump destination will be judged, as shown in the for loop from line 2 to line 10.
[0100] Among them, if the first count value included in the first trace information is not covered in the global count storage set (as shown in the third line of code in the figure), it proves that a new test path is generated during the simulation execution of the generated transaction. In this case, the generated transaction passes the checkpoint evaluation (as shown in the fourth line of code in the figure), and the first count value is stored in the global count storage set (as shown in the fifth line of code in the figure). If the first count value has been covered in the global count storage set, the first destination pointed to by the jump contained in the instruction is judged. If the first destination is not covered in the global destination storage set and the instruction is not rolled back (as shown in the sixth line of code in the figure), it proves that a new destination jump is generated during the simulation execution of the generated transaction, which is also regarded as a new test path. In this case, the generated transaction passes the checkpoint evaluation (as shown in the seventh line of code in the figure), and the first destination is stored in the global destination storage set (as shown in the eighth line of code in the figure).
[0101] According to another implementation, the checkpoint evaluation also includes determining the relevance of the generated transaction to the asset token circulation. Specifically, the second track information is extracted from the execution track of the generated transaction, in which the function call information related to the token circulation is recorded. According to the second track information, it is determined whether the generated transaction generates new asset token circulation information. If it generates new asset token circulation information, it is determined that the generated transaction passes the evaluation, and the generated transaction is added to the seed pool as a seed transaction.
[0102] In one scenario of the above implementation, in the process of extracting the second trajectory information, focus on the instructions containing functions related to token transfer, and collect relevant information about token circulation. Specifically, the opcodes related to function calls are inserted in the execution trajectory to extract function call instructions, and the target instructions of the target function related to token transfer are screened out, and the target position of the target instruction is determined. The first token circulation information is read from the target position in the storage space, including the starting point, target and quantity of this token transfer. In a typical contract compilation language, opcodes related to function calls may include: CALL, DELEGATECALL; target functions related to token transfer include: transfer function, transferFrom function.
[0103] In this scenario, if the storage information at the target location in the global token circulation storage set does not cover the first token circulation information, it is determined that new asset token circulation information is generated, and the first token circulation information is added to the global token circulation storage set.
[0104] by Figure 5C Taking the pseudocode shown as an example, a checkpoint stub according to an embodiment is provided. In the simulated execution of the generated transaction, the execution trace data is collected (the variable trace shown in the figure). The opcode is detected for each instruction in the execution trace data (the for loop in lines 2-7 shown in the figure). If the instruction contains a function call instruction, that is, the opcode is one of CALL and DELEGATECALL (the judgment shown in the third line of code in the figure), and the function called by the function call instruction is a target function related to the token transfer, such as a transfer function or a transferFrom function, or the parameter value (call value) of the function call is not zero, it is determined that a new asset token circulation has been generated. Collect the target position (PC) of the target instruction, and read the first token circulation information (token) from the storage space (Memory&Stack) flow ), store the first token transfer information into the token transfer storage set (F local ), as shown in line 5 of the figure.
[0105] Next, Figure 5D Taking the pseudo code shown as an example, a checkpoint evaluation method in this embodiment is provided. After each generated transaction simulation is executed, the collected token circulation storage set is compared with the global token circulation storage set (F). The global token circulation storage set stores the transaction context information related to the asset token circulation that has been executed during the overall process of this profit vulnerability detection. Figure 5D, this pseudo code will output the checkpoint evaluation result (is_waypoint), true means passing the evaluation, false means failing the evaluation. During the checkpoint evaluation process, each transaction related to the asset token circulation will be judged, as shown in the for loop from line 2 to line 7.
[0106] Among them, if the storage information at the target position in the global token circulation storage set (i represents the target position in the figure) does not cover the first token circulation information (as shown in the third line of code in the figure), it proves that a new asset token circulation is generated during the simulation execution of the generated transaction. In this case, the generated transaction passes the checkpoint evaluation (as shown in the fourth line of code in the figure), and the first token circulation information is added to the global token circulation storage set (as shown in the fifth line of code in the figure).
[0107] One or more of the above embodiments describe a method for performing checkpoint evaluation during the simulated execution of a generated transaction. Through checkpoint evaluation, invalid generated transactions can be avoided from being added to the seed pool as seed transactions, thereby avoiding excessive exploration of useless code / generated transactions. In addition, by paying attention to token circulation information, the seed transactions in the seed pool can provide richer execution contexts for token circulation-related functions, thereby increasing the depth of mining.
[0108] It should be understood that the above steps S303 and S305 can be repeatedly executed, and seed transactions are continuously selected from the seed pool as the basis for mutation, new generated transactions are generated, and checkpoint evaluation is performed on them. The generated transactions that pass the evaluation are added to the seed pool as seed transactions as the basis for subsequent mutations. In this way, a large number of generated transactions will be gradually added and accumulated in the seed pool. These generated transactions, together with the original historical transactions, serve as the basis for profit vulnerability detection.
[0109] Specifically, as mentioned above, by executing the execution tracks generated by historical transactions and generated transactions, the asset states corresponding to before and after execution can be obtained. In the next step, through difference analysis, based on the comparison of the asset states of historical transactions and generated transactions, the profit loopholes in the smart contract will be identified.
[0110] In step S307, the profit loopholes of the plurality of smart contracts are identified based on the first asset state after the execution of the first transaction in the seed pool, and the second asset state after the execution of the second transaction, wherein the first transaction is a historical transaction and the second transaction is a generated transaction related to the first transaction.
[0111] As mentioned earlier, in attacks against smart contract profit vulnerabilities, the adversary obtains additional profits for itself by implementing transaction calls that are not expected by the smart contract developer, or causes other users to suffer asset losses by implementing additional smart contract transaction calls.
[0112] According to one embodiment, the first transaction is initiated by a first user, the second transaction is initiated by a second user, and the first transaction and the second transaction are executed based on the same blockchain state. If the assets of the second user in the second asset state are greater than the assets of the first user in the first asset state, it is determined that the plurality of smart contracts have profit loopholes.
[0113] Specifically, the first transaction as a historical transaction is initiated by the user, and the transaction is based on the blockchain state S pre Execute. After execution, the new blockchain status is S post , the first transaction can be expressed as tx = (user, S pre ,S post ). The second transaction is a generation transaction, initiated by the adversary, based on the same blockchain state S pre Execute, and generate a new blockchain state S' post , the second transaction can be expressed as tx0 = (adversary, S pre ,S' post The assets owned by the user after the first transaction as a historical transaction is executed, denoted as V(user,S post ), which is the normal asset that the user can obtain in the historical transaction. The asset owned by the adversary after the generated transaction is executed is denoted as V(adversary, S' post ), which is the asset that the adversary can obtain after executing the constructed second transaction. post )>V(user,S post ), it means that, under the same blockchain state, more asset gains can be obtained by executing constructed transactions than normal transactions, that is, the adversary obtains additional gains for himself by implementing transaction calls beyond the expectations of the smart contract developers. Therefore, it can be determined that there is a profit vulnerability in the smart contract.
[0114] According to another embodiment, the first transaction as a historical transaction is initiated by the first user (ordinary honest user) and executed based on the first historical state; the constructed second transaction is initiated by the second user (adversary) and executed after simulating the execution of the first transaction in the test environment. If the second asset of the first user in the second asset state is less than the first asset of the first user in the first asset state, it is determined that the several smart contracts have profit loopholes. In other words, if the first transaction of the honest user is executed in the constructed environment (test environment) and then the second transaction constructed by the adversary is executed, the assets of the honest user (the second asset) are reduced compared to the assets in the normal state (the first asset), indicating that the honest user's funds are damaged and the smart contract has a profit loophole.
[0115] In a specific scenario, the execution effect of the constructed second transaction requires the auxiliary execution of another generated transaction, namely, the third transaction. That is, the generated third transaction is first executed to obtain the first intermediate state, and the third transaction is also initiated by the second user. Then, the first transaction is simulated and executed based on the first intermediate state to obtain the second intermediate state. Then, the second transaction is executed based on the second intermediate state to obtain the second asset state.
[0116] Specifically, the first transaction is still represented as tx, and its real historical execution track in the blockchain is tx = (user, S pre ,S post ). Assume that the adversary initiates the third transaction tx0. After executing the third transaction in the blockchain state S0, the first intermediate state S1 is generated. The execution of the third transaction can be expressed as tx0=(adversary, S0, S1). Then, based on the blockchain state S1, the first transaction initiated by the user is simulated and executed to obtain a new blockchain state S2, that is, the second intermediate state. The simulated execution of the first transaction can be expressed as tx=(user, S1, S2). Next, based on the blockchain state S2, the second transaction tx1 initiated by the adversary is executed again to generate a new blockchain state S3. The execution of this transaction is expressed as tx1=(adversary, S2, S3). In other words, the above three transactions constitute a complete transaction context: tx0→tx→tx1. It is assumed that the user executes the first transaction on the intermediate state constructed by the adversary.
[0117] It is understandable that under normal circumstances, the assets owned by the user after the first transaction is executed are expressed as V(user,S post), which is the normal assets owned by the user in the historical transactions. The assets owned by the user after the adversary executes the generated transaction are denoted as V(user,S3), which is the assets owned by the user in the transaction context constructed by the adversary. post )>V(user,S3), it means that the adversary constructs an execution environment of an intermediate state by executing the constructed transaction. The user will obtain less asset income than normal transactions by executing historical transactions in the constructed state. That is, the adversary causes other users to suffer asset losses by implementing additional smart contract transaction calls. Therefore, it can be determined that there is a profit vulnerability in the smart contract.
[0118] Through the above difference analysis method, the execution status of a large number of seed transactions in the seed pool can be analyzed. Specifically, the asset status of the executed historical transactions and the generated transactions can be compared. According to the above criteria, the profit loopholes of the smart contract can be identified.
[0119] It should be noted that in the process of profit-taking vulnerability detection, the identity of the transaction initiator (adversary or user) is often difficult to determine. In some scenarios, from the perspective of the adversary, the change in the seized assets is a positive gain, while from the perspective of the user, it is a negative gain. Therefore, the identification of profit-taking vulnerabilities is only related to the relative change relationship of assets in a specific scenario, and has nothing to do with the positive or negative direction of the asset change.
[0120] In this specification, the word "first" in terms such as the first transaction and the first intermediate state, as well as the corresponding "second", "third", "fourth" (if any), etc. in the text are merely for the convenience of distinction and description and do not have any limiting meaning.
[0121] The foregoing describes certain embodiments of the present specification, and other embodiments are within the scope of the appended claims. In some cases, the actions or steps described in the claims may be performed in an order different from that in the embodiments, and the desired results may still be achieved. In addition, the processes depicted in the accompanying drawings do not necessarily have to be performed in the specific order or sequential order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0122] Figure 6 Schematic diagram of a vulnerability detection device for smart contracts on a blockchain according to an embodiment of this specification. The system 600 is deployed in a computing device, which can be implemented by any device, equipment, platform, device cluster, etc. with computing and processing capabilities. Figure 3 The system 600 includes:
[0123] The collection module 601 is configured to collect multiple historical transactions of calling several smart contracts on the blockchain to form an initial seed pool.
[0124] The seed mutation module 602 is configured to perform a mutation operation based on the seed transactions in the seed pool to obtain generated transactions.
[0125] The seed evaluation module 603 is configured to perform a checkpoint evaluation by executing the generated transaction, and add the generated transaction that passes the evaluation as a seed transaction to the seed pool. The checkpoint evaluation is used to eliminate invalid transactions and / or identify transactions related to the circulation of asset tokens.
[0126] The identification module 604 is configured to identify profit vulnerabilities of the plurality of smart contracts based on a first asset state after a first transaction is executed in the seed pool, and a second asset state after a second transaction is executed, wherein the first transaction is a historical transaction and the second transaction is a generated transaction related to the first transaction.
[0127] According to another embodiment of the present specification, there is also provided a computer program product, including a computer program / instruction, which implements the aforementioned combination when executed by a processor. Figure 3 The steps of the method.
[0128] According to another embodiment, the present specification further provides a computing device, including a memory and a processor, wherein the memory stores executable code, and when the processor executes the executable code, the aforementioned combination is implemented. Figure 3 The steps of the method.
[0129] Those skilled in the art should be aware that in one or more of the above examples, the functions described in the embodiments of the present invention may be implemented using hardware, software, firmware, or any combination thereof. When implemented using software, these functions may be stored in a computer-readable medium or transmitted as one or more instructions or codes on a computer-readable medium.
[0130] The specific implementation methods described above further describe the purpose, technical solutions and beneficial effects of the embodiments of the present invention in detail. It should be understood that the above description is only a specific implementation method of the embodiments of the present invention and is not intended to limit the scope of protection of the present invention. Any modification, equivalent replacement, improvement, etc. made on the basis of the technical solution of the present invention shall be included in the scope of protection of the present invention.
Claims
1. A vulnerability detection method for smart contracts on a blockchain, comprising: Collect multiple historical transactions of calling several smart contracts on the blockchain to form an initial seed pool; Perform a mutation operation based on the seed transaction in the seed pool to obtain a generated transaction; Performing a checkpoint evaluation by executing the generated transaction, adding the generated transaction that passes the evaluation as a seed transaction to the seed pool, wherein the checkpoint evaluation is used to eliminate invalid transactions and / or identify transactions related to the circulation of asset tokens; According to a first asset state after a first transaction is executed in the seed pool, and a second asset state after a second transaction is executed, profit vulnerabilities of the plurality of smart contracts are identified, wherein the first transaction is a historical transaction, and the second transaction is a generated transaction related to the first transaction.
2. The method according to claim 1, wherein: The step of performing a checkpoint evaluation by executing the generated transaction comprises: Extracting first track information from the execution track of executing the generated transaction, wherein path jump related information is recorded; Determining, according to the first trace information, whether the generated transaction generates a new test path and whether it is rolled back by the program; If it generates a new test path and is not rolled back, it is determined that the generated transaction passes the evaluation.
3. The method according to claim 2, wherein: The first trace information includes a first count value of a program counter corresponding to executing a jump-related instruction and a first jump destination; Determining whether the generated transaction generates a new test path includes: If the first count value is not covered in the global count storage set, determining to generate a new test path, and adding the first count value to the global count storage set; If the global count storage set has covered the first count value, and the global destination storage set has not covered the first destination, it is determined to generate a new test path, and the first destination is added to the global destination storage set.
4. The method according to claim 1, wherein: The step of performing a checkpoint evaluation by executing the generated transaction comprises: Extracting second track information from the execution track of executing the generated transaction, wherein the function call information related to the token circulation is recorded; Determining whether the generated transaction generates new asset token circulation information according to the second track information; If it generates new asset token transfer information, it is determined that the generated transaction has passed the evaluation.
5. The method according to claim 4, wherein: Extracting second track information from the execution track of executing the generated transaction includes: Extract the function call instruction from the execution trace, and determine that the called function is the target instruction of the target function related to the token transfer and its target location; The first token transfer information is read from the target location in the storage space, including the starting point, target, and quantity of this token transfer.
6. The method according to claim 5, wherein: The operation codes of the function call instruction include CALL and DELEGATECALL; the target function includes transfer and transferFrom.
7. The method according to claim 5, wherein: Determining whether the generated transaction generates new asset token circulation information includes: If the storage information at the target location in the global token circulation storage set does not cover the first token circulation information, it is determined to generate new asset token circulation information, and the first token circulation information is added to the global token circulation storage set.
8. The method according to claim 1, wherein: The first transaction is initiated by a first user, the second transaction is initiated by a second user, the first transaction and the second transaction are executed based on the same blockchain state, and the identifying of the profit vulnerabilities of the plurality of smart contracts includes: If the assets of the second user in the second asset state are greater than the assets of the first user in the first asset state, it is determined that the plurality of smart contracts have profit loopholes.
9. The method according to claim 1, wherein: The first transaction is initiated by a first user and executed based on a first historical state; the second transaction is initiated by a second user and executed after simulating the execution of the first transaction in a test environment, and the identifying of the profit vulnerabilities of the plurality of smart contracts includes: If the assets of the first user in the second asset state are less than the assets of the first user in the first asset state, it is determined that the plurality of smart contracts have profit loopholes.
10. The method according to claim 9, wherein: The second asset status is obtained by: executing the generated third transaction to obtain a first intermediate state, where the third transaction is initiated by the second user; Simulate and execute the first transaction based on the first intermediate state to obtain a second intermediate state; The second transaction is executed based on the second intermediate state to obtain the second asset state.
11. The method according to claim 1, wherein: The mutation operation based on the seed transaction in the seed pool includes: Mutate the parameters contained in any single seed transaction; or, mutate the combination of two seed transactions.
12. The method according to claim 1, wherein: The collecting of multiple historical transactions of calling multiple smart contracts on the blockchain also includes: For any target historical transaction, the block number and block timestamp of the block where the transaction is located are obtained, and they are stored in the seed pool corresponding to the target historical transaction for re-execution of the transaction.
13. The method according to claim 1, wherein: The multiple historical transactions do not include invalid transactions, and the invalid transactions are transactions that trigger a revert rollback operation.
14. A vulnerability detection device for smart contracts on a blockchain, comprising: A collection module, configured to collect multiple historical transactions of calling a plurality of smart contracts on the blockchain to form an initial seed pool; A seed mutation module, configured to perform a mutation operation based on the seed transactions in the seed pool to obtain a generated transaction; A seed evaluation module, configured to perform a checkpoint evaluation by executing the generated transaction, and add the generated transaction that passes the evaluation to the seed pool as a seed transaction, wherein the checkpoint evaluation is used to eliminate invalid transactions and / or identify transactions related to the circulation of asset tokens; The identification module is configured to identify profit vulnerabilities of the plurality of smart contracts based on a first asset state after a first transaction is executed in the seed pool, and a second asset state after a second transaction is executed, wherein the first transaction is a historical transaction and the second transaction is a generated transaction related to the first transaction.
15. A computer program product, comprising a computer program / instruction, which, when executed by a processor, implements the steps of the method according to any one of claims 1 to 13.
16. A computing device comprising a memory and a processor, characterized in that: The memory stores executable codes, and when the processor executes the executable codes, the method according to any one of claims 1 to 13 is implemented.
Citation Information
Patent Citations
Intelligent contract transaction sequence dependent vulnerability variation fuzzy test method
CN111563742A
Intelligent contract fuzzy test method and device and storage medium
CN112131115A
Block chain transaction processing method and system
CN115021945A
Vulnerability detection method and device for smart contract
CN116644435A
Method and device for performing vulnerability detection on target application
CN117113360A
