A method for detecting vulnerabilities of smart contracts based on self-supervised learning
By constructing a smart contract opcode program dependency graph and using a self-supervised learning graph autoencoder and a stacked denoising autoencoder model, deep semantic features of smart contracts are extracted, which solves the problems of insufficient accuracy and robustness of vulnerability detection in existing methods and achieves more efficient vulnerability detection.
Patent Information
- Application Number
- CN202310260966.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-17
- Publication Date
- 2026-01-30
- Estimated Expiration
- 2043-03-17
AI Technical Summary
Existing bytecode-level smart contract vulnerability detection methods do not provide a comprehensive semantic and structural representation of smart contracts, resulting in low model robustness and redundancy in feature extraction.
We construct a smart contract opcode program dependency graph, train a graph autoencoder and a stacked denoising autoencoder model using a self-supervised learning method, and extract deep semantic features of smart contracts for smart contract vulnerability detection.
It improves the accuracy and robustness of vulnerability detection, solves the problems of insufficient dataset and feature redundancy in existing methods, and achieves more efficient vulnerability detection.
Smart Images

Figure CN116340951B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of smart contract security, specifically relating to a smart contract vulnerability detection method based on self-supervised learning. Background Technology
[0002] Because smart contract code is not widely open source, more and more research focuses on analyzing smart contract bytecode to determine whether smart contracts contain vulnerabilities.
[0003] The paper "A novel smart contract vulnerability detection method based on information graph and ensemble learning" proposes a smart contract vulnerability detection method based on information graphs and ensemble learning. This method calculates the co-occurrence frequency of opcodes from collected smart contract opcodes, constructing an opcode co-occurrence matrix. Key opcode sequences of smart contracts are identified using predefined rules, and an information graph is constructed based on co-occurrence relationships to represent the vulnerability patterns of the smart contracts. Nodes in the information graph are composed of opcodes, which are connected according to co-occurrence frequency and order of appearance. To convert nodes into a vector matrix representation, all paths in the information graph of each smart contract are extracted, constructing an opcode sequence set, which is used to train embedding models such as Word2Vec. After obtaining the vector representation of the opcode sequence, it is input into the ensemble learning model for model training.
[0004] The paper "CodeNet: Code-targeted convolutional neural network architecture for smart contract vulnerability detection" proposes a novel CNN architecture, CodeNet, which performs vulnerability detection on smart contracts while preserving their semantics and context. To generate the input for the CNN architecture, the preprocessing stage converts a fixed sequence of bytecode into an RGB image, with each three bytes of bytecode mapped to RGB pixels. To preserve the semantics and contextual information of the bytecode, CodeNet modifies the two-dimensional RGB image into a one-dimensional form. Since convolutional operations with stride lose pixel-level features, thus compromising the semantics and contextual information of smart contracts, CodeNet uses non-stride convolutional operations and employs depthwise separable convolutions to mitigate the increased computational cost and parameters associated with non-stride convolutions.
[0005] However, existing bytecode-level smart contract vulnerability detection methods do not fully represent the semantics and structure of smart contracts, and the extracted features are largely redundant. The models cannot learn rich semantic and structural features related to vulnerabilities, resulting in low robustness of the trained vulnerability detection models. Summary of the Invention
[0006] To address the aforementioned problems, the present invention aims to provide a smart contract vulnerability detection method and system based on self-supervised learning. This method constructs a smart contract opcode program dependency graph at the smart contract bytecode level, and then uses self-supervised learning to enable the model to further learn the key semantic features most relevant to vulnerabilities in the smart contract for smart contract vulnerability detection.
[0007] To achieve the above objectives, the present invention adopts the following technical solution:
[0008] A smart contract vulnerability detection method based on self-supervised learning, the method comprising:
[0009] Collect smart contract bytecode;
[0010] Construct the smart contract opcode program dependency graph;
[0011] Extract vulnerability-related program dependency graph slices to construct a self-supervised learning dataset and a vulnerability detection model dataset;
[0012] A self-supervised learning model is trained using a self-supervised learning dataset to extract slice features of the program dependency graph.
[0013] Train a smart contract vulnerability detection model using a vulnerability detection model dataset;
[0014] The trained self-supervised learning model is used to extract features related to vulnerabilities in the smart contract under test, which are then input into the trained smart contract vulnerability detection model for vulnerability detection.
[0015] Furthermore, the construction of the smart contract opcode program dependency graph specifically includes:
[0016] Disassemble the smart contract bytecode into a sequence of smart contract opcodes;
[0017] Simulate the execution of smart contract operation sequences to construct an opcode control flow graph and an opcode data dependency graph;
[0018] Construct an opcode control dependency graph based on the opcode control flow graph;
[0019] Merge the opcode control dependency graph and the opcode data dependency graph to construct the opcode program dependency graph.
[0020] Furthermore, the extraction of vulnerability-related program dependency graph tiles and the construction of a self-supervised learning dataset and a vulnerability detection model dataset specifically include:
[0021] Based on the characteristics of smart contract vulnerabilities, select opcodes that may trigger vulnerabilities as candidate nodes;
[0022] In the opcode program dependency graph, candidate nodes are used as initial nodes, and forward slicing and backward slicing algorithms are executed to construct vulnerability-related opcode program dependency graph slices, thus constructing an opcode program dependency graph slice dataset.
[0023] A certain proportion of the data in the opcode program dependency graph slice dataset is selected as a self-supervised learning dataset, and the remaining opcode program dependency graph slices are used to perform vulnerability annotation using the Mythril tool as a vulnerability detection model dataset.
[0024] Furthermore, the self-supervised learning model includes a graph autoencoder model and a stacked denoising autoencoder model.
[0025] Furthermore, the graph autoencoder specifically includes an embedding layer, a masking layer, an encoding layer, a decoding layer, a loss calculation layer, and an output layer, wherein:
[0026] The Embedding layer converts the nodes of the program-dependent graph slices into vector feature representations;
[0027] The masking layer uses a random sampling method without replacement to select a subset of nodes and uses a mask [MASK] to mask the features of these nodes;
[0028] The encoding layer takes the masked slices of the program dependency graph as input and uses the graph neural network to learn the hidden encoding of the nodes as output.
[0029] The decoding layer takes the hidden code output by the encoding layer as input and uses a graph neural network to reconstruct the features of the nodes as output.
[0030] The loss calculation layer uses cosine error as the loss function to calculate the error between the original features and the reconstructed features of the occluded node, and guides the graph autoencoder model to be trained through backpropagation;
[0031] The output layer replaces the decoding layer and loss calculation layer of the trained graph autoencoder model, takes the node features output by the encoding layer as input, and uses graph pooling operation to output program-dependent graph slice features.
[0032] After the graph autoencoder is trained, the embedding layer, encoding layer, and output layer are retained for extracting initial features of the program dependency graph slices.
[0033] Furthermore, the graph neural network can be any type of graph neural network layer, including GCN, GAT, GIN, and GGNN, etc.
[0034] Furthermore, the graph pooling operation is a non-parametric pooling operation, such as AveragePooling and MaxPooling.
[0035] Furthermore, the noise reduction autoencoder specifically includes a noise introduction layer, an encoding layer, a decoding layer, and a cross-entropy loss layer, wherein:
[0036] The noise introduction layer sets certain dimensions of the input multidimensional original data features to 0 with a certain probability, thus obtaining noisy data features.
[0037] The coding layer takes noisy data features as input and extracts hidden layer features;
[0038] The decoding layer takes the hidden layer features as input and reconstructs features of the same dimension as the input data as output.
[0039] The cross-entropy loss layer calculates the cross-entropy loss between the original data features and the reconstructed data features, guiding the denoising autoencoder to be trained through backpropagation;
[0040] After the noise reduction autoencoder is trained, the encoding layer is retained as the feature extraction layer.
[0041] Furthermore, the stacked denoising autoencoder consists of multiple stacked denoising autoencoders, and its training process is as follows:
[0042] The first layer of the denoising autoencoder uses the initial features of the program-dependent graph slices output by the trained graph autoencoder as input for training;
[0043] After that, each layer of the denoising autoencoder uses the hidden features output by the previous layer as the input of the current layer for training.
[0044] Once each layer of the noise reduction autoencoder has been trained, the stacked noise reduction autoencoder training is complete.
[0045] The output of the trained stacked denoising autoencoder depends on the denoising features of the image slices.
[0046] Furthermore, the smart contract vulnerability detection model is a commonly used machine learning model, including SVM, KNN, and random forest. The smart contract vulnerability detection model takes the denoised features of the program dependency graph slices output by the trained self-supervised learning model as input and the probability of vulnerabilities in the corresponding program dependency graph slices as output.
[0047] A smart contract vulnerability detection system based on self-supervised learning, the system comprising:
[0048] The smart contract collection module is used to obtain smart contract bytecode datasets;
[0049] The data preprocessing module is used to build self-supervised learning datasets and vulnerability detection model datasets;
[0050] The graph autoencoder module is used to train the graph autoencoder model and extract the initial features of the graph slices that depend on the smart contract opcode program.
[0051] The stacked denoising autoencoder module is used to train the stacked denoising autoencoder model and denoise the initial features of the opcode program dependency graph slice to obtain denoised features.
[0052] The smart contract vulnerability detection module is used to train a smart contract vulnerability detection model to detect whether smart contracts have vulnerabilities.
[0053] Furthermore, the data preprocessing module specifically includes:
[0054] Disassemble the smart contract bytecode into a sequence of smart contract opcodes;
[0055] Simulate the execution of smart contract operation sequences, and establish an opcode program dependency graph.
[0056] Based on the characteristics of smart contract vulnerabilities, select opcodes that may trigger vulnerabilities as candidate nodes;
[0057] In the opcode program dependency graph, candidate nodes are used as initial nodes, and forward slicing and backward slicing algorithms are executed to construct vulnerability-related opcode program dependency graph slices, thus constructing an opcode program dependency graph slice dataset.
[0058] A certain proportion of the data in the opcode program dependency graph slice dataset is selected as the self-supervised learning dataset, and the remaining opcode program dependency graph slices are used to perform vulnerability annotation using the Mythril tool as the vulnerability detection model dataset.
[0059] Furthermore, the graph autoencoder module specifically includes an embedding layer, a masking layer, an encoding layer, a decoding layer, a loss calculation layer, and an output layer.
[0060] Furthermore, the stacked noise reduction autoencoder module specifically includes multiple stacked noise reduction autoencoders.
[0061] Furthermore, the noise reduction autoencoder specifically includes a noise introduction layer, an encoding layer, a decoding layer, and a cross-entropy loss layer.
[0062] Furthermore, the smart contract vulnerability detection module specifically includes machine learning models such as SVM, KNN, and random forest.
[0063] The present invention has the following advantages over the prior art:
[0064] 1. This invention analyzes smart contract vulnerabilities at the bytecode level, avoiding the problem of insufficient datasets caused by the low open-source nature of smart contracts;
[0065] 2. This invention uses a masking-based graph autoencoder model, which does not provide label information during training. This allows the model to learn deep semantic features of smart contracts by reconstructing node features, solving the problem that the features extracted by existing methods remain at a shallow level and improving the accuracy of vulnerability detection.
[0066] 3. This invention uses a stacked noise-reducing autoencoder model, which adds noise to improve the model's generalization performance, solves the problem of excessive feature redundancy in existing methods, and improves the robustness of vulnerability detection. Attached Figure Description
[0067] Figure 1 This is a flowchart of the smart contract vulnerability detection method based on self-supervised learning according to Embodiment 1 of the present invention;
[0068] Figure 2 This is a block diagram of the smart contract vulnerability detection system based on self-supervised learning according to Embodiment 2 of the present invention; Detailed Implementation
[0069] The present invention will now be further described in conjunction with the accompanying drawings and specific embodiments.
[0070] Example 1
[0071] like Figure 1 As shown, the embodiment of the smart contract vulnerability detection method based on self-supervised learning provided by the present invention includes four stages: smart contract collection, data preprocessing, model training, and smart contract vulnerability detection, wherein:
[0072] Smart contract collection: Obtain smart contract bytecode from platforms such as EtherScan and build a smart contract bytecode dataset;
[0073] Data preprocessing: Construct a smart contract opcode program dependency graph by simulating execution, extract vulnerability-related program dependency graph slices, and construct a self-supervised learning dataset and a vulnerability detection model dataset;
[0074] Model Training: A self-supervised learning model is trained using a self-supervised learning dataset to extract program dependency graph slice features; a smart contract vulnerability detection model is trained using a vulnerability detection model dataset.
[0075] Smart contract vulnerability detection: After preprocessing the data of the smart contract to be tested, the opcode program dependency graph slices are obtained, and a trained smart contract vulnerability detection model is used to detect vulnerabilities.
[0076] The smart contract vulnerability detection method based on smart contract opcode program dependency graph and graph neural network proposed in this embodiment specifically includes the following steps:
[0077] S1: Collect smart contract bytecode;
[0078] S2: Construct the smart contract opcode program dependency graph;
[0079] Furthermore, step S2 includes:
[0080] Disassemble the smart contract bytecode into a sequence of smart contract opcodes;
[0081] Simulate the execution of smart contract operation sequences and construct an opcode program dependency graph.
[0082] S3: Extract vulnerability-related program dependency graph slices to construct a self-supervised learning dataset and a vulnerability detection model dataset;
[0083] Furthermore, step S3 includes:
[0084] Based on the characteristics of smart contract vulnerabilities, opcodes that may trigger vulnerabilities are selected as candidate nodes. In this example, the opcode CALL, which is related to reentrancy vulnerabilities, is selected as a candidate node.
[0085] In the opcode program dependency graph, CALL is used as the initial node. The forward slicing and backward slicing algorithms are executed to construct the vulnerability-related opcode program dependency graph slices and construct the opcode program dependency graph slice dataset.
[0086] A certain proportion of the data in the opcode program dependency graph slice dataset is selected as a self-supervised learning dataset, and the remaining opcode program dependency graph slices are labeled with reentrancy vulnerabilities using the Mythril tool as a vulnerability detection model dataset.
[0087] S4: Train a graph autoencoder using a self-supervised learning dataset;
[0088] Furthermore, step S4 includes:
[0089] Convert the nodes of the program-dependent graph slice into vector feature representations;
[0090] A subset of nodes is selected using random sampling without replacement, and the features of these nodes are masked using a mask.
[0091] The masked program dependency graph slices are taken as input, and the hidden codes of the nodes are learned by the graph neural network as output.
[0092] The hidden code output by the coding layer is used as input, and the features of the nodes are reconstructed using a graph neural network as output.
[0093] The cosine error is used as the loss function to calculate the error between the original features and the reconstructed features of the occluded node, and the graph autoencoder model is trained by backpropagation.
[0094] S5: Training a stacked denoising autoencoder;
[0095] Furthermore, step S5 includes:
[0096] The first layer of the denoising autoencoder takes the initial features of the program-dependent graph slices output by the trained graph autoencoder as input;
[0097] With a certain probability, some dimensions of the input multidimensional original data features are set to 0 to obtain noisy data features;
[0098] Noisy data features are used as input to extract hidden layer features;
[0099] The hidden layer features are used as input, and the features with the same dimension as the input data are reconstructed as output.
[0100] The cross-entropy loss between the original data features and the reconstructed data features is calculated to guide the training of the denoising autoencoder through backpropagation;
[0101] Subsequently, each denoising autoencoder uses the hidden features output by the previous denoising autoencoder as the input for training the current layer.
[0102] S6: Train a smart contract vulnerability detection model using a vulnerability detection model dataset;
[0103] Furthermore, step S6 includes:
[0104] The trained graph autoencoder and stacked denoising autoencoder are used to extract denoising features of opcode program dependency graph slices in the vulnerability detection model dataset, and then combined with the corresponding real vulnerability labels to train the smart contract vulnerability detection model.
[0105] S7: Use the trained self-supervised learning model to extract features related to vulnerabilities in the smart contract under test, and input them into the trained smart contract vulnerability detection model for vulnerability detection.
[0106] Example 2
[0107] like Figure 2As shown, the smart contract vulnerability detection system based on self-supervised learning provided by this invention includes a smart contract collection module, a data preprocessing module, a graph autoencoder module, a stacked noise reduction autoencoder module, and a smart contract vulnerability detection module. Wherein:
[0108] The smart contract collection module is used to collect smart contract bytecode and build a smart contract bytecode dataset.
[0109] The data preprocessing module constructs a smart contract opcode program dependency graph based on the smart contract bytecode, and then uses a slicing algorithm to extract slices of the smart contract opcode program dependency graph based on the candidate opcode nodes, thereby constructing a self-supervised learning dataset and a vulnerability detection model dataset.
[0110] The graph autoencoder module uses a self-supervised learning dataset to train a graph autoencoder model and outputs opcodes that depend on the initial features of graph slices.
[0111] The stacked denoising autoencoder module trains the stacked denoising autoencoder model using a self-supervised learning dataset and outputs opcode program dependency graph slice denoising features.
[0112] The smart contract vulnerability detection module trains a smart contract vulnerability detection model by combining a trained graph autoencoder, a stacked denoising autoencoder, and a vulnerability detection model dataset, and outputs the probability that the corresponding smart contract has a vulnerability.
Claims
1. A method for detecting vulnerabilities of a smart contract based on self-supervised learning, characterized in that, The method comprises: Collecting smart contract bytecode; Building a smart contract operation code program dependency graph; Extracting vulnerability-related operation code program dependency graph slices, building a self-supervised learning dataset and a vulnerability detection model dataset; Training a self-supervised learning model using the self-supervised learning dataset and extracting program dependency graph slice features; Training a smart contract vulnerability detection model using the vulnerability detection model dataset; Using the trained self-supervised learning model to extract vulnerability-related features of the smart contract to be tested, and then inputting them into the trained smart contract vulnerability detection model for vulnerability detection; The self-supervised learning model comprises a graph autoencoder model and a stacked denoising autoencoder model; The denoising autoencoder comprises a noise introduction layer, an encoding layer, a decoding layer, and a cross-entropy loss layer, wherein: The noise introduction layer sets some dimensions of the input program dependency graph slice features to 0 with a certain probability to obtain noisy data features; The encoding layer takes the noisy data features as input and extracts hidden layer features; The decoding layer takes the hidden layer features as input and reconstructs features of the same dimension as the input data as output; The cross-entropy loss layer calculates the cross-entropy loss between the original data features and the reconstructed data features to guide the training of the denoising autoencoder through backpropagation; After the denoising autoencoder is trained, the encoding layer is retained as a feature extraction layer; The stacked denoising autoencoder is composed of multiple stacked denoising autoencoders, and its training process is as follows: The first layer of denoising autoencoder takes the initial features of the program dependency graph slice output by the trained graph autoencoder as input for training; Each subsequent layer of denoising autoencoder takes the hidden features output by the previous layer of denoising autoencoder as input for training after the training of the previous layer is completed; After each layer of denoising autoencoder is trained, the stacked denoising autoencoder is trained; The trained stacked denoising autoencoder outputs denoised features of the program dependency graph slice.
2. The self-supervised learning based intelligent contract vulnerability detection method of claim 1, wherein, The building of the smart contract operation code program dependency graph specifically comprises: Disassembling the smart contract bytecode into a smart contract operation code sequence; Simulating the execution of the smart contract operation sequence to build an operation code control flow graph and an operation code data dependency graph; Building an operation code control dependency graph based on the operation code control flow graph; Merging the operation code control dependency graph and the operation code data dependency graph to build the operation code program dependency graph.
3. The self-supervised learning based intelligent contract vulnerability detection method of claim 1, wherein, The extraction of vulnerability-related program dependency graph slices, the building of the self-supervised learning dataset and the vulnerability detection model dataset specifically comprises: Selecting operation codes that may trigger vulnerabilities as candidate nodes based on smart contract vulnerability features; In the operation code program dependency graph, taking the candidate nodes as initial nodes, performing forward slicing and backward slicing algorithms to build vulnerability-related operation code program dependency graph slices and build an operation code program dependency graph slice dataset; Selecting a certain proportion of data from the operation code program dependency graph slice dataset as a self-supervised learning dataset, and using the remaining operation code program dependency graph slices to perform vulnerability labeling using the Mythril tool as a vulnerability detection model dataset.
4. The self-supervised learning based intelligent contract vulnerability detection method of claim 1, wherein, The graph autoencoder specifically comprises an Embedding layer, a mask layer, an encoding layer, a decoding layer, a loss calculation layer, and an output layer, wherein: The Embedding layer converts the nodes of the program dependence graph slice into vector feature representations; The mask layer selects a portion of the nodes of the program dependence graph slice using a method of non-replacement random sampling and masks the features of the selected nodes using a mask [MASK]; The encoding layer uses a graph neural network to learn the hidden encoding of the nodes as output using the program dependence graph slice masked by the mask as input; The decoding layer uses a graph neural network to reconstruct the features of the nodes as output using the hidden encoding output by the encoding layer as input; The loss calculation layer uses a cosine error as a loss function to calculate the error between the original features and the reconstructed features of the masked nodes, guiding the graph autoencoder model to be trained through backpropagation; The output layer replaces the decoding layer and the loss calculation layer of the trained graph autoencoder model, uses the node features output by the encoding layer as input, and outputs the program dependence graph slice features using a graph pooling operation. The graph autoencoder retains the Embedding layer, the encoding layer, and the output layer after training for extracting initial features of the program dependence graph slice.
5. The self-supervised learning based intelligent contract vulnerability detection method of claim 4, wherein, The graph neural network is any one of the GCN, GAT, GIN, and GGNN graph neural network layers.
6. The self-supervised learning based intelligent contract vulnerability detection method of claim 4, wherein, The graph pooling operation is a non-parametric pooling, including any one of the AveragePooling and MaxPooling.
7. The self-supervised learning based intelligent contract vulnerability detection method of claim 1, wherein, The smart contract vulnerability detection model is any one of the SVM, KNN, and random forest, and the smart contract vulnerability detection model takes the denoised program dependence graph slice feature output by the trained self-supervised learning model as input and outputs the probability of the corresponding program dependence graph slice having a vulnerability.
Citation Information
Patent Citations
Intelligent contract vulnerability detection method based on artificial intelligence
CN115017513A
Smart contract code vulnerability detection method and apparatus, computer device and storage medium
WO2021037196A1