Intelligent contract vulnerability detection method and device, equipment and storage medium

By converting the source code of the smart contract into bytecode, and combining the feature fusion technology of the multi-scale feature map, the BiLSTM model inputs the multi-head attention mechanism for vulnerability detection, the problem of insufficient efficiency and accuracy of smart contract vulnerability detection in the existing technology is solved, and more efficient and accurate vulnerability detection is achieved.

CN119513880APending Publication Date: 2025-02-25JIANGSU ANHENG NETWORK SECURITY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411666558.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-20
Publication Date
2025-02-25

AI Technical Summary

Technical Problem

The existing smart contract vulnerability detection methods have technical flaws in data flow graph simplification, model complexity, graph structure processing complexity, and computational cost of cross-modal learning. They pay too much attention to coarse-grained feature extraction and neglect fine-grained feature learning, resulting in limited detection efficiency and accuracy.

Method used

By obtaining the source code of the smart contract, converting it into bytecode, and using a bytecode network based on the Bert model and the DR-GCN model to perform feature extraction, we obtain the bytecode features. At the same time, a multi-scale feature map is created based on the historical smart contract dataset, node and edge features are extracted, and feature fusion is performed to form historical fusion features. The bytecode features and historical fused features are input to the BiLSTM model based on the multi-head attention mechanism for vulnerability detection.

Benefits of technology

It significantly improves the accuracy and comprehensiveness of smart contract vulnerability detection, reduces the complexity of vulnerability detection, improves detection efficiency, and enhances the security and reliability of smart contracts, reducing computing costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119513880A_ABST
    Figure CN119513880A_ABST
Patent Text Reader

Abstract

The invention discloses an intelligent contract vulnerability detection method and device, equipment and a storage medium, and relates to the technical field of network security, and the method comprises the steps: obtaining a source code of a current to-be-detected intelligent contract, and converting the source code into a byte code to obtain a current byte code; inputting the current byte code into a byte code network obtained by training a Bert model and a DR-GCN model by using a historical byte code data set, and carrying out feature extraction to obtain byte code features; inputting the byte code features and the historical fused features into a trained contract vulnerability detection model of a BiLSTM model based on a multi-head attention mechanism for vulnerability detection to obtain a vulnerability detection result; wherein the historical fused features are features obtained by fusing the node features and the edge features. According to the method, the complexity of vulnerability detection can be reduced, the accuracy and comprehensiveness of vulnerability detection and the detection efficiency are improved, and the security and reliability of the smart contract are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of network security technology, and in particular to a smart contract vulnerability detection method, device, equipment and storage medium. Background Art

[0002] As a transaction medium, blockchain can make information transparent and tamper-proof. Through precisely designed consensus protocols (such as proof of work), it can ensure transaction security and cleverly manage the creation of transaction units. The blockchain platform can realize trustless value transfer between network entities, enabling developers to provide smart contracts (a type of intelligent program), thus creating a new era of decentralized programmable transactions.

[0003] However, smart contracts running on blockchain platforms are designed to control digital assets using predefined rules, usually involving large amounts of cryptocurrency value, and therefore inevitably attract the attention of hackers, and their security issues have led to huge economic losses. In addition, once deployed, smart contracts cannot be simply modified, and their defects are irreversible unless the blockchain itself is reversed, a process that requires controlling most of the network's computing power. Therefore, in order to reduce security risks, it is necessary to detect vulnerabilities in smart contracts before launching them on blockchain platforms.

[0004] At present, the mainstream smart contract vulnerability detection solution uses deep learning technology, and when analyzing the source code of the smart contract, it represents it as a text sequence, tree or graph, such as through TMP (Time Messaging Protocol), Bi-RNN (Bidirectional Recurrent Neural Network), GCN (Graph Convolutional Network) and other deep learning networks, and performs vulnerability detection on smart contracts based on graph data. However, although various types of deep learning networks have their own advantages in smart contract vulnerability detection, they have technical defects in data flow graph simplification, model complexity, graph structure processing complexity, and computational cost of cross-modal learning. In addition, the above-mentioned deep learning methods based on graph data focus too much on coarse-grained feature extraction and ignore the fine-grained feature learning of graph data, such as the structure of the graph and the correlation between different graphs (such as control flow graphs, data flow graphs, program dependency graphs, etc.). The structure of the graph contains the call execution path information between the functions or code statements of the contract code, and different representation graphs of smart contracts have their own semantic characteristics. Therefore, mining the correlation between different representation graphs can make up for the missing semantic information. In addition, the loss fluctuations during network training will lead to overfitting, thereby affecting the learning effect of the model.

[0005] In addition, most of the current vulnerability detection methods are based on source code detection. However, in actual scenarios, such as blockchain platforms, developers generally do not open source their source code, which brings huge challenges to smart contract vulnerability detection. Detectors can only perform vulnerability detection tasks by analyzing the underlying bytecode, but it is very difficult to recover the semantic information of control flow and data flow from the bytecode, which affects the efficiency and accuracy of detection. Summary of the invention

[0006] In view of this, the purpose of this application is to provide a smart contract vulnerability detection method, device, equipment and storage medium, which can more comprehensively and accurately represent the characteristics of smart contracts, reduce the complexity of vulnerability detection, realize the extraction and utilization of fine-grained features, significantly improve the accuracy and comprehensiveness of vulnerability detection, and improve the detection efficiency and the security and reliability of smart contracts, enhance user trust, and in the absence of source code, can still efficiently and accurately complete the smart contract vulnerability detection task. The specific scheme is as follows:

[0007] In the first aspect, the present application discloses a smart contract vulnerability detection method, which is applied to a blockchain platform, comprising:

[0008] Obtain the source code of the smart contract to be tested to obtain the current source code, and convert the current source code into bytecode to obtain the current bytecode;

[0009] Input the current bytecode into the trained bytecode network to extract features of the current bytecode to obtain bytecode features; the bytecode network is a model obtained by training the Bert model and the DR-GCN model using the historical bytecode dataset;

[0010] The bytecode features and the historical fused features are input into the trained contract vulnerability detection model to detect the vulnerabilities in the smart contract to be detected and obtain the contract vulnerability detection results; the historical fused features are the features obtained by fusing the node features and the edge features, and the node features and the edge features are the features obtained by extracting the nodes and edges in the multi-scale feature graph created based on the historical smart contract data set; the trained contract vulnerability detection model is a model obtained by training the BiLSTM model based on the multi-head attention mechanism using the historical smart contract data set.

[0011] Optionally, the step of obtaining the source code of the smart contract to be detected, before obtaining the current source code, further includes:

[0012] Collect different types of historical smart contracts to obtain a historical smart contract data set, and respectively obtain the source code of each historical smart contract in the historical smart contract data set to obtain multiple historical source codes;

[0013] Convert each of the historical source codes into a graph data format to obtain a multi-scale feature graph including an abstract syntax tree, a control flow graph, and a data flow graph;

[0014] Extracting features of nodes and edges in the multi-scale feature graph respectively to obtain node features and edge features;

[0015] The node feature and the edge feature are subjected to feature fusion to obtain the historical fused feature.

[0016] Optionally, the converting of each of the historical source codes into a graph data format to obtain a multi-scale feature graph including an abstract syntax tree, a control flow graph, and a data flow graph includes:

[0017] Convert each of the historical source codes into an abstract syntax tree according to the grammatical structure of the code;

[0018] A control flow graph and a data flow graph of each of the historical source codes are generated based on the abstract syntax tree, and the abstract syntax tree, the control flow graph and the data flow graph are integrated to obtain the multi-scale feature graph.

[0019] Optionally, the generating a control flow graph and a data flow graph of each of the historical source codes based on the abstract syntax tree includes:

[0020] Performing a depth-first search on the abstract syntax tree to traverse and search each node in the abstract syntax tree, and discarding nodes irrelevant to vulnerability detection during the traversal process to obtain a post-search syntax tree;

