Smart contract vulnerability detection methods, devices, equipment, and storage media

By obtaining formal deployment information of smart contracts through formal verification methods, executing functions and detecting results, the problem of low accuracy in existing smart contract vulnerability detection is solved, and high-precision vulnerability detection is achieved.

CN115062313BActive Publication Date: 2025-12-02THE PEOPLES BANK OF CHINA DIGITAL CURRENCY INST
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202210736727.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-06-27
Publication Date
2025-12-02
Estimated Expiration
2042-06-27

AI Technical Summary

Technical Problem

Existing smart contract vulnerability detection methods have low detection accuracy and cannot effectively detect known and unknown vulnerabilities.

Method used

Formal verification methods are employed to obtain formal deployment information of smart contracts, execute functions in the formal behavior list, and use a formal specification library to detect execution results. A directed graph and execution queue are established to ensure comprehensive coverage of transaction scenarios and improve detection accuracy.

Benefits of technology

It achieves high-precision vulnerability detection of smart contracts during actual operation, and can detect both known and unknown vulnerability types, thus improving the comprehensiveness and accuracy of detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115062313B_ABST
    Figure CN115062313B_ABST
Patent Text Reader

Abstract

This application discloses a method, apparatus, device, and storage medium for detecting smart contract vulnerabilities, relating to the blockchain field. The method includes: acquiring formal deployment information of the smart contract; wherein the formal deployment information includes a formal initial state and a list of formal behaviors; the list of formal behaviors includes the smart contract's initialization execution function and behavioral functions; based on the formal initial state, executing the initialization execution function and behavioral functions included in the list of formal behaviors to obtain the execution result of the smart contract; and detecting the execution result of the smart contract according to a formal specification library of the smart contract, to obtain a vulnerability detection result, wherein the formal specification library includes the formal specifications of the smart contract. This solves the problem of low detection accuracy in existing smart contract vulnerability detection methods.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of blockchain technology, and in particular to methods, apparatus, devices and storage media for detecting smart contract vulnerabilities. Background Technology

[0002] Blockchain is a novel application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanisms, and cryptographic algorithms. It uses a decentralized consensus mechanism to maintain a complete, distributed, and immutable ledger database, enabling participants to achieve a unified ledger system without establishing trust relationships. In blockchain, smart contracts are written digitally. A smart contract is a set of commitments defined in digital form, including protocols that allow contract participants to execute these commitments. The combination of smart contracts and the characteristics of blockchain technology ensures that the entire process of storage, retrieval, and execution is transparent, traceable, and immutable. Furthermore, a state machine system built from the blockchain's built-in consensus algorithm enables smart contracts to operate efficiently.

[0003] Due to the immutable nature of blockchain, vulnerabilities in smart contracts caused by design flaws in the program itself could lead to irreversible and significant losses. Therefore, smart contract vulnerability detection is crucial. Currently, the main method for smart contract vulnerability detection involves incorporating the inherent rules of known vulnerabilities into the software and using these rules for detection. This method can only detect discovered vulnerabilities and has relatively low accuracy. Summary of the Invention

[0004] This application provides a method, apparatus, device, and storage medium for detecting smart contract vulnerabilities, which solves the problem of low detection accuracy in existing smart contract vulnerability detection methods.

[0005] To achieve the above technical objectives, the embodiments of this application adopt the following technical solutions:

[0006] In a first aspect, embodiments of this application provide a smart contract vulnerability detection method. The method is applied to a virtual machine server and includes: obtaining formal deployment information of a smart contract; wherein the formal deployment information includes a formal initial state and a formal behavior list; the formal behavior list includes the smart contract's initialization execution function and behavior function; based on the formal initial state, executing the initialization execution function and behavior function included in the formal behavior list to obtain the execution result of the smart contract; and detecting the execution result of the smart contract according to a formal specification library of the smart contract to obtain a vulnerability detection result; the formal specification library includes the formal specification of the smart contract.

[0007] In this embodiment, formal deployment information of the smart contract is first obtained. Then, based on the formal initial state included in the formal deployment information, the initialization execution function and behavior function included in the formal behavior list are executed to obtain the execution result of the smart contract. Finally, the execution result of the smart contract is detected according to the formal specification library of the smart contract to obtain the vulnerability detection result. This vulnerability detection result can characterize whether the smart contract has vulnerabilities during actual operation. Both known and unknown vulnerability types can be detected, demonstrating high vulnerability detection accuracy.

[0008] In one embodiment, the execution result of the smart contract is detected according to the formal specification library of the smart contract to obtain the vulnerability detection result, including: for each behavior function, detecting whether the execution result of the initialization execution function and the behavior function based on the formal initial state conforms to the formal specification;

[0009] If the conditions are met, the execution result will be associated with the formal initial state, and if the execution result is not included in the formal deployment information, the execution result will be included as a new formal initial state in the formal deployment information; if the conditions are not met, an error message will be reported.

[0010] In one embodiment, the method further includes: establishing a directed graph; the directed graph includes nodes corresponding to a formalized initial state, and two nodes that are associated are connected by directed edges;

[0011] If the conditions are met, the execution result will be used as the basis for establishing a relationship with the formal initial state. This includes: if the conditions are met, checking whether there is a node in the directed graph that corresponds to the execution result; if it exists, setting a directed edge from the initial state to the node; if it does not exist, adding the execution result to the directed graph and setting a directed edge from the initial state to the node corresponding to the execution result.

