A blockchain smart contract vulnerability detection method based on pre-training technology

CN117313098BActive Publication Date: 2026-08-21UNIV OF ELECTRONICS SCI & TECH OF CHINA
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311088354.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-08-28
Publication Date
2026-08-21
Estimated Expiration
2043-08-28

AI Technical Summary

Technical Problem

[0003](1)对于智能合约序列暴力截断的方式存在着丢失漏洞相关信息的风险;

Benefits of technology

[0021] (1) Avoid loss of vulnerability information. This invention solves the problem of loss of vulnerability-related sequences caused by brute-force truncation by using a predefined vulnerability pattern-assisted slicing method, thereby improving the accuracy of smart contract vulnerability detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117313098B_ABST
    Figure CN117313098B_ABST
Patent Text Reader

Abstract

The application discloses a blockchain smart contract vulnerability detection method based on pre-training technology, applied to the field of blockchain smart contract vulnerability detection, in order to solve the problem that the user of the smart contract on the blockchain cannot judge the security of the called non-open source contract and causes loss. The application extracts the core code fragments of the smart contract and the strong correlation with the vulnerability by the pre-defined vulnerability mode auxiliary slicing, guarantees the integrity and reliability of the feature extraction of the smart contract through the pre-training technology, and finally realizes the efficient and accurate detection of the five vulnerabilities of the smart contract, i.e. reentrant, dangerous delegation call, timestamp dependence, multiplication after division and integer overflow, by fine-tuning the pre-training model and extracting global contract information through the bidirectional long short-term memory model, so that the contract detection time is reduced and the contract detection accuracy is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to blockchain smart contract security technology, and its feature is a smart contract vulnerability detection technology. Background Technology

[0002] The concept of smart contracts was proposed by SZABO in 1996. He defined a smart contract as a digital pledge that stipulates the agreements that the contracting parties can execute. The decentralized, immutable, transparent, and traceable characteristics of blockchain provide a reliable environment for smart contracts, enabling their successful application in fields such as transportation, healthcare, and finance, making them one of the core technologies of blockchain. However, with the development and application of blockchain technology, various vulnerabilities have been discovered in smart contracts. In particular, several recent incidents involving attacks on smart contracts resulting in losses of tens of millions of dollars have raised increasing concerns about blockchain security. Existing technologies have the following problems:

[0003] (1) There is a risk of losing vulnerability-related information when brute-force truncation of smart contract sequences;

[0004] (2) There are many limitations in learning the semantic information of contract vulnerability context. It is unable to learn the deeper content of the vulnerability and there is a problem of insufficient extraction of contract feature information.

[0005] (3) Some dynamic contract detection tools are characterized by low contract detection efficiency and high false alarm rate. Summary of the Invention

[0006] The technical problem to be solved by this invention is a vulnerability detection method that can ensure the account security of contract users for non-open source smart contracts.

[0007] The technical solution adopted by this invention to solve the above-mentioned technical problems is a blockchain smart contract vulnerability detection method based on pre-training technology. Users input downloaded on-chain bytecode files into a contract detection model after compilation and data preprocessing. The model then determines whether the contract contains a certain vulnerability based on the output, thereby preventing users from invoking insecure smart contracts and causing losses. The method includes the following steps:

[0008] 1) Process the bytecode sequence on the blockchain into a smart contract opcode sequence;

[0009] 2) Extract the core code sequence by slicing the smart contract opcode sequence according to predefined contract vulnerability rules; the core code sequence is a sequence that may contain vulnerabilities;

[0010] Contract vulnerability patterns include reentrancy vulnerabilities, dangerous delegate call vulnerabilities, timestamp dependency vulnerabilities, multiplication vulnerabilities after division, and integer overflow vulnerabilities;

[0011] The auxiliary slicing rule for reentrancy vulnerabilities is: if the opcode sequence contains a CALL instruction and the Ethereum Gas fee is sufficient, after the asset record is changed and the actual transfer occurs, the arithmetic logic instruction precedes the CALL instruction.

