A smart contract vulnerability detection method and system at the source code level

By building an abstract syntax tree and using deep neural network models, the smart contract vulnerability detection method solves the problem of high missed and false alarm rates in the existing technology, and realizes efficient and accurate detection of multiple vulnerability types to ensure the security of smart contracts sex.

CN114297654BActive Publication Date: 2024-11-22BEIJING UNIV OF TECH
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202111669478.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-31
Publication Date
2024-11-22
Estimated Expiration
2041-12-31

AI Technical Summary

Technical Problem

The existing smart contract vulnerability detection methods ignore the syntax structure and semantic information of the code when processing source code, resulting in high rates of missed and false positives, and the inability to detect multiple vulnerability types at one time.

Method used

By building an abstract syntax tree, extracting lexical unit sequences and vectorizing them, combining with the deep neural network model of attention mechanism, it realizes automatic extraction of vulnerability features and performing binary classification detection of multiple vulnerability types.

Benefits of technology

It significantly improves the accuracy and efficiency of vulnerability detection, reduces the rates of missed and false alarms, and points out the specific forms and locations of vulnerabilities to ensure the safe operation of smart contracts.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114297654B_ABST
    Figure CN114297654B_ABST
Patent Text Reader

Abstract

The present invention discloses a method and system for detecting smart contract vulnerabilities at the source code level, the method comprising: obtaining smart contract sample data and preprocessing, detecting and analyzing whether there are vulnerabilities and the types of vulnerabilities, and marking with multiple labels as a sample set; generating an abstract syntax tree for the sample set, generating a lexical unit sequence text and vectorizing it, and forming a sample data set through packaging and filling; combining the attention mechanism to construct a vulnerability detection model based on a deep neural network; continuously training and optimizing the corresponding binary classifier for each vulnerability type, selecting the one with the best classification effect as a multi-label classifier; inputting the vectorized data of the smart contract source code to be detected into the multi-label classifier to obtain the vulnerability detection result. Through the technical solution of the present invention, the accuracy and efficiency of vulnerability detection are improved, and the underreporting rate and false alarm rate are reduced, so as to discover vulnerabilities as early as possible and take remedial measures in time.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of code vulnerability detection, and in particular to a smart contract vulnerability detection method at the source code level and a smart contract vulnerability detection system at the source code level. Background Art

[0002] As an emerging technology, blockchain has attracted widespread attention from experts and scholars at home and abroad. As an important part of blockchain technology, smart contracts have great potential to reshape conventional business processes. Their programmable characteristics have expanded the business scenarios of blockchain to multiple fields such as supply chain, medical care, insurance, and administrative law enforcement. Since the research and application of smart contracts are still in their early stages, developers often do not receive rigorous training and guidance, and there is also a lack of standard smart contract development language reference specifications. In the process of writing and deploying smart contracts, some loopholes will inevitably be introduced. Due to the tamper-proof nature of blockchain, once a smart contract is successfully deployed on the chain, the contract code itself and the data generated cannot be modified or upgraded. If there are loopholes in the contract code, it will leave huge hidden dangers. For this reason, it is of great significance to conduct a security audit of the smart contract code before it is deployed to the blockchain network.

[0003] With the continuous expansion of the number and scale of smart contracts, the increasing complexity of functions, and the gradual increase in the difficulty of vulnerability mining, traditional software testing and expert review methods can no longer meet the needs of smart contract security, and researchers have begun to focus on the research of automated vulnerability detection methods. Common smart contract vulnerability detection methods include symbolic execution, fuzz testing, and formal verification. Symbolic execution can accurately explore the execution path of the program with the help of constraint collection and solution, but it takes a lot of time and may even cause state space explosion and constraint solving problems. It is not suitable for large-scale contract vulnerability detection. Fuzz testing is a testing technology widely used in traditional applications. It detects vulnerabilities by inputting unexpected data into the target program and monitoring the abnormal results of the program. Compared with traditional applications, smart contracts have many different characteristics, which greatly reduce the effect of fuzz testing for smart contracts. Formal verification technology can understand the code at a higher semantic level and is suitable for relatively small-scale and complex smart contracts, but its degree of automation is relatively low, and most of them require manual secondary verification, which increases the detection cost.

