Intelligent contract vulnerability detection method based on feature fusion and deep learning technology
Through feature fusion and deep learning technology, combined with multi-level feature extraction and fusion strategies, the problems of low detection efficiency of smart contract vulnerability and insufficient feature extraction are solved, and higher detection accuracy and stability are achieved.
Patent Information
- Application Number
- CN202410996065.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-24
- Publication Date
- 2024-11-12
AI Technical Summary
The existing smart contract vulnerability detection methods are inefficient and cannot effectively detect complex vulnerabilities. Manual review is time-consuming and the accuracy depends on expert knowledge. Deep learning methods have high computing resources requirements in large-scale detection, and a single model has defects in feature extraction. .
Using a smart contract vulnerability detection method based on feature fusion and deep learning, a balanced data set is generated through cross-verification and data preprocessing, and features are extracted using word embedding models and channel self-attention technology, combining convolutional neural networks and bidirectional gating loops. Unit and bidirectional long and short-term memory network models are learned, and vulnerability detection is used using MLP classification model through multi-level feature extraction and fusion strategies.
Improve the accuracy and robustness of smart contract vulnerability detection, enhance the breadth and depth of feature extraction, reduce the deviation and variance of a single model, improve detection performance and stability, and can more comprehensively capture the structure and feature.
Smart Images