[0012] In one embodiment, the formal deployment information includes multiple formal initial states; the method further includes: determining an execution queue based on the multiple formal initial states; wherein the execution queue includes formal initial states that conform to the formal specification among the multiple formal initial states; and executing initialization execution functions and behavior functions included in the formal behavior list based on the formal initial states, including: executing the initialization execution functions and behavior functions included in the formal behavior list based on each formal initial state in the execution queue.

[0013] In one embodiment, the method further includes: determining a customized formal specification for the smart contract based on the smart contract's requirements information; and adding the customized formal specification to a formal specification library.

[0014] Secondly, embodiments of this application provide a smart contract vulnerability detection device. The device is applied to a virtual machine server and includes: an acquisition module for acquiring formal deployment information of a smart contract; wherein the formal deployment information includes a formal initial state and a formal behavior list; the formal behavior list includes the smart contract's initialization execution function and behavior functions;

[0015] The execution module is used to execute the initialization execution functions and behavior functions included in the formal behavior list based on the formal initial state, and obtain the execution result of the smart contract;

[0016] The detection module is used to detect the execution results of smart contracts based on the formal specification library of smart contracts, and obtain the vulnerability detection results; the formal specification library includes the formal specifications of smart contracts.

[0017] In one embodiment, the detection module is specifically configured to: for each behavioral function, detect whether the execution results of the initialization execution function and the behavioral function based on the formal initial state conform to the formal specification; if they conform, establish an association between the execution results and the formal initial state, and if the execution results are not included in the formal deployment information, include the execution results as a new formal initial state in the formal deployment information; if they do not conform, report an error message.

[0018] In one embodiment, the apparatus further includes a creation module; the creation module is used to create a directed graph; the directed graph includes nodes corresponding to a formalized initial state, and two nodes with an association relationship are connected by a directed edge; the detection module is specifically used to: if the condition is met, check whether there is a node in the directed graph corresponding to the execution result; if it exists, set a directed edge from the initial state to the node; if it does not exist, add the execution result to the directed graph, and set a directed edge from the initial state to the node corresponding to the execution result.

[0019] In one embodiment, the formal deployment information includes multiple formal initial states; the apparatus further includes a determination module; the determination module is configured to determine an execution queue based on the multiple formal initial states; wherein the execution queue includes formal initial states that conform to the formal specification among the multiple formal initial states;

[0020] The execution module is specifically used to: execute the initialization execution functions and behavior functions included in the formal behavior list based on each formal initial state in the execution queue.

[0021] In one embodiment, the apparatus further includes a determining module and an adding module; the determining module is used to determine a customized formal specification for the smart contract based on the smart contract's requirement information; the adding module is used to add the customized formal specification to a formal specification library.

[0022] Thirdly, this application provides an electronic device, including: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program, when executed by the processor, implements the smart contract vulnerability detection method of the first aspect.

[0023] Fourthly, this application provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the smart contract vulnerability detection method of the first aspect.

[0024] Fifthly, this application provides a computer program product that, when run on a computer, causes the computer to perform the method provided in the first aspect above.

[0025] The beneficial effects described in the second, third, fourth and fifth aspects of this application can be referred to the analysis of the beneficial effects in the first aspect, and will not be repeated here. Attached Figure Description

[0026] Figure 1 This is a schematic diagram illustrating an application scenario of a smart contract vulnerability detection method provided in an embodiment of this application.

[0027] Figure 2 A flowchart illustrating a smart contract vulnerability detection method provided in this application embodiment;

[0028] Figure 3 A schematic diagram of a formalized list of behaviors provided for an embodiment of this application;

[0029] Figure 4 A flowchart illustrating another smart contract vulnerability detection method provided in this application embodiment;

[0030] Figure 5 A schematic diagram of a directed graph provided in an embodiment of this application;

[0031] Figure 6 This is another schematic diagram of a directed graph provided in an embodiment of this application;

[0032] Figure 7 A flowchart illustrating another smart contract vulnerability detection method provided in this application embodiment;

[0033] Figure 8 A flowchart illustrating another smart contract vulnerability detection method provided in this application embodiment;

[0034] Figure 9 This is a schematic diagram of the structure of a smart contract vulnerability detection device provided in an embodiment of this application;

[0035] Figure 10This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation

[0036] The features and exemplary embodiments of various aspects of this application will now be described in detail. To make the objectives, technical solutions, and advantages of this application clearer, the application will be further described in detail below with reference to the accompanying drawings and specific embodiments. It should be understood that the specific embodiments described herein are only configured to explain this application and are not configured to limit this application. For those skilled in the art, this application can be implemented without some of these specific details. The following description of the embodiments is merely to provide a better understanding of this application by illustrating examples of this application.

[0037] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations; nor should they be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Thus, features defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of this embodiment, unless otherwise stated, "a plurality of" means two or more. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0038] Currently, there are two main methods for detecting smart contract vulnerabilities. One method involves extracting the inherent operational rules of existing vulnerabilities and incorporating them into software for vulnerability detection. The other method is manual detection, where individuals review the smart contract logic to determine the presence of vulnerabilities. However, the first method can only detect already discovered vulnerabilities, resulting in limited detection capabilities and low accuracy. The second method is inefficient, and its accuracy is limited by the skills of the personnel conducting the detection.

