A smart contract security audit system and method based on formal verification

Through formal verification technology, the input module, static analysis module, formal verification module, simulation execution module and report and suggestion module are integrated to solve the security loopholes and logical defects before smart contract deployment, and improve the security and audit efficiency of smart contracts. Reduce economic losses and human factor risks.

CN118277284BActive Publication Date: 2024-10-29EAST CHINA JIAOTONG UNIVERSITY
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202410713333.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-06-04
Publication Date
2024-10-29
Estimated Expiration
2044-06-04

AI Technical Summary

Technical Problem

Economic losses and trust crises caused by programming errors or security vulnerabilities after smart contracts are deployed. Traditional security audit methods are inefficient and prone to leaking logic errors and security vulnerabilities.

Method used

Adopt a smart contract security audit system based on formal verification, including input module, static analysis module, formal verification module, simulation execution module and report and suggestion module, through syntax checking, semantic analysis, security rule checking, formal verification and simulation Execute and generate security audit reports and customized repair suggestions.

Benefits of technology

Significantly improve the security of smart contracts, reduce economic losses caused by code defects, improve audit efficiency, reduce security risks caused by human factors, shorten the development cycle, generate easy-to-understand security audit reports and provide practical repair suggestions.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118277284B_ABST
    Figure CN118277284B_ABST
Patent Text Reader

Abstract

The present invention relates to a smart contract security audit system and method based on formal verification. The present invention integrates an input module, a static analysis module, a formal verification module, a simulation execution module, and a report and suggestion module. Before the smart contract is deployed, it can accurately discover security vulnerabilities and logical defects in the smart contract through formal verification and other analysis technologies, thereby significantly improving its security and reducing economic losses that may be caused by code defects. The present invention greatly improves the efficiency of smart contract audits, shortens the development cycle, and generates easy-to-understand security audit reports and provides practical customized repair suggestions, which can help developers improve code and build user trust, making important contributions to the stability and reliability of the entire blockchain ecosystem.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of smart contract technology, and in particular to a smart contract security audit system and method based on formal verification. Background Art

[0002] With the rapid development and widespread application of blockchain technology, smart contracts, as one of its core innovations, have been widely used in various decentralized applications. Smart contracts allow trusted transactions and protocol automation without intermediaries. However, due to the immutability of blockchain, once a smart contract is deployed to a blockchain network, its code cannot be changed, which means that any programming errors or security vulnerabilities may cause serious economic losses and a crisis of trust.

[0003] Currently, there are many potential risks in the development and deployment of smart contracts. For example, recursive attacks may allow attackers to exhaust contract resources by repeatedly calling specific functions; excessive permissions may lead to malicious manipulation of contract behavior; randomness issues will introduce uncertainty and increase the risk of contract manipulation. However, traditional security audit methods rely on manual review and testing, which is not only inefficient, but also prone to missing unforeseen logical errors and security vulnerabilities. Summary of the invention

[0004] The technical problem to be solved by the present invention is to provide a smart contract security audit system and method based on formal verification.

[0005] The technical solution of the present invention to solve the above technical problems is as follows: a smart contract security audit system based on formal verification, comprising:

[0006] Input module, used to receive the source code of the smart contract to be reviewed;

[0007] A static analysis module, used to perform syntax checking, semantic analysis, and security rule checking on the source code of the smart contract to be reviewed, and generate static analysis results;

[0008] A formal verification module, used to use a formal verification tool to perform security verification on the source code of the smart contract to be reviewed and generate a formal verification result;

[0009] A simulation execution module, used to identify execution paths with potential risks in the source code of the smart contract to be reviewed, and to detect the response of the smart contract to be reviewed to the running status during actual use, and to generate simulation execution results;

[0010] A report and suggestion module, used to generate a security audit report and customized repair suggestions for the smart contract to be audited based on the static analysis results, the formal verification results, and the simulation execution results;

[0011] Wherein, the static analysis module includes:

[0012] Syntax checking unit: used to identify syntax errors in the source code of the smart contract to be reviewed, and construct an abstract syntax tree according to the source code of the smart contract to be reviewed;

[0013] Semantic analysis unit: used to traverse the abstract syntax tree through a depth-first search algorithm to detect semantic errors in the source code of the smart contract to be audited, wherein the semantic errors include at least one of a variable type error, a function return value processing error, and a logical consistency error;

[0014] Security rule unit: used to use the security rule library to perform pattern matching on the source code of the smart contract to be reviewed, and identify potential security vulnerabilities in the source code of the smart contract to be reviewed;

[0015] A result output unit is used to generate the static analysis result based on the identified grammatical errors, semantic errors and potential security vulnerabilities.

[0016] The beneficial effects of the present invention are: it integrates an input module, a static analysis module, a formal verification module, a simulation execution module, and a report and suggestion module, and can accurately discover security vulnerabilities and logical defects in smart contracts through formal verification and other analysis techniques before the smart contract is deployed, thereby significantly improving its security and reducing economic losses caused by code defects. This system greatly improves the efficiency of smart contract audits, reduces security risks caused by human factors, shortens the development cycle, and generates easy-to-understand security audit reports and provides practical customized repair suggestions, which can help developers improve code and build user trust, making important contributions to the stability and reliability of the entire blockchain ecosystem.

[0017] Based on the above technical solution, the present invention can also be improved as follows.

[0018] Furthermore, the security rule unit includes:

[0019] A security rule base subunit: used to obtain the security rule base, which includes security vulnerability detection rules;