[0004] There are currently a number of solutions to the problem of smart contract vulnerability detection, but the following problems still exist:

[0005] 1) Since more than half of Ethereum smart contracts do not disclose their source code, many studies are conducted at the bytecode level. However, bytecode is a digital sequence composed of 0s and 1s, and it is difficult to directly extract the grammatical structure and semantic information of the code from it. In addition, the smart contract source code is treated as plain text during the processing process and directly processed by natural language processing technology, ignoring the grammatical structure and semantic information contained in the code, which will lead to the loss of some vulnerability features, resulting in a high rate of missed reports and false positives;

[0006] 2) Most solutions simply judge the security of the code and give a simple binary classification value similar to 0 or 1 as the vulnerability detection result, and it is impossible to detect all the vulnerability types implied in it at one time. Summary of the invention

[0007] In view of the above problems, the present invention provides a smart contract vulnerability detection method and system at the source code level. The abstract syntax tree that carries structured information such as syntax and semantics is used to perform intermediate representation of the source code, which can minimize redundancy and retain complete vulnerability features. The lexical unit sequence is obtained by traversing the abstract syntax tree and vectorized. Finally, a deep neural network model combined with an attention mechanism is used to automatically extract vulnerability features from a large amount of code. Each vulnerability type is detected separately through multiple binary classifications, and multiple vulnerability types existing in the source code can be output at one time, which significantly improves the efficiency of vulnerability detection, improves the accuracy of vulnerability detection to a certain extent, reduces the missed alarm rate and false alarm rate, and can point out the specific form of the vulnerability in the source code and the location where the vulnerability is generated, so as to discover the vulnerability as early as possible and take remedial measures in time to ensure that the smart contract can run safely and correctly.

[0008] To achieve the above objectives, the present invention provides a smart contract vulnerability detection method at the source code level, comprising:

[0009] Acquire smart contract sample data by crawling, and pre-process the smart contract sample data;

[0010] Detect and analyze whether there are vulnerabilities and the types of vulnerabilities in the preprocessed smart contract sample data, and mark them with multiple labels as a sample set;

[0011] Performing lexical analysis and grammatical analysis on the sample set to generate an abstract syntax tree, and performing depth-first traversal on the abstract syntax tree to generate a lexical unit sequence text;

[0012] Using a word embedding model to vectorize the lexical unit sequence text, and packing and filling the vectorized data to form a sample data set;

[0013] Combined with the attention mechanism, a vulnerability detection model based on deep neural network is constructed;

[0014] Continuously train and optimize the corresponding binary classifier in the vulnerability detection model for each vulnerability type, and select the vulnerability detection model with the best classification effect as the multi-label classifier;

[0015] The smart contract source code that needs to be tested for vulnerabilities is subjected to the above-mentioned preprocessing and vectorization operations, and the vectorized data is input into the multi-label classifier to output the vulnerability detection result of the smart contract source code.

[0016] In the above technical solution, preferably, if the type of the smart contract sample data obtained by crawling or other means is in bytecode form, the data in bytecode form is restored to source code through reverse analysis operations such as disassembly and decompilation. If the smart contract sample data obtained is in source code form, there is no need to perform reverse analysis operations.

[0017] In the above technical solution, preferably, the specific process of detecting and analyzing whether there are vulnerabilities and the types of vulnerabilities in the pre-processed smart contract sample data includes:

[0018] Based on the preset vulnerability library, multiple detection and analysis methods are used to determine whether the preprocessed smart contract sample data contains vulnerabilities in the vulnerability library, generate a label to indicate whether there are vulnerabilities, and refine the sample data with vulnerabilities to obtain vulnerability types;

[0019] The detection and analysis results of sample data obtained in different ways are summarized. When the proportion of labels of the same vulnerability type in all detection results of the same sample data reaches a preset proportion, the label is used as the label of whether the current sample data has this type of vulnerability.

[0020] In the above technical solution, preferably, the specific process of performing lexical analysis and grammatical analysis on the sample set to generate an abstract syntax tree includes:

