Intelligent contract functional vulnerability detection method and system based on large language model

Through the smart contract functional vulnerability detection method based on the large language model, combined with the dual-perspective analysis from the perspectives of auditors and attackers, the problem of difficult detection of functional vulnerabilities in smart contracts is solved, and efficient and accurate vulnerability detection and analysis are achieved to make up for it. The limitations of single-view detection capability are achieved.

CN119004475AActive Publication Date: 2024-11-22ZHEJIANG UNIV

Patent Information

Application Number
CN202411040279.6
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

Existing smart contract code detection technology is difficult to effectively detect functional vulnerabilities. Traditional detection methods rely on fixed rules and cannot effectively identify vulnerabilities related to high-level business logic in smart contracts.

Method used

Using a smart contract functional vulnerability detection method based on a large language model, the smart contract core function code is detected and analyzed by defining the vulnerability category to be detected and used by a dual-view agent from the auditor's perspective and the attacker's perspective, and combined with multiple rounds of inquiries Strategy and fine-tune datasets to enable detection and analysis of functional vulnerabilities.

Benefits of technology

It realizes efficient and reliable detection of functional vulnerabilities of smart contracts, maximizes the removal of false positives, maximizes the preservation of potential vulnerabilities, improves the accuracy of detection results and the emergence of large language models in contract vulnerabilities audits and exploitation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119004475A_ABST
    Figure CN119004475A_ABST
Patent Text Reader

Abstract

The invention provides an intelligent contract functional vulnerability detection method and system based on a large language model. The method comprises the following steps: defining a mainstream functional vulnerability category of an intelligent contract to be detected; inputting an intelligent contract item source code; filtering operation based on a contract level and a function level is carried out on the input source code, and a core function code related to the business is extracted; detecting and analyzing the core function codes one by one by using a large language model agent based on an auditor view angle; detecting and analyzing the core function codes one by one by using a large language model agent based on an attacker view angle; and establishing a fusion model of the double-view-angle agents, fusing results output by the agents of the two view angles, and outputting a final functional vulnerability detection result. The method and the system have strong understanding analysis capability and reasoning capability, and can realize efficient and reliable functional vulnerability detection on the smart contract.
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 a large language model. Background Art

[0002] With the development of blockchain technology, it has brought great impacts to the production and life of human society, especially in the financial field. Smart contracts are the basic pillar of blockchain, managing massive assets on the chain and playing a vital role in promoting various commercial transactions. However, if there are exploitable vulnerabilities in the code level of smart contracts, it will cause irreparable economic losses.

[0003] In order to prevent these smart contract vulnerabilities from affecting normal on-chain transactions, a large number of existing technologies have been applied to solve the security detection of smart contract codes, such as code static analysis technology, code dynamic analysis technology, etc. These technologies often use heuristic rules to detect vulnerabilities in the code, and can often only detect conventional vulnerabilities in smart contracts that are similar to traditional software vulnerabilities, such as integer overflow, reentrancy vulnerabilities, etc. However, the main vulnerabilities in smart contracts are often not these conventional vulnerabilities, most of which are functional vulnerabilities in smart contracts. Functional vulnerabilities are mainly closely related to the high-level business logic of smart contracts. Since such vulnerabilities are highly dependent on the understanding of code implementation, traditional detection technologies are difficult to detect using fixed rules.

[0004] Based on this, the development teams of the majority of smart contract projects are in urgent need of a reliable smart contract functional vulnerability detection method and system, which is of great significance for ensuring the security of smart contracts. Summary of the invention

[0005] In view of the above background, the present invention aims to provide a method and system for functional vulnerability detection of smart contracts based on a large language model. The method and system have strong understanding and analysis capabilities and reasoning capabilities, and can realize efficient and reliable functional vulnerability detection of smart contracts.

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

[0007] The present invention proposes a method for detecting functional vulnerabilities of smart contracts based on a large language model, comprising the following steps:

[0008] S1, defines the categories of smart contract functional vulnerabilities to be detected, including primary categories and secondary categories belonging to each primary category;

[0009] S2, input the source code of the smart contract project to be analyzed;

[0010] S3, performs contract-level filtering on the input source code to extract core contracts related to the business;

[0011] S4, performing a function-level filtering operation on the core contract to extract the core function code related to the business;

[0012] S5, using a large language model agent based on the auditor’s perspective to detect and analyze the core function codes one by one, and obtain the secondary categories to which the potential functional vulnerabilities in each core function code belong;