[0020] Security audit subunit: used to perform pattern matching between the security vulnerability detection rules and the source code of the smart contract to be audited, identify the patterns in the source code of the smart contract to be audited that match the security vulnerability detection rules, and record the patterns in the source code of the smart contract to be audited that match the security vulnerability detection rules as potential security vulnerabilities.

[0021] The beneficial effect of adopting the above further solution is that the security rule library is used to perform pattern matching with the source code of the smart contract to be audited, so as to quickly identify potential security vulnerabilities.

[0022] Furthermore, the formal verification module includes:

[0023] Tool configuration unit: used to obtain the formal verification tool;

[0024] Parameter configuration unit: used to obtain verification parameters, wherein the verification parameters include search depth, search width and security attributes to be verified;

[0025] Model building unit: used to build a formal model based on the source code of the smart contract to be reviewed and the formal verification tool;

[0026] Verification and analysis unit: used to use the formal verification tool to analyze the formal model based on the verification parameters, detect whether the source code of the smart contract to be reviewed meets the security properties to be verified, and generate a formal verification result.

[0027] The beneficial effect of adopting the above further scheme is: applying formal verification tools to analyze the source code of the smart contract to be audited, detecting whether the source code of the smart contract to be audited meets the security properties to be verified, and being able to accurately discover specific types of security vulnerabilities such as recursive attacks, excessive permissions, and randomness problems.

[0028] Furthermore, the formal verification tool includes a formal tool and an algorithm library; the formal tool adopts a theorem prover, a model checker or a static analysis tool; the algorithm library adopts a logical reasoning algorithm, a state space exploration algorithm or an abstract interpretation algorithm.

[0029] The beneficial effect of adopting the above further solution is that different formal tools and algorithm libraries can be selected according to different smart contracts, so as to cope with the source code of smart contracts to be audited with different complexity and business logic.

[0030] Further, the simulation execution module includes:

[0031] A state machine model building unit: used to build a state machine model according to the source code of the smart contract to be reviewed;

[0032] A state space graph construction unit: used to generate a state space graph of the smart contract to be reviewed according to the state machine model;

[0033] Anomaly detection unit: used for traversing the state space graph, detecting and recording execution paths with potential risks in each state transition process involved in traversing the state space graph;

[0034] Simulation unit: used to simulate the operation of the smart contract to be reviewed during actual use, and to detect whether the response of the smart contract to be reviewed to different operation conditions is correct;

[0035] Evaluation unit: used to integrate the abnormality detection situation of the abnormality detection unit and the simulation situation of the simulation unit to generate a simulation execution result.

[0036] The beneficial effect of adopting the above further scheme is that it is possible to foresee various execution paths and external events that the smart contract to be audited may encounter in practice by simulating the actual operation situation, and discover abnormal situations.

[0037] Furthermore, the simulation of the operation of the smart contract to be audited during actual use includes:

[0038] Simulate different types of transactions that the smart contract to be audited may encounter during actual use;

[0039] Simulate the blockchain environment events generated on the blockchain where the smart contract to be reviewed is deployed during the actual use of the smart contract to be reviewed;

[0040] Simulate the abnormal behaviors encountered by the smart contract to be reviewed during actual use.

[0041] Furthermore, the report and suggestion module includes:

[0042] Data extraction and processing unit: used to obtain the static analysis results, the formal verification results and the simulation execution results, and integrate and pre-process them to obtain result data;

[0043] Semantic analysis and understanding unit: used to convert the result data into natural language description using natural language understanding technology to obtain semantic content; classify errors and warnings in the semantic content through text classification and clustering algorithms to generate a security audit report;

[0044] A knowledge base integration and query unit: used to obtain a knowledge base and query the knowledge base for repair strategies and suggestions that match errors in the semantic content, wherein the knowledge base includes smart contract security best practices, historical repair cases, and common vulnerability patterns;

[0045] A customized suggestion generating unit: used for generating the customized repair suggestion based on the semantic content and the repair strategies and suggestions in the knowledge base that match the errors in the semantic content;

[0046] Interactive suggestion optimization unit: used to provide a user interaction interface and adjust the customized repair suggestion according to the user's feedback on the customized repair suggestion.

[0047] The beneficial effect of adopting the above further solution is that it can generate easy-to-understand security audit reports and provide specific code improvement measures and technical optimization suggestions, thereby assisting developers to modify the source code of the smart contract to be audited and improving the efficiency of code security audit and modification.

[0048] To solve the above technical problems, the present invention also provides a smart contract security audit method based on formal verification, including: receiving the source code of the smart contract to be audited; performing syntax check, semantic analysis and security rule check on the source code of the smart contract to be audited, and generating static analysis results; using a formal verification tool to perform security verification on the source code of the smart contract to be audited, and generating formal verification results; identifying execution paths with potential risks in the source code of the smart contract to be audited, and detecting the response of the smart contract to be audited to the running status during actual use, and generating simulated execution results; generating a security audit report and customized repair suggestions for the smart contract to be audited according to the static analysis results, the formal verification results and the simulated execution results;

[0049] Among them, the syntax check, semantic analysis and security rule check of the source code of the smart contract to be audited to generate a static analysis result includes: identifying syntax errors in the source code of the smart contract to be audited, and building an abstract syntax tree based on the source code of the smart contract to be audited; traversing the abstract syntax tree through a depth-first search algorithm to detect semantic errors in the source code of the smart contract to be audited, and the semantic errors include at least one of variable type errors, function return value processing errors and logical consistency errors; using a security rule library to perform pattern matching on the source code of the smart contract to be audited to identify potential security vulnerabilities in the source code of the smart contract to be audited; generating the static analysis result based on the identified syntax errors, semantic errors and potential security vulnerabilities. BRIEF DESCRIPTION OF THE DRAWINGS