[0039] In summary, existing smart contract vulnerability detection methods have low accuracy.

[0040] To address the aforementioned technical issues, this application embodiment considers that when smart contracts run in a virtual machine, their execution logic may change according to the virtual machine's own rules, leading to unexpected vulnerabilities. Therefore, it directly applies a virtual machine (or virtual machine server) for smart contract vulnerability detection. Accordingly, a smart contract vulnerability detection method is provided. This method includes: the virtual machine first obtains the formal deployment information of the smart contract; then, based on the formal initial state included in the formal deployment information, it executes the initialization execution function and behavior function included in the formal behavior list to obtain the execution result of the smart contract; finally, it detects the execution result of the smart contract according to the formal specification library of the smart contract to obtain the vulnerability detection result. This vulnerability detection result can characterize whether the smart contract has vulnerabilities during actual operation, detecting both known and unknown vulnerability types, resulting in high vulnerability detection accuracy. Furthermore, for each formal initial state of the smart contract, an initialization execution function is executed to verify whether the execution results of all cases are compliant (e.g., whether they conform to formal specifications), making the smart contract vulnerability detection result more comprehensive and further improving detection accuracy.

[0041] The smart contract vulnerability detection method described in this application applies formal verification methods to smart contract vulnerability detection. Formal verification methods use mathematical formulas, theorems, and systems to verify the functional correctness and security attributes of a system. Compared with traditional detection methods, formal verification methods can discover inconsistencies between the target and the system description. Therefore, using formal verification methods to verify smart contracts can effectively test the security of smart contracts and detect whether vulnerabilities exist.

[0042] The execution entity of the smart contract vulnerability detection method provided in this application can be an electronic device with data processing capabilities, such as a computer or server that has deployed a virtual machine program. The server can be a single server or a server cluster consisting of multiple servers. In some embodiments, the server cluster can also be a distributed cluster. This application does not limit the execution entity of this smart contract vulnerability detection method. The following description uses a server with a virtual machine deployed as the execution entity.

[0043] Reference Figure 1 This is a schematic diagram illustrating an application scenario of a smart contract vulnerability detection method provided in an embodiment of this application. Figure 1As shown, the execution entity of the smart contract vulnerability detection method provided in this application embodiment can be a server deployed with multiple virtual machines (or a virtual machine cluster), where the virtual machine cluster includes at least one virtual machine. This virtual machine can obtain the source code of the smart contract (or the smart contract itself or smart contract code). For example, the source code of the smart contract may include functions (such as a transfer function) for implementing transactions between account A and account B, as well as the agreed-upon transaction rules between account A and account B. The virtual machine can obtain the formal deployment information of the smart contract, and then, based on the formal initial state included in the formal deployment information, execute the initialization execution functions and behavior functions included in the formal behavior list to obtain the execution result of the smart contract. Then, it detects the execution result of the smart contract according to the formal specification library of the smart contract to obtain the vulnerability detection result. This vulnerability detection result can characterize whether the smart contract has vulnerabilities during actual operation; it can detect both known and unknown vulnerability types, resulting in high vulnerability detection accuracy for smart contracts.

[0044] In this application, the execution entity of the smart contract vulnerability detection method is preferably a virtual machine server. Running smart contracts directly on a virtual machine server eliminates the need for logical conversion using proprietary languages, thus lowering the barrier to entry for smart contract vulnerability detection.

[0045] Figure 2 A flowchart illustrating a smart contract vulnerability detection method according to an embodiment of this application is shown. Figure 2 As shown, this method is executed by a virtual machine (or virtual machine server), and the method may include the following steps:

[0046] S210, obtain formal deployment information of smart contracts.

[0047] The formal deployment information includes a formal initial state and a list of formal behaviors. The list of formal behaviors includes the smart contract's initialization execution function and behavioral functions. There is only one initialization execution function, which is used to initialize the virtual machine for deploying the smart contract based on the formal initial state. There can be multiple behavioral functions, which execute various functions of the smart contract in the initialized virtual machine.

[0048] In this embodiment, formal deployment information can be obtained by parsing the code of a smart contract. For example, the code of a smart contract can be input into a smart contract parser, which parses the smart contract to obtain a formal initial state and a list of formal behaviors. Specifically, the smart contract parser can determine the list of functions that can be called externally and their corresponding parameter lists based on the function definitions of the smart contract, thus obtaining the formal initial state and the list of formal behaviors. The initialization execution functions and behavior functions in the formal behavior list are used to deploy the smart contract in the virtual machine, thereby obtaining all the formal deployment information. The list of formal behaviors can be as follows: Figure 3 As shown.

[0049] For example, the formal initial state could include: Account A has 100 yuan, and Account B has 0 yuan. The formal behavior list could include an initialization execution function: a payment function. This payment function takes two parameters: the destination account and the transfer amount. Furthermore, the execution rule for this payment function is that the transfer amount must be an integer, and the transfer amount ranges from 0 to 255. For instance, the formal behavior list could include the following initialization execution functions: A transfers 1 yuan to B, A transfers 100 yuan to B, A transfers 255 yuan to B, etc.