[0021] Use syntax parsing tools to generate lexical analyzers and syntax analyzers based on custom grammar rules;

[0022] Using the lexical analyzer to convert the smart contract source code in the sample set into a corresponding Token stream;

[0023] The syntax analyzer is used to analyze the relationship between syntax units in the Token stream to generate the abstract syntax tree.

[0024] In the above technical solution, preferably, the specific process of using the word embedding model to vectorize the lexical unit sequence text and packing and filling the vectorized data to form a sample data set includes:

[0025] Using the CBOW model of the Word2vec algorithm as a word embedding model, obtaining word vector feature text of the lexical unit sequence text, and converting each feature text into a word vector;

[0026] The word vectors are processed to a uniform length using a packing and padding method to completely retain the feature data and form a sample data set.

[0027] In the above technical solution, preferably, the smart contract vulnerability detection method at the source code level also includes:

[0028] For the sample data set obtained by packaging and filling, the SMOTETomek method is used to perform combined sampling of contracts with vulnerabilities and contracts without vulnerabilities, and the vulnerability detection model is trained using the combined sampled data.

[0029] In the above technical solution, preferably, the neural network structure of the vulnerability detection model includes a recurrent neural network, a long short-term memory network, a bidirectional long short-term memory network, a gated recurrent unit, a bidirectional gated recurrent unit and Wide&Deep.

[0030] In the above technical solution, preferably, the specific process of continuously training and optimizing the corresponding binary classifier in the vulnerability detection model for each vulnerability type includes:

[0031] Randomly select 80% of the sample data from the sample data set as a training data set, and the remaining 20% ​​of the sample data as a test data set;

[0032] Using the training data set and labels marked with corresponding sample data, training a binary classifier for each vulnerability type in the vulnerability detection model;

[0033] The test data set is used to evaluate the vulnerability detection model formed by the combination of binary classifiers, and the parameters of the vulnerability detection model are adjusted and optimized according to the evaluation result feedback.

[0034] In the above technical solution, preferably, according to the output result of the multi-label classifier, it is determined whether there is a vulnerability in the smart contract source code to be detected and the specific vulnerability type;

[0035] Based on the attention mechanism, determine the location of the vulnerability in the source code of the smart contract.

[0036] The present invention further proposes a smart contract vulnerability detection system at the source code level, which uses the smart contract vulnerability detection method at the source code level disclosed in any one of the above technical solutions, including:

[0037] A sample data acquisition module, used to acquire smart contract sample data by crawling or other means, and pre-process the smart contract sample data;

[0038] A sample set marking module is used to detect and analyze whether there are vulnerabilities and vulnerability types in the pre-processed smart contract sample data, and mark them with multiple labels as a sample set;

[0039] A code intermediate representation module, used for performing lexical analysis and grammatical analysis on the sample set to generate an abstract syntax tree, and performing depth-first traversal on the abstract syntax tree to generate a lexical unit sequence text;

[0040] A sample data vectorization module is used to vectorize the lexical unit sequence text using a word embedding model, and pack and fill the vectorized data to form a sample data set;

[0041] The detection model building module is used to combine the attention mechanism to build a vulnerability detection model based on deep neural network;

[0042] A model training optimization module is used to continuously train and optimize the corresponding binary classifier in the vulnerability detection model for each vulnerability type, and select the vulnerability detection model with the best classification effect as the multi-label classifier;

[0043] The code vulnerability detection module is used to perform the above-mentioned preprocessing and vectorization operations on the smart contract source code that needs to be detected for vulnerabilities, and input the vectorized data into the multi-label classifier to output the vulnerability detection result of the smart contract source code.

[0044] Compared with the prior art, the present invention has the following beneficial effects:

[0045] 1) The abstract syntax tree that carries structured information such as syntax and semantics is used to represent the source code in the middle, which can minimize redundancy and retain complete vulnerability features;

[0046] 2) By detecting each vulnerability type separately through multiple binary classifications, multiple vulnerability types existing in the source code can be output at one time, significantly improving the efficiency of vulnerability detection;