[0050] Figure 1 A schematic diagram of a smart contract security audit system based on formal verification of the present invention;

[0051] Figure 2 This is an execution flow chart of a static analysis module of a smart contract security audit system based on formal verification of the present invention;

[0052] Figure 3 This is an execution flow chart of a formal verification module of a smart contract security audit system based on formal verification of the present invention;

[0053] Figure 4This is an execution flow chart of a simulation execution module of a smart contract security audit system based on formal verification of the present invention;

[0054] Figure 5 This is an execution flow chart of a report and suggestion module of a smart contract security audit system based on formal verification in the present invention;

[0055] Figure 6 This is a flow chart of a smart contract security audit method based on formal verification of the present invention. DETAILED DESCRIPTION

[0056] The principles and features of the present invention are described below. The examples given are only used to explain the present invention and are not used to limit the scope of the present invention.

[0057] Embodiment 1

[0058] like Figures 1 to 5 As shown, this embodiment provides a smart contract security audit system based on formal verification, including: an input module for receiving the source code of the smart contract to be audited; a static analysis module for performing syntax checking, semantic analysis and security rule checking on the source code of the smart contract to be audited, and generating static analysis results; a formal verification module for using a formal verification tool to perform security verification on the source code of the smart contract to be audited, and generating formal verification results; a simulation execution module for identifying execution paths with potential risks in the source code of the smart contract to be audited, and detecting the response of the smart contract to be audited to the running status during actual use, and generating simulation execution results; a report and suggestion module for generating a security audit report and customized repair suggestions for the smart contract to be audited based on the static analysis results, the formal verification results and the simulation execution results.

[0059] Among them, the static analysis module includes: a grammar checking unit: used to identify grammatical errors in the source code of the smart contract to be audited, and to construct an abstract syntax tree based on the source code of the smart contract to be audited; a semantic analysis unit: used to traverse the abstract syntax tree through a depth-first search algorithm to detect semantic errors in the source code of the smart contract to be audited, and the semantic errors include at least one of variable type errors, function return value processing errors, and logical consistency errors; a security rule unit: used to use a security rule library to perform pattern matching on the source code of the smart contract to be audited, and identify potential security vulnerabilities in the source code of the smart contract to be audited; a result output unit: used to generate the static analysis result based on the identified grammatical errors, semantic errors, and potential security vulnerabilities.

[0060] This system integrates input module, static analysis module, formal verification module, simulation execution module and report and suggestion module. It can accurately find security vulnerabilities and logical defects of smart contracts through formal verification and other analysis techniques before smart contracts are deployed, thereby significantly improving their security and reducing economic losses caused by code defects. This system greatly improves the efficiency of smart contract audits, reduces security risks caused by human factors, shortens the development cycle, and generates easy-to-understand security audit reports and provides practical customized repair suggestions, which can help developers improve code and build user trust, making important contributions to the stability and reliability of the entire blockchain ecosystem.

[0061] The source code of the smart contract to be audited is written in a smart contract language, such as the Solidity programming language. In the syntax check unit, the source code of the smart contract to be audited is scanned using a syntax analyzer to ensure that it complies with the specifications of the smart contract language. The parameters of the syntax check unit are set to the coding specifications of the smart contract language, and the parameters can be appropriately adjusted according to actual usage requirements. For example, if the source code of the smart contract to be audited is written in the Solidity programming language, the parameters are set to the coding specifications officially provided by the Solidity programming language to ensure the quality and security of the code. The parameters specifically include specifying the version of the Solidity programming language to be followed to ensure that the source code of the smart contract to be audited matches the specific language specifications; setting the code style, such as indentation and naming conventions, to maintain the consistency and readability of the code; defining the language features that are allowed or prohibited to avoid the use of functions that may cause security vulnerabilities; configuring security rules, such as prohibiting low-level mathematical operations to prevent integer overflow, and requiring permission checks to avoid unauthorized access; adjusting compiler warning and error settings to identify and warn potential problems, such as the use of unused variables or dangerous functions; specifying code structure parameters to ensure the reasonable organization of smart contracts; setting optimization options to improve code execution efficiency. Reasonable configuration and application of parameters can help automatically detect and correct problems in the source code of smart contracts during the development phase, thereby enhancing the security and reliability of smart contracts.

[0062] Before using the syntax analyzer to identify syntax errors in the source code of the smart contract to be audited, the lexical analyzer is also used to decompose the source code of the smart contract to be audited into a series of tokens, such as keywords, variable names, operators, etc. In the process of constructing the Abstract Syntax Tree (AST), the syntax analyzer combines these tokens into an internal data structure that conforms to the syntax structure according to the syntax rules of the Solidity programming language, and constructs an AST representing the code structure. This tree structure describes in detail the various elements in the code and their relationships, where the root node represents the entire smart contract and the leaf nodes represent the most basic syntax units. Subsequently, the AST is traversed through the Depth First Search (DFS) algorithm, starting from the root node, and visiting each node layer by layer to ensure that all child nodes are checked. In this process, the semantic analyzer performs a detailed check on each node in the AST to verify the correctness of the variable type, the processing of the function return value, and the consistency of the code logic, thereby ensuring the semantic correctness and security of the smart contract source code. This process provides a solid foundation for the security audit of smart contracts.