Figure CN118940277A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of blockchain security and relates to a smart contract vulnerability detection method based on feature fusion and deep learning technology. Background Art
[0002] With the rapid development of blockchain technology, smart contracts, as computer programs that automatically execute contracts, are being widely used on blockchain platforms. Smart contracts automate transactions and contract execution through programming codes, and their security and stability are crucial to the healthy operation of the entire blockchain system. Although smart contracts provide an efficient and transparent transaction mechanism, they have various security risks and vulnerabilities due to their open nature and complex programming languages. These vulnerabilities may lead to serious consequences such as financial losses, contract execution errors, and user information leakage, which seriously affect the security and credibility of the blockchain system.
[0003] At present, security detection for smart contracts mainly relies on traditional technologies such as static analysis, symbolic execution and fuzz testing. Traditional smart contract vulnerability detection methods mainly rely on the combination of manual review and static analysis tools. However, these methods may not be able to detect some complex vulnerability patterns, and manual review is a time-consuming and labor-intensive task. Its accuracy is highly dependent on expert knowledge. Even experienced experts may miss potential vulnerabilities due to negligence or subjective factors. Therefore, smart contract vulnerability detection methods are increasingly using technologies such as machine learning and deep learning to improve the accuracy and efficiency of detection. They rely on neural networks to automatically identify vulnerability features, ensuring accuracy while reducing manual overhead. Symbolic execution technology explores different execution paths and possible state changes of the program by symbolizing the symbolic variables of the program instead of executing specific input values. However, the state space of smart contracts may be very large, resulting in the symbolic execution method may require a lot of computing resources and time to execute the process. At the same time, because the branch statements and loop statements in smart contracts will cause the execution path of the program to grow exponentially, this method may face the problem of path explosion. The basic principle of fuzz testing is to send a large amount of randomly generated input data to the program to be tested, and then monitor the abnormal response and behavior of the program to discover security issues. Because it is based on random input, it may not be effective in discovering specific vulnerabilities and errors.
[0004] Due to the increasing types of smart contract vulnerabilities, most existing smart contract vulnerability detection methods still have the problems of low efficiency and inability to cope with large-scale smart contract vulnerability detection. Deep learning plays an increasingly important role in the field of smart contract vulnerability detection. For novel security vulnerability types, deep learning methods have good scalability and adaptability. At present, software vulnerability detection methods based on deep learning can be roughly divided into two categories. The first method is to represent the source code as a graph structure such as an abstract syntax tree or a code attribute graph, with code statements as nodes in the graph, and the data flow, control flow and other information of the code as the edges of the graph, and then use graph neural networks to learn and classify vulnerability features; the second method is to convert the source code into a text sequence and use natural language processing technology to learn and classify vulnerability features. Although the first detection method can capture the code structure and complex dependencies more accurately during the representation process, its implementation complexity is high and requires additional steps to construct and process graph data, such as generating AST or attribute graphs, defining node edge features, etc. In addition, graph neural networks have high computing resource requirements for large-scale graph data, and their design and parameter adjustment are more challenging, so their popularity in practical applications is limited. Although the second method fully retains the logical information of the code in terms of representation, it will lose some contextual semantic information. In order to solve the above problems, we integrated different word embedding models to make the smart contract vulnerability feature extraction more comprehensive, and used multiple deep learning models for learning and classification to make it more comprehensive in semantic feature extraction, thereby making up for the shortcomings of a single model in this regard. Summary of the invention
[0005] The purpose of the present invention is to detect different types of smart contract vulnerabilities more accurately by solving the problem of inadequacy of smart contract vulnerability feature extraction and feature learning. To this end, the present invention proposes a smart contract vulnerability detection method based on feature fusion and deep learning technology, comprising:
[0006] Step 1: Use cross-validation to reasonably divide the data set, and perform normalization and oversampling preprocessing on the sample set used for the test to obtain a more balanced vulnerability data set;
[0007] Step 2: construct an integrated network model through the word embedding model, and use the model to extract the features of the smart contract vulnerability code;
[0008] Step 3: Based on the features obtained in step 2, channel self-attention technology is used to achieve feature fusion to obtain preliminary training sample sets and test sample sets;
[0009] Step 4: Based on the features obtained in steps 2 and 3, use convolutional neural network, bidirectional gated recurrent unit and bidirectional long short-term memory network model training to obtain the final training sample set and test sample set, use the MLP classification model to classify the data for vulnerabilities, and evaluate the detection performance of the model based on the prediction results.
[0010] In the first aspect, the specific steps of the above step 1 are as follows:
[0011] Step 1.1, use the AtExtractCode file to read the file content from the given file path, identify and extract the functions and constructors in the file, store the content of each function or constructor in a list, and finally return a list containing all functions and constructors;
[0012] Step 1.2: Normalize the variable and function names defined by the developer in the code and delete the comment statements;
[0013] Step 1.3, oversampling the training data using the oversampling method will alleviate the problem of vulnerability data imbalance and improve the training accuracy of the data set; set the sampling ratio according to the sample imbalance ratio to determine the sampling rate, and for each minority class sample, use the K nearest neighbor algorithm to find its nearest neighbor sample in the feature space, and generate a new sample by randomly interpolating between the sample and its nearest neighbor sample. The interpolation process uses a random coefficient α to control the position of the new sample so that it is between the original sample and the nearest neighbor sample.
[0014] In the second aspect, the specific steps of the above step 2 are as follows:
[0015] Step 2.1, store the preprocessed training data sample set in the form of a list, build an integrated network model of the word embedding model, and use word2vec, fasttext and glove (word vector embedding technology) for word vector embedding, and set the length of the vector to 100 to keep the obtained word vector data consistent.
[0016] In the third aspect, the specific steps of the above step 3 are as follows:
[0017] Step 3.1, build an integrated network model. First, load multiple pre-trained word embedding models (word2vec, fasttext, and glove), and ensure that all embedded model vector dimensions are the same;
[0018] Step 3.2, assign a weight to each word embedding model through the channel attention mechanism. This mechanism includes average pooling and maximum pooling operations, as well as convolutional layers and activation functions, and finally uses the Sigmoid function to calculate the attention weight;
[0019] Step 3.3, stack the three embedding vectors of each word together, calculate the weighted combination through the channel attention mechanism, and get the final integrated word embedding representation. Get the final feature vector X[e1,e2,...,e n ].
[0020] In the fourth aspect, the specific steps of the above step 4 are as follows:
[0021] Step 4.1, build an integrated deep learning model architecture by using convolutional neural network, bidirectional gated recurrent unit and bidirectional long short-term memory network models to learn and extract features, and convert the final word embedding representation into prediction results;
[0022] In step 4.2, vulnerability data of the target dataset is detected through the MLP classifier to evaluate the detection performance of the model.
[0023] Compared with the existing methods, the present invention has the following beneficial effects:
[0024] 1. A smart contract vulnerability feature fusion method combining multi-word embedding method and channel attention mechanism is proposed. This method adopts multi-level feature extraction strategy and fusion strategy, and combines channel attention mechanism to more comprehensively capture the structure and characteristics of smart contracts. In addition, this method not only expands the breadth and depth of feature extraction, but also enhances the flexibility and accuracy of feature fusion through the attention mechanism.
[0025] 2. A smart contract vulnerability detection method combining ensemble learning and deep learning is proposed. This method organically combines multiple deep learning models in an ensemble learning mode for vulnerability detection. It can not only improve the accuracy and robustness of detection, but also find a balance between different data features and vulnerability types. At the same time, it effectively reduces the possible deviation and variance of a single model, thereby improving the overall detection performance and stability. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] Figure 1 It is an overall flow chart of a method for a smart contract vulnerability detection model based on feature fusion and deep learning technology.
[0027] Figure 2 It is a detailed flowchart of a method for smart contract vulnerability detection model based on feature fusion and deep learning technology.
[0028] Figure 3 It is the data sample set information used in the experimental link of the present invention.
[0029] Figure 4 It is the division of the data sample training set and test set used in the experimental link of the present invention.
[0030] Figure 5It is the information and results of experiments on smart contract vulnerability detection models obtained by extracting data features using different schemes.
[0031] Figure 6 It is the information and results of experiments on smart contract vulnerability detection models obtained by learning and classifying the extracted data features using different schemes.
[0032] Figure 7 It is the information and results of experimentally comparing the stability of a smart contract vulnerability detection model method based on feature fusion and deep learning technology proposed in the present invention with other advanced smart contract vulnerability detection methods.
[0033] Figure 8 It is a data display of experimental comparison of the method of smart contract vulnerability detection model based on feature fusion and deep learning technology proposed in this invention with other advanced smart contract vulnerability detection methods. DETAILED DESCRIPTION
[0034] The present invention is further described below in conjunction with the accompanying drawings and implementation examples. It should be noted that the implementation examples described are only intended to facilitate the understanding of the present invention and do not have any limiting effect on it.
[0035] The present invention aims to address the problem of insufficient feature extraction and feature learning in smart contract vulnerability detection. A method for feature fusion through a channel attention mechanism is proposed, as well as a method for feature learning by combining the advantages of different deep learning models through an ensemble learning model. The results are classified through MLP to establish a smart contract vulnerability detection model with good detection performance. A large number of experiments are carried out to prove the feasibility and efficiency of the method.
[0036] like Figure 1 As shown, a method of a smart contract vulnerability detection model based on feature fusion and deep learning technology of the present invention includes:
[0037] Step 201 uses the loadtxt() reading method to reasonably divide the data set (80% for training set and 20% for test set), uses the AtExtractCode file to read the file content from the given file path, identifies and extracts the functions and constructors in the file, stores the content of each function or constructor in a list, and finally returns a list containing all functions and constructors, and performs preprocessing such as normalization and oversampling on the data set to obtain a more balanced vulnerability data set;
[0038] The purpose of implementing data preprocessing in the present invention is that, by dividing the data set and extracting the function content, we can effectively reduce the influence of noise and redundant information, while ensuring the stable performance of the model on the input data. These preprocessing steps not only provide clear input for the subsequent deep learning model, but also lay a solid foundation for the implementation of the vulnerability detection method.
[0039] Step 2011 normalizes the user-defined variable names and function names, and deletes comment statements, blank lines, Solidity code versions, and other parts that are not related to the vulnerability.
[0040] Step 2012 uses an oversampling method to expand the samples with loopholes so that the number of samples with loopholes and samples without loopholes is basically the same.
[0041] The oversampling calculation process includes: obtaining the number of minority class samples (samples without loopholes) and majority class samples (samples with loopholes) in the current data set. Setting the sampling ratio according to the sample imbalance ratio to determine the sampling multiplier, and calculating how many new minority class samples need to be generated. For each minority class sample x i , randomly select a nearest neighbor sample x neighbor , and calculate the difference diff between the two, and generate a random coefficient α between 0 and 1, and calculate the position of the new sample by linear interpolation. The new sample generated in this way is between the demonstration minority sample x i and its nearest neighbor sample x neighbor The characteristic distribution of the original data is retained, while the number of minority class samples is increased.
[0042] Step 202, constructing an integrated network model through a word embedding model, and using the model to extract features of smart contract vulnerability codes;
[0043] Step 221 stores the preprocessed training data sample set in the form of a list, builds an integrated network model of the word embedding model, and uses word2vec, fasttext and glove (word vector embedding technology) to embed the word vectors, and sets the length of the vector to 100 to keep the word vector data consistent. Finally, a global pooling operation is used to aggregate each feature dimension to obtain a fixed-length feature vector X[e1,e2,...,e n ].
[0044] Step 203, using the channel self-attention technology according to the above-obtained features to achieve feature fusion and obtain the final training sample set and test sample set;
[0045] In the above step 203, the steps of implementing feature fusion using the channel attention technology are as follows:
[0046] Step 2031, a weight is assigned to each word embedding model through the channel attention mechanism. Each word has three word embedding vectors {E1, E2, E3} from different sources, and the dimension of each word embedding is d. For each word w i The word embedding vectors are {e i1 ,e i2 ,e i3}. Stack these three word embedding vectors together to form a 3D tensor E i . i Perform a global average pooling operation to obtain a 1D vector, represented as AngPool(E i ), where each element is E i The average value in the channel dimension. ij Represents the embedding E of the i-th word i The value at the jth channel.
[0047]
[0048] For E i Perform a global maximum pooling operation to obtain a 1D vector, represented by MaxPool(E i ), where each element is E i Maximum value over the channel dimension.
[0049] MaxPool(E i )=max(e i1 ,e i2 ,e i3 )
[0050] The results of global average pooling and global maximum pooling are concatenated to form a 2D vector, represented as PoolConcat(E i ).
[0051] PoolConcat(E i )=[AvgPool(E i );MaxPool(E i )]
[0052] The concatenated vector is input into a convolutional layer for feature extraction to obtain an intermediate vector, where ReLU is the activation function.
[0053] Conv(E i )=ReLU(Conv1D(PoolConcat(E i )))
[0054] Next, the output of the convolutional layer is input into a fully connected layer for further feature extraction.
[0055] FC(E i )=ReLU(FC(Conv(E i )))
[0056] Finally, the output of the fully connected layer is passed through the Sigmoid activation function to obtain the attention weight a of each word embedding vector i . Where s represents the Sigmoid activation function.
[0057] a i =s(FC(E i ))
[0058] Step 2032, calculate the weighted combination to obtain the most popular integrated word embedding representation. For each word embedding vector e ij , using the corresponding attention weight a ij Weighted.
[0059] ¢
[0060] e ij =a ij ×e ij
[0061] Add the weighted word embedding vectors to get each word w i The final integrated word embedding representation e i . Combine the final integrated word embedding representations of all words to form the final feature vector X[e1,e2,...,e n ].
[0062] Step 204: According to the above features, a convolutional neural network, a bidirectional gated recurrent unit, and a bidirectional long short-term memory network model are used for training to obtain the final training sample set and test sample set, and the MLP classification model is used to classify the data for vulnerabilities.
[0063] Step 2041: Based on the feature X[e1,e2,...,e n ] Input it to the input layer of three neural networks respectively. The operation of each neural network is as follows:
[0064] The convolutional neural network layer first uses multiple one-dimensional convolution kernels to extract local features, then performs a maximum pooling operation on the convolution results to reduce the feature dimension, and then combines the self-attention mechanism to calculate the attention score of each word, and applies the attention score to the pooled feature representation to obtain the weighted feature representation.
[0065]
[0066]
[0067]
[0068]
[0069] in, represents the feature representation of the jth convolution kernel after the i-th word is embedded; Conv1D(e ij ) represents the embedding vector e of the i-th word i Perform a one-dimensional convolution operation; It is the feature representation after the maximum pooling operation on the convolution result; Attention CNN (h) represents the attention score calculated by the self-attention mechanism, where is the weight matrix in the self-attention mechanism, is the bias term in the self-attention mechanism; softmax is the activation function; h CNN×att It is the weighted CNN feature representation.
[0070] The bidirectional gated recurrent unit layer inputs the pooled features into the bidirectional GRU layer to capture context information, which includes the forward GRU and the backward GRU. The attention score of each word is calculated and applied to the output of the BiGRU.
[0071]
[0072]
[0073]
[0074]
[0075] in It represents the feature representation of the i-th word after passing through the BiGRU layer; and Respectively represent the output of the forward GRU and the output of the backward GRU; W a BiGRU is the weight matrix in the self-attention mechanism, is the bias term in the self-attention mechanism; h BiGRU×att It is the weighted BiGRU feature representation.
[0076] The bidirectional long short-term memory network layer inputs the input features into the bidirectional LSTM layer to further capture long-distance dependencies. The bidirectional LSTM includes forward LSTM and backward LSTM. It is combined with the self-attention mechanism to apply the attention score to the output of BiLSTM.
[0077]
[0078]
[0079]
[0080]
[0081] in It represents the feature representation of the i-th word after passing through the BiLSTM layer; and Represent the output of the forward LSTM and the output of the backward LSTM respectively; is the weight matrix in the self-attention mechanism, is the bias term in the self-attention mechanism; h BiLSTM×att It is the weighted BiLSTM feature representation.
[0082] Finally, the weighted feature representations obtained from each parallel model are fused and connected together.
[0083] h fused =[h CNN×att ,h BiGRU×att ,h BiLSTM×att ]
[0084] Step 2042 detects vulnerability data in the target data set through the MLP classifier and evaluates the detection performance of the model.
[0085] z1=ReLU(W1h fused +b1)
[0086] z2=ReLU(W2z1+b2)
[0087]
[0088] Among them, W1, W2, W3 are the weight matrices of the MLP classifier, b1, b2, b3 are the bias items of the MLP classifier. z1 represents the output of the first layer, z2 represents the output of the second layer, It is the output layer of the MLP classifier and gives the final prediction result.
[0089] The present invention mainly detects smart contract vulnerabilities and uses the smart contract public data set SmartBugs to test the effect. Figure 3 The distribution of the imbalanced dataset used in the experiment is shown. Figure 4 The distribution of training and test sets after data balancing is shown.
[0090] In order to verify the effectiveness of feature fusion in the smart contract vulnerability detection method based on feature fusion and deep learning technology proposed in this invention, experiments were conducted using Word2Vec, FastText, GloVe, a tandem model combining the three, and the method proposed in this invention. Multiple experiments were conducted on each combination, and the average result was calculated as the final result. The final result is shown in the figure. Figure 5 shown.
[0091] In order to verify that the smart contract vulnerability detection method based on feature fusion and deep learning technology proposed in the present invention uses a method of integrating multiple deep learning models to achieve better results in smart contract vulnerability detection, experiments are conducted using CNN, BiGRU, BiLSTM, CNN+BiGRU, CNN+BiLSTM, BiGRU+BiLSTM and the method of the present invention. The accuracy, precision, recall and F1 score of the experiments are shown in Figure 2. Figure 6 shown.
[0092] Figure 7 and Figure 8 The results show the detection performance of the smart contract vulnerability detection method based on feature fusion and deep learning technology proposed in this paper, and the experimental comparison with DeeSCVHunter, Peaculiar, BLSTM-ATT and TMP methods is carried out on the experimental data set used by this paper. Figure 8 It can be seen that DeeSCVHunter and TMP target reentrancy vulnerabilities and timestamp dependency vulnerabilities; Peaculiar and BLSTM-ATT can only detect reentrancy vulnerabilities. The “\” indicates that the tool cannot detect the corresponding vulnerability type. It is observed that the detection accuracy, precision, recall rate and F1 score of the present invention after the experiment are higher than those of other models. And in Figure 7 It can be seen that the method proposed in the present invention performs best in terms of stability, has the smallest quarter spacing, and has no outliers. The experimental results not only prove that the smart contract vulnerability detection method based on feature fusion and deep learning technology proposed in the present invention is very effective in detecting smart contract vulnerabilities, but also prove that the method proposed in the present invention is superior to many current advanced methods.
Claims
1. A method for detecting smart contract vulnerability models based on feature fusion and deep learning technology, characterized in that: The steps include: Step 1: Use cross-validation to reasonably divide the data set, and perform normalization and oversampling preprocessing on the sample set used for the test to obtain a more balanced vulnerability data set; Step 2: construct an integrated network model through the word embedding model, and use the model to extract the features of the smart contract vulnerability code; Step 3: Based on the features obtained in step 2, channel self-attention technology is used to achieve feature fusion to obtain preliminary training sample sets and test sample sets; Step 4: Based on the features obtained in steps 2 and 3, use convolutional neural network, bidirectional gated recurrent unit and bidirectional long short-term memory network model training to obtain the final training sample set and test sample set, use the MLP classification model to classify the data for vulnerabilities, and evaluate the detection performance of the model based on the prediction results.
2. A method according to claim 1, characterized in that The specific implementation of step 1 includes the following steps: Step 1.1, use the AtExtractCode file to read the file content from the given file path, identify and extract the functions and constructors in the file, store the content of each function or constructor in a list, and finally return a list containing all functions and constructors; Step 1.2: Normalize the variable and function names defined by the developer in the code and delete the comment statements; Step 1.3, oversampling the training data using the oversampling method will alleviate the problem of vulnerability data imbalance and improve the training accuracy of the data set; set the sampling ratio according to the sample imbalance ratio to determine the sampling rate, and for each minority class sample, use the K nearest neighbor algorithm to find its nearest neighbor sample in the feature space, and generate a new sample by randomly interpolating between the sample and its nearest neighbor sample. The interpolation process uses a random coefficient α to control the position of the new sample so that it is between the original sample and the nearest neighbor sample.
3. The method according to claim 1, characterized in that: The specific implementation of step 2 includes the following steps: Step 2.1, store the preprocessed training data sample set in the form of a list, build an integrated network model of the word embedding model, and use word2vec, fasttext and glove (word vector embedding technology) for word vector embedding, and set the length of the vector to 100 to keep the obtained word vector data consistent.
4. A method according to claim 1, characterized in that The specific implementation of step 3 includes the following steps: Step 3.1, build an integrated network model. First, load multiple pre-trained word embedding models (word2vec, fasttext, and glove), and ensure that all embedded model vector dimensions are the same; Step 3.2, assign a weight to each word embedding model through the channel attention mechanism. This mechanism includes average pooling and maximum pooling operations, as well as convolutional layers and activation functions, and finally uses the Sigmoid function to calculate the attention weight; Step 3.3, stack the three embedding vectors of each word together, calculate the weighted combination through the channel attention mechanism, and obtain the final integrated word embedding representation, and obtain the final feature vector X[e1,e2,...,e n ].
5. A method according to claim 1, characterized in that: The specific implementation of step 4 includes the following steps: Step 4.1, build an integrated deep learning model architecture by using convolutional neural network, bidirectional gated recurrent unit and bidirectional long short-term memory network models to learn and extract features, and convert the final word embedding representation into prediction results; In step 4.2, vulnerability data of the target dataset is detected through the MLP classifier to evaluate the detection performance of the model.
Citation Information
Cited By
Intelligent contract vulnerability detection tool evaluation method and system based on neural network
CN119577788A