[0013] S6, using a large language model agent based on the attacker's perspective to detect and analyze the core function codes one by one, and obtain the first-level category to which the potential functional vulnerabilities in each core function code belong;

[0014] S7, establishes a fusion model of a dual-perspective agent, and uses the fusion model to fuse the secondary categories and primary categories output by the large language model agent based on the auditor's perspective and the large language model agent based on the attacker's perspective, and outputs the final functional vulnerability detection results.

[0015] As a preferred solution, in step S5, the use of a large language model agent based on the auditor's perspective to detect and analyze the core function codes one by one specifically includes the following steps:

[0016] S501: Build a fine-tuning dataset based on historical smart contract projects to enhance the auditing capabilities of large language models;

[0017] S502: Build a large language model agent based on the auditor's perspective, and use a fine-tuning dataset for enhancing the auditing capability of the large language model to fine-tune the agent;

[0018] S503: Based on a multi-round inquiry strategy based on business scenarios and implementation forms, a fine-tuned large language model agent is used to perform security analysis on the core function codes one by one;

[0019] S504: Determine potential functional vulnerabilities in the core function code of the smart contract based on the analysis results of the large language model agent, and determine the secondary category of the functional vulnerabilities based on the analysis results of the large language model agent.

[0020] As a further preferred solution, in step S503, the multi-round inquiry strategy based on the business scenario and implementation form includes the following three rounds, as follows:

[0021] The first round is to define the agent as the auditor role, and the agent does not need to answer;

[0022] The second round is to input the core function code to be analyzed to the agent, and ask the agent questions related to the smart contract business scenario to determine whether any of the preset business scenarios exists in the core function code; the format of the question related to the smart contract business scenario is "whether there is a certain business scenario in the code"; if the answer of the agent in this round is "yes", the business scenario existing in the core function code is obtained, and the third round of inquiry is executed, otherwise the inquiry ends;

[0023] The third round is to input the core function code that needs to be analyzed into the large language model, and ask the large language model questions related to the implementation form of the smart contract code; the format of the questions related to the implementation form of the smart contract is "Is there a certain business scenario in the code and does the code satisfy a certain form in business implementation"; if the answer of the intelligent agent is "yes", the business implementation form of the core function code is obtained.

[0024] As a further preferred solution, step S504 is specifically as follows: if the three rounds of the multi-round inquiry strategy based on the business scenario and implementation form are all executed and the answer of the intelligent agent in the third round is "yes", then it is judged that the input smart contract function code contains potential functional vulnerabilities, and based on the business scenario judged in the second round and the code implementation form judged in this round, the secondary category to which the potential functional vulnerability in the core function code belongs is judged; otherwise, there is no functional vulnerability in the core function code.

[0025] As a preferred solution, in step S6, the use of a large language model agent based on the attacker's perspective to detect and analyze the core function codes one by one specifically includes the following steps:

[0026] S601: Construct a fine-tuning dataset based on historical smart contract attack events to enhance the vulnerability exploitation capabilities of large language models;

[0027] S602: Build a large language model agent based on the attacker's perspective, and fine-tune the agent using a fine-tuning dataset used to enhance the vulnerability exploitation capability of the large language model;

[0028] S603: Based on the multi-round query strategy based on vulnerability features and attack exploitation, the fine-tuned intelligent agent is used to perform security analysis on the core function code of the smart contract;

[0029] S604: Determine potential functional vulnerabilities in the core function code of the smart contract based on the analysis results of the large language model agent, and determine the primary category to which the functional vulnerabilities belong based on the analysis results of the large language model agent.

[0030] As a further preferred solution, in step S603, the multi-round query strategy based on vulnerability features and attack exploitation includes the following three rounds, as follows:

[0031] The first round defines the agent as the attacker, and the agent does not need to answer;

[0032] The second round is to input the core function code of the smart contract that needs to be analyzed to the agent, and ask questions related to the characteristics of the smart contract vulnerabilities. The format of the question is "Is there any unsafe logic implementation in the code?" If the agent's answer in this round is "yes", the unsafe logic in the core function code is determined and the third round of inquiry is executed, otherwise the inquiry ends;

[0033] The third round is to input the core function code of the smart contract and the unsafe logic obtained in the second round into the large language model, and ask the agent questions related to the analysis of smart contract attacks and exploits; the format of the question is "Introduce a certain type of attack method with characteristics and attack process, and ask whether this type of attack can be used to exploit the related unsafe logic in the code"; if the agent's answer is "yes", the core function code is at risk of being attacked, and its attack method is determined.