[0063] Optionally, in an embodiment, the security rule unit includes: a security rule base sub-unit: used to obtain the security rule base, which includes security vulnerability detection rules; a security audit sub-unit: used to perform pattern matching on the security vulnerability detection rules and the source code of the smart contract to be audited, identify patterns in the source code of the smart contract to be audited that match the security vulnerability detection rules, and record the patterns in the source code of the smart contract to be audited that match the security vulnerability detection rules as potential security vulnerabilities.

[0064] The security rule base can use existing ones or rebuild them. The security vulnerability detection rules are the detection logic of common smart contract vulnerabilities, such as reentrancy attack detection logic, integer overflow detection logic, and permission abuse detection logic. Before pattern matching, it is necessary to use security audit tools to scan the source code of the smart contract to be audited and extract key information, such as function calls, permission settings, state variable modifications, etc. During the pattern matching process, each security vulnerability detection rule in the security rule base needs to be pattern matched with the source code of the smart contract to be audited through a pattern recognition algorithm.

[0065] In some embodiments, the security audit tool generates a detailed report based on the recorded potential security vulnerabilities, listing all potential security vulnerabilities, including their location, type, and possible impact, and providing repair suggestions and improvement measures. Although automated tools can efficiently identify many potential problems, manual review is still required to confirm the accuracy and relevance of these warnings. Developers or security experts need to determine whether the warnings in the report are true and decide whether remediation measures are needed based on the content of the report, combined with the business logic and context of the smart contract. Then, by modifying the code, fix the security vulnerabilities that are confirmed to need to be fixed. After the repair, the security rule unit should be used to re-audit the repaired smart contract source code to verify whether the security vulnerabilities have been successfully resolved and to ensure that no new security issues have been introduced.

[0066] Optionally, in an embodiment, the formal verification module includes: a tool configuration unit: used to obtain the formal verification tool; a parameter configuration unit: used to obtain verification parameters, the verification parameters including search depth, search width and security attributes to be verified; a model construction unit: used to construct a formal model based on the source code of the smart contract to be audited and the formal verification tool; a verification analysis unit: used to use the formal verification tool to analyze the formal model based on the verification parameters, detect whether the source code of the smart contract to be audited meets the security attributes to be verified, and generate a formal verification result.

[0067] Search depth is used to limit the number of state variables in the formal model considered when the formal verification tool analyzes the formal model during the formal verification process. This is usually a trade-off process. The greater the search depth, the more problems may be found, but the demand for computing resources is also higher. Search width is used to limit the range of state variables in the formal model considered in each search step when the formal verification tool analyzes the formal model during the formal verification process. The search width setting affects the breadth and parallelism of the search. Search depth and search width jointly limit the scope and level of detail of the formal verification tool's verification of the source code of the smart contract to be audited. The security attributes to be verified are the security attributes that need to be verified for the smart contract to be audited. They are determined according to actual needs, and the security attributes that need to be verified are clearly defined, and the security attributes to be verified are converted into formal specifications.

[0068] In some embodiments, the auditor needs to further analyze the formal verification results to determine whether the formal model or verification parameters need to be adjusted. After the formal model or verification parameters are adjusted, the formal verification module is re-entered until all security attributes to be verified can be detected.

[0069] In some embodiments, the developer repairs and optimizes the source code of the smart contract based on the formal verification results to eliminate the problems that appear in the formal verification results. After the repair is completed, the modified source code of the smart contract is re-entered into the formal verification module and formal verification is performed again to ensure that the previously discovered problems are solved and no new problems are introduced. The formal verification process is completed only when the source code of the smart contract has completely passed the formal verification and ensured that it meets all the security properties to be verified. Thus, the formal verification module can ensure that the source code of the smart contract exhibits strong security and robustness in the face of various attacks and vulnerabilities.

[0070] Optionally, in an embodiment, the formal verification tool includes a formal tool and an algorithm library; the formal tool adopts a theorem prover, a model checker or a static analysis tool; the algorithm library adopts a logical reasoning algorithm, a state space exploration algorithm or an abstract interpretation algorithm.

[0071] Theorem provers can be Coq, Isabelle / HOL, etc. Theorem provers allow users to verify the correctness of the system by constructing mathematical proofs. Model checkers can be SPIN, UPPAAL, etc. Model checkers check whether the system satisfies certain properties by systematically exploring all possible states. Static analysis tools can be ACL2, Haskell's LiquidHaskell, etc. Static analyzers can analyze the properties of code without executing the code.

[0072] Logical reasoning algorithms are used to deduce the validity of logical formulas in theorem proofs. State space exploration algorithms are used to traverse all possible states of the system in model checking. Abstract interpretation algorithms are used to approximate program behavior to discover potential errors or unsafe behaviors.

[0073] The selection of formal tools and algorithm libraries affects the entire formal verification process. Choosing appropriate formal tools and algorithm libraries is crucial to ensuring the successful execution of the formal verification process. It determines the efficiency, accuracy and effective use of resources. The selection of formal tools and algorithm libraries usually relies on a series of evaluation criteria and methodologies to guide the selection. The criteria considered include: project requirements and system characteristics, security and reliability requirements, tool maturity and support, applicability of algorithm libraries, ease of use and integration capabilities, cost-benefit analysis, etc. For example, for smart contract systems with finite states, model checkers are a better choice.

