Smart contract vulnerability detection method, device and equipment and storage medium

By parsing the source code of smart contracts and adjusting the function logic according to the virtual machine rules to generate formal rules, the problem of low accuracy in existing smart contract vulnerability detection is solved, and high-precision vulnerability detection is achieved.

CN115062312BActive Publication Date: 2025-11-28THE PEOPLES BANK OF CHINA DIGITAL CURRENCY INST
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202210736683.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-06-27
Publication Date
2025-11-28
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

By analyzing the source code of smart contracts, the execution logic of functions and the calling relationships between functions are obtained. The function logic is adjusted according to the function execution rules of the virtual machine, formal rules are generated for vulnerability detection, and formal verification methods are used to simulate the operation of smart contracts to detect whether there are vulnerabilities in the smart contracts.

Benefits of technology

It achieves high-precision smart contract vulnerability detection, capable of detecting both known and unknown vulnerabilities, thus improving the accuracy and efficiency of detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115062312B_ABST
    Figure CN115062312B_ABST
Patent Text Reader

Abstract

The application discloses a smart contract vulnerability detection method and device, equipment and a storage medium, and relates to the field of blockchains. The smart contract vulnerability detection method comprises the following steps: analyzing the source code of a smart contract to obtain the running logic of each function in the source code of the smart contract and the calling relationship between the functions; the source code of the smart contract comprises a plurality of functions; adjusting the running logic of the functions according to the function running rules of a virtual machine to obtain the new running logic of the functions; generating formal rules according to the new running logic of the functions and the calling relationship between the functions; and performing vulnerability detection on the smart contract according to the formal rules to obtain a vulnerability detection result. The application solves the problem of low detection accuracy of the existing smart contract vulnerability detection method.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of blockchains, and in particular to a smart contract vulnerability detection method and device, equipment and a storage medium. BACKGROUND

[0002] A blockchain is a new application mode of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanism and encryption algorithm. The blockchain is a technology for using a decentralized consensus mechanism to maintain a complete, distributed and tamper-proof ledger database, which enables participants in the blockchain to realize a unified ledger system without establishing a trust relationship. In the blockchain, a smart contract is written in a digital form, and a smart contract is a set of digitally defined commitments, including protocols on which contract participants can execute the commitments. The storage, reading and execution of the entire process are transparent, traceable and tamper-proof, which are guaranteed by the characteristics of the smart contract and the blockchain technology. At the same time, a state machine system is built by the consensus algorithm of the blockchain, so that the smart contract can be efficiently run.

[0003] Due to the tamper-proof nature of the blockchain, once a smart contract vulnerability is caused by a problem in the program itself, it will cause irreversible major losses. Therefore, the detection of the smart contract vulnerability is crucial. The current smart contract vulnerability detection method mainly uses the inherent rules of the existing vulnerabilities to write into the software, and uses the inherent rules in the software to detect the smart contract. This detection method can only detect the discovered vulnerabilities, and the detection accuracy is low. SUMMARY

[0004] The embodiments of the present application provide a smart contract vulnerability detection method, device, equipment and storage medium, which solves the problem of low detection accuracy of the existing smart contract vulnerability detection method.

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

[0006] In a first aspect, the embodiments of the present application provide a smart contract vulnerability detection method, comprising:

[0007] parsing a source code of a smart contract to obtain a running logic of a function included in the smart contract and a calling relationship between functions; the source code of the smart contract includes a plurality of functions;

[0008] adjusting the running logic of the function according to a function running rule of a virtual machine to obtain a new running logic of the function; wherein the function running rule includes a running rule obtained by formalizing processing of a virtual machine feature; the virtual machine feature is a feature affecting the running logic of the function in the process of running the function by the virtual machine; the new running logic of the function matches an actual running logic of the function on the virtual machine;

[0009] generate a formal rule according to the new running logic of the function and the function-to-function calling relationship; the formal rule is used to specify a running rule that should be met when the function is run based on the new running logic of the function;

[0010] perform vulnerability detection on the smart contract according to the formal rule, to obtain a vulnerability detection result.

[0011] In the embodiments of the present application, the running logic of each function and the function-to-function calling relationship in the source code of the smart contract are parsed, and the running logic of the function is adjusted according to the function running rule of the virtual machine, to obtain a new running logic of the function, so that the new running logic obtained can conform to the actual running situation in the virtual machine. Then, a formal rule is generated according to the new running logic of the function and the function-to-function calling relationship, and the formal rule can simulate the running of the smart contract, so as to perform vulnerability detection on the smart contract and obtain a vulnerability detection result. The vulnerability detection result can represent whether the smart contract has a vulnerability in actual running, and both known vulnerability types and unknown vulnerability types can be detected, so the vulnerability detection precision of the smart contract is high.

[0012] In one embodiment, the formal rule includes a formal logic description and a formal specification description. The formal logic description represents the running logic of the function included in the smart contract in the virtual machine, and the formal specification description represents a running rule that should be met in the running process of the function included in the smart contract. The vulnerability detection on the smart contract is performed according to the formal rule, to obtain a vulnerability detection result, including:

[0013] convert the formal logic description into a formal logic code, and convert the formal specification description into a formal verification specification;

[0014] execute the formal logic code to obtain an execution result;

[0015] generate a vulnerability detection result according to the execution result and the formal verification specification, the vulnerability detection result representing whether the execution result conforms to the formal verification specification.

[0016] In one embodiment, the vulnerability detection result includes a vulnerability description corresponding to a case where the smart contract does not conform to the formal verification specification. The method further includes modifying the source code of the smart contract according to the vulnerability description.

[0017] In one embodiment, before the running logic of the function is adjusted according to the function running rule of the virtual machine to obtain a new running logic of the function, the method further includes:

[0018] obtain a virtual machine feature;

[0019] formalize the virtual machine feature to obtain the function running rule of the virtual machine;

[0020] store the function running rule into a virtual machine formal feature library.

[0021] In an embodiment, the function corresponds to a plurality of running logics; according to the function running rule of the virtual machine, the running logic of the function is adjusted to obtain a new running logic of the function, including:

[0022] query the virtual machine formal feature library to obtain the function running rule corresponding to the function;

[0023] According to the function running rule, the running logic of the function is split and recombined to obtain a new running logic of the function.

[0024] In a second aspect, the embodiments of the present application provide a smart contract vulnerability detection device, including:

[0025] The parsing module is configured to parse the source code of the smart contract to obtain the running logic of the function included in the smart contract and the calling relationship between the functions; the source code of the smart contract includes a plurality of functions;

[0026] The adjusting module is configured to adjust the running logic of the function according to the function running rule of the virtual machine to obtain a new running logic of the function; wherein the function running rule includes a running rule obtained by formalizing the features of the virtual machine; the features of the virtual machine are features that affect the running logic of the function during the process of running the function by the virtual machine; the new running logic of the function matches the actual running logic of the function on the virtual machine;

[0027] The generating module is configured to generate a formal rule according to the new running logic of the function and the calling relationship between the functions; wherein the formal rule is used to specify the running rule that should be met when running the function based on the new running logic of the function;

[0028] The detection module is configured to detect the vulnerability of the smart contract according to the formal rule to obtain a vulnerability detection result.

[0029] In an embodiment, the formal rule includes a formal logic description and a formal specification description; the formal logic description represents the running logic of the function included in the smart contract on the virtual machine, and the formal specification description represents the running rule that should be met during the running process of the function included in the smart contract; the detection module is specifically configured to:

[0030] convert the formal logic description into a formal logic code, and convert the formal specification description into a formal verification specification;

[0031] execute the formal logic code to obtain an execution result;

[0032] generate a vulnerability detection result according to the execution result and the formal verification specification, wherein the vulnerability detection result represents whether the execution result conforms to the formal verification specification.

[0033] In an embodiment, the vulnerability detection result comprises a vulnerability description corresponding to a case where the smart contract does not conform to the formal verification specification; the apparatus further comprises a modification module;

[0034] The modification module is configured to modify the source code of the smart contract according to the vulnerability description.

[0035] In an embodiment, the apparatus further comprises an acquisition module, a processing module and a storage module;

[0036] The acquisition module is configured to acquire the virtual machine features before adjusting the running logic of the function according to the function running rules of the virtual machine to obtain new running logic of the function.

[0037] The processing module is configured to perform formal processing on the virtual machine features to obtain the function running rules of the virtual machine.

[0038] The storage module is configured to store the function running rules in the virtual machine formal feature library.

[0039] In an embodiment, the function corresponds to multiple running logics; the adjustment module is specifically configured to:

[0040] Query the virtual machine formal feature library to obtain the function running rules corresponding to the function.

[0041] Split and recombine the running logic of the function according to the function running rules to obtain new running logic of the function.

[0042] In a third aspect, an embodiment of the present application provides an electronic device, comprising a memory, a processor and a computer program stored in the memory and executable on the processor, and the computer program is executed by the processor to implement the smart contract vulnerability detection method of the first aspect.

[0043] In a fourth aspect, an embodiment of the present application provides a computer readable storage medium, and the computer readable storage medium stores a computer program, and the computer program is executed by a processor to implement the smart contract vulnerability detection method of the first aspect.

[0044] In a fifth aspect, the present application provides a computer program product, when the computer program product is executed on a computer, the computer program product causes the computer to execute the method provided in the first aspect.

[0045] The beneficial effects described in the second aspect, the third aspect, the fourth aspect and the fifth aspect of the present application can be analyzed with reference to the beneficial effects of the first aspect, which will not be repeated here. BRIEF DESCRIPTION OF DRAWINGS

[0046] Figure 1 An application scenario diagram of a smart contract vulnerability detection method provided by an embodiment of the present application;

[0047] Figure 2 A flowchart of a smart contract vulnerability detection method provided for an embodiment of the present application is shown in FIG. 1;

[0048] Figure 3 A function-to-function calling relationship diagram provided for an embodiment of the present application is shown in FIG. 2;

[0049] Figure 4 A flowchart of another smart contract vulnerability detection method provided for an embodiment of the present application is shown in FIG. 3;