[0034] As a further preferred solution, step S604 is specifically as follows: if all three rounds are executed and the answer of the intelligent agent in the third round is "yes", it is determined that the input smart contract function code contains potential functional vulnerabilities, and based on the attack method determined in the inquiry, the first-level category to which the potential functional vulnerability belongs is determined; otherwise, there is no functional vulnerability in the core function code.

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

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

[0037] (1) The present invention proposes a method and system for detecting functional vulnerabilities in smart contracts based on a large language model. The method and system proposed in the present invention do not rely on fixed detection rules, have strong understanding and analysis capabilities, and reasoning capabilities, and solve the technical problem that functional vulnerabilities in smart contracts are difficult to detect.

[0038] (2) The present invention provides a multi-perspective smart contract vulnerability analysis method, which innovatively combines the analysis results of the auditor and the attacker and performs a reasonable fusion analysis to achieve the goal of "maximizing the removal of false positives and maximizing the retention of potential vulnerabilities", thus making up for the shortcomings of the limited capabilities of single-perspective intelligent agents in vulnerability analysis.

[0039] (3) The present invention proposes a method for stimulating the emergence ability of a large language model from the perspective of smart contract auditing through fine-tuning, and a method for stimulating the emergence ability of a large language model from the perspective of smart contract vulnerability exploitation through fine-tuning. The large language model is innovatively guided to deeply learn representative audit reports and historical attack events, focusing on learning the annotated and extracted vulnerability features, impacts and exploitation methods, thereby improving the emergence ability of the large language model in contract vulnerability auditing and contract vulnerability exploitation, and better playing a positive role in the process of functional vulnerability detection.

[0040] (4) The present invention proposes a multi-round inquiry strategy that combines the auditor's thinking characteristics and a multi-round inquiry strategy that combines the attacker's thinking characteristics. Innovatively guiding the large language model to analyze the smart contract code according to the audit thinking mode of "identifying scenarios" and "checking code implementation" and the attack thinking mode of "identifying vulnerability characteristics" and "exploiting vulnerability characteristics" makes the large model's reasoning and judgment of vulnerabilities more reliable, and improves the accuracy of the identification results of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] Figure 1 This is a flow chart of the detection method of functional vulnerabilities in smart contracts based on a large language model;

[0042] Figure 2 Construct a flowchart for a large language model agent based on the auditor's perspective;

[0043] Figure 3 Construct a flowchart for a large language model agent from the attacker's perspective;

[0044] Figure 4 This is the overall design diagram of the smart contract functional vulnerability detection system based on the large language model. DETAILED DESCRIPTION

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

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

[0047] Step 0, define the mainstream functional vulnerability categories of smart contracts that the present invention intends to detect, including primary categories and secondary categories.

[0048] Specifically, there is a subordinate relationship between the first-level category and the second-level category. A first-level category can include multiple second-level categories, and a second-level category can only belong to one first-level category.

[0049] Specifically, the first-level categories of smart contract functional vulnerabilities in the present invention include: contract token value manipulation vulnerabilities, contract token transfer authorization missing vulnerabilities, calculation logic error vulnerabilities and control logic error vulnerabilities.

[0050] Specifically, the contract token value manipulation vulnerability in the present invention includes two secondary categories, namely: the contract token manipulation vulnerability caused by automatic exchange and the contract token value operation vulnerability caused by non-automatic exchange.

[0051] Specifically, the contract token transfer authorization missing vulnerability in the present invention includes two secondary categories, namely the contract token unauthorized transfer vulnerability and the contract token authorization mechanism logic error vulnerability.

[0052] Specifically, the computational logic error vulnerabilities in the present invention include three secondary categories, namely, checkpoint saving order error vulnerabilities, contract token update computation order error vulnerabilities, and contract token allocation computation mechanism error vulnerabilities.

[0053] Specifically, the control logic error vulnerability in the present invention includes three secondary categories, namely, the voting mechanism manipulable vulnerability, the contract token illegal locking vulnerability and the locking control mechanism error vulnerability.

[0054] The vulnerability categories included in the above-mentioned primary categories and secondary categories can also be designed according to the needs of technical personnel in this field.

[0055] Step 1: Enter the entire source code of the smart contract project.

[0056] Step 2: Perform contract-level analysis and filtering on the input smart contract project source code, extract the core contracts related to the business and input them into the next step.