[0012] The auxiliary slicing rules for dangerous delegate invocation vulnerabilities are: the opcode sequence contains the delegate invocation instruction DELEGATECALL and the self-destruct instruction SELFDESTRUCT, and the DELEGATECALL instruction parameters contain the call data loading instruction CALLDATALOAD and the call value instruction CALLVALUE.

[0013] The auxiliary slicing rule for the timestamp dependency vulnerability is: the opcode sequence contains the timestamp instruction TIMESTAMP and the numeric instruction NUMBER, and also contains the CALL instruction;

[0014] The auxiliary slicing rule for the multiplication vulnerability after division is: the opcode sequence contains a division instruction DIV and a multiplication instruction MUL, and the DIV instruction precedes the MUL instruction;

[0015] The auxiliary slicing rule for integer overflow vulnerabilities is: the opcode sequence contains various arithmetic operation instructions;

[0016] 3) Input the core code sequence into the pre-trained BERT model SCOBERT, which is designed for smart contract opcode sequences. The pre-trained SCOBERT model outputs word vectors of the core code sequence. During the pre-training process of the pre-trained BERT model, the opcode sequence compiled from on-chain bytecode is used as training data.

[0017] 4) Input the word vectors of the core code sequence into a bidirectional long short-term memory neural network, and the bidirectional long short-term memory neural network outputs smart contract feature information;

[0018] 5) Input the smart contract feature information into the fully connected layer, and the fully connected layer outputs a binary classification result indicating whether there is a vulnerability.

[0019] This invention first obtains smart contract opcode sequences by compiling and preprocessing on-chain bytecode files acquired from a blockchain platform. Then, it uses predefined pattern-assisted slicing rules to slice specific contract vulnerabilities, reducing the length of the contract opcode sequences. Next, the sequence encoding is input into the pre-trained SCOBERT model of this invention, passing through an embedding layer and a three-layer Transformer encoding layer to obtain the feature vector representation corresponding to the input opcode sequence. This feature vector is then passed through a bidirectional long short-term memory neural network to obtain overall contract feature information, and finally input into a fully connected layer for classification to obtain the contract vulnerability classification results.

[0020] The beneficial effects of this invention are:

[0021] (1) Avoid loss of vulnerability information. This invention solves the problem of loss of vulnerability-related sequences caused by brute-force truncation by using a predefined vulnerability pattern-assisted slicing method, thereby improving the accuracy of smart contract vulnerability detection.

[0022] (2) Completeness of Contract Feature Extraction. Addressing the problem of high false positive rates in contract detection caused by insufficient contract feature extraction in existing methods, this invention proposes a blockchain smart contract vulnerability detection method based on pre-training technology. This method offers more complete contract feature extraction, improving the accuracy, recall, precision, and F1 score of smart contract vulnerability detection. Furthermore, because it uses opcode sequences compiled from on-chain bytecode for pre-training, it is applicable to non-open-source smart contracts.

[0023] (3) Fast contract detection speed. The blockchain smart contract vulnerability detection method based on pre-trained model proposed in this invention is a deep learning method. Compared with dynamically executed smart contract vulnerability detection tools, it has the characteristics of fast execution speed and can handle a large number of contract detection requests, thereby meeting the contract detection needs of users. Attached Figure Description

[0024] Figure 1 This is the overall flowchart of the present invention. Detailed Implementation

[0025] The specific embodiments of the present invention will now be described with reference to the accompanying drawings to enable those skilled in the art to better understand the invention. It should be particularly noted that in the following description, detailed descriptions of known functions and designs that might obscure the main content of the invention will be omitted here.

[0026] For ease of description, the relevant technical terms appearing in the specific implementation method will be explained first:

[0027] Smart contract: A computer program that can automatically execute events and actions;

[0028] Attention mechanism: A special structure embedded in a machine learning model to automatically learn and calculate the contribution of input data to output data;

[0029] MLM: Masked Language Model

[0030] NSP: Next Prediction

[0031] Transformer: A neural network model based on an attention mechanism;