[0021] A breadth-first search is performed on the post-search syntax tree to traverse and search each node in the post-search syntax tree, and during the traversal process, edges are added between different nodes in the post-search syntax tree according to the execution order of the historical source code and the information flow in the historical source code to obtain a control flow graph and a data flow graph.

[0022] Optionally, the extracting features of nodes and edges in the multi-scale feature graph respectively to obtain node features and edge features includes:

[0023] Identify nodes and edges in the multi-scale feature graph to obtain a node set and an edge set;

[0024] Initializing the nodes in the node set by using the Word2Vec model to obtain initialized nodes, and initializing the edges in the edge set by using the degree centrality index to obtain initialized edges;

[0025] A recursive neural network is used to extract features of the initialized nodes in the multi-scale feature map to obtain node features, and a multi-head attention mechanism is used to extract features of the initialized edges in the multi-scale feature map to obtain edge features.

[0026] Optionally, the fusing the node feature and the edge feature to obtain the historical fused feature includes:

[0027] Performing feature aggregation on all the node features and all the edge features respectively to obtain aggregated node features and aggregated edge features;

[0028] The aggregated node features and the aggregated edge features are subjected to feature fusion to obtain the historical fused features.

[0029] Optionally, the smart contract vulnerability detection method further includes:

[0030] Convert the source codes of all historical smart contracts in the historical smart contract dataset into bytecodes to obtain a historical bytecode dataset;

[0031] The historical bytecode dataset is input into the Bert model and DR-GCN model for training to obtain the bytecode network, and the historical bytecode features output by the bytecode network during the training process and the historical fusion features are input into the BiLSTM model based on the multi-head attention mechanism for training to obtain the trained contract vulnerability detection model.

[0032] In the second aspect, the present application discloses a smart contract vulnerability detection device, which is applied to a blockchain platform, including:

[0033] The source code acquisition module is used to obtain the source code of the smart contract to be tested and obtain the current source code;

[0034] A source code conversion module, used for converting the current source code into bytecode to obtain current bytecode;

[0035] A feature extraction module, used for inputting the current bytecode into a trained bytecode network to extract features from the current bytecode to obtain bytecode features; the bytecode network is a model obtained by training a Bert model and a DR-GCN model using a historical bytecode dataset;

[0036] A vulnerability detection module is used to input the bytecode features and the historical fused features into the trained contract vulnerability detection model to detect the vulnerabilities in the smart contract to be detected and obtain the contract vulnerability detection results; the historical fused features are the features obtained by fusing the node features and the edge features, and the node features and the edge features are the features obtained by extracting the nodes and edges in the multi-scale feature graph created based on the historical smart contract data set; the trained contract vulnerability detection model is a model obtained by training the BiLSTM model based on the multi-head attention mechanism using the historical smart contract data set.

[0037] In a third aspect, the present application discloses an electronic device, comprising a processor and a memory; wherein, when the processor executes a computer program stored in the memory, the aforementioned smart contract vulnerability detection method is implemented.

[0038] In a fourth aspect, the present application discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, the aforementioned smart contract vulnerability detection method is implemented.

[0039] It can be seen that the present application is applied to the blockchain platform, first obtaining the source code of the smart contract to be detected to obtain the current source code, and converting the current source code into bytecode to obtain the current bytecode, and then inputting the current bytecode into the bytecode network obtained by training the Bert model and the DR-GCN model using the historical bytecode dataset, so as to extract features from the current bytecode to obtain bytecode features, and then inputting the bytecode features and the historical fused features into the trained contract vulnerability detection model obtained by training the BiLSTM model based on the multi-head attention mechanism using the historical smart contract dataset, so as to detect the vulnerabilities in the smart contract to be detected and obtain the contract vulnerability detection results, wherein the historical fused features are the features obtained by fusing the node features and the edge features, and the node features and the edge features are the features obtained by extracting features from the nodes and edges in the multi-scale feature graph created based on the historical smart contract dataset.When performing vulnerability detection on smart contracts, the present application first obtains the source code of the smart contract to be detected, then converts the source code into bytecode and inputs it into a bytecode network obtained by training the Bert model and the DR-GCN model using a historical bytecode dataset for feature extraction to obtain bytecode features. Since the Bert model can accurately represent the semantic information in the text, and the DR-GCN model can ensure that the representation of all classes in the graph data is equal, the bytecode features extracted by the bytecode network can predict the missing source code in the smart contract, so that in the absence of source code, the smart contract vulnerability detection task can still be completed efficiently and accurately; further, the bytecode features and the historical fused features are input into a contract vulnerability detection model obtained by training the BiLSTM model based on the multi-head attention mechanism using the historical smart contract dataset for vulnerability detection. It should be pointed out that the historical fused features are the features obtained by fusing the node features and the edge features, wherein the node features The edge features are features obtained after feature extraction of nodes and edges in a multi-scale feature graph created based on a historical smart contract data set. That is, the present application pre-creates a multi-scale feature graph based on a historical smart contract data set, and extracts features of nodes and edges in the multi-scale feature graph, and then fuses the extracted node features and edge features. When performing vulnerability detection, the fused features are input as known information together with the bytecode features into the contract vulnerability detection model for vulnerability detection. By creating a multi-scale feature graph, including an abstract syntax tree, a control flow graph, and a data flow graph, etc., the features of the smart contract can be more comprehensively and accurately represented, and the complexity of vulnerability detection can be reduced, thereby realizing the extraction and utilization of fine-grained features, significantly improving the accuracy and comprehensiveness of vulnerability detection. In addition, compared with traditional vulnerability detection schemes, it reduces dependence on expert knowledge, reduces computing costs, improves detection efficiency, and improves the security and reliability of smart contracts. At the same time, it enhances user trust, thereby promoting the popularization and application of blockchain technology. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] 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, the drawings described below are only embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.

[0041] Figure 1 A flow chart of a smart contract vulnerability detection method disclosed in this application;

[0042] Figure 2 A specific SCSG feature extraction framework diagram disclosed in this application;

[0043] Figure 3 A specific information enhancement framework diagram disclosed in this application;

[0044] Figure 4 A schematic diagram of a specific bytecode network and feature extraction process disclosed in this application;

[0045] Figure 5 A schematic diagram of a specific smart contract vulnerability detection process disclosed in this application;

[0046] Figure 6 This is a schematic diagram of the structure of a smart contract vulnerability detection device disclosed in this application;

[0047] Figure 7 This is a structural diagram of an electronic device disclosed in this application. DETAILED DESCRIPTION

[0048] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only 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 creative work are within the scope of protection of this application.

[0049] The present application embodiment discloses a smart contract vulnerability detection method, which is applied to the blockchain platform. Figure 1 As shown, the method includes:

[0050] Step S11: Obtain the source code of the smart contract to be detected, obtain the current source code, and convert the current source code into bytecode to obtain the current bytecode.

[0051] It should be pointed out that the smart contract vulnerability detection solution proposed in this application is applied to the blockchain platform. When smart contract vulnerability detection is required, the source code (Source Code) of the current smart contract to be detected is first obtained, and then the obtained current source code is converted into bytecode (Byte Code) to obtain the current bytecode. Specifically, the current source code can be converted into bytecode through the Solidity compiler.

[0052] Step S12: Input the current bytecode into the trained bytecode network to extract features of the current bytecode to obtain bytecode features; the bytecode network is a model obtained by training the Bert model and the DR-GCN model using a historical bytecode dataset.

