An automated detection method for smart contract vulnerabilities based on graph neural networks
The automatic detection method of smart contract vulnerabilities based on graph neural network solves the problem of relying on manual review for smart contract vulnerability detection in the existing technology, achieves more efficient and accurate vulnerability detection, reduces costs and improves detection efficiency.
Patent Information
- Application Number
- CN202110170847.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-02-08
- Publication Date
- 2024-09-24
- Estimated Expiration
- Not applicable · inactive patent
Smart Images

Figure CN113157385B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of smart contract technology, and in particular to an automated detection method for smart contract vulnerabilities based on graph neural networks. Background Art
[0002] Smart contracts are programs running on the blockchain. They are computable transaction protocols that execute contract terms. The contract terms and trigger conditions are specified through program code. Once the trigger conditions are met, the contract will execute automatically. BERT is a pre-training model proposed by Google AI Research in October 2018. The full name of BERT is Bidirectional Encoder Representation from Transformers. BERT can be used to extract features from source code data to train vulnerability embedding vectors.
[0003] Smart contracts inevitably have security vulnerabilities, but frequent smart contract vulnerability incidents seriously threaten the security of smart contract applications. Current smart contract vulnerability detection methods are mostly based on code audits based on expert experience and rely on existing smart contract vulnerability databases. However, the types of smart contract vulnerabilities currently covered are incomplete, with many false positives and missed positives, and rely on manual review. They cannot meet the challenges of the growing number and scale of smart contracts, increasingly complex functions, and difficulty in vulnerability mining.
[0004] The present invention is based on the automatic detection method of smart contract vulnerabilities in the graph neural network. The smart contract vulnerability detection method proposed in the present invention has a higher degree of automation, reduces the dependence on domain expert knowledge, greatly saves the cost of code auditing, and improves the efficiency of code auditing. Compared with other methods of using deep learning to detect smart contract vulnerabilities, this method is based on the graph neural network method and uses the smart contract code attribute graph to better preserve the grammatical and semantic information in the source code. Summary of the invention
[0005] In view of the above-mentioned defects of the prior art, the technical problem to be solved by the present invention is to provide a method for automatically analyzing smart contract vulnerabilities based on graph neural network. Figure 1 As shown, the following steps are included:
[0006] (1) The smart contract virtual machine compiles the smart contract vulnerability source code;
[0007] (2) Construct the smart contract code property graph (CPG);
[0008] (3) Pre-train the code property graph (CPG) using BERT pre-training;
[0009] (4) Create a graph embedding model to extract vulnerability features;
[0010] (5) Training vulnerability detection model;
[0011] (6) Use the model to perform vulnerability detection.
[0012] Furthermore, the step (1) is specifically as follows: building a smart contract compilation and operation environment, installing a smart contract virtual machine, using the smart contract virtual machine to compile the smart contract source code, and generating bytecode.
[0013] Furthermore, the step (2) is specifically: constructing a smart contract code property graph (CPG), including obtaining a smart contract abstract syntax tree (AST), a function control flow graph (CFG), and a program dependency graph (PDG) from the compiler.
[0014] Furthermore, the step (3) is specifically as follows: firstly input the code attribute graph into BERT for pre-training to extract the semantic information therein, use the Bert pre-training model to pre-train the CPG, modify the top softmax layer, and fine-tune the parameters.
[0015] Furthermore, the step (4) is specifically as follows: extract vulnerability features by creating a graph embedding model, which may be a graph neural network model such as a message passing neural network (MPNN) or a gated graph neural network (GGNN) to calculate a code property graph (CPG). Input the vector containing semantic information into the MPNN or GGNN to extract graph structure information, aggregate the features through the graph neural network model, and finally output the update coefficient and reset coefficient through the fully connected layer.
[0016] Furthermore, the step (5) is specifically as follows: the training vulnerability data set is divided into a test set and a validation set. When the amount of data is relatively small, a 7:3 ratio of training data to test data or a 6:2:2 ratio of training data, validation data and test data can be used. The training process needs to be debugged, using regularization techniques, residual connections and other methods.
[0017] Furthermore, step (6) is specifically as follows: using the model to detect vulnerabilities requires compiling the smart contract code and constructing a smart contract code property graph (CPG), and the construction method is the same as step (2). The trained graph neural network model is passed in, and the graph neural network model will give the vulnerabilities of the input smart contract. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] Hereinafter, some specific embodiments of the present invention will be described in detail in an exemplary and non-limiting manner with reference to the accompanying drawings. The same reference numerals in the accompanying drawings indicate the same or similar components or parts. It should be understood by those skilled in the art that these drawings are not necessarily drawn to scale. The objects and features of the present invention will become more apparent in view of the following description in conjunction with the accompanying drawings, in which:
[0019] Figure 1 This is a flow chart of the method for automatically detecting smart contract vulnerabilities based on graph neural networks of the present invention. DETAILED DESCRIPTION
[0020] In order to make the present invention more clearly understandable for its invention points, the present invention will be further described below in conjunction with the accompanying drawings and examples. In the following description, many details and specific examples are set forth, and these examples are provided in order to enable a more thorough understanding of the present invention and to be able to convey the present invention to those skilled in the art in a complete and vivid manner. Although the present invention can be implemented in many other ways different from this description, those skilled in the art can make corresponding generalizations without violating the connotation of the present invention, so the present invention is not limited by the specific examples and specific drawings disclosed below.
[0021] The technology of the present invention will be described in detail below in conjunction with the accompanying drawings and specific embodiments.
[0022] like Figure 1 As shown, the method for automatic detection of smart contract vulnerabilities based on graph neural network provided by the present invention includes the following steps:
[0023] S1. The smart contract virtual machine V compiles the smart contract vulnerability source code S, and after compilation, generates bytecode B.
[0024] S2. Construct a smart contract code property graph C based on the generated bytecode B. The smart contract code property graph C includes the smart contract abstract syntax tree (AST), function control flow graph (CFG), and program dependency graph (PDG) obtained from the compiler.
[0025] S3. Use BERT pre-training to pre-train the code attribute graph C. First, input the code attribute graph into BERT for pre-training to extract the semantic information. Use the Bert pre-training model to pre-train CPG, modify the top softmax layer, and fine-tune the parameters. The final smart contract code attribute graph C is represented as G(V, A), where V is a list of all node vectors in the graph and A is the adjacency matrix of the graph.
[0026] S4. Use a message passing neural network (MPNN) to learn a better node vectorization representation from G(V, A), aggregate the features through the graph neural network model, and finally output the update coefficients and reset coefficients through the fully connected layer.
[0027] S5. Training vulnerability detection model, the training vulnerability data set is divided into test set and verification set, using 6:2:2 training data, verification data and test data. The training data and test set smart contract source code are processed by building the smart contract code attribute graph and then passed into the MPNN model M for training and testing.
[0028] S6. Use model M to detect vulnerabilities, compile the smart contract code to be detected and build the smart contract code property graph (CPG), the construction method is the same as step (2). Pass in the trained MPNN model, and the MPNN model will give the vulnerabilities of the input smart contract.
Claims
1. A method for automatic detection of smart contract vulnerabilities based on graph neural network, specifically comprising the following steps: (1) The smart contract virtual machine compiles the smart contract vulnerability source code S, and after compilation, generates bytecode B; (2) constructing a smart contract code property graph C based on the generated bytecode B, wherein the smart contract code property graph C includes the smart contract abstract syntax tree (AST), function control flow graph (CFG), and program dependency graph (PDG) obtained from the compiler; (3) Use BERT pre-training to pre-train the code attribute graph C. First, input the code attribute graph into BERT for pre-training to extract the semantic information. Use the Bert pre-training model to pre-train the CPG, modify the top softmax layer, and fine-tune the parameters. Finally, the smart contract code attribute graph C is represented as G(V, A), where V is a list of all node vectors in the graph and A is the adjacency matrix of the graph. (4) Use a message passing neural network (MPNN) to learn a better node vectorization representation from G(V, A), aggregate the features through a graph neural network model, and finally output the update coefficients and reset coefficients through a fully connected layer; (5) Training the vulnerability detection model, dividing the training vulnerability data set into a training set, a validation set, and a test set. The training set and the test set are processed by constructing the smart contract code attribute graph and then passed into the MPNN model M for training and testing. The validation set is used to adjust the hyperparameters of the MPNN model. (6) Use model M to perform vulnerability detection, compile the smart contract code to be detected and construct the smart contract code property graph (CPG). The construction method is the same as step (2). The trained MPNN model is passed in, and the MPNN model will give the vulnerabilities of the input smart contract.
2. According to claim 1, a method for automatic detection of smart contract vulnerabilities based on graph neural network is characterized in that: In the step (1), the smart contract virtual machine compiles the smart contract vulnerability source code, which can be a specific smart contract virtual machine or a general smart contract virtual machine.
3. According to claim 1, a method for automatically detecting smart contract vulnerabilities based on graph neural network is characterized in that: The step (2) constructs a smart contract code property graph (CPG), which can be constructed using automated tools and includes a smart contract abstract syntax tree, a control flow graph, and program dependency graph semantics.
4. According to claim 1, a method for automatic detection of smart contract vulnerabilities based on graph neural network is characterized in that: The step (3) uses the Bert pre-training model to pre-train the CPG code attribute graph, modify the top softmax layer, and fine-tune the parameters.
5. According to claim 1, the method for automatic detection of smart contract vulnerabilities based on graph neural network is characterized in that: The step (4) uses a graph embedding model to extract vulnerability features, which can be achieved by using a graph neural network model such as a message passing neural network (MPNN) or a gated graph neural network (GGNN) to calculate a code property graph (CPG).
6. According to claim 1, a method for automatic detection of smart contract vulnerabilities based on graph neural network is characterized in that: The training vulnerability data set in step (5) is divided into a test set and a validation set, and parameter debugging is required during the training process.
7. The method for automatic detection of smart contract vulnerabilities based on graph neural network according to claim 6 is characterized in that: The dataset for smart contract vulnerability training can be publicly known vulnerabilities or self-labeled vulnerabilities.
8. According to claim 1, a method for automatic detection of smart contract vulnerabilities based on graph neural network is characterized in that: The step (6) uses the model to perform vulnerability detection, which requires compiling the smart contract code and building a smart contract code property graph (CPG).