[0057] Specifically, the smart contract code analysis and filtering operation based on the contract level refers to the use of heuristic rules to remove contract files that are irrelevant to vulnerability detection, including general library files, interface files, etc., and output a core file set. The general library files refer to library files that are commonly used in other smart contracts, such as SafeMath library files, Address library files, and SafeERC20 library files.

[0058] Step 3: Analyze and filter the core contracts at the function level, extract the core functions related to the business and input them into the next step.

[0059] Specifically, the smart contract code analysis and filtering operation based on the function level refers to the matching analysis of function names, parameters, and basic function contents using heuristic rules, and outputting a set of functions containing actual business functions. Specifically, the function set containing actual business functions refers to functions excluding basic calculation functions and interaction functions, which play a core role in realizing the functions of smart contract projects.

[0060] Specifically, the heuristic rules refer to abstract rules proposed based on the summary of manual function analysis experience, which can be automatically implemented using computer programs to execute rules for batch processing tasks.

[0061] Step 4: Use a large language model agent based on the auditor’s perspective to detect and analyze the core function codes one by one.

[0062] Specifically, this step includes the following sub-steps: Figure 2 As shown:

[0063] Sub-step 401, collect source code and audit reports of representative smart contract projects. The audit report refers to a review and analysis report of the source code of the smart contract project, including the business analysis implemented by the code, whether the code implementation is compliant and legal, the precise location of various vulnerabilities in the code, the types of various vulnerabilities in the code and their risk levels, the necessity and method of repairing various vulnerabilities. The representative smart contract project is preferably a large-scale smart contract project in history, and it has functional vulnerabilities.

[0064] Sub-step 402, the data information collected in sub-step 401 is structured to construct a fine-tuning data set for enhancing the auditing capability of the large language model. Specifically, this sub-step processes the source code and audit report into a format that can be used for large language model training, and each set of data includes: "Vulnerability code content", "Specific business description in vulnerability code", "Types of functional vulnerabilities covered", and "Description of the impact caused by the vulnerability".

[0065] Sub-step 403, build a large language model agent based on the auditor's perspective, and use the data set used to enhance the auditing ability of the large language model to perform model fine-tuning training. Specifically, the fine-tuning training is: for specific task scenarios, use the relevant fine-tuning data set to fine-tune the large language model, improve the ability of the large language model in performing certain tasks, and then stimulate the ability of the large language model in performing certain tasks.

[0066] Sub-step 404 proposes a multi-round inquiry strategy based on business scenarios and implementation forms to achieve security analysis of the core code of the smart contract; specifically, the multi-round inquiry strategy in this sub-step includes three rounds of inquiries that stimulate the emergence of the large language model. The principles of question design follow the audit thinking mode of "identifying scenarios" and "checking code implementation", as follows:

[0067] (1) The first round is to issue task instructions to the agent, requiring the agent to play the role of an auditor. This round is the task instruction issuance stage, and the agent does not need to answer.

[0068] (2) The second round is to input the smart contract code that needs to be analyzed to the agent, and ask the agent questions related to the smart contract business scenario; the format of the question is "Is there a certain business scenario in the code?", where "a certain business scenario" needs to be replaced with a preset smart contract business scenario, and the basic format of the large language model answer is "yes" or "no"; if the answer of the agent in this round is "yes", the subsequent third round will continue, otherwise the inquiry will end. This round of inquiry will ask the agent multiple questions according to the question format, replacing the "certain business scenario" in the format, and verifying whether there is any of the preset business scenarios in the core function code. Specifically, the preset smart contract business scenario is, for example: the function of this code is to transfer the contract token from address 1 to the business function of address 2.

[0069] (3) The third round is to input the smart contract code that needs to be analyzed into the large language model, and ask the large language model questions related to the implementation form of the smart contract; the format of the question is "Is there a certain business scenario in the code and whether the code satisfies a certain form in business implementation", where "certain business scenario" needs to be replaced with the business scenario obtained in the second round, and "certain form" needs to be replaced with the preset smart contract business implementation form. The basic format of the large language model's answer is "yes" or "no". This round of inquiry will also ask the intelligent agent multiple times according to the question format, changing the "certain form" in the format. Specifically, the preset smart contract code implementation form is, for example: before the code for the contract token sending operation, this section of code lacks a code statement for judging the legality of the number of tokens authorized to transfer in this round.

