Intelligent contract functional vulnerability detection method and system based on invariant verification

Through the smart contract functional vulnerability detection method based on invariant verification, the large language model and dynamic fuzz testing principle are used to automatically analyze the smart contract source code, solving the problems of manual dependence and inefficiency in the existing technology, and achieving efficient Smart contract vulnerability detection.

CN119004476AActive Publication Date: 2024-11-22ZHEJIANG UNIV
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202411040280.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-31
Publication Date
2024-11-22
Estimated Expiration
2044-07-31

AI Technical Summary

Technical Problem

The existing functional vulnerability detection methods of smart contracts rely on manual auditing, which are inefficient and have low automation, making it difficult to effectively detect logical relationships in smart contracts, resulting in low analysis efficiency and low automation.

Method used

Using an invariant verification method, pre-analyze the core function code of the smart contract through a large language model, generate an invariant inspector template, combine the dynamic fuzzy testing principle verification model, automate the analysis and verification of the smart contract source code, detect potential vulnerabilities and Output functional vulnerability detection results.

Benefits of technology

It realizes efficient automation of functional vulnerability detection of smart contracts, reduces dependence on humans, significantly improves detection efficiency, and ensures the verifiability and authenticity of detection results through dynamic verification.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119004476A_ABST
    Figure CN119004476A_ABST
Patent Text Reader

Abstract

The invention provides a smart contract functional vulnerability detection method and system based on invariant verification. The method comprises the following steps: defining a smart contract functional vulnerability category to be detected; inputting an intelligent contract source code file to be detected and preprocessing; pre-analyzing potential vulnerabilities and vulnerability types thereof by using a large language model; detecting key variables or key statements in the function codes with potential vulnerabilities by using a large language model, generating an invariant checker by using the key variables and the key statements, and inserting the checker into the source code of the smart contract; based on a dynamic fuzzy test principle, establishing a verification model for the invariant checker; and verifying the source code inserted with the invariant checker by using the verification model, and outputting a detection result. According to the method, the intelligent contract is efficiently and automatically analyzed, the technical challenge that the analysis difficulty of the logical relationship in the intelligent contract code is large is solved, and the efficiency of detecting the functional vulnerability of the intelligent contract is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of smart contract vulnerability detection, and in particular to a smart contract functional vulnerability detection method and system based on invariant verification. Background Art

[0002] Smart contracts are applications that run on blockchain platforms. Smart contracts define a series of rules that are automatically enforced through code. Once a smart contract is published on a blockchain platform, it cannot be deleted, and the interaction between users and smart contracts is tamper-proof and irreversible. Therefore, smart contracts have a wide range of application scenarios in areas such as digital currency transactions and supply chain management.

[0003] However, if there are exploitable vulnerabilities in smart contracts, the economic losses caused by these vulnerabilities will be irreversible. Among the existing smart contract vulnerabilities that cause actual harm, functional vulnerabilities account for the vast majority. These functional vulnerabilities are closely related to the business transaction rules defined in the smart contract, and they are very difficult to detect. The existing detection method is often to analyze the smart contract code and related businesses through manual auditors, extract a set of logical dependencies related to functional vulnerability analysis, and then verify them one by one. Although the existing technology can detect some functional vulnerabilities, it relies heavily on manual labor, resulting in low analysis efficiency and low degree of automation. Therefore, in the field of smart contract security, there is an urgent need for a method and system that can automatically analyze and verify the logical relationships in smart contracts, thereby realizing the detection of functional vulnerabilities in smart contracts, which is of great value for improving the security of smart contracts. Summary of the invention

[0004] In view of the above background, the present invention aims to provide a method and system for detecting functional vulnerabilities of smart contracts based on invariant verification.

[0005] In order to achieve the above-mentioned object of the invention, the present invention provides the following technical solutions:

[0006] The present invention proposes a method for detecting functional vulnerabilities of smart contracts based on invariant verification, comprising the following steps:

[0007] S1, defines the type of smart contract functional vulnerability to be detected;

[0008] S2, input the smart contract source code to be analyzed and preprocess it into multiple smart contract core function codes;

[0009] S3, based on the functional vulnerability types of the smart contracts to be detected, use the large language model to pre-analyze the potential vulnerabilities and vulnerability types of the core function codes of each smart contract;