[0050] In one possible approach, the virtual machine can obtain the smart contract from the electronic device storing the smart contract via communication or physical connection. Then, it can parse the smart contract using a smart contract parser to obtain formal deployment information. In another possible approach, the electronic device deploying the smart contract can parse the smart contract using a smart contract parser to obtain a formal initial state and a list of formal behaviors. The virtual machine can then obtain the formal initial state and the list of formal behaviors from this electronic device and use this information as formal deployment information, deploying it in a local data repository. This allows for the retrieval of the smart contract's formal deployment information locally, thus reducing the power consumption and processing complexity of the virtual machine parsing the smart contract.

[0051] It should be noted that in this embodiment, the formal initial state can be one or more, and is not limited thereto. Formal deployment information can include all formal initial states obtained by parsing the smart contract, or it can include a portion of the formal initial states obtained by parsing the smart contract, such as formal initial states that conform to formal specifications, i.e., screening out compliant formal initial states to execute the method described in this application. Specifically, the process of screening out compliant formal initial states can be referred to the following... Figure 8 As described in the corresponding embodiments.

[0052] S220, based on the formal initial state, executes the initialization execution function and behavior function included in the formal behavior list to obtain the execution result of the smart contract.

[0053] The execution result of the smart contract includes the execution result obtained from executing the initialization execution function. Referring to the example in step S210, the target account in the initialization execution function...

[0054] For example, the execution result of a smart contract could be: Account A has 90 yuan and Account B has 10 yuan.

[0055] Furthermore, when executing the initialization execution function and behavior function included in the formal behavior list based on the formal initial state, the initialization execution function is first executed based on the formal initial state to initialize the virtual machine, and then the behavior function is executed in the initialized virtual machine to obtain the execution result of the smart contract.

[0056] S230: Based on the formal specification library of smart contracts, the execution results of the smart contract are tested to obtain the vulnerability detection results.

[0057] The formal specification library includes formal specifications for smart contracts. These specifications describe the requirements that the state of a smart contract should meet, including general specifications and custom formal specifications. General specifications are requirements that all smart contracts must conform to, such as ensuring that the sum of the balances of all accounts remains constant during smart contract execution, that account balances cannot be negative, and defining the range of integer values. Custom formal specifications are the rules that the currently validated smart contract must follow, such as defining the range of integer values ​​that the current smart contract should fall within. The implementation of the formal specification library can be found below. Figure 7 As described in the corresponding embodiments.

[0058] In this embodiment of the application, the custom formal specification has a higher priority than the general specification. For the same constraint, the custom formal specification shall prevail. For example, if the custom formal specification constrains the range of integer values ​​to be 0 to 255, while the general specification determines the range of integer values ​​to be 0 to 1023, then the 0 to 255 range of the custom formal specification shall prevail in the formal verification process.

[0059] In this embodiment, formal deployment information of the smart contract is first obtained. Then, based on the formal initial state included in the formal deployment information, the initialization execution function and behavior function included in the formal behavior list are executed to obtain the execution result of the smart contract. Finally, the execution result of the smart contract is detected according to the formal specification library of the smart contract to obtain the vulnerability detection result. This vulnerability detection result can characterize whether the smart contract has vulnerabilities during actual operation. Both known and unknown vulnerability types can be detected, demonstrating high vulnerability detection accuracy.

[0060] In one embodiment, such as Figure 4 As shown, in S230, the execution result of the smart contract is checked according to the formal specification library of the smart contract, and the vulnerability detection result can include the following S2301-S2303:

[0061] S2301: For each behavioral function, check whether the execution results of the initialization execution function and the behavioral function, based on the formal initial state, conform to the formal specification. If they conform, proceed to S2302; otherwise, proceed to S2303.

[0062] In this process, the execution result can be determined to be in accordance with the formal specification by comparing the formal specification with the execution result.

[0063] For example, the formal initial state could include: Account A has 100 yuan, and Account B has 0 yuan. The list of formal behaviors could include an initialization execution function: a payment function. This payment function has two parameters: the destination account and the transfer amount. The execution rule for this payment function is that the transfer amount must be an integer, and the transfer amount ranges from 0 to 255. The formal specification is: if the total account balance remains unchanged (i.e., the total amount is 100), and the account balance cannot be negative, then the formal specification is expressed as A + B = 100 ∩ A ≥ 0 ∩ B ≥ 0.

[0064] When executing the transfer function, since the transfer amount ranges from 0 to 255, the functions will sequentially execute: A transfers 0 yuan to B, A transfers 1 yuan to B, ..., A transfers 255 yuan to B. For each execution result of the transfer function, it is determined whether the result conforms to the formal specifications.

[0065] For example, if the execution result is that account A has 10 yuan and account B has 100 yuan, and A+B=110, which is greater than 100, then the execution result does not conform to the formal specifications.

[0066] S2302 establishes a relationship between the execution result and the formal initial state, and includes the execution result as a new formal initial state in the formal deployment information if the execution result is not included in the formal deployment information.