[0074] Optionally, in an embodiment, the simulation execution module includes: a state machine model construction unit: used to construct a state machine model according to the source code of the smart contract to be audited; a state space graph construction unit: used to generate a state space graph of the smart contract to be audited according to the state machine model; anomaly detection unit: used to traverse the state space graph, detect and record execution paths with potential risks in each state transition involved in the process of traversing the state space graph; simulation unit: used to simulate the operation of the smart contract to be audited during actual use, and detect whether the response of the smart contract to be audited to different operating conditions is correct; evaluation unit: used to integrate the anomaly detection situation of the anomaly detection unit and the simulation situation of the simulation unit to generate a simulation execution result.

[0075] The state machine model includes all possible states of the smart contract and the transitions between states, and the state space diagram covers all state changes that the smart contract may experience during its life cycle. The simulation unit uses advanced simulation technology to simulate transactions initiated by external accounts and blockchain environment events, so as to reproduce the actual operation of the smart contract in a virtual environment. The evaluation unit can comprehensively evaluate the behavior of the smart contract, including normal operation procedures and extreme and edge cases, by combining the abnormal detection of the abnormal detection unit and the simulation of the simulation unit, to ensure that the smart contract can remain stable and secure under any circumstances. The evaluation unit evaluates the performance indicators of the smart contract, including execution efficiency, resource consumption, response time, etc. At the same time, the security of the smart contract will also be evaluated to check whether there are security vulnerabilities or potential attack surfaces. The simulation execution results include execution paths with potential risks, successfully executed transactions, failed transactions, causes of abnormal situations, etc.

[0076] Through this detailed simulation execution process, the simulation execution module provides developers with a powerful tool to help them identify and fix potential problems before smart contracts are deployed, thereby improving the reliability and security of smart contracts.

[0077] Optionally, in an embodiment, the simulation of the operation of the smart contract to be reviewed during actual use includes: simulating different types of transactions encountered by the smart contract to be reviewed during actual use; simulating blockchain environment events generated on the blockchain on which the smart contract to be reviewed is deployed during actual use of the smart contract to be reviewed; and simulating abnormal behaviors encountered by the smart contract to be reviewed during actual use.

[0078] The simulation unit generates multiple types of transactions to simulate various input situations that the smart contract may encounter, including normal transactions, boundary condition transactions, and deliberately constructed malicious transactions, in order to test the smart contract's ability to handle different inputs.

[0079] Smart contracts may respond to specific events on the blockchain, such as when a certain threshold is reached or when the balance of an account changes. The simulation unit will simulate these event triggering conditions to check whether the response logic of the smart contract is correct.

[0080] In order to test the robustness of smart contracts, the simulation unit will deliberately introduce abnormal behaviors, such as transaction failures, network delays, resource limitations, etc., to evaluate the performance of smart contracts under abnormal operating conditions.

[0081] Optionally, in an embodiment, the report and suggestion module includes: a data extraction and processing unit: used to obtain the static analysis results, the formal verification results and the simulation execution results, and integrate and preprocess them to obtain result data; a semantic analysis and understanding unit: used to use natural language understanding technology to convert the result data into a natural language description to obtain semantic content; through text classification and clustering algorithms, the errors and warnings in the semantic content are classified to generate a security audit report; a knowledge base integration and query unit: used to obtain a knowledge base and query the knowledge base for repair strategies and suggestions that match the errors in the semantic content, wherein the knowledge base includes smart contract security best practices, historical repair cases and common vulnerability patterns; a customized suggestion generation unit: used to generate the customized repair suggestion based on the semantic content and the repair strategies and suggestions in the knowledge base that match the errors in the semantic content; an interactive suggestion optimization unit: used to provide a user interaction interface and adjust the customized repair suggestion based on user feedback on the customized repair suggestion.

[0082] The static analysis results, formal verification results, and simulation execution results include error reports, warnings, coverage statistics, etc. In the preprocessing process, natural language processing (NPL) technology is used to clean the text, including removing noise, correcting spelling errors, standardizing terminology, etc. The cleaned text is segmented and tagged with parts of speech to identify key technical terms and concepts.

[0083] A knowledge base is formed by collecting known smart contract security best practices in existing smart contracts, samples of repairs for known security vulnerabilities, logical errors and other problems in existing smart contracts, and known common security vulnerability patterns in existing smart contracts.

[0084] In the customized suggestion generation unit, based on the semantic content and the repair strategies and suggestions in the knowledge base that match the errors in the semantic content, a rule engine or a machine learning model is used to generate customized repair suggestions. For complex security issues, it may be necessary to combine multiple knowledge sources and historical cases to provide a comprehensive solution. Customized repair suggestions include code refactoring suggestions, security vulnerability patching measures, and performance optimization strategies.

[0085] The interactive suggestion optimization unit provides an interactive user interface that allows developers to evaluate and provide feedback on the generated customized suggestions. Based on the developer's feedback, the level of detail and repair plan of the customized suggestions are adjusted to ensure the practicality and operability of the customized suggestions.

[0086] In this embodiment, the report and suggestion module also includes a continuous learning and improvement unit, which is used to collect feedback and repair results from developers as data input for continuous learning; and use machine learning algorithms, such as reinforcement learning or transfer learning, to continuously optimize the suggestion generation model to improve its accuracy and adaptability.

[0087] In addition, the Report & Suggestion module can also provide relevant code examples and predicted effects of the fixed code, so that developers can intuitively see the potential impact of the implementation of the modification suggestions. This process is designed to reduce the burden on developers, allowing them to solve security issues in smart contracts with minimal effort. Finally, the Report & Suggestion module ensures that all suggestions and reports are traceable and actionable, allowing developers to obtain the necessary information directly from the security audit report to modify and optimize the source code of the smart contract. In this way, the Report & Suggestion module provides a comprehensive security audit and improvement framework for the development and maintenance of smart contracts.

