Intelligent contract code vulnerability detection system based on deep learning method

By building a smart contract code vulnerability detection model based on deep learning, using DistilBert and BiLSTM-Attention networks to detect vulnerabilities in smart contracts, solving the problem that existing tools cannot fully detect diversified vulnerabilities, and achieving higher detection accuracy and efficiency.

CN119691752AActive Publication Date: 2025-03-25SUN YAT SEN UNIV
View PDF 8 Cites 0 Cited by

Patent Information

Application Number
CN202411767389.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-04
Publication Date
2025-03-25
Estimated Expiration
2044-12-04

AI Technical Summary

Technical Problem

Existing smart contract code vulnerability detection tools cannot effectively detect diversified vulnerability types, resulting in the inability to fully guarantee the security of smart contracts.

Method used

Using a deep learning-based method, a smart contract code vulnerability detection model is built using the DistilBert pre-trained model and BiLSTM-Attention network, and the smart contract data set is marked and trained to generate detection results.

Benefits of technology

It improves the accuracy and efficiency of smart contract code vulnerability detection, can detect on different data sets and multiple types of smart contracts, covers more vulnerability types, and enhances the security in the development process of smart contracts.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119691752A_ABST
    Figure CN119691752A_ABST
Patent Text Reader

Abstract

The invention discloses an intelligent contract code vulnerability detection system based on a deep learning method, and the system comprises a data processing module which obtains an intelligent contract data set and carries out the marking of the intelligent contract data set, and obtains a training set; the model construction module is used for introducing a BiLSTM-Attention network on the basis of a DistilBert pre-training model, and constructing a detection model; the model training module is used for training the detection model based on the training set to obtain a trained detection model; and the code detection module inputs an intelligent contract code into the trained detection model and outputs a detection result. By using the method and the device, the detected vulnerability types and the detection accuracy can be improved. The method can be widely applied to the field of data detection.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of information security technology, and in particular to a smart contract code vulnerability detection system based on a deep learning method. Background Art

[0002] With the development of blockchain technology, smart contracts have gradually become a core component of decentralized applications, but their security issues have become increasingly prominent, especially the frequent financial losses caused by code vulnerabilities. With the increase in the number of smart contracts and the promotion of decentralized applications (DApp), the digital assets involved in smart contracts have grown exponentially. Compared with traditional software, the security issues of smart contracts are more difficult and the reality is more severe. The credibility of smart contracts comes from their immutability. Once deployed and launched, they cannot be modified. Anyone can launch an attack on the security vulnerabilities in the contract. If the contract does not have corresponding defense measures, it will be impossible to curb the deterioration of security issues, which will seriously damage the economic value of the contract itself and the public's trust in the project.

[0003] The smart contract code vulnerability detection tools currently available on the market are mainly formal verification: Formal verification uses logical language to formally model smart contract documents and codes, and checks the functional correctness and security properties of smart contracts through mathematical reasoning logic and proof, ensuring absolute correctness within a certain range, making up for the limitations of contract testing and contract auditing. However, the types of vulnerabilities it covers are incomplete, and most of them can only detect low-level security violations and vulnerabilities. Summary of the invention

[0004] In view of this, in order to solve the technical problem that the existing code vulnerability detection method cannot cover the existing diverse vulnerability types, the present invention proposes a smart contract code vulnerability detection system based on a deep learning method, the system comprising:

[0005] The data processing module obtains the smart contract data set and labels the smart contract data set with labels to obtain a training set;

[0006] The model building module uses the DistilBert pre-trained model as a basis, introduces the BiLSTM-Attention network, and builds a detection model;

[0007] The optimization module uses the training set to train the detection model, and obtains the trained detection model after the preset conditions are met;

[0008] The code detection module inputs the smart contract code into the trained detection model and outputs the detection result.

[0009] In some embodiments, the data processing module further includes:

[0010] The codes in the smart contract dataset are cleaned, including removing version declarations, removing comments, removing blank lines, removing leading spaces, and cleaning up code snippets.

[0011] This optimization step helps to reduce the amount of data and improve the efficiency of model training and model detection.

[0012] In some embodiments, the data processing module specifically includes:

[0013] Obtain smart contract data sets, analyze vulnerability types and distribution in the data, and obtain the distribution of vulnerabilities;

