Intelligent contract vulnerability automatic repairing method and system based on vulnerability report

By building a sliced ​​data dictionary and using a large language model for entity recognition and structure repair, and generating and optimizing patch code, the problem of smart contract logic vulnerability repair is solved, and efficient source code-level repair and high repair rate are achieved.

CN119597326AActive Publication Date: 2025-03-11PEKING UNIV
View PDF 7 Cites 0 Cited by

Patent Information

Application Number
CN202411476604.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-22
Publication Date
2025-03-11
Estimated Expiration
2044-10-22

AI Technical Summary

Technical Problem

It is difficult for existing technology to effectively repair logic vulnerabilities in smart contracts at the source code level, and existing tools are mainly limited to fixing traditional vulnerabilities and cannot handle business logic-related vulnerabilities.

Method used

By building a sliced ​​data dictionary, obtain vulnerability description and location information of smart contracts, combine large language models for entity recognition and structure repair, generate patch code and optimize it for repair.

Benefits of technology

It realizes the repair of logic vulnerabilities in smart contracts at the source code level, improves the repair rate, can handle complex business logic-related vulnerabilities, and reduces the workload of developers.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119597326A_ABST
    Figure CN119597326A_ABST
Patent Text Reader

Abstract

The invention discloses an intelligent contract vulnerability automatic repairing method and system based on a vulnerability report, and belongs to the technical field of block chains. The method comprises the following steps: constructing a slice data dictionary containing contract key elements and vulnerability code slices; for a smart contract, contract key elements related to structured repair are obtained based on vulnerability description of the smart contract, and related vulnerability code slices are obtained in combination with the slice data dictionary; generating patch codes of the related vulnerability code slices; and optimizing the patch code, and repairing the smart contract by using the optimized patch code to obtain a repairing result of the smart contract. According to the method, common logic vulnerabilities in the real world at present can be repaired on the source code level, and the method has a high and complete repair rate.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of blockchain technology, and in particular to a method and system for automatically repairing smart contract vulnerabilities based on vulnerability reports. Background Art

[0002] Blockchain technology is a new application model of computer technologies such as distributed computing, P2P networks, cryptography, and consensus mechanisms. Data is combined into blocks in the form of transactions, connected in a chain structure, and consensus is formed in a distributed network. Due to its tamper-proof and traceable characteristics, it has received widespread attention and application in many fields such as finance, credit reporting, and product traceability.

[0003] Smart contracts are executable codes on the blockchain, and developers can use them to implement complex functions. Smart contracts run in a specific environment, of which Ethereum Virtual Machine (EVM) is the most common one, and is used in many blockchain platforms such as Ethereum and Hyperledger Burrow.

[0004] Since blockchain often involves financial asset transactions and other activities, in order to prevent smart contracts from being attacked by hackers, developers will ask third-party code auditing agencies to test smart contracts. For example, Code4Rena is a professional smart contract auditing platform. Nevertheless, repairing code based on third-party vulnerability reports is also a complex and arduous task. Although there are many automated repairs for traditional program codes, there are few existing methods and tools for repairing smart contract vulnerabilities. The main ones are as follows:

[0005] 1. SGUARD, proposed by Singapore Management University, first statically analyzes the control flow and data flow of smart contracts to detect contract vulnerabilities, and then automatically repairs contract vulnerabilities by inserting security patches. However, SGUARD can only repair four more traditional vulnerabilities: internal reentrancy vulnerabilities, cross-function reentrancy vulnerabilities, access control, and arithmetic overflow vulnerabilities.

[0006] 2.EVMPatch, proposed by the University of Duisburg-Essen, is a tool that targets bytecode-level repairs rather than source code-level repairs, and uses a bytecode rewriting engine to repair security vulnerabilities in deployed contracts. First, the vulnerability is detected through static analysis, and then the bytecode is rewritten using a predefined patch template to complete the patch insertion. Similarly, this tool can only repair underlying code problems such as access control and integer overflow.

[0007] In fact, many current contract vulnerabilities are not code implementation issues, but business logic issues. Such issues cannot be fixed with simple static analysis. The main difficulties are as follows:

[0008] 1. Vulnerabilities in the contract logic cannot be fixed through bytecode because the source code needs to be rewritten;