[0053] In this embodiment, after the current source code is converted into bytecode to obtain the current bytecode, the current bytecode is further input into a bytecode network obtained by training a Bert (Bidirectional Encoder Representations from Transformers, a bidirectional language representation model based on the Transformer architecture) model and a DR-GCN (Dual-Regularized Graph Convolutional Network, a model that integrates entity description and relationship graph convolutional neural network (R-GCN)) model using a historical bytecode dataset, thereby performing feature extraction on the current bytecode to obtain corresponding bytecode features. Among them, the Bert model can more accurately represent the semantic information in the text by introducing bidirectional context information. The DR-GCN model is used to deal with the imbalance problem in graph data. The GCN (Graph Convolutional Network) in the DR-GCN model can realize the convolution operation of graph structured data by combining the structure of the graph and the features of the nodes, making up for the shortcomings of traditional CNN (convolutional neural network) and RNN (recurrent neural network) in processing irregular structured data, and can be applied to graph learning tasks such as node classification, graph classification, edge prediction and graph embedding.

[0054] It is understandable that in actual scenarios, developers generally do not open source the complete source code, so the currently obtained source code may be missing, which brings challenges to contract vulnerability detection. In order to overcome the above defects, this application inputs the current bytecode into the trained bytecode network based on the Bert model and the DR-GCN model, so as to realize the prediction of the missing source code in the smart contract to be detected, that is, to predict the missing source code information through the bytecode network.

[0055] Step S13: Input the bytecode features and the historical fused features into the trained contract vulnerability detection model to detect the vulnerabilities in the smart contract to be detected and obtain the contract vulnerability detection results; the historical fused features are the features obtained by fusing the node features and the edge features, and the node features and the edge features are the features obtained by extracting the nodes and edges in the multi-scale feature graph created based on the historical smart contract data set; the trained contract vulnerability detection model is a model obtained by training the BiLSTM model based on the multi-head attention mechanism using the historical smart contract data set.

[0056] In this embodiment, after extracting features from the current bytecode through the bytecode network to obtain bytecode features, the bytecode features and the historical fused features are input into a trained contract vulnerability detection model obtained by training a BiLSTM model based on a multi-head attention mechanism using the historical smart contract dataset, thereby detecting vulnerabilities in the smart contract to be detected and obtaining contract vulnerability detection results; wherein the historical fused features are features obtained by fusing node features and edge features, and the node features and the edge features are features obtained by extracting features from nodes and edges in a multi-scale feature graph created based on the historical smart contract dataset.

[0057] It should be pointed out that before performing smart contract vulnerability detection, this application pre-creates and trains two models, namely a bytecode network (model) for predicting missing source code in the smart contract to be detected and a trained contract vulnerability detection model for vulnerability detection. The specific training process of the two models is introduced below.

[0058] In this embodiment, the method of obtaining the source code of the smart contract to be detected, before obtaining the current source code, specifically also includes: collecting different types of historical smart contracts to obtain a historical smart contract data set, and respectively obtaining the source code of each historical smart contract in the historical smart contract data set to obtain multiple historical source codes; converting each of the historical source codes into a graph data format to obtain a multi-scale feature graph including an abstract syntax tree, a control flow graph, and a data flow graph; respectively extracting features from nodes and edges in the multi-scale feature graph to obtain node features and edge features; and performing feature fusion on the node features and the edge features to obtain the historical fused features. In this embodiment, before performing smart contract vulnerability detection, different types of historical smart contracts are first collected to obtain a historical smart contract data set, and then the source code of each historical smart contract in the historical smart contract data set is obtained respectively, so as to obtain the historical source code of all historical smart contracts. Then, considering that the source code is a structured complex data form, in order to effectively analyze the vulnerabilities in the source code, the source code can be converted into a representation form that is easier to analyze. Specifically, the historical source code corresponding to each historical smart contract is converted into a multi-scale representation. For example, each historical source code is converted into a graph data format through a code parsing tool to obtain an output including an abstract syntax tree (AST), a control flow graph (CFG), and a data flow graph (DFG), and the abstract syntax tree, the control flow graph, and the data flow graph are integrated to obtain a multi-scale feature graph (Semantic Code Structure Graph, SCSG); where DFG is used to describe the flow of data values ​​in the code, nodes are variables, edges are data flows between variables, and it helps to understand the data dependencies in the code; CFG is used to describe the execution order of statements in the code, nodes are a group of codes, edges are call relationships, and it helps to understand the execution order of the code; AST is used to describe the grammatical structure of the code, nodes are programming language symbols, edges are grammatical rules, and it helps to understand the structure and execution logic of the code. Furthermore, feature extraction is performed on all nodes and all edges in the multi-scale feature graph (i.e., SCSG) corresponding to each historical smart contract to obtain corresponding node features and edge features, and the two features are fused to obtain historical fused features. The two types of features in the historical fused features can be used as known information to participate in real-time smart contract vulnerability detection, thereby achieving more accurate detection. For details, see Figure 2As shown in the figure, node feature extraction (Node Feature Extract) is performed on each node in the multi-scale feature graph (ie, SCSG), and at the same time, edge feature extraction (Edge Feature Extract) is performed on each edge in the multi-scale feature graph (ie, SCSG), and then the extracted node feature (ie, Node Feature) and edge feature (Edge Feature) are fused to obtain the fused feature.

[0059] The converting of each of the historical source codes into a graph data format to obtain a multi-scale feature graph including an abstract syntax tree, a control flow graph, and a data flow graph may specifically include: converting each of the historical source codes into an abstract syntax tree according to the syntax structure of the code; generating a control flow graph and a data flow graph of each of the historical source codes based on the abstract syntax tree, and integrating the abstract syntax tree, the control flow graph, and the data flow graph to obtain the multi-scale feature graph. That is, first converting the historical source code into an AST according to the syntax structure of the code, then generating corresponding CFG and DFG based on the AST, and then integrating the AST, CFG, and DFG to obtain a multi-scale feature graph (i.e., SCSG). It is understandable that AST can well represent the structural information and grammatical information in the code, but in addition to the above information, the source code itself also has many different dimensional features such as data flow and control flow, and these features are very critical in the process of defect detection. Therefore, it is not enough to use only AST as a code representation. AST also lacks basic details such as data flow and statement execution order. The nodes in CFG and DFG represent different program elements, such as function calls or variables in smart contract functions. At the same time, edges are used to capture the relationship between different nodes (ie, program elements), such as control flow and data flow. In addition, the order of edges is crucial because it represents the execution order of the smart contract source code segment. Therefore, on the basis of AST, combining CFG and DFG to build SCSG can integrate different dimensional information of the code, and in the subsequent vulnerability detection based on SCSG, it can enable the vulnerability detection model to understand and analyze smart contracts from multiple angles, thereby improving the accuracy of vulnerability detection.

[0060] In a specific implementation, the generation of the control flow graph and data flow graph of each historical source code based on the abstract syntax tree may specifically include: performing a depth-first search on the abstract syntax tree to traverse and search each node in the abstract syntax tree, and discarding nodes irrelevant to vulnerability detection during the traversal process to obtain a post-search syntax tree; performing a breadth-first search on the post-search syntax tree to traverse and search each node in the post-search syntax tree, and adding edges between different nodes in the post-search syntax tree according to the execution order of the historical source code and the information flow in the historical source code during the traversal process to obtain a control flow graph and a data flow graph. It should be noted that AST can fully represent the syntax and semantic information of the smart contract code, including the following aspects: (1) data information, such as the contract parameter list and its corresponding attribute information; (2) operation information, such as the statement execution order, operators, and API (Application Programming Interface) call information related to the programming language; (3) control flow information, such as the relationship between the control flow and the data flow. Specifically, the AST of the smart contract can be generated by an AST generation tool (such as Python-Solidity-Parser).

