A method for detecting re-entrant vulnerabilities of smart contracts based on a twin network

By processing smart contract samples using a twin network architecture, extracting features using Word2vec and LSTM layers, and combining Dropout and Dense layers, the problems of low accuracy and insufficient data in smart contract reentrancy vulnerability detection are solved, achieving efficient and accurate vulnerability detection.

CN114065219BActive Publication Date: 2026-05-01YANGZHOU UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
YANGZHOU UNIV
Filing Date
2021-11-25
Publication Date
2026-05-01

AI Technical Summary

Technical Problem

Existing smart contract vulnerability detection methods, especially reentrancy vulnerability detection, suffer from low detection accuracy, insufficient datasets, and difficulty in effectively utilizing neural network learning, resulting in high security risks for smart contracts.

Method used

We employ a twin network architecture, process smart contract samples using the Word2vec model to generate embedding vectors and label them as positive and negative samples, extract features using LSTM and ReLU layers, improve model robustness by combining Dropout and Dense layers, and use Euclidean distance to calculate similarity for vulnerability detection.

Benefits of technology

It achieves high accuracy and short training time in detecting smart contract reentrancy vulnerabilities, expands the dataset, and improves the accuracy and robustness of detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114065219B_ABST
    Figure CN114065219B_ABST
Patent Text Reader

Abstract

The application discloses a kind of based on twin network's smart contract reentrant vulnerability detection method, comprising, collate original smart contract sample, form the initial sample set, and to the initial sample set is handled, generate the first time processing sample set;Through Word2vec model to the first time processing sample set embedding vector and matrix composition, obtain the second time processing sample set;Let the positive sample and negative sample quantity in the second time processing sample set be consistent, obtain the third time processing sample set, and utilize the third time processing sample set to make dataset;Data set is respectively input neural network A and neural network B, to extract feature A and feature B, and calculate the similarity between feature A and feature B;The similarity of feature A and feature B is compared with threshold value respectively, complete detection;The application can accurately detect smart contract reentrant vulnerability, and expand the size of dataset.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the technical field of smart contract vulnerability detection, and in particular to a smart contract reentrancy vulnerability detection method based on twin networks. Background Technology

[0002] The concept of smart contracts was first proposed by Nick Szabo, who defined them as "a set of digitally defined protocols that outline how the participating parties will fulfill these protocols." Szabo aimed to change traditional contract mechanisms by using cryptographic protocols and digital security mechanisms to create contracts that are logically clear, easily detectable, and with well-defined responsibilities. This would place paper contracts in a decentralized environment. However, due to the limitations of technology at the time, there was no suitable platform to support smart contracts, and a reliable execution environment could not be provided, hindering their widespread adoption. Blockchain technology provides a robust execution environment for smart contracts, and its rise has reshaped the field, resolving previous issues of technological immaturity and lack of application scenarios. With the help of blockchain technology, smart contract technology has begun to truly automate. Blockchain itself possesses characteristics such as decentralization, immutability, traceability, and programmability, providing a mechanism for resolving trust issues in smart contracts.

[0003] Compared to traditional contracts, smart contracts are themselves participants and executors of the contract. Therefore, the execution process does not require third-party involvement; the contract executes automatically once the conditions are met. Smart contracts reduce the cost of execution and decrease distrust among contract participants. Blockchain technology based on smart contracts is widely used in finance, energy, and the Internet of Things (IoT). However, with the rapid development of smart contracts, their number and complexity are constantly increasing, leading to a rise in security issues and significant losses. Smart contracts involve digital assets, and due to the immutable nature of blockchain, once deployed on the chain, they cannot be changed, thus facing a more severe challenge than traditional software. In 2016, a vulnerability in The DAO smart contract resulted in a loss of $55 million worth of Ether. In 2017, a vulnerability in the Parity wallet smart contract resulted in a loss of over $30 million worth of Ether. These security issues have severely hindered the development of blockchain and created a crisis of trust in smart contracts among users.

