An ethereume smart contract vulnerability detection method and system based on graph attention

CN117892310BActive Publication Date: 2026-10-27ZHEJIANG UNIV OF TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410018087.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-01-05
Publication Date
2026-10-27
Estimated Expiration
2044-01-05

AI Technical Summary

Technical Problem

智能合约在区块链上运行,它们的代码和状态存储在账本中,一旦部署就无法进行升级和修改,因此存在的漏洞成为了一项现实挑战,可能严重威胁区块链应用的安全性和可信度

Benefits of technology

[0042](1)对不同的漏洞类型,提出了漏洞图,通过漏洞图快速聚集到能发生漏洞的位置,在检测出漏洞的时候可以提供漏洞出现的具体位置;

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117892310B_ABST
    Figure CN117892310B_ABST
Patent Text Reader

Abstract

A kind of method and system for detecting vulnerabilities of Ethereum smart contract based on graph attention, the method includes: S1: analyze the cause of vulnerability of Ethereum smart contract and obtain the abstract syntax tree of smart contract code, and then obtain the vulnerability type of code through a voting system;S2: according to the mode of generating vulnerability of the analyzed smart contract, different vulnerabilities are combined into vulnerability graph according to their causes and abstract syntax tree;S3: according to the vulnerability graph generated in S2, use the multi-layer graph attention classification method to quickly and accurately identify vulnerabilities.The present application can not only adapt to various vulnerability types, but also provide more suggestions for code auditors on vulnerability detection, thereby saving audit time.The present application can significantly improve the speed of vulnerability detection, reduce the false positive rate, improve the accuracy of detection, and show generalization performance in vulnerability detection of different smart contracts, suitable for large-scale contract vulnerability detection tasks.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention pertains to Ethereum smart contract vulnerability detection and deep learning in the field of network security, specifically involving a method and system for Ethereum smart contract vulnerability detection based on graph attention. Background Technology

[0002] Smart contracts are a key application of blockchain technology; however, they are not invincible. Running on the blockchain, their code and state are stored in the ledger, and once deployed, they cannot be upgraded or modified. Therefore, vulnerabilities pose a real challenge and can seriously threaten the security and trustworthiness of blockchain applications. These vulnerabilities can be caused by programming errors, design flaws, or complex interoperability issues. To date, there have been several serious incidents caused by smart contract vulnerabilities, such as the reentrancy vulnerability in TheDao incident and the attack on the cross-chain protocol Poly Network.

[0003] To prevent attacks exploiting smart contract vulnerabilities, developers should follow secure coding guidelines and carefully review potentially vulnerable code before deploying smart contracts. Secure smart contracts help ensure reliable execution and reduce the loss of digital assets. Assessing the security of smart contracts requires a deep understanding of their execution logic, and developers need to invest significant effort and attention in identifying vulnerable code. Therefore, automated vulnerability detection methods are widely used in the smart contract field, such as static analysis methods (symbolic execution, pattern matching) and dynamic analysis methods (fuzz detection, taint analysis, etc.). Currently, deep learning methods have demonstrated superior detection capabilities in numerous vulnerability detection experiments. This approach utilizes neural networks and large-scale datasets to identify complex vulnerability patterns, improving the ability to detect potential vulnerabilities. Summary of the Invention

[0004] This invention aims to address the aforementioned shortcomings in existing technologies by proposing a method and system for detecting Ethereum smart contract vulnerabilities based on graph attention.

[0005] This invention uses a graph-structured representation of the source code or bytecode of Ethereum smart contracts and employs a multi-layered attention mechanism to effectively capture the complex relationships between multiple elements within the contract, thereby identifying potential vulnerability patterns. This invention is adaptable to various vulnerability types, such as reentrancy attacks, integer overflows, and unchecked external input, and can provide code reviewers with more suggestions on vulnerability detection, thus saving review time. This invention significantly improves vulnerability detection speed, reduces false positive rates, and enhances detection accuracy. Furthermore, it demonstrates generalization performance across different smart contracts, making it suitable for large-scale contract vulnerability detection tasks.

[0006] To achieve the above objectives, the present invention provides the following technical solution: The present invention provides a method for detecting Ethereum smart contract vulnerabilities based on graph attention, comprising:

[0007] S1: Analyze the causes of Ethereum smart contract vulnerabilities and obtain the smart contract code and its abstract syntax tree, and then use a voting system to determine the vulnerability types in the code;