[0009] 2. To fix contract logic vulnerabilities, you need to first understand the business logic of the entire contract. There is no fixed template that can be applied;

[0010] 3. To fix contract logic vulnerabilities, it is necessary to combine code logic and business logic at the same time, so it is necessary to consider the combination of static analysis and deep learning; Summary of the invention

[0011] The technical problem to be solved by the present invention is to provide a method and system for automatically repairing smart contract vulnerabilities based on vulnerability reports in response to the above-mentioned deficiencies in the prior art, so as to achieve the purpose of being able to repair common logical vulnerabilities in the real world at the source code level and having a higher and more complete repair rate.

[0012] In order to solve the above technical problems, the technical solutions adopted by the present invention include the following contents.

[0013] A method for automatically repairing smart contract vulnerabilities based on vulnerability reports, the method comprising:

[0014] Build a slice data dictionary containing contract key elements and vulnerability code slices;

[0015] For a smart contract, key contract elements involved in structured repair are obtained based on the vulnerability description of the smart contract, and the relevant vulnerability code slices are obtained in combination with the slice data dictionary;

[0016] Generate patch code for the relevant vulnerability code slice;

[0017] The patch code is optimized, and the smart contract is repaired using the optimized patch code to obtain a repair result of the smart contract.

[0018] Furthermore, the construction of a slice data dictionary containing contract key elements and vulnerability code slices includes:

[0019] Collect smart contract samples containing vulnerabilities and third-party vulnerability detection reports of the smart contract samples, and obtain the source code of the smart contract samples;

[0020] Compile and test the source code of the smart contract sample to obtain the compiled version and environment in which the smart contract sample runs;

[0021] Perform entity recognition on the third-party vulnerability detection report based on the large language model to obtain vulnerability description and vulnerability location information;

[0022] Based on the vulnerability description and the vulnerability location information, all source codes associated with the vulnerable functions are screened out from the source code of the smart contract sample, and the corresponding source codes are divided into function-level code slice data;

[0023] Combined with the compiled version and environment in which the smart contract sample is run, static analysis is performed on the source code of the smart contract sample, and based on the static analysis results, a contract key element dependency graph of the smart contract containing the vulnerability is constructed;

[0024] Combine function-level code slice data and contract key element dependency graph to build the original slice data dictionary;

[0025] The original slice data dictionary is filtered according to the vulnerability description and the vulnerability location information to obtain a slice data dictionary containing contract key elements and vulnerability code slices.

[0026] Furthermore, for a smart contract, obtaining key contract elements involved in structured repair based on the vulnerability description of the smart contract includes:

[0027] The vulnerability description of the smart contract is embedded into the domain prompt word template, and combined with the large language model, the attack steps and scope of the corresponding vulnerability are obtained;

[0028] Based on the attack steps and scope of the corresponding vulnerability, generate the key elements of the contract involved in the structured repair.

[0029] Furthermore, generating patch codes for the relevant vulnerability code slices includes:

[0030] The relevant vulnerability code slice is embedded into the domain code repair prompt word template, and combined with the large language model, the patch code of the relevant vulnerability code slice is obtained; wherein the prompt word module of the domain code repair prompt word template includes: role playing, task description, external structured information and expected output, and the content of the external structured information includes: attack process, repair strategy, code interaction structure diagram and code slice.

[0031] Further, optimizing the patch code includes:

[0032] The patch code and the vulnerability description of the smart contract are embedded into a code verification / optimization prompt word template to verify the patch code; wherein the prompt word module of the code verification / optimization prompt word template includes: role playing, task description, external structured information and expected output, and the content of the external structured information includes: attack process, repair strategy, code interaction structure diagram, code slice and patch code;

[0033] When the verification result shows that the patch code has no vulnerability, the patch code is used as the optimized patch code;

[0034] When the verification result shows that the patch code has a vulnerability, an optimization result of the patch code is output.

[0035] Furthermore, the smart contract is repaired using the optimized patch code to obtain a repair result of the smart contract, including:

[0036] Compile and test the source code of the smart contract to obtain the compiled version and environment in which the smart contract runs;