[0047] 3) By adding an attention mechanism to the neural network model, the important features extracted by the neural network can be weighted, which can improve the accuracy of vulnerability detection to a certain extent. At the same time, the weights of the attention network layer can be visualized, and the main elements that cause the vulnerability can be marked, which is convenient for subsequent analysis or repair of the code, so as to discover the vulnerability as early as possible and take remedial measures in time to ensure that the smart contract can run safely and correctly;

[0048] 4) Considering that there are far fewer contracts with vulnerabilities than contracts without vulnerabilities in the smart contract data set, there is a problem of data imbalance. If such a data set is directly used for training, it may interfere with the learning process of the model. Therefore, the present invention adopts the SMOTETomek method to balance the sample data by combining sampling, reducing the impact of the large gap between sample data between classes on the learning effect. BRIEF DESCRIPTION OF THE DRAWINGS

[0049] Figure 1 A schematic diagram of a flow chart of a method for detecting smart contract vulnerabilities at the source code level disclosed in an embodiment of the present invention;

[0050] Figure 2 A schematic diagram of the overall framework of a method for detecting smart contract vulnerabilities at the source code level disclosed in an embodiment of the present invention;

[0051] Figure 3 A schematic diagram of the modules of a smart contract vulnerability detection system at the source code level disclosed in an embodiment of the present invention.

[0052] In the figure, the corresponding relationship between each component and the reference numeral is as follows:

[0053] 11. Sample data acquisition module, 12. Sample set labeling module, 13. Code intermediate representation module, 14. Sample data vectorization module, 15. Detection model construction module, 16. Model training optimization module, 17. Code vulnerability detection module. DETAILED DESCRIPTION

[0054] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0055] The present invention is further described in detail below in conjunction with the accompanying drawings:

[0056] like Figure 1 and Figure 2 As shown, a source code level smart contract vulnerability detection method provided by the present invention includes:

[0057] Obtain smart contract sample data through crawlers and other means, and pre-process the smart contract sample data;

[0058] Detect and analyze whether there are vulnerabilities and the types of vulnerabilities in the preprocessed smart contract sample data, and mark them with multiple labels as a sample set;

[0059] Perform lexical analysis and grammatical analysis on the sample set to generate an abstract syntax tree, and perform depth-first traversal on the abstract syntax tree to generate a lexical unit sequence text;

[0060] Use the word embedding model to vectorize the lexical unit sequence text, and pack and fill the vectorized data to form a sample data set;

[0061] Combined with the attention mechanism, a vulnerability detection model based on deep neural network is constructed;

[0062] For each vulnerability type, continuously train and optimize the corresponding binary classifier in the vulnerability detection model, and select the vulnerability detection model with the best classification effect as the multi-label classifier;

[0063] The smart contract source code that needs to be tested for vulnerabilities is subjected to the above-mentioned preprocessing and vectorization operations, and the vectorized data is input into a multi-label classifier to output the vulnerability detection results of the smart contract source code.

[0064] In this embodiment, an abstract syntax tree carrying structured information such as syntax and semantics is used to perform an intermediate representation of the source code, which can minimize redundancy and retain complete vulnerability features. The lexical unit sequence is obtained by traversing the abstract syntax tree and vectorized. Finally, a deep neural network model combined with an attention mechanism is used to automatically extract vulnerability features from a large amount of code. Each vulnerability type is detected separately through multiple binary classifications, and multiple vulnerability types existing in the source code can be output at one time, which significantly improves the efficiency of vulnerability detection, improves the accuracy of vulnerability detection to a certain extent, reduces the missed alarm rate and false alarm rate, and can point out the specific form of the vulnerability in the source code and the location where the vulnerability is generated, so as to discover the vulnerability as early as possible and take remedial measures in time to ensure that the smart contract can run safely and correctly.