[0010] S4, using the core function code with potential vulnerabilities in the large language model to detect, obtain key variables or key statements therein, select an invariant checker template according to the vulnerability type of the potential vulnerability, fill the key variables or key statements into the invariant checker template, generate an invariant checker, and insert the invariant checker into the smart contract source code;

[0011] S5, establish a verification model for the invariant checker, use the verification model to analyze the smart contract source code with the invariant checker inserted and verify whether the pre-analyzed potential vulnerability in the smart contract source code exists. If so, output the potential vulnerability and its vulnerability type as the functional vulnerability detection result.

[0012] Preferably, step S5 includes the following sub-steps:

[0013] 1) Create two sets: contract execution operation and contract status pair set C, and initial contract status set C s ; Define and initialize the intermediate variable s mut ,t mut , and set the verification time for the verification model;

[0014] 2) Randomly obtain a program execution operation transaction and program status pair (s, t) from the contract execution operation and contract status pair set C, where s represents the contract status and t represents the contract execution operation

[0015] 3) Generate a random number between 0 and 1. If the generated random number is greater than the set probability P, the contract execution operation t is mutated and the mutated value is assigned to the variable t. mut , and keep the state s unchanged, and assign the contract state s to the variable s mut ; Otherwise, from the initial contract state set C s Randomly obtain the next contract state and assign it to variable s mut , and keep transaction t unchanged, assign the contract execution operation t to variable t mut ;

[0016] 4) With variable s mut is the executed contract status, variable t mut For program execution operations, execute the smart contract source code with the invariant checker inserted to obtain the new contract state s′ after execution;

[0017] 5) If this round of program execution is worth including, then (s mut , t mut ) is added to the set C as a new contract execution operation and contract state pair; if this round of program execution is worth including and the new contract state s′ is not in the initial contract state set C sThen add the new contract state s′ to the set C s middle;

[0018] The criterion for judging whether a program execution is worth including is whether this round of program execution increases the program coverage. If the program coverage is increased, then this round of program execution is worth including, otherwise not.

[0019] 6) Repeat the above sub-steps 2)-5) until a conflict occurs in the invariant in the smart contract source code, or the repeated execution time of sub-steps 2)-5) exceeds the running verification time; if a conflict occurs in the invariant in the smart contract source code, trace the core function code related to the invariant checker inserted in the source code, and the potential vulnerability in the core function code is an existing functional vulnerability, otherwise the potential vulnerability in the core function code does not exist.

[0020] The present invention also proposes a smart contract functional vulnerability detection system based on invariant verification, which is used to implement the above-mentioned smart contract functional vulnerability detection method.

[0021] Compared with the prior art, the present invention has the following beneficial effects:

[0022] (1) The present invention proposes a method and system for detecting functional vulnerabilities in smart contracts based on invariant verification. The method and system proposed in the present invention can efficiently and automatically analyze smart contracts, reduce the dependence of the smart contract functional vulnerability analysis process on manual labor, and greatly improve the efficiency of smart contract functional vulnerability detection.

[0023] (2) The present invention proposes a functional vulnerability detection invariant automatic generation technology based on an invariant checker template. This technology summarizes the audit experience of excellent manual auditors, designs an invariant checker template, and uses a large language model to perform reasoning analysis on the smart contract code to automatically generate an effective smart contract invariant checker. This technology combines the historical experience summarized by humans and the uniqueness of the code in different smart contracts to generate an invariant checker, solving the technical challenge of the difficulty of analyzing the logical relationship in the smart contract code.

[0024] (3) The present invention proposes an invariant checker verification model based on the principle of dynamic fuzz testing. The verification model continuously generates valuable test sequences for analyzing functional vulnerabilities of smart contracts by analyzing and mutating states and state transaction pairs that are worth mining during program execution, thereby realizing the verification of the invariant checker. The dynamic execution verification method ensures that the vulnerabilities detected and analyzed by the present invention are verifiable and real. BRIEF DESCRIPTION OF THE DRAWINGS

[0025] Figure 1This is a flow chart of a method for detecting functional vulnerabilities in smart contracts based on invariant verification according to the present invention;

[0026] Figure 2 A flow chart of the verification model execution based on dynamic fuzzy testing of the present invention;

[0027] Figure 3 This is a general architecture diagram of a smart contract functional vulnerability detection system based on invariant verification of the present invention. DETAILED DESCRIPTION

