A smart contract vulnerability detection method for protecting a blockchain using a propagation chain

By constructing a propagation chain using the CuVuD model and utilizing a multi-head attention layer and a transformer layer to detect smart contract vulnerabilities, the problem of insufficient adaptability of existing methods is solved, and efficient smart contract vulnerability detection is achieved.

CN117056935BActive Publication Date: 2026-11-24CHONGQING UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310976801.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-08-04
Publication Date
2026-11-24
Estimated Expiration
2043-08-04

AI Technical Summary

Technical Problem

Existing smart contract vulnerability detection methods are unable to adapt to the explosive growth in the number of smart contracts.

Method used

The CuVuD model is adopted, and the source code is parsed into an AST by improving the JoranHonig syntax rules. The propagation chain is constructed, and vulnerability detection is performed using a multi-head attention layer, a LayerNormalization layer, and a transformer layer. Isolated nodes are pruned, and the Sigmoid function is used to output the vulnerability probability.

Benefits of technology

It significantly improves the performance of smart contract vulnerability detection, with substantial improvements in recall, accuracy, and F1 score compared to existing methods, outperforming seven mainstream methods.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117056935B_ABST
    Figure CN117056935B_ABST
Patent Text Reader

Abstract

This invention discloses a method for detecting smart contract vulnerabilities in blockchains using a propagation chain. The method first parses the source code into an AST (Abstract Syntax Tree) to determine the variable sequence, then generates and prunes the contract propagation chain, and finally detects vulnerabilities in the smart contract. This method uses a propagation chain to detect smart contract vulnerabilities in blockchains, significantly improving the vulnerability detection capability and making it more suitable for practical engineering applications.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of smart contract vulnerabilities, and in particular to a smart contract vulnerability detection method that uses a propagation chain to protect the blockchain. Background Technology

[0002] Blockchain vulnerabilities include smart contract vulnerabilities, 51% attacks, and double-spending attacks. Smart contract vulnerabilities are among the most common and serious blockchain vulnerabilities, potentially leading to severe economic and financial losses. According to Bcsec and Slowmist, economic losses due to smart contract vulnerabilities have exceeded billions of dollars. Because smart contracts are pieces of program code, code security issues are inevitable during their design and development. Furthermore, smart contracts deployed on public blockchains are typically exposed to open network environments, further making them vulnerable to attacks. These studies highlight the importance of vulnerability detection and security audits before smart contract deployment, as these can reduce security vulnerabilities and protect digital assets. To date, researchers have developed numerous tools for detecting smart contract vulnerabilities, such as: static analysis-based methods (e.g., SmarkCheck and Slither), formal analysis-based methods (e.g., ZEUS and Securify), fuzzing-based methods (e.g., ContractFuzzer), and symbolic execution-based methods (e.g., Oyente, Osiris, Mythril, and Manticore).

[0003] Currently, numerous scholars have joined the research in this field. Among them, SmartEmbed uses deep learning models to calculate the similarity to known vulnerabilities in smart contracts to detect the existence of vulnerabilities. Sgram uses Oyente for labeling and then combines N-gram language modeling and lightweight static semantic labeling to predict vulnerabilities. Zhuang et al. constructed a relational graph to represent the syntactic and semantic features of smart contracts and used graph neural networks for learning and computation. Huang et al., based on manually labeled datasets, first converted the smart contract bytecode to RGB color, and then used convolutional neural networks to train and predict smart contract security vulnerabilities. Tann et al. used MAIAN to label smart contract security issues and used LSTM to predict potential vulnerabilities.

[0004] However, these smart contract vulnerability detection methods have some drawbacks because they require manual summarization of vulnerability rules and patterns in advance, making them unsuitable for the explosively growing number of smart contracts currently available. Summary of the Invention

[0005] In view of the above-mentioned problems in the existing technology, the technical problem to be solved by the present invention is: how to solve the adaptability problem of smart contract vulnerability detection methods.

[0006] To solve the above-mentioned technical problems, the present invention adopts the following technical solution: a smart blockchain protected by a propagation chain.

[0007] The vulnerability detection method includes the following steps:

[0008] S100: Improved treesitter by using the JoranHonig syntax rules.

[0009] Use treesitter to parse the source code into an AST.

[0010] Represent the leaf nodes of the AST as a set of nodes V = {v1, v2, ..., v...} k}, where v1, v2 and v k These represent the 1st, 2nd, and kth nodes, respectively.

[0011] S200: Set the variable sequence V as a node in the propagation chain.

[0012] Let the edge e of the propagation chain be... <v i v j > represents v in the variable sequence j From v i Or v j By v i Calculations show that v j From v i V is represented by a dashed line. j By v i The calculated value is represented by a solid line, v. i and v j Let i and j represent the i-th and j-th nodes, respectively.

[0013] Add the edges of the nodes to the propagation chain. Let the propagation chain CH(C)=(V,E) represent the propagation chain of the source code C, where V represents the set of nodes and E represents the set of edges, E={e1,e2,...,e1}.

[0014] S300: Constructing and training the CuVuD model:

[0015] S310: The CuVuD model comprises six parts: an input layer, a connection layer, a multi-head attention layer, a LayerNormalization layer, several transformation layers, and a linear layer; the first four input units in the CuVuD input layer will take the token set CT = {ct1, ct2, ..., ct...} of the source code C. mThe set of token positions and the set of variable positions are concatenated into a sequence I1, and the set of token positions and the set of variable positions are concatenated into another sequence I2. I1 and I2 are then converted into an input vector X. 0 As the representation vector of source code C, the 5th input unit is the propagation chain, denoted as CH(C) = (V, E). The 6th input unit is the set of vulnerability key information obtained from source code C, V′ = {v′1, v′2, ..., v′}. n}, where v1, v2 and v i These represent variables that are on the same row as the first, second, and i-th vulnerability key information, respectively.

[0016] In the CuVuD model, in CH(C) according to v′ in V′ i (i∈{1, 2, ..., n}) Find the corresponding node and trim it to v′. i Edges with no association are identified and a new set of edges E′ is obtained.

[0017] Pruning isolated nodes involves removing nodes in CH(C) that are not traversed by edges in E′, thus obtaining a simplified vulnerability propagation chain CH1(C); S320: In the connection layer of the CuVuD model, I1, I2, and CH1(C) are converted into input vector X. 0 ;

[0018] S330: Input vector X 0 The vector X of each transformer layer will be passed through a multi-head attention layer, a layer normalization layer, and several transformer layers, as shown in Equation (1). n-1 After a multi-head attention layer operation, a vector H is generated. n Then, calculate the output vector X according to formula (2). n To generate specific contextual representations;

[0019] H n =LN(MHSA(X) n-1 )+X n-1 )#(1)

[0020] X n =LN(FFN(H n )+H n )#(2)

[0021] Here, MHSA represents a multi-head self-attention operation, FFN is a two-layer feedforward network, and LN represents the LayerNormalization operation.

[0022] S340: After the nth transformer layer, use a Layer Normalization layer for regularization, and use a linear layer and a Sigmoid function to output the probability y that the contract contains a vulnerability.

[0023] y = sigmoid(X) n )#(6)

[0024] S350: The propagation chain is used as the input to the CuVuD model to train the CuVuD model. The trained CuVuD model is obtained when the loss of the CuVuD model no longer decreases.

[0025] S400: For a dataset of smart contracts to be tested, the dataset is processed using methods S100 and S200 to obtain the corresponding propagation chain to be tested. The propagation chain to be tested is then input into the trained CuVuD model, and the trained CuVuD model outputs the probability of vulnerabilities in the dataset of smart contracts to be tested.

[0026] Preferably, step S330 includes the following steps:

[0027] For the nth transformer layer, the output is X. n MHSA(X) n-1 The calculation process for ) is shown in formulas (3)-(5):

[0028] Q i =X n-1 W i Q K i =X n-1 W i K V i =X n-1 W i v #(3)

[0029]

[0030]

[0031]

[0032] In formula 3, X n-1 Q represents the output of n-1 transformer layers. i K i and V iThis represents the three learnable parameter matrices obtained by backpropagation optimization during the training of the CuVuD model, which are mapped to three vectors, namely the query vector W. i Q Key vector W i K Sum vector W i v .