[0004] Faced with such significant losses, researchers have proposed numerous methods, such as formal verification, symbolic execution, dynamic execution, static analysis, taint analysis, and fuzz testing. Symbolic execution includes dynamic and static symbolic execution. These methods have been put into practice, for example, the static analysis framework Slither and the Mythril analysis tool. However, because these tools rely on rigid logical rules, which are often simple and easily circumvented, these methods are not suitable for general smart contracts. With the development of neural networks, their ability to learn and update themselves through large datasets has attracted attention, and more and more scholars are researching neural network-based smart contract detection tools. By incorporating attention mechanisms into deep learning, the accuracy of feature extraction in the model can be improved, thereby increasing the final model's precision. However, these models do not consider that neural network learning for smart contracts requires a large set of samples, while the number of smart contracts is relatively small. Furthermore, to achieve complex functionality, functions within smart contracts call each other, and vulnerabilities may arise from a single call. Manual labeling is difficult and prone to errors. Summary of the Invention

[0005] The purpose of this section is to outline some aspects of embodiments of the present invention and to briefly describe some preferred embodiments. Simplifications or omissions may be made in this section, as well as in the abstract and title of this application, to avoid obscuring the purpose of these documents; however, such simplifications or omissions should not be construed as limiting the scope of the invention.

[0006] In view of the aforementioned existing problems, the present invention is proposed.

[0007] To address the aforementioned technical problems, this invention provides the following technical solution: It includes organizing original smart contract samples to form an initial sample set, processing the initial sample set to generate a first-processed sample set; embedding vectors into the first-processed sample set using a Word2vec model to form a matrix, obtaining a second-processed sample set; ensuring the number of positive and negative samples in the second-processed sample set is equal to obtain a third-processed sample set, and using the third-processed sample set to create a dataset; inputting the dataset into neural network A and neural network B respectively to extract features A and B, and calculating the similarity between features A and B; comparing the similarity between features A and B with a threshold respectively to complete the detection.

[0008] As a preferred embodiment of the smart contract reentrancy vulnerability detection method based on twin networks described in this invention, the method includes: processing the initial sample set by: identifying key variables and key function calls related to smart contract reentrancy vulnerabilities; extracting smart contracts from the initial sample set, deleting blank lines, non-ASCII characters, comments, and non-key segments from the smart contracts, and extracting code lines related to smart contract reentrancy vulnerabilities from them, and compiling them into code segments; and using the code segments as the sample set for the first processing.

[0009] As a preferred embodiment of the smart contract reentrancy vulnerability detection method based on twin networks described in this invention, the sample set for the second processing includes: the sample set for the first processing is labeled as label 0 and label 1, respectively, wherein label 0 indicates that it does not contain reentrancy vulnerabilities, and label 1 indicates that it contains reentrancy vulnerabilities; the sample set for the first processing is divided into a label sequence; wherein the label sequence includes keywords, operations, rule change variables, and symbols; the labeled sample set for the first processing and the label sequence are input into a Word2vec model, the Word2vec model generates corresponding labels, and the labels are integrated to obtain the sample set for the second processing.

[0010] As a preferred embodiment of the smart contract reentrancy vulnerability detection method based on twin networks described in this invention, the sample set for the third processing includes: calculating the number of samples in the sample set of the second processing that contain reentrancy vulnerabilities and those that do not; randomly selecting the same number of samples from the larger sample set as the smaller sample set; and forming the sample set for the third processing from the samples extracted from the larger sample set and the smaller sample set.

[0011] As a preferred embodiment of the smart contract reentrancy vulnerability detection method based on twin networks described in this invention, the method includes: dividing the sample set processed in the third step into two categories: label 1 and label 0; selecting a sample from label 0 or label 1 as a reference class using a control variable n and a random function random(0,1), and correspondingly selecting a sample from label 1 or label 0 as a test class; repeating this process until each sample in both sample sets has been used as a reference class; and forming positive and negative sample pairs from the reference class and the test class to create a sample set, thus forming the dataset; wherein the positive sample is the reference class and the negative sample is the test class.

[0012] As a preferred embodiment of the smart contract reentrancy vulnerability detection method based on twin networks described in this invention, the method further includes: randomly selecting sample pairs from the positive and negative sample pairs, inputting the reference class into neural network A, and inputting the test class into neural network B; extracting features through the LSTM layers of neural network A and neural network B respectively; nonlinearizing features A and B through a ReLU layer; after the data passes through a Dense layer, it passes through a ReLU layer and a Dropout layer again to increase the robustness of the model, with the data dimension being (none, 200); accelerating the convergence of neural networks A and B using the Dropout layer and the Dense layer, and then mapping the outputs of neural networks A and B to the (0, 1) interval through the SoftMax function.