[0050] Figure 5 A flowchart of another smart contract vulnerability detection method provided for an embodiment of the present application is shown in FIG. 4;

[0051] Figure 6 A structural diagram of a smart contract vulnerability detection device provided for an embodiment of the present application is shown in FIG. 5;

[0052] Figure 7 A structural diagram of an electronic device provided for an embodiment of the present application is shown in FIG. 6. DETAILED DESCRIPTION

[0053] The features and exemplary embodiments of various aspects of the present application will be described in detail below, in order to make the purposes, technical solutions and advantages of the present application clearer, the present application will be further described in detail below in combination with the drawings and specific embodiments. It should be understood that the specific embodiments described herein are configured only to explain the present application and are not configured to limit the present application. The present application can be implemented without some of these specific details for those skilled in the art. The following description of the embodiments is merely to provide a better understanding of the present application by showing examples of the present application.

[0054] It should be noted that, in this document, relational terms such as first and second and the like can be used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. The terms "comprises", "comprising", or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but can include other elements not expressly listed or inherent to such process, method, article, or apparatus. An element proceeded by "comprises... a" does not, without more constraints, exclude the presence of additional identical elements in the process, method, article, or apparatus that comprises the element. The terms "includes", "including", or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that includes a list of elements does not include only those elements but can include other elements not expressly listed or inherent to such process, method, article, or apparatus. An element proceeded by "includes... a" does not, without more constraints, exclude the presence of additional identical elements in the process, method, article, or apparatus that includes the element.

[0055] There are two main methods for detecting vulnerabilities in smart contracts. One is to extract the inherent operating rules of the discovered vulnerabilities and write them into software to detect vulnerabilities in smart contracts. The other is manual detection, which involves manually checking the logic of smart contracts to determine whether there are vulnerabilities. However, the first method can only detect discovered vulnerabilities and has limited detection capabilities and low detection accuracy. The second method is very inefficient and its detection accuracy is limited by the ability of the detection personnel.

[0056] In summary, the existing methods for detecting vulnerabilities in smart contracts have low accuracy.

[0057] To solve the above technical problems, the embodiments of the present application consider that when a smart contract runs in a virtual machine, the operating logic of the smart contract will be changed according to the self-rules of the virtual machine, resulting in unexpected vulnerabilities. A method for detecting vulnerabilities in smart contracts is provided, which includes: analyzing the operating logic of each function in the source code of the smart contract and the calling relationship between functions, and adjusting the operating logic of the functions according to the function operating rules of the virtual machine to obtain new operating logic of the functions, so that the new operating logic obtained can conform to the actual situation of running in the virtual machine. Then, based on the new operating logic of the functions and the calling relationship between the functions, a formal rule is generated, which can simulate the running of the smart contract, thereby realizing the detection of vulnerabilities in the smart contract and obtaining a vulnerability detection result. The vulnerability detection result can represent whether there are vulnerabilities in the smart contract when it is actually running, whether it is a known vulnerability type or an unknown vulnerability type can be detected, and the detection accuracy of the vulnerabilities in the smart contract is high.

[0058] 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.

[0059] The execution subject 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 a server. 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 subject of this smart contract vulnerability detection method. The following description uses a server as the execution subject.

[0060] 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 1 As shown, the server in the smart contract vulnerability detection method provided in this application embodiment is connected to a virtual machine cluster, which includes at least one virtual machine. The server can obtain virtual machine characteristics from the virtual machine cluster based on the aforementioned communication connection. Furthermore, the server stores the source code of the smart contract (or smart contract or smart contract code). For example, the source code of the smart contract may include functions (such as transfer functions) for implementing transactions between account A and account B, as well as the agreed transaction rules between account A and account B. The server can check the operational logic of the functions included in the smart contract in conjunction with the virtual machine characteristics, and update / adjust the smart contract based on the detection results (such as updating / adjusting the operational logic of the functions in the smart contract), so that the operational logic of the functions in the updated smart contract matches the actual operational logic of the functions on the virtual machine, thereby implementing the smart contract vulnerability detection method provided in this application embodiment using the updated smart contract.

[0061] Figure 2 A flowchart illustrating a smart contract vulnerability detection method according to an embodiment of this application is shown. Figure 2 As shown, the method may include the following steps:

[0062] S210, parse the source code of the smart contract to obtain the execution logic of each function in the source code of the smart contract and the calling relationship between functions.

[0063] The smart contract is a computer protocol designed to disseminate, verify, or execute contracts in an information-based manner. The smart contract allows trusted transactions without a third party, which are traceable and irreversible. The source code of the smart contract includes a plurality of functions. By parsing the source code of the smart contract, the running logic of each function in the source code of the smart contract and the calling relationship between functions can be obtained.

[0064] The running logic of the function can refer to the process / steps that should be executed in time sequence when the function is running. The calling relationship between functions can be used to represent the calling order between functions, etc. If there is a calling relationship between two functions, it means that one function needs to call another function to execute normally. As shown in Figure 3 The calling relationship between functions can be identified by a function call relationship graph. The smart contract includes three functions A, B, and C. There is a calling relationship between A and C, which means that function A needs to be called when running function C, i.e., the running of function C depends on function A. There is a calling relationship between B and C, which means that function B needs to be called when running function C, i.e., the running of function C depends on function B. There is no calling relationship between A and B, which means that function B and function A run independently and are not dependent on each other.