[0033] In formula (4), Represented as K i The transpose of M∈R; |I|×|I| Let M be a mask matrix, where M is a set of values ​​for the i-th and j-th tokens if the i-th token and the j-th token are related. ij =0, otherwise -∞, the mask matrix is ​​calculated as shown in Formula 7; head i Let d represent the i-th attention head. k The dimension of head in multi-head attention.

[0034] In formula (7), for CH1(C), E1 is the edge set {e1, e2, ..., e1}, E′1 is the set representing the relationships between smart contract tokens and variables in the propagation chain, and ct j Represented as the j-th token, <v i ct j > <ct j v i >∈E′1 if and only if variable v i From the ct in the source code token j Determined and node v i query and ct j The node-keys are related. [CLS] is a special marker in S300 indicating the initial position connecting the source code token set CT and the variable set V. [SEP] is a special symbol separating the two sets. `query` represents the query node; `node_key` represents `ct`. j A unique identifier.

[0035] When node v i and node v j There exists a directed edge ( <v i v j If the nodes are either >∈E1) or the nodes are the same (i=j), then queryq vj With node-keyk vi There is a correlation. Otherwise, the attention value will be assigned -∞, and after being calculated by sofmax in formula (4), it will be assigned 0;

[0036] In formula (5), head1 and head m These represent the 1st and mth attention heads, respectively, where m is the number of heads in a multi-head attention system, and d... k For the head dimension in multi-head attention, These are the parameters of the CuVuD model.

[0037] Compared with the prior art, the present invention has at least the following beneficial effects:

[0038] (1) The present invention uses a simpler propagation chain to represent graph information, thereby improving the generalization of the model and solving the problem that the existing technology constructs smart contracts as contract graphs, but the information structure in the contract graphs is too complex and not conducive to the generalization of the model.

[0039] (2) Experimental results show that the vulnerability detection effect of the method of the present invention is significantly better than that of 7 mainstream smart contract vulnerability detection methods, and greatly improves the performance of smart contract vulnerability detection. Attached Figure Description

[0040] Figure 1 This is a simplified flowchart of the method of the present invention.

[0041] Figure 2 This method extracts a specific example diagram of the propagation chain process from the given source code.

[0042] Figure 3 The bar chart shows the improvement of CuVuD's accuracy, recall, and F1 score across 7 baselines. Detailed Implementation

[0043] The present invention will now be described in further detail.

[0044] A method for detecting smart contract vulnerabilities in a blockchain using a propagation chain includes the following steps:

[0045] S100: Improves Treesitter by using JoranHonig syntax rules, thereby overcoming Treesitter's lack of support for the Solidity language.

[0046] Use treesitter to parse the source code into an AST.

[0047] Represent the leaf nodes of the AST as a set of nodes V = {v1, v2, ..., v...} k}, where v1, v2 and v k These represent the 1st, 2nd, and kth nodes, respectively.

[0048] S200: Set the variable sequence V as a node in the propagation chain.

[0049] Let the edge e of the propagation chain be... <v i v j > represents v in the variable sequence j From v i Or v j By v i Calculations show that v j From v i V is represented by a dashed line. j By v i The calculated value is represented by a solid line, v. i and v j Let i and j represent the i-th and j-th nodes, respectively.

[0050] Add the edges of the nodes to the propagation chain. Let the propagation chain CH(C)=(V,E) represent the propagation chain of the source code C, where V represents the set of nodes and E represents the set of edges, E={e1,e2,...,e1}.

[0051] S300: Constructing and training the CuVuD model:

[0052] S310: The CuVuD model comprises six parts: an input layer, a connection layer, a multi-head attention layer, a LayerNormalization layer, several transformation layers, and a linear layer; the first four input units in the CuVuD input layer will take the token set CT = {ct1, ct2, ..., ct...} of the source code C. m The set of token positions and the set of variable positions are concatenated into a sequence I1, and the set of token positions and the set of variable positions are concatenated into another sequence I2. I1 and I2 are then converted into an input vector X. 0 As the representation vector of source code C, the 5th input unit is the propagation chain, denoted as CH(C) = (V, E). The 6th input unit is the set of vulnerability key information obtained from source code C, V′ = {v′1, v′2, ..., v′}. n}, where v′1, v′2 and v′ i These represent variables that are on the same row as the first, second, and i-th vulnerability key information, respectively.

