A deep learning method-based intelligent contract code vulnerability detection system
By utilizing a deep learning-based smart contract code vulnerability detection system with DistilBert and BiLSTM-Attention networks, the problem of traditional methods being unable to comprehensively detect smart contract vulnerabilities is solved, achieving efficient and accurate vulnerability detection and improving the security and development efficiency of smart contracts.
Patent Information
- Application Number
- CN202411767389.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-04
- Publication Date
- 2025-11-21
- Estimated Expiration
- 2044-12-04
AI Technical Summary
Existing smart contract code vulnerability detection tools cannot fully cover the diverse types of vulnerabilities, especially high-level security violations and vulnerabilities, leading to serious security problems for smart contracts. Furthermore, traditional methods require writing new rules for each new vulnerability type or variant, resulting in low detection efficiency.
A smart contract code vulnerability detection system based on deep learning is proposed. The detection model is constructed using the DistilBert pre-trained model and the BiLSTM-Attention network. The detection accuracy and efficiency of the model are improved through data cleaning, labeling, training set adjustment and oversampling.
It improves the accuracy and efficiency of smart contract code vulnerability detection, and can perform detection on different datasets and various types of smart contracts, ensuring the security and efficiency of the smart contract development process.
Smart Images

Figure CN119691752B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of information security technology, and in particular to a smart contract code vulnerability detection system based on deep learning methods. Background Technology
[0002] With the development of blockchain technology, smart contracts have gradually become a core component of decentralized applications (DApps). However, their security issues are becoming increasingly prominent, especially the frequent occurrence of financial losses due to code vulnerabilities. The increasing number of smart contracts and the promotion of DApps have led to an exponential growth in the digital assets involved in smart contracts. Compared to traditional software, the security issues of smart contracts are more challenging and the reality is more severe. The trustworthiness of smart contracts stems from their immutability; once deployed, they cannot be modified. Anyone can launch attacks on the security vulnerabilities within the contract. If the contract lacks corresponding defensive measures, the escalation of security problems will be unstoppable, severely damaging the economic value of the contract itself and public trust in the project.
[0003] Currently, the main smart contract code vulnerability detection tools on the market are formal verification tools. Formal verification uses logic languages to formally model smart contract documents and code, and checks the functional correctness and security attributes of smart contracts through mathematical reasoning and proof. It ensures absolute correctness within a certain range and makes up for the limitations of contract testing and contract auditing. However, it does not cover all types of vulnerabilities and can mostly only detect low-level security violations and vulnerabilities. Summary of the Invention
[0004] In view of this, in order to address the technical problem that existing code vulnerability detection methods cannot cover the diverse types of vulnerabilities, this invention proposes a smart contract code vulnerability detection system based on deep learning methods, the system comprising:
[0005] The data processing module acquires the smart contract dataset and labels it to obtain the training set.
[0006] The model building module is based on the DistilBert pre-trained model and introduces a BiLSTM-Attention network to build a detection model;
[0007] The optimization module uses the training set to train the detection model, and obtains the trained detection model after meeting the preset conditions.
[0008] The code detection module inputs the smart contract code into the trained detection model and outputs the detection results.
[0009] In some embodiments, the data processing module further includes:
[0010] Clean the code in the smart contract dataset, including removing version declarations, comments, blank lines, leading spaces, and cleaning up code snippets.
[0011] This optimized process helps reduce the amount of data and improve model training and detection efficiency.
[0012] In some embodiments, the data processing module specifically includes:
[0013] Obtain the smart contract dataset, analyze the vulnerability types and distribution within the data, and obtain the vulnerability distribution information;
[0014] Based on the vulnerabilities in the source code of the smart contract, the extracted vulnerability information is used to label the smart contract, thus obtaining a training set.
[0015] In some embodiments, the model training module specifically includes:
[0016] The training set data is segmented, and the labels within it are encoded using one-hot encoding.
[0017] For the training set, fill the sequences that do not meet the preset length with 0, and partially truncate the sequences that exceed the preset length to obtain the adjusted training set;
[0018] Oversampling is performed on the adjusted training set to improve the balance of the dataset, resulting in a resampled training set.
[0019] The detection model is trained using the numerical labels and segmented word blocks from the resampled training set to obtain the trained detection model.
[0020] Based on the above scheme, this invention provides a smart contract code vulnerability detection system based on deep learning methods. It utilizes DistilBert, BiLSTM, and Attention techniques from deep learning to build a detection model. This method, based on smart contract source code and deep learning technology, can perform detection on different datasets and various types of smart contracts, whereas traditional methods may require writing new rules for each new vulnerability type or variant. The vulnerability detection accuracy of this invention is higher than traditional smart contract code vulnerability detection tools, ensuring security and efficiency in the smart contract development process. Compared to traditional smart contract code vulnerability detection tools, the detection efficiency of this invention is significantly higher. Attached Figure Description
[0021] Figure 1This is a structural block diagram of a smart contract code vulnerability detection system based on deep learning methods according to the present invention.
[0022] Figure 2 This is a schematic diagram illustrating the vulnerability distribution in an embodiment of the present invention;
[0023] Figure 3 This is a partial structural schematic diagram of the detection model of the present invention.
[0024] Figure 4 This is a schematic diagram of part of the workflow of the detection model of the present invention.
[0025] Figure 5 This is a schematic diagram of the overall workflow of the present invention. Detailed Implementation
[0026] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0027] It should be noted that, for ease of description, only the parts relevant to the invention are shown in the accompanying drawings. Unless otherwise specified, the embodiments and features described in this application can be combined with each other.
[0028] It should be understood that the terms "system," "apparatus," "unit," and / or "module" used in this application are a method of distinguishing different components, elements, parts, sections, or assemblies at different levels. However, if other terms can achieve the same purpose, they may be replaced by other expressions.
[0029] As indicated in this application and claims, unless the context clearly indicates otherwise, the words "a," "an," "a," and / or "the" are not specifically singular and may include the plural. Generally, the terms "comprising" and "including" only indicate the inclusion of expressly identified steps and elements, which do not constitute an exclusive list, and the method or apparatus may also include other steps or elements. An element defined by the phrase "comprising an..." does not exclude the presence of other identical elements in the process, method, product, or apparatus that includes the element.
[0030] In the description of the embodiments of this application, "a plurality of" refers to two or more. The terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature.
[0031] Furthermore, flowcharts are used in this application to illustrate the operations performed by the system according to embodiments of this application. It should be understood that the preceding or following operations are not necessarily performed precisely in sequence. Instead, the steps can be processed in reverse order or simultaneously. Additionally, other operations can be added to these processes, or one or more steps can be removed from them.
[0032] Reference Figure 1 The diagram below shows a structural block diagram of an optional example of the smart contract code vulnerability detection system based on deep learning proposed in this invention. The system proposed in this embodiment may include, but is not limited to, the following modules:
[0033] The data processing module completes step S1;
[0034] Step S1: Obtain the smart contract dataset and label the smart contract dataset to obtain the training set;
[0035] The model building module completes step S2;
[0036] Step S2: Based on the DistilBert pre-trained model, introduce the BiLSTM-Attention network to construct the detection model;
[0037] The model training module completes step S3;
[0038] Step S3: Train the detection model based on the training set to obtain the trained detection model;
[0039] After the code inspection module is completed, proceed to step S4;
[0040] Step S4: Input the smart contract code into the trained detection model and output the detection results.
[0041] In some feasible embodiments, the data processing module further includes:
[0042] Code cleaning includes the following steps:
[0043] i. Remove version declaration: Remove the Solidity version declaration from the code so that the code will not be affected by version differences.
[0044] ii. Remove comments and non-ASCII characters: Remove comments (including multi-line and single-line comments) and non-ASCII characters from the code, and remove elements that may interfere with subsequent processing.
[0045] iii. Remove blank lines: This function removes blank lines from the code, making the code more compact and reducing irrelevant content.
[0046] iv. Remove leading spaces: Removing leading spaces from each line makes the code format more consistent.
[0047] v. Clean up code snippets: Perform further cleanup on code snippets, including removing strings and character literals, and replacing user-defined functions with FUN followed by a number, and variable names with VAR followed by a number.
[0048] In some feasible embodiments, the data processing module specifically performs the following processing steps:
[0049] S1.1 Obtain a smart contract dataset (containing smart contracts on several public blockchains) and analyze the smart contracts to obtain the distribution of vulnerabilities;
[0050] For each smart contract, the original dataset collected information including its address (the address where the contract is deployed), tools, lines (lines that may have security issues), nb_vulnerabilities (the number of vulnerabilities discovered), and source_code (the smart contract's source code). The tools column contains the results of audits performed on the smart contract using different security analysis tools (such as Mythril). Specifically, the tools column is formatted as a nested dictionary containing the audit results from multiple security analysis tools. Each tool has its own key, corresponding to a dictionary with two subkeys: vulnerabilities and categories. The vulnerabilities key corresponds to a dictionary containing detailed information on all vulnerabilities detected by that tool. Each vulnerability has a unique identifier (e.g., 'reentrancy-no-eth', 'Integer Underflow', etc.) as the key. The categories key corresponds to a dictionary containing the vulnerability categories detected by the tool and their corresponding counts.
[0051] The smart contracts in the dataset include a total of eight types: 'other', 'unchecked_low_calls', 'arithmetic', 'access_control', 'reentrancy', 'denial_service', 'front_running', and 'time_manipulation'. Figure 2 This demonstrates the distribution of different vulnerabilities in the contracts within the dataset.
[0052] S1.2. Based on the vulnerabilities in the source code of the smart contract, the smart contract is marked to obtain a training set.
[0053] Create a new dataset named dataset_vuln, which contains addresses and source code, as well as vulnerability information extracted from tools. For each vulnerability marked as existing in tools, the corresponding column in dataset_vuln will be set to true; otherwise, it will be marked as false.
[0054] Based on the obtained dataset_vuln, label the smart contracts. For the source code: if both its 'reentrancy' and 'unchecked_low_calls' columns are True, then its 'multi_label' column is labeled 3; if the 'reentrancy' column is True and the 'unchecked_low_calls' column is False, then its 'multi_label' column is labeled 2; if the 'reentrancy' column is False and the 'unchecked_low_calls' column is True, then its 'multi_label' column is labeled 1; if both its 'reentrancy' and 'unchecked_low_calls' columns are False, then its 'multi_label' column is labeled 0.
[0055] In some feasible embodiments, the specific processing steps of the model training module include:
[0056] S3.1 Constructing model inputs.
[0057] The preprocessed dataset contains two columns: 'Cleaned Contract' and 'Label'. 'CleanedContract' represents the preprocessed smart contract code, and 'Label' is the corresponding label. Python's LabelEncoder is used to convert the labels in the label column into integer codes. Then, the training and test sets are split, with the test set comprising 0.2% of the total data. A tokenizer is fitted using the training data X_train to construct a vocabulary. Then, the training data X_train and the test data X_test are converted into numerical sequences based on the vocabulary. Finally, to_categorical is used to perform one-hot encoding on the labels of the training and test sets.
[0058] In this embodiment, each sequence is set to a length of 512. For sequences shorter than 512, zeros are used for padding; sequences longer than 512 are truncated. [CLS] and [SEP] are marked at the beginning and end of each sentence. Furthermore, this embodiment uses a special encoder to convert the text tags of each contract into numeric tags.
[0059] S3.2, Oversampling.
[0060] After labeling the data, the number of contracts labeled "0" is far greater than the number of contracts labeled with other numbers, which may cause the model's predictions to be biased towards the majority class. Here, this embodiment addresses the problem of imbalanced dataset distribution by oversampling the training set. In this embodiment, the `SMOTE` class is imported from the `imblearn.oversampling` module in Python, an instance of the `SMOTE` class, `smote`, is created, `sampling_strategy='auto'` is set to automatically determine the amount of oversampling needed, and `random_state=42` is set to ensure the reproducibility of the results. The `smote.fit_resample` method is used to oversample the padded training set and its corresponding labels to obtain the resampled training set.
[0061] S3.3 Training process.
[0062] Numeric labels and segmented word blocks are input into the DistilBert pre-trained model. During training, parameters such as the number of epochs, batch size, and learning rate are continuously adjusted, and appropriate evaluation metrics are set to calculate data such as precision and false positive rate for each label type. In this embodiment, the number of training epochs is set to 10; the batch size is set to 32; the learning rate is set to 2e-05; and the weight decay rate is set to 0.001 for regularization to prevent overfitting.
[0063] In some embodiments, the specific structure of the detection model is as follows:
[0064] Based on the characteristics of the DistilBERT model, an optimized BiLSTM-Attention network is added to the model, using the output of the last hidden layer in the DistilBERT pre-trained model as the training dataset for the BiLSTM-Attention network. This embodiment adds an adapter layer as a bridge between the DistilBERT and BiLSTM-Attention layers, compressing the 768-dimensional output of DistilBERT to the 128-dimensional input of the BiLSTM. The BiLSTM-Attention layer consists of a BiLSTM layer and an Attention layer. The BiLSTM layer contains 128 LSTM units, using dropout and recursive dropout to reduce overfitting. The input and output of the Attention layer are both outputs of the BiLSTM layer. The output of the Attention layer is then added to the original output of the LSTM layer to form a residual connection. After normalizing the output of the residual connection, a global average pooling layer is applied to compress the sequence data into a fixed-size vector, reducing the number of model parameters and obtaining the pooled_output. Finally, a fully connected layer with 64 units and a ReLU activation function is created, followed by batch normalization and a Dropout layer to prevent overfitting. The output layer contains 4 units and a softmax activation function, serving as a structural reference for the BiLSTM-Attention network in the detection model. Figure 3 .
[0065] The detection model's workflow is partially referenced. Figure 4 .
[0066] In some embodiments, a model testing step is further included, wherein:
[0067] Clean up the code in the test set, including removing version declarations, comments and non-ASCII characters, removing blank lines, removing leading spaces, and cleaning up code snippets.
[0068] For the test set after code cleaning, this embodiment uses a word segmenter to perform segmentation operations, and performs segmentation and truncation according to the sequence length. It also marks the beginning and end of the sentences with [CLS] and [SEP].
[0069] The processed test set is fed into the model for detection, and the test results are evaluated and analyzed.
[0070] In addition, the specific application testing process of the model follows the test steps.
[0071] Based on the above scheme, the overall workflow of the present invention is as follows: Figure 5 .
[0072] The above is a detailed description of the preferred embodiments of the present invention. However, the present invention is not limited to the embodiments described. Those skilled in the art can make various equivalent modifications or substitutions without departing from the spirit of the present invention. All such equivalent modifications or substitutions are included within the scope defined by the claims of this application.
Claims
1. A smart contract code vulnerability detection system based on deep learning methods, characterized in that, include: The data processing module acquires the smart contract dataset and labels the smart contract dataset to obtain the training set; The model building module is based on the DistilBert pre-trained model and introduces a BiLSTM-Attention network to build a detection model. The model training module trains the detection model based on the training set to obtain the trained detection model; The code detection module inputs the smart contract code into the trained detection model and outputs the detection results. An adapter layer is provided between the DistilBert pre-trained model and the BiLSTM-Attention network; The BiLSTM-Attention network includes a BiLSTM layer and an Attention layer. The BiLSTM layer includes multiple LSTM units, wherein: The output of the Attention is added to the original output of the LSTM unit to form a residual connection; The output of the residual connection is normalized. A global average pooling layer is applied to convert the sequence data into a vector, which is then output through a fully connected layer.
2. The smart contract code vulnerability detection system based on deep learning method according to claim 1, characterized in that, The data processing module also includes: The code in the smart contract dataset is cleaned.
3. The smart contract code vulnerability detection system based on deep learning method according to claim 2, characterized in that, The cleaning methods include removing version declarations, removing comments, removing blank lines, and cleaning up code snippets.
4. The smart contract code vulnerability detection system based on deep learning method according to claim 1, characterized in that, The process of obtaining a smart contract dataset and labeling the smart contract dataset to obtain a training set specifically includes: Obtain smart contract datasets and analyze smart contracts to obtain the distribution of vulnerabilities; Based on the vulnerabilities in the source code of the smart contract, the smart contract is labeled to obtain a training set.
5. The smart contract code vulnerability detection system based on deep learning method according to claim 4, characterized in that, The process of training the detection model based on the training set to obtain the trained detection model specifically includes: The labels of the training set are one-hot encoded; Based on a preset sequence length, the data in the training set is adjusted to obtain an adjusted training set; The adjusted training set is oversampled to obtain a resampled training set; The detection model is trained based on the digital labels and segmented word blocks of the resampled training set to obtain the trained detection model.
6. The smart contract code vulnerability detection system based on deep learning method according to claim 5, characterized in that, Also includes: Configure the indicator calculation function to evaluate the precision and false alarm rates for all types of data.
Citation Information
Patent Citations
Block chain intelligent contract vulnerability detection method and device based on deep learning
CN109977682A
Intelligent contract vulnerability detection method combining GRU and SVM
CN114036528A