[0028] The present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be pointed out that the embodiments described below are intended to facilitate the understanding of the present invention and do not have any limiting effect on the present invention.

[0029] The present invention provides a method for detecting functional vulnerabilities in smart contracts based on invariant verification. The specific process is as follows: Figure 1 As shown, the specific implementation of each step is as follows:

[0030] Step 0: Define the type of smart contract functional vulnerability that the present invention intends to detect. Design an invariant checker template based on the characteristics of the smart contract functional vulnerability.

[0031] Specifically, there are ten types of vulnerabilities to be detected in this embodiment, including contract token manipulation vulnerabilities caused by automatic exchange, contract token value operation vulnerabilities caused by non-automatic exchange, contract token unauthorized transfer vulnerabilities, contract token authorization mechanism logic error vulnerabilities, checkpoint save order error vulnerabilities, contract token update calculation order error vulnerabilities, contract token allocation calculation mechanism error vulnerabilities, voting mechanism manipulable vulnerabilities, contract token illegal locking vulnerabilities, and locking control mechanism error vulnerabilities. The vulnerability types can also be designed according to the needs of technicians in this field.

[0032] Invariants refer to mathematical constraints that always hold true at specific lines of code during the normal operation of smart contracts. These constraints include the numerical relationship between variables and the range of the variables' numerical values. The invariant checker checks the invariants in smart contracts to determine whether they hold true. The invariant checker template is the invariant checker after removing the variables to be tested and the program locations to be inserted. These variables to be tested usually have different symbols in different smart contract codes.

[0033] Specifically, this embodiment designs six types of checker templates, including: an invariant checker template for contract token value change detection, an invariant checker template for contract token exchange rate change detection, an invariant checker template for contract token quantity change detection, an invariant checker template for statement execution order detection, an invariant checker template for contract token allocation mechanism security detection, and an invariant checker template for state security detection. These six types of invariant checker templates can generate invariant checkers after filling in the variables to be tested and determining the program position to be inserted.

[0034] Among them, the invariant checker used to detect changes in contract token value can detect contract token manipulation vulnerabilities caused by automatic exchange; the invariant checker used to detect changes in contract token exchange rate can detect contract token value operation vulnerabilities caused by non-automatic exchange; the invariant checker used to detect changes in contract token quantity can detect unauthorized transfer vulnerabilities, contract token authorization mechanism logic error vulnerabilities and contract token illegal locking vulnerabilities; the invariant checker used for statement execution order detection can detect checkpoint saving order error vulnerabilities and contract token update calculation order error vulnerabilities; the invariant checker used for contract token allocation mechanism security detection can detect contract token allocation calculation mechanism error vulnerabilities; the invariant checker used for state security detection can detect voting mechanism manipulable vulnerabilities.

[0035] Step 1: Get the smart contract source code file and perform preprocessing operations.

[0036] Specifically, the input can be a smart contract source code file or an on-chain address. If the input is a source code file, preprocessing analysis is performed directly. If the input is an on-chain address, the smart contract source code file corresponding to the address is crawled and downloaded, and then preprocessing analysis is performed.

[0037] Specifically, the preprocessing operations of smart contract codes include filtering analysis, code division and establishing mapping relationships. The filtering analysis operation uses heuristic rules to filter smart contract codes that are not related to vulnerability analysis and retain the core code of the smart contract. Code division is to divide the core code of the smart contract by function as the granularity unit to obtain the smart contract function code for subsequent analysis. Establishing a mapping relationship means establishing a three-level mapping relationship table of "project-contract-function" between each smart contract function code, the smart contract to which the function code belongs, and the smart contract project to which the smart contract belongs.

[0038] Step 2: Use the large language model to analyze the smart contract code and pre-analyze potential vulnerability functions and their vulnerability types.

[0039] Specifically, this step proposes a multi-round query strategy for a large language model for pre-analyzing potential vulnerability functions. The multi-round query strategy guides the large language model to gradually analyze and reason about the smart contract function code, so that the large language model can make a reasonable response. The specific multi-round query includes the following three rounds:

[0040] (1) The first round is to issue task instructions to the large language model, informing it that it will perform tasks such as reading, understanding, auditing, and security analysis of smart contract codes;