[0053] In the CuVuD model, in CH(C) according to v′ in V′ i (i∈{1, 2, ..., n}) Find the corresponding node and trim it to v′. i Edges with no association are identified and a new set of edges E′ is obtained.

[0054] Pruning isolated nodes involves removing nodes in CH(C) that are not traversed by edges in E′, thus obtaining a simplified vulnerability propagation chain CH1(C); S320: In the connection layer of the CuVuD model, I1, I2, and CH1(C) are converted into input vector X. 0 .

[0055] S330: Input vector X 0 The vector X of each transformer layer will be processed through a multi-head attention layer, a layer normalization layer, and several transformer layers (n=12), as shown in Equation (1). n-1 After a multi-head attention layer operation, a vector H is generated. n Then, calculate the output vector X according to formula (2). n To generate specific contextual representations.

[0056] H n =LN(MHSA(X) n-1 )+X n-1 )#(1)

[0057] X n =LN(FFN(H n )+H n )#(2)

[0058] Here, MHSA represents a multi-head self-attention operation, FFN is a two-layer feedforward network, and LN represents the LayerNormalization operation.

[0059] S340: After the nth transformer layer, use a Layer Normalization layer for regularization, and use a linear layer and a Sigmoid function to output the probability y that the contract contains a vulnerability.

[0060] y = sigmoid(X) n )#(6)

[0061] S350: The propagation chain is used as input to train the CuVuD model. The trained CuVuD model is obtained when the loss no longer decreases. CuVuD uses the Adam optimizer and attempts to find the optimal hyperparameter settings: the learning rate is 2e... -5 The training set has a batch size of 2, the validation set has a batch size of 32, the gradient accumulation step is 1, and the Adam epsilon is e. -8 1e-8. For the splitting of the original dataset, the experiment randomly selected 20% as the training set, 10% as the validation set, and the remaining 70% as the test set, following the literature.

[0062] S400: For a dataset of smart contracts to be tested, the dataset is processed using methods S100 and S200 to obtain the corresponding propagation chain to be tested. The propagation chain to be tested is then input into the trained CuVuD model, and the trained CuVuD model outputs the probability of vulnerabilities in the dataset of smart contracts to be tested.

[0063] Specifically, S330 includes the following steps:

[0064] For the nth transformer layer, the output is X. n MHSA(X) n-1 The calculation process for ) is shown in formulas (3)-(5):

[0065] Q i =X n-1 W i Q K i =X n-1 W i K V i =X n-1 W i v #(3)

[0066]

[0067]

[0068]

[0069] In formula 3, X n-1 Q represents the output of n-1 transformer layers. i K i and V i This represents the three learnable parameter matrices obtained by backpropagation optimization during the training of the CuVuD model, which are mapped to three vectors, namely the query vector W. i Q Key vector W i K Sum vector W i v .

[0070] X n-1 Linearly projected onto a graph by Q i K i and V i In the triplet, Q i K i and V iW respectively i Q W i K W i v Calculate Q i K i and V i These are three learnable parameter matrices obtained through a linear transformation layer and optimized via backpropagation during training.

[0071] To enable the transformer layer to learn graph structures, graph-based masked attention is used to establish relationships between tokens in the smart contract. A mask matrix M is used to implement graph-based masked attention.

[0072] In formula (4), Represented as K i The transpose of M∈R; |I|×|I| Let M be a mask matrix, where M is a set of values ​​for the i-th and j-th tokens if the i-th token and the j-th token are related. ij =0, otherwise -∞, the mask matrix is ​​calculated as shown in Formula 7; head i Let d represent the i-th attention head. k The dimension of head in multi-head attention.