[0067] In this embodiment, an association is established between the execution result and the formal initial state, which can be used to trace the correspondence between the function execution result and the formal initial state. When the execution result is not included in the formal deployment information, it is included as a new formal initial state in the formal deployment information. This new formal initial state can then be deployed to the virtual machine for formal specification verification. This ensures that the formal initial state can fully cover various transaction scenarios during smart contract execution, thereby ensuring that no smart contract detection is missed and improving the accuracy of smart contract detection.

[0068] Furthermore, to more intuitively illustrate the correspondence between the execution result of the tracing function and the formal initial state, this application can also illustrate the relationship between the execution result and the formal initial state using a directed graph. Specifically, the process illustrated using a directed graph is as follows:

[0069] Before executing S230 or S210, establish a directed graph.

[0070] Here, establishing a directed graph can be replaced by describing the formal initial state of the graph based on the formal deployment information of smart contracts; the directed graph includes nodes corresponding to the formal initial state, and two nodes with a relationship are connected by directed edges.

[0071] Furthermore, in S2302, the execution result is used as an association with the formalized initial state, which may include: if the conditions are met, checking whether there is a node in the directed graph corresponding to the execution result; if it exists, setting a directed edge from the initial state to the node; if it does not exist, adding the execution result to the directed graph and setting a directed edge from the initial state to the node corresponding to the execution result.

[0072] For example, Figure 5 For an initial directed graph, the directed graph can be represented as follows: Figure 5 As shown, there are three formalized initial states: A, B, and C. A and B are related, A and C are related, and B and C are not related.

[0073] Reference Figure 5 The directed graph shown has an execution result D that is related to C added to it. The resulting directed graph can be seen as follows: Figure 6 As shown.

[0074] In this embodiment of the application, a directed graph is used to represent the relationship between the formal initial state and the execution result, which makes it easier to intuitively show the correspondence between the execution result and the formal initial state.

[0075] S2303, report error message.

[0076] In this embodiment of the application, if the execution result of the initialization execution function does not conform to the formal specifications, an error message is reported, which can promptly warn of problems existing in the smart contract.

[0077] based on Figure 4The method shown can verify the execution results of each formal initial state under each initialization execution function, establish a correlation between compliant execution results and formal initial states, and report non-compliant execution results, so as to trace the correspondence between compliant function execution results and formal initial states, and promptly alert to non-compliant function execution results in smart contracts.

[0078] In one embodiment, to meet users' customization needs, a custom formal specification can be used to verify smart contracts, such as... Figure 7 As shown, before executing S230, the method may further include:

[0079] S240 determines the customized formal specifications of the smart contract based on the smart contract's requirements information.

[0080] Different smart contracts have different requirements, and the customized formal specifications of the smart contract can be determined based on these requirements. For example, if a smart contract stipulates the transaction rules for account A and account B, and the requirement of the smart contract is that the total amount between the accounts does not exceed 3000, then the corresponding customized formal specification could be: A + B < 3000.

[0081] Since formal verification involves verifying all possible cases, in order to reduce the time spent on formal verification, a customized formal specification can be used to replace the general formal specification in actual operation, thereby reducing the number of possible cases.

[0082] S250 adds the customized formal specification to the formal specification library so that when S230 is executed, the customized formal specification is retrieved from the formal specification library, and the execution result of the smart contract is detected based on the customized formal specification of the smart contract to obtain the vulnerability detection result.

[0083] A formal specification library is a type of database used to store formal specifications. In this embodiment, customized formal specifications are added to the formal specification library, making it easier for applications to directly call these specifications. Furthermore, the customized formal specifications in the library can meet users' customization needs.

[0084] In one embodiment, to improve the execution efficiency of the method shown in this application, before executing S220, it is also necessary to screen the initialization formal state in the formal deployment information of the smart contract to identify valid (or compliant) formal initial states, thus avoiding the problem of low execution efficiency caused by processing invalid formal initial states. Specifically, the method is as follows: Figure 8 As shown, the formal deployment information includes multiple formal initial states; the method may also include:

[0085] S260, determine the execution queue based on multiple formal initial states; wherein the execution queue includes formal initial states that conform to the formal specifications among the multiple formal initial states.

[0086] There can be multiple formal initial states. The deployment order of formal initial states in the virtual machine can be determined by the execution queue to avoid missing any formal initial states.

[0087] Furthermore, S220: Based on the formal initial state, executing the initialization execution function and behavior function included in the formal behavior list may include: based on each formal initial state in the execution queue, executing the initialization execution function and behavior function included in the formal behavior list.

[0088] In this embodiment, each formal initial state in the execution queue is traversed, and the corresponding initialization execution function and behavior function included in the formal behavior list are executed. This ensures that the formal initial states can completely cover various transaction scenarios during smart contract execution, thereby ensuring no omissions during smart contract detection and improving the accuracy of smart contract detection. Furthermore, all formal initial states in the execution queue are formal initial states that conform to the formal specification. Only valid formal initial states are executed with initialization execution functions, avoiding the problem of low execution efficiency caused by processing invalid formal initial states.

[0089] In one embodiment, in order to ensure that no possible transaction scenarios are overlooked and to guarantee a comprehensive check of the smart contract, the method may further include: adding the execution result as a new formal initial state to the execution queue if there is no formal initial state in the execution queue that is identical to the execution result.