[0041] (2) The second round is to input the core function code of the smart contract to be analyzed into the large language model, issue query instructions to the large language model, let the large language model analyze the meaning of this code and the specific business covered, and let the large language model format the output. The query instructions in the second round are designed based on common smart contract businesses. If the response of the large language model in the second round of inquiry is an empty set, it will not enter the third round of inquiry, and it is deemed that there are no potential vulnerabilities in the core function code.

[0042] (3) The third round is to input the smart contract code to be analyzed and the information replied by the big language model in the second round of inquiry to the big language model, issue inquiry instructions to the big language model, let the big language model analyze the potential vulnerability types in this section of code, the potential vulnerability types must be one or more of the smart contract functional vulnerability types to be detected in step 0, and let the big language model format the output. The inquiry instructions in the third round are designed based on the smart contract functional vulnerability types to be detected defined in step 0.

[0043] If the response of the large language model in the third round of inquiries is not an empty set, the next step of analysis is performed on the function code and potential vulnerabilities. Otherwise, the analysis of the function code is terminated.

[0044] Step 3: Use the large language model to select the required invariant checker template for the key variables or key statements in the potential vulnerability function according to the potential vulnerability type determined in step 2, and fill the key variables and key statements into the invariant checker template to generate an invariant checker, and insert the invariant checker into the smart contract source code.

[0045] Specifically, if multiple potential vulnerabilities are obtained through analysis in step 2, then an invariant checker template is selected for each potential vulnerability, and the key variables or key statements in the corresponding potential vulnerability function are filled into the corresponding invariant checker template to obtain multiple smart contract source codes with invariant checkers inserted, and the source codes are respectively executed and analyzed in subsequent steps. The method of inserting the invariant checker into the smart contract source code is a well-known method in the art and will not be described in detail in the present invention.

[0046] Specifically, in this step, the large language model query strategy is used to identify key variables and statements. The multi-round query strategy guides the large language model to perform variable identification and statement analysis on the smart contract function code, so that the large language model can extract the correct information. The specific multi-round query includes the following two rounds:

[0047] (1) The first round is to issue task instructions to the large language model, informing it that it will perform variable identification and statement analysis in the smart contract code;

[0048] (2) The second round is to input the smart contract code to be analyzed into the large language model and issue query instructions to the large language model: let the large language model extract the key variables and key statements required by the query instructions, and let the large language model format the output.

[0049] Specifically, the key variables and key statements required by different invariant checker templates are different. The invariant checker template used for contract token value change detection needs to fill in the variables used to store the value of the token to be detected; the invariant checker template used for contract token exchange rate change detection needs to fill in the variables used to store the number of input tokens and the variables used to store the number of output tokens during the exchange process; the invariant checker template used for contract token quantity change detection needs to fill in the variables used to store the number of token changes, the variables used to store the addresses of stakeholders in this round of transactions, and the variables used to represent authorization or locking behaviors; the invariant checker template used for statement execution order detection needs to fill in two key statements for the execution order to be checked; the invariant checker template used for contract token allocation mechanism security detection needs to fill in the variables used to store the allocation shares to be detected, the variables used to store the tokens to be converted into allocation shares, and the variables used to store the addresses of stakeholders in this round of transactions; the invariant checker template used for state security detection needs to fill in the variables used to store the state to be detected and the statements that trigger state changes.

[0050] Step 4: Based on the principle of dynamic fuzz testing, a verification model for the invariant checker is established.

[0051] Specifically, the verification model generates a variety of different input execution programs to cover various situations during program execution and comprehensively verify the invariant checker. The establishment and execution process of the verification model includes the following sub-steps, such as Figure 2 As shown:

[0052] Sub-step 401, define and initialize the model. This sub-step creates two sets: contract execution operation and contract state pair set C and initial contract state set C s , define and initialize the intermediate variable s mut ,t mut, and set the verification time for the verification model.

[0053] The contract execution operation and contract state pair set C and the initial contract state set C s Specifically, one or more possible contract execution operations and contract state pairs (s, t) that may appear during the execution of the smart contract program are pre-set to form a set C, and one or more possible initial contract states of the program execution are pre-set to form a set C s . Where s represents the contract status during program execution, t represents the contract execution operation, and a contract execution operation means executing a contract program once after inputting a set of input data.

[0054] Sub-step 402, select a contract execution operation and a contract status pair to start this round of execution. Specifically, a contract execution operation and a contract status pair (s, t) is randomly obtained from the set C.