[0088] The Report & Suggestion module is responsible for collecting and analyzing a series of data such as static analysis results, formal verification results, and simulation execution results to form a comprehensive security assessment. The Report & Suggestion module can convert technical analysis results into easy-to-understand natural language descriptions and generate a detailed security audit report. The report not only lists all the problems found, but also classifies and assesses the risk level according to the severity of the problem, so that developers can quickly identify the security risks that need to be addressed first. In summary, the Report & Suggestion module provides a complete solution for the security audit and continuous improvement of smart contracts, thereby ensuring that smart contracts are optimal in terms of security and efficiency. The thoroughness and automation of this process significantly improves the efficiency and accuracy of smart contract audits, and provides a solid foundation for the secure deployment and operation of smart contracts.

[0089] The system is described below with reference to specific embodiments:

[0090] The input module receives the source code of the smart contract to be audited, which is written in Solidity language and contains about 1,000 lines of code involving token transfer, permission management, and event logging functions.

[0091] In the static analysis module, static analysis tools such as ESLint are used to perform syntax and semantic checks on the source code of the smart contract to be audited. The parameters are set as follows: the Solidity version is specified as 0.5.16, and all security-related rule checks are enabled. The results show that 15 potential security issues were found in the unoptimized code, including 5 uninitialized variables and 10 operations that may cause integer overflow.

[0092] In the formal verification module, the formal verification tool included with the Solc compiler is used to perform in-depth analysis on the source code of the smart contract. The parameters are set to the default values, and the exploration width and exploration depth are set to 1000 and 100 respectively. The results show that after the increased recursion depth limit, the previously existing recursive vulnerability was successfully identified and fixed.

[0093] In the simulation execution module, a simulator based on Ethereum (EVM) is used to simulate the behavior of smart contracts. The parameters set include a transaction frequency of 10 times per second, randomly generated contract addresses, and trigger events. The simulation execution module reveals a conditional race vulnerability that leads to unfair resource allocation under a specific transaction sequence.

[0094] In the Report and Recommendation module, the automatically generated security audit report lists all the issues found, categorizes them by risk level, and provides detailed repair suggestions. The report points out that the conditional race problem can be solved by adding appropriate exception handling logic and conditional lock mechanism.

[0095] According to the above suggestions, the developer modified the source code of the smart contract and resubmitted it for review. After another review, the new security audit report showed that all identified security issues had been properly fixed.

[0096] In summary, after the security audit, 15 potential security issues in the source code of the smart contract were fully identified and fixed. The repaired smart contract passed all security checks without any false positives or omissions. In addition, the entire audit and repair process shortened the time by about 70% compared to traditional manual audits, significantly improving development efficiency.

[0097] Embodiment 2

[0098] like Figure 6As shown, this embodiment provides a smart contract security audit method based on formal verification, including: receiving the source code of the smart contract to be audited; performing syntax checking, semantic analysis and security rule checking on the source code of the smart contract to be audited, and generating static analysis results; using a formal verification tool to perform security verification on the source code of the smart contract to be audited, and generating formal verification results; identifying execution paths with potential risks in the source code of the smart contract to be audited, and detecting the response of the smart contract to be audited to the running status during actual use, and generating simulation execution results; generating a security audit report and customized repair suggestions for the smart contract to be audited based on the static analysis results, the formal verification results and the simulation execution results.

[0099] Among them, the syntax check, semantic analysis and security rule check of the source code of the smart contract to be audited to generate a static analysis result includes: identifying syntax errors in the source code of the smart contract to be audited, and building an abstract syntax tree based on the source code of the smart contract to be audited; traversing the abstract syntax tree through a depth-first search algorithm to detect semantic errors in the source code of the smart contract to be audited, and the semantic errors include at least one of variable type errors, function return value processing errors and logical consistency errors; using a security rule library to perform pattern matching on the source code of the smart contract to be audited to identify potential security vulnerabilities in the source code of the smart contract to be audited; generating the static analysis result based on the identified syntax errors, semantic errors and potential security vulnerabilities.

[0100] Optionally, in an embodiment, a security rule library is used to perform pattern matching on the source code of the smart contract to be audited to identify potential security vulnerabilities in the source code of the smart contract to be audited, including: obtaining the security rule library, which includes security vulnerability detection rules; performing pattern matching on the security vulnerability detection rules and the source code of the smart contract to be audited to identify patterns in the source code of the smart contract to be audited that match the security vulnerability detection rules, and recording the patterns in the source code of the smart contract to be audited that match the security vulnerability detection rules as potential security vulnerabilities.

[0101] Optionally, in an embodiment, a formal verification tool is used to perform security verification on the source code of the smart contract to be audited, and a formal verification result is generated, including: obtaining the formal verification tool; obtaining verification parameters, the verification parameters including search depth, search width and security attributes to be verified; constructing a formal model based on the source code of the smart contract to be audited and the formal verification tool; using the formal verification tool to analyze the formal model based on the verification parameters, detecting whether the source code of the smart contract to be audited meets the security attributes to be verified, and generating a formal verification result.

[0102] Optionally, in an embodiment, the formal verification tool includes a formal tool and an algorithm library; the formal tool adopts a theorem prover, a model checker or a static analysis tool; the algorithm library adopts a logical reasoning algorithm, a state space exploration algorithm or an abstract interpretation algorithm.