[0061] However, AST may contain information irrelevant to vulnerability detection, thereby confusing the training of the neural network. To solve this problem, this application normalizes AST in the process of building SCSG. Specifically, AST can be simplified to eliminate redundant nodes irrelevant to the vulnerability detection task, and edges can be added to AST to illustrate the relationship between data flow and control flow. Specifically, a depth-first search (DFS) may be performed on the AST to traverse and search each node in the AST, and nodes irrelevant to vulnerability detection may be discarded during the traversal process, such as attribute field (such as Name, Type, Body, Parameters, etc.) nodes, flag field (such as isStateVar, isIndexed, storageLocation, etc.) nodes, and description field (such as TypeName, ExpressionStatement, IfStatement, etc.) nodes, etc., to obtain a post-search syntax tree. Next, a breadth-first search (BFS) may be performed on the post-search syntax tree to explore and traverse all adjacent nodes in the post-search syntax tree in turn, and edges may be added between different nodes in the post-search syntax tree according to the execution order of the historical source code and the information flow in the historical source code during the traversal process (that is, edges may be added between the remaining nodes of the AST after discarding irrelevant nodes), thereby obtaining CFG and DFG. By reducing redundant information during the traversal process and retaining only information related to the detection target, the efficiency of subsequent vulnerability analysis can be improved. The AST representation generated by this normalization process can be more focused on the vulnerability detection task.

[0062] It should be pointed out that the process of building SCSG based on AST involves the organization of code elements in smart contract functions, including function entry, input parameter list and one or more statements, and in the constructed SCSG, the connecting edges between node elements can be regarded as the order of code execution. For expression nodes (nodes other than parameter lists), control flow and structural elements (such as subtrees, conditional statements, etc.) (represented by AST nodes) can be converted into edges connecting related nodes. Therefore, this application performs a breadth-first search on the post-search syntax tree to identify the control flow and structural information in the post-search syntax tree, and add corresponding edges between nodes. In addition, the vulnerability control flow and data flow information can be used to add forward edges (AST edges), data flow edges and control flow edges in the post-search syntax tree, respectively. Moreover, when traversing the post-search syntax tree, it is also possible to match the corresponding nodes in the DFG (Data Flow Graph, program dependency graph), CFG, and the post-search syntax tree, and add specific node information to the SCSG. Therefore, the final constructed SCSG contains three types of edges, namely, AST edges (which can be regarded as forward edges, which are added according to the execution order of the code segments during the standardization process), DFG edges, and CFG edges.

[0063] In this embodiment, the feature extraction of the nodes and edges in the multi-scale feature graph is performed respectively to obtain node features and edge features, which may specifically include: identifying the nodes and edges in the multi-scale feature graph to obtain a node set and an edge set; initializing the nodes in the node set using the Word2Vec model to obtain initialized nodes, and initializing the edges in the edge set using the degree centrality index to obtain initialized edges; extracting features from the initialized nodes in the multi-scale feature graph using a recursive neural network to obtain node features, and extracting features from the initialized edges in the multi-scale feature graph using a multi-head attention mechanism to obtain edge features. In this embodiment, before extracting features from the nodes and edges in the multi-scale feature graph, each node and edge needs to be initialized. Specifically, after obtaining the multi-scale feature graph (i.e., SCSG), the nodes and edges in the SCSG are identified to obtain a node set and an edge set, and then the Word2Vec (word to vector, a tool for converting words into vector form) model is used to initialize each node in the node set to obtain initialized nodes, see Figure 3As shown, after converting the source code in the historical smart contract dataset into AST, CFG and DFG are generated based on AST, and a multi-scale feature graph (i.e. SCSG) containing 6 nodes and connection relationships is constructed based on AST, CFG and DFG. Then, the invocation path information of each node in SCSG is obtained, and each node in SCSG is traversed by BFS to learn the nodes in SCSG through the Word2Vec model, and the invocation path information is added to the corresponding node of the SCSG. It should be pointed out that for each BFS group of a node, it can be mapped to a node, the set of adjacent nodes is obtained, and it is input into the pre-trained Word2Vec model, which will output In the feature set X, N is the number of nodes in SCSG, and D is the dimension of the node feature. In addition, the Word2Vec model is specifically used to learn the continuous feature representation of nodes in SCSG and store the recognized knowledge obtained from adjacent nodes. By using breadth-first search (BFS) to initialize the node features of each node, the call relationship between function variables can be incorporated into the node features of the contract. In addition, grammatical and semantic information such as AST, DFG, CFG, etc. can also be incorporated. This information is very important for vulnerability detection. The above initialization process can fully capture the structure and grammatical details of the smart contract. Next, considering that the edge is a call relationship between adjacent nodes or variables, the call path information of each node can be combined, and the edges in the edge set can be initialized using the degree centrality indicator and the initialized nodes.

[0064] It should be pointed out that although nodes irrelevant to vulnerability detection are discarded in the process of building SCSG based on AST, that is, most of the noise is filtered, thereby reducing the impact of irrelevant variables and statements on the smart contract vulnerability detection task, the nodes related to the vulnerability are also divided into important nodes and minor nodes. Therefore, in order to highlight the important nodes and the corresponding key call path information, the edges in SCSG need to be initialized. Edge initialization specifically refers to the mutual fusion of features between two adjacent nodes of the edge. Specifically, the center point attribute of the node degree (which determines the degree of the node with other nodes, that is, the number of connections) can be used to fuse the features of two adjacent nodes. By using the degree distribution of adjacent nodes to initialize each edge, different node features can be fused into edge features. It should be pointed out that when the edge features are subsequently fused with the node features, the order of the edges relative to the nodes needs to be maintained. In addition, the number of nodes must remain unchanged. The specific edge initialization process includes: 1) calculating the degree of each node; 2) finding the edge with the smallest node degree; 3) deleting the edge with the smallest node degree. Through the above three steps, the order and number of edges can be kept the same as those of nodes; if the number of edges is less than the number of nodes, a 0 vector can be used to replace the corresponding edge. Finally, for the ordered edges, a degree-centric algorithm can be introduced to assign features.

[0065] It should be pointed out that the node representation generated by the Word2Vec model contains part of the topological information of the SCSG graph. After initialization, the traversal algorithm (such as the BFS algorithm) uses the graph data to reselect features for each node, and incorporates the call path information into the SCSG graph. In the final SCSG, each node contains not only the original information of the variable or function call, but also the complete contract call information chain (i.e., the path in the call path information), which helps to detect cross-contract vulnerabilities because contracts have both external calls and internal calls. In addition, when initializing the edges, the fusion method used in this application is to enhance the representativeness of important nodes in the edge features by utilizing the degree center attribute of the nodes, which supplements the features that may not be learned or lost in the node features. By initializing the nodes and edges in the SCSG, the element features, description syntax, and partial semantic information of the code can be extracted from the SCSG. At the same time, the graphical structure features that describe the semantic information in the code can be captured, and semantic features can be obtained from the edges, including data flow, control flow, and code structure.

[0066] Furthermore, after initializing the nodes and edges in the SCSG, a recurrent neural network (RNN) can be used to extract features of the initialized nodes in the multi-scale feature graph (i.e., SCSG) to obtain node features, and a multi-head attention mechanism can be used to extract features of the initialized edges in the multi-scale feature graph (i.e., SCSG) to obtain edge features.

[0067] It should be pointed out that each node in SCSG contains the call path information of the contract function and the semantic structure information of the specific vulnerability. Considering that node features can be aggregated according to node timing, vulnerability information can be extracted without introducing noise or losing key information; message passing based on node timing (referring to the order of code execution) can make full use of data structure types, and multiple executions of the information propagation network help maintain the integrity of the information, but the time interval for learning context information is large; sequence-based learning may cause forgetting of previous information or learning important information as general information, thereby affecting the vulnerability detection task. In order to overcome the above problems, this application uses a recursive neural network (ie, RNN) to recursively operate the information propagation network, thereby reducing the noise that may affect the vulnerability detection task and better learning the semantic and structural information that is very important for the detection task. In addition, in order to prevent overfitting, the node features of each node in SCSG can be pooled, and at the same time, the nodes can be sorted to put important information nodes in the front position. It should be pointed out that in the process of generating CFG and DFG based on AST, since irrelevant nodes in AST have been deleted, variables or function calls that are not related to the detection task will not be introduced. Furthermore, nodes with rich semantic and structural information can be reordered to improve the learning of vulnerability information during subsequent edge feature splicing, thereby improving the learning efficiency of the model.

