Smart contract security detection methods, systems, equipment, terminals and applications
Through the smart contract security detection method based on graph neural network, the problem of lack of detection methods for the Hyperledger Fabric platform smart contract in the existing technology is solved, and effective vulnerability detection of Fabric smart contracts is realized, which improves detection efficiency and reduces capital losses. risk.
Patent Information
- Application Number
- CN202110862067.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-07-29
- Publication Date
- 2025-02-11
- Estimated Expiration
- 2041-07-29
AI Technical Summary
The existing smart contract security detection methods are mainly aimed at the Ethereum platform, and the lack of targeted detection methods for the Hyperledger Fabric platform, making it difficult to detect and analyze smart contracts on the Fabric platform.
Using a smart contract security detection method based on graph neural network, the word2vec model is trained by using open source Go code on Github, encapsulate smart contract functions, construct an abstract syntax tree, extract data flow and control flow information, convert it into a graph model, and use graphs Neural networks are trained to detect smart contract vulnerabilities.
It realizes effective vulnerability detection of Hyperledger Fabric smart contracts, improves detection efficiency, can capture more structural information of smart contract vulnerabilities, and reduces the risk of enterprises suffering from capital losses due to contract security vulnerabilities.
Smart Images

Figure CN113626826B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of blockchain security technology, and in particular relates to a smart contract security detection method, system, equipment, terminal and application. Background Art
[0002] At present, most smart contracts on blockchain platforms involve the transaction and processing of digital assets or cryptocurrencies. Therefore, vulnerabilities in smart contracts may be exploited to expose users to malicious attacks. For this reason, in recent years, more and more researchers have begun to study smart contract security detection methods. However, existing research is mainly carried out around the Ethereum platform, and there is a lack of targeted detection methods for smart contract security detection on alliance chain platforms represented by Hyperledger Fabric. The main reasons are as follows:
[0003] (1) Smart contracts on the Fabric platform are mainly deployed within organizations and are difficult to access;
[0004] (2) Ethereum and Fabric have different platform characteristics, and the security detection tools and methods of the two platforms cannot be directly used interchangeably;
[0005] (3) The Fabric platform has few public smart contracts, making it impossible to conduct large-scale analysis and research.
[0006] Smart contracts are software that runs on the blockchain system. Like other software systems, the earlier problems in smart contracts are discovered, the less manpower and material resources are invested in correcting them. As more and more assets are managed on the Fabric platform, once problems occur in smart contracts running on the Fabric platform, it will not only affect the business of the enterprise, but will also inevitably cause financial losses. Therefore, security research on Fabric smart contracts is particularly urgent and necessary.
[0007] Through the above analysis, the problems and defects of the prior art are as follows:
[0008] (1) Existing research mainly focuses on the Ethereum platform, but there is a lack of targeted detection methods for smart contract security detection on consortium chain platforms represented by Hyperledger Fabric.
[0009] (2) Smart contracts on the Fabric platform are mainly deployed within organizations and are difficult to access. There are relatively few public smart contracts on the Fabric platform, making it impossible to conduct large-scale analysis and research.
[0010] (3) Ethereum and Fabric have different platform characteristics, and the security detection tools and methods of the two platforms cannot be directly used interchangeably.
[0011] The difficulty of solving the above problems and defects is:
[0012] On the one hand, due to the small amount of public smart contract codes on the Fabric platform, there is insufficient data set available for graph neural networks. The lack of training data limits the quality of the machine learning detection model, which increases the difficulty of training the model of the present invention. On the other hand, the existing blockchain platform contract security detection tools are mainly for the Ethereum platform and cannot be directly used for the Fabric platform. In addition, there is an extreme lack of reference information on Fabric contract vulnerability detection, which further increases the difficulty of implementing the solution.
[0013] The significance of solving the above problems and defects is:
[0014] With the continuous in-depth development and gradual implementation of blockchain technology, the relationship between blockchain and life is becoming closer and closer, and the security detection of smart contracts is becoming more and more important. According to the characteristics of the Hyperledger Fabric platform, the present invention systematically and completely analyzes the security issues and potential risks faced by smart contracts on the Fabric platform, conducts a comprehensive security assessment of smart contracts, improves the overall security of the Fabric blockchain system, and plays a certain preventive role in preventing corporate funds from being lost due to contract security vulnerabilities, thereby greatly reducing the risk of corporate fund loss. Summary of the invention
[0015] In response to the problems existing in the prior art, the present invention provides a smart contract security detection method, system, device, terminal and application, and in particular, relates to a smart contract security detection method, system, device, terminal and application based on graph neural network.
[0016] The present invention is implemented as follows: a smart contract security detection method, the smart contract security detection method comprising the following steps:
[0017] Step 1: Use the open source Go code on Github to train the word2vec model, initialize the program graph nodes, and obtain the initial vector of each node in the graph representation of each source code;
[0018] Step 2: According to the smart contract syntax, encapsulate the open source Github code into a smart contract function as the main source of the data set;
[0019] Step 3: Convert the encapsulated function into an abstract syntax tree and extract data flow and control flow information to construct a relationship graph;
[0020] Step 4: Convert the data flow and control flow information of the smart contract into a graph model. The graph model can retain and infer more control and data flow information to capture more structural information of smart contract vulnerabilities as input of the graph neural network.
[0021] Step 5: Use the trained word2vec model to convert the graph nodes into vectors. The purpose is to construct a vector space so that words with close contextual relationships in the source code are close to each other in the vector space.
[0022] Step 6: Use a graph neural network to train the graph model to obtain a set of model parameters, and finally use the model using these parameters to complete the classification task of the present invention;
[0023] Step 7: Read all node information, convert the smart contract function graph model into a vector, and pass the code embedding vector to the downstream neural network for prediction through code embedding;
[0024] Step eight, using the classification model to determine whether the function vector contains smart contract vulnerability information, and finally achieving the classification task of the present invention.
[0025] Furthermore, the smart contract security detection method further includes:
[0026] (1) Use open source code to train the word2vec model, encapsulate the open source code into a smart contract function, and establish a training dataset through manual annotation;
[0027] (2) The dataset is divided into training set, validation set and test set according to the ratio of 8:1:1;
[0028] (3) Use the AST analysis tool developed in Go language to process the source code file and generate the AST graph structure of the source file;
[0029] (4) Save the generated graph structure to a file, and the generated program graph structure file corresponds to the source file;
[0030] (5) Use the word2vec algorithm to train the graph structure file in step (4);
[0031] (6) Train the graph neural network vulnerability detection model to complete classification detection.
[0032] Further, in step (4), the program graph is constructed by syntax nodes and syntax tags in AST; a standard AST node has only one edge, which is used to represent the parent-child relationship between two AST nodes; the model adds multiple edges including protection edges, jumps and the final dictionary to the AST, and uses an adjacency matrix to record the edges of each relationship graph for obtaining additional syntax, data and control information; for each edge, an additional backward edge is added to propagate information in the relationship graph; wherein the syntax node in the AST is a non-terminal in the language syntax, including an AST node of an if statement or a function declaration; the syntax tag is a terminal, including an identifier name and a constant value.
[0033] Further, in step (5), the training of the graph structure file using the word2vec algorithm is implemented by calling the Gensim library; the word2vec network maps the nodes and tags of each program graph into a vector, so that words with close contextual relationships in the source code are adjacent to each other in the vector space.
[0034] The source code file is divided into functions, and the trained word2vec model is used to process the functions one by one to initialize the vector of each function graph node, create data flow and control flow graph, and save the function graph nodes and graph structure separately in files. The files use 0 and 1 to mark whether the function contains vulnerabilities. These files will be used as input data for the graph neural network.
[0035] Further, in step (6), the training of the graph neural network vulnerability detection model includes:
[0036] The neighborhood aggregation algorithm is used to learn multi-relational graphs. The GGNN model represents each node of the relationship graph as a vector containing 100 features and updates the node embedding through the neighborhood aggregation scheme; the 100-dimensional embedding vector h of the graph node v v It is calculated by the embedding layer by recursively aggregating and transforming the representation vectors of its neighboring nodes; nodes exchange information by sending their current state, i.e., the embedding vector, as messages to all neighbors along the edge; at each node, the messages are summarized and used to update the associated node representation in the next embedding layer, i.e., the next iteration; after repeating this process to update the node state for a fixed number of iterations, the graph is aggregated to a single embedding vector using a readout function.
[0037] Another object of the present invention is to provide a smart contract security detection system using the smart contract security detection method, the smart contract security detection system comprising:
[0038] word2vec model training module, used to train word2vec models using open source Go code on Github;
[0039] The contract function encapsulation module is used to encapsulate the open source Github code into a smart contract function according to the smart contract syntax;
[0040] The conversion and extraction module is used to convert the encapsulated function into an abstract syntax tree and extract data flow and control flow information;
[0041] Information conversion module, used to convert the data flow and control flow information of smart contracts into a graph model;
[0042] The graph node conversion module is used to convert graph nodes into vectors using the trained word2vec model;
[0043] A graph model training module, which is used to train graph models using graph neural networks;
[0044] The graph model conversion module is used to read all node information and convert the smart contract function graph model into a vector;
[0045] The contract vulnerability judgment module is used to use the classification model to determine whether the function vector contains smart contract vulnerability information.
[0046] Another object of the present invention is to provide a computer device, the computer device comprising a memory and a processor, the memory storing a computer program, and when the computer program is executed by the processor, the processor performs the following steps:
[0047] Use the open source Go code on Github to train the word2vec model; encapsulate the open source Github code into a smart contract function according to the smart contract syntax; convert the encapsulated function into an abstract syntax tree and extract data flow and control flow information; convert the data flow and control flow information of the smart contract into a graph model;
[0048] Use the trained word2vec model to convert graph nodes into vectors; use graph neural network to train the graph model; read out all node information and convert the smart contract function graph model into a vector; use the classification model to determine whether the function vector contains smart contract vulnerability information.
[0049] Another object of the present invention is to provide a computer-readable storage medium storing a computer program, wherein when the computer program is executed by a processor, the processor executes the following steps:
[0050] Use the open source Go code on Github to train the word2vec model; encapsulate the open source Github code into a smart contract function according to the smart contract syntax; convert the encapsulated function into an abstract syntax tree and extract data flow and control flow information; convert the data flow and control flow information of the smart contract into a graph model;
[0051] Use the trained word2vec model to convert graph nodes into vectors; use graph neural network to train the graph model; read out all node information and convert the smart contract function graph model into a vector; use the classification model to determine whether the function vector contains smart contract vulnerability information.
[0052] Another object of the present invention is to provide an information data processing terminal, which is used to implement the smart contract security detection system.
[0053] Another object of the present invention is to provide an application of the smart contract security detection system in Hyperledger Fabric smart contract vulnerability detection.
[0054] In combination with all the above technical solutions, the advantages and positive effects of the present invention are as follows: the smart contract security detection system provided by the present invention realizes vulnerability detection of Hyperledger Fabric smart contracts.
[0055] The Fabric-based smart contract vulnerability detection system and method provided by the present invention, which uses a graph neural network combined method, has the following advantages over the existing smart contract vulnerability detection system:
[0056] (1) It reduces the tediousness of manual function definition by human experts and improves the efficiency of smart contract security testing;
[0057] (2) By learning smart contracts through graph neural networks, the model can retain and infer more control and data flow information to capture more structural information of smart contract vulnerabilities;
[0058] (3) The source code in the open source project is used for annotation, which solves the problem of insufficient Fabric smart contract data set. By annotating the source code in the open source project, the annotated source code is trained and good results are achieved. BRIEF DESCRIPTION OF THE DRAWINGS
[0059] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments of the present invention. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0060] Figure 1 It is a flow chart of the smart contract security detection method provided by an embodiment of the present invention.
[0061] Figure 2 It is a schematic diagram of the smart contract security detection method provided by an embodiment of the present invention.
[0062] Figure 3 It is a structural block diagram of the smart contract security detection system provided by an embodiment of the present invention;
[0063] In the figure: 1. word2vec model training module; 2. contract function encapsulation module; 3. conversion and extraction module; 4. information conversion module; 5. graph node conversion module; 6. graph model training module; 7. graph model conversion module; 8. contract vulnerability judgment module. DETAILED DESCRIPTION
[0064] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0065] Example 1
[0066] 1) The experiment uses TensorFlow-2.1.0 to implement the Fabric smart contract security detection model.
[0067] 2) Use the Go language ast package to construct the AST graph of the source code and extract different edge relationships from it. That is, traverse all the AST nodes of the source code. When traversing, all nodes are numbered sequentially, and the relationship between different edges is obtained according to specific rules, and the variable names are rewritten using a unified naming scheme. This step ensures that semantic differences such as variable names in the program will not affect the choice of token embedding.
[0068] 3) Create a directory and then create the train.py training file.
[0069] 4) Write the GGNN model training code in this file. The code starts with the following package loading code.
[0070]
[0071] 5) In the same directory, create a test file called test.py.
[0072] 6) Write the GGNN model test code in this file. The code starts with the following package loading code.
[0073] import json
[0074] import os
[0075] import random
[0076] import re
[0077] import sys
[0078] import psutil
[0079] import time
[0080] from typing import Dict,Optional,Callable,Any
[0081] import nni
[0082] import jsonlines
[0083] import numpy as np
[0084] import tensorflow as tf
[0085] from tensorflow.python.training.tracking import tdata_structures as tf_data_structures
[0086] from dpu_utils.utils import RichPath
[0087] from tensorflow_core.python.keras import Sequential
[0088] from tensorflow_core.python.keras.layers import Dense
[0089] 7) Training and testing are performed on a Huawei cloud server equipped with GPU: 1*v100NV32, CPU: 8 cores 64GiB.
[0090] 8) According to the vulnerability type, the training samples are divided into multiple batches and the GGNN model is trained separately, where each batch of samples consists of positive samples and negative samples.
[0091] 9) In order to minimize the distance between the two probability distributions of the predicted value and the true value, the cross entropy loss is selected as the objective function.
[0092] 10) This model uses the minimum batch stochastic gradient descent (SGD) and Adam algorithm with a learning rate of 0.001. Training will terminate when the loss is less than 0.005 or reaches a maximum of 100 training epochs.
[0093] 11) Five-fold cross validation was used to evaluate all methods on their respective datasets. This standard method is used to evaluate the generalization ability of prediction models.
[0094] 12) Select the following four standard indicators as evaluation indicators:
[0095] Accuracy: The ratio of correctly labeled cases to the total number of test cases.
[0096] Precision: The ratio of correctly predicted samples to the total number of samples predicted to have a specific label.
[0097] Recall: The ratio of correctly predicted samples to the total number of test samples belonging to a class.
[0098] F1 score: the average of Precision and Recall, calculated as: 2×(Recall×Precision) / (Recall+Precision). This metric is convenient for testing the distribution of vulnerability types.
[0099] 13) In order to verify the accuracy of the model based on the gated graph neural network, the present invention designed a graph embedding model that does not contain control flow and data flow during the test process to compare the model of this embodiment. The model of this embodiment is represented by GGNN-CP, and the model that does not contain control flow and data flow is represented by GGNN-noCP. The test results for each evaluation index are as follows:
[0100]
[0101] According to the test results, the GGNN-CP model is superior to the GGNN-noCP model in terms of accuracy and the ability to distinguish negative samples, which means that GGNN-CP is better than GGNN-noCP in terms of source code representation ability, and GGNN-CP can represent more internal relations of the source code.
[0102] 14) Using t-SNE (t-Distributed Stochastic Neighbor Embedding) to visualize the sample detection results, it can be seen that the detection effect of GGNN-CP is significantly better than that of GGNN-noCP.
[0103] In view of the problems existing in the prior art, the present invention provides a smart contract security detection method, system, device, terminal and application. The present invention is described in detail below with reference to the accompanying drawings.
[0104] like Figure 1 As shown, the smart contract security detection method provided by the embodiment of the present invention includes the following steps:
[0105] S101, training a word2vec model using the open source Go code on Github;
[0106] S102, encapsulate the open source Github code into a smart contract function according to the smart contract syntax;
[0107] S103, converting the encapsulated function into an abstract syntax tree, and extracting data flow and control flow information;
[0108] S104, converting the data flow and control flow information of the smart contract into a graph model;
[0109] S105, using the trained word2vec model to convert the graph nodes into vectors;
[0110] S106, training the graph model using a graph neural network;
[0111] S107, read out all node information and convert the smart contract function graph model into a vector;
[0112] S108, using the classification model to determine whether the function vector contains smart contract vulnerability information.
[0113] The principle diagram of the smart contract security detection method provided by the embodiment of the present invention is as follows: Figure 2 shown.
[0114] like Figure 3 As shown, the smart contract security detection system provided by the embodiment of the present invention includes:
[0115] Word2vec model training module 1, used to train the word2vec model using the open source Go code on Github;
[0116] Contract function encapsulation module 2 is used to encapsulate the open source Github code into a smart contract function according to the smart contract syntax;
[0117] The conversion and extraction module 3 is used to convert the encapsulated function into an abstract syntax tree and extract data flow and control flow information;
[0118] Information conversion module 4, used to convert the data flow and control flow information of the smart contract into a graph model;
[0119] A graph node conversion module 5, used to convert graph nodes into vectors using a trained word2vec model;
[0120] A graph model training module 6, used for training the graph model using a graph neural network;
[0121] Graph model conversion module 7, used to read all node information and convert the smart contract function graph model into a vector;
[0122] The contract vulnerability judgment module 8 is used to use the classification model to judge whether the function vector contains smart contract vulnerability information.
[0123] The technical solution of the present invention is further described below in conjunction with embodiments.
[0124] The purpose of the present invention is to propose a Hyperledger Fabric smart contract security detection system based on graph neural network, which realizes vulnerability detection of Hyperledger Fabric smart contracts.
[0125] The embodiment of the present invention provides a method for detecting vulnerabilities in Hyperledger Fabric based on a graph neural network, comprising the following steps:
[0126] In the first step, the model first uses open source code to train a word2vec model, then packages the open source code into smart contract functions, and creates a training data set through manual annotation.
[0127] In the second step, the data set in step 1 is sorted according to the ratio of 8:1:1 and divided into training set, validation set and test set;
[0128] The third step is to use the AST analysis tool developed in Go language to process the source code file and generate the AST graph structure of the source file;
[0129] The fourth step is to save the graph structure generated in step three to a file. The generated program graph structure file corresponds to the source file one by one. The program graph is constructed by syntax nodes in the AST (i.e., non-terminals in the language syntax, such as AST nodes for if statements or function declarations) and syntax tags (terminals, such as identifier names and constant values). A standard AST node has only one edge, which is used to represent the parent-child relationship between two AST nodes. In order to obtain additional syntax, data, and control information, the model adds multiple edges such as guard edges, jumps, and final dictionaries to the AST, and uses an adjacency matrix to record the edges of each relationship graph. For each edge, an additional backward edge is added. These backward edges help to propagate information in the relationship graph.
[0130] The fifth step is to train the step four graph structure file using the word2vec algorithm, and the present invention is implemented by calling the Gensim library. The word2vec network maps the nodes and tags of each program graph into a vector, so that the words with close contextual relationships in the source code are adjacent to each other in the vector space. In the present embodiment, the present invention divides the source code file into functions, processes the functions one by one with the trained word2vec model, realizes the initialization of each function graph node vector, creates a data flow, a control flow graph, and saves the graph nodes and graph structure of the function separately into files, and the files are marked with 0 and 1 to indicate whether the function contains vulnerabilities, and these files will be used as input data for the graph neural network.
[0131] The sixth step is to train the graph neural network vulnerability detection model to complete classification detection. The present invention uses the neighborhood aggregation algorithm to learn multiple relationship graphs. The GGNN model represents each node of the relationship graph as a vector containing 100 features, and updates the node embedding through the neighborhood aggregation scheme. The 100-dimensional embedding vector h of the graph node v is v is calculated by the embedding layer by recursively aggregating and transforming the representation vectors of its neighboring nodes. Nodes exchange information by sending their current state (i.e., embedding vector) as messages to all neighbors along the edge. At each node, the messages are summarized and then used to update the associated node representation in the next embedding layer (i.e., the next iteration). After repeating this process to update the node state for a fixed number of iterations, the graph is aggregated to a single embedding vector using a readout function.
[0132] The Fabric-based smart contract vulnerability detection system and method of the embodiment of the present invention, which uses a graph neural network combined method, has the following advantages over the existing smart contract vulnerability detection system:
[0133] 1. It reduces the tediousness of manual definition of functions by human experts and improves the efficiency of smart contract security detection;
[0134] 2. By learning smart contracts through graph neural networks, the model can retain and infer more control and data flow information to capture more structural information of smart contract vulnerabilities;
[0135] 3. Using the source code in the open source project for annotation solves the problem of insufficient Fabric smart contract data set. By annotating the source code in the open source project, the annotated source code is trained and good results are achieved.
[0136] The graph embedding model of the present invention has an accuracy of 0.942, a precision of 0.893, a recall rate of 1, and an F1 score of 0.943, while the graph embedding model without control flow and data flow has an accuracy of 0.915, a precision of 0.846, a recall rate of 1, and an F1 score of 0.917.
[0137] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When the use is implemented in whole or in part in the form of a computer program product, the computer program product includes one or more computer instructions. When the computer program instructions are loaded or executed on a computer, the process or function described in the embodiment of the present invention is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions may be transmitted from one website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL) or wireless (e.g., infrared, wireless, microwave, etc.) mode) to another website site, computer, server or data center. The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk SolidState Disk (SSD)), etc.
[0138] The above description is only a specific implementation mode of the present invention, but the protection scope of the present invention is not limited thereto. Any modifications, equivalent substitutions and improvements made by any technician familiar with the technical field within the technical scope disclosed by the present invention and within the spirit and principle of the present invention should be covered by the protection scope of the present invention.
Claims
1. A smart contract security detection method, characterized in that: The smart contract security detection method comprises the following steps: Step 1: Use the open source Go code on Github to train the word2vec model. Step 2: Encapsulate the open source Github code into a smart contract function according to the smart contract syntax; Step 3: Convert the encapsulated function into an abstract syntax tree and extract data flow and control flow information; Step 4: Convert the data flow and control flow information of the smart contract into a graph model; Step 5: Use the trained word2vec model to convert graph nodes into vectors; Step 6: Use graph neural network to train the graph model; Step 7: Read all node information and convert the smart contract function graph model into a vector; Step 8: Use the classification model to determine whether the function vector contains smart contract vulnerability information; The smart contract security detection method performs vulnerability detection on the Hyperledger Fabric platform smart contract.
2. The smart contract security detection method according to claim 1, characterized in that: The smart contract security detection method further includes: (1) Use open source code to train the word2vec model, encapsulate the open source code into a smart contract function, and establish a training dataset through manual annotation; (2) The dataset is divided into training set, validation set and test set according to the ratio of 8:1:1; (3) Use the AST analysis tool developed in Go language to process the source code file and generate the AST graph structure of the source file; (4) Save the generated graph structure to a file. The generated program graph structure file corresponds to the source file. (5) Use the word2vec model to train the graph structure file in step (4); (6) Train the graph neural network vulnerability detection model to complete classification detection.
3. The smart contract security detection method according to claim 2, characterized in that: In step (4), the program graph is constructed by the syntax nodes and syntax tags in the AST; a standard AST node has only one edge, which is used to represent the parent-child relationship between two AST nodes; The model adds multiple edges including guard edges, jumps, and the final dictionary to the AST, and uses an adjacency matrix to record the edges of each relationship graph to obtain additional syntax, data, and control information; For each edge, an additional backward edge is added to propagate information in the relationship graph; wherein the syntax nodes in the AST are non-terminals, including AST nodes of if statements or function declarations; and the syntax markers are terminals, including identifier names and constant values.
4. The smart contract security detection method according to claim 2, characterized in that: In step (5), the training of the graph structure file using the word2vec model is implemented by calling the Gensim library; the word2vec model maps the nodes and tags of each program graph into a vector, so that words with close contextual relationships in the source code are close to each other in the vector space; The source code file is divided into functions, and the trained word2vec model is used to process the functions one by one to initialize the vector of each function graph node, create data flow and control flow graph, and save the graph nodes and graph structure of the function separately into files. The files use 0 and 1 to mark whether the function contains vulnerabilities. These files will be used as input data for the graph neural network.
5. The smart contract security detection method according to claim 2, characterized in that: In step (6), the training of the graph neural network vulnerability detection model includes: The neighborhood aggregation algorithm is used to learn multi-relational graphs. The GGNN model represents each node of the relationship graph as a vector containing 100 features and updates the node embedding through the neighborhood aggregation scheme. The 100-dimensional embedding vector of is calculated by the embedding layer by recursively aggregating and transforming the representation vectors of its neighboring nodes; nodes exchange information by sending their current state, i.e., the embedding vector, as messages to all neighbors along the edge; at each node, the messages are summarized and used to update the associated node representation in the next embedding layer, i.e., the next iteration; after repeating this process to update the node state for a fixed number of iterations, the readout function is used to aggregate the graph nodes to a single embedding vector.
6. A smart contract security detection system for implementing the smart contract security detection method according to any one of claims 1 to 5, characterized in that: The smart contract security detection system includes: word2vec model training module, used to train word2vec models using open source Go code on Github; The contract function encapsulation module is used to encapsulate the open source Github code into a smart contract function according to the smart contract syntax; The conversion and extraction module is used to convert the encapsulated function into an abstract syntax tree and extract data flow and control flow information; Information conversion module, used to convert the data flow and control flow information of smart contracts into a graph model; The graph node conversion module is used to convert graph nodes into vectors using the trained word2vec model; A graph model training module, which is used to train graph models using graph neural networks; The graph model conversion module is used to read all node information and convert the smart contract function graph model into a vector; The contract vulnerability judgment module is used to use the classification model to determine whether the function vector contains smart contract vulnerability information.
7. A computer device, characterized in that: The computer device includes a memory and a processor, the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the smart contract security detection method according to any one of claims 1 to 5, including the following steps: Use the open source Go code on Github to train the word2vec model; encapsulate the open source Github code into a smart contract function according to the smart contract syntax; convert the encapsulated function into an abstract syntax tree and extract data flow and control flow information; convert the data flow and control flow information of the smart contract into a graph model; Use the trained word2vec model to convert graph nodes into vectors; use graph neural network to train the graph model; Read out all node information and convert the smart contract function graph model into a vector; use the classification model to determine whether the function vector contains smart contract vulnerability information.
8. A computer-readable storage medium storing a computer program, wherein when the computer program is executed by a processor, the processor executes the smart contract security detection method according to any one of claims 1 to 5, comprising the following steps: Use the open source Go code on Github to train the word2vec model; encapsulate the open source Github code into a smart contract function according to the smart contract syntax; convert the encapsulated function into an abstract syntax tree and extract data flow and control flow information; convert the data flow and control flow information of the smart contract into a graph model; Use the trained word2vec model to convert graph nodes into vectors; use graph neural network to train the graph model; Read out all node information and convert the smart contract function graph model into a vector; use the classification model to determine whether the function vector contains smart contract vulnerability information.
9. An information data processing terminal, characterized in that: The information data processing terminal is used to implement the smart contract security detection system as described in claim 6.
10. An application of the smart contract security detection system as claimed in claim 6 in Hyperledger Fabric smart contract vulnerability detection.
Citation Information
Patent Citations
A software defect automatic detection method based on an open source code library
CN109697162A
Intelligent contract endless loop detection method based on graph convolutional neural network
CN110674503A