Intelligent contract vulnerability detection method and system based on multiple modes
Through the multimodal data feature extraction and fusion of smart contracts, the deep learning model is used to solve the problem of low detection accuracy of smart contract vulnerability, achieving higher detection accuracy and stability, and ensuring the security of the blockchain network.
Patent Information
- Application Number
- CN202410927303.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-10
- Publication Date
- 2024-11-12
AI Technical Summary
The existing smart contract vulnerability detection methods have low accuracy and cannot fully analyze smart contracts. Deep learning-based methods are difficult to meet the needs in terms of detection speed and accuracy, especially when facing emerging vulnerabilities.
Using multimodal smart contract vulnerability detection method, feature extraction and fusion is performed through three modal data of smart contract source code, opcode and abstract syntax tree, and using deep learning models to improve detection accuracy, including preprocessing, feature extraction and Feature fusion module, using BiGRU and attention mechanism to extract semantic features and are classified by random inactivation layer, linear layer, batch normalization layer, and Softmax layer.
It significantly improves the accuracy of smart contract vulnerabilities detection, can effectively detect vulnerabilities in smart contracts, ensure that the legitimate assets of Ethereum users are not infringed, and maintains the stability of the blockchain network.
Smart Images

Figure CN118940272A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a multimodal smart contract vulnerability detection method and system. Background Art
[0002] The concept of smart contracts was first proposed by Nick Szabo in 1997. The role of smart contracts is to achieve decentralized trusted transactions and automated execution. It can ensure that the rights and interests of all parties to the contract are protected while avoiding the intervention of intermediaries, improve the transparency and efficiency of transactions, and reduce transaction costs and risks. The role of blockchain is to link data in the form of blocks in chronological order, and use cryptographic methods to ensure the security and credibility of data, realize decentralized data exchange and storage, and can be used to record and verify various types of transactions, information and assets. It has the characteristics of security, transparency, traceability, and efficiency. Due to the above advantages of blockchain, the implementation of smart contracts is feasible. Researchers can widely apply smart contracts to medical, financial, political and other fields by developing smart contract applications. Mohammed E CM et al. implemented a medical and health application based on blockchain and smart contract technology. The application effectively balanced the three requirements of privacy, security and data efficiency. Yang L et al. proposed a new data ownership confirmation scheme through smart contract technology, which realized the protection of ownership in the data market.
[0003] Ethereum is an open source blockchain smart contract platform. On the Ethereum platform, users can write smart contracts according to their needs to implement operations such as transfers and transactions. However, due to the uneven technical level of smart contract writers, low-level writers will inevitably make programming errors and generate code vulnerabilities during the writing process. In addition, smart contracts containing vulnerabilities are visible to all Ethereum users, which will lead to the proliferation of vulnerability exploitation. Since smart contracts cannot be modified once they are released, it is very important to ensure that smart contracts have no vulnerabilities before they are deployed.
[0004] So far, many researchers have proposed a variety of tools for smart contract vulnerability detection based on existing code vulnerability detection technology, which can be roughly divided into four categories: taint analysis, fuzz testing, symbolic execution, and formal verification. With the discovery of more and more smart contract vulnerability types and the increasing number of smart contracts, these traditional smart contract vulnerability detection tools are no longer able to meet user needs due to their slow detection speed and inability to detect new types of vulnerabilities. In contrast, the vulnerability detection method based on deep learning extracts vulnerability features from a large number of smart contracts with vulnerabilities. After the model is trained, it can detect the latest vulnerabilities, and the detection speed is fast. It has become a mainstream smart contract vulnerability detection method.
[0005] However, the deep learning-based vulnerability detection methods that have been proposed so far only extract features from one mode of the smart contract, such as the control flow chart, source code, or opcode, which makes it impossible to conduct a comprehensive analysis of the smart contract. In addition, most of the current deep learning vulnerability detection methods are based on the self-attention model (transformer) and its improved model. Although the Transformer and Long Short-Term Memory Network (LSTM) models can increase the range of feature dependencies that can be captured to about 200 characters, they still cannot obtain global dependency information when facing very long texts such as smart contracts. In addition, the LSTM-based smart contract vulnerability detection tool needs to set the maximum length of the input data to prevent the gradient from disappearing during model training, which will cause the long smart contract to lose feature information when it is intercepted. Summary of the invention
[0006] In order to solve the problem of low vulnerability detection accuracy in smart contract vulnerability detection methods, the present invention proposes a multi-modal smart contract vulnerability detection method and system. The deep learning model fully utilizes the data of three modes, namely, smart contract source code, operation code, and abstract syntax tree, to improve the accuracy of the smart contract vulnerability detection method and ensure that the legitimate assets of Ethereum smart contract users are not infringed, thereby maintaining the stability of the Ethereum blockchain network.
[0007] In order to achieve the above object, the technical solution of the present invention is:
[0008] The first aspect of the present invention proposes a multimodal smart contract vulnerability detection method, comprising the following steps:
[0009] Step 1: Obtain the source code of the smart contract to be tested for subsequent training;
[0010] Step 2: Compile the source code to obtain the operation code and abstract syntax tree of the smart contract to be detected, so as to improve the accuracy of smart contract vulnerability detection by using the feature information of the three modes of source code, operation code and abstract syntax tree of the smart contract;
[0011] Step 3: Obtain the word vectors of the three modal data, namely the source code, operation code and abstract syntax tree of the smart contract to be tested, so as to facilitate input into the preset smart contract vulnerability detection model for training;
[0012] Step 4: Input the word vectors of the three modal data into the preset smart contract vulnerability detection model to obtain the vulnerability status of the smart contract to be detected.
[0013] Furthermore, before step three, the method further includes:
[0014] The three modal data of the source code, operation code and abstract syntax tree of the smart contract to be detected are preprocessed respectively, and the preprocessing includes:
[0015] Delete the content not related to the vulnerability in the three modal data, and standardize the custom content in the three modal data to facilitate subsequent training.
[0016] Furthermore, the deletion of the contents irrelevant to the vulnerability in the three modal data specifically includes:
[0017] Delete version annotations, comments, import statements, line breaks, brackets, and consecutive spaces in the source code to reduce the length and noise of the source code;
[0018] Delete the compilation error data in the opcode, the keywords that do not affect the execution result, and the operands after "PUSH" to reduce the length of the opcode and facilitate subsequent use;
[0019] Delete the smart contract version number, the unique identifier of the node, the position symbol of the node in the source file, brackets and colons in the abstract syntax tree to reduce the length of the abstract syntax tree.
[0020] Furthermore, the custom content in the three modal data is standardized, including:
[0021] Use the same representation for statements with the same meaning in the source code, and replace user-defined variable names with a unified format to reduce the workload;
[0022] The keywords with numbers at the end of the opcode are processed into characters of the same format to reduce the workload and avoid training errors caused by format problems.
[0023] Modify the user-defined variable names in the abstract syntax tree to a unified format to reduce the workload.
[0024] Furthermore, the preset smart contract vulnerability detection model includes a feature extraction module and a feature fusion module; wherein the feature extraction module includes a source code feature extraction branch, an operation code feature extraction branch and an abstract syntax tree feature extraction branch;
[0025] The source code feature extraction branch includes a BiGRU module and a word-level attention mechanism module connected in sequence, so as to fully extract context dependency information;
[0026] The source code word vector is input into the BiGRU module for semantic feature extraction to obtain a semantic feature vector of the source code, and then the semantic feature vector is input into the attention mechanism module to generate and weight attention weights for each word element in the semantic feature vector, thereby obtaining a weighted semantic feature vector;
[0027] The opcode feature extraction branch and the abstract syntax tree feature extraction branch both include two BiGRU modules, a first attention mechanism module at the word level, and a second attention mechanism module at the sentence level, so as to fully extract context dependency information;
[0028] Taking words as units, the word vectors of the opcode and the abstract syntax tree are respectively input into the first BiGRU modules corresponding to each other for semantic feature extraction to obtain the respective sentence semantic feature vectors, and then the respective sentence semantic feature vectors are input into the first attention mechanism modules corresponding to each other to generate attention weights for each word element in the sentence semantic feature vector and weight them, thereby obtaining the respective weighted sentence semantic feature vectors;
[0029] Taking sentences as units, all weighted sentence semantic feature vectors of the opcodes and abstract trees are input into the second BiGRU module for semantic feature extraction to obtain their respective global semantic feature vectors, and then each global semantic feature vector is input into its corresponding second attention mechanism module to generate and weight attention weights for each sentence element in the global semantic feature vector, thereby obtaining their respective weighted global semantic feature vectors.
[0030] Furthermore, the feature fusion module includes a random dropout layer, a linear layer, a feature fusion layer, a batch normalization layer, and a Softmax layer, which facilitates the use of three modal features of smart contract source code, operation code, and abstract syntax tree to improve the accuracy of smart contract vulnerability detection;
[0031] The operation code output by the feature extraction module, the weighted global semantic feature vector of the abstract syntax tree, and the weighted sentence semantic feature vector of the source code are input into the random inactivation layer to randomly discard the output of a part of the neurons. The remaining data are input into the linear layer to make the data dimension the same. The data of the same dimension are input into the feature fusion layer for fusion to obtain the fused features. The fused features are input into the batch normalization layer for regularization. The regularized data is input into the Softmax layer for classification to obtain the vulnerability detection results.
[0032] The second aspect of the present invention proposes a multimodal smart contract vulnerability detection system, comprising:
[0033] Data acquisition module, used to obtain the source code of the smart contract to be tested;
[0034] A compiling module, used to compile the source code to obtain the operation code and abstract syntax tree of the smart contract to be tested;
[0035] The word vector module is used to obtain the word vectors of the three modal data of the source code, operation code and abstract syntax tree of the smart contract to be tested;
[0036] The detection module is used to input the word vectors of the three modal data into the preset smart contract vulnerability detection model to obtain the vulnerability status of the smart contract to be detected.
[0037] The third aspect of the present invention proposes a terminal device, comprising a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, wherein when the processor executes the computer program, it implements a vulnerability detection method based on multi-feature learning described in the first aspect.
[0038] The fourth aspect of the present invention proposes a computer-readable storage medium, which includes a stored computer program, wherein when the computer program is running, the device where the storage medium is located is controlled to execute the vulnerability detection method based on multi-feature learning described in the first aspect above.
[0039] Through the above technical solution, the beneficial effects of the present invention are:
[0040] (1) The present invention makes full use of data in three modalities: smart contract source code, operation code, and abstract syntax tree, extracts feature information related to smart contract vulnerabilities from a broader potential semantics, and effectively utilizes the potential relationship between the three modal data, thereby improving the accuracy of current smart contract vulnerability detection.
[0041] (2) According to the comparison of experimental results, compared with several baseline methods including a smart contract vulnerability detection method based on a hybrid model (CBGRU), a robust serial hybrid model for smart contract vulnerability detection (SPCBIG-EC), and a model based on a triple loss function and a long short-term memory network (BiLSTM_Attn_TripletLoss), the present invention achieves higher accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] Figure 1 It is a flowchart of a multi-modal smart contract vulnerability detection method provided by an embodiment of the present invention.
[0043] Figure 2 It is a structural diagram of a smart contract vulnerability detection model provided by an embodiment of the present invention.
[0044] Figure 3 It is a schematic diagram of accuracy values of various models under a test environment provided by an embodiment of the present invention.
[0045] Figure 4 It is a schematic diagram of the detection effect of various models on various types of vulnerabilities in a test environment provided by an embodiment of the present invention.
[0046] Figure 5 This is an architecture diagram of a multimodal smart contract vulnerability detection system provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0047] The present invention will be further described below in conjunction with the accompanying drawings and specific embodiments:
[0048] Example 1
[0049] like Figure 1 As shown, a multimodal smart contract vulnerability detection method includes the following steps:
[0050] S101: Obtain the source code of the smart contract to be tested.
[0051] S102: Compile the source code to obtain the operation code and abstract syntax tree of the smart contract to be tested.
[0052] S103: Obtain word vectors of three modal data, namely, source code, operation code and abstract syntax tree of the smart contract to be detected.
[0053] S104: Input the word vectors of the three modal data into a preset smart contract vulnerability detection model to obtain the vulnerability status of the smart contract to be detected.
[0054] The embodiment of the present invention provides a multi-modal smart contract vulnerability detection method, which obtains the source code of the smart contract to be detected, compiles the source code of the smart contract into opcodes and abstract syntax trees, and pre-processes the source code, opcodes, and abstract syntax trees. Then, the word vector model fast text classifier is used to convert the data of the three modes into word vector form. Finally, the potential information in these three modes is used to train the model to obtain a deep learning hybrid model that can be used for vulnerability detection. The accuracy of the smart contract vulnerability detection method based on deep learning is improved, the legitimate assets of Ethereum smart contract users are guaranteed not to be infringed, and the stability of the Ethereum blockchain network is dimensioned.
[0055] Example 2
[0056] Based on the above embodiment, the embodiment of the present invention provides a specific training process of a smart contract vulnerability detection model, and the specific process is as follows:
[0057] S201: Construct a training data set and divide the entire data set into a training set and a test set.
[0058] Specifically, we download the public dataset smartbugs-wild for smart contract vulnerability detection from the website as a training dataset. This dataset includes smart contract source code and vulnerability classification labels, and the dataset has used 9 smart contract vulnerability detection tools to detect smart contracts. According to the experimental results, smart contract vulnerabilities are divided into four categories, namely denial of service vulnerabilities (denial_of_service), reentrancy vulnerabilities (reentrancy), arithmetic vulnerabilities (arithmetic), and timestamp dependency vulnerabilities (timestamp_dependency).
[0059] In order to reduce the noise interference in the data set, in practical applications, the smartbugs-wild data set needs to be cleaned, the smart contract compilation tool py-solc is used to compile the smart contract, the smart contracts that cannot be compiled and report errors are discarded, and the smart contract vulnerability data set for training and testing is obtained by filtering according to the vulnerability classification labels.
[0060] S202: Obtain multimodal data of the smart contract.
[0061] Specifically, the opcode and abstract syntax tree of the smart contract are obtained by compiling the source code. In order to further reduce noise interference and improve the efficiency of vulnerability detection, it is necessary to preprocess the data of the three modes of source code, opcode and abstract syntax tree. For source code data: delete version annotations, comments, import statements and other statements that are not related to the vulnerability, delete line breaks, brackets, continuous spaces and other symbols that are not related to the vulnerability, so as to reduce the length and noise of the source code. The same meaning statements such as "string constant public name" and "string public constant name" are expressed in the same way. On this basis, the user-defined variable names are replaced with a unified format, for example, the names defined as "contract" are changed to "CONTRACT_i", thereby reducing the scale of work.
[0062] For opcode data: First, you need to delete opcodes that have compile-time errors, such as "(Unknown Opcode)". Secondly, by performing semantic analysis on the opcodes, the keywords with numbers at the end of the opcodes, such as "PUSHi", are processed into the same format "PUSH". Next, delete the keyword "LOG" that has no effect on the execution result. In addition, the three keywords "DUP", "SWAP", and "POP" are used to operate on the stack data in the Ethereum virtual machine and will not affect the execution result, so they also need to be deleted. Finally, since the number starting with "0x" after "PUSH" represents the operand, the operand has little to do with smart contract vulnerability detection, so delete the operand after "PUSH".
[0063] For the abstract syntax tree, first modify all user-defined variable names to "USER_DEFINED", and after semantic analysis, delete the smart contract version number "['solidity', '^', '0.4', '.24']", the node's unique identifier "id", and the node's location in the source file "src" that are not related to the vulnerability, and then delete brackets, colons and other symbols to reduce the length of the abstract syntax tree. After preprocessing, the data set is divided into a training set and a test set according to the ratio.
[0064] S203: The preprocessed three modal data are trained using a fast text classifier (fasttext) word vector model to obtain word vector representations of the three modalities.
[0065] Specifically, taking the source code as an example, assuming an abstract syntax tree file in It represents each sentence divided according to the position of the hierarchical symbols added in the smart contract file, and L represents the number of sentences. It consists of T words. It can be expressed as j∈[1,T], where express The jth word in the sentence. Similarly, the opcode file D o The j-th sentence in is represented as The jth word in the sentence is represented as
[0066] The word embedding layer will The words in are replaced by the word vectors obtained during word embedding training, and we get v ij a It is represented as the word vector of the jth word in the i-th sentence. Similarly, there are also sentences in the opcode
[0067] In contrast, the source code is relatively short and does not require stratification for feature extraction, so it is finally processed as Where f represents the length of the source code sentence.
[0068] S204: Build a smart contract vulnerability detection model and train it using the training set data.
[0069] Specifically, the smart contract vulnerability detection model in the embodiment of the present invention mainly includes a feature extraction module and a feature fusion module. The feature extraction module includes a source code feature extraction branch, an operation code feature extraction branch, and an abstract syntax tree feature extraction branch, which are used for feature extraction of three modal data, such as Figure 2 As shown in the figure, for the abstract syntax tree and operation code, since the length of these two data structures is relatively long, the context dependency information cannot be fully extracted when the long short-term memory network (LSTM) module is used for feature extraction.
[0070] Therefore, for these two types of data, both the opcode feature extraction branch and the abstract syntax tree feature extraction branch use the heterogeneous graph attention network (HAN). HAN is divided into four modules, including two BiGRU modules, the first attention mechanism module at the word level, and the second attention mechanism module at the sentence level.
[0071] Taking words as units, the word vectors of the opcode and the abstract syntax tree are respectively input into the first BiGRU modules corresponding to each other for semantic feature extraction to obtain the respective sentence semantic feature vectors, and then the respective sentence semantic feature vectors are input into the first attention mechanism modules corresponding to each other to generate attention weights for each word element in the sentence semantic feature vector and weight them, thereby obtaining the respective weighted sentence semantic feature vectors. The specific process is as follows:
[0072] Word encoding: This process uses a layer of bidirectional gated recurrent unit (GRU) module to extract semantic features. The calculation process of GRU can be expressed by formula 1-4:
[0073] For the information x input at time t t , the reset gate determines how much information from the previous time t-1 needs to be retained, r t The calculation formula is expressed as:
[0074] r t =σ(W r x t +U r h t-1 +b r ) (1)
[0075] Among them, r t is the reset gate, σ is the activation function, W r is the weight matrix, x t is the information input at time t, U r is the weight matrix, h t-1 is the information at time t-1, b r For deviation.
[0076] The update gate is used to determine how much memory information from the previous time step to retain, and can learn to ignore useless memory. The update gate is calculated according to the following formula:
[0077] z t =σ(W z x t +U z h t-1 +b z ) (2)
[0078] Among them, z t is the update gate, σ is the activation function, W z is the weight matrix, x t is the information input at time t, U z is the weight matrix, h t-1 is the input information of the t-1 time step, b z For deviation.
[0079] The calculation formula for the candidate hidden state is shown in Formula 3.
[0080] h t =tanh(W h x t +(U z h t-1 )*r t +b h ) (3)
[0081] Among them, h t is the candidate hidden state, W h is the weight matrix, x t is the information input at time t, U z is the weight matrix, h t-1 is the input information of the t-1 time step, b h is the deviation, r t To reset the gate.
[0082] Finally, the hidden state output of GRU at time t is expressed as:
[0083] h t =(1-z t )*W z x t +z t *h t (4)
[0084] Among them, h t is the hidden state at time t, z t is the update gate, W z is the weight matrix, x t is the information input at time t, h t is a candidate hidden state.
[0085] Therefore, for the abstract syntax tree sentence sequence The forward and backward calculations of a bidirectional GRU can be expressed as:
[0086]
[0087] in, is the hidden state calculated forward, is the hidden state calculated forward at time t-1, is the word vector of the jth word in the i-th sentence, is the hidden state calculated backwards, is the hidden state calculated backward at time t+1, t∈[1,T], after which the two vectors are concatenated together to obtain the hidden state vector:
[0088]
[0089] in, is the hidden state vector, is the hidden state vector calculated forward, is the hidden state calculated backward.
[0090] After the sentence sequence passes through the GRU layer, it finally becomes is the hidden vector set, is the Tth vector in the hidden vector set.
[0091] Word attention: Based on the results of GRU output, the model introduces a word-level attention mechanism. By assigning weights to each word in the sentence, the model can focus on the words that are most relevant to vulnerability identification, thereby improving the accuracy of the model's vulnerability classification. Elements in Input it into a multi-layer perceptron (MLP) to improve the representation ability of the model and obtain the hidden output representation Then input the above results into the tanh activation function, and limit the output value to [-1, 1], thereby introducing nonlinear factors and increasing the expression and fitting ability of the model. This process is expressed by the formula:
[0092]
[0093] in, To hide the output, W w is the weight matrix, is the hidden state vector, b w For deviation.
[0094] Subsequently, through With word context vector The weight of each word is obtained by multiplication, and the softmax function is used to normalize it to get the final weight of each word.
[0095]
[0096] in, The final weight for each word is, To hide the output, is the word context vector.
[0097] Multiplying the weight of each word by the hidden state vector gives:
[0098]
[0099] in, is the weighted vector for each word, The final weight for each word is, is the hidden state vector.
[0100] sequence The final expression is:
[0101]
[0102] in, For the sequence, is the weighted vector for each word.
[0103] Taking sentences as units, all weighted sentence semantic feature vectors of the opcode and abstract tree are input into the second BiGRU module for semantic feature extraction to obtain their respective global semantic feature vectors, and then their respective global semantic feature vectors are input into their respective corresponding second attention mechanism modules to generate attention weights for each sentence element in the global semantic feature vector and weight them, thereby obtaining their respective weighted global semantic feature vectors. The specific process is as follows:
[0104] Sentence encoding: Next, we use bidirectional GRU to extract the features of the sentence at the sentence level. The forward and backward formulas are as follows:
[0105]
[0106] in, is the hidden state calculated forward, is the hidden state calculated forward at time t-1, For the sequence, is the hidden state calculated backwards, is the hidden state calculated backward at time t+1, i∈[1,L], after which the two vectors are concatenated together to obtain the hidden state vector:
[0107]
[0108] in, is the hidden state vector, is the hidden state vector calculated forward, is the hidden state calculated backward.
[0109] The abstract syntax tree sentence sequence eventually becomes is an abstract syntax tree sentence sequence, is the Tth vector in the abstract syntax tree sentence sequence.
[0110] Sentence Attention: Based on the results of the bidirectional GRU output, a sentence-level attention mechanism is introduced. a In The process is expressed by the following formula:
[0111]
[0112] in, To hide the output, W s is the weight matrix, is the hidden state vector, b s is the deviation, is the final weight of the sentence, is the sentence context vector, is the sentence weight vector, is the hidden state vector.
[0113] The vector of the abstract syntax tree is finally represented as:
[0114]
[0115] Among them, V a is the vector of the abstract syntax tree, Weight vector for the sentence.
[0116] Similarly, the vector of opcodes is finally represented as:
[0117]
[0118] Among them, V o is a vector of opcodes, is the opcode weight vector.
[0119] Although the HAN model has a good effect in feature extraction of long sentences, the feature extraction of each small sentence is independent of each other during stratification, and the feature extraction of each sentence cannot utilize the information of the previous sentence, which causes a certain degree of semantic damage. Therefore, for shorter smart contract source code, the source code feature extraction branch uses bidirectional GRU and attention mechanism for semantic analysis. The final vector representation is:
[0120]
[0121] Among them, V s is the vector of source code, Weight vector for source code.
[0122] After obtaining the vector representation of the three modal data, the vectors are feature fused. In the feature fusion module, a random dropout layer is first passed to randomly discard the output of a part of the neurons, thereby reducing overfitting. Next, the data is input into the linear layer, and the opcode vector, abstract syntax tree vector, and source code vector are represented using the same dimension. The three new vectors obtained are input into the feature fusion layer for feature fusion. Feature fusion can more comprehensively understand and express the input data and provide the model with a broader potential space representation. Next, the result after feature fusion is input into the batch normalization (BtachNormal) layer to regularize the result, and finally input into the normalized exponential function (Softmax) layer to obtain the classification result. The above process can be expressed by the following formulas 21-23:
[0123]
[0124] V n+1 =BatchNormal(V n ) (twenty two)
[0125] Output=Softmax(V n+1 ) (twenty three)
[0126] Among them, V n is the random dropout layer output data, V o is the vector of opcodes, V a is the vector of the abstract syntax tree, V s is the vector of source code, V n+1 is the data after regularization, It is the feature fusion process.
[0127] S205: Obtain the word vectors of the three modal data of the test set according to the above process, input the word vectors of the three modal data corresponding to the test set into the trained smart contract vulnerability detection model, and obtain the vulnerability status of the test set data.
[0128] Specifically, after training, the best deep learning hybrid model is loaded, and the performance of the deep learning hybrid model is evaluated using the test set to obtain the evaluation results. The Pytorch deep learning framework is used. The model is on a portable computer equipped with an Intel Core i7-12700H CPU and an NVIDIA RTX 3080ti Laptop GPU, with a memory capacity of 16G and an operating system of Windows 11. In the experiment, the adaptive moment estimation (Adam) optimizer was selected for the deep learning hybrid model, the learning rate was set to 0.001, the training rounds were set to 30, the data packing size was set to 64, the parameter of the Dropout layer was set to 0.5, and the loss function used the cross entropy loss function (CrossEntropyLoss) with each classification weight set.
[0129] According to the experimental results Figure 3 , Figure 4 As shown, according to the comparison of experimental results, compared with a smart contract vulnerability detection method based on a hybrid model (CBGRU), a robust serial hybrid model for smart contract vulnerability detection (SPCBIG-EC) and BiLSTM_Attn_TripletLoss, several baseline methods, the present invention achieves higher accuracy.
[0130] Example 3
[0131] Corresponding to the above method, such as Figure 5 As shown, an embodiment of the present invention provides a multi-modal smart contract vulnerability detection system, including:
[0132] The data acquisition module is used to obtain the source code of the smart contract to be tested.
[0133] The compiling module is used to compile the source code to obtain the operation code and abstract syntax tree of the smart contract to be tested.
[0134] The word vector module is used to obtain the word vectors of the three modal data of the source code, operation code and abstract syntax tree of the smart contract to be tested.
[0135] The detection module is used to input the word vectors of the three modal data into the preset smart contract vulnerability detection model to obtain the vulnerability status of the smart contract to be detected.
[0136] It should be noted that the multimodal smart contract vulnerability detection system provided in an embodiment of the present invention is to implement the above-mentioned multimodal smart contract vulnerability detection method. Its specific functions can be referred to the above-mentioned method embodiments, which will not be repeated here.
[0137] Example 4
[0138] Based on the above embodiments, an embodiment of the present invention further provides a terminal device, including a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, and the processor executes the computer program to implement a vulnerability detection method based on multi-feature learning in the above embodiments.
[0139] Preferably, the present invention also provides a computer-readable storage medium, the storage medium including a stored computer program, wherein when the computer program is running, the device where the storage medium is located is controlled to execute a vulnerability detection method based on multi-feature learning in the above-mentioned embodiment.
[0140] In summary, this invention makes full use of the data of three modes: smart contract source code, operation code, and abstract syntax tree, extracts feature information related to smart contract vulnerabilities from a broader potential semantics, and effectively utilizes the potential relationship between the three modes of data, thereby improving the accuracy of current smart contract vulnerability detection. Figure 3 , Figure 4 As shown in the figure, according to the comparison of experimental results, compared with several baseline methods including a smart contract vulnerability detection method based on a hybrid model (CBGRU), a robust serial hybrid model for smart contract vulnerability detection (SPCBIG-EC), and a model based on a triple loss function and a long short-term memory network (BiLSTM_Attn_TripletLoss), the multimodal hybrid model achieved higher accuracy.
Claims
1. A multi-modal smart contract vulnerability detection method, characterized in that: The following steps are involved: Step 1: Get the source code of the smart contract to be tested; Step 2: Compile the source code to obtain the operation code and abstract syntax tree of the smart contract to be tested; Step 3: Obtain the word vectors of the three modal data of the source code, operation code and abstract syntax tree of the smart contract to be tested respectively; Step 4: Input the word vectors of the three modal data into the preset smart contract vulnerability detection model to obtain the vulnerability status of the smart contract to be detected.
2. According to a multimodal smart contract vulnerability detection method according to claim 1, it is characterized in that: Before step 3, also include: The three modal data of the source code, operation code and abstract syntax tree of the smart contract to be detected are preprocessed respectively, and the preprocessing includes: The contents irrelevant to the vulnerability in the three modal data were deleted, and the custom contents in the three modal data were standardized.
3. According to the multimodal smart contract vulnerability detection method according to claim 2, it is characterized in that: The deletion of the contents irrelevant to the vulnerability in the three modal data specifically includes: Delete version annotations, comments, import statements, line breaks, brackets, and consecutive spaces in the source code; Delete the compilation error data in the opcode, the keywords that have no effect on the execution result, and the operands after "PUSH"; Remove the smart contract version number, the unique identifier of the node, the position symbol of the node in the source file, brackets and colons in the abstract syntax tree.
4. According to a multimodal smart contract vulnerability detection method according to claim 2, it is characterized in that: Standardize the custom content in the three modal data, including: Make the statements with the same meaning in the source code adopt the same expression, and replace the user-defined variable names with a unified format; Treat keywords with numbers at the end of the opcode as characters of the same format; Modify the user-defined variable names in the abstract syntax tree to a unified format.
5. According to a multimodal smart contract vulnerability detection method according to claim 1, it is characterized in that: The preset smart contract vulnerability detection model includes a feature extraction module and a feature fusion module; wherein the feature extraction module includes a source code feature extraction branch, an operation code feature extraction branch and an abstract syntax tree feature extraction branch; The source code feature extraction branch includes a BiGRU module and a word-level attention mechanism module connected in sequence; The source code word vector is input into the BiGRU module for semantic feature extraction to obtain a semantic feature vector of the source code, and then the semantic feature vector is input into the attention mechanism module to generate and weight attention weights for each word element in the semantic feature vector, thereby obtaining a weighted semantic feature vector; The operation code feature extraction branch and the abstract syntax tree feature extraction branch each include two BiGRU modules, a first attention mechanism module at the word level, and a second attention mechanism module at the sentence level; Taking words as units, the word vectors of the opcode and the abstract syntax tree are respectively input into the first BiGRU modules corresponding to each other for semantic feature extraction to obtain the respective sentence semantic feature vectors, and then the respective sentence semantic feature vectors are input into the first attention mechanism modules corresponding to each other to generate attention weights for each word element in the sentence semantic feature vector and weight them, thereby obtaining the respective weighted sentence semantic feature vectors; Taking sentences as units, all weighted sentence semantic feature vectors of the opcodes and abstract trees are input into the second BiGRU module for semantic feature extraction to obtain their respective global semantic feature vectors, and then each global semantic feature vector is input into its corresponding second attention mechanism module to generate and weight attention weights for each sentence element in the global semantic feature vector, thereby obtaining their respective weighted global semantic feature vectors.
6. According to a multimodal smart contract vulnerability detection method according to claim 5, it is characterized in that: The feature fusion module includes a random dropout layer, a linear layer, a feature fusion layer, a batch normalization layer and a Softmax layer; The operation code output by the feature extraction module, the weighted global semantic feature vector of the abstract syntax tree, and the weighted sentence semantic feature vector of the source code are input into the random inactivation layer to randomly discard the output of a part of the neurons. The remaining data are input into the linear layer to make the data dimension the same. The data of the same dimension are input into the feature fusion layer for fusion to obtain the fused features. The fused features are input into the batch normalization layer for regularization. The regularized data is input into the Softmax layer for classification to obtain the vulnerability detection results.
7. A multimodal smart contract vulnerability detection system, characterized in that: include: Data acquisition module, used to obtain the source code of the smart contract to be tested; A compiling module, used to compile the source code to obtain the operation code and abstract syntax tree of the smart contract to be tested; The word vector module is used to obtain the word vectors of the three modal data of the source code, operation code and abstract syntax tree of the smart contract to be tested; The detection module is used to input the word vectors of the three modal data into the preset smart contract vulnerability detection model to obtain the vulnerability status of the smart contract to be detected.
8. A terminal device, characterized in that: It comprises a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, wherein when the processor executes the computer program, a vulnerability detection method based on multi-feature learning as described in any one of claims 1 to 6 is implemented.
9. A computer-readable storage medium, characterized in that: The storage medium includes a stored computer program, wherein when the computer program is running, the device where the storage medium is located is controlled to execute a vulnerability detection method based on multi-feature learning as described in any one of claims 1 to 6.
Citation Information
Cited By
Intelligent contract vulnerability detection method and system based on multi-modal fusion and information entropy
CN119720209A
Intelligent contract vulnerability detection method and system based on multi-modal fusion and information entropy
CN119720209B
Vulnerability detection method and system for smart contract control flow based on multi-modal deep learning model
CN119862575A
Intelligent contract vulnerability detection method based on adaptive data partitioning and GPT-4o fusion preprocessing technology
CN119939599A