[0068] The specific process of extracting node features is as follows: During the feature propagation process, the features of the current node will be transmitted to the adjacent end nodes of the propagation edge. At time t, the features are transmitted to the propagation edge. , Indicates the type of the starting node of the i-th round of propagation edge at time t, represents the hidden features of the starting node of the propagation edge at time t, where i represents the round of propagation, represents the features to be propagated, The calculation formula is:

[0069] ; In the formula, Represents the dot product operation;

[0070] After obtaining the features of the previous node, it is necessary to update the features of the terminal node of the current propagation edge and summarize the features of the previous node to the current node to obtain , The calculation formula is:

[0071] ;

[0072] ;

[0073] ;

[0074] ;

[0075] ;

[0076] In the formula, , , , , , are runtime parameters, including the weight matrix and bias vector.

[0077] Next, since the graph features have not been fully extracted and the graph sequence has propagation characteristics, the hidden state of the last node in the current propagation round can be With the hidden state of the first node Connect them together and use this concatenated feature as the initial feature for the next round of node propagation, and then start a new round of feature propagation. In this case, represents the hidden state of the Nth node, and represents the hidden state of the first node. Similarly, represents the initial hidden state of the first node in the (i+1)th round.

[0078] in, .

[0079] Finally, the node features of the final propagation graph are combined and concatenated with the node features of the initial propagation graph to obtain the features of each node , The calculation formula is:

[0080] .

[0081] Regarding the extraction of edge features based on the multi-head attention mechanism, it should be pointed out that after the features of each edge and node in SCSG are initialized, it is necessary to ensure that they have the same code execution timing. Considering that the information of code execution order is very important for vulnerability detection, this information can be used to locate the specific location of the vulnerability. In order to aggregate the features of nodes in SCSG, the node timing can be used to perform feature aggregation without destroying the timing of the original nodes. Before edge feature aggregation, position encoding needs to be used to add a timing label to each edge to maintain the timing of edges and nodes so that even if the labels are disrupted during aggregation, the labels can be used to reorder. Among them, the formula for position encoding is defined as follows:

[0082] ;

[0083] ;

[0084] In the formula, i represents the order, and the symbols p, dp, and d edge They represent the position of the edge in SCSG, the dimension of the position encoding, and the dimension of the edge feature respectively. and They represent the encoding values ​​of the 2dp and (2dp+1)th dimensions of the encoding vector at position p, respectively. Each dimension of the position encoding corresponds to a sine wave with a wavelength from 2π to 20000π. When using position encoding, the edge feature can be expressed as the following formula:

[0085] ; In the formula, Indicates the current edge feature;