[0065] Specifically, the present invention aims to propose a universal, source code-level smart contract vulnerability detection solution. The Ethereum smart contract is used as the research object. The smart contract source code is obtained through crawlers and decompilation tools. The acquired source code is first preprocessed, and the preprocessed data set is multi-labeled. By studying and analyzing specific contract instances with vulnerabilities in the real world from the source code level, the corresponding grammatical structure and semantic information are extracted for smart contracts with different vulnerability types, and these feature information are converted into vector form using a word embedding model, which is input together with the label into a deep learning model combined with an attention mechanism for automatic learning. Through continuous training and optimization of the model, the best network structure and optimal parameter combination for each vulnerability are found. The smart contract to be detected is converted into a vector form using the same processing method and input into the trained model. The model will give a detection result containing detailed information such as whether the smart contract has a vulnerability, the specific vulnerability type, and the specific location of the vulnerability in the code according to the calculation results.

[0066] In the above embodiment, preferably, the subsequent operation is determined according to the form of the input smart contract. Specifically, if the type of the smart contract sample data obtained by crawlers or other methods is in bytecode form, the data in bytecode form is restored to source code through reverse analysis operations such as disassembly and decompilation, and then subsequent operations are performed; if the input smart contract is in source code form, no reverse analysis is required. In other words, detecting a smart contract in bytecode form has an additional reverse analysis step compared to directly detecting a smart contract in source code form.

[0067] Specifically, first, we use crawler technology to crawl a certain amount of smart contract source code from the Ethereum browser. Considering that more than half of the smart contracts in Ethereum are not open source, we use the web3.eth.getCode interface to obtain the corresponding smart contract in bytecode form based on the known contract address. Before performing vulnerability detection at the source code level, the obtained bytecode needs to be restored to source code through disassembly and decompilation operations to facilitate feature extraction and analysis. Then, we preprocess the obtained data set and delete comments and other statements that are not related to the vulnerability.

[0068] In the above embodiment, preferably, the specific process of detecting and analyzing whether there are vulnerabilities and the types of vulnerabilities in the preprocessed smart contract sample data includes:

[0069] Based on the preset vulnerability library, multiple detection and analysis methods are used to determine whether the preprocessed smart contract sample data contains vulnerabilities in the vulnerability library, generate labels to indicate whether there are vulnerabilities, and refine the sample data with vulnerabilities to obtain vulnerability types;

[0070] The detection and analysis results of sample data obtained in different ways are summarized. When the proportion of labels of the same vulnerability type in all detection results of the same sample data reaches a preset proportion, the label is used as the label of whether the current sample data has this type of vulnerability.

[0071] Specifically, we combined vulnerability libraries such as Awesome-Buggy-ERC20-Tokens, used public and effective vulnerability detection tools and manual analysis to generate vulnerability labels for preprocessed sample data, and refined the samples with vulnerabilities to obtain specific vulnerability types. Then, we summarized the detection results obtained by different methods. In order to ensure the accuracy of the labels as much as possible, for the same vulnerability, only those that meet the

[0072]

[0073] The tag is considered valid. i =1 represents that the i-th method is marked as vulnerable, and m represents the total number of different detection methods.

[0074] Specifically, the label is Y=[Y 1 ,Y 2 ,...,Y N ], where N represents the N types of vulnerabilities that need to be detected. n =0 means that the smart contract sample does not have the nth vulnerability, Y n = 1 means that the smart contract sample has the nth vulnerability. For example, the vector [1, 0, 1, 0, 0] means that the sample has both the first and third types of vulnerabilities, while the vector [0, 0, 0, 0, 0] means that the sample does not have any of the five vulnerability types.

[0075] In the above embodiment, preferably, the present invention starts from the source code level, comprehensively considers the grammatical structure and semantic information contained in the smart contract source code, and chooses to use the abstract syntax tree as the carrier of structured information to construct a structured data set. The specific process includes:

[0076] First, use grammar parsing tools such as Antrl4 to generate a lexical analyzer and a grammatical analyzer according to custom grammar rules, and then use them to perform lexical analysis and grammatical analysis on the smart contract source code respectively;

[0077] Use the lexical analyzer to convert the smart contract source code in the sample set into the corresponding token stream;

[0078] The grammar analyzer is used to analyze the relationship between grammatical units in the token stream and generate an abstract syntax tree. Then the generated abstract syntax tree is traversed in depth first to convert it into a lexical unit sequence text.

