NFT smart contract defect detection method and system based on large language model
Through the NFT smart contract defect detection method based on the large language model, the abstract syntax tree features of the smart contract are extracted and feature vectors are generated. The large language model is used for detection, which solves the problem of low detection performance in traditional methods and achieves higher-precision defect detection.
Patent Information
- Application Number
- CN202411542051.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-31
- Publication Date
- 2025-09-26
- Estimated Expiration
- 2044-10-31
AI Technical Summary
Traditional smart contract defect detection methods have the problem of low detection performance, resulting in poor accuracy of detection results.
An NFT smart contract defect detection method based on a large language model is adopted. By obtaining the abstract syntax tree of the smart contract, the grammatical structure and behavioral characteristics are extracted, and the feature vector is generated and stored in the knowledge base. The cosine similarity comparison and large language model are used for defect detection to generate an audit report.
The accuracy of smart contract defect detection is improved, and it can identify programming errors and deep semantic vulnerabilities in complex semantics and contextual relationships.
Smart Images

Figure CN119396690B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer network security technology, and in particular to a method and system for detecting defects in NFT smart contracts based on a large language model. Background Art
[0002] A fungible token (NFT) is a non-copyable digital asset or unique identifier managed on a blockchain, used to assign, link, or prove ownership of different physical and digital goods. When an NFT is created, a smart contract is deployed on the blockchain, serving as a digital certificate of ownership. This smart contract contains information about the NFT, such as its title, description, and ownership history. With the rapid development of the NFT market, security issues with smart contracts have gradually emerged, becoming a key factor hindering the healthy development of the market. Once deployed, smart contracts are immutable. While this immutability ensures transaction transparency and reliability, it also means that if vulnerabilities exist, attackers can exploit them for malicious purposes, potentially resulting in significant financial losses. These vulnerabilities primarily stem from logical flaws, contract design flaws, and a failure to adequately consider potential security risks during the development process.
[0003] Traditional methods for detecting smart contract vulnerabilities, such as analysis tools like Slither and Truffle, can comprehensively review smart contracts without executing code, identifying common security flaws. However, these methods have limitations. Static analysis often relies on predefined rules and patterns, making it prone to false positives when dealing with complex smart contracts, thus complicating the accurate identification of real security threats. While dynamic analysis, which detects runtime behavior by executing code and can discover more context-sensitive vulnerabilities, also faces challenges such as high computational cost, limited coverage, and difficulty simulating real-world attack scenarios.
[0004] Therefore, traditional smart contract defect detection methods have the problem of low detection performance, resulting in poor accuracy of detection results. Summary of the Invention
[0005] Based on this, in order to solve the above technical problems, a method and system for detecting NFT smart contract defects based on a large language model are provided, which can improve the detection accuracy of smart contract defect detection.
[0006] A method for detecting defects in NFT smart contracts based on a large language model, the method comprising:
[0007] Obtain source code and sample code with NFT smart contract defects;
[0008] Generate an abstract syntax tree of the NFT smart contract based on the source code, extract the grammatical structure and behavioral features in the abstract syntax tree, and obtain key elements;
[0009] Screening and filtering the key elements, and identifying the key elements based on the abstract syntax tree to obtain defect-related features;
[0010] Combining the defect-related features with the sample code, inputting the combined features into a text embedding model to convert them into feature vectors and code vectors, and storing them in a knowledge base;
[0011] Determine the target feature vector and target code vector corresponding to the contract to be detected, and perform cosine similarity comparison with the feature vector and code vector in the knowledge base to obtain a comparison result;
[0012] According to the comparison result, the target feature vector and the target code vector are input into a large language model to generate an audit report for smart contract defect detection.
[0013] In one embodiment, the source code and sample code with NFT smart contract defects are obtained, including:
[0014] Obtain source code with ERC 721 Reentrancy, Public Burn, and Risky Mutable Proxy vulnerabilities;
[0015] Based on the source code, smart contracts with defect types of ERC 721Reentrancy, Public Burn, and RiskyMutable Proxy are classified, and the corresponding defect sample codes are extracted.
[0016] In one embodiment, extracting the grammatical structure and behavioral features in the abstract syntax tree to obtain key elements includes:
[0017] Extract the code structure and grammatical hierarchy of the smart contract based on the different defect types;
[0018] Key elements are extracted based on the abstract syntax tree; the key elements include state variables, function definitions, control flows, function call records, and function call locations.
[0019] In one embodiment, the key elements are screened and filtered, and key elements are identified based on the abstract syntax tree to obtain defect-related features, including:
[0020] Filter based on the key elements to obtain key information corresponding to ERC 721 Reentrancy, Public Burn, and RiskyMutable Proxy defects;
[0021] The syntax pattern in the function definition is extracted based on the abstract syntax tree, the logical relationship of the function call record is analyzed, and the usage status of the state variable is checked to obtain defect-related features corresponding to ERC 721 Reentrancy, PublicBurn, and Risky Mutable Proxy defects.
[0022] In one embodiment, the defect-related features are combined with the sample code, the combined features are input into a text embedding model to be converted into feature vectors and code vectors, and the combined features are stored in a knowledge base, including:
[0023] Combine the defect-related features corresponding to the ERC 721 Reentrancy, Public Burn, and Risky Mutable Proxy defects with the defect sample codes corresponding to the ERC 721 Reentrancy, Public Burn, and Risky Mutable Proxy defects, respectively, to obtain the combined features;
[0024] Inputting the combined features into a text embedding model, converting the combined features into feature vectors and code vectors through the text embedding model, and performing high-dimensional vector representation on the feature vectors and code vectors;
[0025] The feature vector and code vector are stored in a knowledge base.
[0026] In one embodiment, determining a target feature vector and a target code vector corresponding to a contract to be detected includes:
[0027] Obtaining a smart contract to be tested, and extracting an actual abstract syntax tree from the smart contract to be tested, to obtain structural information corresponding to the smart contract to be tested;
[0028] Filtering the actual abstract syntax tree to obtain defect-related features corresponding to the smart contract to be detected;
[0029] The defect-related features corresponding to the smart contract to be detected are combined with the sample code and then input into the text embedding model to be converted into a target feature vector and a target code vector.
[0030] In one embodiment, performing cosine similarity comparison on the target feature vector and the target code vector with the feature vector and the code vector in the knowledge base includes:
[0031] Calculating a dot product of the target feature vector and the feature vector divided by a product of their amplitudes to obtain a first similarity value between the target feature vector and the feature vector;
[0032] A dot product of the target code vector and the code vector is calculated and divided by the product of their amplitudes to obtain a second similarity value between the target code vector and the code vector.
[0033] In one embodiment, the target feature vector and the target code vector are input into a large language model based on the comparison result to generate an audit report for smart contract defect detection, including:
[0034] According to the comparison result, inputting the target feature vector with the highest first similarity value and the target code vector with the highest second similarity value into a large language model;
[0035] Performing similarity verification and contextual relationship analysis using the large language model;
[0036] The target feature vector and the target code vector are semantically analyzed by the large language model and an audit report is generated.
[0037] A NFT smart contract defect detection system based on a large language model, the system comprising:
[0038] Code acquisition module, used to obtain source code and sample code with NFT smart contract defects;
[0039] A feature extraction module is used to generate an abstract syntax tree of the NFT smart contract based on the source code, extract the grammatical structure and behavioral features in the abstract syntax tree, and obtain key elements;
[0040] A screening and filtering module is used to screen and filter the key elements, and identify the key elements based on the abstract syntax tree to obtain defect-related features;
[0041] A feature conversion module, configured to combine the defect-related features with the sample code, input the combined features into a text embedding model to convert them into feature vectors and code vectors, and store them in a knowledge base;
[0042] A vector comparison module is used to determine the target feature vector and target code vector corresponding to the contract to be detected, and compare the target feature vector and code vector with the feature vector and code vector in the knowledge base by cosine similarity to obtain a comparison result;
[0043] A defect detection module is used to input the target feature vector and the target code vector into a large language model according to the comparison result, and generate an audit report for smart contract defect detection.
[0044] The above-mentioned NFT smart contract defect detection method and system based on the large language model extracts the grammatical structure and behavioral features in the abstract syntax tree, converts them into vector representation through the text embedding model and stores them in the knowledge base, so as to facilitate the subsequent extraction of features for cosine similarity calculation to determine the defect type. The use of the large language model can analyze and understand complex semantics and contextual relationships, which can not only identify programming errors, but also capture deep semantic vulnerabilities, thereby improving the detection performance of smart contract defects and improving detection accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] Figure 1 This is a diagram of an application environment for an NFT smart contract defect detection method based on a large language model in one embodiment;
[0046] Figure 2 1. A flowchart of an NFT smart contract defect detection method based on a large language model in one embodiment;
[0047] Figure 3 A schematic diagram of the process architecture for knowledge retrieval and audit report generation in one embodiment;
[0048] Figure 4 Schematic diagram of the process architecture of an NFT smart contract defect detection method based on a large language model in one embodiment;
[0049] Figure 5 This is a structural block diagram of an NFT smart contract defect detection system based on a large language model in one embodiment;
[0050] Figure 6 FIG. 1 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0051] In order to make the purpose, technical solutions and advantages of this application more clear, the following further describes this application in detail with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain this application and are not intended to limit this application.
[0052] The NFT smart contract defect detection method based on a large language model provided in the embodiment of the present application can be applied to Figure 1 In the application environment shown. Figure 1As shown, the application environment includes a computer device 110. The computer device 110 can obtain source code and sample code containing NFT smart contract defects; the computer device 110 can generate an abstract syntax tree of the NFT smart contract based on the source code, extract the grammatical structure and behavioral features in the abstract syntax tree, and obtain key elements; the computer device 110 can filter the key elements and identify the key elements based on the abstract syntax tree to obtain defect-related features; the computer device 110 can combine the defect-related features with the sample code, input the combined features into a text embedding model to convert them into feature vectors and code vectors, and store them in a knowledge base; the computer device 110 can determine the target feature vector and target code vector corresponding to the contract to be tested, compare them with the feature vectors and code vectors in the knowledge base through cosine similarity, and obtain a comparison result; the computer device 110 can input the target feature vector and target code vector into a large language model based on the comparison result to generate an audit report for smart contract defect detection. The computer device 110 can be, but is not limited to, various personal computers, laptops, smartphones, robots, unmanned aerial vehicles, and other devices.
[0053] In one embodiment, the overall workflow of the NFT smart contract defect detection method based on a large language model can be composed of three main stages: knowledge base construction, knowledge base retrieval, and large language model reasoning. Figure 2 As shown in the figure, a method for detecting defects in NFT smart contracts based on a large language model is provided, which includes the following steps:
[0054] Step 202: Obtain source code and sample code with NFT smart contract defects.
[0055] In the NFT smart contract defect detection method based on a large language model provided in this application, static analysis can be used to first extract structural and grammatical information from the smart contract defect code.
[0056] Specifically, in one embodiment, a large language model-based NFT smart contract defect detection method may further include a code acquisition process, specifically comprising: obtaining source code with ERC721Reentrancy, PublicBurn, or RiskyMutableProxy defects; and, based on the source code, classifying smart contracts with ERC721Reentrancy, PublicBurn, or RiskyMutableProxy defects and extracting corresponding defective sample code. In other words, the computer device may classify smart contracts with the three defect types in the collected database and extract corresponding defective sample code.
[0057] Step 204: Generate an abstract syntax tree of the NFT smart contract based on the source code, extract the grammatical structure and behavioral features in the abstract syntax tree, and obtain key elements.
[0058] After obtaining the source code and sample code, the computer device can extract the abstract syntax tree of the smart contract. Specifically, in one embodiment, the provided NFT smart contract defect detection method based on a large language model can also include a feature extraction process, which specifically includes: extracting the code structure and grammatical hierarchy of the smart contract based on different defect types; extracting key elements based on the abstract syntax tree; key elements include state variables, function definitions, control flow, function call records, and function call locations.
[0059] The computer device can extract the abstract syntax tree and obtain key elements, including state variables, function definitions, and function call records, while also recording the locations of these calls. Specifically, the computer device can first extract the structure and grammatical hierarchy of the contract code based on the characteristics of different types of defects; then extract key elements, including state variables stae_var, function definitions fun_def, and function call records fun_call, while also recording the locations of these calls call_loc. In this embodiment, for ERC 721 standard security functions, the location and function name are recorded. If any state variables are modified, the names of these modified variables are extracted and associated with the corresponding function call locations.
[0060] Step 206 : Filter the key elements and identify the key elements based on the abstract syntax tree to obtain defect-related features.
[0061] The computer can further filter the abstract syntax tree to identify key information related to known defects, thereby obtaining defect-related features. Specifically, it can filter and identify grammatical patterns in function definitions, analyze the logical relationships of function calls, and check the use of state variables.
[0062] In one embodiment, a large language model-based NFT smart contract defect detection method provided may also include a process of filtering and screening relevant features, the specific process including: filtering according to key elements to obtain key information corresponding to ERC721Reentrancy, Public Burn, and Risky Mutable Proxy defects; extracting syntax patterns in function definitions based on abstract syntax trees, analyzing the logical relationships of function call records, and checking the usage status of state variables to obtain defect-related features corresponding to ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy defects.
[0063] The computer device can further filter the obtained key elements to obtain key information related to the three defects, thereby obtaining defect-related features.
[0064] In step 208 , the defect-related features are combined with the sample code, and the combined features are input into a text embedding model to be converted into feature vectors and code vectors, which are then stored in the knowledge base.
[0065] The computer device can combine the extracted defect-related features with known defect code snippets, i.e., sample codes, input them into the text embedding model text-embeding-004, and store them in the knowledge base.
[0066] In one embodiment, a large language model-based NFT smart contract defect detection method provided may also include a vector conversion process, the specific process including: combining defect-related features corresponding to ERC 721Reentrancy, Public Burn, and RiskyMutable Proxy defects with defect sample codes corresponding to ERC 721Reentrancy, Public Burn, and RiskyMutable Proxy defects, respectively, to obtain combined features; inputting the combined features into a text embedding model, converting the combined features into feature vectors and code vectors through the text embedding model, and representing the feature vectors and code vectors as high-dimensional vectors; storing the feature vectors and code vectors in a knowledge base.
[0067] The computer device can combine the three defect features obtained with the corresponding code example snippets and convert them into high-dimensional vector representations using a text embedding model (text-embedding-004). Then, the converted feature vectors of the three defects and the code example vectors can be stored in a knowledge base (VectorDB) to store vectors of different defect types.
[0068] Step 210: Determine the target feature vector and target code vector corresponding to the contract to be detected, and perform cosine similarity comparison with the feature vector and code vector in the knowledge base to obtain a comparison result.
[0069] In one embodiment, a large language model-based NFT smart contract defect detection method is provided that may also include a process of determining a target feature vector and a target code vector. The specific process includes: obtaining a smart contract to be detected, and extracting an actual abstract syntax tree from the smart contract to be detected to obtain structural information corresponding to the smart contract to be detected; filtering the actual abstract syntax tree to obtain defect-related features corresponding to the smart contract to be detected; combining the defect-related features corresponding to the smart contract to be detected with the sample code and inputting them into a text embedding model to convert them into a target feature vector and a target code vector.
[0070] When actually performing smart contract defect detection, it is necessary to determine the contract to be detected, and then extract the features in the abstract syntax tree based on the contract to be detected, and execute steps 204 to 208. That is, extract the abstract syntax tree from the contract to be detected to obtain the structural information of the basic contract; further filter the abstract syntax tree to obtain key information related to known defects, including the syntax pattern in the function definition, analyze the logical relationship of the function call, and check the use of state variables, etc., so as to obtain defect-related features; input the obtained key information together with the source code into the text embedding model (text-embedding-004) to generate feature vectors and code vectors as the target feature vectors and target code vectors.
[0071] In one embodiment, a large language model-based NFT smart contract defect detection method provided may also include a cosine similarity comparison process, the specific process including: calculating the dot product of the target feature vector and the feature vector divided by the product of their amplitudes to obtain a first similarity value between the target feature vector and the feature vector; calculating the dot product of the target code vector and the code vector divided by the product of their amplitudes to obtain a second similarity value between the target code vector and the code vector.
[0072] like Figure 3 As shown, when performing preliminary defect matching, the computer device can use cosine similarity to evaluate the similarity between the target feature vector and the target code vector and the known defect vectors in the knowledge base. Specifically, the closer the similarity value is to 1, the more similar the directions of the two vectors in the feature space are, indicating a higher degree of match between the contract being analyzed and the known defects.
[0073] In step 212, the target feature vector and the target code vector are input into the large language model according to the comparison result to generate an audit report for smart contract defect detection.
[0074] The computer device can input the target feature vector and the target code vector into the large language model gemini-pro-1.5 and generate an audit report.
[0075] Specifically, in one embodiment, a method for detecting defects in an NFT smart contract based on a large language model is provided, which may also include a process of reasoning and generating an audit report through a large language model. The specific process includes: according to the comparison result, inputting the target feature vector with the highest first similarity value and the target code vector with the highest second similarity value into the large language model; performing similarity verification and contextual relationship analysis through the large language model; performing semantic analysis on the target feature vector and the target code vector through the large language model and generating an audit report.
[0076] The computer device can obtain vectors with high similarity, and then input these defect type vectors, as well as the feature vector and code vector of the contract to be tested, into the large language model for inference. The large language model deeply analyzes the contextual relationship between the identified defect type and the current contract, as well as the behavioral patterns of known vulnerabilities. The large language model then performs semantic analysis on the input vectors and generates a detailed audit report. The audit report includes the detected defect type, a detailed defect description, potential security impact, and recommended remediation measures.
[0077] In one embodiment, an application framework of an NFT smart contract defect detection method based on a large language model is provided. Figure 3 、 Figure 4 As shown in the figure: the smart contract source code of three defect types is obtained, and then the feature vectors and corresponding defect example codes extracted from the smart contract source code based on the abstract syntax tree are converted into vector representations through the text embedding model and stored in the vector database, where the database acts as a knowledge base; features are extracted from the contract to be tested, and input into the embedding model with the contract code to generate feature vectors and code vectors, which are compared with the known defect vectors in the knowledge base, and cosine similarity is used to perform preliminary defect type and complete knowledge retrieval; a large language model is used to reason and conduct detailed analysis of the matched defect types, and a final audit report is produced.
[0078] It should be understood that, although the various steps in the above flow chart are shown in sequence as indicated by the arrows, these steps are not necessarily performed in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and these steps can be performed in other orders. Moreover, at least a portion of the steps in the above flow chart may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily performed at the same time, but can be performed at different times, and the execution order of these sub-steps or stages is not necessarily to be performed in sequence, but can be performed in turn or alternately with other steps or at least a portion of the sub-steps or stages of other steps.
[0079] In one embodiment, Figure 5 As shown, a NFT smart contract defect detection system based on a large language model is provided, including: a code acquisition module 510, a feature extraction module 520, a screening and filtering module 530, a feature conversion module 540, a vector comparison module 550 and a defect detection module 560, wherein:
[0080] A code acquisition module 510 is used to obtain source code and sample code with NFT smart contract defects;
[0081] Feature extraction module 520, used to generate an abstract syntax tree of the NFT smart contract based on the source code, extract the grammatical structure and behavioral features in the abstract syntax tree, and obtain key elements;
[0082] The screening and filtering module 530 is used to screen and filter key elements and identify key elements based on the abstract syntax tree to obtain defect-related features;
[0083] A feature conversion module 540 is used to combine defect-related features with sample code, input the combined features into a text embedding model to convert them into feature vectors and code vectors, and store them in a knowledge base;
[0084] The vector comparison module 550 is used to determine the target feature vector and target code vector corresponding to the contract to be detected, and compare the target feature vector and code vector with the feature vector and code vector in the knowledge base by cosine similarity to obtain a comparison result;
[0085] The defect detection module 560 is used to input the target feature vector and the target code vector into the large language model according to the comparison result, and generate an audit report for the smart contract defect detection.
[0086] In one embodiment, the code acquisition module 510 is further used to obtain source code with ERC 721Reentrancy, PublicBurn, and Risky Mutable Proxy defects; based on the source code, smart contracts with defect types of ERC721Reentrancy, PublicBurn, and Risky Mutable Proxy are classified, and corresponding defect sample codes are extracted.
[0087] In one embodiment, the feature extraction module 520 is also used to extract the code structure and grammatical hierarchy of the smart contract according to different defect types; extract key elements based on the abstract syntax tree; the key elements include state variables, function definitions, control flows, function call records, and function call locations.
[0088] In one embodiment, the screening and filtering module 530 is further used to filter according to key elements to obtain key information corresponding to ERC721Reentrancy, Public Burn, and Risky Mutable Proxy defects; extract the syntax pattern in the function definition based on the abstract syntax tree, analyze the logical relationship of the function call record, and check the usage status of the state variables to obtain defect-related features corresponding to ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy defects.
[0089] In one embodiment, the feature conversion module 540 is further used to combine the defect-related features corresponding to the ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy defects with the defect example codes corresponding to the ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy defects, respectively, to obtain combined features; input the combined features into a text embedding model, convert the combined features into feature vectors and code vectors through the text embedding model, and represent the feature vectors and code vectors as high-dimensional vectors; and store the feature vectors and code vectors in a knowledge base.
[0090] In one embodiment, the vector comparison module 550 is further used to obtain the smart contract to be detected, and extract the actual abstract syntax tree from the smart contract to be detected to obtain structural information corresponding to the smart contract to be detected; filter the actual abstract syntax tree to obtain defect-related features corresponding to the smart contract to be detected; combine the defect-related features corresponding to the smart contract to be detected with the sample code, and then input them into the text embedding model to convert them into target feature vectors and target code vectors.
[0091] In one embodiment, the vector comparison module 550 is further used to calculate the dot product of the target feature vector and the feature vector divided by the product of their amplitudes to obtain a first similarity value between the target feature vector and the feature vector; and calculate the dot product of the target code vector and the code vector divided by the product of their amplitudes to obtain a second similarity value between the target code vector and the code vector.
[0092] In one embodiment, the defect detection module 560 is also used to input the target feature vector with the highest first similarity value and the target code vector with the highest second similarity value into the large language model based on the comparison results; perform similarity verification and context relationship analysis through the large language model; perform semantic analysis on the target feature vector and the target code vector through the large language model and generate an audit report.
[0093] In one embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure diagram may be as follows: Figure 6 As shown. The computer device includes a processor, memory, network interface, display screen and input device connected via a system bus. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with an external terminal via a network connection. When the computer program is executed by the processor, it implements a method for detecting defects in NFT smart contracts based on a large language model. The display screen of the computer device can be a liquid crystal display or an electronic ink display screen. The input device of the computer device can be a touch layer covering the display screen, or it can be a button, trackball or touchpad provided on the computer device housing, or it can be an external keyboard, touchpad or mouse.
[0094] Those skilled in the art will understand that Figure 6 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than shown in the figure, or combine certain components, or have a different component arrangement.
[0095] In one embodiment, a computer device is provided, including a memory and a processor, wherein a computer program is stored in the memory, and when the processor executes the computer program, the steps of the NFT smart contract defect detection method based on a large language model are implemented.
[0096] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps of the NFT smart contract defect detection method based on a large language model are implemented.
[0097] Those skilled in the art will appreciate that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
[0098] The technical features of the above embodiments can be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0099] The above-described embodiments merely represent several implementation methods of the present application. While the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the present invention. It should be noted that a person skilled in the art could make various modifications and improvements without departing from the spirit of the present application, all of which fall within the scope of protection of the present application. Therefore, the scope of protection of the present patent application shall be determined by the appended claims.
Claims
1. A method for detecting defects in NFT smart contracts based on a large language model, characterized in that: The method comprises: Obtain source code and sample code with NFT smart contract defects; Generate an abstract syntax tree of the NFT smart contract based on the source code, extract the grammatical structure and behavioral features in the abstract syntax tree, and obtain key elements; Screening and filtering the key elements, and identifying the key elements based on the abstract syntax tree to obtain defect-related features; Combining the defect-related features with the sample code, inputting the combined features into a text embedding model to convert them into feature vectors and code vectors, and storing them in a knowledge base; Determine the target feature vector and target code vector corresponding to the contract to be detected, and perform cosine similarity comparison with the feature vector and code vector in the knowledge base to obtain a comparison result; According to the comparison result, the target feature vector and the target code vector are input into the large language model to generate an audit report for smart contract defect detection, including: according to the comparison result, the target feature vector with the highest first similarity value and the target code vector with the highest second similarity value are input into the large language model; similarity verification and context relationship analysis are performed through the large language model; semantic analysis of the target feature vector and the target code vector is performed through the large language model and an audit report is generated; after obtaining vectors with high similarity, the vector of the defect type and the feature vector and code vector of the contract to be detected are input into the large language model for reasoning; the large language model is used to deeply analyze the context relationship between the identified defect type and the current contract, as well as the behavior pattern of known vulnerabilities; then the large language model performs semantic analysis on the input vector and generates a detailed audit report; wherein, the audit report includes the detected defect type, including a detailed defect description, potential security impact and recommended remedial measures.
2. The NFT smart contract defect detection method based on a large language model according to claim 1 is characterized in that: Get source code and sample code with NFT smart contract vulnerabilities, including: Obtain source code with ERC 721 Reentrancy, Public Burn, and Risky Mutable Proxy vulnerabilities; Based on the source code, smart contracts with defect types of ERC 721 Reentrancy, Public Burn, and Risky MutableProxy are classified, and the corresponding defect sample codes are extracted.
3. The NFT smart contract defect detection method based on a large language model according to claim 2 is characterized in that: Extract the grammatical structure and behavioral features from the abstract syntax tree to obtain key elements, including: Extract the code structure and grammatical hierarchy of the smart contract based on the different defect types; Key elements are extracted based on the abstract syntax tree; the key elements include state variables, function definitions, control flows, function call records, and function call locations.
4. The NFT smart contract defect detection method based on a large language model according to claim 3 is characterized in that: The key elements are screened and filtered, and key elements are identified based on the abstract syntax tree to obtain defect-related features, including: Filter based on the key elements to obtain key information corresponding to ERC 721 Reentrancy, Public Burn, and RiskyMutable Proxy defects; The syntax pattern in the function definition is extracted based on the abstract syntax tree, the logical relationship of the function call record is analyzed, and the usage status of the state variable is checked to obtain defect-related features corresponding to ERC 721 Reentrancy, Public Burn, and Risky Mutable Proxy defects.
5. The NFT smart contract defect detection method based on a large language model according to claim 4 is characterized in that: Combining the defect-related features with the sample code, inputting the combined features into a text embedding model to convert them into feature vectors and code vectors, and storing them in a knowledge base, including: Combine the defect-related features corresponding to the ERC 721 Reentrancy, Public Burn, and Risky Mutable Proxy defects with the defect sample codes corresponding to the ERC 721 Reentrancy, Public Burn, and Risky Mutable Proxy defects, respectively, to obtain the combined features; Inputting the combined features into a text embedding model, converting the combined features into feature vectors and code vectors through the text embedding model, and performing high-dimensional vector representation on the feature vectors and code vectors; The feature vector and code vector are stored in a knowledge base.
6. The NFT smart contract defect detection method based on a large language model according to claim 1 is characterized in that: Determine the target feature vector and target code vector corresponding to the contract to be tested, including: Obtaining a smart contract to be tested, and extracting an actual abstract syntax tree from the smart contract to be tested, to obtain structural information corresponding to the smart contract to be tested; Filtering the actual abstract syntax tree to obtain defect-related features corresponding to the smart contract to be detected; The defect-related features corresponding to the smart contract to be detected are combined with the sample code and then input into the text embedding model to be converted into a target feature vector and a target code vector.
7. The NFT smart contract defect detection method based on a large language model according to claim 6 is characterized in that: Comparing the target feature vector and the target code vector with the feature vector and the code vector in the knowledge base by cosine similarity, including: Calculating a dot product of the target feature vector and the feature vector divided by a product of their amplitudes to obtain a first similarity value between the target feature vector and the feature vector; A dot product of the target code vector and the code vector is calculated and divided by the product of their amplitudes to obtain a second similarity value between the target code vector and the code vector.
8. An NFT smart contract defect detection system based on a large language model, characterized by: The system comprises: Code acquisition module, used to obtain source code and sample code with NFT smart contract defects; A feature extraction module is used to generate an abstract syntax tree of the NFT smart contract based on the source code, extract the grammatical structure and behavioral features in the abstract syntax tree, and obtain key elements; A screening and filtering module is used to screen and filter the key elements, and identify the key elements based on the abstract syntax tree to obtain defect-related features; A feature conversion module, configured to combine the defect-related features with the sample code, input the combined features into a text embedding model to convert them into feature vectors and code vectors, and store them in a knowledge base; A vector comparison module is used to determine the target feature vector and target code vector corresponding to the contract to be detected, and compare the target feature vector and code vector with the feature vector and code vector in the knowledge base by cosine similarity to obtain a comparison result; A defect detection module is configured to input the target feature vector and target code vector into a large language model based on the comparison result to generate an audit report for smart contract defect detection, including: inputting the target feature vector with the highest first similarity value and the target code vector with the highest second similarity value into the large language model based on the comparison result; performing similarity verification and contextual relationship analysis through the large language model; performing semantic analysis on the target feature vector and target code vector through the large language model and generating an audit report; after obtaining vectors with high similarity, inputting the vector of the defect type and the feature vector and code vector of the contract to be detected into the large language model for inference; performing in-depth analysis on the contextual relationship between the identified defect type and the current contract, as well as the behavioral pattern of known vulnerabilities through the large language model; then performing semantic analysis on the input vector and generating a detailed audit report; wherein the audit report includes the detected defect type, including a detailed defect description, potential security impact, and recommended remedial measures.
9. The NFT smart contract defect detection system based on a large language model according to claim 8 is characterized in that: The code acquisition module is also used to: obtain source code with ERC 721 Reentrancy, Public Burn, and Risky MutableProxy defects; based on the source code, classify smart contracts with defect types of ERC 721 Reentrancy, Public Burn, and Risky Mutable Proxy, and extract corresponding defect sample codes.
Citation Information
Patent Citations
Vulnerability detection method for smart contract code snippets and related device
CN114201756A
Ethereum intelligent contract vulnerability detection and positioning method based on deep learning
CN116484384A