[0008] S2: Based on the analysis of the pattern of smart contract vulnerability generation, different vulnerabilities are combined with the abstract syntax tree according to their causes to form a vulnerability diagram;

[0009] S3: Based on the vulnerability graph generated in S2, use a multi-layer graph attention classification method to quickly and accurately identify vulnerabilities.

[0010] Preferably, step S1 includes:

[0011] S1.1: By analyzing technical documents such as the syntax description and code characteristics of Ethereum smart contracts, and combining them with the descriptions of vulnerabilities on publicly available smart contract vulnerability platforms, data collection and organization of smart contract source code are carried out.

[0012] S1.2: Based on the smart contract code collected in step S1.1, the abstract syntax tree of different versions of smart contract code is standardized through the official interface and Solidity version description;

[0013] S1.3: Based on the smart contracts collected in step S1.1, the collected code is voted on using various vulnerability detection tools such as Slither to obtain vulnerability labels for the smart contracts.

[0014] The smart contract code vulnerability entity classification rules were obtained from larger security websites;

[0015] The abstract syntax tree generated by the smart contract code is used to obtain the spatial structure inside the smart contract source code. Standardizing the abstract syntax tree is to ensure that different versions of the smart contract are in the same space.

[0016] Preferably, step S2 includes:

[0017] S2.1: Based on the principle of vulnerability occurrence, extract the core nodes, core statements, core blocks, and core intervals where the vulnerability occurs. Identify the node types on the abstract syntax tree and connect them to obtain the vulnerability graph. ,in This indicates that the edge weight is 1;

[0018] S2.2: Vulnerability Diagram By assigning different weights to edges connecting nodes of different types, a new vulnerability graph with different edge weight relationships can be obtained. .

[0019] The core nodes are statements that may lead to vulnerabilities. For example, a reentrancy vulnerability is caused by the `call` and `value` statements in the `call.value` statement.

[0020] The core statement is a sentence that conforms to the grammatical structure where the core node is located. Taking into account the input and output of functions, the effects of preceding functions and global variables, the variables used in the core statement and the local and global variables that have undergone variable passing are all marked as core statement nodes.

[0021] The core block is the code block containing the core statement. Variables used by the core block and local and global variables that have been passed are all marked as core block nodes.

[0022] The core area refers to the entire smart contract;

[0023] The edge weight values ​​assign different weights to edges within different levels. From top to bottom, there are four types: 2.0x, 1.5x, 1.25x, and x (x>0). The default value is x=1, thus resulting in different edge types. , , and ;

[0024] The vulnerability diagram Its node V is a high-dimensional vector obtained by encoding the text of the node.

[0025] The vulnerability diagram ,in V is a graph node;

[0026] Preferably, step S3 includes:

[0027] S3.1: The completed vulnerability graph Save to the database;

[0028] S3.2: Vulnerability graphs are analyzed using a multi-layer graph attention model. Classify vulnerabilities.

[0029] The multi-layer graph attention model calculates the attention cross-relation coefficients between different nodes i and j. ,

[0030] (1)

[0031] in It is a node The neighbors can be obtained by concatenating the self-attention features from multiple heads.

[0032] (2)

[0033] in Indicates splicing, This represents the attention coefficient of the k-head. Let represent the input weight matrix of the k-head linear layer. The final result y can be obtained, where y = ...

[0034] (3)

[0035] Ultimately, the accuracy of vulnerability detection is measured using the F1 score.

[0036] This invention provides an Ethereum smart contract vulnerability detection system based on graph attention, including a dataset collection module, a vulnerability graph construction module, and a vulnerability detection module.

[0037] The dataset collection module is used to collect smart contract code and its abstract syntax tree, and then obtain the types of vulnerabilities in the code through a voting system;

[0038] The vulnerability graph construction module is used to construct corresponding abstract syntax trees and vulnerability graph representations from the received smart contract data based on its syntax and code characteristics.

[0039] The vulnerability detection module uses a multi-layer graph attention model to analyze the constructed graph structure to detect vulnerabilities in smart contracts. It accurately identifies various vulnerability types, such as reentrancy attacks and integer overflows, based on vulnerability characteristics.

[0040] This invention collects smart contract datasets from open-source websites using a dataset collection module, generates vulnerability graphs from the collected datasets using a vulnerability graph construction module, and then feeds the resulting vulnerability graphs to a vulnerability detection module to check whether the code contains vulnerabilities.

[0041] This invention uses a graph attention-based method for vulnerability detection, which has the following advantages compared to traditional detection methods and tools:

[0042] (1) For different vulnerability types, vulnerability maps are proposed. The vulnerability maps can be used to quickly gather the locations where vulnerabilities may occur, and can provide the specific location of the vulnerability when it is detected.

[0043] (2) Using a graph attention-based detection method, the detection time for each piece of code is basically fixed, which is faster than traditional detection methods.

[0044] (3) Using a graph attention-based detection method, the vulnerability graph can be converged quickly by different weights, resulting in lower training costs and higher accuracy. Attached Figure Description

[0045] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in 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.

[0046] Figure 1 A flowchart of the method provided in an embodiment of the present invention;

[0047] Figure 2 This is a schematic diagram of the system structure provided in an embodiment of the present invention;

[0048] Figure 3 Example diagram of the voting method provided in the embodiment of the present invention;

[0049] Figure 4 This is an example diagram of vulnerability graph construction provided in an embodiment of the present invention;

[0050] Figure 5 An example diagram of the detection model provided in an embodiment of the present invention. Detailed Implementation

[0051] The technical solution of the present invention will be clearly and completely described below with reference to the embodiments. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0052] To make the above-mentioned objects, features and advantages of the present invention more apparent and understandable, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments.

[0053] Example 1

[0054] Reference Figure 1 This embodiment provides a graph attention-based Ethereum smart contract vulnerability detection method, including the following steps:

[0055] S1: Analyze the causes of Ethereum smart contract vulnerabilities and obtain the smart contract code and its abstract syntax tree, and then use a voting system to determine the vulnerability types in the code;

[0056] S2: Based on the analysis of the generation patterns of smart contract vulnerabilities, different vulnerabilities are combined with abstract syntax trees according to their causes to form a vulnerability graph;

[0057] S3: Based on the vulnerability graph generated in S2, vulnerabilities are quickly and accurately identified using a multi-layer graph attention classification method. The vulnerability detection system includes: a dataset collection module, a vulnerability graph construction module, and a vulnerability detection module.

[0058] The data collection module collects Ethereum smart contracts and obtains their abstract syntax trees (ASTs). The smart contract source code data is primarily collected from the open-source community. Based on the collected smart contract dataset n, the AST of the dataset code is obtained. , Indicates the first An abstract syntax tree for each smart contract. The abstract syntax trees of different versions of smart contracts vary to some extent. This is achieved by obtaining the abstract syntax trees of all smart contracts through the compiler. Afterwards, Normalization can result in a set of normalized nodes. , Indicates the first The name of each node, for After standardization, , All nodes within are composed of sets This represents the set of normalized abstract syntax trees. Data preprocessing is performed, which involves training an RNN network to process the node feature set. By converting text types into vector pattern representations, we can obtain a set of normalized abstract syntax trees. The smart contract vulnerability detection module obtains vulnerability tags from the dataset, such as... Figure 3 As shown, dataset vulnerability labels can be obtained using U voting tools. , , No. A set of tags corresponding to a smart contract ,in , This indicates that the smart contract corresponds to the first A tag for each vulnerability type. It means there are no vulnerabilities. This indicates that there is a vulnerability. Figure 3 The voting tools shown are only a partial example for demonstration purposes.

[0059] The vulnerability graph construction module is an abstract syntax tree. This is combined with vulnerability information extracted from the code. As an abstract syntax tree (AST), its tree-like structure means that excessively deep paths can lead to the loss of node information; therefore, the AST is... Convert into the corresponding vulnerability diagram , ,in express The corresponding graph network, Represents graph nodes. This indicates an edge weight of 1. Compared to a tree structure, where nodes in a graph are nodes in a tree and edges represent hierarchical relationships, the nodes in a graph have equal weights, which is beneficial for learning the structure. This can be achieved through understanding the vulnerability graph. Observation reveals that, in the diagram As a simply connected graph, it lacks logical connections between nodes. However, most vulnerabilities arise from the coupling of multiple variables, rather than from a single node. Figure 4 This is a schematic diagram of contract subgraph extraction for reentrancy vulnerabilities. The specific code that generates the vulnerability is `call.value`. However, if the state of the variable changes and enters the `if` branch to trigger the vulnerable code, the reentrancy vulnerability will not occur. Therefore, when detecting reentrancy vulnerabilities, in addition to considering the core critical code, it is also necessary to consider the preceding paths that may trigger the vulnerability. Since the vectors referencing the core code may change state in different scopes, different reference vectors need to be extended outward to include more states. Therefore, a graph augmentation processing method is proposed. By defining the importance of variables, four different layers are proposed: core nodes, core statements, core blocks, and core intervals. For example, `call` and `value` are core nodes, the `require()` statement containing `call.value` is a core statement, the `if-else` statement is a core block, and the current contract is a core interval. To differentiate the importance of nodes, edges between different levels are assigned different weights, from top to bottom: 2.0x, 1.5x, 1.25x, and x (x>0), for a total of four types. Edges between core nodes have a weight of 2.0x, edges between core statements have a weight of 1.5x, edges between core blocks have a weight of 1.25x, the core interval is the current contract, and all other edges have a base weight of 1.0x. The weights of edges between different levels are calculated based on the minimum weight of the edges within the two levels, with a default x=1. This results in four edge types: , , and Considering the impact of function input / output, pre-functions, and global variables, variables used in core statements and those that have undergone variable passing are also marked as being within core statements; similarly, variables used in core blocks and those that have undergone variable passing are also marked as being within core blocks. By converting code of varying importance into graphs, a vulnerability diagram can be obtained. ,in , Indicates the first The graph network corresponding to each contract.

