A smart contract vulnerability detection method based on local and sequence feature fusion
Through a deep learning model based on the fusion of local and sequence features, the problems of high cost of vulnerability detection and high false negative rate of smart contracts are solved, and fast and accurate vulnerability detection is achieved, which is suitable for smart contracts in different programming languages.
Patent Information
- Application Number
- CN202210710381.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-22
- Publication Date
- 2025-02-14
- Estimated Expiration
- 2042-06-22
AI Technical Summary
The existing smart contract vulnerability detection methods require exploring all execution paths of the contract, which leads to high time costs and is prone to false negative rates in complex contracts. Most smart contracts are not open source and are difficult to rely on source code information for rapid detection.
Using a smart contract vulnerability detection method based on local and sequence features, by collecting and filtering smart contract bytecodes, building a deep learning model, using Word2Vec model to extract word vectors of opcodes, combining convolutional layer, BiGRU and attention mechanism, Extract local and sequence features of smart contracts to realize vulnerability detection.
It realizes the rapid and accurate detection of smart contract vulnerabilities without relying on the smart contract source code. It is suitable for smart contracts in different programming languages, with a wider detection range and higher performance.
Smart Images

Figure CN115080981B_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 based on the fusion of local and sequence features. Background Art
[0002] Blockchain has attracted great attention since its inception due to its advantages such as decentralization, immutability, openness and transparency. Blockchain solves the problem of value transfer in a non-trust environment through technologies such as distributed ledgers, consensus mechanisms, asymmetric encryption, and hash algorithms. Ethereum is an open source public chain platform. Its uniqueness lies in the introduction of the smart contract mechanism, which achieves Turing completeness. A smart contract is a protocol program that runs on the blockchain platform and can be triggered to execute according to certain conditions. Most smart contracts are written in Solidity language. Since the language is still developing rapidly, various contract coding specifications are not well followed, and most contracts lack careful design, a large part of the existing smart contracts have loopholes, resulting in great security risks. Attackers may attack these vulnerable contracts for some purpose. For example, some smart contracts manage a large amount of funds, and attacking such contracts may illegally obtain huge economic benefits. Therefore, various attacks on smart contracts emerge in an endless stream, causing huge losses.
[0003] Moreover, because blockchain is tamper-proof, smart contracts cannot be changed once deployed, and even if vulnerabilities are found after deployment, they cannot be patched in time. Traditional detection tools are mainly inspired by program vulnerability detection methods, which require exploring all execution paths of the contract, so they require a high execution time cost, and when encountering more complex contracts, they need to limit the exploration depth to prevent path explosion, resulting in a high false negative rate. The current machine learning-based methods rely heavily on smart contract source code information. Since most smart contracts on blockchain platforms such as Ethereum are not open source and are still growing explosively, it is of great significance for smart contract platforms such as Ethereum to quickly detect a large number of contracts without relying on source code information. Summary of the invention
[0004] The technical problem to be solved by the present invention is to provide a smart contract vulnerability detection method based on the fusion of local and sequence features to achieve the detection of smart contract vulnerabilities in view of the shortcomings of the above-mentioned prior art.
[0005] In order to solve the above technical problems, the technical solution adopted by the present invention is: a smart contract vulnerability detection method based on the fusion of local and sequence features, comprising the following steps:
[0006] Step 1: Collect M smart contract bytecodes from the blockchain platform, and filter out the smart contract bytecodes that do not meet the length, time and call frequency requirements to form a smart contract dataset;
[0007] Step 2: Label the smart contracts in the smart contract dataset and obtain the corresponding operation code number sequence of the entire smart contract dataset through preprocessing;
[0008] Step 2.1: Label the smart contract dataset; use a variety of bytecode-oriented smart contract vulnerability detection tools to label the smart contract dataset with vulnerability labels at the same time, remove the smart contracts with different labeling results, and save the remaining smart contracts in the form of triples consisting of contract address, vulnerability label and contract bytecode to form a labeled smart contract dataset;
[0009] Step 2.2: Convert the smart contract bytecode into opcode using a disassembly tool and remove the operands to form an opcode sequence;
[0010] Step 2.3: Count the average, median, and mode of the opcode lengths in the labeled smart contract data set as the basis for truncating or padding each smart contract opcode to equal length, and process the opcodes to equal length; count the positive and negative class ratios of smart contracts, and use a balancing algorithm to balance the positive and negative class ratios when the positive and negative class ratios exceed the set ratio;
[0011] Step 2.4: Number all the opcodes in the entire annotated smart contract data set using decimal numbers according to their frequency of occurrence to form a word frequency table, and replace the opcode sequence in step 2.2 with the corresponding numbers in the word frequency table to obtain the opcode number sequence;
[0012] Step 3: Build a smart contract vulnerability detection model based on deep learning, and train the model using the labeled opcode number sequence obtained in step 2;
[0013] Step 3.1: Construct an embedding layer; use the Word2Vec model to pre-train the opcodes and obtain the word vectors corresponding to the opcodes; the input of the Word2Vec model is the opcode sequence of the entire labeled smart contract data set, and the output is a key-value pair consisting of the opcode and its corresponding word vector; arrange the word vectors corresponding to the opcodes in the order of numbers in step 2.4 to form an embedding matrix E as the embedding layer; the input of the embedding layer is the opcode number sequence processed in step 2.4, and the opcode number is converted into the corresponding word vector sequence using the embedding matrix E as the input of the next convolutional layer;
[0014] Step 3.2: Construct a convolutional layer to extract local features of the smart contract opcode; use m convolutional layers with a width of d and a height of h i, i∈1,2,…,n convolution kernel performs convolution operation on the operation code, d is the word vector dimension corresponding to the operation code, n is the number of convolution kernels of different sizes used, the local features of the operation code are combined and screened to obtain the feature vector V j , j∈1,2,…,s, s=m*n, as the input of the next maximum pooling layer;
[0015] Step 3.3: Each feature vector V is pooled through a block-wise maximum pooling layer. j Divide into k blocks, keep only the largest eigenvalue in each block, then concatenate each largest local eigenvalue in order, and combine all eigenvectors by column to form a feature matrix M with s columns and k rows, which is used as the input of the lower bidirectional gating unit;
[0016] Step 3.4: Input the feature matrix M of the previous layer into the Bidirectional Gate Recurrent Unit (BiGRU) in row order, and extract the contract sequence features through the loop structure in the Bidirectional Gate Recurrent Unit. The bidirectional structure extracts the complete past and future context information of the smart contract at the current moment; the Bidirectional Gate Recurrent Unit inputs an s-dimensional vector at each time step, for a total of k time steps, and the output vector is used as the input of the next attention layer;
[0017] Step 3.5: The attention layer weights the output vector of the bidirectional gating unit through the self-attention mechanism to obtain the attention vector;
[0018] Step 3.6: Input the attention vector into the fully connected network for classification to obtain the vulnerability classification result;
[0019] Step 4: Use the smart contract vulnerability detection model trained in step 3 to perform vulnerability classification detection on the smart contracts to be tested on the blockchain platform.
[0020] The beneficial effects of adopting the above technical solution are: the present invention provides a smart contract vulnerability detection method based on the fusion of local and sequence features, (1) a smart contract vulnerability detection model is built, which is bytecode-oriented and can quickly and accurately detect smart contract vulnerabilities without relying on any information in the smart contract source code. This is not only not restricted by whether the smart contract is open source, but also can shield the differences in upper-level languages, so that the detection model is not only applicable to Solidity language, but also to smart contracts written in other high-level languages such as Java and go, and the detection range is wider. (2) The local features of the smart contract are enhanced by the convolutional network, and then the sequence features of the smart contract are extracted by the BiGRU model with a bidirectional loop structure, and the attention mechanism is added to enhance the keyword weight to jointly complete the feature extraction. The smart contract vulnerability detection model combines local features and sequence features, and can learn more effective features, so the vulnerability detection performance is higher, and it can detect smart contract vulnerabilities more efficiently and extensively. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] Figure 1 A flowchart of a smart contract vulnerability detection method based on the fusion of local and sequence features provided by an embodiment of the present invention;
[0022] Figure 2 A schematic diagram of a smart contract vulnerability detection model provided by an embodiment of the present invention;
[0023] Figure 3 A schematic diagram of smart contract vulnerability detection model parameters provided by an embodiment of the present invention;
[0024] Figure 4 A schematic diagram of the process of converting an opcode into a word vector provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0025] The specific implementation of the present invention is further described in detail below in conjunction with the accompanying drawings and examples. The following examples are used to illustrate the present invention, but are not intended to limit the scope of the present invention.
[0026] In this embodiment, a smart contract vulnerability detection method based on the fusion of local and sequence features is provided. Figure 1 As shown, the following steps are included:
[0027] Step 1: By analyzing the display format of smart contracts on the blockchain platform, write an automated crawler script to collect M smart contract bytecodes from the blockchain platform, and filter out the smart contract bytecodes that do not meet the length, time and call frequency requirements to form a smart contract data set;
[0028] The bytecode is a sequence of hexadecimal digits compiled from the smart contract source code. Each bytecode represents an opcode (EVM instruction) or operand, which is the form in which the smart contract is stored on the blockchain after deployment.
[0029] This embodiment takes the blockchain platform Ethereum as an example, visits the Ethereum browser (cn.etherscan.com), analyzes the website source code and writes a crawler script in Python, uses Xpath (XMLPathLanguage) to locate the smart contract bytecode information, and downloads and collects 60,000 copies of the smart contract bytecode. The creation time of the contract can be known through the timestamp of the smart contract creation block, and the Ethereum browser can query all transactions that call the contract through the contract address. Through this information, the data set is removed with extremely short bytecodes (the number of bytecodes is less than 50), extremely long creation time (timestamp before 1400 days) and extremely low call frequency (the total number of calls is less than 2 times).
[0030] Step 2: Label the smart contracts in the smart contract dataset, and obtain all the opcode number sequences corresponding to the entire smart contract dataset through preprocessing such as disassembly, removal of operands, and calculation of word frequency;
[0031] Step 2.1: Label the smart contract dataset; use a variety of bytecode-oriented smart contract vulnerability detection tools to label the smart contract dataset with vulnerability labels at the same time, remove the smart contracts with different labeling results, and save the remaining smart contracts in the form of triples consisting of contract address, vulnerability label and contract bytecode to form a labeled smart contract dataset;
[0032] This example uses Oyente, Securify, and Mythril to annotate the smart contract data set. At the same time, to simplify the example, this example does not distinguish between vulnerability types and annotates them as 1 (with vulnerability) and 0 (without vulnerability). If the annotation results of the three tools are different, the smart contract is removed from the data set, and the remaining smart contracts are saved in the form of triples (contract address, vulnerability label, contract bytecode) to form an annotated data set. In this example, the annotated data set includes 19,366 smart contracts.
[0033] Step 2.2: Convert the smart contract bytecode into opcodes through a disassembly tool, and remove the operands to form an opcode sequence; the opcodes are some instructions for operating the EVM (Ethereum Virtual Machine). The Ethereum Yellow Paper contains the correspondence between opcodes and EVM operations, such as '60' represents the push instruction 'PUSH1';
[0034] In this embodiment, the Ethereum official disassembly tool "cn.etherscan.com / opcode-tool" is used to convert the bytecode sequence into an opcode sequence and remove the operands. The conversion can also be performed according to the correspondence between bytecodes and opcodes in the Ethereum Yellow Paper.
[0035] Step 2.3: Count the average, median, and mode of the opcode lengths in the labeled smart contract data set as the basis for truncating or padding each smart contract opcode to equal length, and process the opcodes to equal length; Count the positive and negative class ratios of smart contracts. When the positive and negative class ratio (positive class is a contract with vulnerabilities, negative class is a contract without vulnerabilities) exceeds the set ratio (1:5), use a balancing algorithm to balance the positive and negative class ratios;
[0036] In this example, the average length of the opcodes in the labeled smart contract data set is 3680, the median is 1434, and the mode is 1088. According to the bytecode length information, each smart contract is equal to 1600 bytecodes, and "0" is added if it is shorter, and truncated if it is longer. The positive and negative class ratios of the smart contracts are counted to determine whether a balancing algorithm needs to be used for balancing. In this example, the positive and negative classes are 11839 and 7527 respectively, and the positive and negative data are similar, so there is no need to use a balancing algorithm for balancing.
[0037] Step 2.4: Number all the operation codes in the entire annotated smart contract data set using decimal numbers according to their frequency of occurrence to form a word frequency table; for example, the operation code "PUSH1" is numbered 1.
[0038] In this embodiment, the operation codes in the entire annotated smart contract data set are numbered according to the frequency of occurrence. Each operation code is represented by a decimal number and numbered from 1 to form a word frequency table. The word with the highest frequency is numbered 1, the second highest is numbered 2, and so on. In this example, "PUSH1" (byte code is 60) is numbered 1, "ADD" (byte code is 01) is numbered 2, "POP" (byte code is 50) is numbered 3, and so on.
[0039] Step 3: Construct a smart contract vulnerability detection model based on deep learning, and use the labeled operation code number sequence obtained in step 2 to train the model; in this embodiment, the constructed smart contract vulnerability detection model based on deep learning is as follows: Figure 2 As shown, the model parameters are Figure 3 shown.
[0040] Step 3.1: Construct an embedding layer; use the Word2Vec model to pre-train the opcodes and obtain the word vectors corresponding to the opcodes; this model can implement word clustering from a large amount of corpus in an unsupervised manner, with the goal of representing each opcode as a word vector of fixed dimension d. The word vector contains semantic knowledge, and the word vectors corresponding to opcodes with similar semantics are also similar; the input of the Word2Vec model is the opcode sequence of the entire labeled smart contract data set, and the output is a key-value pair consisting of the opcode and its corresponding word vector; the word vectors corresponding to the opcodes are arranged in the order of numbers in step 2.4 to form an embedding matrix E as the embedding layer; the input of the embedding layer is the opcode number sequence processed in step 2.4, and the opcode number is converted into the corresponding word vector sequence output using the embedding matrix E as the input of the next convolutional layer;
[0041] In this embodiment, each opcode is represented as a 150-dimensional word vector. The reason for selecting 150 is that there are about 150 opcodes defined in the Ethereum Yellow Paper. Then, the embedding matrix E is constructed by sorting the rows according to the word frequency table numbers, that is, the first row of E is the word vector corresponding to "PUSH1", the second row of E is the word vector corresponding to "ADD", and the size of E is 257*150 (the 0th row is the all-0 vector, corresponding to the symbol "0" filled in step 2.3). Through the embedding matrix E, the opcode number sequence in each smart contract is converted into a word vector sequence as the input of the next layer. The process of converting the opcode number to a word vector is as follows: Figure 4 As shown (wherein, a onehot vector is a unit vector with only one dimension being 1 and the rest being all 0, and the position of 1 is the number of each opcode).
[0042] Step 3.2: Construct a convolutional layer to extract local features of the smart contract opcode; use m convolutional layers with a width of d and a height of h i , i∈1,2,…,n convolution kernel performs convolution operation on the operation code, d is the word vector dimension corresponding to the operation code, n is the number of convolution kernels of different sizes used, the local features of the operation code are combined and screened to obtain the feature vector V j , j∈1,2,…,s, s=m*n, as the input of the next maximum pooling layer;
[0043] In this embodiment, the convolutional layer constructed is a convolutional layer structure similar to textCNN, using 128 convolution kernels with a width of 150 (word vector dimension) and a height of [10, 11, 12] to combine and filter the local features of the operation code to obtain the feature vector V j , j∈1,2,…,384, as the input of the next layer.
[0044] Step 3.3: Each feature vector V is pooled through a block-wise maximum pooling layer. jDivide into k blocks, keep only the largest eigenvalue in each block, then concatenate each largest local eigenvalue in order (i.e. keep the original order), and combine all eigenvectors by column to form a feature matrix M with s columns and k rows, which is used as the input of the lower-level bidirectional gating unit to preserve the sequence information of the smart contract in a coarse-grained manner;
[0045] In this embodiment, each feature vector V is pooled by a block maximum pooling layer. j Divide it into 24 blocks, retain only the largest eigenvalue in each block, then concatenate each maximum local eigenvalue in order, and combine all eigenvectors by column to form a feature matrix M with 384 columns and 24 rows as the input of the lower layer.
[0046] Step 3.4: Input the feature matrix M of the previous layer into the bidirectional gate recurrent unit (BiGRU) in row order, extract the contract sequence features through the loop structure in the bidirectional gate unit, and the bidirectional structure extracts the complete past and future context information of the smart contract at the current moment; the bidirectional gate unit inputs an s-dimensional vector (i.e., a row of the feature matrix M) at each time step, for a total of k time steps, and the output vector is used as the input of the next attention layer;
[0047] In this embodiment, the 24*384 feature matrix M of the upper layer is input into the bidirectional gate recurrent unit (BiGRU) in row order. The unit sets 128 hidden units in one direction. The contract sequence features are extracted through the loop structure in the BiGRU, and the bidirectional structure extracts the complete past and future context information of the contract at the current moment. A 384-dimensional vector (a row of the feature matrix M) is input into the bidirectional gate unit at each time step, for a total of 24 time steps, and the output vector is used as the input of the lower layer.
[0048] Step 3.5: The attention layer weights the output vector of the bidirectional gating unit through the self-attention mechanism to obtain the attention vector;
[0049] In this embodiment, the output vector of BiGRU is weighted by the self-attention mechanism, and more important hidden states are given greater weights. For example, the weight of some operation codes corresponding to the call.value() function involving transfers is greater than the operation code corresponding to the constructor initialization, and the output result is the Attention vector.
[0050] Step 3.6: Input the attention vector into the fully connected network for classification to obtain the vulnerability classification result;
[0051] In this embodiment, the Attention vector is input into a two-layer fully connected network for classification. The number of fully connected network units is 64 and 1 respectively. The vulnerability classification result is obtained according to the last unit value (1: vulnerability, 0: no vulnerability, which can also be converted into multiple labels to distinguish the vulnerability type).
[0052] Step 4: Extract the bytecode of the smart contract to be tested on the blockchain platform, disassemble the bytecode into opcodes and remove the operands, embed the opcode sequence into a word vector matrix, and then use the smart contract vulnerability detection model trained in step 3 to perform vulnerability classification detection on the smart contract.
[0053] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some or all of the technical features therein. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope defined by the claims of the present invention.
Claims
1. A smart contract vulnerability detection method based on the fusion of local and sequence features, characterized by: Collect smart contract bytecodes to form a smart contract dataset; Label the smart contract data in the smart contract dataset and obtain the operation code number sequence through preprocessing; Build a smart contract vulnerability detection model based on deep learning; Use opcode number sequences to train smart contract vulnerability detection models; Use the trained smart contract vulnerability detection model to perform vulnerability classification detection on the smart contract to be tested; The following steps are involved: Step 1: Collect M smart contract bytecodes from the blockchain platform, and filter out the smart contract bytecodes that do not meet the length, time and call frequency requirements to form a smart contract dataset; Step 2: Label the smart contracts in the smart contract dataset and obtain the operation code number sequence corresponding to the entire smart contract dataset through preprocessing; Step 3: Build a smart contract vulnerability detection model based on deep learning, and train the model using the labeled opcode number sequence obtained in step 2; The deep learning-based smart contract vulnerability detection model includes an embedding layer, a convolutional layer, a block maximum pooling layer, a bidirectional gating unit, an attention layer, and a fully connected network connected in sequence; Step 4: Use the smart contract vulnerability detection model trained in step 3 to perform vulnerability classification detection on the smart contracts to be tested on the blockchain platform; The specific method of step 2 is: Step 2.1: Label the smart contract dataset to form a labeled smart contract dataset; Step 2.2: Convert the smart contract bytecode into opcode using a disassembly tool and remove the operands to form an opcode sequence; Step 2.3: Count the average, median, and mode of the opcode lengths in the labeled smart contract data set as the basis for truncating or padding each smart contract opcode to equal length, and process the opcodes to equal length; count the positive and negative class ratios of smart contracts, and use a balancing algorithm to balance the positive and negative class ratios when the positive and negative class ratios exceed the set ratio; Step 2.4: Number all the opcodes in the entire annotated smart contract data set using decimal numbers according to their frequency of occurrence to form a word frequency table, and replace the opcode sequence in step 2.2 with the corresponding numbers in the word frequency table to obtain the opcode number sequence.
2. According to claim 1, a smart contract vulnerability detection method based on local and sequence feature fusion is characterized by: The specific method of step 2.1 is: A variety of bytecode-oriented smart contract vulnerability detection tools are used to simultaneously annotate vulnerability labels on the smart contract dataset, and smart contracts with different annotation results are removed. The remaining smart contracts are saved in the form of triplets consisting of contract address, vulnerability label and contract bytecode to form a labeled smart contract dataset.
3. According to claim 2, a smart contract vulnerability detection method based on local and sequence feature fusion is characterized in that: The construction method of the embedded layer in the smart contract vulnerability detection model is: The Word2Vec model is used to pre-train the opcodes to obtain the word vectors corresponding to the opcodes. The input of the Word2Vec model is the opcode sequence of the entire labeled smart contract data set, and the output is a key-value pair consisting of the opcode and its corresponding word vector. The word vectors corresponding to the opcodes are arranged in numerical order to form an embedding matrix E as the embedding layer. The embedding layer input is the opcode number sequence, and the embedding matrix E is used to convert the opcode number into the corresponding word vector sequence output as the input of the next convolutional layer.
4. According to claim 3, a smart contract vulnerability detection method based on local and sequence feature fusion is characterized in that: The smart contract vulnerability detection model constructs a convolutional layer to extract local features of the smart contract operation code. The specific method is as follows: Use m width d and height h i , i∈1,2,…,n convolution kernel performs convolution operation on the operation code, d is the word vector dimension corresponding to the operation code, n is the number of convolution kernels of different sizes used, the local features of the operation code are combined and screened to obtain the feature vector V j , j∈1,2,…,s, s=m*n, as the input of the next maximum pooling layer.
5. According to claim 4, a smart contract vulnerability detection method based on local and sequence feature fusion is characterized in that: The smart contract vulnerability detection model transforms each feature vector V into j Divide it into k blocks, retain only the largest eigenvalue in each block, then concatenate each maximum local eigenvalue in order, and combine all eigenvectors by column to form a feature matrix M with s columns and k rows, which serves as the input of the lower-layer bidirectional gating unit.
6. According to claim 5, a smart contract vulnerability detection method based on local and sequence feature fusion is characterized in that: The smart contract vulnerability detection model inputs the feature matrix M obtained by the block maximum pooling layer into the bidirectional gating unit in row order, extracts the contract sequence features through the loop structure in the bidirectional gating unit, and the bidirectional structure extracts the complete past and future context information of the smart contract at the current moment; each time step of the bidirectional gating unit inputs an s-dimensional vector, a total of k time steps, and the output vector is used as the input of the next attention layer.
7. The method for detecting smart contract vulnerabilities based on the fusion of local and sequence features according to claim 6, characterized in that: The attention layer of the smart contract vulnerability detection model weights the output vector of the bidirectional gating unit through a self-attention mechanism to obtain an attention vector.
8. The method for detecting smart contract vulnerabilities based on the fusion of local and sequence features according to claim 7, characterized in that: The smart contract vulnerability detection model inputs the attention vector obtained by the attention layer into the fully connected network for classification to obtain a vulnerability classification result.
Citation Information
Patent Citations
Intelligent contract vulnerability detection method based on multi-task learning
CN114218580A