[0065] In S220, the running logic of the function is adjusted according to the function running rule of the virtual machine to obtain a new running logic of the function.

[0066] The function running rule includes a running rule obtained by formalizing the characteristics of the virtual machine. The characteristics of the virtual machine refer to characteristics that affect the running logic of the smart contract during the running of the virtual machine on the smart contract. It should be noted that in the embodiments of the present application, the virtual machine operates the smart contract can be understood as the virtual machine running the functions included in the source code of the smart contract, and the running logic of the smart contract can refer to the running logic of the functions included in the source code of the smart contract.

[0067] Further, the function running rule includes the function characteristics and the adjustment rule corresponding to the function characteristics. When the function in the smart contract meets the function characteristics, the running logic of the function is adjusted according to the adjustment rule corresponding to the function characteristics.

[0068] For example, assuming that the virtual machine is an Embedded Virtual Machine (EVM), and the function included in the smart contract is a transfer function, the running logic of the transfer function is a transfer operation. When the EVM executes the transfer function of the smart contract to perform the transfer operation, the fallback mechanism of the smart contract is triggered, also known as the callback function, which causes the running logic of the transfer function of the smart contract to change, and the callback operation is also performed at the same time. In this case, the fallback mechanism can be a virtual machine characteristic.

[0069] In an embodiment, the function running rules can be obtained in advance by formalizing the virtual machine features, and the function running rules are stored in a virtual machine formalization database to obtain a virtual machine formalization feature library. Based on the virtual machine formalization feature library, efficient acquisition of the function running rules can be achieved. The generation process of the function running rules can refer to the following Figure 5 Corresponding embodiments are described below.

[0070] The new running logic of the function obtained by adjusting the running logic of the function according to the function running rules can match the actual running logic of the smart contract in the virtual machine, such as being consistent / same. In this way, the virtual machine does not affect the running logic of the smart contract in the process of detecting the vulnerability of the smart contract based on the new running logic, and the detection accuracy is high.

[0071] For example, adjusting the running logic of the function according to the function running rules of the virtual machine can be achieved by splitting and recombining the original running logic of the function, or adding new running logic, etc.

[0072] For example, EVM has a vulnerability called reentrant attack vulnerability, which refers to when the first smart contract transfers money to the second smart contract, the second smart contract performs a second transfer by calling the function of the first smart contract. Now assume that the first smart contract includes a transfer function for transferring money from account M to account N. The transfer function is divided into three steps: the first step is to determine whether the transfer amount is greater than the balance of account M; the second step is to call the system function to transfer money to account N (account N is the account corresponding to the second smart contract); the third step is to deduct the transfer amount from the balance of account M. Further, querying the EVM formalization feature library, it is found that EVM has a feature: when a smart contract account receives money, it will automatically trigger the callback function of the smart contract, so in this example, when account M transfers money to account N corresponding to the second smart contract through the first smart contract, EVM will trigger the callback function of the second smart contract, which can call the transfer function of the first smart contract, resulting in a second transfer. The function running rules in this case include a transfer feature and the corresponding adjustment rules, that is, when the first smart contract includes a transfer operation and matches the transfer feature in the function running rules, the transfer logic of the first smart contract needs to be split and recombined. In this example, splitting is to separate the first step and the second step from the third step, and recombination is to add the callback function of the second smart contract. After recombination, the transfer process of the first smart contract becomes first step, second step, first step, second step, third step, third step. If the first step, second step, and third step correspond to the first function, then the execution process of the transfer function after recombination becomes first step, second step, first function, third step.

[0073] In this example, the virtual machine formal feature library is used to adjust the execution logic of smart contract functions. The adjusted function execution logic results in a new execution logic that better reflects the smart contract's behavior within the virtual machine. Vulnerability detection based on this new execution logic achieves higher accuracy.

[0074] S230 generates formal rules based on the new execution logic of the functions and the calling relationships between functions.

[0075] A smart contract consists of two components: the execution logic of each function and the calling relationships between functions. New smart contracts can be generated based on the new execution logic and calling relationships between functions. Formalizing these new smart contracts yields formal rules.

[0076] Furthermore, formal rules include formal logical descriptions and formal specification descriptions. Formal logical descriptions characterize the execution logic of a smart contract within the virtual machine (EVM), such as the execution logic formed after a fallback call caused by a contract transfer operation in the EVM is inserted into the original contract logic. Based on this formal logical description, errors in the contract transfer operation can be detected during subsequent formal verification. Formal specification descriptions characterize the execution rules that a smart contract should satisfy during operation, such as the total amount remaining unchanged during contract execution and the individual account balance not being zero. Formal rules can be generated based on the new execution logic of functions and the call relationships between functions.

[0077] S240 performs vulnerability detection on smart contracts according to formal rules and obtains the vulnerability detection results.

[0078] Based on this formal rule, the operation of smart contracts can be simulated, thereby enabling vulnerability detection of smart contracts and obtaining vulnerability detection results. These results can characterize whether vulnerabilities exist in the actual operation of the smart contract.