[0037] After replacing the corresponding vulnerability code slice with the optimized patch code, compile verification is performed based on the compiled version and environment of the smart contract;

[0038] After the compilation verification is passed, the repair result of the smart contract is obtained.

[0039] Furthermore, after repairing the smart contract using the optimized patch code and obtaining the repair result of the smart contract, the method further includes:

[0040] Using a vulnerability detector to perform vulnerability detection on the repair result of the smart contract; wherein the vulnerability detector includes: Slither or GPTScan;

[0041] If the vulnerability detector does not detect a vulnerability in the repair result of the smart contract, the output repair task is completed.

[0042] A smart contract vulnerability automatic repair system based on vulnerability reports, the system comprising:

[0043] Dictionary construction module, used to build a slice data dictionary containing contract key elements and vulnerability code slices;

[0044] A vulnerability code acquisition module is used to obtain key contract elements involved in structured repair for a smart contract based on the vulnerability description of the smart contract, and obtain relevant vulnerability code slices in combination with the slice data dictionary;

[0045] A patch code generation module, used to generate patch codes for the relevant vulnerability code slices;

[0046] The vulnerability repair module is used to optimize the patch code and use the optimized patch code to repair the smart contract to obtain a repair result of the smart contract.

[0047] An electronic device, comprising: a processor and a memory storing computer program instructions; when the processor executes the computer program instructions, the method for automatically repairing smart contract vulnerabilities based on vulnerability reports described in any one of the above items is implemented.

[0048] A computer-readable storage medium, characterized in that computer program instructions are stored on the computer-readable storage medium, and when the computer program instructions are executed by a processor, any of the above-mentioned methods for automatically repairing smart contract vulnerabilities based on vulnerability reports is implemented.

[0049] Compared with the prior art, the present invention repairs real vulnerabilities in real-world contracts through static analysis at the source code layer, dependency and slice construction of contract code elements, code semantic understanding and vulnerability attack analysis based on LLM, vulnerability code patch generation, repair code verification and other steps. Due to the adoption of static analysis of contract code, the contract code structure can be effectively analyzed. On the one hand, it can screen out valid codes related to vulnerabilities, and on the other hand, it helps to enhance LLM's understanding of the contract code structure and reduce illusions. Since the repair method based on traditional symbolic execution and patch templates is very limited, and the form of repairable vulnerabilities is single, the generalization ability of repair can be greatly improved through contract vulnerability repair based on LLM. In particular, in the repair of business logic, this method can be well applied in the field of smart contract security auditing, helping developers to automatically repair security vulnerabilities in smart contracts, greatly reducing the workload of developers. BRIEF DESCRIPTION OF THE DRAWINGS

[0050] Figure 1 A smart contract in source code form and a vulnerability detection report fragment thereof are provided in an embodiment of the present invention.

[0051] Figure 2 A process diagram of a method for automatically repairing smart contract vulnerabilities based on vulnerability reports provided in an embodiment of the present invention.

[0052] Figure 3 An example diagram of function-level code slicing provided by an embodiment of the present invention.

[0053] Figure 4 A domain code repair prompt word template provided by an embodiment of the present invention.

[0054] Figure 5A code verification / optimization prompt word template provided by an embodiment of the present invention.

[0055] Figure 6 A patch code for repairing vulnerabilities provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0056] The specific implementation of the present invention is further described in detail below in conjunction with the accompanying drawings and examples. The following examples are used to illustrate the present invention, but are not intended to limit the scope of the present invention.

[0057] This embodiment is as follows Figure 1 Taking the combination of the three smart contract code snippets and their corresponding vulnerability detection report related contents as an example, the smart contract is automatically repaired using a smart contract vulnerability automatic repair method based on vulnerability report of the present invention.

[0058] like Figure 2 As shown, the technical solution of the present invention includes a dictionary construction stage, a vulnerability location stage, a patch generation stage and a patch optimization stage.

[0059] 1. Dictionary construction phase.

[0060] The dictionary construction phase is used to build a slice data dictionary based on smart contract samples and their third-party vulnerability detection reports. The construction process of the slice data dictionary includes the following steps 1.1-1.5.

[0061] Step 1.1: Obtain smart contract samples and third-party vulnerability detection reports, compile and detect the code of the smart contract samples, and automatically configure the compilation version and environment for the running of the smart contract samples.