[0070] Sub-step 405, the large language model agent outputs the result based on the auditor's perspective. Specifically, the standard for the agent to judge the existence of functional vulnerabilities is: all three rounds are completed and the answer to the third round is "yes". In the inquiry strategy, the third round of inquiry is the deepening of the second round of inquiry. The second round of inquiry and the third round of inquiry have a one-to-one relationship. The second round of inquiry and the third round of inquiry are both based on the secondary category of vulnerability classification in question design. Therefore, the vulnerability classification granularity finally output by the agent is the secondary category of smart contract functional vulnerabilities.

[0071] Step 5: Use a large language model agent based on the attacker’s perspective to detect and analyze the core function codes one by one.

[0072] Specifically, this step includes the following sub-steps: Figure 3 As shown:

[0073] Sub-step 501, collects smart contract attack events and analysis reports that have actually occurred in history. The analysis report refers to the analysis of real attack events in history, including an introduction to this round of attacks, analysis of the attack process of the attacker, analysis of the vulnerability code of the attacked party, the impact of this round of attacks, and vulnerability repair methods and defense measures.

[0074] Sub-step 502, the data information collected in sub-step 501 is structured to construct a fine-tuning dataset for enhancing the vulnerability exploitation capability of the large language model. Specifically, this sub-step processes the attack events and analysis reports into a format that can be used for large model training. Each set of data includes: "attacked code content", "covered functional vulnerability types", "specific methods of exploiting vulnerabilities in this round of attacks", and "impacts caused by this round of attacks".

[0075] Sub-step 503, build a large language model agent based on the attacker's perspective, and use the data set used to enhance the vulnerability exploitation capability of the large language model to perform model fine-tuning training. The fine-tuning training method is the same as sub-step 403.

[0076] Sub-step 504 proposes a multi-round inquiry strategy based on vulnerability features and attack exploitation to achieve security analysis of the core code of the smart contract; specifically, the multi-round inquiry strategy in this sub-step includes three rounds of inquiries that stimulate the emergence of the large language model. The principle of question design follows the attack thinking mode of "identifying vulnerability features" and "exploiting vulnerability features", as follows:

[0077] (1) The first round is to issue task instructions to the large language model, requiring the large language model to play the role of an attacker. This round is the task instruction issuance stage, and the large language model does not need to answer.

[0078] (2) The second round is to input the smart contract code that needs to be analyzed into the large language model, and ask the large language model questions related to the characteristics of smart contract vulnerabilities; the format of the question is "Is there any unsafe logic implementation in the code?", and the basic format of the large language model's answer is "yes" or "no"; if the answer of the intelligent agent in this round is "yes", the subsequent third round will continue, otherwise the inquiry will end.

[0079] Unsafe logic example: This code lacks judgment on the identity of the token sender during contract token transfer.

[0080] (3) The third round is to input the smart contract code that needs to be analyzed into the large language model, and ask the large language model questions related to the analysis of smart contract attack exploitation; the format of the question is "Introduce a certain type of attack method with characteristics and attack process, and ask whether this type of attack can be used to exploit the relevant vulnerabilities in the code", and the basic format of the large language model's answer is "yes" or "no".

[0081] For example, in the third round of inquiries, there is an attack method that can bypass security checks through parameter passing, modify the address of the token sender, and then illegally transfer the victim's contract token to the attacker's address. Can this attack method be used to exploit the unsafe logic in this code?

[0082] Sub-step 505, the large language model agent outputs the result based on the attacker's perspective. Specifically, the standard for the agent to judge the existence of functional vulnerabilities is: all three rounds are completed and the answer to the third round is "yes", and the vulnerability classification granularity finally output by the agent is the first-level category of functional vulnerabilities in smart contracts. In the inquiry strategy, the third round of inquiry is the deepening of the second round of inquiry. The second round of inquiry and the third round of inquiry have a many-to-one relationship. The second round of inquiry is based on the second-level category of vulnerability classification in question design, and the third round of inquiry is based on the first-level category of vulnerability classification in question design. Therefore, the vulnerability classification granularity finally output by the agent is the first-level category of functional vulnerabilities in smart contracts.

[0083] Step 6: Establish a dual-perspective agent fusion model, fuse the output results of the large language model agent based on the auditor's perspective and the large language model agent based on the attacker's perspective, and output the final functional vulnerability detection results.

[0084] Specifically, the fusion model of the dual-view agent defines the following symbolic representation:

[0085] Symbol representation (1) is the first-level classification of smart contract functional vulnerabilities, denoted as: T.

[0086] Symbol representation (2) is the secondary classification of smart contract functional vulnerabilities, denoted as: t.