[0055] Sub-step 403, input mutation strategy selection. Specifically, generate a random number between 0 and 1. If this random number is greater than the set probability P, execute mutation strategy 1: mutate the contract execution operation t to generate t mut , and keep the contract state s unchanged, and assign s to s mut ; Otherwise, execute mutation strategy 2: from the initial contract state set C s Get the next contract status s mut , and keep the contract execution operation t unchanged, assign t to t mut The mutation refers to bit-by-bit reversal, integer addition and subtraction arithmetic operations, sequence content replacement, etc. of the program execution operation sequence.

[0056] Sub-step 404, execute the contract procedure. Specifically, in the contract state s mut Execute the contract execution operation on t mut , obtain the new contract state s′ after execution and the key information f observed during this round of program execution. The key information f refers to the characteristic information that can reflect the execution status of this round of program, including the change information of program state and the change information of memory data.

[0057] Sub-step 405, evaluate the result of this round of loop. Specifically, determine whether this round of program execution is worth including based on the key information f observed during this round of program execution. If the program execution is worth including, then the contract execution operation and contract status pair (s mut , t mut ) is added to the set C. If this round of execution is worth including and the new contract state s′ is not in the set C s Then add the contract state s′ to the set C sSpecifically, the criterion for judging whether a program execution is worth collecting is whether this round of program execution increases the program coverage. The program coverage is used to describe the proportion and degree of source code tested in a program. If the program coverage is increased, then this round of execution is worth collecting, otherwise not.

[0058] Sub-step 406, repeating the above sub-steps 402-405 until an invariant conflict occurs, or the repetition time of sub-steps 402-405 exceeds the running verification time. The invariant conflict refers to the invariant defined in the invariant checker inserted into the source code conflicting during the program execution.

[0059] Step 5: Use the verification model to verify the source code with the invariant checker inserted.

[0060] Specifically, if the invariant checker triggers an exception during the verification process, it proves that there are potential vulnerabilities in the contract code that were inferred during the pre-analysis. Otherwise, the verification is performed through the verification model according to the above sub-steps 401-406.

[0061] Based on the same inventive concept, this embodiment also provides a smart contract functional vulnerability detection system based on invariant verification, such as Figure 3 As shown, it specifically includes the following modules:

[0062] Input module: This module is used to read the source code of the smart contract project to be analyzed.

[0063] Specifically, if the input content is a source code file, it is read directly; if the input content is a smart contract address, the smart contract source code file at the address on the chain is downloaded and then read.

[0064] Data preprocessing module: This module is used to filter and analyze the smart contract code, divide the code and establish a mapping relationship, and extract the smart contract core function code required for subsequent execution and analysis.

[0065] Vulnerability pre-analysis module: This module uses a large language model to conduct multiple rounds of inquiries on the core function code of the smart contract, gradually analyzing and reasoning about potential vulnerabilities and their types.

[0066] Invariant checker generation module: This module uses a large language model to analyze key variables or key statements in the core function code with potential vulnerabilities, and fills these key variables and key statements into the invariant checker template to generate an invariant checker, which is then inserted into the smart contract code.

[0067] Verification module: This module establishes a verification model for the invariant checker based on the principle of dynamic fuzz testing, and uses the established verification model to verify the invariant checker to verify whether the potential vulnerabilities pre-analyzed in the smart contract code exist.

[0068] Output module: This module is used to output the vulnerability results after verification.

[0069] For the system embodiment, since it basically corresponds to the method embodiment, the relevant parts can refer to the partial description of the method embodiment, and the implementation methods of the remaining modules will not be repeated here. The system embodiment described above is only schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of the present invention. Ordinary technicians in this field can understand and implement it without paying creative work.

[0070] The embodiments of the system of the present invention can be applied to any device with data processing capabilities, and the device with data processing capabilities can be a device or apparatus such as a computer. The system embodiments can be implemented by software, or by hardware or a combination of software and hardware. Taking software implementation as an example, as a device in a logical sense, the corresponding computer program instructions in the non-volatile memory are read into the memory by the processor of any device with data processing capabilities and run.

[0071] The embodiments described above provide a detailed description of the technical solutions and beneficial effects of the present invention. It should be understood that the above are only specific embodiments of the present invention and are not intended to limit the present invention. Any modifications, supplements and equivalent substitutions made within the scope of the principles of the present invention should be included in the protection scope of the present invention.