[0062] Step 1.2: Document analysis of third-party vulnerability detection reports. For contract vulnerability reports, LLM is used for entity recognition to extract vulnerability descriptions and vulnerability location information.

[0063] exist Figure 1 In the implementation example, the vulnerability detection report shows that the vulnerability is associated with functions such as crossSwapExactTokensForTokens, getAmountsOut, registerTrade, _swapExactT4T, _swap, and is related to the MarginRouter and UniswapStyleLib contracts.

[0064] Step 1.3: Filter the code files to be tested in the folder of the smart contract sample, filter out all code files associated with the vulnerable functions, and divide all codes into function levels. Figure 3 for Figure 1Example of function-level code slicing in the embodiment. The _swap function is the vulnerability code closely related to the vulnerability found by the tool. Figure 1 Code search associated with code slices.

[0065] Step 1.4: Combined with the compiled version and environment of the smart contract sample, static analysis is performed on the source code of the smart contract sample to build an abstract syntax tree, function call graph, and data flow graph. Based on all static analysis results, a contract key element dependency graph of the smart contract file is built.

[0066] Step 1.5: Combine the function-level code slice data from step 1.3 and the contract key element dependency graph from step 1.4 to build a slice data dictionary of key elements and corresponding code slices. Then, based on the vulnerability description and vulnerability location information from step 1.2, filter out the valid subset of the code slice data dictionary required to fix the vulnerability.

[0067] 2. Vulnerability location stage.

[0068] For a smart contract to be repaired, the vulnerability description of the smart contract is embedded in the designed domain prompt word template and input into LLM (such as GPT-4) to analyze the specific attack steps and scope of the corresponding vulnerability. The code elements involved in the structured repair are generated based on the attack steps and scope. Among them, the vulnerability description of the smart contract can also be generated based on the LLM in step 1.2.

[0069] 3. Patch generation phase.

[0070] The present invention extracts all code slices associated with the vulnerability from the code context slice data dictionary based on the generated structured repair element data. Then, the associated code slices are embedded into Figure 4 The repair code is generated in the domain code repair prompt word template.

[0071] 4. Patch optimization stage.

[0072] This embodiment establishes an LLM-based vulnerability repair verifier, and inputs the generated repair code and the vulnerability description into the verifier. Figure 5 This is the code verification / optimization hint template for the bug fix verifier.

[0073] If the verifier outputs that the repaired code has no vulnerabilities, it will enter the compilation verification and rule verification.

[0074] If the verifier outputs that the patched code still has defects, the verifier will repair the patch code again.

[0075] If the verifier outputs that the repaired code has no defects, the patch code (or optimized patch code) is replaced with the original code, and then compiled and verified. After the compilation and verification pass, the vulnerability detector such as Slither, GPTScan and other tools are used to re-detect the vulnerability. If it passes, the repair task is completed.

[0076] Figure 6 The code in the yellow box is a code patch generated in this embodiment, which is used to repair the smart contract vulnerability.

[0077] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some or all of the technical features therein. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope defined by the claims of the present invention.

Claims

1. A method for automatically repairing smart contract vulnerabilities based on vulnerability reports, characterized in that: The method comprises: Build a slice data dictionary containing contract key elements and vulnerability code slices; For a smart contract, key contract elements involved in structured repair are obtained based on the vulnerability description of the smart contract, and the relevant vulnerability code slices are obtained in combination with the slice data dictionary; Generate patch code for the relevant vulnerability code slice; The patch code is optimized, and the smart contract is repaired using the optimized patch code to obtain a repair result of the smart contract.