[0087] Specifically, the dual-perspective agent fusion model defines the following fusion rules, which include five fusion rules:

[0088] Rule (1) If t belongs to T, then add t to the candidate category set.

[0089] Rule (2): If t belongs to T, then t is added to the candidate category set, and other secondary categories belonging to T are also added to the candidate category set.

[0090] Rule (3) If t does not belong to T, skip it.

[0091] Rule (4) If T is empty and only t exists, skip it.

[0092] Rule (5) If t is empty and only T exists, skip it.

[0093] It should be noted that the number t is empty or T is empty, which means that the current core function code has no functional vulnerabilities for the large language model agent based on the auditor's perspective or for the large language model agent based on the attacker's perspective.

[0094] According to the characteristics of different vulnerabilities and their differences, the present invention adopts different fusion rules for different primary categories of vulnerabilities to achieve the purpose of "maximizing the removal of false positives and maximizing the retention of potential vulnerabilities". For the primary categories of token price manipulation vulnerabilities and token transfer authorization missing vulnerabilities, the dual-perspective intelligent agent fusion model selects fusion rules (2)(3)(4)(5); in other cases, the dual-perspective intelligent agent fusion model selects fusion rules (1)(3)(4)(5). The candidate category set obtained after fusion is the output functional vulnerability detection result, and the vulnerability classification granularity is the secondary category of smart contract functional vulnerabilities.

[0095] Based on the same inventive concept, this embodiment also provides a smart contract functional vulnerability detection system based on a large language model, such as Figure 4 As shown, it specifically includes the following modules:

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

[0097] Data preprocessing module: This module is used to analyze and filter the input smart contract project source code based on the contract level and function level, and extract the core function code related to the business.

[0098] Large model agent module based on the auditor's perspective: This module uses the fine-tuned and trained large language model agent based on the auditor's perspective to execute multi-round query strategies based on business scenarios and implementation forms to achieve security analysis of the core code of the smart contract.

[0099] Large model agent module based on attacker’s perspective: This module uses the fine-tuned large language model agent based on attacker’s perspective to execute multi-round query strategies based on vulnerability features and attack exploits to achieve security analysis of the core code of the smart contract.

[0100] Fusion analysis module: This module is used to fuse the output results of the large language model agent based on the auditor's perspective and the large language model agent based on the attacker's perspective, and output the final vulnerability detection results.

[0101] Furthermore, the system also includes the following modules:

[0102] Training data collection module: This module is used to collect source code and audit reports of representative smart contract projects, real smart contract attack events and analysis reports that have occurred in history, and build two fine-tuning datasets to enhance the auditing and vulnerability exploitation capabilities of large language models.

[0103] Model fine-tuning training module: This module is used to fine-tune the large language model agent based on the auditor's perspective and fine-tune the large language model agent based on the attacker's perspective.

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

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

[0106] 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 a large language model, characterized in that: The following steps are involved: S1, defines the categories of smart contract functional vulnerabilities to be detected, including primary categories and secondary categories belonging to each primary category; S2, input the source code of the smart contract project to be analyzed; S3, performs contract-level filtering on the input source code to extract core contracts related to the business; S4, performing a function-level filtering operation on the core contract to extract the core function code related to the business; S5, using a large language model agent based on the auditor’s perspective to detect and analyze the core function codes one by one, and obtain the secondary categories to which the potential functional vulnerabilities in each core function code belong; S6, using a large language model agent based on the attacker's perspective to detect and analyze the core function codes one by one, and obtain the first-level category to which the potential functional vulnerabilities in each core function code belong; S7, establishes a fusion model of a dual-perspective agent, and uses the fusion model to fuse the secondary categories and primary categories output by the large language model agent based on the auditor's perspective and the large language model agent based on the attacker's perspective, and outputs the final functional vulnerability detection results.

2. The method for detecting functional vulnerabilities in smart contracts based on a large language model according to claim 1, characterized in that: In step S1, the first-level categories include: contract token value manipulation vulnerabilities, contract token transfer authorization missing vulnerabilities, calculation logic error vulnerabilities and control logic error vulnerabilities.

3. The method for detecting functional vulnerabilities of smart contracts based on a large language model according to claim 1, characterized in that: In step S3, the contract-level filtering operation refers to: for the input source code, using heuristic rules to remove contract files that are not related to vulnerability detection, and the remaining source code after removal is the core contract related to the business; the contract files that are not related to vulnerability detection specifically include general library files and interface files.