[0079] Furthermore, such as Figure 4 As shown, in one embodiment, S240: Performing vulnerability detection on the smart contract according to formal rules to obtain vulnerability detection results may include:

[0080] S2401 converts formal logic descriptions into formal logic code and formal specification descriptions into formal verification specifications.

[0081] S2402, execute the formal logic code and obtain the execution result.

[0082] S2403 generates vulnerability detection results based on the execution results and formal verification specifications. The vulnerability detection results indicate whether the execution results conform to the formal verification specifications.

[0083] The formalized logic description can be converted into executable formalized logic code. The formalized specification description is converted into a formalized verification specification. The formalized logic code is a mathematical-based description that can be run in a proprietary formalized verification tool. Commonly used formalized verification tools include TLA+, Scyther, etc. The formalized verification specification refers to the limited conditions for the formalized verification tool when executing the formalized logic code. Based on the matching of the limited conditions and the execution results of the formalized logic code, it can be determined whether there is a vulnerability and the specific situation of the vulnerability.

[0084] For example, if the execution result of the formalized logic code meets the limited conditions, there is no vulnerability, indicating that the execution result of the formalized logic code conforms to the formalized verification specification; otherwise, if the execution result of the formalized logic code does not meet the limited conditions, there is a vulnerability, indicating that the execution result of the formalized logic code does not conform to the formalized verification specification.

[0085] In the embodiments of the present application, the running logic of each function in the source code of the smart contract and the calling relationship between functions are parsed, and the running logic of the function is adjusted according to the function running rules of the virtual machine to obtain a new running logic of the function, so that the new running logic obtained can conform to the actual running situation in the virtual machine. Based on the new running logic of the function and the calling relationship between functions, a formalized rule is generated, based on which the smart contract can be simulated to run, and thus the vulnerability detection of the smart contract is realized, and a vulnerability detection result is obtained. The vulnerability detection result can indicate whether there is a vulnerability in the actual running of the smart contract, whether it is a known vulnerability type or an unknown vulnerability type can be detected, and the vulnerability detection accuracy of the smart contract is high.

[0086] In one embodiment, if the vulnerability detection result includes a vulnerability description corresponding to the situation that the smart contract does not conform to the formalized verification specification; the method can further include modifying the source code of the smart contract according to the vulnerability description.

[0087] The vulnerability description indicates the reason for the vulnerability of the source code of the smart contract, and the source code of the smart contract can be modified based on the vulnerability description. In the embodiments of the present application, modifying the source code of the smart contract can refer to modifying the related parameters of the function whose execution result does not conform to the formalized specification in the smart contract, such as modifying the initialization parameters of the function, the behavior parameters in the behavior list, etc.

[0088] For example, the smart contract includes a transfer function for transferring from A to B, C, where the initialization parameters are A has 100 yuan, B and C have 0 yuan, and the behavior list is: payment, with two parameters: destination account, transfer amount, and the transfer amount type is integer, with a value range of 0 to 255. Assuming the formal specification is: the total amount remains unchanged, that is, the total amount is 100, and the account balance cannot be negative, then the formal specification is represented as A + B + C = 100 ∩ A >= 0 ∩ B >= 0 ∩ C >= 0. When the transfer behavior is executed, since the transfer amount value range is 0 to 255, the transfer function will execute A to B or C transfer 0 yuan, 1 yuan, …, 255 yuan. Assuming that the execution result of the transfer function is A is 99, B is 1, and C is 0, which meets the formal verification specification A + B + C = 100 ∩ A >= 0 ∩ B >= 0 ∩ C >= 0. Assuming that the execution result of the transfer function is A is 99, B is 1, and C is 1, A + B + C ≠ 100, then the check result does not meet the formal specification; at this time, the source code of the smart contract needs to be modified.

[0089] In the embodiments of the application, modifying the source code of the smart contract according to the vulnerability description can eliminate the source code vulnerability of the smart contract and improve the security of the source code of the smart contract.

[0090] In one embodiment, as shown in Figure 5 Before S220: adjusting the running logic of the function according to the function running rule of the virtual machine to obtain the new running logic of the function, the method can further include:

[0091] S250, obtaining virtual machine features.

[0092] The virtual machine features can be obtained based on the communication connection with the virtual machine server. The virtual machine is the running environment of the smart contract. Different virtual machines have different virtual machine features. The virtual machine features refer to features that affect the running logic of the smart contract during the running of the smart contract, for example, the fallback mechanism triggered by the EVM when executing the contract transfer operation will cause the execution logic of the contract to change, in which case the fallback mechanism can be a virtual machine feature.

[0093] S260, formalizing the virtual machine features to obtain the function running rule.

[0094] The virtual machine features can be formalized by a formalization method, and then the function running rule is obtained. The formalization method in logic science refers to a method of analyzing and researching the formal structure of thinking. It compares various thinking forms (mainly propositions and reasoning) with different contents, finds out the connection between each part, such as the connection between concepts in a proposition and the connection between propositions in reasoning, extracts the common formal structure, and then introduces a symbolic language to express the formal structure. Through the formalization method, the virtual machine features can be converted into the function running rule.