2. The method according to claim 1, characterized in that The construction includes a slice data dictionary containing contract key elements and vulnerability code slices, including: Collect smart contract samples containing vulnerabilities and third-party vulnerability detection reports of the smart contract samples, and obtain the source code of the smart contract samples; Compile and test the source code of the smart contract sample to obtain the compiled version and environment in which the smart contract sample runs; Perform entity recognition on the third-party vulnerability detection report based on the large language model to obtain vulnerability description and vulnerability location information; Based on the vulnerability description and the vulnerability location information, all source codes associated with the vulnerability function are screened out from the source code of the smart contract sample, and the corresponding source codes are divided into function-level code slice data; Combined with the compiled version and environment in which the smart contract sample is run, static analysis is performed on the source code of the smart contract sample, and based on the static analysis results, a dependency graph of key contract elements of the smart contract containing the vulnerability is constructed; Combine function-level code slice data and contract key element dependency graph to build the original slice data dictionary; The original slice data dictionary is filtered according to the vulnerability description and the vulnerability location information to obtain a slice data dictionary containing contract key elements and vulnerability code slices.

3. The method according to claim 1, characterized in that For a smart contract, key contract elements involved in structured repair are obtained based on the vulnerability description of the smart contract, including: The vulnerability description of the smart contract is embedded into the domain prompt word template, and combined with the large language model, the attack steps and scope of the corresponding vulnerability are obtained; Based on the attack steps and scope of the corresponding vulnerability, generate the key elements of the contract involved in the structured repair.

4. The method according to claim 1, characterized in that: Generate patch code for the relevant vulnerability code slice, including: The relevant vulnerability code slice is embedded into the domain code repair prompt word template, and combined with the large language model, the patch code of the relevant vulnerability code slice is obtained; wherein the prompt word module of the domain code repair prompt word template includes: role playing, task description, external structured information and expected output, and the content of the external structured information includes: attack process, repair strategy, code interaction structure diagram and code slice.

5. The method according to claim 1, characterized in that Optimize the patch code, including: The patch code and the vulnerability description of the smart contract are embedded into a code verification / optimization prompt word template to verify the patch code; wherein the prompt word module of the code verification / optimization prompt word template includes: role playing, task description, external structured information and expected output, and the content of the external structured information includes: attack process, repair strategy, code interaction structure diagram, code slice and patch code; When the verification result shows that the patch code has no vulnerability, the patch code is used as the optimized patch code; When the verification result shows that the patch code has a vulnerability, an optimization result of the patch code is output.

6. The method according to claim 1, characterized in that The smart contract is repaired using the optimized patch code to obtain a repair result of the smart contract, including: Compile and test the source code of the smart contract to obtain the compiled version and environment for running the smart contract; After replacing the corresponding vulnerability code slice with the optimized patch code, compile verification is performed based on the compiled version and environment of the smart contract; After the compilation verification is passed, the repair result of the smart contract is obtained.

7. The method according to any one of claims 1 to 6, characterized in that: After repairing the smart contract using the optimized patch code and obtaining the repair result of the smart contract, the method further includes: Using a vulnerability detector to perform vulnerability detection on the repair result of the smart contract; wherein the vulnerability detector includes: Slither or GPTScan; If the vulnerability detector does not detect a vulnerability in the repair result of the smart contract, the output repair task is completed.

8. An automatic smart contract vulnerability repair system based on vulnerability reports, characterized in that: The system comprises: Dictionary construction module, used to build a slice data dictionary containing contract key elements and vulnerability code slices; A vulnerability location module is used to obtain key contract elements involved in structured repair for a smart contract based on the vulnerability description of the smart contract, and obtain relevant vulnerability code slices in combination with the slice data dictionary; A patch generation module, used to generate patch codes for the relevant vulnerability code slices; The patch optimization module is used to optimize the patch code and use the optimized patch code to repair the smart contract to obtain a repair result of the smart contract.

9. An electronic device, characterized in that: The electronic device includes: a processor and a memory storing computer program instructions; when the processor executes the computer program instructions, it implements an automatic repair method for smart contract vulnerabilities based on vulnerability reports as described in any one of claims 1-7.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer program instructions, which, when executed by a processor, implement an automated smart contract vulnerability repair method based on vulnerability reporting as described in any one of claims 1 to 7.

Citation Information

Patent Citations

  • Intelligent contract vulnerability repairing method and device and medium

    CN115310100A

  • Intelligent contract vulnerability automatic repair system and method based on byte code rewriting

    CN116522342A

  • Intelligent contract vulnerability mining detection method based on simulated attack

    CN117150501A

  • Intelligent contract threat transaction sequence generation method and system

    CN118709188A

  • Blockchain security

    US20200372154A1