[0013] As a preferred embodiment of the smart contract reentrancy vulnerability detection method based on twin networks described in this invention, the similarity includes calculating the similarity using Euclidean distance.

[0014] As a preferred embodiment of the smart contract reentrancy vulnerability detection method based on twin networks described in this invention, it further includes: the loss function used in the training of neural networks A and B is:

[0015]

[0016] The margin represents the set threshold, y=0 indicates that the two samples are not similar, y=1 indicates that the two samples are similar, and N refers to the batch size, which is the batch size in machine learning.

[0017] The beneficial effects of this invention are as follows: This invention uses a twin network to detect smart contract reentrancy vulnerabilities, resulting in short training time and high accuracy; This invention expands the size of the dataset by constructing positive and negative sample pairs, so that each data in the dataset can serve as a reference class and a test class. Attached Figure Description

[0018] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort. Wherein:

[0019] Figure 1 This is a schematic diagram of the overall structure of the smart contract reentrancy vulnerability detection method based on twin networks according to the first embodiment of the present invention;

[0020] Figure 2This diagram illustrates the testing and training steps of the smart contract reentrancy vulnerability detection method based on twin networks as described in the first embodiment of the present invention.

[0021] Figure 3 This is the final training result of the smart contract reentrancy vulnerability detection method based on twin networks described in the second embodiment of the present invention. Detailed Implementation

[0022] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.

[0023] Example 1

[0024] like Figures 1-2 As shown, this is the first embodiment of the present invention, which provides a method for detecting smart contract reentrancy vulnerabilities based on twin networks, including:

[0025] S1: Organize the original smart contract samples to form the initial sample set, and process the initial sample set to generate the first processed sample set.

[0026] Organize the original smart contract samples, and combine the smart contract samples containing reentrancy vulnerabilities and those not containing reentrancy vulnerabilities into the initial sample set;

[0027] The initial sample set is processed by removing statements in the smart contract samples that are not related to the reentrancy vulnerability, extracting code snippets related to the smart contract reentrancy vulnerability from the initial sample set, and organizing them into the first sample set for processing.

[0028] S2: The Word2vec model is used to embed vectors into the sample set processed in the first process and form a matrix to obtain the sample set processed in the second process.

[0029] The sample set processed in the first time is labeled as label 0 and label 1, respectively, where label 0 indicates that there is no reentrancy vulnerability and label 1 indicates that there is a reentrancy vulnerability;

[0030] The sample set processed in the first step is divided into a label sequence; the label sequence includes keywords, operations, rule-changing variables, and symbols.

[0031] The sample set and label sequence of the first processing are input into the Word2vec model, the Word2vec model generates corresponding labels, and the labels are integrated to obtain the sample set of the second processing.

[0032] S3: Make the number of positive and negative samples in the sample set of the second processing the same, obtain the sample set of the third processing, and use the sample set of the third processing to create a dataset.

[0033] Calculate the number of samples in the sample set processed in the second step that contain reentrancy vulnerabilities and the number that do not.

[0034] Randomly select the same number of samples from the larger sample set as from the smaller sample set;

[0035] The sample set for the third processing will be composed of samples drawn from the larger sample set and the smaller sample set.

[0036] S4: Input the dataset into neural network A and neural network B respectively to extract features A and B, and calculate the similarity between features A and B.

[0037] The sample set processed in the third step is divided into two categories: labeled 1 and labeled 0.

[0038] By controlling the variable n and using the random function random(0,1), a sample is drawn from label 0 or label 1 as the reference class, and a sample is drawn from label 1 or label 0 as the test class.

[0039] Repeat this process until every sample in both sample sets has been used as a reference class, at which point the loop ends.

[0040] The reference class and the test class are combined into positive and negative sample pairs, and then a sample set is formed to create a dataset.

[0041] Among them, positive samples are the reference class, and negative samples are the test class.

[0042] Randomly select sample pairs from positive and negative sample pairs, input the reference class into neural network A, and input the test class into neural network B;