[0073] In formula (7), for CH1(C), E1 is the edge set {e1, e2, ..., e1}, E′1 is the set representing the relationships between smart contract tokens and variables in the propagation chain, and ct j Represented as the j-th token, <vi,ct j > <ct j v i >∈E′1 if and only if variable v i From the ct in the source code token j Determined and node v i query and ct j The node-keys are related. [CLS] is a special marker in S300 indicating the initial position connecting the source code token set CT and the variable set V. [SEP] is a special symbol separating the two sets. `query` represents the query node; `node_key` represents `ct`. j A unique identifier.

[0074] When node v i and node v j There exists a directed edge ( <v i v jIf the nodes are either >∈E1) or the nodes are the same (i=j), then queryq vj With node-keyk vi There is a correlation. Otherwise, the attention value will be assigned -∞, and after being calculated by sofmax in formula (4), it will be assigned 0;

[0075] In formula (5), head1 and head m These represent the 1st and mth attention heads, respectively, where m is the number of heads in a multi-head attention system, and d... k For the head dimension in multi-head attention, These are the parameters of the CuVuD model.

[0076] Experimental Test

[0077] This invention is based on the SmartBugs Wild Dataset. The SmartBugs Wild Dataset is a dataset containing 47,398 smart contracts extracted from the Ethereum network. It was analyzed using SmartBugs, and each smart contract was labeled by professionals to determine whether it contained vulnerabilities. The dataset is also open source online (https: / / github.com / zz8477 / Scruple).

[0078] We compared the CuVuD model with seven state-of-the-art smart contract vulnerability detection methods: three traditional expert-knowledge-based methods (Manticore, Osiris, and Slither) and four deep learning-based methods (GCN, Vanilla-RNN, LSTM, and GRU). We used three widely used evaluation metrics in the field of smart contract vulnerability detection: recall, precision, and F1 score. The calculation methods are shown below:

[0079] Precision=True Positive / (True Positive+False Positive)#(8)

[0080] Recall=True Positive / (True Positive+False Negative)#(9)

[0081] F1=Precision×Recall / 2×(Precision+Recall)#(10)

[0082] The experimental results (Table 1) show that the first three traditional smart contract vulnerability detection methods (Manticore, Osiris, Oyente, Slither, and SmartCheck) achieved the highest recall rate (0.78 for Slither), the lowest (0.50 for Manticore), and an average of 0.60); the highest accuracy (0.82 for Slither), the lowest (0.53 for Osiris), and an average of 0.64; and the highest F1 score (0.84 for Slither), the lowest (0.50 for Manticore), and an average of 0.62. Compared to these methods, CuVuD shows an average relative improvement of 60% in recall, 41% in accuracy, and 50% in F1 score.

[0083]

[0084] Table 1 compares the performance of relevant methods in terms of accuracy, recall, and F1 score.

[0085] The last four deep learning-based smart contract vulnerability detection methods (GCN, Vanilla-RNN, LSTM, and GRU) showed the following recall rates: highest (0.76 for GCN), lowest (0.45 for Vanilla-RNN), and average (0.60 for GRU); highest accuracy (0.68 for GCN), lowest (0.49 for GRU), and average (0.55 for GRU); and highest F1 score (0.72 for GCN), lowest (0.46 for Vanilla-RNN), and average (0.57 for CuVuD). Compared to deep learning-based smart contract vulnerability detection methods, CuVuD showed an average relative improvement of 60% in recall, 64% in accuracy, and 63% in F1 score.

[0086] Therefore, it can be seen that compared with the seven other smart contract vulnerability detection methods, CuVuD achieves an average relative improvement of 58% in recall, 54% in accuracy, and 65% in F1 score. Thus, we can conclude that CuVuD is significantly more effective than the seven mainstream smart contract vulnerability detection methods in detecting vulnerabilities, significantly improving the vulnerability detection results.

Claims