[0032] Bi-LSTM: Bidirectional Long Short-Term Memory Network, a variant of recurrent neural network.

[0033] This invention is applied in the field of blockchain smart contract vulnerability detection, as described below. Figure 1 This paper describes the process of a blockchain smart contract vulnerability detection method based on pre-training technology. First, the acquired on-chain bytecode sequence of the blockchain smart contract is compiled into a smart contract opcode sequence using a compiler. Then, based on this method and auxiliary slicing rules formulated according to predefined vulnerability patterns, the smart contract opcode sequence is sliced ​​to obtain core opcode sequence fragments related to the vulnerability. Next, the data is preprocessed to obtain input data for the pre-trained model. The data is encoded through an encoding embedding layer and a three-layer Transformer encoder. During pre-training, two pre-training tasks, MLM and NSP, are trained separately. The pre-trained model outputs feature representations of the opcode sequence. Then, the contract context information is obtained through a Bi-LSTM layer, and finally, the data is input into a fully connected layer for classification to obtain the contract detection output.

[0034] Specifically, the following steps are included:

[0035] (1) Compile and preprocess the bytecode sequence on the blockchain into an opcode sequence.

[0036] This step takes a bytecode data SC_BYTECODE as input and outputs opcode data SC_OPCODE. First, the control flow graph of the bytecode SC_BYTECODE is obtained using the CFG function. Then, an empty list SC_OPCODE is initialized, and the functions in the smart contract are sorted according to their starting addresses, while the basic blocks within the functions are sorted according to their execution order. By traversing the sorted functions and basic blocks, and adding the opcode from each basic block to the SC_OPCODE list, a preliminary processed sequence of smart contract opcodes is obtained.

[0037] (2) Predefined vulnerability pattern rules

[0038] Based on existing smart contract vulnerability analyses, the following predefined pattern rules related to the opcode sequence behavior of several smart contract vulnerabilities are summarized, thereby assisting in the slicing of opcode sequences to obtain the core sequence that may contain vulnerabilities. It is important to note that the rules summarized below are only for assisting slicing and should not be used as the sole criterion for determining vulnerabilities. The purpose of assisting slicing is to reduce the workload of searching for vulnerabilities across the entire sequence to searching for vulnerabilities in the core sequence.

[0039] (2.1) Reentrancy vulnerability

[0040] Reentrancy vulnerabilities are among the most frequently exploited vulnerabilities in blockchain networks. The DAO incident was an example of an attack exploiting a reentrancy vulnerability. Reentrancy allows attackers to execute recursive calls to request and receive funds from vulnerable contracts. The key instruction in the opcode of a reentrancy vulnerability is the `CALL` instruction, which can call methods in other contracts to send large amounts of Ether. Therefore, combined with a predefined pattern, the auxiliary rules for reentrancy vulnerabilities in the opcode sequence are defined as follows: For a smart contract, if a `CALL` instruction exists in its opcode sequence and the Gas is sufficient, after the asset record change occurs and the actual transfer takes place, the order of the arithmetic logic instructions and the `CALL` instruction is checked.

[0041] (2.2) Dangerous delegate invocation vulnerability

[0042] Dangerous delegate invocation vulnerabilities generally refer to invoking unknown and insecure contracts. Therefore, the opcodes strongly associated with this vulnerability are DELEGATECALL (delegating a method in another contract), SELFDESTRUCT (destroying the contract and sending all funds), CALLVALUE (carrying the amount in a message in units of Wei), and CALLDATALOAD (retrieving an int256 from message data). Therefore, the rules for auxiliary slices should check if the opcode contract contains the DELEGATECALL and SELFDESTRUCT opcode instructions. Additionally, the rules should check if the DELEGATECALL instruction parameters contain the CALLDATALOAD and CALLVALUE instructions.

[0043] (2.3) Timestamp Dependency Vulnerability

