Intelligent contract vulnerability detection method and system based on fusion features and enhanced network
By combining DistilBERT, CNN and multi-head self-attention BiLSTM, the problems of imperfect feature extraction and high resource consumption in smart contract vulnerability detection are solved, and more efficient and accurate vulnerability detection is achieved.
Patent Information
- Application Number
- CN202411211846.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-30
- Publication Date
- 2024-12-06
AI Technical Summary
The existing smart contract vulnerability detection methods have problems such as high computing resource requirements, long time consumption and imperfect feature extraction, especially the pre-trained model and deep learning methods are insufficient in capturing context and long-distance dependencies.
The lightweight pre-trained model DistilBERT is used to capture the context features of smart contracts, combine with the convolutional neural network CNN to capture local features, and form multi-dimensional features through feature fusion. At the same time, a multi-head self-attention mechanism is introduced in BiLSTM to deeply explore multi-dimensional features potential long-distance dependencies.
It effectively reduces the cost and time of model training, improves the accuracy and robustness of vulnerability detection, enhances the ability to identify complex smart contract vulnerability patterns, and provides more efficient smart contract vulnerability detection capabilities.
Smart Images

Figure CN119089451A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of blockchain security technology, and in particular to a smart contract vulnerability detection method and system based on fusion features and enhanced networks. Background Art
[0002] A smart contract is a digital contract in the form of computer code. It uses computer code to execute and manage pre-set agreement terms and can automatically perform specified operations when the set conditions are met. Through smart contracts, people can realize the automatic execution and verification of transactions without the intervention of a third party, which greatly improves the efficiency and credibility of transactions. Although smart contracts play a vital role in blockchain transactions, like other applications, smart contracts also have security vulnerabilities and are easy to become the target of cyber attacks. Since smart contracts have the characteristics of being unchangeable after being deployed on the blockchain, even if vulnerabilities are found, it is difficult for developers to repair them online, which may cause huge economic losses. Especially for smart contracts that control a large number of cryptocurrencies and financial assets, once they become the target of attacks, they may trigger a chain reaction and cause immeasurable losses. Therefore, it is particularly important to detect smart contract vulnerabilities before deploying smart contracts.
[0003] Smart contract vulnerabilities refer to potential security risks during the design, development or deployment of smart contracts, which may lead to serious economic losses. Common smart contract vulnerabilities include reentrancy attacks, timestamp dependency vulnerabilities, denial of service vulnerabilities, integer underflow and overflow vulnerabilities, etc. There are currently a variety of smart contract vulnerability detection solutions, among which the smart contract vulnerability detection method based on artificial intelligence algorithms is more commonly used, including detection methods based on deep learning and pre-trained models, but these methods still have certain problems:
[0004] (1) Smart contract vulnerability detection methods based on pre-trained models automatically identify and analyze the code structure of smart contracts, extract vulnerability features from them, and use supervised learning and other models to detect vulnerabilities. However, they have defects such as high computing resource requirements and long time consumption. For example, the BERT pre-trained language model is used to perform semantic processing on smart contract codes. After training with a large amount of text data, it can capture complex language features. However, the BERT model has high computing resource requirements, long training and inference time, high memory and storage requirements, and requires a lot of resources for updating and maintenance.
[0005] (2) Compared with the detection method based on pre-trained models, the intelligent vulnerability detection method based on deep learning consumes less resources and is more widely used. It can automatically identify and analyze the code structure of smart contracts, extract vulnerability features from smart contracts, and use supervised learning or semi-supervised learning models to detect vulnerabilities, but it often ignores certain aspects of the features. For example, the deep learning method based on the fusion of word2vec and GCN (Graph Convolutional Networks) features, although the fusion of Word2Vec and GCN features can combine semantic similarity and graph structure information, it ignores context information and ignores context dependencies when generating feature vectors. For another example, when using the Bidirectional Long Short-Term Memory (Bidirectional Long Short-Term Memory) network and attention mechanism to process source code sequences and extract vulnerability features, due to the particularity of smart contract vulnerabilities, some smart contract vulnerabilities may rely on the relationship between long-distance code fragments. When processing long sequences, although the introduction of the attention mechanism in the BiLSTM network can capture more context information than the simple BiLSTM, it will ignore some dependencies when capturing long-distance dependencies, and the feature extraction is imperfect, and there are still certain limitations. Summary of the invention
[0006] In order to solve the deficiencies of the above-mentioned prior art, the present invention provides a smart contract vulnerability detection method and system based on fusion features and enhanced networks, which uses a lightweight pre-trained model DistilBERT to capture the contextual features of the smart contract, and uses a convolutional neural network CNN to capture the local features of the smart contract. Multi-dimensional features are formed through feature fusion, so as to better capture the contract characteristics and reduce the cost and time of model training; a multi-head self-attention mechanism is introduced into BiLSTM to capture the relationship between multiple positions in the sequence at the same time, enhance the ability to pay attention to different subspaces, deeply explore the potential long-distance dependencies and hidden states of multi-dimensional features, and improve the feature expression ability; through the above two feature extraction optimization methods, vulnerability features can be extracted more comprehensively, effectively improving the accuracy of the final vulnerability detection.
[0007] In a first aspect, the present invention provides a smart contract vulnerability detection method based on fusion features and enhanced networks.
[0008] A smart contract vulnerability detection method based on fusion features and enhanced network, including:
[0009] Obtain the source code data of the Ethereum smart contract to be tested;
[0010] Preprocess the acquired source code data to generate a word sequence;
[0011] Input the word sequence into the trained smart contract vulnerability detection model and output the smart contract vulnerability detection results;
[0012] Among them, the word sequence is input into the DistilBERT model and CNN network in parallel, and the context feature vector and local feature vector of the same dimension of the word sequence are extracted respectively, and a multi-dimensional fused feature vector is generated after fusion; the multi-dimensional fused feature vector is input into the enhanced BiLSTM network based on the multi-head self-attention mechanism, and the multi-head self-attention feature vector of the input feature is first extracted, and then the multi-head self-attention feature vector is subjected to forward and reverse hidden state learning to extract the hidden state feature vector; the hidden state feature vector is input into the fully connected layer, and the vulnerability detection result is output.
[0013] In a second aspect, the present invention provides a smart contract vulnerability detection system based on fusion features and enhanced networks.
[0014] A smart contract vulnerability detection system based on fusion features and enhanced network, including:
[0015] A data acquisition module is used to obtain source code data of the Ethereum smart contract to be tested;
[0016] A data preprocessing module, used for preprocessing the acquired source code data to generate a word sequence;
[0017] A vulnerability detection module is used to input a word sequence into a trained smart contract vulnerability detection model and output a smart contract vulnerability detection result; the vulnerability detection module includes:
[0018] The multi-dimensional feature fusion module is used to input the word sequence into the DistilBERT model and the CNN network in parallel, extract the context feature vector and local feature vector of the same dimension of the word sequence respectively, and generate a multi-dimensional fused feature vector after fusion;
[0019] The sequence learning module is used to input the multi-dimensional fusion feature vector into the enhanced BiLSTM network based on the multi-head self-attention mechanism, first extract the multi-head self-attention feature vector of the input feature, then perform forward and reverse hidden state learning on the multi-head self-attention feature vector to extract the hidden state feature vector;
[0020] The output module is used to input the hidden state feature vector into the fully connected layer and output the vulnerability detection results.
[0021] In a third aspect, the present invention further provides an electronic device comprising a memory and a processor, and computer instructions stored in the memory and executed on the processor, wherein when the computer instructions are executed by the processor, the steps of the method described in the first aspect are completed.
[0022] In a fourth aspect, the present invention further provides a computer-readable storage medium for storing computer instructions, which, when executed by a processor, complete the steps of the method described in the first aspect.
[0023] One or more of the above technical solutions have the following beneficial effects:
[0024] 1. The present invention provides a method and system for detecting smart contract vulnerabilities based on fusion features and enhanced networks. First, the lightweight pre-trained model DistilBERT is used to capture the contextual features of the smart contract, which contains the contextual relationship and semantic information of the smart contract; at the same time, the convolutional neural network CNN is used to capture the local features of the smart contract, which contains the dependency relationship and local information between adjacent elements of the smart contract; and then multi-dimensional fusion features are formed through feature fusion to better capture and understand the overall meaning of the smart contract code, and accurately and comprehensively express the characteristics of the smart contract. This method can effectively enhance the model's ability to recognize complex smart contract vulnerability patterns and improve the accuracy and robustness of detection by providing richer and more diverse feature representations. DistilBERT reduces the model size by about 40% and increases the inference speed by about 60%, which can effectively reduce the cost and time of model training and has more efficient smart contract vulnerability detection capabilities.
[0025] 2. The present invention introduces a multi-head self-attention mechanism into the BiLSTM network in sequence learning, and constructs an enhanced BiLSTM training architecture based on the multi-head self-attention mechanism. The introduction of the multi-head self-attention mechanism before BiLSTM can capture long-distance dependencies in parallel on a global scale, minimize information loss, and enhance the model's ability to capture global information; in addition, BiLSTM uses bidirectional information flow to process sequence data, which can improve the model's understanding of sequence context. The enhanced BiLSTM training architecture not only solves the shortcomings of the traditional attention mechanism and BiLSTM combination in capturing global information and processing long-distance dependencies, but also greatly enhances the model's modeling ability for complex dependencies and contextual information, and improves the comprehensiveness and accuracy of smart contract vulnerability detection. Through the above-mentioned enhanced BiLSTM training architecture, the present invention deeply explores the potential long-distance dependencies and hidden states of multi-dimensional features, which can effectively improve the feature expression ability. Finally, based on the two proposed feature extraction optimization methods, vulnerability features can be extracted more comprehensively, effectively improving the accuracy of the final vulnerability detection. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] The accompanying drawings in the specification, which constitute a part of the present invention, are used to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute improper limitations on the present invention.
[0027] Figure 1 This is an overall flow chart of the smart contract vulnerability detection method based on fusion features and enhanced network according to an embodiment of the present invention. DETAILED DESCRIPTION
[0028] It should be noted that the following detailed descriptions are exemplary only, are intended to describe specific embodiments, are intended to provide further explanation of the present invention, and are not intended to limit exemplary embodiments according to the present invention. Unless otherwise specified, all technical and scientific terms used herein have the same meanings as those of ordinary skill in the art to which the present invention belongs. In addition, it should also be understood that when the terms "comprising" and / or "including" are used in this specification, it indicates the presence of features, steps, operations, devices, components and / or combinations thereof.
[0029] Embodiment 1
[0030] This embodiment provides a smart contract vulnerability detection method based on fusion features and enhanced networks, which fuses the DistilBERT model with the feature vector extracted by CNN (Convolutional Neural Network) to further improve the model's context understanding and local feature extraction capabilities, and introduces a multi-head self-attention mechanism in BiLSTM for sequence learning to obtain the hidden state of each time step, based on which smart contract vulnerability detection with higher accuracy is achieved. Figure 1 As shown, the method proposed in this embodiment specifically includes the following steps:
[0031] Step S1, obtaining source code data of the Ethereum smart contract to be tested;
[0032] Step S2, preprocessing the acquired source code data to generate a word sequence;
[0033] Step S3: input the word sequence into the trained smart contract vulnerability detection model, and output the smart contract vulnerability detection result;
[0034] Among them, the word sequence is input into the DistilBERT model and CNN network in parallel, and the context feature vector and local feature vector of the same dimension of the word sequence are extracted respectively, and a multi-dimensional fused feature vector is generated after fusion; the multi-dimensional fused feature vector is input into the enhanced BiLSTM network based on the multi-head self-attention mechanism, and the multi-head self-attention feature vector of the input feature is first extracted, and then the multi-head self-attention feature vector is subjected to forward and reverse hidden state learning to extract the hidden state feature vector; the hidden state feature vector is input into the fully connected layer, and the vulnerability detection result is output.
[0035] The following content introduces the smart contract vulnerability detection method based on fusion features and enhanced network proposed in this embodiment in more detail.
[0036] In the above steps S1 and S2, the source code data of the Ethereum smart contract to be detected is obtained, and the obtained source code data is preprocessed to generate a word sequence.
[0037] The preprocessing of the source code data is as follows: using the Solidity compiler, the acquired source code data is parsed and compiled into an abstract syntax tree AST; the nodes of the abstract syntax tree AST are selected, which are various elements in the smart contract, including functions, variables, operators, etc., and these nodes are converted into tokens to generate a token sequence {Tok1, Tok2, Tok3, ..., Tokq}, and finally the token sequence is output as a word sequence.
[0038] In the above step S3, the word sequence is input into the trained smart contract vulnerability detection model to output the smart contract vulnerability detection result. The smart contract vulnerability detection model includes a parallel DistilBERT model and a CNN network, an enhanced BiLSTM network based on a multi-head self-attention mechanism, and a fully connected layer. In this embodiment, the model is used to output the vulnerability detection result, specifically including the following steps:
[0039] Step S3.1, input the word sequence into the DistilBERT model and CNN network in parallel, extract the context feature vector and local feature vector of the same dimension of the word sequence respectively, and generate a multi-dimensional fused feature vector after fusion.
[0040] Step S3.1.1, before inputting the word sequence into the DistilBERT model, first, add special annotations to the generated word sequence, that is, add special tokens to the token sequence. Among them, special tokens include annotations set at the end of the sequence for segmenting different input segments and annotations set at the beginning of the sequence for indicating the element type information of the entire input sequence (the type includes functions, variables, operators, etc.). In this embodiment, [CLS] is added to the beginning of the generated token sequence {Tok1, Tok2, Tok3, ..., Tokq} to indicate the element type information of the entire input sequence, and [SEP] is added at the end to segment different input segments.
[0041] Then, the tokenizer in the DistilBERT model is used to split long words or identifiers in the word sequence into smaller segments, ensuring that the model can handle a variety of complex identifiers and keywords.
[0042] Step S3.1.2: Input the split word sequence into the DistilBERT model, and input the word sequence / token sequence generated in step S2 into the CNN network in parallel, and obtain and output the processed feature vectors respectively.
[0043] (1) Extracting contextual features based on the DistilBERT model
[0044] First, load the pre-trained model. Specifically, use Hugging Face's Transformers library to load the pre-trained DistilBERT model.
[0045] Secondly, the generated word sequence / token sequence is input into the DistilBERT model to obtain the embedding vector of each word / token in the word sequence, forming an embedding vector sequence {L1, L2, ... Lq}; the embedding vector sequence is then input into the 6-layer Transformer layer for processing to obtain the processed context feature vector {L T 1,L T 2,...,L T q}; each Transformer layer includes a self-attention mechanism and a feedforward neural network to capture the dependencies and contextual information between different positions in the sequence.
[0046] (2) Extracting local features based on CNN network
[0047] In this embodiment, the CNN network includes a 1D convolution layer and a maximum pooling layer. First, the above-generated word sequence / token sequence {Tok1, Tok2, Tok3, ..., Tokq} is input into the 1D convolution layer of the CNN network, and the word sequence is convoluted to extract the feature map of the input sequence. The convolution layer uses 15 different filters, each with a filter size of 20 and a stride of 1. The activation function uses the hyperbolic tangent function tanh, which is defined as follows:
[0048]
[0049] For the input token sequence {Tok1, Tok2, Tok3, ..., Tokq}, the convolution kernel is used to slide on the input data, perform dot product operation, and use the tanh activation function to generate the activated feature map. The formula for the convolution operation is:
[0050] (I*K)(i,j)=∑ m ∑ n I(i+m,j+n)·K(m,n);
[0051] In the above formula, I is the input feature map, K is the convolution kernel, i and j are the position indexes of the output feature map, and m and n are the position indexes of the convolution kernel.
[0052] Secondly, the maximum pooling layer is used to perform the maximum pooling operation on the extracted feature map to generate a local feature vector. Specifically, the feature map output by the convolution operation is input to the maximum pooling layer, the pooling window is used to slide on the feature map, the maximum value in the window coverage area is selected, the maximum value of each position of the pooling window is stored, and the pooled feature map {E1, E2, E3, ..., Eq} is generated. The formula for the pooling operation is:
[0053] P(i,j)=max 0≤m<k,0≤n<k F(i·s+m,j·s+n);
[0054] In the above formula, s is the stride, the pooling window size is k*k, F is the input feature map, i and j are the position indexes of the output feature map, and m and n are the position indexes of the convolution kernel.
[0055] Step S3.1.3: Use a multi-dimensional smart contract feature fusion method to fuse the context feature vector and local feature vector of the same dimension of the extracted word sequence, and generate a multi-dimensional fused feature vector after fusion. That is, the context feature vector generated based on DistilBERT and the local feature vector extracted based on the CNN network are spliced in the feature dimension to obtain the fused feature.
[0056] Specifically, a multi-dimensional smart contract feature fusion method based on DistilBERT and CNN is used to transform the context feature vector {L T 1,L T 2,...,L T q} and the feature map vector {E1, E2, E3, ..., Eq} extracted by CNN are concatenated in the feature dimension. The concatenation formula is:
[0057] R i =[E i ; C i ], i = 1, 2, ..., q;
[0058] In this way, we can obtain the concatenated multi-dimensional fusion features {R1, R2, R3, ..., R q}.
[0059] Step S3.2, input the multi-dimensional fused feature vector into the enhanced BiLSTM network based on the multi-head self-attention mechanism, and use the multi-head self-attention mechanism and BiLSTM to perform sequence learning on the fused features, that is, first extract the multi-head self-attention feature vector of the input feature, then perform forward and reverse hidden state learning on the multi-head self-attention feature vector to obtain the hidden state of the fused feature, and extract the hidden state feature vector.
[0060] Specifically, the multi-dimensional fusion features {R1, R2, R3, ..., R q} is input into the enhanced BiLSTM training architecture based on the multi-head self-attention mechanism, which consists of a multi-head self-attention mechanism and BiLSTM. Due to the particularity of smart contract vulnerabilities, some vulnerabilities in Solidity smart contracts rely on the relationship between long-distance code snippets. For example, reentry attacks require understanding the complex calling relationships between different functions and contracts, which may span multiple code snippets; or delegate calls need to detect calling relationships across multiple contracts and functions. There are similar vulnerabilities such as timestamp dependency, integer overflow, and permission control. Therefore, in this embodiment, a multi-head self-attention mechanism is first used to capture these long-distance dependencies globally through multiple parallel self-attention heads, so as to minimize information loss and improve the accuracy of smart contract vulnerability detection. Then, the potential hidden state in the sequence is learned using a BiLSTM composed of two LSTM networks. One of the two LSTM networks is used to process the forward sequence (i.e., time from 1 to T), and the other is used to process the backward sequence (i.e., time from T to 1).
[0061] Firstly, the multi-dimensional fused feature vector is input into the multi-head self-attention mechanism. Through multiple parallel self-attention heads, the attention scores of the input features are calculated to capture the long-distance dependencies between the input features globally and extract the multi-head self-attention feature vector.
[0062] Specifically, the fused features are input into the multi-head self-attention mechanism to calculate the attention score. The formula is as follows:
[0063] MultiHead(Q,K,V)=Concat(head1,head2,head3,...head n )U;
[0064] Among them, U is a linear transformation matrix, which is used to concatenate the outputs of multiple attention heads and project them back to the original dimension of the model. The calculation formula for each attention head is as follows:
[0065] head i =Attention(QW i Q ,KWi K ,VW i V );
[0066] Among them, QW i Q ,KW i K ,VW i V is the weight matrix of each head. The calculation formula of Attention is as follows:
[0067]
[0068] Among them, z is the dimension of the fused feature; Q is the query vector, which is composed of the fused feature R i and the query vector weight matrix; K is the key vector, which is the fusion feature R i and the key vector weight matrix; V is the value vector, which is obtained by fusion feature R i The sum is obtained by multiplying the value vector weight matrix. After the multi-head self-attention mechanism, the multi-head self-attention feature sequence {A1, A2, A3, ... Aq} is obtained.
[0069] Secondly, the multi-head self-attention feature vector is input into the enhanced BiLSTM network, and forward and reverse hidden state learning is performed on the multi-head self-attention feature vector to extract the hidden state feature vector.
[0070] Among them, an LSTM network is used to process the forward sequence, including:
[0071] (1) Forget gate processing
[0072] The feature sequence obtained above is input into the forget gate of the forward LSTM network, and the hidden state h at the previous moment is t-1 and the current input x t After linear transformation, the sigmoid activation function is applied to obtain the output f of the forget gate t , the linear transformation formula is as follows:
[0073] f t =σ(W f ·[h t-1 ,x t ]+b f );
[0074] Among them, W f is the weight matrix of the forget gate, b f is the bias vector of the forget gate, σ is the sigmoid activation function, which compresses the result of the linear transformation to between 0 and 1. tIf an element of is close to 0, then the memory cell state C at the corresponding position t-1 The information in will be forgotten; if f t If an element of is close to 1, then the memory cell state C at the corresponding position t-1 The information in will be retained.
[0075] (2) Input gate processing
[0076] By the hidden state h of the previous moment t-1 and the current input x t After linear transformation, the sigmoid activation function is applied to obtain the activation value i of the input gate t , the linear transformation formula is as follows:
[0077] i t =σ(W i ·[h t-1 ,x t ]+b i );
[0078] Among them, W i is the weight matrix of the forget gate, b i is the bias vector of the forget gate.
[0079] Again, the hidden state h of the previous moment t-1 and the current input x t After linear transformation, the hyperbolic tangent function tanh activation function is applied to obtain the candidate memory unit state The linear transformation formula is as follows:
[0080]
[0081] Among them, W C is the weight matrix of the candidate memory unit, b C is the bias vector of the candidate memory unit, and tanh is the tanh activation function, which is used to compress the result of the linear transformation to between -1 and 1.
[0082] After calculating the activation value i t and candidate memory cell states After that, update the current memory unit state C t , and its calculation formula is as follows:
[0083]
[0084] Among them, f t is the output of the forget gate, indicating the proportion of the memory unit state at the previous moment of forgetting, C t-1 is the state of the memory unit at the previous moment, i tIndicates the proportion of new information that needs to be introduced at the current moment, It is new information at the present moment.
[0085] (3) Output gate processing
[0086] The hidden state h at the previous moment t-1 and the current input x t Concatenate to form a new vector combined = [h t-1 ,x t ], pass it to the output gate, perform linear transformation, and then apply the sigmoid activation function to get the activation value o of the output gate t , the linear transformation is as follows:
[0087] o t =σ(W o ·combined+b o );
[0088] Among them, W o is the weight matrix of the forget gate, b o is the bias vector of the forget gate.
[0089] Update the hidden state, passing the activation value o of the output gate t and the current memory cell state C t The tanh value is multiplied to get the hidden state h at the current moment tf , the formula is:
[0090] h tf =o t *tanh(C t ).
[0091] In addition, another LSTM network is used to process the previous and next sequences, including:
[0092] The hidden state h of the forward LSTM tf and the hidden state h of the reverse LSTM tb Concatenate and get the hidden state h of BiLSTM t In this way, the hidden state h at each time step t is t Contains contextual information from front to back and back to front.
[0093] Step S3.3, input the obtained hidden state feature vector to the fully connected layer, and output the vulnerability detection result. Specifically, the hidden state feature vector is input to the fully connected layer, and the hidden state feature vector of the last time step with a dimension of 2d is mapped to the number of categories N (in this embodiment, N=2, i.e., including two types of vulnerabilities and non-vulnerabilities) in the fully connected layer, and then passed to the Sigmoid activation function to obtain the probability of the input sequence belonging to the existence of vulnerabilities or the non-existence of vulnerabilities.
[0094] Specifically, the above data is input into the fully connected layer, and the vector with dimension 2d is mapped to the number of categories N using the following linear transformation. The formula is as follows:
[0095] y=W·h+b;
[0096] Where h is the hidden state vector of the last time step with dimension 2d, W is the weight matrix with dimension (N,2d), b is the bias vector with dimension N, and y is the classifier output with dimension N.
[0097] After that, the output of the fully connected layer is passed to the Sigmoid activation function to obtain the probability of the input sequence having a vulnerability or not having a vulnerability. The formula of the Sigmoid activation function is:
[0098]
[0099] Furthermore, in this embodiment, vulnerability detection is performed using a trained smart contract vulnerability detection model, and the training process of the smart contract vulnerability detection model includes:
[0100] First, we obtain the source code data of multiple Ethereum smart contracts with vulnerability labels and construct a dataset. Specifically, we select 10,000 real Ethereum smart contracts to construct the SCV dataset, which contains a series of smart contract codes and related vulnerability labels. We use 70% of the smart contracts in the dataset as the training set for model training, and the remaining 30% as the validation set to test the effectiveness of the model.
[0101] Secondly, each source code data in the dataset is preprocessed to generate a word sequence marked with the vulnerability type. Specifically, the Solidity compiler is used to compile the smart contract code into AST and extract nodes from it, and these nodes are converted into token sequences {Tok1, Tok2, Tok3, ..., Tokq}.
[0102] Finally, based on the word sequence marked with the vulnerability type, the word sequence is input into the constructed smart contract vulnerability detection model to train the smart contract vulnerability detection model until the loss function is minimized and the trained model is obtained.
[0103] In this embodiment, during the training process, a cross entropy loss function is used to measure the difference between the predicted result and the true label, and an SGD (stochastic gradient descent) optimizer is used to update the model parameters through back propagation to minimize the loss function. The performance on the validation set is monitored during the training process, and the training is stopped when the performance on the validation set no longer improves. The formula of the cross entropy loss function is:
[0104]
[0105] Where n represents the number of samples Y i represents the true label, represents the label predicted by the discriminator.
[0106] Embodiment 2
[0107] This embodiment provides a smart contract vulnerability detection system based on fusion features and enhanced network, including:
[0108] A data acquisition module is used to obtain source code data of the Ethereum smart contract to be tested;
[0109] A data preprocessing module, used for preprocessing the acquired source code data to generate a word sequence;
[0110] A vulnerability detection module is used to input a word sequence into a trained smart contract vulnerability detection model and output a smart contract vulnerability detection result; the vulnerability detection module includes:
[0111] The multi-dimensional feature fusion module is used to input the word sequence into the DistilBERT model and the CNN network in parallel, extract the context feature vector and local feature vector of the same dimension of the word sequence respectively, and generate a multi-dimensional fused feature vector after fusion;
[0112] The sequence learning module is used to input the multi-dimensional fusion feature vector into the enhanced BiLSTM network based on the multi-head self-attention mechanism, first extract the multi-head self-attention feature vector of the input feature, then perform forward and reverse hidden state learning on the multi-head self-attention feature vector to extract the hidden state feature vector;
[0113] The output module is used to input the hidden state feature vector into the fully connected layer and output the vulnerability detection results.
[0114] Embodiment 3
[0115] This embodiment provides an electronic device, including a memory and a processor, and computer instructions stored in the memory and executed on the processor. When the computer instructions are executed by the processor, the steps in the smart contract vulnerability detection method based on fusion features and enhanced network as described above are completed.
[0116] Embodiment 4
[0117] This embodiment also provides a computer-readable storage medium for storing computer instructions. When the computer instructions are executed by a processor, the steps in the smart contract vulnerability detection method based on fusion features and enhanced network as described above are completed.
[0118] The steps involved in the above embodiments 2 to 4 correspond to the method embodiment 1, and the specific implementation methods can refer to the relevant description part of embodiment 1. The term "computer-readable storage medium" should be understood as a single medium or multiple media including one or more instruction sets; it should also be understood to include any medium that can store, encode or carry an instruction set for execution by a processor and enable the processor to execute any method in the present invention.
[0119] Those skilled in the art should understand that the modules or steps of the present invention described above can be implemented by a general-purpose computer device, or alternatively, they can be implemented by a program code executable by a computing device, so that they can be stored in a storage device and executed by the computing device, or they can be made into individual integrated circuit modules, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. The present invention is not limited to any specific combination of hardware and software.
[0120] The above description is only a preferred embodiment of the present invention. Although the specific implementation mode of the present invention is described in conjunction with the accompanying drawings, it is not a limitation of the protection scope of the present invention. Those skilled in the art should understand that on the basis of the technical solution of the present invention, various modifications or variations that can be made by those skilled in the art without creative work are still within the protection scope of the present invention.
Claims
1. A smart contract vulnerability detection method based on fusion features and enhanced network, characterized in that: include: Obtain the source code data of the Ethereum smart contract to be tested; Preprocess the acquired source code data to generate a word sequence; Input the word sequence into the trained smart contract vulnerability detection model and output the smart contract vulnerability detection results; Among them, the word sequence is input into the DistilBERT model and CNN network in parallel, and the context feature vector and local feature vector of the same dimension of the word sequence are extracted respectively, and a multi-dimensional fused feature vector is generated after fusion; the multi-dimensional fused feature vector is input into the enhanced BiLSTM network based on the multi-head self-attention mechanism, and the multi-head self-attention feature vector of the input feature is first extracted, and then the multi-head self-attention feature vector is subjected to forward and reverse hidden state learning to extract the hidden state feature vector; the hidden state feature vector is input into the fully connected layer, and the vulnerability detection result is output.
2. A smart contract vulnerability detection method based on fusion features and enhanced network as claimed in claim 1, characterized in that: The pre-processing comprises: Using the Solidity compiler, the acquired source code data is parsed into an abstract syntax tree AST; Select nodes of the abstract syntax tree AST, convert and generate a token sequence, and output the token sequence as a word sequence; the nodes are various elements in the smart contract, including functions, variables, and operators.
3. A smart contract vulnerability detection method based on fusion features and enhanced network as claimed in claim 1, characterized in that: The smart contract vulnerability detection model includes a parallel DistilBERT model and CNN network, an enhanced BiLSTM network based on a multi-head self-attention mechanism, and a fully connected layer; The training process of the smart contract vulnerability detection model includes: Obtain source code data of multiple Ethereum smart contracts with vulnerability labels and construct a dataset; Preprocess each source code data in the dataset to generate a word sequence marked with vulnerability types; Based on the word sequence labeled with the vulnerability type, the smart contract vulnerability detection model is trained until the loss function is minimized to obtain a trained model; the loss function used is the cross entropy loss function.
4. A smart contract vulnerability detection method based on fusion features and enhanced network as claimed in claim 1, characterized in that: Before inputting the word sequence into the DistilBERT model, special annotations are first added to the word sequence, and then the word segmenter in the DistilBERT model is used to split the long words in the word sequence; Adding special annotations refers to adding special tokens to the token sequence, and the special tokens include annotations set at the end of the sequence for dividing different input segments and annotations set at the beginning of the sequence for indicating the element type information of the entire input sequence.
5. A smart contract vulnerability detection method based on fusion features and enhanced network as described in claim 4, characterized in that: Input the split word sequence into the DistilBERT model to obtain the embedding vector of each word in the word sequence to form an embedding vector sequence; The embedded vector sequence is then input into a 6-layer Transformer layer to obtain the processed context feature vector; each Transformer layer includes a self-attention mechanism and a feedforward neural network to capture the dependencies and context information between different positions in the sequence.
6. A smart contract vulnerability detection method based on fusion features and enhanced network as claimed in claim 1, characterized in that: The CNN network includes a 1D convolutional layer and a maximum pooling layer; The word sequence is input into the CNN network. First, the word sequence is convolved through a 1D convolutional layer to extract the feature map of the input sequence. Then, the extracted feature map is subjected to a maximum pooling operation through a maximum pooling layer to generate a local feature vector.
7. A smart contract vulnerability detection method based on fusion features and enhanced network as claimed in claim 1, characterized in that: The multi-dimensional fusion feature vector is input into the enhanced BiLSTM network based on the multi-head self-attention mechanism, including: The multi-dimensional fusion feature vector is input into the multi-head self-attention mechanism. Through multiple parallel self-attention heads, the attention score of the input feature is calculated to capture the long-distance dependency between input features globally and extract the multi-head self-attention feature vector. The multi-head self-attention feature vector is input into the enhanced BiLSTM network, and the multi-head self-attention feature vector is subjected to forward and reverse hidden state learning to extract the hidden state feature vector.
8. A smart contract vulnerability detection system based on fusion features and enhanced network, characterized in that: include: A data acquisition module is used to obtain source code data of the Ethereum smart contract to be tested; A data preprocessing module, used for preprocessing the acquired source code data to generate a word sequence; A vulnerability detection module is used to input a word sequence into the trained smart contract vulnerability detection model and output the smart contract vulnerability detection result; The vulnerability detection module includes: The multi-dimensional feature fusion module is used to input the word sequence into the DistilBERT model and the CNN network in parallel, extract the context feature vector and local feature vector of the same dimension of the word sequence respectively, and generate a multi-dimensional fused feature vector after fusion; The sequence learning module is used to input the multi-dimensional fusion feature vector into the enhanced BiLSTM network based on the multi-head self-attention mechanism, first extract the multi-head self-attention feature vector of the input feature, then perform forward and reverse hidden state learning on the multi-head self-attention feature vector to extract the hidden state feature vector; The output module is used to input the hidden state feature vector into the fully connected layer and output the vulnerability detection results.
9. An electronic device, characterized in that: The method comprises a memory and a processor, and computer instructions stored in the memory and executed on the processor. When the computer instructions are executed by the processor, the steps of a smart contract vulnerability detection method based on fusion features and enhanced network as described in any one of claims 1 to 7 are completed.
10. A computer-readable storage medium, characterized in that: Used to store computer instructions, which, when executed by a processor, complete the steps of a smart contract vulnerability detection method based on fusion features and enhanced networks as described in any one of claims 1-7.