4. The method for detecting functional vulnerabilities of smart contracts based on a large language model according to claim 1, characterized in that: In step S4, the function-level filtering operation refers to: using heuristic rules to analyze each function contained in the core contract in turn, and outputting a function set containing actual business functions, that is, the core function code related to the business.

5. The method for detecting functional vulnerabilities of smart contracts based on a large language model according to claim 1, characterized in that: In step S5, the use of the large language model agent based on the auditor's perspective to detect and analyze the core function codes one by one specifically includes the following steps: S501: A fine-tuning dataset is constructed based on historical smart contract projects to enhance the auditing capability of the large language model. In the fine-tuning dataset, a set of data is established for each functional vulnerability existing in the source code of the historical smart contract project. Each set of data includes: vulnerability code content, specific business description in the vulnerability code, types of functional vulnerabilities covered, and description of the impact caused by the vulnerability; S502: Build a large language model agent based on the auditor's perspective, and use a fine-tuning dataset for enhancing the auditing capability of the large language model to fine-tune the agent; S503: Based on the multi-round query strategy of business scenarios and implementation forms, the fine-tuned large language model agent is used to perform security analysis on the core function codes one by one; S504: Determine potential functional vulnerabilities in the core function code of the smart contract based on the analysis results of the large language model agent, and determine the secondary category of the functional vulnerabilities based on the analysis results of the large language model agent.

6. The method for detecting functional vulnerabilities in smart contracts based on a large language model according to claim 5, characterized in that: In step S503, the multi-round inquiry strategy based on the business scenario and implementation form includes the following three rounds, as follows: The first round is to define the agent as the auditor role, and the agent does not need to answer; The second round is to input the core function code to be analyzed to the agent, and ask the agent questions related to the smart contract business scenario to determine whether any of the preset business scenarios exists in the core function code; the format of the question related to the smart contract business scenario is "whether there is a certain business scenario in the code", where "a certain business scenario" needs to be replaced with the preset smart contract business scenario, and the basic format of the large language model answer is "yes" or "no"; if the answer of the agent in this round is "yes", the business scenario existing in the core function code is obtained, and the third round of inquiry is executed, otherwise the inquiry is terminated; The third round is to input the core function code to be analyzed into the large language model, and ask the large language model questions related to the implementation form of the smart contract code; the format of the questions related to the implementation form of the smart contract is "whether there is a certain business scenario in the code and whether the code satisfies a certain form in business implementation", where "certain business scenario" needs to be replaced with the business scenario obtained in the second round, and "certain form" needs to be replaced with the preset smart contract business implementation form. The basic format of the large language model's answer is "yes" or "no"; if the agent's answer is "yes", the business implementation form of the core function code is obtained, The step S504 is specifically as follows: if the three rounds of the multi-round query strategy based on the business scenario and implementation form are all executed and the answer of the intelligent agent in the third round is "yes", it is judged that the input smart contract function code contains potential functional vulnerabilities, and according to the business scenario judged in the second round and the code implementation form judged in this round, the secondary category to which the potential functional vulnerability in the core function code belongs is judged; Otherwise, there are no functional vulnerabilities in the core function code.

7. The method for detecting functional vulnerabilities of smart contracts based on a large language model according to claim 1, characterized in that: In step S6, the use of the large language model agent based on the attacker's perspective to detect and analyze the core function codes one by one specifically includes the following steps: S601: Based on historical smart contract attack events, a fine-tuning dataset is constructed to enhance the vulnerability exploitation capability of the large language model. In the fine-tuning dataset, a set of data is established for each round of attack in the historical smart contract attack events. Each set of data includes the content of the attacked code, the types of functional vulnerabilities covered, the specific methods of exploiting the vulnerabilities in this round of attacks, and the impact of this round of attacks; S602: Build a large language model agent based on the attacker's perspective, and fine-tune the agent using a fine-tuning dataset used to enhance the vulnerability exploitation capability of the large language model; S603: Based on the multi-round query strategy based on vulnerability features and attack exploitation, the fine-tuned intelligent agent is used to perform security analysis on the core function code of the smart contract; S604: Determine potential functional vulnerabilities in the core function code of the smart contract based on the analysis results of the large language model agent, and determine the primary category to which the functional vulnerabilities belong based on the analysis results of the large language model agent.