[0060] The vulnerability identification module uses a multi-layer graph attention model to analyze vulnerability graphs. Classify them, specifically as follows: Figure 5 As shown. The multi-layer graph attention model used in this example is GAT, which contains a multi-layer self-attention mechanism that can notice the corresponding vulnerability location through edge relationships, and finally achieve the identification of graph vulnerabilities.

[0061] The input to GAT is , , The number of nodes. The number of features. Indicates that the input is Each node of the nodes Each feature, output , indicating output Each node of the nodes Features. Computation node. Attention coefficient , ,in It is a node Neighbors The weight matrix is ​​parameterized from the weight matrix. Then use The function is in The attention cross-correlation coefficient is obtained from all choices. ,

[0062] (1)

[0063] Using multi-head attention is beneficial for feature recognition. Feature concatenation can be used to obtain...

[0064] (2)

[0065] in Indicates splicing, This represents the attention coefficient of the k-head. This represents the input weight matrix of the k-head linear layer. Figure 4 A 3-layer GAT awareness model was used, which outputs labels after passing through a Pooling layer and an MLP layer to detect whether there are vulnerabilities.

[0066] Example 2

[0067] Reference Figure 2 This embodiment provides an Ethereum smart contract vulnerability detection system based on graph attention, including a dataset collection module, a vulnerability graph construction module, and a vulnerability detection module.

[0068] The dataset collection module is used to collect smart contract code and its abstract syntax tree, and then obtain the types of vulnerabilities in the code through a voting system;

[0069] The vulnerability graph construction module is used to construct corresponding abstract syntax trees and vulnerability graph representations from the received smart contract data based on its syntax and code characteristics.

[0070] The vulnerability detection module uses a multi-layer graph attention model to analyze the constructed graph structure to detect vulnerabilities in smart contracts. It accurately identifies various vulnerability types, such as reentrancy attacks and integer overflows, based on vulnerability characteristics.

[0071] This invention not only adapts to various vulnerability types, such as reentrancy attacks, integer overflows, and unchecked external input, but also provides code reviewers with more suggestions on vulnerability detection, thereby saving review time. This invention significantly improves vulnerability detection speed, reduces false positive rates, and enhances detection accuracy. Furthermore, it demonstrates generalization performance in vulnerability detection across different smart contracts, making it suitable for large-scale contract vulnerability detection tasks.

[0072] The embodiments described above are merely preferred embodiments of the present invention and are not intended to limit the scope of the present invention. Various modifications and improvements made to the technical solutions of the present invention by those skilled in the art without departing from the spirit of the present invention should fall within the protection scope defined by the claims of the present invention.

Claims