[0014] According to the vulnerabilities of the source code in the smart contract, the smart contract is marked using the extracted vulnerability information to obtain a training set.

[0015] In some embodiments, in the model training module, specifically including:

[0016] Split the data of the training set and perform one-hot encoding on the labels;

[0017] For the training set, the sequences that do not meet the preset length are filled with 0, and the sequences that exceed the preset length are partially truncated to obtain the adjusted training set;

[0018] Oversampling the adjusted training set to improve the balance of the data set to obtain a resampled training set;

[0019] The detection model is trained based on the digital labels and segmented word blocks of the resampled training set to obtain a trained detection model.

[0020] Based on the above scheme, the present invention provides a smart contract code vulnerability detection system based on a deep learning method, which uses DistilBert and BiLSTM and Attention technology in deep learning to build a detection model. This method is based on smart contract source code and deep learning technology, and can be used for detection on different data sets and multiple types of smart contracts, while traditional methods may need to write new rules for each new vulnerability type or variant; the vulnerability detection accuracy of the present invention is improved compared with traditional smart contract code vulnerability detection tools, ensuring the security and efficiency of the smart contract development process; compared with traditional smart contract code vulnerability detection tools, the detection efficiency of the present invention is much higher than that of traditional tools. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] Figure 1It is a structural block diagram of a smart contract code vulnerability detection system based on a deep learning method of the present invention;

[0022] Figure 2 is a schematic diagram of vulnerability distribution in an embodiment of the present invention;

[0023] Figure 3 It is a partial structural schematic diagram of the detection model of the present invention.

[0024] Figure 4 It is a schematic diagram of part of the working process of the detection model of the present invention.

[0025] Figure 5 It is a schematic diagram of the overall workflow of the present invention. DETAILED DESCRIPTION

[0026] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0027] It should be noted that, for the convenience of description, only the parts related to the relevant invention are shown in the drawings. In the absence of conflict, the embodiments and features in the embodiments of the present application can be combined with each other.

[0028] It should be understood that the "system", "device", "unit" and / or "module" used in this application is a method for distinguishing different components, elements, parts, portions or assemblies at different levels. However, if other words can achieve the same purpose, the word can be replaced by other expressions.

[0029] As shown in this application and claims, unless the context clearly indicates an exception, the words "a", "an", "a kind" and / or "the" do not refer to the singular, but also include the plural. Generally speaking, the terms "include" and "comprise" only indicate the inclusion of clearly identified steps and elements, and these steps and elements do not constitute an exclusive list. The method or device may also include other steps or elements. The elements defined by the sentence "includes a..." do not exclude the existence of other identical elements in the process, method, commodity or device that includes the elements.

[0030] In the description of the embodiments of the present application, "plurality" means two or more than two. The following terms "first" and "second" are used for descriptive purposes only and are not to be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Thus, the features defined as "first" and "second" may explicitly or implicitly include one or more of the features.

[0031] In addition, flow charts are used in the present application to illustrate the operations performed by the system according to the embodiments of the present application. It should be understood that the preceding or following operations are not necessarily performed accurately in order. On the contrary, the various steps may be processed in reverse order or simultaneously. At the same time, other operations may also be added to these processes, or a certain step or several steps of operations may be removed from these processes.

[0032] Reference Figure 1 , is a structural block diagram of an optional example of a smart contract code vulnerability detection system based on a deep learning method proposed in the present 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, obtaining a smart contract data set and marking the smart contract data set to obtain a training set;

[0035] Model building module, completing step S2;

[0036] Step S2: Based on the DistilBert pre-training model, introduce the BiLSTM-Attention network to build a detection model;

[0037] Model training module, completing step S3;

[0038] Step S3: training the detection model based on the training set to obtain a trained detection model;

[0039] Code detection module, after completion, step S4;

[0040] Step S4: input the smart contract code into the trained detection model and output the detection result.