[0086] Multi-head attention mechanism is used for input data , which can take advantage of queries and keys (each key has dimensions and values ​​(dimensions are ) Calculate the attention coefficient for each query set at the same time. Query and compile into a matrix , and the keys and values ​​are encoded in matrices and The attention relevance calculation process of edge sequence is as follows:

[0087] ;

[0088] In addition, the multi-head attention mechanism allows the model to focus on each segment of the sequence at the same time, then merge the attention outputs of all heads, and then obtain the final multi-head attention output after another linear transformation. , the specific calculation formula is:

[0089] .

[0090] In the formula, , is the parameter matrix. Then, after obtaining the final result from the multi-head attention mechanism, see Figure 2 As shown, a multilayer perceptron (MLP) and a fully connected output layer can be used to obtain the final aggregate edge feature f. The feature is defined as follows:

[0091] ;

[0092] In the formula, MH represents the calculation result of the multi-head attention module. Finally, after obtaining the node features and edge features of SCSG, these two types of features are spliced ​​and the splicing result is used as the source code feature of the contract function (i.e. SCSG) .

[0093] In this embodiment, the node features and the edge features are subjected to feature fusion to obtain the historical fused features, which may specifically include: performing feature aggregation on all the node features and all the edge features to obtain aggregated node features and aggregated edge features; performing feature fusion on the aggregated node features and aggregated edge features to obtain the historical fused features. In this embodiment, after obtaining the node features and edge features, all the node features and all the edge features may be subjected to feature aggregation to obtain two feature sets, and then the two feature sets (including semantic features of two different modalities) are fused to form the final graph features of the smart contract function (i.e., SCSG), i.e., the above-mentioned source code features. .

[0094] In this embodiment, before performing smart contract vulnerability detection, it specifically includes: converting the source code of all historical smart contracts in the historical smart contract data set into bytecode to obtain a historical bytecode data set; inputting the historical bytecode data set into a Bert model and a DR-GCN model for training to obtain the bytecode network, and inputting the historical bytecode features output by the bytecode network during the training process and the historical fusion features into a BiLSTM model based on a multi-head attention mechanism for training to obtain a trained contract vulnerability detection model. For details, see Figure 4 As shown in the figure, the source code of each historical smart contract in the historical smart contract dataset can be converted into bytecode first, and then a control flow graph (CFG) can be derived based on the bytecode obtained after the conversion. Then, the control flow graph (CFG) is used as input to train the Bert model and the DR-GCN model in sequence to obtain the trained bytecode network. Furthermore, based on the output result of the bytecode network (i.e., byte features) and the information enhancement technology (i.e., Figure 2 and Figure 3The source code features (i.e., fused features, including node features and edge features) obtained by fusion are input into the BiLSTM model based on the multi-head attention mechanism for training, thereby obtaining a model for vulnerability detection.

[0095] It should be pointed out that an essential stage of training the bytecode network (model) based on the Bert model and the DR-GCN model is to derive the control flow graph (CFG), which specifically includes segmenting bytecode blocks (i.e., nodes) and identifying edges that determine the control flow, where the bytecode block consists of a series of instructions. Since the BERT model has excellent performance in processing serialized instruction data, this application uses the BERT model to encode the bytecode blocks of smart contracts. The specific processing flow can be divided into the following three stages: In the pre-training stage of the BERT model, two tasks can be introduced to promote the model's understanding of the bytecode of the smart contract. First, the instruction-level features within the block are captured through the mask language task; second, in order to understand the control flow relationship between blocks, the connection between adjacent bytecode blocks in the CFG can be captured and simulated through the pre-designed contiguous segment prediction activity (CSP) task. Considering that different types of vulnerabilities may require different identification features, the BERT model can be personalized and pre-trained according to the vulnerability type during the fine-tuning phase. This step ensures that the model can extract more accurate features for different vulnerabilities. Finally, the BERT model trained for a specific vulnerability type is operated to extract the features of the bytecode block, which includes not only the instruction information within the code block, but also the control flow relationship between code blocks. Utilizing the encoding capabilities of the trained BERT model, it is possible to deeply capture the details in each bytecode block and their role in the entire control flow graph (CFG). This process helps to better understand and analyze potential vulnerabilities in smart contracts.

[0096] See also Figure 4As shown in the figure, the derivation (construction) process of the bytecode-based control flow graph (CFG) can specifically include: after obtaining the source code of the smart contract, use the Solidity compiler to convert it into bytecode, and deploy it through the automated tool Pyevm to obtain the control flow graph after bytecode compilation, where the control flow graph consists of bytecode basic blocks (i.e., nodes in the graph) and control flow edges connecting them. The main principles for constructing the control flow graph are as follows: (1) The basic code block contains a series of EVM (ETHereum Virtual Machine) instructions, which are connected to subsequent basic code blocks through branch instructions in the EVM, where branch instructions (such as JUMP, RETURN, STOP, SUICIDE, SELFDESTRUCT, REVERT, or INVALID) can be the end mark of the basic code block, that is, the branch instruction is regarded as a marker for dividing the basic code block. (2) The control flow relationship between the basic program blocks of the bytecode (such as conditional jumps, returns, etc.) is very close, rather than isolated from each other, so the control flow edges between the basic code blocks can well represent the semantic relationship between bytecode conditional statements and function calls. (3) The edges of the basic modules are divided into three categories: forward edges, true edges, and false edges. The forward edge is different from the source code processing of the smart contract. Although the forward edge is an unconditional jump, it is still a jump in the bytecode block representation, such as Figure 4 As shown in Figure 1, three different line types can be used to display data flow edges with different semantic information. Furthermore, after obtaining the control flow graph of the bytecode, a feature learning network DR-GCN (i.e., degree-free GCN) is introduced on the GCN framework to learn the high-level semantic information of the control flow graph.

[0097] In addition, after obtaining the control flow graph of the bytecode, it is necessary to extract its features. The specific extraction process is based on the general graph convolutional neural network propagation formula To be carried out, Represents the neighboring matrix of the graph. First, obtain the adjacency matrix A of a smart contract function, A is the identity matrix of (0,1), and then use Represents the adjacency matrix of a control flow graph with self-loop functions, where I represents the identity matrix. It is The feature matrix of the layer, is a trainable weight matrix. This application uses the square of A to increase the connectivity between nodes in the control flow graph G; then the neighboring matrix in the convolutional neural network propagation formula is Remove it from the equation. The advantage of doing so is that the degree information of the node can be ignored, thereby improving the stability of the graph model with uneven degree distribution and reducing gradient problems. Finally, the propagation formula is derived as follows:

[0098] ;

[0099] In the formula, is the activation function, is the weight matrix in the DR-GCN model. Then, the features of the control flow graph corresponding to the bytecode sequence are obtained through the DR-GCN model. .

[0100] See also Figure 5 As shown in the figure, the specific vulnerability detection process includes: ) and information enhancement technology (i.e. Figure 2 and Figure 3 ) obtained from the source code features (i.e., fused features , including node features and edge features) are input into the trained contract vulnerability detection model of the BiLSTM model based on the multi-head attention mechanism for vulnerability detection, thereby obtaining the contract vulnerability detection results. Through two different types of graph features, namely byte features And the fused features , which can solve the problem of insufficient source code vulnerability information at the bytecode level, so that the model can maintain strong performance in smart contract vulnerability detection even without source code information, thereby improving the security and reliability of smart contracts and enhancing users' trust in smart contracts.

[0101] Furthermore, in order to optimize the vulnerability detection model, a feature fusion network based on sequence tasks can be introduced. The main task of this network is to integrate byte features. And the fused features , so that the model can learn the correlation between the two features more effectively, thereby improving the accuracy and robustness of vulnerability detection. It should be pointed out that the network uses a focal loss function to solve the class imbalance problem in the data set, where the calculation formula of the focal loss function is:

[0102] ;

[0103] In the formula, is the probability that the sample is predicted to be positive, is an adjustment factor used to modify the rate of decrease in weights between data samples so that the formula can emphasize samples that are difficult to distinguish while reducing the influence of typical samples. The variable n is the number of samples, and the balance factor Used to deal with uneven sample distribution. The default value is usually =2, =0.25.

[0104] It should be pointed out that in the byte feature And the fused features When integrating, two feature sets are specifically integrated, among which, , represents the feature set at the source code level, including the features of n contract functions (SCSG); , represents the feature set at the bytecode level, which contains the bytecode features corresponding to the source code level. Further, a new set of fusion features is defined , and this set of features is used as the actual value vector to input the BiLSTM model for feature fusion and vulnerability detection. Specifically, in the BiLSTM model, the hidden state of each time step contains information about the current moment and context. Considering that future context is beneficial to tasks involving bytecode modeling, the BiLSTM model extends the unidirectional LSTM by introducing a second layer, so the hidden-to-hidden connections flow in reverse time order. For example, at time step t, the forget gate and input gate The calculation formula is:

[0105] ;

[0106] ;

[0107] In the formula, Indicates the current hidden state. represents the input of LSTM unit at time t, , , , These are all trainable parameters.

[0108] The updating process of temporary storage unit and subsequent unit status is as follows:

[0109] ;

[0110] ;

[0111] The steps to finally output the gate and calculate the next hidden state are:

[0112] ;

[0113] ;

[0114] Where W is , etc., and b , etc. are all trainable parameters. The data flow during the backward pass operation is consistent with the above process. Specifically, for the input at time t, the final output of Bi-LSTM is for:

[0115] ;

[0116] In the formula, and Represents two calculation results derived from a formula, the difference lies in the direction of derivation, where To deduce from front to back, To deduce from back to front.

[0117] Next, define the parameters related to the self-attention mechanism, such as defining L as the matrix of all hidden outputs , t represents the total length of the BiLSTM output, and the value of o is determined by the weighted sum of these output vectors. Further, the following calculation is performed:

[0118] ;

[0119] ;

[0120] ;

[0121] In the formula, W represents a trainable parameter, and the weight Indicates the importance of a single time step input during model inference and helps compute critical sequences.

[0122] Then, the final sequence representation is obtained .

[0123] Finally, the obtained sequence is represented as Input into the sigmoid network for vulnerability classification to obtain the final vulnerability detection result , the specific calculation formula is:

[0124] .

[0125] It is understandable that the feature fusion network based on sequence tasks can utilize the temporal features in source code features and bytecode features, which is achieved by using the bidirectional long short-term memory (BiLSTM) model as a fusion mechanism. In this way, the vulnerabilities in smart contracts can be analyzed more comprehensively and accurately, thereby improving the effectiveness of the detection model. In addition, the attention mechanism allows the detection model to focus only on the information required for task learning. It can select specific inputs and allow the network to focus only on nodes and edges that are more relevant to the task, thereby improving the effectiveness of training and the accuracy of detection.

[0126] Furthermore, after obtaining the contract vulnerability detection model of the BiLSTM model based on the multi-head attention mechanism, the vulnerability detection ability of the model can be verified through a validation set, such as the dataset SmartWild (which consists of four common vulnerabilities). After verification, it is found that the model significantly improves the effectiveness of smart contract vulnerability detection, and surpasses traditional vulnerability detection methods in accuracy, precision, recall rate and F1 score.

[0127] It can be seen that the embodiment of the present application is applied to the blockchain platform. When performing vulnerability detection on the smart contract, the source code of the smart contract to be detected is first obtained, and then the source code is converted into bytecode and input into the bytecode network obtained after training the Bert model and the DR-GCN model using the historical bytecode dataset for feature extraction to obtain bytecode features. Since the Bert model can accurately represent the semantic information in the text, and the DR-GCN model can ensure that the representation of all classes in the graph data is equal, the bytecode features extracted by the bytecode network can predict the missing source code in the smart contract, so that in the absence of source code, the smart contract vulnerability detection task can still be completed efficiently and accurately; further, the bytecode features and the historical fused features are input into the contract vulnerability detection model obtained after training the BiLSTM model based on the multi-head attention mechanism using the historical smart contract dataset for vulnerability detection. It should be pointed out that the historical fused features are the features obtained by fusing the node features and the edge features, wherein the node features and The edge feature is a feature obtained after feature extraction of nodes and edges in a multi-scale feature graph created based on a historical smart contract data set. That is, the embodiment of the present application pre-creates a multi-scale feature graph based on a historical smart contract data set, and extracts features of nodes and edges in the multi-scale feature graph, and then fuses the extracted node features and edge features. When performing vulnerability detection, the fused features are input into the contract vulnerability detection model together with the bytecode features as known information for vulnerability detection. By creating a multi-scale feature graph, including an abstract syntax tree, a control flow graph, and a data flow graph, etc., the features of the smart contract can be more comprehensively and accurately represented, and the complexity of vulnerability detection can be reduced, thereby realizing the extraction and utilization of fine-grained features, significantly improving the accuracy and comprehensiveness of vulnerability detection. In addition, compared with traditional vulnerability detection schemes, it reduces dependence on expert knowledge, reduces computing costs, improves detection efficiency, and improves the security and reliability of smart contracts. At the same time, it enhances user trust, promotes the popularization and application of blockchain technology, and provides stronger security protection for blockchain applications.

[0128] Correspondingly, the present application embodiment also discloses a smart contract vulnerability detection device, see Figure 6 As shown, the device comprises:

[0129] The source code acquisition module 11 is used to acquire the source code of the smart contract to be tested and obtain the current source code;

[0130] A source code conversion module 12, used for converting the current source code into bytecode to obtain current bytecode;

[0131] The feature extraction module 13 is used to input the current bytecode into the trained bytecode network to extract features of the current bytecode to obtain bytecode features; the bytecode network is a model obtained by training the Bert model and the DR-GCN model using the historical bytecode data set;

[0132] The vulnerability detection module 14 is used to input the bytecode features and the historical fused features into the trained contract vulnerability detection model to detect the vulnerabilities in the smart contract to be detected and obtain the contract vulnerability detection results; the historical fused features are the features obtained by fusing the node features and the edge features, and the node features and the edge features are the features obtained by extracting the nodes and edges in the multi-scale feature graph created based on the historical smart contract data set; the trained contract vulnerability detection model is a model obtained by training the BiLSTM model based on the multi-head attention mechanism using the historical smart contract data set.

[0133] Among them, the specific working processes of the above-mentioned modules can refer to the corresponding contents disclosed in the aforementioned embodiments, which will not be repeated here.

[0134] It can be seen that the embodiment of the present application is applied to the blockchain platform, first obtaining the source code of the smart contract to be detected to obtain the current source code, and converting the current source code into bytecode to obtain the current bytecode, and then inputting the current bytecode into the bytecode network obtained by training the Bert model and the DR-GCN model using the historical bytecode dataset, so as to extract features from the current bytecode to obtain bytecode features, and then inputting the bytecode features and the historical fused features into the trained contract vulnerability detection model obtained by training the BiLSTM model based on the multi-head attention mechanism using the historical smart contract dataset, so as to detect the vulnerabilities in the smart contract to be detected and obtain the contract vulnerability detection results, wherein the historical fused features are the features obtained by fusing the node features and the edge features, and the node features and the edge features are the features obtained by extracting features from the nodes and edges in the multi-scale feature graph created based on the historical smart contract dataset.When performing vulnerability detection on a smart contract, the embodiment of the present application first obtains the source code of the smart contract to be detected, then converts the source code into bytecode and inputs it into a bytecode network obtained by training the Bert model and the DR-GCN model using a historical bytecode dataset for feature extraction to obtain bytecode features. Since the Bert model can accurately represent the semantic information in the text, and the DR-GCN model can ensure that the representation of all classes in the graph data is equal, the bytecode features extracted by the bytecode network can predict the missing source code in the smart contract, so that in the absence of source code, the smart contract vulnerability detection task can still be completed efficiently and accurately; further, the bytecode features and the historical fused features are input into a contract vulnerability detection model obtained by training the BiLSTM model based on the multi-head attention mechanism using the historical smart contract dataset for vulnerability detection. It should be pointed out that the historical fused features are the features obtained by fusing the node features and the edge features, wherein the node features The edge features are features obtained after feature extraction of nodes and edges in a multi-scale feature graph created based on a historical smart contract data set. That is, the embodiment of the present application pre-creates a multi-scale feature graph based on a historical smart contract data set, and extracts features of nodes and edges in the multi-scale feature graph, and then fuses the extracted node features and edge features. When performing vulnerability detection, the fused features are input into the contract vulnerability detection model together with the bytecode features as known information for vulnerability detection. By creating a multi-scale feature graph, including an abstract syntax tree, a control flow graph, and a data flow graph, etc., the features of the smart contract can be more comprehensively and accurately represented, and the complexity of vulnerability detection can be reduced, thereby realizing the extraction and utilization of fine-grained features, significantly improving the accuracy and comprehensiveness of vulnerability detection. In addition, compared with traditional vulnerability detection schemes, it reduces dependence on expert knowledge, reduces computing costs, improves detection efficiency, and improves the security and reliability of smart contracts. At the same time, it enhances user trust, thereby promoting the popularization and application of blockchain technology.

[0135] In some specific embodiments, before the source code acquisition module 11, the following steps may also be included:

[0136] The contract collection unit is used to collect different types of historical smart contracts and obtain a historical smart contract data set;

[0137] A source code acquisition unit, used to respectively acquire the source code of each historical smart contract in the historical smart contract data set to obtain multiple historical source codes;

[0138] A format conversion unit, used to convert each of the historical source codes into a graph data format to obtain a multi-scale feature graph including an abstract syntax tree, a control flow graph, and a data flow graph;

[0139] A first feature extraction unit is used to extract features from nodes and edges in the multi-scale feature graph to obtain node features and edge features;

[0140] The first feature fusion unit is used to perform feature fusion on the node feature and the edge feature to obtain the historical fused feature.

[0141] In some specific embodiments, the format conversion unit may specifically include:

[0142] A first source code conversion unit, configured to convert each of the historical source codes into an abstract syntax tree according to the syntax structure of the code;

[0143] A generating unit, configured to generate a control flow graph and a data flow graph of each of the historical source codes based on the abstract syntax tree;

[0144] An integration unit is used to integrate the abstract syntax tree, the control flow graph and the data flow graph to obtain the multi-scale feature graph.

[0145] In some specific embodiments, the generating unit may specifically include:

[0146] A first traversal unit is used to perform a depth-first search on the abstract syntax tree to traverse and search each node in the abstract syntax tree, and discard nodes irrelevant to vulnerability detection during the traversal process to obtain a post-search syntax tree;

[0147] The second traversal unit is used to perform a breadth-first search on the post-search syntax tree to traverse and search each node in the post-search syntax tree, and in the traversal process, add edges between different nodes in the post-search syntax tree according to the execution order of the historical source code and the information flow in the historical source code to obtain a control flow graph and a data flow graph.

[0148] In some specific embodiments, the first feature extraction unit may specifically include:

[0149] An identification unit, used to identify nodes and edges in the multi-scale feature graph to obtain a node set and an edge set;

[0150] A node initialization unit, used to initialize the nodes in the node set using a Word2Vec model to obtain initialized nodes;

[0151] An edge initialization unit, used for initializing edges in the edge set by using a degree centrality index to obtain initialized edges;

[0152] A second feature extraction unit is used to extract features of the initialized nodes in the multi-scale feature map using a recursive neural network to obtain node features;

[0153] The third feature extraction unit is used to extract features from the initialized edges in the multi-scale feature map through a multi-head attention mechanism to obtain edge features.

[0154] In some specific embodiments, the first feature fusion unit may specifically include:

[0155] A feature aggregation unit, used to perform feature aggregation on all the node features and all the edge features to obtain aggregated node features and aggregated edge features;

[0156] The second feature fusion unit is used to fuse the aggregated node features and the aggregated edge features to obtain the historical fused features.

[0157] In some specific embodiments, the smart contract vulnerability detection device may further include:

[0158] A second source code conversion unit, used to convert the source codes of all historical smart contracts in the historical smart contract data set into bytecodes to obtain a historical bytecode data set;

[0159] The model training unit is used to input the historical bytecode data set into the Bert model and the DR-GCN model for training to obtain the bytecode network, and input the historical bytecode features output by the bytecode network during the training process and the historical fusion features into the BiLSTM model based on the multi-head attention mechanism for training to obtain the trained contract vulnerability detection model.

[0160] Furthermore, the present application also discloses an electronic device. Figure 7 This is a structural diagram of an electronic device 20 according to an exemplary embodiment. The content in the diagram cannot be regarded as any limitation on the scope of use of the present application.

[0161] Figure 7 A schematic diagram of the structure of an electronic device 20 provided in an embodiment of the present application. The electronic device 20 may specifically include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 is used to store a computer program, which is loaded and executed by the processor 21 to implement the relevant steps in the smart contract vulnerability detection method disclosed in any of the aforementioned embodiments. In addition, the electronic device 20 in this embodiment may specifically be an electronic computer.

[0162] In this embodiment, the power supply 23 is used to provide working voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and the external device, and the communication protocol it follows is any communication protocol that can be applied to the technical solution of the present application, and is not specifically limited here; the input and output interface 25 is used to obtain external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs and is not specifically limited here.

[0163] In addition, the memory 22, as a carrier for storing resources, can be a read-only memory, a random access memory, a disk or an optical disk, etc. The resources stored thereon can include an operating system 221, a computer program 222, etc., and the storage method can be temporary storage or permanent storage.

[0164] The operating system 221 is used to manage and control the hardware devices and computer program 222 on the electronic device 20, which can be Windows Server, Netware, Unix, Linux, etc. In addition to including a computer program that can be used to complete the smart contract vulnerability detection method executed by the electronic device 20 disclosed in any of the aforementioned embodiments, the computer program 222 can further include a computer program that can be used to complete other specific tasks.

[0165] Furthermore, the present application also discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, the aforementioned disclosed smart contract vulnerability detection method is implemented. The specific steps of the method can refer to the corresponding contents disclosed in the aforementioned embodiments, and will not be repeated here.

[0166] Furthermore, an embodiment of the present application also discloses a computer program product, including a computer program / instruction, which, when executed by a processor, implements the steps of the smart contract vulnerability detection method disclosed above.

[0167] In this specification, each embodiment is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part.

[0168] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0169] The steps of the method or algorithm described in conjunction with the embodiments disclosed herein may be implemented directly using hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.