[0079] In the above embodiments, preferably, for all deep neural networks or classifiers, the input can only be sample data in the format of numerical vectors. In order to input the extracted features into the deep learning model, it is necessary to adopt a suitable encoding method to map the previously obtained lexical unit sequence text into the numerical space.

[0080] The present invention adopts the CBOW model of the Word2vec algorithm as a word embedding model, obtains the word vector feature text of the lexical unit sequence text, and converts each feature text into a word vector;

[0081] Neural networks require a fixed-length vector matrix as the input of the model. Usually, truncation or zero padding methods are used to unify the length of the vector, which will lead to the loss of some feature data. In order to fully retain the vectorized data of the source code, the present invention uses a packing and padding method to unify the length of the word vector to fully retain the feature data and form a sample data set.

[0082] In the above embodiment, preferably, considering that the number of contracts with vulnerabilities in the smart contract data set is far less than the number of contracts without vulnerabilities, there is a problem of imbalance in sample data between classes. Therefore, for the sample data set obtained by packaging and filling, the SMOTETomek method is used to perform combined sampling of contracts with vulnerabilities and contracts without vulnerabilities, and the vulnerability detection model is trained using the combined sampled data to balance the sample data and reduce the impact of the large gap in sample data between classes on the learning effect.

[0083] In the above embodiment, preferably, the specific process of continuously training and optimizing the corresponding binary classifier in the vulnerability detection model for each vulnerability type includes:

[0084] 80% of the samples are randomly selected as the training data set for model training, and the remaining 20% ​​of the samples are used as the test data set to evaluate the performance of the model after learning the parameters on the training data set;

[0085] Using the training data set and the labels of the corresponding sample data, the binary classifier for each vulnerability type in the vulnerability detection model is trained;

[0086] The vulnerability detection model formed by the combination of binary classifiers is evaluated using the test data set, and the parameters of the vulnerability detection model are adjusted and optimized based on the evaluation results.

[0087] In the above embodiment, preferably, a vulnerability detection model is designed and constructed by conducting an in-depth analysis of the smart contract in the Ethereum blockchain platform and combining the program analysis technology and deep learning algorithm commonly used in existing vulnerability detection. Different vulnerability types have different vulnerability characteristics. Models with different network structures in neural networks have different data representation and learning capabilities. Using a single network model may not have a good learning and training effect on all vulnerability types. In order to achieve the detection of multiple vulnerabilities, the present invention converts multi-label classification into binary classification for each vulnerability type, trains N binary classifiers for N vulnerability types, and each binary classifier has a good detection effect on one of the N vulnerabilities. The output combination of multiple models is the final detection result for multiple vulnerability types.

[0088] In the above embodiments, the present invention inputs sample data sets of different vulnerability types together with marked labels into different neural network structures of the vulnerability detection model for training, including but not limited to recurrent neural networks, long short-term memory networks, bidirectional long short-term memory networks, gated recurrent units, bidirectional gated recurrent units, and Wide&Deep, etc.

[0089] On this basis, a number of evaluation indicators such as accuracy, precision, recall, F1 value, Matthew correlation coefficient and Hamming loss are compared and evaluated. At the same time, an attention mechanism is added to the constructed neural network model to weight the important features extracted by the neural network and improve the accuracy of vulnerability detection. By continuously training and optimizing multiple deep neural network models under the same experimental environment and the same smart contract sample data set, the multi-label classification model with the best classification effect is finally selected to generate a multi-label classifier.

[0090] In the above embodiment, preferably, according to the output result of the multi-label classifier, it is determined whether there is a vulnerability in the smart contract source code to be detected and the specific vulnerability type;

[0091] Based on the attention mechanism, determine the location of the vulnerability in the source code of the smart contract.

[0092] The present invention further proposes a smart contract vulnerability detection system at the source code level, which applies the smart contract vulnerability detection method at the source code level disclosed in any one of the above embodiments, including:

[0093] The sample data acquisition module 11 is used to acquire the smart contract sample data by crawling or other means, and pre-process the smart contract sample data;

[0094] The sample set marking module 12 is used to detect and analyze whether there are vulnerabilities and vulnerability types in the pre-processed smart contract sample data, and mark them with multiple labels as a sample set;