[0090] In this embodiment, if no formal initial state identical to the execution result exists in the execution queue, the execution result is added to the execution queue as a new formal initial state. This new formal initial state can then be deployed to the virtual machine, and the execution result obtained after executing the initialization function based on the new formal initial state is subjected to formal specification verification. This ensures that the formal initial state fully covers all transaction scenarios during smart contract execution, thereby ensuring no omissions in smart contract detection and improving the accuracy of smart contract detection.

[0091] The above method will be explained in detail below with specific examples:

[0092] First, create an execution queue Q and a directed graph G inside the virtual machine. The execution queue Q can be found in the description of the execution queue in step S260, and the directed graph G can be found in... Figure 5And the directed graph description in step S2302. The above method can be executed based on the execution queue Q and the directed graph G, specifically including the following steps:

[0093] ① Obtain the formal deployment information of the smart contract, and check whether the formal initialization state meets the formal specifications using a formal inspection tool. If it does not meet the specifications, report an error directly; if it does meet the specifications, proceed to step ②. That is, step ① screens the formal initial states included in the formal deployment information to obtain compliant formal initial states, and places the compliant formal initial states into the execution queue Q.

[0094] ② Place the formally initialized state that satisfies the formal specification into the execution queue Q, and simultaneously add the formally initialized state as a node to the directed graph G. There can be one or more formally initialized states.

[0095] ③ When the execution queue Q is not empty, execute steps 1) to 4) below:

[0096] 1) Pop the first formal initialization state s from the execution queue Q, and use s to execute the initialization execution function to implement the virtual machine initialization.

[0097] 2) Execute all the behavior functions in the formal behavior list and put the result of each behavior function into the Set collection.

[0098] 3) If the Set is not empty: For each element t in the Set, perform the following operation, and after performing the following operation, remove element t from the Set:

[0099] Check if t conforms to the formal specifications; if not, report an error.

[0100] If the conditions are met, check if t already exists in the directed graph G. If it exists, add a directed edge from s to t. If it does not exist, add t to the execution queue Q and add it to the directed graph, while adding a directed edge from s to t in the directed graph.

[0101] 4) If Set is empty, continue processing the next formal initial state in the execution queue Q. Repeat steps 1) to 4) above. When all possible cases (i.e., all formal initial states in the execution queue) have been executed, the execution queue Q is empty, and the process ends.

[0102] In this embodiment, the formal initial states in the execution queue are traversed to ensure that each formal initial state can be executed for vulnerability detection. If the execution result is not included in the formal deployment information, the execution result is included as a new formal initial state in the formal deployment information. This new formal initial state can then be deployed to the virtual machine for formal specification verification. This ensures that the formal initial states completely cover all transaction scenarios during smart contract execution, thereby ensuring no smart contract detection is missed and improving the accuracy of smart contract detection.

[0103] Figures 1-8 The method for detecting smart contract vulnerabilities is described below. Figure 9 and Figure 10 This application describes the apparatus provided in its embodiments. To achieve the above-described functions, the smart contract vulnerability detection apparatus includes hardware structures and / or software modules corresponding to the execution of each function. Those skilled in the art will readily recognize that, based on the algorithmic steps of the examples described in conjunction with the embodiments disclosed herein, this application can be implemented in hardware or a combination of hardware and computer software. Whether a function is executed in hardware or by computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0104] This application embodiment can, exemplarily, divide a smart contract vulnerability detection device into functional modules based on the above method. The smart contract vulnerability detection device can be divided into functional modules corresponding to each function, or two or more functions can be integrated into one processing module. The integrated module can be implemented in hardware or as a software functional module. It should be noted that the module division in this application embodiment is illustrative and only represents one logical functional division; in actual implementation, other division methods may be used.

[0105] Figure 9 This illustration shows a schematic diagram of the structure of a smart contract vulnerability detection device provided in one embodiment of this application. Figure 9 Each module in the device shown has the ability to implement Figure 2 The functions of each step in the process are defined, and their corresponding technical effects are achieved. For example... Figure 9 As shown, the device may include:

[0106] Module 910 is used to acquire formal deployment information of the smart contract. This formal deployment information includes the formal initial state and a list of formal behaviors. The list of formal behaviors includes the smart contract's initialization execution function and behavior functions.

[0107] Execution module 920 is used to execute the initialization execution functions and behavior functions included in the formal behavior list based on the formal initial state, and obtain the execution result of the smart contract. The execution result of the smart contract includes the execution result obtained from executing the initialization execution functions.

[0108] The detection module 930 is used to detect the execution results of smart contracts according to the formal specification library of smart contracts, and obtain the vulnerability detection results. The formal specification library includes formal specifications for smart contracts.

[0109] In this embodiment, formal deployment information of the smart contract is first obtained. Then, based on the formal initial state included in the formal deployment information, the initialization execution function and behavior function included in the formal behavior list are executed to obtain the execution result of the smart contract. Finally, the execution result of the smart contract is detected according to the formal specification library of the smart contract to obtain the vulnerability detection result. This vulnerability detection result can characterize whether the smart contract has vulnerabilities during actual operation. Both known and unknown vulnerability types can be detected, demonstrating high vulnerability detection accuracy.

[0110] In one embodiment, the detection module 930 is specifically used for:

[0111] For each behavior function, check whether the execution results of the initialization execution function and the behavior function, based on the formal initial state, conform to the formal specification.