[0170] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.

[0171] The above is a detailed introduction to a smart contract vulnerability detection method, device, equipment and storage medium provided by the present application. This article uses specific examples to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea; at the same time, for general technical personnel in this field, according to the idea of ​​the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.

Claims

1. A smart contract vulnerability detection method, characterized in that: Applied to blockchain platforms, including: Obtain the source code of the smart contract to be tested to obtain the current source code, and convert the current source code into bytecode to obtain the current bytecode; Input the current bytecode into the trained bytecode network to extract features of the current bytecode to obtain bytecode features; the bytecode network is a model obtained by training the Bert model and the DR-GCN model using the historical bytecode dataset; The bytecode features and the historical fused features are input into the trained contract vulnerability detection model to detect the vulnerabilities in the smart contract to be detected and obtain the contract vulnerability detection results; the historical fused features are the features obtained by fusing the node features and the edge features, and the node features and the edge features are the features obtained by extracting the nodes and edges in the multi-scale feature graph created based on the historical smart contract data set; the trained contract vulnerability detection model is a model obtained by training the BiLSTM model based on the multi-head attention mechanism using the historical smart contract data set.

2. The smart contract vulnerability detection method according to claim 1, characterized in that: The step of obtaining the source code of the smart contract to be detected, before obtaining the current source code, further includes: Collect different types of historical smart contracts to obtain a historical smart contract data set, and respectively obtain the source code of each historical smart contract in the historical smart contract data set to obtain multiple historical source codes; Convert each of the historical source codes into a graph data format to obtain a multi-scale feature graph including an abstract syntax tree, a control flow graph, and a data flow graph; Extracting features of nodes and edges in the multi-scale feature graph respectively to obtain node features and edge features; The node feature and the edge feature are subjected to feature fusion to obtain the historical fused feature.