[0095] The code intermediate representation module 13 is used to perform lexical analysis and grammatical analysis on the sample set to generate an abstract syntax tree, and perform depth-first traversal on the abstract syntax tree to generate a lexical unit sequence text;

[0096] The sample data vectorization module 14 is used to vectorize the lexical unit sequence text using the word embedding model, and pack and fill the vectorized data to form a sample data set;

[0097] A detection model building module 15 is used to build a vulnerability detection model based on a deep neural network in combination with an attention mechanism;

[0098] A model training optimization module 16 is used to continuously train and optimize the corresponding binary classifier in the vulnerability detection model for each vulnerability type, and select the vulnerability detection model with the best classification effect as the multi-label classifier;

[0099] The code vulnerability detection module 17 is used to perform the above-mentioned preprocessing and vectorization operations on the smart contract source code that needs to be detected for vulnerabilities, and input the vectorized data into a multi-label classifier to output the vulnerability detection results of the smart contract source code.

[0100] In this embodiment, an abstract syntax tree carrying structured information such as syntax and semantics is used to perform an intermediate representation of the source code, which can minimize redundancy and retain complete vulnerability features. The lexical unit sequence is obtained by traversing the abstract syntax tree and vectorized. Finally, a deep neural network model combined with an attention mechanism is used to automatically extract vulnerability features from a large amount of code. Each vulnerability type is detected separately through multiple binary classifications, and multiple vulnerability types existing in the source code can be output at one time, which significantly improves the efficiency of vulnerability detection, improves the accuracy of vulnerability detection to a certain extent, reduces the missed alarm rate and false alarm rate, and can point out the specific form of the vulnerability in the source code and the location where the vulnerability is generated, so as to discover the vulnerability as early as possible and take remedial measures in time to ensure that the smart contract can run safely and correctly.

[0101] According to the smart contract vulnerability detection system at the source code level disclosed in the above embodiment, the functions that can be implemented by each module thereof correspond to the functions that can be implemented by each step in the smart contract vulnerability detection method at the source code level disclosed in the above embodiment. According to the specific implementation method disclosed in the above embodiment, the implementation methods of each module will not be repeated here.

[0102] The above are only preferred embodiments of the present invention and are not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A smart contract vulnerability detection method at the source code level, characterized in that: include: Acquire smart contract sample data by crawling, and pre-process the smart contract sample data; Detect and analyze whether there are vulnerabilities and the types of vulnerabilities in the preprocessed smart contract sample data, and mark them with multiple labels as a sample set; Performing lexical analysis and grammatical analysis on the sample set to generate an abstract syntax tree, and performing depth-first traversal on the abstract syntax tree to generate a lexical unit sequence text; Using a word embedding model to vectorize the lexical unit sequence text, and packing and filling the vectorized data to form a sample data set; Combined with the attention mechanism, a vulnerability detection model based on deep neural network is constructed; Continuously train and optimize the corresponding binary classifier in the vulnerability detection model for each vulnerability type, and select the vulnerability detection model with the best classification effect as the multi-label classifier; The smart contract source code that needs to be tested for vulnerabilities is subjected to the above-mentioned preprocessing and vectorization operations, and the vectorized data is input into the multi-label classifier, and the vulnerability detection result of the smart contract source code is obtained as output; The specific process of detecting and analyzing whether there are vulnerabilities and the types of vulnerabilities in the pre-processed smart contract sample data includes: Based on the preset vulnerability library, multiple detection and analysis methods are used to determine whether the preprocessed smart contract sample data contains vulnerabilities in the vulnerability library, generate a label to indicate whether there are vulnerabilities, and refine the sample data with vulnerabilities to obtain vulnerability types; The detection and analysis results of sample data obtained in different ways are summarized. When the proportion of labels of the same vulnerability type in all detection results of the same sample data reaches a preset proportion, the label is used as the label of whether the current sample data has this type of vulnerability; The specific process of performing lexical analysis and grammatical analysis on the sample set to generate an abstract syntax tree includes: Use syntax parsing tools to generate lexical analyzers and syntax analyzers based on custom grammar rules; Using the lexical analyzer to convert the smart contract source code in the sample set into a corresponding Token stream; The syntax analyzer is used to analyze the relationship between syntax units in the Token stream to generate the abstract syntax tree.