8. The method for detecting functional vulnerabilities in smart contracts based on a large language model according to claim 7, characterized in that: In step S603, the multi-round query strategy based on vulnerability features and attack exploitation includes the following three rounds, as follows: The first round defines the agent as the attacker, and the agent does not need to answer; The second round is to input the core function code of the smart contract that needs to be analyzed to the agent, and ask questions related to the characteristics of the smart contract vulnerabilities. The format of the question is "Is there any unsafe logic implementation in the code?" The basic format of the agent's answer is "yes" or "no". If the agent's answer in this round is "yes", the unsafe logic in the core function code is determined and the third round of inquiry is executed, otherwise the inquiry ends; The third round is to input the core function code of the smart contract that needs to be analyzed and the unsafe logic obtained in the second round into the large language model, and ask the agent questions related to the analysis of smart contract attack and utilization; the format of the question is "Introduce a certain type of attack method with characteristics and attack process, can this type of attack exploit the related unsafe logic in the code", where "Introduce a certain type of attack method with characteristics and attack process" needs to be replaced with the corresponding introduction according to the preset attack method, and the basic format of the agent's answer is "yes" or "no"; if the agent's answer is "yes", the core function code is at risk of being attacked, and its attack method is determined; The step S604 is specifically as follows: if all three rounds are completed and the answer of the intelligent agent in the third round is "yes", it is determined that the input smart contract function code contains potential functional vulnerabilities, and based on the attack method determined in the inquiry, the first-level category to which the potential functional vulnerability belongs is determined; otherwise, there is no functional vulnerability in the core function code.

9. The method for detecting functional vulnerabilities of smart contracts based on a large language model according to claim 2, characterized in that: In step S7, the dual-perspective agent fusion model is established, and the secondary categories and primary categories output by the large language model agent based on the auditor's perspective and the large language model agent based on the attacker's perspective are fused through the fusion model, specifically including: Initialize the candidate category set. For each core function code, the dual-view intelligent agent fusion model adopts different fusion rules according to different primary categories, and fuses the primary category of the functional vulnerability obtained in step S6 and the secondary category of the functional vulnerability obtained in step S5, and updates the candidate category set. The final candidate category set obtained after fusion is the output functional vulnerability detection result, specifically the secondary category of the functional vulnerability of the smart contract; The dual-perspective agent fusion model specifically includes the following fusion rules: Rule 1): If t belongs to T, then add t to the candidate category set; Rule 2): If t belongs to T, then add t to the candidate category set, and add other secondary categories belonging to T to the candidate category set; Rule 3): If t does not belong to T, skip it; Rule 4): If T is empty and only t exists, skip it; Rule 5): If t is empty and only T exists, skip it; In the above rules, T represents the first-level category of smart contract functional vulnerabilities, and t represents the second-level category of smart contract functional vulnerabilities; For vulnerabilities of the first-level category of contract token value manipulation vulnerabilities and contract token transfer authorization missing vulnerabilities, the fusion model of the dual-perspective intelligent agent adopts fusion rules 2), 3), 4) and 5); for vulnerabilities of the first-level category of computational logic error vulnerabilities and control logic error vulnerabilities, the fusion rules 1), 3), 4) and 5) are adopted.

10. A smart contract functional vulnerability detection system based on a large language model, used to implement the method described in claim 1, characterized in that: The system comprises: Input module, used to read the source code of the smart contract project to be analyzed; The data preprocessing module is used to perform analysis and filtering operations based on the contract level and function level, and further perform analysis and filtering operations at the function level to extract the core function codes related to the business; The large model agent module based on the auditor's perspective is used to detect and analyze the core function code from the auditor's perspective and obtain the secondary categories to which the potential functional vulnerabilities in each core function code belong; The large model agent module based on the attacker's perspective is used to detect and analyze the core function code from the attacker's perspective and obtain the first-level category to which the potential functional vulnerabilities in each core function code belong; The fusion analysis module is used to fuse the output results of the large language model agent module based on the auditor's perspective and the large language model agent module based on the attacker's perspective, and output the final vulnerability detection results.

Citation Information

Patent Citations

  • Multi-composite detection method, device and equipment for smart contract and storage medium

    CN117725594A

  • Intelligent contract vulnerability detection optimization method and device, equipment, medium and product

    CN118171286A

  • Apparatus and method for analyzing vulnerabilities of smart contract code

    US20230418951A1

  • KR20200094618A

Cited By

  • Intelligent contract vulnerability detection method and system based on large language model guidance

    CN120145396A

  • Filter function recognition method and device based on large language model, electronic equipment and storage medium

    CN120951343A

  • Deterministic code security audit pre-scanning and multi-agent collaborative reasoning method

    CN122595336A