[0043] Feature extraction is performed using LSTM layers of neural networks A and B, respectively;

[0044] The ReLU layer is used to non-linearize features A and B;

[0045] After passing through the Dense layer, the data goes through the ReLU layer and the Dropout layer again to increase the robustness of the model. The data dimension is (none, 200).

[0046] The Dropout and Dense layers are used to accelerate the convergence of neural networks A and B. Then, the outputs of neural networks A and B are mapped to the (0, 1) interval using the SoftMax function.

[0047] The similarity is calculated using Euclidean distance.

[0048] S5: Compare the similarity of feature A and feature B with the threshold respectively to complete the detection.

[0049] Example 2

[0050] To verify the effectiveness of the techniques used in this method, this embodiment selects a traditional neural network model, traditional detection tools, and this method for comparative testing. The experimental results are compared using scientific methods to verify the real effectiveness of this method.

[0051] The dataset used in this embodiment contains a dataset of reentrancy vulnerabilities for 1671 code snippets, of which 197 are vulnerable code snippets and 1273 are not easily attacked. After processing the dataset, the number of training samples was expanded to 1836 and the number of test samples was expanded to 941.

[0052] This method uses Admax as the optimizer during neural network training, with a default learning rate of 0.002. Due to the limited dataset size, 20 epochs were used during training. To achieve faster results, a batch size of 64 was used, and training was performed directly on a CPU (i7-8700k), running Windows 10 with 32GB of RAM. The Long Short-Term Memory (LSTM) artificial neural network model used a dropout rate of 0.5, as this generates the most randomly generated network structures, which is beneficial for enhancing the generalization ability of the method. After training, to ensure the objectivity of the results, multiple training iterations were performed on the test set, and the average was taken after 40 tests. The evaluation criterion was the accuracy of the method on the test samples. The results are as follows: Figure 3 As shown, the accuracy of this method is 93.3%, with the highest being 91.45% and the lowest being 95.18% out of 40 tests. Compared with currently proposed neural network methods, this method has higher accuracy in detecting smart contract reentrancy vulnerabilities. The specific results are shown in Table 1.

[0053] Table 1: Comparison of Siamese Network + LSTM and Neural Network-based Tools

[0054]

[0055]

[0056] Compared with traditional smart contract vulnerability detection tools, this method has higher accuracy in detecting smart contract reentrancy vulnerabilities, as shown in Table 2.

[0057] Table 2: Accuracy comparison results of this method and non-neural network-based tools.

[0058] Model / Detection Tools Accuracy of model / testing tools on the testing machine This method 93.30% Security 53.30% SmartCheck 52.00% Mythril 60.00% Oyente 71.50%

[0059] As shown in Tables 1 and 2, this method has certain advantages over traditional neural network models and detection tools in detecting reentrancy vulnerabilities. To better compare the model performance, in addition to the accuracy mentioned above, we will use the following metrics: False Positive Rate (FPR), False Negative Rate (FNR), Recall or True Positive Rate (TPR), Precision (PRE), and F1-score (F1). The False Positive Rate (FPR) represents the probability that a contract with a reentrancy vulnerability is identified as vulnerability-free; the Recall Rate represents the probability that a contract without a reentrancy vulnerability is identified as vulnerability-free; the False Negative Rate (FNR) represents the probability that a smart contract without a reentrancy vulnerability is identified as having a vulnerability; the Precision (PRE) represents the probability that a smart contract identified as vulnerability-free actually does not have a reentrancy vulnerability; and the F1-score is the harmonic mean of precision and recall, a metric used in classification problems. The calculation methods for the five metrics are shown in the following formulas:

[0060]

[0061]

[0062] Wherein, FN represents a sample that is judged as negative but is actually a positive sample; FP represents a sample that is judged as positive but is actually a negative sample; TN represents a sample that is judged as negative and is actually a negative sample; and TP represents a sample that is judged as positive and is actually a positive sample.

[0063] We selected the last training data of the model from the comparative experiments in the previous group and calculated the metrics. The final results are shown in Table 3.

[0064] Table 3: Comparison of evaluation scores between this method and neural network-based tools.