1. A method for detecting Ethereum smart contract vulnerabilities based on graph attention, characterized in that, Includes the following steps: S1: Analyze the causes of Ethereum smart contract vulnerabilities and obtain the smart contract code and its abstract syntax tree, and then use a voting system to determine the vulnerability types in the code; S2: Based on the analysis of the generation patterns of smart contract vulnerabilities, different vulnerabilities are combined with abstract syntax trees according to their causes to form a vulnerability graph; specifically including: S201: Based on the vulnerability's underlying principles, extract the core nodes, core statements, core blocks, and core intervals where the vulnerability occurs. Identify the node types on the abstract syntax tree and connect them to obtain the vulnerability graph. ,in This indicates that the edge weight is 1; S202: Vulnerability Diagram By assigning different weights to edges connecting nodes of different types, a vulnerability graph with varying edge weight relationships is obtained. ; The core nodes are statements that can trigger vulnerabilities. The core statement is a sentence that conforms to the grammatical structure where the core node is located. Taking into account the input and output of functions, the effects of preceding functions and global variables, the variables used in the core statement and the local and global variables that have undergone variable passing are all marked as core statement nodes. The core block is the code block containing the core statement. Variables used by the core block and local and global variables that have been passed are all marked as core block nodes. The core area refers to the entire smart contract; Edge weight values ​​assign different weights to edges within different levels, ranging from 2.0x, 1.5x, 1.25x, and 1.25x from top to bottom, for a total of four types. The default value is x=1, thus resulting in different edge types: , , and ; The vulnerability diagram ,in V is a graph node; The vulnerability diagram Its node V is a high-dimensional vector obtained by encoding the text of the node; S3: Based on the vulnerability graph generated in S2, identify vulnerabilities using a multi-layer graph attention classification method.

2. The Ethereum smart contract vulnerability detection method based on graph attention according to claim 1, characterized in that, Step S1 includes: S101: By analyzing the syntax descriptions and code feature technical documents of Ethereum smart contracts, and combining the descriptions of vulnerabilities on publicly available smart contract vulnerability platforms, data collection and organization of smart contract source code are carried out. S102: Based on the smart contract code collected in step S101, the abstract syntax tree of different versions of smart contract code is standardized through the official interface and Solidity version description; S103: Based on the smart contracts collected in step S101, the collected code is voted on using the Slither vulnerability detection tool to obtain vulnerability labels for the smart contracts. The smart contract code vulnerability entity classification rules were obtained from the security website CVE; The abstract syntax tree generated by the smart contract code is used to obtain the spatial structure inside the smart contract source code. Standardizing the abstract syntax tree is to ensure that different versions of the smart contract are in the same space.

3. The Ethereum smart contract vulnerability detection method based on graph attention according to claim 1, characterized in that, Step S3 includes: S301: The completed vulnerability graph Save to the database; S302: Vulnerability graphs are analyzed using a multi-layer graph attention model. Classify vulnerabilities; The multi-layer graph attention model calculates the attention cross-relation coefficients between different nodes i and j. , (1) in It is a node The neighbors are obtained by concatenating the self-attention features from multiple heads. (2) in Indicates splicing, This represents the attention coefficient of the k-head. This represents the input weight matrix of the k-head linear layer; the final result y can be obtained, where (3) Ultimately, the accuracy of vulnerability detection is measured using the F1 score.

4. An Ethereum smart contract vulnerability detection system based on graph attention, characterized in that, include: The module includes a dataset collection module, a vulnerability graph generation module, and a vulnerability detection module. The dataset collection module is used to collect smart contract code and its abstract syntax tree, and then obtain the types of vulnerabilities in the code through a voting system; The vulnerability graph generation module is used to convert the abstract syntax tree of code into a graph representation type and proposes various edge features to increase the relationships between nodes; Specifically, it includes: S201: Based on the vulnerability's underlying principles, extract the core nodes, core statements, core blocks, and core intervals where the vulnerability occurs. Identify the node types on the abstract syntax tree and connect them to obtain the vulnerability graph. ,in This indicates that the edge weight is 1; S202: Vulnerability Diagram By assigning different weights to edges connecting nodes of different types, a vulnerability graph with varying edge weight relationships is obtained. ; The core nodes are statements that can trigger vulnerabilities. The core statement is a sentence that conforms to the grammatical structure where the core node is located. Taking into account the input and output of functions, the effects of preceding functions and global variables, the variables used in the core statement and the local and global variables that have undergone variable passing are all marked as core statement nodes. The core block is the code block containing the core statement. Variables used by the core block and local and global variables that have been passed are all marked as core block nodes. The core area refers to the entire smart contract; Edge weight values ​​assign different weights to edges within different levels, ranging from 2.0x, 1.5x, 1.25x, and 1.25x from top to bottom, for a total of four types. The default value is x=1, thus resulting in different edge types: , , and ; The vulnerability diagram ,in V is a graph node; The vulnerability diagram Its node V is a high-dimensional vector obtained by encoding the text of the node; The vulnerability detection module is used to identify the types of vulnerabilities in code using a multi-layer graph attention model.