Claims

1. A method for detecting functional vulnerabilities in smart contracts based on invariant verification, characterized in that: The following steps are involved: S1: Define the type of smart contract functional vulnerability to be detected; S2: Input the smart contract source code to be analyzed and preprocess it into multiple smart contract core function codes; S3: Based on the functional vulnerability types of smart contracts to be detected, use the large language model to pre-analyze the potential vulnerabilities and vulnerability types of the core function codes of each smart contract; S4: Use the core function code with potential vulnerabilities in the large language model to detect, obtain key variables or key statements therein, select an invariant checker template according to the vulnerability type of the potential vulnerability, fill the key variables or key statements into the invariant checker template, generate an invariant checker, and insert the invariant checker into the smart contract source code; S5: Establish a verification model for the invariant checker, use the verification model to analyze the smart contract source code with the invariant checker inserted and verify whether the pre-analyzed potential vulnerability in the smart contract source code exists. If so, output the potential vulnerability and its vulnerability type as the functional vulnerability detection result.

2. The method for detecting functional vulnerabilities of smart contracts based on invariant verification according to claim 1 is characterized in that: In step S1, the types of functional vulnerabilities of the smart contract to be detected include: token value operation vulnerabilities caused by automatic token exchange, token value operation vulnerabilities caused by non-automatic token exchange, unauthorized token transfer vulnerabilities, token authorization mechanism logic error vulnerabilities, checkpoint save order error vulnerabilities, contract token update calculation order error vulnerabilities, contract token allocation calculation mechanism error vulnerabilities, voting mechanism manipulable vulnerabilities, contract token illegal locking vulnerabilities and locking control mechanism error vulnerabilities.

3. The method for detecting functional vulnerabilities of smart contracts based on invariant verification according to claim 1 is characterized in that: In step S2, the preprocessing includes filtering analysis and code division; the filtering analysis uses heuristic rules to filter the smart contract codes that are not related to the vulnerability analysis and retain the core code of the smart contract; The code division is to divide the core code of the smart contract into functions as granularity units to obtain multiple smart contract core function codes.

4. The method for detecting functional vulnerabilities of smart contracts based on invariant verification according to claim 1 is characterized in that: In step S3, the pre-analysis is to use a multi-round query strategy to guide the large language model to analyze each smart contract core function code one by one to determine whether there is a type of smart contract functional vulnerability to be detected in the code; the multi-round query includes the following three rounds: The first round is to define the large language model to perform reading, understanding, auditing and security analysis of smart contract codes; The second round is to input the core function code of the smart contract to be analyzed into the large language model, and issue a query instruction to the large language model, which guides the large language model to analyze the meaning of this code and the specific business covered and output it; The third round is to input the core function code of the smart contract to be analyzed and the information replied by the big language model in the second round of inquiry to the big language model, and issue inquiry instructions to the big language model. The inquiry instructions are designed based on the functional vulnerability types of the smart contract to be detected defined in step S1, including instructing the big language model to analyze the potential vulnerabilities and potential vulnerability types in this section of the code and output them. The potential vulnerability types are one or more of the functional vulnerability types of the smart contract to be detected.

5. The method for detecting functional vulnerabilities of smart contracts based on invariant verification according to claim 1 is characterized in that: In step S4, the use of the large language model for key variables or key statements in the core function code with potential vulnerabilities is specifically: using a large language model query strategy for identifying key variables and statements, guiding the large language model to perform variable identification and statement analysis on the core function code of the smart contract, so that the large language model extracts the key variables or key statements in the core function code.

6. The method for detecting functional vulnerabilities of smart contracts based on invariant verification according to claim 5 is characterized in that: The large language model query strategy for identifying key variables and sentences includes the following two rounds: The first round is to define the large language model that will perform variable recognition and statement analysis in the smart contract code; The second round is to input the core function code of the smart contract that needs to be analyzed into the large language model, and issue query instructions to the large language model, which include instructing the large language model to extract the required variables and statements and output them.