[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 in the code so that the processed 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 in the code to remove elements that may interfere with subsequent processing.

[0045] iii. Remove blank lines: The function removes blank lines in the code, making the code more compact and reducing irrelevant content.

[0046] iv. Remove spaces at the beginning of each line: By removing spaces at the beginning of each line, the format of the code is made more uniform.

[0047] v. Clean up code snippets: Further clean up the code snippets, including removing string and character literals, and replacing user-defined functions with FUN+numbers, and variable names with VAR+numbers.

[0048] In some feasible embodiments, the data processing module specifically performs the following processing steps:

[0049] S1.1. Obtain a smart contract dataset (including 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 collects their address (the address after the contract is deployed), tools, lines (lines with possible security issues), nb_vulnerabilities (the number of vulnerabilities found), and source_code (smart contract source code). Among them, tools contains the results of auditing the smart contract using different security analysis tools (such as Mythril). Specifically, the data format of the tools column is a nested dictionary, which contains the results of multiple security analysis tools auditing the smart contract. 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 about all the vulnerabilities detected by the tool. Each vulnerability has a unique identifier (such as 'reentrancy-no-eth', 'Integer Underflow', etc.) as a 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 eight types: 'other', 'unchecked_low_calls', 'arithmetic', 'access_control', 'reentrancy', 'denial_service', 'front_running', and 'time_manipulation'. Figure 2 The distribution of different vulnerabilities in the contracts in the dataset is shown.

[0052] S1.2. According to the vulnerabilities of the source code in the smart contract, the smart contract is marked to obtain a training set.

[0053] Create a new dataset dataset_vuln, which contains the address and source code, as well as the 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] According to the dataset_vuln dataset obtained, the smart contract is labeled. For the source code: if its 'reentrancy' and 'unchecked_low_calls' columns are both True, its 'multi_label' column is labeled as 3; if the 'reentrancy' column is True and the 'unchecked_low_calls' column is False, its 'multi_label' column is labeled as 2; if the 'reentrancy' column is False and the 'unchecked_low_calls' column is true, its 'multi_label' column is labeled as 1; if its 'reentrancy' and 'unchecked_low_calls' columns are both False, its 'multi_label' column is labeled as 0.

[0055] In some feasible embodiments, the model training module includes the following specific processing steps:

[0056] S3.1. Construct model input.

[0057] The preprocessed dataset contains two columns, 'Cleaned Contract' and 'Label'. 'CleanedContract' represents the preprocessed smart contract code, and 'Label' is the corresponding label. Use LabelEncoder in pyhton to convert the label of the label column into an integer code. Then split the training set and the dataset, with the test set accounting for 0.2 of the total data. Use the training data X_train to fit the Tokenizer and build a vocabulary. Then convert the training data X_train and the test data X_test into numerical sequences according to the vocabulary. And use to_categorical to perform one-hot encoding on the labels of the training set and the test set.

[0058] This embodiment sets the length of each sequence to 512. For those sequences that are not long enough, this embodiment uses 0 to fill them. For those sequences that are longer than 512, this embodiment truncates the excess part. And [CLS] and [SEP] are marked at the beginning and end of the sentence. At the same time, this embodiment uses a special encoder to convert the text label of each contract into a digital label.

[0059] S3.2. Oversampling.

[0060] After labeling the data, since the number of contracts marked as "0" is much greater than the number of contracts marked as other numbers, this may cause the model's prediction results to be biased towards the majority class. Here, this embodiment oversamples the training set to solve the problem of unbalanced data set distribution. In this embodiment, the SMOTE class is imported from the imblearn.oversampling module in Python, and an instance of the SMOTE class smote is created. Sampling_strategy = 'auto' is set to automatically determine the number of oversamplings required, and random_state = 42 is set to ensure the repeatability of the results. The smote.fit_resample method is used to oversample the padded training set and the corresponding labels to obtain the resampled training set.

[0061] S3.3, training process.

[0062] The digital labels and segmented word blocks are input into the DistilBert pre-training model. During the training process, the parameters such as the number of iterations (epoch), block size (batch_size), and learning rate (learing_rate) are continuously adjusted, and a reasonable evaluation index calculation function is set to calculate the accuracy, false alarm rate and other data for each label type. During the training process, this embodiment sets the number of training rounds (epoch) to 10; the batch size (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 DistiBert model, an optimized BiLSTM-Attention network is added to the model, and the output of the last hidden layer in the DistilBert pre-trained model is used as the training data set of the BiLSTM-Attention network. This embodiment adds an adapter layer as a bridge between the DistilBert and BiLSTM-Attention layers to compress the 768-dimensional output of DistilBERT to the 128-dimensional input of BiLSTM. The BiLSTM-Attention layer consists of a BiLSTM layer and an Attention layer. The BiLSTM layer contains 128 LSTM units, and dropout and recursive dropout are used to reduce overfitting. The input and output of the Attention layer are both the outputs of the BiLSTM layer. Then the output of the Attention is added to the original output of the LSTM layer to form a residual connection. After the output of the residual connection is normalized, a global average pooling layer is applied to compress the sequence data into a fixed-size vector, reduce the number of parameters of the model, and obtain pooled_output. Finally, a fully connected layer with 64 units and ReLU activation function is created, followed by batch normalization and Dropout layers to prevent overfitting. The output layer contains 4 units and softmax activation function. The structure of the BiLSTM-Attention network in the detection model is referenced Figure 3 .

[0065] Part of the workflow of this detection model refers to Figure 4 .

[0066] In some embodiments, the method further comprises a model testing step, wherein:

[0067] Clean up the code in the test set, including removing version declarations, comments, non-ASCII characters, blank lines, leading spaces, and code snippets.

[0068] For the test set after code cleaning, this embodiment uses a tokenizer to perform a cutting operation, and splits and truncates according to the sequence length, and also marks the beginning and end of the sentence with [CLS] and [SEP].

[0069] Put the processed test set into the model for testing, and evaluate and analyze the test results

[0070] In addition, the specific application detection process of the model refers to the test steps.

[0071] Based on the above scheme, the overall workflow of the present invention refers to Figure 5 .

[0072] The above is a specific description of the preferred implementation of the present invention, but the invention is not limited to the embodiments. Those skilled in the art may make various equivalent modifications or substitutions without violating the spirit of the present invention. These equivalent modifications or substitutions are all included in the scope defined by the claims of this application.

Claims

1. A smart contract code vulnerability detection system based on deep learning method, characterized in that: include: A data processing module, which obtains a smart contract data set and labels the smart contract data set to obtain a training set; The model building module introduces the BiLSTM-Attention network based on the DistilBert pre-training model to build a detection model; A model training module is used to train the detection model based on the training set to obtain a trained detection model; The code detection module inputs the smart contract code into the trained detection model and outputs the detection result.

2. According to claim 1, a smart contract code vulnerability detection system based on deep learning method is characterized in that: The data processing module also includes: Clean the codes in the smart contract dataset.

3. According to claim 2, a smart contract code vulnerability detection system based on a deep learning method is characterized in that: The cleaning method includes removing version declarations, removing comments, removing blank lines, and cleaning up code snippets.

4. According to claim 1, a smart contract code vulnerability detection system based on deep learning method is characterized in that: The process of obtaining a smart contract data set and labeling the smart contract data set to obtain a training set specifically includes: Obtain smart contract data sets and analyze smart contracts to obtain the distribution of vulnerabilities; According to the vulnerabilities of the source code in the smart contract, the smart contract is marked to obtain a training set.

5. According to claim 4, a smart contract code vulnerability detection system based on deep learning method is characterized in that: The process of training the detection model based on the training set to obtain a trained detection model specifically includes: One-hot encode the labels of the training set; Based on a preset sequence length, adjusting the data of the training set to obtain an adjusted training set; Oversampling the adjusted training set 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 a trained detection model.

6. According to claim 5, a smart contract code vulnerability detection system based on deep learning method is characterized in that: Also includes: Set the indicator calculation function to evaluate the precision and false positive rate of all types.

7. According to claim 1, a smart contract code vulnerability detection system based on deep learning method is characterized in that: An adapter layer is provided between the DistilBert pre-trained model and the BiLSTM-Attention network.

8. According to claim 7, a smart contract code vulnerability detection system based on deep learning method is characterized in that: The BiLSTM-Attention network includes a BiLSTM layer and an Attention layer, and the BiLSTM layer includes multiple LSTM units, wherein: Add the output of Attention to the original output of the LSTM unit to form a residual connection; Normalize the output of the residual connection; A global average pooling layer is applied to convert the sequence data into a vector and output through a fully connected layer.

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

  • Industry classification method and device for company brief introduction

    CN115718889A

  • Intelligent contract vulnerability detection method and system

    CN115828258A

  • Shield tunneling speed intelligent prediction method based on hybrid neural network

    CN115983333A