[0095] In S270, the function running rule is stored in the virtual machine formalization feature library.

[0096] The virtual machine features are formalized to obtain the function running rule. The function running rule is stored in a database of the function running rule, that is, the virtual machine formalization feature library.

[0097] In the embodiment of the application, the function running rule is generated in advance and can be stored in the virtual machine formalization feature library. The function running rule can be directly obtained from the virtual machine formalization feature library. The function running rule can be used to adjust the running logic of the function in the smart contract, so that the new running logic obtained after adjustment conforms to the actual situation of the smart contract running in the virtual machine, and the vulnerability detection accuracy of the smart contract is improved.

[0098] Figures 1-5 The smart contract vulnerability detection method is described. The following describes the smart contract vulnerability detection method with reference to the accompanying drawings Figure 6 and the accompanying drawings Figure 7 The device provided by the embodiment of the application is described. In order to realize the above functions, the smart contract vulnerability detection device includes hardware structure and / or software module corresponding to each function. Those skilled in the art should easily realize that the algorithm steps of each example described in combination with the embodiments disclosed in the present application can be realized in the form of hardware or a combination of hardware and computer software. Whether a certain function is executed by hardware or computer software driven hardware depends on the specific application of the technical solution and the design constraint conditions. Professional technicians can use different methods to realize the described functions for each specific application, but such implementation should not be considered beyond the scope of the present application.

[0099] The embodiments of the present application can divide the function modules of the smart contract vulnerability detection apparatus according to the above method. The smart contract vulnerability detection apparatus can divide each function module according to each function, or integrate two or more functions in one processing module. The above integrated module can be realized in the form of hardware or in the form of software function module. It should be noted that the division of the modules in the embodiments of the present application is illustrative, and is only a logical function division. In actual implementation, there can be another division manner.

[0100] Figure 6 The structure diagram of the smart contract vulnerability detection apparatus provided by an embodiment of the present application is shown, Figure 6 Each module of the apparatus has the function of realizing Figure 2 each step, and can achieve the corresponding technical effect. For example Figure 6 The apparatus can include:

[0101] The parsing module 610 is configured to parse the source code of the smart contract to obtain the running logic of the functions included in the smart contract and the calling relationship between the functions. The source code of the smart contract includes a plurality of functions.

[0102] The adjusting module 620 is configured to adjust the running logic of the functions according to the function running rules of the virtual machine to obtain the new running logic of the functions. The function running rules include the running rules obtained by formalizing the features of the virtual machine. The features of the virtual machine are the features that affect the running logic of the functions in the process of running the functions by the virtual machine. The new running logic of the functions matches the actual running logic of the functions on the virtual machine.

[0103] The generating module 630 is configured to generate the formal rules according to the new running logic of the functions and the calling relationship between the functions. The formal rules are used to specify the running rules that should be met when running the functions based on the new running logic of the functions.

[0104] The detecting module 640 is configured to perform vulnerability detection on the smart contract according to the formal rules to obtain the vulnerability detection result.

[0105] In the embodiments of the present application, the running logic of each function in the source code of the smart contract and the calling relationship between functions are analyzed, and the running logic of the functions is adjusted according to the function running rules of the virtual machine to obtain new running logic of the functions, so that the new running logic obtained can conform to the actual running situation in the virtual machine. Based on the new running logic of the functions and the calling relationship between functions, a formal rule is generated, based on which the smart contract can be simulated to implement vulnerability detection of the smart contract, and a vulnerability detection result is obtained, which can represent whether the smart contract has a vulnerability in actual running, whether it is a known vulnerability type or an unknown vulnerability type can be detected, and the vulnerability detection accuracy of the smart contract is high.

[0106] In an embodiment, the formal rule includes a formal logic description and a formal specification description. The formal logic description represents the running logic of the functions included in the smart contract in the virtual machine, and the formal specification description represents the running rules that should be met by the functions included in the smart contract during the running process. The detection module 640 is specifically configured to:

[0107] convert the formal logic description into a formal logic code, and convert the formal specification description into a formal verification specification.

[0108] execute the formal logic code to obtain an execution result.

[0109] generate a vulnerability detection result according to the execution result and the formal verification specification, the vulnerability detection result representing whether the execution result conforms to the formal verification specification.

[0110] In an embodiment, the vulnerability detection result includes a vulnerability description corresponding to the case that the smart contract does not conform to the formal verification specification. The apparatus further includes a modification module.

[0111] The modification module is configured to modify the source code of the smart contract according to the vulnerability description.

[0112] In an embodiment, the apparatus further includes an acquisition module, a processing module and a storage module.

[0113] The acquisition module is configured to acquire virtual machine features before adjusting the running logic of the functions according to the function running rules of the virtual machine to obtain new running logic of the functions.

[0114] The processing module is configured to perform formal processing on the virtual machine features to obtain the function running rules of the virtual machine.

[0115] The storage module is configured to store the function running rules to a virtual machine formal feature library.

[0116] In an embodiment, the function corresponds to multiple running logics. The adjustment module 620 is specifically configured to:

[0117] Query the formal feature library of the virtual machine to obtain a function running rule corresponding to the function.

[0118] Split and recombine the running logic of the function according to the function running rule to obtain a new running logic of the function.

[0119] In the embodiments of the application, the running logic of each function in the source code of the smart contract and the calling relationship between functions are analyzed, and the running logic of the function is adjusted according to the function running rule of the virtual machine to obtain a new running logic of the function, so that the new running logic obtained can conform to the actual running situation in the virtual machine. Based on the new running logic of the function and the calling relationship between functions, a formal rule is generated, which can simulate the running of the smart contract, and then the vulnerability detection of the smart contract is implemented to obtain a vulnerability detection result, which can represent whether the smart contract has a vulnerability in actual running, and both known vulnerability types and unknown vulnerability types can be detected, and the vulnerability detection accuracy of the smart contract is high.

[0120] Figure 7 A structural schematic diagram of an electronic device provided by an embodiment of the application is shown. As shown in the figure, the device can include a processor 701 and a memory 702 storing computer program instructions. Figure 7

[0121] Specifically, the processor 701 can include a central processing unit (CPU), or an application specific integrated circuit (ASIC), or one or more integrated circuits configured to implement the embodiments of the application.

[0122] The memory 702 can include a mass storage for data or instructions. By way of example and not limitation, the memory 702 can include a hard disk drive (HDD), a floppy disk drive, a flash memory, an optical disk, a magneto-optical disk, a magnetic tape, or a universal serial bus (USB) drive or a combination of two or more of these. In one example, the memory 702 can include a removable or non-removable (or fixed) medium, or the memory 702 is a non-volatile solid state memory. The memory 702 can be internal or external to the integrated gateway disaster recovery device.

[0123] ​In one example, the memory 702 can be a Read Only Memory (ROM). In one example, the ROM can be a mask programmed ROM, a Programmable ROM (PROM), an Erasable PROM (EPROM), an Electrically Erasable PROM (EEPROM), an Electrically Alterable ROM (EAROM), or a flash memory, or a combination of two or more of these.

[0124] The processor 701 implements the method in the embodiments of the present application by reading and executing the computer program instructions stored in the memory 702. Figure 2 The processor 701 implements the method in the embodiments of the present application by reading and executing the computer program instructions stored in the memory 702. Figure 2 The corresponding technical effects achieved by the examples of the embodiments of the present application are not described herein for brevity.

[0125] In one example, the electronic device can further include a communication interface 703 and a bus 710. As shown in the figure, the processor 701, the memory 702, and the communication interface 703 are connected through the bus 710 and complete communication with each other. Figure 7

[0126] The communication interface 703 is mainly used to realize the communication between the modules, devices, units, and / or equipment in the embodiments of the present application.

[0127] The bus 710 includes hardware, software, or both, which couples the components of the online data traffic billing device to each other. By way of example, and not limitation, the bus can include an Accelerated Graphics Port (AGP) or other graphics bus, an Enhanced Industry Standard Architecture (EISA) bus, a Front Side Bus (FSB), a Hyper Transport (HT) interconnect, an Industry Standard Architecture (ISA) bus, an InfiniBand interconnect, a Low Pin Count (LPC) bus, a memory bus, a Micro Channel 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 another suitable bus or interconnect, or a combination of two or more of these. Where appropriate, the bus 710 can include one or more buses. Although the present application describes and illustrates a particular bus, the present application contemplates any suitable bus or interconnect.

[0128] The electronic device can execute the smart contract vulnerability detection method in the embodiments of the present application, thereby realizing Figure 2 Figure 2 Figure 7 Figure 2 ​Corresponding technical effects of the described smart contract vulnerability detection method.

[0129] In addition, in combination with the smart contract vulnerability detection method in the above embodiments, the embodiments of the present application can provide a computer storage medium for implementation. The computer storage medium has computer program instructions stored thereon; the computer program instructions are executed by a processor to implement any one of the smart contract vulnerability detection methods in the above embodiments.

[0130] In exemplary embodiments, the embodiments of the present application also provide a computer program product, which, when the computer program product is run on a computer, causes the computer to implement the smart contract vulnerability detection method in the above embodiments.

[0131] Through the above description of the implementation, those skilled in the art can clearly understand that, for the convenience and brevity of description, only the above-mentioned division of each functional module is taken as an example for illustration, and in actual application, the above-mentioned functions can be completed by different functional modules according to needs, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above.

[0132] In several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only illustrative, such as the division of modules or units, which is only a logical function division, and actual implementation can have another division manner, such as combining or integrating multiple units or components into another device, or some features can be ignored or not executed. In addition, the coupling or direct coupling or communication connection between the displayed or discussed units can be indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.

[0133] The units described as separate components can or can not be physically separated, and the components shown as units can be one physical unit or multiple physical units, that is, they can be located in one place or distributed to multiple different places. Some or all of the units can be selected according to actual needs to achieve the purpose of the present embodiment.

[0134] In addition, each functional unit in each embodiment of the present application can be integrated in one processing unit, or each unit can exist physically, or two or more units can be integrated in one unit. The above integrated unit can be realized in the form of hardware or in the form of software functional unit.