1. A method for detecting smart contract vulnerabilities in a blockchain using a propagation chain protection mechanism, characterized in that, Includes the following steps: S100: Improved treesitter by using JoranHonig syntax rules; Use treesitter to parse the source code into an AST; Representing leaf nodes of AST as a set of nodes wherein , and represent the 1st, 2nd, and kth nodes, respectively. S200: Set the variable sequence to the nodes of the propagation chain; Set the edges of the propagation chain denote the sequence of variables from or by calculated, from denoted by a dashed line, by calculated, denoted by a solid line, denote the ith and jth nodes, respectively; Add the edges of the nodes to the propagation chain, and use the propagation chain. This represents the propagation chain of source code C, where V represents the set of nodes and E represents the set of edges. ; S300: Construct and train the CuVuD model: S310: The CuVuD model comprises six parts: an input layer, a connection layer, a multi-head attention layer, a LayerNormalization layer, several transformation layers, and a linear layer; the first four input units in the CuVuD input layer will use the source code. Token collection and variable set Connect them into a sequence Simultaneously, the set of locations for source code tokens and the set of locations for variables are linked into another sequence. ,Will and Convert to input vector As the representation vector of source code C, the 5th input unit is the propagation chain, represented as... The 6th input unit is from the source code. A collection of key vulnerability information obtained from [the source]. ,in , and These represent variables that are on the same row as the first, second, and i-th vulnerability key information entries, respectively. In the CuVuD model, According to In Find the corresponding node and trim. Find edges that are not related and obtain a new set of edges. ; Pruning isolated nodes is imminent There is no By pruning the nodes through which the edges pass, a simplified vulnerability propagation chain is obtained. ; S320: In the connection layer of the CuVuD model, , and Convert to input vector ; S330: Input vector This will be achieved through a multi-head attention layer, a layer normalization layer, and several transformer layers, as shown in the formula. As shown, the vectors of each transformer layer After a multi-head attention layer operation, a vector is generated. Then follow the formula The calculated output vector To generate specific contextual representations; Wherein, MHSA represents a multi-head self-attention operation, FFN is a two-layer feedforward network, and LN represents the LayerNormalization operation; S340: After the nth transformer layer, use a Layer Normalization layer for regularization, and use a linear layer and a sigmoid function to output the probability that the contract contains vulnerabilities. ; S350: The propagation chain is used as the input to the CuVuD model to train the CuVuD model. When the loss of the CuVuD model no longer decreases, the trained CuVuD model is obtained. S400: For a dataset of smart contracts to be tested, the dataset is processed using methods S100 and S200 to obtain the corresponding propagation chain to be tested. The propagation chain to be tested is then input into the trained CuVuD model, and the trained CuVuD model outputs the probability of vulnerabilities in the dataset of smart contracts to be tested.

2. The smart contract vulnerability detection method using propagation chain protection for blockchain as described in claim 1, characterized in that, S330 includes the following steps: For the nth transformer layer, the output is MHSA ( The calculation process is as follows: - As shown: In formula 3, For the output of n-1 transformer layers, , and This represents the three learnable parameter matrices obtained by backpropagation optimization during the training of the CuVuD model, which are mapped to three vectors, namely the query vectors. Key vector Sum value vector ; In formula (4), Represented as transpose; Let be a mask matrix, where if the i-th token and the j-th token are related, then... Otherwise, it is - The calculation of the mask matrix is ​​shown in Formula 7; This represents the i-th attention head. The head dimension in multi-head attention; In formula (7), for , For the set of edges , To represent the set of smart contract tokens that are related to variables in the propagation chain, Represented as the j-th token, If and only if the variable From the source code token Determined and node query and The node-keys are related; [CLS] is a special marker in S300 indicating the initial position connecting the source code token set CT and the variable set V; [SEP] is a special symbol separating the two sets; query represents the query node; node_key is represented as... A unique identifier; When node and nodes There exists a directed edge ( If the query is for the same node (i=j), then the query... With node-key There is a correlation; otherwise, an attention value will be assigned - After being calculated by formula (4), sofmax is assigned to 0; In formula (5), and Let represent the 1st and mth attention heads, respectively, where m is the number of heads in a multi-head attention system. For the head dimension in multi-head attention, These are the parameters of the CuVuD model.

Citation Information

Patent Citations

  • Intelligent contract vulnerability detection method based on transfer learning

    CN114547621A

  • Synthesis of verification languages

    US6907599B1