[0103] Optionally, in an embodiment, identifying execution paths with potential risks in the source code of the smart contract to be audited, and detecting the response of the smart contract to be audited to the running conditions during actual use, and generating a simulation execution result, include: constructing a state machine model according to the source code of the smart contract to be audited; generating a state space graph of the smart contract to be audited according to the state machine model; traversing the state space graph, detecting and recording the execution paths with potential risks in each state transition involved in the process of traversing the state space graph; simulating the running conditions of the smart contract to be audited during actual use, and detecting whether the response of the smart contract to different running conditions to be audited is correct; and generating a simulation execution result by integrating the abnormality detection conditions of the abnormality detection unit and the simulation conditions of the simulation unit.

[0104] Optionally, in an embodiment, the simulation of the operation of the smart contract to be reviewed during actual use includes: simulating different types of transactions encountered by the smart contract to be reviewed during actual use; simulating blockchain environment events generated on the blockchain on which the smart contract to be reviewed is deployed during actual use of the smart contract to be reviewed; and simulating abnormal behaviors encountered by the smart contract to be reviewed during actual use.

[0105] Optionally, in an embodiment, a security audit report and customized repair suggestions for the smart contract to be audited are generated based on the static analysis results, the formal verification results and the simulation execution results, including: obtaining the static analysis results, the formal verification results and the simulation execution results and integrating and preprocessing them to obtain result data; using natural language understanding technology to convert the result data into a natural language description to obtain semantic content; classifying the errors and warnings in the semantic content through text classification and clustering algorithms to generate a security audit report; obtaining a knowledge base and querying the knowledge base for repair strategies and suggestions that match the errors in the semantic content, the knowledge base including smart contract security best practices, historical repair cases and common vulnerability patterns; generating the customized repair suggestions based on the semantic content and the repair strategies and suggestions in the knowledge base that match the errors in the semantic content; providing a user interaction interface, and adjusting the customized repair suggestions based on user feedback on the customized repair suggestions.

[0106] In the description of the present invention, it is to be understood that the terms “center”, “longitudinal”, “lateral”, “length”, “width”, “thickness”, “up”, “down”, “front”, “back”, “left”, “right”, “vertical”, “horizontal”, “top”, “bottom”, “inside”, “outside”, “clockwise”, “counterclockwise”, “axial”, “radial”, “circumferential”, etc., indicating orientations or positional relationships based on the orientations or positional relationships shown in the accompanying drawings, are only for the convenience of describing the present invention and simplifying the description, and do not indicate or imply that the referred device or element must have a specific orientation, be constructed and operated in a specific orientation, and therefore should not be understood as limiting the present invention.

[0107] In addition, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined as "first" and "second" may explicitly or implicitly include at least one of the features. In the description of the present invention, the meaning of "plurality" is at least two, such as two, three, etc., unless otherwise clearly and specifically defined.

[0108] In the present invention, unless otherwise clearly specified and limited, the terms "installed", "connected", "connected", "fixed" and the like should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection or an indirect connection through an intermediate medium, it can be the internal connection of two elements or the interaction relationship between two elements, unless otherwise clearly defined. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood according to specific circumstances.

[0109] In the present invention, unless otherwise clearly specified and limited, a first feature being "above" or "below" a second feature may mean that the first and second features are in direct contact, or the first and second features are in indirect contact through an intermediate medium. Moreover, a first feature being "above", "above" or "above" a second feature may mean that the first feature is directly above or obliquely above the second feature, or simply means that the first feature is higher in level than the second feature. A first feature being "below", "below" or "below" a second feature may mean that the first feature is directly below or obliquely below the second feature, or simply means that the first feature is lower in level than the second feature.

[0110] In the description of this specification, the description with reference to the terms "one embodiment", "some embodiments", "example", "specific example", or "some examples" etc. means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present invention. In this specification, the schematic representations of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner. In addition, those skilled in the art may combine and combine the different embodiments or examples described in this specification and the features of the different embodiments or examples, without contradiction.

[0111] Although the embodiments of the present invention have been shown and described above, it is to be understood that the above embodiments are exemplary and are not to be construed as limitations of the present invention. A person skilled in the art may change, modify, replace and vary the above embodiments within the scope of the present invention.

Claims

1. A smart contract security audit system based on formal verification, characterized in that: include: Input module, used to receive the source code of the smart contract to be reviewed; A static analysis module, used to perform syntax checking, semantic analysis, and security rule checking on the source code of the smart contract to be reviewed, and generate static analysis results; A formal verification module, used to use a formal verification tool to perform security verification on the source code of the smart contract to be reviewed and generate a formal verification result; A simulation execution module, used to identify execution paths with potential risks in the source code of the smart contract to be reviewed, and to detect the response of the smart contract to be reviewed to the running status during actual use, and to generate simulation execution results; A report and suggestion module, used to generate a security audit report and customized repair suggestions for the smart contract to be audited based on the static analysis results, the formal verification results, and the simulation execution results; Wherein, the static analysis module includes: Syntax checking unit: used to identify syntax errors in the source code of the smart contract to be reviewed, and construct an abstract syntax tree according to the source code of the smart contract to be reviewed; Semantic analysis unit: used to traverse the abstract syntax tree through a depth-first search algorithm to detect semantic errors in the source code of the smart contract to be audited, wherein the semantic errors include at least one of a variable type error, a function return value processing error, and a logical consistency error; Security rule unit: used to use the security rule library to perform pattern matching on the source code of the smart contract to be reviewed, and identify potential security vulnerabilities in the source code of the smart contract to be reviewed; A result output unit: used for generating the static analysis result based on the identified grammatical errors, semantic errors and potential security vulnerabilities; The security rule unit includes: A security rule base subunit: used to obtain the security rule base, which includes security vulnerability detection rules; The security audit subunit is used to perform pattern matching between the security vulnerability detection rule and the source code of the smart contract to be audited, identify the pattern in the source code of the smart contract to be audited that matches the security vulnerability detection rule, and record the pattern in the source code of the smart contract to be audited that matches the security vulnerability detection rule as a potential security vulnerability; The formal verification tool includes a formal tool and an algorithm library; the formal tool adopts a theorem prover, a model checker or a static analysis tool; the algorithm library adopts a logical reasoning algorithm, a state space exploration algorithm or an abstract interpretation algorithm; the model checker adopts SPIN.