[0112] If the conditions are met, the execution result will be associated with the formal initial state, and if the execution result is not included in the formal deployment information, the execution result will be included in the formal deployment information as a new formal initial state.

[0113] If it does not meet the requirements, an error message will be reported.

[0114] In one embodiment, the apparatus further includes a setup module.

[0115] The module is used to build a directed graph. The directed graph includes nodes corresponding to the formal initial state, and two nodes that are related are connected by directed edges.

[0116] Detection module 930 is specifically used for:

[0117] If the conditions are met, check if there is a node in the directed graph that corresponds to the execution result. If it exists, set a directed edge from the initial state to the node. If it does not exist, add the execution result to the directed graph and set a directed edge from the initial state to the node corresponding to the execution result.

[0118] In one embodiment, the formal deployment information includes multiple formal initial states. The apparatus also includes a determination module.

[0119] The determination module is used to determine the execution queue based on multiple formal initial states. The execution queue includes formal initial states that conform to the formal specifications.

[0120] Execution module 920 is specifically used to: execute the initialization execution function and behavior function included in the formal behavior list based on each formal initial state in the execution queue.

[0121] In one embodiment, the apparatus further includes a determining module and an adding module.

[0122] The determination module is used to determine the customized formal specifications of the smart contract based on the smart contract's requirements.

[0123] Add a module to add custom formal specifications to the formal specifications library.

[0124] In this embodiment, formal deployment information of the smart contract is first obtained. Then, based on the formal initial state included in the formal deployment information, the initialization execution function and behavior function included in the formal behavior list are executed to obtain the execution result of the smart contract. Finally, the execution result of the smart contract is detected according to the formal specification library of the smart contract to obtain the vulnerability detection result. This vulnerability detection result can characterize whether the smart contract has vulnerabilities during actual operation. Both known and unknown vulnerability types can be detected, demonstrating high vulnerability detection accuracy.

[0125] Figure 10 A schematic diagram of the structure of an electronic device according to an embodiment of this application is shown. Figure 10 As shown, the device may include a processor 101 and a memory 102 storing computer program instructions.

[0126] Specifically, the processor 101 may include a central processing unit (CPU), an application specific integrated circuit (ASIC), or one or more integrated circuits that can be configured to implement the embodiments of this application.

[0127] Memory 102 may include mass storage for data or instructions. For example, and not limitingly, memory 102 may include a hard disk drive (HDD), floppy disk drive, flash memory, optical disk, magneto-optical disk, magnetic tape, or Universal Serial Bus (USB) drive, or a combination of two or more of these. In one instance, memory 102 may include removable or non-removable (or fixed) media, or memory 102 may be non-volatile solid-state memory. Memory 102 may be internal or external to the integrated gateway disaster recovery device.

[0128] In one instance, memory 102 may be read-only memory (ROM). In one instance, the ROM may be a mask-programmed ROM, a programmable ROM (PROM), an erasable PROM (EPROM), an electrically erasable PROM (EEPROM), an electrically rewritable ROM (EAROM), or flash memory, or a combination of two or more of these.

[0129] The processor 101 reads and executes computer program instructions stored in the memory 102 to achieve... Figure 2 The method in the illustrated embodiment achieves... Figure 2 The technical effects achieved by executing the methods in the examples shown are not elaborated here for the sake of brevity.

[0130] In one example, the electronic device may also include a communication interface 103 and a bus 110. Wherein, for example... Figure 10 As shown, the processor 101, memory 102, and communication interface 103 are connected through bus 110 and communicate with each other.

[0131] The communication interface 103 is mainly used to realize communication between various modules, devices, units and / or equipment in the embodiments of this application.

[0132] Bus 110 includes hardware, software, or both, that couples components of an online data traffic metering device together. For example, and not limitingly, the bus may include an Accelerated Graphics Port (AGP) or other graphics bus, an Extended Industry Standard Architecture (EISA) bus, a Front Side Bus (FSB), a Hyper Transport (HT) interconnect, an Industry Standard Architecture (ISA) bus, an Infinite Bandwidth Interconnect, a Low Pin Count (LPC) bus, a memory bus, a Microchannel Architecture (MCA) bus, a Peripheral Component Interconnect (PCI) bus, a PCI-Express (PCI-X) bus, a Serial Advanced Technology Attachment (SATA) bus, a Video Electronics Standards Association Local (VLB) bus, or other suitable buses, or combinations of two or more of these. Where appropriate, bus 110 may include one or more buses. Although specific buses are described and illustrated in embodiments of this application, this application contemplates any suitable bus or interconnect.

[0133] The electronic device can execute the smart contract vulnerability detection method in the embodiments of this application, thereby achieving... Figure 2 The technical effects of the described smart contract vulnerability detection method.

[0134] Furthermore, in conjunction with the smart contract vulnerability detection methods in the above embodiments, this application embodiment can provide a computer storage medium for implementation. The computer storage medium stores computer program instructions; when these computer program instructions are executed by a processor, they implement any of the smart contract vulnerability detection methods in the above embodiments.

[0135] In an exemplary embodiment, this application also provides a computer program product that, when run on a computer, enables the computer to implement the smart contract vulnerability detection method described in the above embodiments.