[0065] Model ACC FPR FNR TPR PRC F1 This method 93.42% 7.02% 6.14% 93.86% 93.04% 93.44% Twin Network + RNN 91.00% 9.65% 8.33% 91.67& 90.48% 91.07% LSTM 81.91% 27.62% 8.57% 91.43% 76.80% 83.48% RNN 78.35% 25.00% 18.26% 81.74% 76.42% 78.99% BLSTM-ATT 88.47% 8.57% / 88.48% 88.50% 88.26%

[0066] Table 3 clearly shows that this method performs well across all metrics and can effectively improve classification performance.

Claims

1. A method for detecting reentrancy vulnerabilities in smart contracts based on twin networks, characterized in that: include, Organize the original smart contract samples to form the initial sample set, and process the initial sample set to generate the first processed sample set; The Word2vec model is used to embed vectors into the sample set processed in the first process and form a matrix to obtain the sample set processed in the second process. Make sure the number of positive and negative samples in the sample set after the second processing is the same, and obtain the sample set after the third processing. Use the sample set after the third processing to create a dataset. The dataset is input into neural network A and neural network B respectively to extract features A and B, and the similarity between features A and B is calculated. The similarity between feature A and feature B is compared with a threshold to complete the detection. Randomly select sample pairs from positive and negative sample pairs, input the reference class into neural network A, and input the test class into neural network B; Feature extraction is performed using LSTM layers of neural networks A and B, respectively; The ReLU layer is used to non-linearize features A and B; After passing through the Dense layer, the data goes through the ReLU layer and the Dropout layer again to increase the robustness of the model. The data dimension is (none, 200). The Dropout layer and Dense layer are used to speed up the convergence of neural networks A and B. Then, the outputs of neural networks A and B are mapped to the (0, 1) interval through the SoftMax function. The similarity is calculated using Euclidean distance. The loss function used for training neural networks A and B is: Where d represents the Euclidean distance between two samples, which can be expressed as the similarity between the two samples, margin represents the set threshold, y=0 indicates that the two samples are not similar, y=1 indicates that the two samples are similar, and N refers to the batch size, that is, the batch size in machine learning.

2. The smart contract reentrancy vulnerability detection method based on twin networks as described in claim 1, characterized in that: This includes processing the initial sample set, Identify the key variables and key function calls associated with the smart contract reentrancy vulnerability; Smart contracts are extracted from the initial sample set. Blank lines, non-ASCII characters, comments, and non-critical fragments in the smart contracts are removed. Code lines related to smart contract reentrancy vulnerabilities are extracted from them and compiled into code snippets. The code snippet was used as the sample set for the first processing.

3. The smart contract reentrancy vulnerability detection method based on twin networks as described in claim 2, characterized in that: The sample set for the second processing includes, The sample set processed in the first step is labeled as label 0 and label 1, respectively, where label 0 indicates that there is no reentrancy vulnerability and label 1 indicates that there is a reentrancy vulnerability. The sample set processed in the first step is divided into a label sequence; wherein, the label sequence includes keywords, operations, rule change variables, and symbols; The sample set and label sequence of the first processing are input into the Word2vec model, the Word2vec model generates corresponding labels, and the labels are integrated to obtain the sample set of the second processing.

4. The smart contract reentrancy vulnerability detection method based on twin networks as described in claim 3, characterized in that: The sample set for the third processing includes, Calculate the number of samples in the sample set processed in the second step that contain reentrancy vulnerabilities and the number that do not. Randomly select the same number of samples from the larger sample set as from the smaller sample set; The sample set for the third processing will be composed of samples drawn from the larger sample set and the smaller sample set.

5. The smart contract reentrancy vulnerability detection method based on twin networks as described in claim 3 or 4, characterized in that: include, The sample set processed in the third step is divided into two categories: labeled 1 and labeled 0. By controlling the variable n and using the random function random(0,1), a sample is drawn from label 0 or label 1 as the reference class, and a sample is drawn from label 1 or label 0 as the test class. Repeat this process until every sample in both sample sets has been used as a reference class, at which point the loop ends. The reference class and the test class are combined into positive and negative sample pairs, and a sample set is formed to create the dataset. Wherein, positive samples are the reference class and negative samples are the test class.

Citation Information

Patent Citations

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

    CN111898134A

  • Solidiity smart contract similarity detection method and system

    CN113268732A