3. The smart contract vulnerability detection method according to claim 2, characterized in that: The converting each of the historical source codes into a graph data format to obtain a multi-scale feature graph including an abstract syntax tree, a control flow graph, and a data flow graph includes: Convert each of the historical source codes into an abstract syntax tree according to the grammatical structure of the code; A control flow graph and a data flow graph of each of the historical source codes are generated based on the abstract syntax tree, and the abstract syntax tree, the control flow graph and the data flow graph are integrated to obtain the multi-scale feature graph.

4. The smart contract vulnerability detection method according to claim 3 is characterized in that: The generating a control flow graph and a data flow graph of each of the historical source codes based on the abstract syntax tree comprises: Performing a depth-first search on the abstract syntax tree to traverse and search each node in the abstract syntax tree, and discarding nodes irrelevant to vulnerability detection during the traversal process to obtain a post-search syntax tree; A breadth-first search is performed on the post-search syntax tree to traverse and search each node in the post-search syntax tree, and during the traversal process, edges are added between different nodes in the post-search syntax tree according to the execution order of the historical source code and the information flow in the historical source code to obtain a control flow graph and a data flow graph.

5. The smart contract vulnerability detection method according to claim 4, characterized in that: The extracting features of nodes and edges in the multi-scale feature graph respectively to obtain node features and edge features includes: Identify nodes and edges in the multi-scale feature graph to obtain a node set and an edge set; Initializing the nodes in the node set by using the Word2Vec model to obtain initialized nodes, and initializing the edges in the edge set by using the degree centrality index to obtain initialized edges; A recursive neural network is used to extract features of the initialized nodes in the multi-scale feature map to obtain node features, and a multi-head attention mechanism is used to extract features of the initialized edges in the multi-scale feature map to obtain edge features.

6. The smart contract vulnerability detection method according to claim 5, characterized in that: The step of fusing the node feature and the edge feature to obtain the historical fused feature includes: Performing feature aggregation on all the node features and all the edge features respectively to obtain aggregated node features and aggregated edge features; The aggregated node features and the aggregated edge features are subjected to feature fusion to obtain the historical fused features.

7. The smart contract vulnerability detection method according to claim 6, characterized in that: Also includes: Convert the source codes of all historical smart contracts in the historical smart contract dataset into bytecodes to obtain a historical bytecode dataset; The historical bytecode dataset is input into the Bert model and DR-GCN model for training to obtain the bytecode network, and the historical bytecode features output by the bytecode network during the training process and the historical fusion features are input into the BiLSTM model based on the multi-head attention mechanism for training to obtain the trained contract vulnerability detection model.

8. A smart contract vulnerability detection device, characterized in that: Applied to blockchain platforms, including: The source code acquisition module is used to obtain the source code of the smart contract to be tested and obtain the current source code; A source code conversion module, used for converting the current source code into bytecode to obtain current bytecode; A feature extraction module, used for inputting the current bytecode into a trained bytecode network to extract features from the current bytecode to obtain bytecode features; the bytecode network is a model obtained by training a Bert model and a DR-GCN model using a historical bytecode dataset; A vulnerability detection module is used to input the bytecode features and the historical fused features into the trained contract vulnerability detection model to detect the vulnerabilities in the smart contract to be detected and obtain the contract vulnerability detection results; the historical fused features are the features obtained by fusing the node features and the edge features, and the node features and the edge features are the features obtained by extracting the nodes and edges in the multi-scale feature graph created based on the historical smart contract data set; the trained contract vulnerability detection model is a model obtained by training the BiLSTM model based on the multi-head attention mechanism using the historical smart contract data set.

9. An electronic device, characterized in that: It comprises a processor and a memory; wherein, when the processor executes the computer program stored in the memory, the smart contract vulnerability detection method as described in any one of claims 1 to 7 is implemented.

10. A computer-readable storage medium, characterized in that: Used to store computer programs; wherein, when the computer program is executed by a processor, the smart contract vulnerability detection method according to any one of claims 1 to 7 is implemented.