A smart contract vulnerability detection method
By combining event-based temporal graph neural networks with the Transformer model, the problems of low efficiency and high false positive rate in smart contract vulnerability detection are solved, achieving high-precision vulnerability detection and binary classification.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- HANGZHOU LINGWEI INFORMATION TECH CO LTD
- Filing Date
- 2023-02-09
- Publication Date
- 2026-07-07
AI Technical Summary
Existing smart contract vulnerability detection methods are inefficient and have a high false positive rate. They cannot effectively identify complex logic vulnerabilities and cannot be fixed after deployment.
By combining event-based temporal graph neural networks with the Transformer model, a control flow graph is generated by decompiling bytecode. The encoder of the Transformer is used to convert semantic features into vector representations. Finally, the event-based temporal graph neural network is combined to capture semantic information and control flow timing information, thereby achieving high-precision vulnerability detection.
It improves the accuracy and efficiency of smart contract vulnerability detection, effectively identifies complex logic vulnerabilities, and transforms the detection results into a binary classification problem, thereby reducing the false positive rate.
Smart Images

Figure CN116010975B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of blockchain smart contract security detection, and in particular relates to a smart contract vulnerability detection method. Background Technology
[0002] In recent years, the number of smart contracts on Ethereum has been increasing, bringing huge economic benefits but also harboring high-risk security problems. Blockchain technology, with its immutability and decentralization, is easily vulnerable to attacks and malicious propagation of blockchain data. However, attacking a blockchain platform is relatively difficult and resource-intensive. Furthermore, because smart contracts run on an underlying infrastructure lacking a trusted computing environment and possess characteristics distinct from traditional programs, they inherently have many far-reaching vulnerabilities, making attacks relatively simple. Once deployed, smart contracts cannot be modified. If security risks are discovered later, they cannot be fixed, effectively exposing vulnerabilities to attackers and making malicious exploitation of the smart contracts impossible. Therefore, how to perform vulnerability detection on smart contracts before deployment has become a crucial issue for blockchain developers.
[0003] Currently, the main methods for detecting smart contract vulnerabilities include formal verification, fuzzing, and symbolic execution. Specifically: formal methods have the advantage of mathematical and logical completeness throughout the verification process, guaranteeing 100% detection accuracy. However, they require manual intervention in modeling and reasoning, resulting in very low efficiency. Fuzzing generates test cases randomly, limiting the execution paths it can cover and leading to low path coverage, making it difficult to discover potential vulnerabilities. Static vulnerability detection tools based on symbolic execution mostly rely on existing expert rules, failing to identify complex logical vulnerabilities and exhibiting a high false positive rate. Summary of the Invention
[0004] The purpose of this invention is to address the limitations of single-method smart contract vulnerability detection techniques by providing a vulnerability detection scheme that combines event-based temporal graph neural networks with Transformer. This scheme generates a control flow graph by decompiling bytecode, uses the Transformer's encoder to convert semantic features (node features) into vector representations, and combines this with an event-based temporal graph neural network to capture semantic information and program-wide control flow timing information, thereby achieving high-precision smart contract vulnerability detection.
[0005] To address the aforementioned technical problems, the specific technical solution of the smart contract vulnerability detection method of the present invention is as follows:
[0006] A method for detecting smart contract vulnerabilities includes the following steps:
[0007] Step 1: Decompile and CFG construction. The decompiled bytecode generates EVM instructions and corresponding parameters. Then, the control flow graph (CFG) is reconstructed based on the decompiled EVM instructions.
[0008] Step 2: Build a dataset from the EVM instructions and train it using the encoder of the Transformer model;
[0009] Step 3: Input the node information in the CFG graph into the Transformer model, and obtain the feature representation vector of the node information after the model's processing;
[0010] Step 4: Construct a temporal graph neural network for events, inputting the graph structure and node information representation vectors into the neural network; after information transmission and updating, and constructing an event sequence based on the temporal information of the nodes to represent the graph information, and finally performing vulnerability detection;
[0011] Step 5: Evaluate the model's performance using target detection metrics.
[0012] Furthermore, step 1 includes the following specific steps:
[0013] The smart contract source code is compiled using the solc tool to obtain bytecode files. The bytecode is then converted into a CFG (Content Graph) in two steps: decompiling to generate assembly instructions and using the assembly instructions to build the CFG. The Octopus tool is used for both decompiling and building the CFG. Octopus first converts the bytecode into an assembly instruction set, and then converts the assembly instruction set into the elements needed for the CFG graph: block sets and edge sets. The block set is a collection of node instructions in the graph. The edge set, besides indicating the start and end points of each edge, also includes the edge type, which includes five categories: unconditional, conditional_true, conditional_false, fallthrough, and call. Information is extracted from the CFG graph, and the required data—target, graph, instruction, attribute, and time—are exported. Target represents the presence or absence of vulnerabilities; graph represents graph information, with parameters for the start point, edge type, and end point; instruction is the block instruction set; attribute represents the edge type; and time represents time sequence information.
[0014] Furthermore, step 2 includes the following specific steps:
[0015] The Transformer model uses an attention mechanism to reduce the distance between any two positions in the input sequence to a constant. The model's input consists of two parts: Token Embedding and Positional Embedding. Token Embedding maps vectors to tokens using an instruction vocabulary. Positional Embedding transforms positional information in a non-constant form, thus compensating for the self-attention mechanism's inability to capture temporal information. In the Transformer, the following rules are used to generate positional information in each dimension:
[0016]
[0017]
[0018] Where PE represents the Positional Embedding matrix, pos∈[0,max_len) represents a specific position, and i∈[0,d... model / 2) Represents a specific dimension, where the vectors of Token Embedding and Positional Embedding are added together as the input to the Transformer model. The Transformer encoder consists of multiple identical layers stacked on top of each other, each layer having two sub-layers. The first sub-layer is a multi-head self-attention convergence; the second sub-layer is a position-based feedforward network. For the multi-head attention in the encoder, its original q, k, and v are the results of the encoder's token input after embedding. After a linear transformation, q, k, and v are respectively obtained as Q, K, and V. The core of the self-attention mechanism is to calculate the attention weights through Q and K; then apply them to V to obtain the entire weight and output. The formula for calculating the output vector is:
[0019]
[0020] The specific calculation formula for the multi-head attention mechanism is as follows:
[0021] MultiHead(Q,K,V)=Concat(head1,...,head h W O (4)
[0022] where head i =Attention(QW i Q,KW i K VW i V (5)
[0023] in
[0024] Furthermore, step 3 includes the following specific steps:
[0025] Given a sequence of blocks T = [T1, T2, T3, ..., T] n ], where n is the sequence length, and the instruction set is input into the Transformer model in blocks. The Transformer's Encoder encodes the instruction and takes the output of the last encoder layer, represented as:
[0026] X s =[E1, E2, E3...........E n (6)
[0027] Ultimately, it was determined that the weighted average of each token vector would be used as the feature representation vector of the block for downstream tasks.
[0028] Furthermore, step 4 includes the following specific steps:
[0029] The temporal event neural network model takes graph structure information and node feature representation vectors as input. It processes each CFG graph using the event temporal graph neural network model to obtain the graph's vector representation. The calculation result is then mapped within the [0,1] interval. If the calculation result exceeds a predetermined threshold, the smart contract is considered to have a vulnerability. The event temporal graph neural network model processes the input data in four stages: node update, event generation, information aggregation, and vulnerability determination. In the node update stage, the neural network model transmits node and edge information through an encoder, mapping a set of nodes to new nodes. In the event generation stage, the temporal relationship between edges in the input data is used to update the information between nodes and generate events. Each event has four features: source node v... l Target node v j Event type e ij and timing information t ij For the hidden state of time sequence information The calculation formula is as follows:
[0030]
[0031] Here, F represents the dimension of the time vector, f is the F-th dimension, and time is encoded using Time2Vec, denoted as T2V. T2V uses the first dimension of the time vector as a linear time representation, and the others as periodic nonlinear representations, ω f and These are learnable linear parameters;
[0032] Constructing events based on their chronological order, given a source node v l Target node v j Event type e ij and timing information t ij Modeling is performed by connecting the following four features, and the calculation formula is as follows:
[0033]
[0034] Where || represents the splicing operation. and They represent nodes v respectively l and node v j The hidden state represented at time n, and This represents the hidden state represented by the edges connecting each pair of nodes. After encoding the generated events sequentially, a series of timestamped events are obtained in chronological order.
[0035] During the information aggregation phase, all events are aggregated, and a global average pooling layer is used to compute the final graph vector G:
[0036]
[0037] In the vulnerability determination phase, the calculated value is mapped to the range [0,1] using a mapping function. The threshold for whether there is a vulnerability in the contract graph is determined by judging the calculated value of each graph vector, and the final threshold for a contract graph with a vulnerability is obtained.
[0038] Furthermore, step 5 includes the following specific steps:
[0039] Introducing a confusion matrix, along with accuracy, precision, recall, and F1 score to evaluate model performance. Accuracy:
[0040] Accuracy:
[0041] Recall rate:
[0042] F1: F1=2*(Precision*Recall) / (Precision+Recall) (13)
[0043] TP represents a true positive, FP represents a false positive, FN represents a false negative, and TN represents a true negative.
[0044] The smart contract vulnerability detection method of this invention has the following advantages: This invention proposes a vulnerability detection method combining an event-based temporal graph neural network and a Transformer. First, the Transformer model based on the attention mechanism abandons the sequential structure of traditional recurrent neural networks, enabling the model to have better parallelism. Through the multi-head attention mechanism within the model, the encoded result simultaneously contains contextual information, effectively representing the node information in the CFG graph. Second, an event-based temporal graph neural network is proposed. This model combines RNN with graph structure and node information for information transmission and updating, and uses the temporal information between nodes to construct events. Finally, the graph information is represented and transformed into a binary classification problem to determine the vulnerability detection result of the graph. Attached Figure Description
[0045] Figure 1 This is a flowchart of the smart contract data processing of the present invention;
[0046] Figure 2 This is the input diagram of the Transformer model Encoder of this invention;
[0047] Figure 3 This is a time-series neural network flowchart of the events of this invention;
[0048] Figure 4 This is a diagram of the Transformer model encoder framework of the present invention;
[0049] Figure 5 This is a flowchart illustrating the combination of a time-series graph neural network and a Transformer for the events described in this invention. Detailed Implementation
[0050] To better understand the purpose, structure, and function of this invention, the following detailed description of a smart contract vulnerability detection method is provided in conjunction with the accompanying drawings.
[0051] The present invention provides a smart contract vulnerability detection method, comprising the following steps:
[0052] Step 1: Decompiling and CFG Construction. The decompiled bytecode will generate EVM instructions (i.e., opCode) and corresponding parameters. Then, the control flow graph (CFG) is reconstructed based on the decompiled EVM instructions.
[0053] The source code of a smart contract is compiled using the solc tool to obtain bytecode files. Converting the bytecode into a CFG (Construction Code Generation Group) requires two steps: decompiling to generate assembly instructions and using the assembly instructions to build the CFG. Only by decompiling the bytecode can the EVM instructions and their parameters be parsed. A basic block is a collection of instructions and their parameters. A CFG is composed of basic blocks, each of which begins with a non-jump instruction and ends with a jump or termination instruction (such as STOP, JUMP, JUMPI, RETURN, REVERT, SELFDESTRUCT, etc.).
[0054] Therefore, Octopus was used to decompile and build the CFG. Octopus first converts the bytecode into an assembly instruction set, then converts the assembly instruction set into the elements needed for the CFG graph: block sets and edge sets, and finally generates the CFG file. A block set is a collection of node instructions in the graph. An edge set, besides indicating the start and end points of an edge, also includes the edge type. Edge types include the following five categories: unconditional, conditional_true, conditional_false, fallthrough, and call. For example... Figure 1 As shown, this illustrates the data processing flow of a smart contract.
[0055] Extracting information from the CFG graph and exporting the required data is shown in Table 1. `target` represents the presence or absence of vulnerabilities; `graph` represents graph information, with parameters for the start point, edge type, and end point; `instruction` is the block instruction set; `Attribute` represents the edge type; and `Time` represents temporal information.
[0056] Table 1 Dataset Information
[0057]
[0058] Step 2: Build a dataset from the EVM instructions and train it using the encoder of the Transformer model.
[0059] The Transformer model employs an attention mechanism that reduces the distance between any two positions in the input sequence to a constant. It abandons the sequential structure of traditional recurrent neural networks, resulting in better parallelism and compatibility with existing GPU frameworks. The model's input consists of two parts: Token Embedding and Positional Embedding. Token Embedding maps vectors to tokens using an instruction vocabulary; Positional Embedding transforms positional information in a non-constant form, thus compensating for the self-attention mechanism's inability to capture temporal information. In the Transformer, this work uses the following rules to generate positional information in each dimension:
[0060]
[0061]
[0062] Where PE represents the Positional Embedding matrix, pos∈[0,max_len) represents a specific position, and i∈[0,d... model / 2) Represents a specific dimension. For example... Figure 2 The diagram shows the input of the Transformer model encoder, where the vectors of Token Embedding and Positional Embedding are added together as the input to the Transformer model.
[0063] like Figure 4 The diagram shows the encoder structure of the Transformer model. The Transformer encoder consists of multiple identical layers stacked on top of each other, with each layer having two sub-layers. The first sub-layer is a multi-head self-attention convergence; the second sub-layer is a position-based feedforward network. For the multi-head attention in the encoder, the original q, k, and v are the results of the encoder's token input after embedding. q, k, and v undergo a linear transformation to obtain Q, K, and V respectively. The core of the self-attention mechanism is to calculate the attention weights using Q and K; then apply them to V to obtain the total weights and output. The formula for calculating the output vector is:
[0064]
[0065] The specific calculation formula for the multi-head attention mechanism is as follows:
[0066] MultiHead(Q,K,V)=Concat(head1,...,head h W O (4)
[0067] where head i =Attention(QW i Q ,KW i K VW i V (5)
[0068] in
[0069] Step 3: Input the node information (instruction set) in the CFG graph into the Transformer model, and obtain the feature representation vector of the node information after the model's processing.
[0070] In this experiment, a sequence of blocks T = [T1, T2, T3, ..., T] is given. n ], where n is the sequence length. The instruction set is input into the Transformer model in blocks, encoded by the Transformer's Encoder, and the output of the last encoder layer is taken, represented as:
[0071] X s =[E1, E2, E3...........E n (6)
[0072] Ultimately, it was determined that the weighted average of each token vector would be used as the feature representation vector of the block for downstream tasks.
[0073] Step 4: Construct a temporal graph neural network for events, inputting the graph structure and node information representation vectors into the neural network; after information transmission and updating, and constructing an event sequence based on the temporal information of the nodes to represent the graph information, and finally performing vulnerability detection.
[0074] The temporal event neural network model takes graph structure information and node feature representation vectors as input. It processes each CFG graph using the event temporal graph neural network model to obtain a vector representation of the graph, and finally maps the calculation result to the interval [0,1]. If the calculation result is greater than a predetermined threshold, the smart contract is considered to have a vulnerability. Figure 3As shown, the event sequence graph neural network model processes the input data in four stages: node update, event generation, information aggregation, and vulnerability determination. In the node update stage, the neural network model transmits node and edge information through an encoder, mapping a set of nodes to new nodes. In the event generation stage, the information between nodes is updated and events are generated using the temporal relationships of edges in the input data; each event has four features: source node v... l Target node v j Event type e ij and timing information t ij For the hidden state of timing information The calculation formula is as follows:
[0075]
[0076] Here, F represents the dimension of the time vector, and f is the F-th dimension. Time is encoded using Time2Vec, denoted as T2V. T2V uses the first dimension of the time vector as a linear time representation, and the others as periodic nonlinear representations. ω f and It is a learnable linear parameter.
[0077] Constructing events based on their chronological order, given a source node v l Target node v j Event type e ij and timing information t ij Modeling is performed by connecting the following four features, and the calculation formula is as follows:
[0078]
[0079] Where || represents the splicing operation. and They represent nodes v respectively l and node v j The hidden state represented at time n, and This represents the hidden state represented by the edges connecting each pair of nodes. After encoding the generated events sequentially, a series of timestamped events are obtained in chronological order.
[0080] During the information aggregation phase, all events are aggregated, and a global average pooling layer is used to compute the final graph vector G:
[0081]
[0082] In the vulnerability assessment phase, a mapping function maps the calculated values to the range [0,1]. The threshold for whether a contract graph has a vulnerability is determined by assessing the calculated values of each graph vector, ultimately arriving at a threshold for a vulnerable contract graph. The process of combining event sequence graph neural networks with Transformer is as follows: Figure 5 As shown.
[0083] Step 5: Evaluate the model's performance using target detection metrics.
[0084] This work introduces a confusion matrix, as well as accuracy, precision, recall, and F1 score. See Table 2 for details.
[0085] Table 2 Confusion Matrix Structure
[0086]
[0087] Accuracy: Accuracy: Recall rate:
[0088] F1: F1=2*(Precision*Recall) / (Precision+Recall) (13)
[0089] It is understood that the present invention has been described through some embodiments, and those skilled in the art will recognize that various changes or equivalent substitutions can be made to these features and embodiments without departing from the spirit and scope of the invention. Furthermore, under the teachings of the present invention, these features and embodiments can be modified to adapt to specific situations and materials without departing from the spirit and scope of the invention. Therefore, the present invention is not limited to the specific embodiments disclosed herein, and all embodiments falling within the scope of the claims of this application are within the protection scope of the present invention.
Claims
1. A method for detecting smart contract vulnerabilities, characterized in that, The steps include the following: Step 1: Decompile and CFG construction. The decompiled bytecode generates EVM instructions and corresponding parameters. Then, the control flow graph (CFG) is reconstructed based on the decompiled EVM instructions. Step 2: Build a dataset from the EVM instructions and train it using the encoder of the Transformer model; Step 3: Input the node information in the CFG graph into the Transformer model, and obtain the feature representation vector of the node information after the model's processing; Step 4: Construct a temporal graph neural network for events, and input the graph structure and the information representation vectors of the nodes into the neural network; After information is transmitted and updated, and an event sequence is constructed based on the temporal information of the nodes to represent the graph information, the vulnerability is finally detected. The input to the temporal event neural network model is graph structure information and node feature representation vectors. The temporal graph neural network model of the event processes each CFG graph to obtain the vector representation of the graph. Finally, the calculation result is mapped in the interval [0,1]. If the calculation result is greater than a predetermined threshold, the smart contract is considered to have a vulnerability. The temporal graph neural network model of the event processes the input data, including four stages: node update stage, event generation stage, information aggregation stage, and vulnerability determination stage. In the node update stage, the neural network model transmits the information of nodes and edges through the encoder. A set of nodes is mapped to new nodes. During the event generation phase, the information between nodes is updated and events are generated using the temporal relationships of edges in the input data. Each event has four characteristics: source node v l Target node v j Event type e ij and timing information t ij For the hidden state of time sequence information The calculation formula is as follows: (7) Here, F represents the dimension of the time vector, and f is the F-th dimension. Time is encoded using Time2Vec, denoted as T2V. T2V uses the first dimension of the time vector as a linear time representation, and the others as periodic nonlinear representations. and These are learnable linear parameters; Constructing events based on their chronological order, given a source node v l Target node v j Event type e ij and timing information t ij Modeling is performed by connecting the following four features, and the calculation formula is as follows: (8) in This represents a splicing operation. and Representing node v respectively l and node v j The hidden state represented at time n, and This represents the hidden state represented by the edges connecting each pair of nodes. After encoding the generated events sequentially, a series of timestamped events are obtained in chronological order. During the information aggregation phase, all events are aggregated, and a global average pooling layer is used to compute the final graph vector G: G=Avgpooling( ) (9) In the vulnerability assessment phase, the calculated values are mapped to the range [0,1] using a mapping function. The threshold for whether a contract graph has a vulnerability is determined by assessing the calculated values of each graph vector, ultimately resulting in a threshold for a contract graph with a vulnerability. Step 5: Evaluate the model's performance using target detection metrics.
2. The smart contract vulnerability detection method according to claim 1, characterized in that, Step 1 includes the following specific steps: The source code of the smart contract is compiled using the solc tool to obtain bytecode files. The bytecode is then converted into CFG in two steps: decompiling to generate assembly instructions and using the assembly instructions to build the CFG. The Octopus tool is used to decompile and build CFG. The first step of Octopus is to convert the bytecode into an assembly instruction set. The second step is to convert the assembly instruction set into the elements required by the CFG graph, namely the block set and the edge set, and then generate the CFG file. The block set is a collection of node instructions in the graph. The edge set, in addition to indicating the start and end points of the edge, also includes the edge type. The edge types include the following five categories: unconditional, conditional_true, conditional_false, fallthrough, and call. Extract information from the CFG graph and export the required data: target, graph, instruction, attribute, and time. Target represents whether there are vulnerabilities; graph represents graph information, with parameters for the start point, edge type, and end point; instruction is the block instruction set; attribute represents the edge type; and time represents time sequence information.
3. The smart contract vulnerability detection method according to claim 1, characterized in that, Step 2 includes the following specific steps: The Transformer model uses an attention mechanism to reduce the distance between any two positions in the input sequence to a constant. The model's input consists of two parts: Token Embedding and Positional Embedding. Token Embedding maps vectors to tokens by constructing an instruction vocabulary. Positional Embedding transforms positional information in a non-constant form, thus compensating for the self-attention mechanism's inability to capture temporal information. In the Transformer, the following rules are used to generate positional information in each dimension: (1) (2) Where PE represents the Positional Embedding matrix. Indicates a specific location. To represent a specific dimension, the vectors of Token Embedding and Positional Embedding are added together and used as the input to the Transformer model; The Transformer encoder consists of multiple identical layers stacked on top of each other. Each layer has two sub-layers. The first sub-layer is a multi-head self-attention convergence layer; the second sub-layer is a position-based feedforward network. For the multi-head attention in the encoder, the original q, k, and v are the results of the encoder's token input after embedding. q, k, and v are then transformed linearly to obtain Q, K, and V. The core of the self-attention mechanism is to calculate the attention weights using Q and K, and then apply them to V to obtain the entire weight and output. The formula for calculating the output vector is as follows: (3) The specific calculation formula for the multi-head attention mechanism is as follows: (4) (5) in , , .
4. The smart contract vulnerability detection method according to claim 1, characterized in that, Step 3 includes the following specific steps: Given a sequence of blocks T = [T1, T2, T3, ..., T] n ], where n is the sequence length, and the instruction set is input into the Transformer model in blocks. The Transformer's Encoder encodes the instruction and takes the output of the last encoder layer, represented as: X s =[E1,E2,E 3........... THE n ] (6) Ultimately, it was determined that the weighted average of each token vector would be used as the feature representation vector of the block for downstream tasks.
5. The smart contract vulnerability detection method according to claim 1, characterized in that, Step 5 includes the following specific steps: We introduce a confusion matrix, and use accuracy, precision, recall, and F1 score to evaluate model performance. Accuracy: (10) Precision: Precision = (11) Recall rate: Recall= (12) F1: F1=2*(Precision*Recall) / (Precision+Recall) (13) TP represents a true positive, FP represents a false positive, FN represents a false negative, and TN represents a true negative.