2. According to claim 1, a smart contract security audit system based on formal verification is characterized in that: The formal verification module includes: Tool configuration unit: used to obtain the formal verification tool; Parameter configuration unit: used to obtain verification parameters, wherein the verification parameters include search depth, search width and security attributes to be verified; Model building unit: used to build a formal model based on the source code of the smart contract to be reviewed and the formal verification tool; Verification and analysis unit: used to use the formal verification tool to analyze the formal model based on the verification parameters, detect whether the source code of the smart contract to be reviewed meets the security properties to be verified, and generate a formal verification result.

3. According to claim 1, a smart contract security audit system based on formal verification is characterized in that: The simulation execution module comprises: A state machine model building unit: used to build a state machine model according to the source code of the smart contract to be reviewed; A state space graph construction unit: used to generate a state space graph of the smart contract to be reviewed according to the state machine model; Anomaly detection unit: used for traversing the state space graph, detecting and recording execution paths with potential risks in each state transition process involved in traversing the state space graph; Simulation unit: used to simulate the operation of the smart contract to be reviewed during actual use, and to detect whether the response of the smart contract to be reviewed to different operation conditions is correct; Evaluation unit: used to integrate the abnormality detection situation of the abnormality detection unit and the simulation situation of the simulation unit to generate a simulation execution result.

4. According to claim 3, a smart contract security audit system based on formal verification is characterized in that: The simulation of the operation of the smart contract to be reviewed during actual use includes: Simulate different types of transactions that the smart contract to be audited may encounter during actual use; Simulate the blockchain environment events generated on the blockchain where the smart contract to be reviewed is deployed during the actual use of the smart contract to be reviewed; Simulate the abnormal behaviors encountered by the smart contract to be reviewed during actual use.

5. According to claim 1, a smart contract security audit system based on formal verification is characterized in that: The report and recommendation module includes: Data extraction and processing unit: used to obtain the static analysis results, the formal verification results and the simulation execution results, and integrate and pre-process them to obtain result data; Semantic analysis and understanding unit: used to convert the result data into natural language description using natural language understanding technology to obtain semantic content; classify errors and warnings in the semantic content through text classification and clustering algorithms to generate a security audit report; A knowledge base integration and query unit: used to obtain a knowledge base and query the knowledge base for repair strategies and suggestions that match errors in the semantic content, wherein the knowledge base includes smart contract security best practices, historical repair cases, and common vulnerability patterns; A customized suggestion generating unit: used for generating the customized repair suggestion based on the semantic content and the repair strategies and suggestions in the knowledge base that match the errors in the semantic content; Interactive suggestion optimization unit: used to provide a user interaction interface and adjust the customized repair suggestion according to the user's feedback on the customized repair suggestion.

6. A smart contract security audit method based on formal verification, characterized in that: include: Receive the source code of the smart contract to be reviewed; Performing syntax checking, semantic analysis, and security rule checking on the source code of the smart contract to be reviewed, and generating static analysis results; Use formal verification tools to verify the security of the source code of the smart contract to be reviewed and generate formal verification results; Identify execution paths with potential risks in the source code of the smart contract to be audited, and detect the response of the smart contract to be audited to the running status during actual use, and generate simulation execution results; Generate a security audit report and customized repair suggestions for the smart contract to be audited based on the static analysis results, the formal verification results, and the simulation execution results; The syntax check, semantic analysis and security rule check of the source code of the smart contract to be reviewed are performed to generate static analysis results, including: Identify syntax errors in the source code of the smart contract to be reviewed, and construct an abstract syntax tree based on the source code of the smart contract to be reviewed; Traversing the abstract syntax tree by a depth-first search algorithm to detect semantic errors in the source code of the smart contract to be audited, wherein the semantic errors include at least one of a variable type error, a function return value processing error, and a logical consistency error; Using the security rule library to perform pattern matching on the source code of the smart contract to be audited, and identifying potential security vulnerabilities in the source code of the smart contract to be audited; Generate the static analysis result based on the identified grammatical errors, semantic errors, and potential security vulnerabilities; Using the security rule library to perform pattern matching on the source code of the smart contract to be audited, identifying potential security vulnerabilities in the source code of the smart contract to be audited, including: obtaining the security rule library, which includes security vulnerability detection rules; performing pattern matching on the security vulnerability detection rules and the source code of the smart contract to be audited, identifying patterns in the source code of the smart contract to be audited that match the security vulnerability detection rules, and recording the patterns in the source code of the smart contract to be audited that match the security vulnerability detection rules as potential security vulnerabilities; The formal verification tool includes a formal tool and an algorithm library; the formal tool adopts a theorem prover, a model checker or a static analysis tool; the algorithm library adopts a logical reasoning algorithm, a state space exploration algorithm or an abstract interpretation algorithm; the model checker adopts SPIN.

Citation Information

Patent Citations

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

    CN111062038A

  • Intelligent contract security detection system based on block chain

    CN117034299A