[0044] Timestamp dependency vulnerabilities typically occur when a smart contract uses a block timestamp as part of the condition for performing a critical operation. Opcodes associated with timestamp dependency vulnerabilities in the opcode sequence include TIMESTAMP (the UNIX timestamp of the current block), NUMBER (the current block number), CALL (calling a method in another contract), and sending Ether or acquiring remaining GAS. Therefore, auxiliary rules for timestamp dependency vulnerabilities are formulated as follows: check if the opcode contains TIMESTAMP, NUMBER, etc., and then check if the sequence contains a CALL opcode instruction. This serves as a slicing rule for auxiliary timestamp dependency vulnerabilities.

[0045] (2.4) Multiplication loopholes after division

[0046] Since Ethereum smart contracts are primarily written in the Solidity programming language, the characteristics of this language can impact smart contract security. Solidity, as a programming language, shares similar structural designs with other programming languages, such as variables and constants. However, unlike other programming languages, Solidity lacks floating-point numbers, and all numerical calculations result in integers. Therefore, when writing smart contracts in Solidity, the precision of numerical calculations can threaten account security, and the MaD (Multiplication after division) vulnerability arises from this. The order of multiplication and division operations can lead to significant calculation errors. The strongly correlated opcode instructions for this vulnerability are MUL and DIV. Therefore, the slicing rules for mitigating the MaD vulnerability are whether the opcode sequence contains MUL and DIV instructions, and their order.

[0047] (2.5) Integer overflow vulnerability

[0048] Integer overflow vulnerabilities include underflow and overflow of integers. Integer overflow occurs when an execution requires a fixed-size variable to store data exceeding the variable's range. The Ethereum Virtual Machine (EVM) specifies that integers are of fixed size as a data type. Attackers may exploit this vulnerability by using smart contract code and creating unexpected logical flows. Therefore, opcode sequences strongly associated with integer overflow vulnerabilities are various arithmetic operation instructions, such as multiplication (MUL), subtraction (SUB), addition (ADD), and division (DIV), which serve as a slicing rule to aid in identifying integer overflow vulnerabilities.

[0049] The predefined contract vulnerability slicing rules are as follows:

[0050] Reentrancy vulnerability: If the opcode sequence contains a CALL instruction and the Ethereum gas fee is sufficient, the arithmetic logic instruction will precede the CALL instruction after the asset record is changed and the actual transfer occurs.

[0051] A dangerous delegate invocation vulnerability exists: the opcode sequence contains the DELEGATECALL and SELFDESTRUCT instructions, and the DELEGATECALL instruction parameters contain the CALLDATALOAD and CALLVALUE instructions;

[0052] Timestamp dependency vulnerability: The opcode sequence contains TIMESTAMP and NUMBER instructions, and also contains a CALL instruction;

[0053] Multiplication vulnerability following division: The opcode sequence contains DIV and MUL instructions, with the DIV instruction preceding the MUL instruction;

[0054] The integer overflow vulnerability is caused by the opcode sequence containing various arithmetic operation instructions.

[0055] The slicing process is as follows:

[0056] Since predefined rules cannot fully cover all possible scenarios of a vulnerability, opcodes strongly related to the vulnerability are selected as the core opcode sequence extension points for auxiliary slicing by combining predefined rules with vulnerability characteristics. The key opcode set includes instructions such as ADD, MUL, and TIMESTAMP, covering the instructions involved in the aforementioned predefined contract vulnerability auxiliary slicing rules. The slicing operation accepts the original smart contract opcode sequence X and returns the extracted base block containing the core opcode as the smart contract core code sequence X*. The method mainly consists of two phases: the first phase is to initialize the key opcodes and the stop condition set; the second phase is to extract the sequence containing the key opcode from the original opcode sequence X. Specifically, starting from the beginning of the sequence, if the current opcode is in the key opcode set, the nearest stop condition is found, and the subsequence (a base block) from the opcode to the current opcode is extracted and stored in a new sequence X*. This process continues until the entire sequence X is traversed. If no subsequence containing the key opcode is found in the entire sequence, the original sequence X is directly returned as X*. Finally, the extracted sequence X* is returned.

[0057] (3) Obtain word vectors through the pre-trained model SCOBERT

