Vulnerability detection method and device for smart contract, equipment and medium
Through the directed graph and feature extraction model of smart contracts, combined with graph neural networks and multi-layer perceptron networks, the accuracy and efficiency problems of smart contract vulnerability detection are solved, and efficient and automated vulnerability identification is achieved.
Patent Information
- Application Number
- CN202410547779.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-06
- Publication Date
- 2024-08-23
- Estimated Expiration
- 2044-05-06
AI Technical Summary
Existing smart contract vulnerability detection methods have problems such as low detection accuracy, low efficiency and low degree of automation. In particular, formal verification relies on hard logic rules defined by experts and cannot keep up with the complexity of smart contract structures. Fuzz testing requires a lot of calculations. Resources, machine learning detection has low accuracy in the process of syntax and semantic conversion.
By obtaining the directed graph of the smart contract, the pre-trained feature extraction model is used to extract the node feature vectors, and based on the node feature vectors and adjacency matrices, combined with the graph neural network and the multi-layer perceptron network, the vulnerabilities of the smart contract are automatically identified.
It improves the accuracy and efficiency of smart contract vulnerability detection, can accurately identify vulnerabilities, reduce false alarm rates, and improve automation.
Smart Images

Figure CN118536119A
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method, device, equipment and medium for detecting vulnerabilities in smart contracts. Background Art
[0002] Smart contracts are codes written on the blockchain. Once an event triggers the terms in the smart contract, the code will be automatically executed. Smart contracts need to be tested for vulnerabilities before they are released.
[0003] At present, traditional smart contract vulnerability detection methods include formal verification, fuzz testing, machine learning, etc. Formal verification relies heavily on hard logic rules pre-defined by experts when performing vulnerability detection. As the structure of smart contracts becomes more complex, the rules defined by experts cannot keep up with the speed of smart contract vulnerability updates, and the logical rules defined by experts may lead to a high false alarm rate. Therefore, the formal verification detection method has the problem of low detection accuracy.
[0004] Although fuzz testing-based methods can discover potential vulnerabilities in smart contracts to a certain extent by inputting a large amount of random or abnormal data, fuzz testing usually requires a lot of computing resources and time, and has problems such as low efficiency and low degree of automation.
[0005] Although vulnerability detection methods based on machine learning have improved the efficiency of detection, most of them convert the smart contract source code into bytecode for detection. During the conversion process, the syntax and semantics of the smart contract may be lost, resulting in low vulnerability detection accuracy.
[0006] Therefore, how to improve the accuracy and efficiency of vulnerability detection in smart contracts has become a technical problem that needs to be urgently solved by technical personnel in this field. Summary of the invention
[0007] In view of the above, the present application provides a smart contract vulnerability detection method, device, equipment and medium, the purpose of which is to solve the above technical problems.
[0008] In a first aspect, the present application provides a method for detecting vulnerabilities in a smart contract, the method comprising:
[0009] Get the target smart contract to be tested;
[0010] Generate a directed graph of the target smart contract;
[0011] Extracting node feature vectors of the directed graph using a pre-trained feature extraction model;
[0012] Based on the node feature vector of the directed graph and the adjacency matrix of the directed graph, a vulnerability detection result of the target smart contract is identified.
[0013] Preferably, generating the directed graph of the target smart contract comprises:
[0014] Analyze the calling relationship between the functions of the target smart contract;
[0015] A directed graph of the target smart contract is generated with the functions of the target smart contract as nodes and the call relationships as edges.
[0016] Preferably, the extracting the node feature vector of the directed graph by using a pre-trained feature extraction model comprises:
[0017] Determine the starting position and the ending position of the function corresponding to each node in the directed graph by using a regular expression;
[0018] Generate a target text corresponding to each function according to the starting position and the ending position;
[0019] The target text is input into the feature extraction model, and the features of the target text are extracted as node feature vectors of the directed graph.
[0020] Preferably, the feature extraction model is obtained based on LLM model training, and the training process of the feature extraction model includes:
[0021] Get a preset number of sample smart contract code files;
[0022] Concatenate the code files of all sample smart contracts to obtain the target text file;
[0023] The target text file is used as an input of the LLM model to perform an unsupervised training operation to obtain the feature extraction model.
[0024] Preferably, the identifying the vulnerability detection result of the target smart contract based on the node feature vector of the directed graph and the adjacency matrix of the directed graph includes:
[0025] Divide the directed graph into N multi-hop subgraphs, where N represents the number of nodes in the directed graph;
[0026] Inputting the node feature vector corresponding to each node, the multi-hop subgraph and the adjacency matrix of the directed graph into the graph neural network to obtain the local features of each node;
[0027] Based on the local features of each node, obtaining the target features of the directed graph;
[0028] The target features of the directed graph are input into a multi-layer perceptron network to obtain a vulnerability detection result of the target smart contract.
[0029] Preferably, obtaining the target feature of the directed graph based on the local feature of each node includes:
[0030] Inputting the local features of each node into the self-attention mechanism network to obtain the global features of each node;
[0031] Adding the global features of each node to obtain an addition result;
[0032] The added result is divided by the number of nodes to obtain the target feature of the directed graph.
[0033] Preferably, the method further comprises:
[0034] The vulnerability detection result of the target smart contract is sent to a preset terminal.
[0035] In a second aspect, the present application provides a smart contract vulnerability detection device, the device comprising:
[0036] Acquisition module: used to obtain the target smart contract to be detected;
[0037] Generation module: used to generate a directed graph of the target smart contract;
[0038] Extraction module: used to extract node feature vectors of the directed graph using a pre-trained feature extraction model;
[0039] Detection module: used to identify the vulnerability detection result of the target smart contract based on the node feature vector of the directed graph and the adjacency matrix of the directed graph.
[0040] In a third aspect, the present application provides an electronic device, including a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus;
[0041] Memory, used to store computer programs;
[0042] The processor is used to implement the vulnerability detection method for the smart contract described in any one of the embodiments of the first aspect when executing the program stored in the memory.
[0043] In a fourth aspect, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the method for detecting vulnerabilities in a smart contract as described in any one of the embodiments of the first aspect is implemented.
[0044] The above technical solution provided by the embodiment of the present application has the following advantages compared with the prior art:
[0045] The present application obtains the target smart contract to be detected, generates a directed graph of the target smart contract, and the directed graph can represent the relationship between the codes of the target smart contract. The node feature vector of the directed graph is extracted by using a pre-trained feature extraction model. Since the node feature vector of the directed graph can represent the importance of the node in the directed graph in addition to the characteristic information of the node, the adjacency matrix of the directed graph can represent the connection relationship between each node in the directed graph. Therefore, based on the node feature vector of the directed graph and the adjacency matrix of the directed graph, it is possible to automatically and accurately identify whether there are vulnerabilities in the target smart contract, thereby improving the vulnerability detection accuracy and efficiency of the target smart contract. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0047] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0048] Figure 1 This is a flow chart of an embodiment of a vulnerability detection method for a smart contract of the present application;
[0049] Figure 2 This is a module diagram of an embodiment of a vulnerability detection device for a smart contract of the present application;
[0050] Figure 3 A schematic diagram of an electronic device embodiment of the present application;
[0051] The realization of the purpose, functional features and advantages of this application will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0052] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.
[0053] The disclosure below provides many different embodiments or examples to realize the different structures of the present application. In order to simplify the disclosure of the present application, the parts and settings of specific examples are described below. Of course, they are only examples, and the purpose is not to limit the present application. In addition, the present application can repeat reference numbers and / or letters in different examples. This repetition is for the purpose of simplification and clarity, and does not itself indicate the relationship between the various embodiments and / or settings discussed.
[0054] This application provides a method for detecting vulnerabilities in smart contracts. Figure 1 The figure is a schematic diagram of a method flow of an embodiment of a method for detecting a vulnerability in a smart contract of the present application. The method can be executed by an electronic device, and the electronic device can be implemented by software and / or hardware. The method for detecting a vulnerability in a smart contract includes:
[0055] Step S10: Obtain the target smart contract to be detected;
[0056] Step S20: Generate a directed graph of the target smart contract;
[0057] Step S30: extracting node feature vectors of the directed graph using a pre-trained feature extraction model;
[0058] Step S40: Based on the node feature vector of the directed graph and the adjacency matrix of the directed graph, identify the vulnerability detection result of the target smart contract.
[0059] Smart contracts are codes written on the blockchain. Once an event triggers the terms in the smart contract, the code will be automatically executed. That is, the code will be executed when the conditions are met without human control. Smart contracts are written in high-level programming languages (for example, Solidity language), then compiled into virtual machine bytecode that supports Turing complete instruction set, and published to the system that supports running smart contracts. Once a smart contract is successfully published, its code cannot be modified or its execution cannot be prevented, that is, smart contracts are irreversible. Smart contracts, like traditional software programs, may also have vulnerabilities in their code. Due to the huge amount of digital assets associated with smart contracts and their irreversibility, vulnerabilities in their code are easy to be attacked. Therefore, after completing the writing of smart contracts or before the smart contracts are published, it is necessary to detect whether there are vulnerabilities in the smart contract code.
[0060] In this embodiment, when a request for detecting whether a smart contract has a vulnerability is received, the electronic device obtains the target smart contract to be detected, wherein the request may include the target smart contract to be detected and may also include the storage path of the target smart contract. That is, the target smart contract may be uploaded by the user when submitting the detection request, or may be obtained from the storage address specified in the request after the user submits the request.
[0061] After obtaining the target smart contract to be detected, a directed graph of the target smart contract is generated. A directed graph is a data structure composed of a set of nodes and the relationships between the nodes. The directed graph can represent the relationship between the codes of the target smart contract, where the nodes of the directed graph represent the functions of the target smart contract, and the edges of the directed graph represent the calling relationship between the functions. Specifically, generating the directed graph of the target smart contract includes:
[0062] Analyze the calling relationship between the functions of the target smart contract;
[0063] A directed graph of the target smart contract is generated with the functions of the target smart contract as nodes and the call relationships as edges.
[0064] The code of the target smart contract is input into the Slither tool. The Slither tool is used to analyze the calling relationship between the functions of the target smart contract, and the calling relationship graph of the target smart contract function can be obtained. The calling relationship graph can clearly express the dependency relationship of the functions in the target smart contract. The functions of the target smart contract are used as nodes and the calling relationship is used as edges to generate a directed graph of the target smart contract. For example, the directed edge between node A and node B in the directed graph indicates that function A in the target smart contract calls function B. Among them, Slither is a smart contract static analysis framework that provides functions such as automated vulnerability detection and code understanding.
[0065] After generating the directed graph of the target smart contract, it is necessary to extract the node feature vector of the directed graph so as to identify and detect the directed graph through the node feature vector. Specifically, a feature extraction model can be pre-trained by a large language model (LLM), and the node feature vector of the directed graph can be extracted using the feature extraction model. The node feature vector of the directed graph is extracted using the pre-trained feature extraction model, including:
[0066] Determine the starting position and the ending position of the function corresponding to each node in the directed graph by using a regular expression;
[0067] Generate a target text corresponding to each function according to the starting position and the ending position;
[0068] The target text is input into the feature extraction model, and the features of the target text are extracted as node feature vectors of the directed graph.
[0069] Since each node in the directed graph represents a function in the target smart contract, it is necessary to obtain the text of the function first. Using the pre-configured regular expression, determine the starting and ending positions of the function code corresponding to each node. After obtaining the starting and ending positions, the content of the function can be output as a text file format, thereby obtaining the text corresponding to each function (recorded as the target text), and inputting the target text into the feature extraction model. The feature extraction model can extract and output the feature vector corresponding to each function. The function corresponding feature vector is the node feature vector corresponding to the function, thereby obtaining each node feature vector of the directed graph. It can be understood that a regular expression is a logical formula for string operations, using some pre-defined specific characters and combinations of these specific characters to form a regular string, which is used to express a filtering or screening logic for the string.
[0070] The feature extraction model is obtained based on the LLM model training, and the training process of the feature extraction model includes:
[0071] Get a preset number of sample smart contract code files;
[0072] Concatenate the code files of all sample smart contracts to obtain the target text file;
[0073] The target text file is used as an input of the LLM model to perform an unsupervised training operation to obtain the feature extraction model.
[0074] For example, 100,000 sample smart contract code files without label information are obtained from a predetermined data source, and the sample smart contract code files are used as training samples to train the model. The code files can be source code files. Before training, the code files of the sample smart contracts need to be processed, and all the sample smart contract code files are spliced into a text file (recorded as the target text file), that is, the target text file contains the code information of all training samples. The target text file is used as the input of the LLM model (for example, the Llama model), and a random section of text in the target text file is repeatedly masked, and the Llama model is allowed to complete the operation, thereby enhancing the Llama model's understanding of the semantics of the smart contract code, ensuring that after the smart contract code is used as the text input of Llama, the output feature vector can accurately express the semantics of the smart contract code.
[0075] After extracting the node feature vector of the directed graph using the feature extraction model, the vulnerability detection result of the target smart contract is identified based on the node feature vector of the directed graph and the adjacency matrix of the directed graph. The adjacency matrix of the directed graph is a two-dimensional matrix with N rows and N columns, which is used to represent the connection relationship between each node in the directed graph, where N represents the number of nodes in the directed graph. Specifically, based on the node feature vector of the directed graph and the adjacency matrix of the directed graph, the vulnerability detection result of the target smart contract is identified, including:
[0076] Divide the directed graph into N multi-hop subgraphs, where N represents the number of nodes in the directed graph;
[0077] Inputting the node feature vector corresponding to each node, the multi-hop subgraph and the adjacency matrix of the directed graph into the graph neural network to obtain the local features of each node;
[0078] Based on the local features of each node, obtaining the target features of the directed graph;
[0079] The target features of the directed graph are input into a multi-layer perceptron network to obtain a vulnerability detection result of the target smart contract.
[0080] Assuming there are 50 nodes in a directed graph, the directed graph is divided into 50 multi-hop subgraphs. In the directed graph, a node i pointing to another node j is a first-degree edge of node i. If node j has a first-degree edge pointing to node k, then the edge from node i to node j and then to node k is a two-degree edge of node i. The set of all M-degree edges of node i is called a multi-hop subgraph of node i. Graph neural networks can learn the relationship between nodes and represent the weights of nodes. Graph neural networks will represent the importance of each node through the connection relationship between nodes, that is, the weight value of the node represents the importance of the node in the directed graph. The larger the weight value, the more important the node. The node feature vector, multi-hop subgraph and adjacency matrix of the directed graph corresponding to each node are input into the graph neural network together to obtain the local features of the node. After obtaining the local features of each node, the network with the self-attention mechanism can obtain the target features of the directed graph. The target features of the directed graph are input into the multi-layer perceptron network to obtain the vulnerability detection results of the target smart contract. For example, the output vulnerability detection result of 1 indicates that the target smart contract has a vulnerability, and the vulnerability detection result of 0 indicates that the target smart contract does not have a vulnerability. The attention mechanism can comprehensively consider the information of the entire directed graph to generate global features with global information. The multi-layer perceptron network is a feedforward neural network model, which consists of multiple neuron layers. Each neuron layer is connected to all neurons in the previous layer. Nonlinear mapping is achieved through the combination of weights and activation functions. The multi-layer perceptron can learn more complex feature representations through the superposition of multiple hidden layers, thereby improving the accuracy of vulnerability detection in the target smart contract.
[0081] Furthermore, obtaining the target feature of the directed graph based on the local feature of each node includes:
[0082] Inputting the local features of each node into the self-attention mechanism network to obtain the global features of each node;
[0083] Adding the global features of each node to obtain an addition result;
[0084] The added result is divided by the number of nodes to obtain the target feature of the directed graph.
[0085] Since the self-attention mechanism network can dynamically adjust the representation of nodes according to the relationship between nodes, by learning the relationship between nodes and other nodes, it can capture the information transfer and influence between nodes, thereby generating a global feature representation. Therefore, the local features of all nodes are input into the self-attention mechanism network to obtain the global features of each node. The global features of all nodes are added together to obtain the addition result, that is, the superimposed feature is obtained. The feature obtained by dividing the addition result by the number of nodes N is used as the target feature of the directed graph, so that the target feature can accurately represent the association information of the nodes in the directed graph.
[0086] In one embodiment, the method further comprises:
[0087] The vulnerability detection result of the target smart contract is sent to a preset terminal.
[0088] After identifying the vulnerability detection results of the target smart contract, the vulnerability detection results of the target smart contract are sent to a preset terminal (for example, a terminal of a developer or tester). When a vulnerability is identified in the target smart contract, relevant personnel can repair the vulnerability in the target smart contract in a timely manner.
[0089] Reference Figure 2 , which is a schematic diagram of the functional modules of the vulnerability detection device 100 for the smart contract of the present application.
[0090] The vulnerability detection device 100 for smart contracts described in the present application can be installed in an electronic device. According to the functions implemented, the vulnerability detection device 100 for smart contracts can include an acquisition module 110, a generation module 120, an extraction module 130 and a detection module 140. The module described in the present application can also be referred to as a unit, which refers to a series of computer program segments that can be executed by an electronic device processor and can complete fixed functions, which are stored in the memory of the electronic device.
[0091] In this embodiment, the functions of each module / unit are as follows:
[0092] Acquisition module 110: used to acquire the target smart contract to be detected;
[0093] Generating module 120: used to generate a directed graph of the target smart contract;
[0094] Extraction module 130: used to extract node feature vectors of the directed graph using a pre-trained feature extraction model;
[0095] Detection module 140: used to identify the vulnerability detection result of the target smart contract based on the node feature vector of the directed graph and the adjacency matrix of the directed graph.
[0096] In one embodiment, generating the directed graph of the target smart contract includes:
[0097] Analyze the calling relationship between the functions of the target smart contract;
[0098] A directed graph of the target smart contract is generated with the functions of the target smart contract as nodes and the call relationships as edges.
[0099] In one embodiment, the extracting the node feature vector of the directed graph using a pre-trained feature extraction model includes:
[0100] Determine the starting position and the ending position of the function corresponding to each node in the directed graph by using a regular expression;
[0101] Generate a target text corresponding to each function according to the starting position and the ending position;
[0102] The target text is input into the feature extraction model, and the features of the target text are extracted as node feature vectors of the directed graph.
[0103] In one embodiment, the feature extraction model is obtained by training based on a large language model, and the training process of the feature extraction model includes:
[0104] Get a preset number of sample smart contract code files;
[0105] Concatenate the code files of all sample smart contracts to obtain the target text file;
[0106] The target text file is used as an input of a large language model to perform an unsupervised training operation to obtain the feature extraction model.
[0107] In one embodiment, the identifying the vulnerability detection result of the target smart contract based on the node feature vector of the directed graph and the adjacency matrix of the directed graph includes:
[0108] Divide the directed graph into N multi-hop subgraphs, where N represents the number of nodes in the directed graph;
[0109] Inputting the node feature vector corresponding to each node, the multi-hop subgraph and the adjacency matrix of the directed graph into the graph neural network to obtain the local features of each node;
[0110] Based on the local features of each node, obtaining the target features of the directed graph;
[0111] The target features of the directed graph are input into a multi-layer perceptron network to obtain a vulnerability detection result of the target smart contract.
[0112] In one embodiment, obtaining the target feature of the directed graph based on the local feature of each node includes:
[0113] Inputting the local features of each node into the self-attention mechanism network to obtain the global features of each node;
[0114] Adding the global features of each node to obtain an addition result;
[0115] The added result is divided by the number of nodes to obtain the target feature of the directed graph.
[0116] In one embodiment, the smart contract vulnerability detection device 100 further includes a sending module, which is used to send the vulnerability detection result of the target smart contract to a preset terminal.
[0117] Reference Figure 3 , which is a schematic diagram of a preferred embodiment of the electronic device of the present application.
[0118] The electronic device includes a processor 111, a communication interface 112, a memory 113 and a communication bus 114, wherein the processor 111, the communication interface 112, and the memory 113 communicate with each other via the communication bus 114;
[0119] Memory 113, used to store computer programs, such as vulnerability detection programs for smart contracts;
[0120] Among them, the processor 111 can be a central processing unit (CPU), a controller, a microcontroller, a microprocessor, or other data processing chips in some embodiments. The processor 111 is generally used to control the overall operation of the electronic device, such as performing control and processing related to data interaction or communication. In this embodiment, the processor 111 is used to run the program code stored in the memory 113 or process data, such as running the program code of the vulnerability detection program of the smart contract.
[0121] The communication interface 112 may optionally include a standard wired interface, a wireless interface (such as a WI-FI interface), and the communication interface 112 may also be used to establish a communication connection between the electronic device and other electronic devices.
[0122] The memory 113 includes at least one type of readable storage medium, and the readable storage medium includes flash memory, hard disk, multimedia card, card-type memory (for example, SD or DX memory, etc.), random access memory (RAM), static random access memory (SRAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), programmable read-only memory (PROM), magnetic memory, disk, optical disk, etc. In some embodiments, the memory 113 may be an internal storage unit of the electronic device, such as a hard disk or memory of the electronic device. In other embodiments, the memory 113 may also be an external storage device of the electronic device, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. equipped with the electronic device. Of course, the memory 113 may also include both the internal storage unit of the electronic device and its external storage device. In this embodiment, the memory 11 is generally used to store the operating system and various computer programs installed in the electronic device, such as the program code of the vulnerability detection program of the smart contract, etc. In addition, the memory 113 can also be used to temporarily store various types of data that have been output or are to be output.
[0123] Figure 3 Only an electronic device having components 111 - 114 is shown, but it is understood that implementing all of the components shown is not a requirement, and more or fewer components may alternatively be implemented.
[0124] In one embodiment of the present application, the processor 111 is used to implement the vulnerability detection method of the smart contract provided by any one of the aforementioned method embodiments when executing the program stored in the memory 113, including:
[0125] Get the target smart contract to be tested;
[0126] Generate a directed graph of the target smart contract;
[0127] Extracting node feature vectors of the directed graph using a pre-trained feature extraction model;
[0128] Based on the node feature vector of the directed graph and the adjacency matrix of the directed graph, a vulnerability detection result of the target smart contract is identified.
[0129] For a detailed description of the above steps, please refer to the above Figure 2 Functional module diagram of an embodiment of a vulnerability detection device 100 for smart contracts and Figure 1 Description of a flowchart of an embodiment of a method for detecting vulnerabilities in smart contracts.
[0130] In addition, the embodiment of the present application also proposes a computer-readable storage medium, which can be non-volatile or volatile. The computer-readable storage medium includes a data storage area and a program storage area, and the program storage area stores a vulnerability detection program for a smart contract. When the vulnerability detection program for a smart contract is executed by a processor, the following operations are implemented:
[0131] Get the target smart contract to be tested;
[0132] Generate a directed graph of the target smart contract;
[0133] Extracting node feature vectors of the directed graph using a pre-trained feature extraction model;
[0134] Based on the node feature vector of the directed graph and the adjacency matrix of the directed graph, a vulnerability detection result of the target smart contract is identified.
[0135] The specific implementation of the computer-readable storage medium of the present application is roughly the same as the specific implementation of the vulnerability detection method of the above-mentioned smart contract, and will not be repeated here.
[0136] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0137] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a general hardware platform, and of course, by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the relevant technology can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0138] It should be noted that the descriptions involving "first", "second", etc. in this application are only for descriptive purposes and cannot be understood as indicating or implying their relative importance or implicitly indicating the number of technical features indicated. Therefore, the features defined as "first" and "second" may explicitly or implicitly include at least one of the features. In addition, the technical solutions between the various embodiments can be combined with each other, but they must be based on the ability of ordinary technicians in this field to implement them. When the combination of technical solutions is contradictory or cannot be implemented, it should be deemed that such combination of technical solutions does not exist and is not within the scope of protection required by this application.
[0139] It should be understood that the terms used herein are only for the purpose of describing specific example embodiments and are not intended to be limiting. Unless the context clearly indicates otherwise, the singular forms "one", "an" and "said" as used herein may also be meant to include plural forms. The terms "include", "comprise", "contain", and "have" are inclusive, and therefore specify the existence of stated features, steps, operations, elements and / or parts, but do not exclude the existence or addition of one or more other features, steps, operations, elements, parts, and / or combinations thereof. The method steps, processes, and operations described herein are not interpreted as necessarily requiring them to be performed in the specific order described or illustrated, unless the execution order is clearly indicated. It should also be understood that additional or alternative steps may be used.
[0140] The above description is only a specific implementation of the present application, so that those skilled in the art can understand or implement the present application. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein can be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application will not be limited to the embodiments shown herein, but will conform to the widest range consistent with the principles and novel features applied for herein.
Claims
1. A method for detecting vulnerabilities in smart contracts, characterized in that: The method comprises: Get the target smart contract to be tested; Generate a directed graph of the target smart contract; Extracting node feature vectors of the directed graph using a pre-trained feature extraction model; Based on the node feature vector of the directed graph and the adjacency matrix of the directed graph, a vulnerability detection result of the target smart contract is identified.
2. The method for detecting a vulnerability in a smart contract according to claim 1, wherein: The generating the directed graph of the target smart contract comprises: Analyze the calling relationship between the functions of the target smart contract; A directed graph of the target smart contract is generated with the functions of the target smart contract as nodes and the call relationships as edges.
3. The method for detecting a vulnerability in a smart contract according to claim 1, wherein: The step of extracting the node feature vector of the directed graph by using a pre-trained feature extraction model includes: Determine the starting position and the ending position of the function corresponding to each node in the directed graph by using a regular expression; Generate a target text corresponding to each function according to the starting position and the ending position; The target text is input into the feature extraction model, and the features of the target text are extracted as node feature vectors of the directed graph.
4. The method for detecting a vulnerability in a smart contract according to claim 1, wherein: The feature extraction model is obtained based on the training of the large language model. The training process of the feature extraction model includes: Get a preset number of sample smart contract code files; Concatenate the code files of all sample smart contracts to obtain the target text file; The target text file is used as an input of a large language model to perform an unsupervised training operation to obtain the feature extraction model.
5. The method for detecting a vulnerability in a smart contract according to claim 1, wherein: The identifying the vulnerability detection result of the target smart contract based on the node feature vector of the directed graph and the adjacency matrix of the directed graph includes: Divide the directed graph into N multi-hop subgraphs, where N represents the number of nodes in the directed graph; Inputting the node feature vector corresponding to each node, the multi-hop subgraph and the adjacency matrix of the directed graph into the graph neural network to obtain the local features of each node; Based on the local features of each node, obtaining the target features of the directed graph; The target features of the directed graph are input into a multi-layer perceptron network to obtain a vulnerability detection result of the target smart contract.
6. The method for detecting a vulnerability in a smart contract according to claim 5, wherein: The obtaining the target feature of the directed graph based on the local feature of each node includes: Inputting the local features of each node into the self-attention mechanism network to obtain the global features of each node; Adding the global features of each node to obtain an addition result; The added result is divided by the number of nodes to obtain the target feature of the directed graph.
7. The method for detecting a vulnerability in a smart contract according to any one of claims 1 to 6, characterized in that: The method further comprises: The vulnerability detection result of the target smart contract is sent to a preset terminal.
8. A smart contract vulnerability detection device, characterized in that: The device comprises: Acquisition module: used to obtain the target smart contract to be detected; Generation module: used to generate a directed graph of the target smart contract; Extraction module: used to extract node feature vectors of the directed graph using a pre-trained feature extraction model; Detection module: used to identify the vulnerability detection result of the target smart contract based on the node feature vector of the directed graph and the adjacency matrix of the directed graph.
9. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, configured to implement the vulnerability detection method for a smart contract according to any one of claims 1 to 7 when executing a program stored in a memory.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the vulnerability detection method for the smart contract as described in any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Program slice vulnerability detection method and system, computer and storage medium
CN115455438A
Method, device and equipment for detecting contract vulnerability of block chain
CN116150770A
Intelligent contract vulnerability detection method based on graph neural network
CN116383832A
Code analysis method and device based on neural network, and electronic equipment
CN116578980A
Operation system code vulnerability detection method, device and equipment and medium
CN116820562A
Cited By
Code security analysis method and device, computer equipment and storage medium
CN119272292A
Large graph model-based block chain system high-centrality intelligent contract identification method
CN119848825A