7. The method for detecting functional vulnerabilities of smart contracts based on invariant verification according to claim 5 or 6, characterized in that: In step S4, the key variables or key statements in the potential vulnerability function are confirmed according to the type of the invariant checker template: if the invariant checker template is an invariant checker template for detecting changes in the value of contract tokens, the key variables or key statements are variables for storing the value of the token to be detected; if the invariant checker template is an invariant checker template for detecting changes in the exchange rate of contract tokens, the key variables or key statements are variables for storing the number of input tokens and variables for storing the number of output tokens during the exchange process; if the invariant checker template is an invariant checker template for detecting changes in the number of contract tokens, the key variables or key statements are variables for storing the number of token changes and variables for storing the benefits of this round of transactions. variables for the addresses of stakeholders and variables for indicating authorization or locking behaviors; if the invariant checker template is an invariant checker template for statement execution order detection, the key variables or key statements are two key statements for checking the execution order; if the invariant checker template is an invariant checker template for contract token allocation mechanism security detection, the key variables or key statements are variables for storing the allocated shares to be detected, variables for storing the tokens to be converted into the allocated shares, and variables for storing the addresses of stakeholders in this round of transactions; if the invariant checker template is an invariant checker template for state security detection, the key variables or key statements are variables for storing the states to be detected and statements for inducing state changes.

8. The method for detecting functional vulnerabilities of smart contracts based on invariant verification according to claim 1, characterized in that: In step S5, the establishment of a verification model for the invariant checker, using the verification model to analyze the smart contract source code into which the invariant checker is inserted and verifying whether the pre-analyzed potential vulnerabilities in the smart contract source code exist, includes the following sub-steps: 1) Create two sets: contract execution operation and contract status pair set C, and initial contract status set C s ; Define and initialize the intermediate variable s mut ,t mut , and set the verification time for the verification model; 2) Randomly obtain a program execution operation transaction and program status pair (s, t) from the contract execution operation and contract status pair set C, where s represents the contract status and t represents the contract execution operation 3) Generate a random number between 0 and 1. If the generated random number is greater than the set probability P, the contract execution operation t is mutated and the mutated value is assigned to the variable t. mut , and keep the state s unchanged, and assign the contract state s to the variable s mut ; Otherwise, from the initial contract state set C s Randomly obtain the next contract state and assign it to variable s mut , and keep transaction t unchanged, assign the contract execution operation t to variable t mut ; 4) With variable s mut is the executed contract status, variable t mut For program execution operations, execute the smart contract source code with the invariant checker inserted to obtain the new contract state s′ after execution; 5) If this round of program execution is worth including, then (s mut , t mut ) is added to set C as a new contract execution operation and contract state pair; If this round of program execution is worth including and the new contract state s′ is not in the initial contract state set C s Then add the new contract state s′ to the set C s middle; The criterion for judging whether this round of program execution is worth including is whether this round of program execution increases the program coverage. If so, this round of program execution is worth including, otherwise not. 6) Repeat the above sub-steps 2)-5) until a conflict occurs in the invariant in the smart contract source code, or the repeated execution time of sub-steps 2)-5) exceeds the running verification time; if a conflict occurs in the invariant in the smart contract source code, trace the core function code related to the invariant checker inserted in the source code, and the potential vulnerability in the core function code is an existing functional vulnerability, otherwise the potential vulnerability in the core function code does not exist.

9. A smart contract functional vulnerability detection system based on invariant verification, used to implement the method described in claim 1, characterized in that: The system specifically comprises: Input module, used to read the smart contract source code to be analyzed; The data preprocessing module is used to preprocess the smart contract source code to obtain multiple smart contract function codes; Vulnerability pre-analysis module, which uses a large language model to pre-analyze key variables or key statements in the core function code that have potential vulnerabilities; An invariant checker generation module, used to fill the key variables and key statements into the invariant checker template, generate the invariant checker, and insert the checker into the smart contract source code; A verification module, used to establish a verification model for the invariant checker, and use the established verification model to analyze the smart contract source code into which the invariant checker is inserted and verify whether the pre-analyzed potential vulnerabilities in the smart contract code exist; The output module is used to output the verified functional vulnerability detection results.

Citation Information

Patent Citations

  • Protocol security attribute automatic extraction method and system for Ethereum smart contract

    CN114331396A

  • Intelligent contract security auditing system and method based on formal verification

    CN118277284A

  • Method for improving recognition ratio of vehicle license plate employing depth information of image

    KR102272295B1

  • Automated method and system for finding vulnerabilities in a program using fuzzing

    US20230214318A1

  • Smart contract vulnerability detection method and system, and electronic device

    WO2024131508A1