[0135] The integrated unit, if implemented in the form of a software function unit and sold or used as an independent product, can be stored in a readable storage medium. Based on such understanding, the technical solutions of the embodiments of the present application essentially or say the part that contributes to the prior art or the whole or part of the technical solutions can be embodied in the form of a software product. The software product is stored in a storage medium, includes a plurality of instructions to make a device (which can be a single-chip microcomputer, a chip, etc.) or a processor execute all or part of the steps of the method described in various embodiments of the present application. The aforementioned storage medium includes: a U disk, a mobile hard disk, a ROM, a RAM, a magnetic disk or an optical disk, and various storage program codes.

[0136] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any change or replacement within the technical scope disclosed in the present application should be covered within the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the protection scope of the claims.

Claims

1. A method for detecting smart contract vulnerabilities, characterized in that, include: Obtain virtual machine characteristics; The virtual machine features are formalized to obtain the function execution rules of the virtual machine; The function execution rules are stored in the virtual machine formal feature library; Analyze the source code of the smart contract to obtain the execution logic of the functions included in the smart contract and the calling relationships between the functions; The source code of the smart contract includes multiple functions; According to the function execution rules of the virtual machine, the execution logic of the function is adjusted to obtain a new execution logic for the function; wherein, the function execution rules include execution rules obtained by formal processing of virtual machine features; the virtual machine features are the features that affect the execution logic of the function during the execution of the function by the virtual machine; the new execution logic of the function matches the actual execution logic of the function on the virtual machine; Formal rules are generated based on the new execution logic of the function and the calling relationships between the functions; wherein, the formal rules are used to specify the execution rules that should be satisfied when the function is run based on the new execution logic of the function; the formal rules include formal logic description and formal specification description; the formal logic description characterizes the execution logic of the functions included in the smart contract in the virtual machine, and the formal specification description characterizes the execution rules that the functions included in the smart contract should satisfy during the execution process; The formal logic description is converted into formal logic code, and the formal specification description is converted into a formal verification specification; Execute the formal logic code to obtain the execution result; Based on the execution result and the formal verification specification, a vulnerability detection result is generated, which indicates whether the execution result conforms to the formal verification specification.

2. The smart contract vulnerability detection method according to claim 1, characterized in that, The vulnerability detection results include a vulnerability description corresponding to the smart contract not conforming to the formal verification specification; The method further includes modifying the source code of the smart contract according to the vulnerability description.

3. The smart contract vulnerability detection method according to claim 1 or 2, characterized in that, The function corresponds to multiple execution logics; adjusting the execution logic of the function according to the function execution rules of the virtual machine to obtain the new execution logic of the function includes: The function execution rules corresponding to the function are obtained by querying the virtual machine formal feature library. The execution logic of the function is split and reorganized according to the function execution rules to obtain the new execution logic of the function.

4. A smart contract vulnerability detection device, characterized in that, include: The acquisition module is used to acquire virtual machine characteristics; The processing module is used to formalize the virtual machine features to obtain the function execution rules of the virtual machine; The storage module is used to store the function execution rules into the virtual machine formal feature library; The parsing module is used to parse the source code of the smart contract to obtain the execution logic of the functions included in the smart contract and the calling relationship between the functions; The source code of the smart contract includes multiple functions; An adjustment module is used to adjust the execution logic of a function according to the function execution rules of a virtual machine, thereby obtaining a new execution logic for the function; wherein, the function execution rules include execution rules obtained by formal processing of virtual machine features; the virtual machine features are features that affect the execution logic of the function during the execution of the function by the virtual machine; the new execution logic of the function matches the actual execution logic of the function on the virtual machine; A generation module is used to generate formal rules based on the new execution logic of the function and the calling relationships between the functions; wherein, the formal rules are used to specify the execution rules that should be satisfied when the function is run based on the new execution logic of the function; the formal rules include formal logic description and formal specification description; the formal logic description characterizes the execution logic of the functions included in the smart contract in the virtual machine, and the formal specification description characterizes the execution rules that the functions included in the smart contract should satisfy during the execution process; The detection module is used to convert the formal logic description into formal logic code and the formal specification description into a formal verification specification. The detection module is also used to execute the formal logic code to obtain the execution result; The detection module is further configured to generate a vulnerability detection result based on the execution result and the formal verification specification, wherein the vulnerability detection result indicates whether the execution result conforms to the formal verification specification.

5. The smart contract vulnerability detection device according to claim 4, characterized in that, The vulnerability detection result includes a vulnerability description corresponding to when the smart contract does not conform to the formal verification specification; the device also includes a modification module. The modification module is used to modify the source code of the smart contract according to the vulnerability description.

6. The smart contract vulnerability detection device according to claim 4 or 5, characterized in that, The function corresponds to multiple execution logics; the adjustment module is specifically used for: The function execution rules corresponding to the function are obtained by querying the virtual machine formal feature library. The execution logic of the function is split and reorganized according to the function execution rules to obtain the new execution logic of the function.

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

  • Intelligent contract formal verification system and method based on state space

    CN111062038A