2. The method for detecting smart contract vulnerabilities at the source code level according to claim 1, characterized in that: If the smart contract sample data obtained is in bytecode form, the data in bytecode form is restored to source code through reverse analysis operations of disassembly and decompilation. If the smart contract sample data obtained is in source code form, there is no need to perform reverse analysis operations.

3. The method for detecting smart contract vulnerabilities at the source code level according to claim 1, characterized in that: The specific process of using the word embedding model to vectorize the lexical unit sequence text and packing and filling the vectorized data to form a sample data set includes: Using the CBOW model of the Word2vec algorithm as a word embedding model, obtaining word vector feature text of the lexical unit sequence text, and converting each feature text into a word vector; The word vectors are processed to a uniform length using a packing and padding method to completely retain the feature data and form a sample data set.

4. The method for detecting smart contract vulnerabilities at the source code level according to claim 1 or 3, characterized in that: Also includes: For the sample data set obtained by packaging and filling, the SMOTETomek method is used to perform combined sampling of contracts with vulnerabilities and contracts without vulnerabilities, and the vulnerability detection model is trained using the sample data after combined sampling.

5. The method for detecting smart contract vulnerabilities at the source code level according to claim 4, characterized in that: The neural network structure of the vulnerability detection model includes a recurrent neural network, a long short-term memory network, a bidirectional long short-term memory network, a gated recurrent unit, a bidirectional gated recurrent unit, and Wide&Deep.

6. The method for detecting smart contract vulnerabilities at the source code level according to claim 4, characterized in that: The specific process of continuously training and optimizing the corresponding binary classifier in the vulnerability detection model for each vulnerability type includes: Randomly select 80% of the sample data from the sample data set as the training data set, and the remaining 20% ​​of the sample data as the test data set; Using the training data set and labels marked with corresponding sample data, training a binary classifier for each vulnerability type in the vulnerability detection model; The test data set is used to evaluate the vulnerability detection model formed by the combination of binary classifiers, and the parameters of the vulnerability detection model are adjusted and optimized according to the evaluation result feedback.

7. The method for detecting smart contract vulnerabilities at the source code level according to claim 6, characterized in that: According to the output results of the multi-label classifier, determine whether there are vulnerabilities in the smart contract source code that needs to be detected and the specific vulnerability types; Based on the attention mechanism, determine the location of the vulnerability in the source code of the smart contract.

8. A smart contract vulnerability detection system at the source code level, applying the smart contract vulnerability detection method at the source code level as claimed in any one of claims 1 to 7, characterized in that: include: A sample data acquisition module, used to acquire smart contract sample data by crawling and preprocess the smart contract sample data; A sample set marking module is used to detect and analyze whether there are vulnerabilities and vulnerability types in the pre-processed smart contract sample data, and mark them with multiple labels as a sample set; A code intermediate representation module, used for performing lexical analysis and grammatical analysis on the sample set to generate an abstract syntax tree, and performing depth-first traversal on the abstract syntax tree to generate a lexical unit sequence text; A sample data vectorization module is used to vectorize the lexical unit sequence text using a word embedding model, and pack and fill the vectorized data to form a sample data set; The detection model building module is used to combine the attention mechanism to build a vulnerability detection model based on deep neural network; A model training optimization module is used to continuously train and optimize the corresponding binary classifier in the vulnerability detection model for each vulnerability type, and select the vulnerability detection model with the best classification effect as the multi-label classifier; The code vulnerability detection module is used to perform the above-mentioned preprocessing and vectorization operations on the smart contract source code that needs to be detected for vulnerabilities, and input the vectorized data into the multi-label classifier to output the vulnerability detection result of the smart contract source code.

Citation Information

Patent Citations

  • Method and device for monitoring smart contract in block chain

    CN110263536A

  • Intelligent contract code vulnerability detection method based on deep learning technology

    CN110543419A

  • Intelligent contract vulnerability detection method and device based on LSTM and BiLSTM

    CN111898134A