[0058] SCOBERT is a pre-trained model based on BERT. BERT is a pre-trained natural language processing model that obtains contextual semantic information by performing deep bidirectional representations on unlabeled text, learning more dimensions and deeper layers of text during training. The Masked Language Model (MLM) and NextSentence Prediction (NSP) methods are the core methods of BERT. MLM is introduced to improve the model's deep bidirectional representation capabilities. Meanwhile, to improve the model's ability to understand semantic relationships between sentences, the BERT model uses the NSP method for pre-training, which predicts whether two randomly selected sentences are adjacent. The trained model can be used in multiple fields such as text classification, and is more convenient and efficient than traditional Natural Language Processing (NLP) methods. The core of the BERT model is the Transformer framework. The input vector X consists of three vectors: Token Embedding, Segment Embedding, and Position Embedding. The Token Embedding contains semantic information of the characters, the Segment Embedding contains contextual information, and the Position Embedding contains positional information. A Transformer consists of a set of encoder components and an equal number of decoders (corresponding to the encoders). Each encoder comprises an attention mechanism and a feedforward neural network, while each decoder consists of an attention mechanism, an encoder-decoder attention layer, and a feedforward neural network. It's worth noting that in BERT, the Transformer only uses the encoder portion of the complete Transformer.

[0059] In smart contract vulnerability detection, the detection of vulnerabilities needs to consider not only the relationship between the following text but also the relationship between the preceding text. The BERT-based Multi-Head Attention mechanism is very suitable for this situation. The SCOBERT model, based on BERT, not only increases the learning ability more applicable to smart contract vulnerabilities but also increases the learning ability of the dependency relationship between different opcodes in contract vulnerabilities. The SCOBERT pre-trained model for smart contract opcode sequences proposed in this invention. The model takes the opcode as input and processes it through the BERT core layer composed of three vectors: Token Embedding, Segment Embedding, and Position Embedding, and finally generates a pre-trained model that can serve smart contract vulnerability detection. In addition, the SCOBERT model takes the opcode sequence as input, so the bytecode needs to be converted into the opcode we need in the data preprocessing stage. The opcode used in the SCOBERT pre-trained model is generated using step (1). The pre-training process uses an unlabeled dataset to train on two pre-training tasks, NSP and MLM.

[0060] (4) Bidirectional Long Short-Term Memory Neural Network to Obtain Global Contract Information

[0061] LSTM (Long Short-Term Memory) is a type of RNN (Recurrent Neural Network). Due to its design characteristics, LSTM is well-suited for modeling time-series data. Bi-LSTM (Bi-directional Long Short-Term Memory) is a combination of forward LSTM and backward LSTM. This invention employs a bi-directional long short-term memory model to obtain smart contract context information. The long short-term memory model is based on the input words at time t. Cell state Temporary cell state Hidden layer state Forgotten Gate Memory Gate and output gate The model is composed of elements that utilize the forgetting of information in the cellular state and the memorization of new information to ensure the transmission of information beneficial to the target task in subsequent steps, while useless information is forgotten. The relevant formulas for the model are as follows:

[0062] Forget Gate Calculation Formula:

[0063]

[0064] Memory gate calculation formula:

[0065]

[0066]

[0067] Formula for calculating the current cell state:

[0068]

[0069] Output gate and current hidden layer calculation:

[0070]

[0071]

[0072] Ultimately, we can obtain the hidden state sequence with the same length as the contract opcode sequence. Based on this, two sets of outputs are obtained through forward and backward long short-term memory. The first and second position vectors of the two outputs are added together and concatenated. By combining the context information of the forward and backward layers, a vector representation of the overall context information of the contract opcode sequence is obtained.

[0073] (5) Classifying contracts using fully connected layers and activation functions.

[0074] After obtaining the vector representation of the contract context information features from step (4), it is mapped to the sample label feature space through a fully connected layer for classification. Each node in the fully connected layer is connected to all nodes in the previous layer, used to synthesize the features extracted earlier. The activation function is a mathematical equation that determines the output of the neural network. This method uses the Sigmoid activation function to achieve single-label classification of smart contracts, and its mathematical expression is as follows:

[0075]

[0076] It can transform continuous real values ​​of input into an output between 0 and 1. When the output is 0 after processing, it means that the contract has no vulnerabilities. When the output is 1, it means that the contract has been found to contain the vulnerability.

[0077] Although the illustrative specific embodiments of the present invention have been described above to enable those skilled in the art to understand the invention, it should be understood that the invention is not limited to the scope of the specific embodiments. For those skilled in the art, various changes are obvious as long as they are within the spirit and scope of the invention as defined and determined by the appended claims, and all inventions utilizing the concept of the present invention are protected.

Claims

1. A method for detecting vulnerabilities in blockchain smart contracts based on pre-training technology, characterized in that, Includes the following steps: 1) Process the bytecode sequence on the blockchain into a smart contract opcode sequence; 2) Extract the core code sequence by slicing the smart contract opcode sequence according to predefined contract vulnerability rules; the core code sequence is a sequence that may contain vulnerabilities; Contract vulnerability patterns include reentrancy vulnerabilities, dangerous delegate call vulnerabilities, timestamp dependency vulnerabilities, multiplication vulnerabilities after division, and integer overflow vulnerabilities; The auxiliary slicing rule for reentrancy vulnerabilities is: if the opcode sequence contains a CALL instruction and the Ethereum Gas fee is sufficient, after the asset record is changed and the actual transfer occurs, the arithmetic logic instruction precedes the CALL instruction. The auxiliary slicing rules for dangerous delegate invocation vulnerabilities are: the opcode sequence contains the delegate invocation instruction DELEGATECALL and the self-destruct instruction SELFDESTRUCT, and the DELEGATECALL instruction parameters contain the call data loading instruction CALLDATALOAD and the call value instruction CALLVALUE. The auxiliary slicing rule for the timestamp dependency vulnerability is: the opcode sequence contains the timestamp instruction TIMESTAMP and the numeric instruction NUMBER, and also contains the CALL instruction; The auxiliary slicing rule for the multiplication vulnerability after division is: the opcode sequence contains a division instruction DIV and a multiplication instruction MUL, and the DIV instruction precedes the MUL instruction; The auxiliary slicing rule for integer overflow vulnerabilities is: the opcode sequence contains various arithmetic operation instructions; 3) Input the core code sequence into the pre-trained BERT model SCOBERT, which is designed for smart contract opcode sequences. The pre-trained SCOBERT model outputs word vectors of the core code sequence. During the pre-training process of the pre-trained BERT model, the opcode sequence compiled from on-chain bytecode is used as training data; 4) Input the word vectors of the core code sequence into a bidirectional long short-term memory neural network, and the bidirectional long short-term memory neural network outputs smart contract feature information; 5) Input the smart contract feature information into the fully connected layer, and the fully connected layer outputs a binary classification result indicating whether there is a vulnerability.

2. The method as described in claim 1, characterized in that, The specific method for processing a bytecode sequence on the blockchain into a smart contract opcode sequence is as follows: obtain the control flow graph of the bytecode on the blockchain, then initialize an empty list, sort the functions in the smart contract according to their starting addresses, sort the basic blocks in the functions according to their execution order, and add the opcode in each basic block to the list by traversing the sorted functions and basic blocks. After traversal, the opcodes in the list form a smart contract opcode sequence.

3. The method as described in claim 1, characterized in that, The pre-trained model SCOBERT outputs word vectors of the core code sequence for smart contract vulnerability detection services through its masked language model MLM and next-sentence prediction method NSP.

4. The method as described in claim 1, characterized in that, The word vectors of the core code sequence output by the pre-trained model are input into a bidirectional long short-term memory neural network. The forward and backward long short-term memories output the hidden state sequences with the same length as the maximum input sequence of the pre-trained model. The forward and backward contextual information is combined by adding the first and second position vectors of the two outputs and concatenating them to form the output smart contract feature information.

5. The method as described in claim 1, characterized in that, The fully connected layer uses the Sigmoid activation function.