[0136] Through the above description of the embodiments, those skilled in the art can clearly understand that, for the sake of convenience and brevity, only the division of the above functional modules is used as an example. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.

[0137] In the several embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of modules or units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another apparatus, or some features may be ignored or not executed. Furthermore, the mutual coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.

[0138] The units described as separate components may or may not be physically separate. A component shown as a unit can be one or more physical units; that is, it can be located in one place or distributed in multiple different locations. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0139] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.

[0140] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solutions of the embodiments of this application, essentially, or the parts that contribute to the prior art, or all or part of the technical solutions, can be embodied in the form of a software product. This software product is stored in a storage medium and includes several instructions to cause a device (which may be a microcontroller, chip, etc.) or processor to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, ROM, RAM, magnetic disks, or optical disks.

[0141] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

Claims

1. A method for detecting smart contract vulnerabilities, characterized in that, The method is applied to a virtual machine server and includes: Obtain formal deployment information of the smart contract; wherein the formal deployment information includes a formal initial state and a list of formal behaviors; the list of formal behaviors includes the smart contract's initialization execution function and behavior functions; Based on the formal initial state, the initialization execution function and behavior function included in the formal behavior list are executed to obtain the execution result of the smart contract; The execution result of the smart contract is tested according to the formal specification library of the smart contract to obtain the vulnerability detection result; the formal specification library includes the formal specification of the smart contract; The step of detecting the execution result of the smart contract according to the formal specification library of the smart contract to obtain the vulnerability detection result includes: For each behavior function, it is checked whether the execution results of the initialization execution function and the behavior function, based on the formal initial state, conform to the formal specification. If the conditions are met, the execution result is associated with the formal initial state, and if the execution result is not included in the formal deployment information, the execution result is included in the formal deployment information as a new formal initial state. If it does not meet the requirements, report an error message; Construct a directed graph; the directed graph includes nodes corresponding to the formalized initial state, and two nodes that are related are connected by directed edges; If the conditions are met, then an association is established between the execution result and the formal initial state, including: if the conditions are met, then checking whether there is a node in the directed graph corresponding to the execution result; if there is, then setting a directed edge from the formal initial state to the node; if there is no such node, then adding the execution result to the directed graph, and simultaneously setting a directed edge from the formal initial state to the node corresponding to the execution result.

2. The method according to claim 1, characterized in that, The formal deployment information includes multiple formal initial states; the method further includes: An execution queue is determined based on the plurality of formal initial states; wherein the execution queue includes formal initial states that conform to the formal specification among the plurality of formal initial states; The step of executing the initialization execution function and behavior function included in the formal behavior list based on the formal initial state includes: executing the initialization execution function and behavior function included in the formal behavior list based on each formal initial state in the execution queue.

3. The method according to claim 1, characterized in that, The method further includes: The customized formal specifications of the smart contract are determined based on the requirements information of the smart contract; Add the customized formal specification to the formal specification library.

4. A smart contract vulnerability detection device, characterized in that, The device is applied to a virtual machine server and includes: The acquisition module is used to acquire formal deployment information of the smart contract; wherein the formal deployment information includes a formal initial state and a list of formal behaviors; the list of formal behaviors includes the initialization execution function and behavior functions of the smart contract; An execution module is used to execute the initialization execution function and behavior function included in the formal behavior list based on the formal initial state, so as to obtain the execution result of the smart contract; The detection module is used to detect the execution result of the smart contract according to the formal specification library of the smart contract, and obtain the vulnerability detection result; the formal specification library includes the formal specification of the smart contract; The detection module is specifically used for: For each behavior function, it is checked whether the execution results of the initialization execution function and the behavior function, based on the formal initial state, conform to the formal specification. If the conditions are met, the execution result is associated with the formal initial state, and if the execution result is not included in the formal deployment information, the execution result is included in the formal deployment information as a new formal initial state. If it does not meet the requirements, report an error message; The device also includes an establishment module; The establishment module is used to establish a directed graph; the directed graph includes nodes corresponding to the formalized initial state, and two nodes with an association relationship are connected by a directed edge; The detection module is specifically used for: If the conditions are met, check if there is a node in the directed graph that corresponds to the execution result. If it exists, set a directed edge from the formal initial state to the node. If it does not exist, add the execution result to the directed graph and set a directed edge from the formal initial state to the node corresponding to the execution result.

5. The apparatus according to claim 4, characterized in that, The formal deployment information includes multiple formal initial states; the device also includes a determination module; The determining module is configured to determine an execution queue based on the plurality of formal initial states; wherein the execution queue includes formal initial states that conform to the formal specification among the plurality of formal initial states; The execution module is specifically used to: execute the initialization execution function and behavior function included in the formal behavior list based on each formal initial state in the execution queue.

6. The apparatus according to claim 4, characterized in that, The device also includes a determining module and an adding module; The determining module is used to determine the customized formal specifications of the smart contract based on the smart contract's requirement information; The adding module is used to add the customized formal specification to the formal specification library.

7. An electronic device, characterized in that, include: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program, when executed by the processor, implements the smart contract vulnerability detection method as described in any one of claims 1 to 3.

8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the smart contract vulnerability detection method as described in any one of claims 1 to 3.

Citation Information

Patent Citations

  • WASM smart contract vulnerability detection method based on symbolic execution

    CN113672515A