Power blockchain smart contract security detection method, device, equipment and medium
By constructing a similarity judgment model based on bidirectional LSTM and combining word2vec and structure2vec algorithms, the problems of false positives and false negatives in the security detection of smart contracts in the power blockchain were solved, and efficient and accurate security detection was achieved.
Patent Information
- Application Number
- CN202211725411.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-28
- Publication Date
- 2026-11-20
- Estimated Expiration
- 2042-12-28
AI Technical Summary
Existing technologies for security detection of smart contracts in the power blockchain suffer from numerous false positives and false negatives, low automation, a wide variety of detection tools that cover inconsistent vulnerability types, and unstable accuracy and false negative rates.
A similarity judgment model based on bidirectional LSTM is adopted. By obtaining the control flow graph (CFG) and program dependency graph (PDG) of the power blockchain smart contract, the model is sliced and transformed into word vectors and graph structure vectors. The model is trained by combining word2vec and structure2vec algorithms to improve detection accuracy.
It enhances the intelligence and accuracy of smart contract security testing, simplifies labor costs, improves testing efficiency and quality, and solves the problem of the generalizability of testing tools.
Smart Images

Figure CN116010951B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of computer testing technology, and specifically relates to a method, device, equipment and medium for security testing of smart contracts in the power blockchain. Background Technology
[0002] As a distributed software system, blockchain still faces the same security and privacy threats as traditional software, such as unauthorized access, directory traversal vulnerabilities, identity spoofing, SQL injection attacks, and DoS attacks. Therefore, common security engineering measures, such as proper key management, strict identity verification and access control, rigorous input parameter validation and formatting, isolation between the business plane and control plane, fault isolation, and the use of security devices like firewalls to implement security policies and anomaly traffic cleanup, remain crucial for blockchain system security. Beyond these traditional security threats, blockchain systems also face new ones. For example, blockchain systems rely on smart contracts to carry core business logic, making smart contract security vulnerabilities a significant concern.
[0003] To address the security risks of smart contracts, the industry has strengthened their security at multiple levels, primarily including smart contract languages, smart contract code inspection, and formal verification tools. In consortium blockchains, many platforms directly use general-purpose programming languages as smart contract development languages, such as Golang, Java, Rust, and JavaScript. These languages are not designed for smart contracts; their greater programming flexibility makes it difficult to implement static language checks and rigorous formal code verification, and also makes it harder to constrain programmer behavior and ensure blockchain state data consistency (unless APIs such as network I / O, file I / O, and random numbers are blocked). Formal verification-based code auditing methods utilize rigorous mathematical theory to model, derive, and prove smart contract code throughout its entire operation, using logical methods to demonstrate whether the contract's results meet expectations regarding consistency, verifiability, and access control. While formal verification can cover all possible execution outcomes, this method is still in its early research stages, with limited related data and supporting tools. Furthermore, its automation level is low, often requiring secondary verification by human personnel. The main idea behind automated auditing methods based on symbolic execution and abstract constraints is to verify path reachability through symbolic execution and constraint solving. This involves generating a Control Flow Graph (CFG) by simulating contract execution. During the analysis, symbolic values replace arbitrary uncertain variables in the source code, such as environment variables and formal parameters. Reachability is verified after all paths are collected. This method determines whether smart contract code has vulnerabilities by statistically analyzing the constraints corresponding to each symbol and the feasibility of the final path. Currently, this method is widely used and mature for vulnerability detection, with representative tools including Oyente, Manticore, and Mythril.
[0004] Traditional automated auditing methods for contract code have the following main problems: First, the degree of automation is relatively low. The security analysis of contract code relies on the technical level and code auditing experience of relevant personnel. Manual processing or secondary verification by technical personnel is required before or after the detection. Second, there is the problem of the generalization of vulnerability types detected. Since the causes of vulnerabilities are diverse and the types of detection tools are very complex, different tools can cover different types of vulnerabilities, and their accuracy and false negative rate are not very stable.
[0005] Compared to traditional methods such as formal verification and symbolic execution, machine learning-based vulnerability detection methods demonstrate superior performance in improving execution efficiency and reducing analysis costs when dealing with large-scale input datasets. More importantly, machine learning's ability to learn automatically eliminates its reliance on manual intervention. However, in smart contract scenarios, the lack of standard machine learning vulnerability feature libraries and feature extraction methods results in a significant number of false positives and false negatives in existing AI-based detection models. Summary of the Invention
[0006] The purpose of this invention is to provide a method, device, equipment and medium for security testing of smart contracts in the power blockchain, so as to improve the intelligence and accuracy of blockchain system testing and solve the technical problems of many false alarms and missed alarms in the current methods.
[0007] To achieve the above objectives, the present invention adopts the following technical solution:
[0008] In a first aspect, the present invention provides a method for security detection of smart contracts in the power blockchain, comprising:
[0009] Obtain the code of the smart contract of the power blockchain to be tested;
[0010] Based on the code of the smart contract of the power blockchain to be tested, the control flow graph (CFG) and program dependency graph (PDG) of the smart contract of the power blockchain to be tested are obtained; based on the control flow graph (CFG) and program dependency graph (PDG), the code of the smart contract of the power blockchain to be tested is sliced to obtain code fragments; the code fragments are converted into word vectors; the features of the control flow graph (CFG) are abstracted into graph structure vectors, and the word vectors and graph structure vectors are concatenated to obtain the detection vector.
[0011] The detection vector is input into a pre-trained bidirectional LSTM similarity judgment model to obtain the detection result;
[0012] Output the detection results.
[0013] A further improvement of the present invention is that: in the step of slicing the code of the smart contract of the power blockchain to be detected according to the control flow graph CFG and the program dependency graph PDG to obtain code fragments, the smart contract code is sliced according to the control flow graph CFG and the program dependency graph PDG based on the sensitivity function to obtain code fragments.
[0014] A further improvement of the present invention is that, in the step of converting code snippets into word vectors, the word vector model word2vec is used to convert code snippets into word vectors.
[0015] A further improvement of the present invention is that: in the step of abstracting the features of the control flow graph CFG into a graph structure vector, the structure2vec algorithm is used to abstract the features of the control flow graph CFG into a graph structure vector.
[0016] A further improvement of this invention is that the training steps of the pre-trained bidirectional LSTM similarity judgment model include:
[0017] Collect vulnerable smart contract samples from public databases to establish a smart contract security vulnerability database;
[0018] The control flow graph (CFG) and program dependency graph (PDG) of the smart contract are obtained through software, and the functional points and function calls in the smart contract are obtained. Based on the control flow graph (CFG) and program dependency graph (PDG), the smart contract code is sliced according to the sensitive functions to obtain code fragments. The code fragments are cleaned to form a set of classified vulnerability samples.
[0019] For code snippets in the classification vulnerability sample set, the word vector model word2vec is used to convert them into word vectors; for the control flow graph (CFG), the structure2vec algorithm is used to abstract it into graph structure vectors; the word vectors and graph structure vectors are concatenated to obtain training data.
[0020] The bidirectional LSTM similarity judgment model is trained using training data until the loss function reaches a set value or the maximum number of training iterations are reached, thus obtaining a well-trained bidirectional LSTM similarity judgment model.
[0021] Secondly, the present invention provides a security detection device for power blockchain smart contracts, comprising:
[0022] The module retrieves the code of the smart contract of the power blockchain to be tested.
[0023] The feature extraction module is used to obtain the control flow graph (CFG) and program dependency graph (PDG) of the smart contract to be detected based on the code of the smart contract; to slice the code of the smart contract to be detected based on the control flow graph (CFG) and program dependency graph (PDG) to obtain code fragments; to convert the code fragments into word vectors; to abstract the features of the control flow graph (CFG) into graph structure vectors; and to concatenate the word vectors and graph structure vectors to obtain the detection vector.
[0024] The detection module is used to input the detection vector into a pre-trained bidirectional LSTM similarity judgment model to obtain the detection result;
[0025] The output module is used to output the detection results.
[0026] A further improvement of the present invention is that: in the step of the feature extraction module slicing the code of the smart contract to be detected in the power blockchain to obtain code fragments based on the control flow graph CFG and the program dependency graph PDG, the smart contract code is sliced according to the sensitivity function to obtain code fragments.
[0027] In the feature extraction module, the word vector model word2vec is used to convert code snippets into word vectors.
[0028] In the step of the feature extraction module to abstract the features of the control flow graph (CFG) into a graph structure vector, the structure2vec algorithm is used to abstract the features of the CFG into a graph structure vector.
[0029] A further improvement of this invention is that the training steps of the pre-trained bidirectional LSTM similarity judgment model include:
[0030] Collect vulnerable smart contract samples from public databases to establish a smart contract security vulnerability database;
[0031] The control flow graph (CFG) and program dependency graph (PDG) of the smart contract are obtained through software, and the functional points and function calls in the smart contract are obtained. Based on the control flow graph (CFG) and program dependency graph (PDG), the smart contract code is sliced according to the sensitive functions to obtain code fragments. The code fragments are cleaned to form a set of classified vulnerability samples.
[0032] For code snippets in the classification vulnerability sample set, the word vector model word2vec is used to convert them into word vectors; for the control flow graph (CFG), the structure2vec algorithm is used to abstract it into graph structure vectors; the word vectors and graph structure vectors are concatenated to obtain training data.
[0033] The bidirectional LSTM similarity judgment model is trained using training data until the loss function reaches a set value or the maximum number of training iterations are reached, thus obtaining a well-trained bidirectional LSTM similarity judgment model.
[0034] Thirdly, the present invention provides an electronic device including a processor and a memory, wherein the processor is used to execute a computer program stored in the memory to implement the aforementioned power blockchain smart contract security detection method.
[0035] Fourthly, the present invention provides a computer-readable storage medium storing at least one instruction, which, when executed by a processor, implements the power blockchain smart contract security detection method.
[0036] Compared with the prior art, the present invention has the following beneficial effects:
[0037] This invention provides a method, apparatus, device, and medium for security testing of smart contracts on the power blockchain. The method involves acquiring the code of the smart contract to be tested; obtaining the control flow graph (CFG) and program dependency graph (PDG) of the smart contract based on the code; slicing the code of the smart contract to obtain code fragments based on the CFG and PDG; converting the code fragments into word vectors; abstracting the features of the CFG into graph structure vectors; and concatenating the word vectors and graph structure vectors to obtain a detection vector; and inputting the detection vector into a pre-trained bidirectional LSTM similarity judgment model to obtain the detection result. This invention transforms the accumulated code of the system under test by testers over a long period into an intelligent model for code-level security testing, guiding testers to perform more accurate security testing of smart contracts. This invention integrates artificial intelligence algorithms into each step of the security testing process, simplifying the manpower required for each step, improving the intelligence and accuracy of blockchain system testing, and facilitating the rapid application of blockchain technology in the power industry.
[0038] This invention provides a data foundation for the analysis of smart contract security vulnerabilities by constructing a smart contract security vulnerability dataset.
[0039] This invention designs a deep learning-based smart contract security vulnerability detection technology architecture, which consists of several modules including preprocessing, program slicing, model training, and classification model training. Smart contracts can be converted into corresponding vectors through data preprocessing models, and security detection can be performed on smart contracts by comparing them with existing smart security contract templates.
[0040] This invention templates smart contract security issues and further solidifies them into features through a neural network model, providing support for solving the generalization capability of security detection.
[0041] Currently, the issue of the generalizability of detected vulnerability types is problematic. Due to the diverse causes of vulnerabilities, detection tools are extremely diverse, covering varying vulnerability types and exhibiting inconsistent accuracy and false negative rates. Furthermore, detection is time-consuming; even with tool-assisted comparative analysis, a comprehensive detection requires considerable time. This invention, however, allows for direct feature comparison and analysis of the code under test, directly locating relevant security templates, thus solving the efficiency and quality issues of smart contract security detection. Attached Figure Description
[0042] The accompanying drawings, which form part of this specification, are used to provide a further understanding of the invention. The illustrative embodiments of the invention and their descriptions are used to explain the invention and do not constitute an undue limitation of the invention. In the drawings:
[0043] Figure 1 This is a flowchart illustrating a security detection method for a smart contract on a power blockchain according to the present invention.
[0044] Figure 2 A diagram illustrating the method of slicing code in smart contracts;
[0045] Figure 3 This is a schematic diagram of the Word2Vec model structure;
[0046] Figure 4 A diagram illustrating the detection of security vulnerabilities in smart contracts based on deep learning;
[0047] Figure 5 A flowchart illustrating another method for security detection of smart contracts in the power blockchain according to the present invention;
[0048] Figure 6 A schematic diagram of the structure of a power blockchain smart contract security detection device according to the present invention;
[0049] Figure 7 A structural block diagram of an electronic device according to the present invention. Detailed Implementation
[0050] The present invention will now be described in detail with reference to the accompanying drawings and embodiments. It should be noted that, unless otherwise specified, the embodiments and features described herein can be combined with each other.
[0051] The following detailed description is exemplary and intended to provide further detailed explanation of the invention. Unless otherwise specified, all technical terms used in this invention have the same meaning as commonly understood by one of ordinary skill in the art. The terminology used in this invention is for describing particular embodiments only and is not intended to limit the scope of exemplary embodiments according to the invention.
[0052] Explanation of technical terms:
[0053] Blockchain: A model for implementing and managing transaction processing by constructing an unforgeable, tamper-proof, and traceable blockchain data structure through transparent and trusted rules in a peer-to-peer network environment.
[0054] Chained-block data structure: A data structure in which transactions occurring within a certain period are stored in units of blocks, and the blocks are linked together in chronological order using cryptographic algorithms.
[0055] Smart contract: A contract defined in digital form that can automatically execute its terms.
[0056] Encryption: The process of exchanging cryptographic data to produce ciphertext. It typically involves a set of transformations using a set of algorithms and a set of input parameters. These input parameters are usually referred to as the key.
[0057] Security testing: Verifying the security of the system under test during the research and development and maintenance process through a combination of automated testing tools and human judgment.
[0058] BiLSTM: A single-layer BiLSTM is composed of two LSTMs. One processes the input sequence in the forward direction, and the other processes the sequence in the reverse direction. After processing, the outputs of the two LSTMs are concatenated to extract long dependencies.
[0059] Struct2Vec: A graph embedding method applied to unweighted undirected graphs, defining vertex similarity from the perspective of spatial structural similarity.
[0060] Word2Vec is a word embedding method that maps words from a high-dimensional space to a low-dimensional word vector space.
[0061] Example 1
[0062] Please see Figure 1 As shown, this invention provides a method for security detection of smart contracts in the power blockchain, with the following specific steps:
[0063] S11. Establish a smart contract security vulnerability database.
[0064] A database of vulnerable smart contract samples was extensively collected from public databases, including smart contract code based on datasets from large open-source blockchains such as Ethereum, FISCO BCOS, and Fabric. This included bytecode of smart contracts using the `web3.eth.getCode` function and smart contract vulnerabilities from major vulnerability information platforms such as SWC and DASP. The database uses unified standards to collect and classify vulnerable smart contract code, and stores basic information and characteristic attributes of various types of vulnerable smart contracts.
[0065] S12. Extracting characteristics of vulnerable smart contract programs
[0066] S121, Program Slicing: Security testing is performed on smart contracts in the smart contract security vulnerability database to locate the location and type of smart contract security vulnerabilities. This invention proposes program slicing of smart contract source code based on function points. First, the control flow graph (CFG) and program dependency graph (PDG) of the smart contracts in the smart contract security vulnerability database are obtained through software, and the function points and function calls in the smart contracts are acquired.
[0067] S122. A feature extraction method is employed, using sensitivity functions to slice the smart contract code. Static text detection is then performed based on a smart contract security vulnerability database. First, based on the control flow graph (CFG) and program dependency graph (PDG), the smart contract code is sliced according to sensitivity functions to obtain code fragments. These code fragments undergo data cleaning to form a categorized vulnerability sample set.
[0068] S13. Convert features into vectors
[0069] After smart contracts are sliced, they need to be converted into vector form that can be processed by deep learning. For the code fragments (source code, intermediate code, and instruction sequences) obtained from smart contract slices, the word vector model word2vec is used to convert the "words" into word vectors. This invention uses the word2vec method to vectorize the smart contract source code. word2vec can represent words with a low-dimensional dense vector, which effectively solves the curse of dimensionality problem caused by one-hot encoding. word2vec is a simple neural network consisting of an input layer, a hidden layer, and an output layer. The training process can be viewed as training an N-gram language model through a neural network, while simultaneously calculating word vectors for smart contract keywords or parameters. The input layer of the model uses one-hot encoding, the hidden layer does not have an activation function, and the output layer has the same dimension as the input layer, using Softmax regression. word2vec includes two important models: the CBOW model and the Skip-gram model, which are structurally opposite processes.
[0070] like Figure 3 As shown, since the input of the input layer is one-hot encoded, assuming the size of the smart contract vocabulary is V, the dimension of the final word vector is N, that is, the dimension of the vector space is also V, the window size of the context vocabulary is C, and the weight sharing matrix is W = V * N.
[0071] For the control flow graph (CFG), the structure2vec algorithm is used to abstract the features of the structure graph into graph structure vectors, and the word vectors and graph structure vectors are concatenated as training data.
[0072] S14. Feature learning and judgment based on deep learning algorithms
[0073] Drawing inspiration from patch comparison techniques, this invention establishes a similarity judgment model based on bidirectional LSTM. This model compares the smart contract program to be tested with known vulnerable smart contracts, detecting vulnerabilities by determining whether the features of the tested smart contract are similar to those of the vulnerable smart contracts. This invention proposes the concept of a vulnerability template, comparing the tested smart contract program with the template to improve upon the high false positive rate of direct detection methods.
[0074] Since the code belongs to a sequence model, a recurrent neural network is used to learn features. The direct classification model consists of two Bi-LSTM layers. The process of detecting similar features in vulnerabilities is a binary classification problem, using cross-entropy as the loss function and the high-performance Adam algorithm as the optimization function. Dropout is implemented to prevent overfitting. The similarity discrimination model consists of two direct classification models, concatenating the outputs of the two direct attention layers, and finally classifying them into 0 and 1 categories by a classifier, thus determining whether the two sets of code are similar. Compared to the direct classification model, the similarity discrimination model reduces the false positive rate by judging the similarity of a large number of templates.
[0075] The bidirectional LSTM similarity judgment model is trained using the training data obtained in S13 until the loss function reaches the set value or the maximum number of training iterations are reached, thus obtaining the trained bidirectional LSTM similarity judgment model.
[0076] S15. Obtain the smart contract of the power blockchain to be tested. Use software to obtain the control flow graph (CFG) and program dependency graph (PDG) of the smart contract, and extract the function points and function calls within the smart contract. Based on the CFG and PDG, slice the code of the smart contract to be tested according to the sensitivity function to obtain code fragments. For the code fragments of the smart contract to be tested, use the word2vec model to convert them into word vectors. For the control flow graph (CFG) of the smart contract to be tested, use the structure2vec algorithm to abstract the features of the CFG into graph structure vectors. Concatenate the word vectors and graph structure vectors, and input them into a trained bidirectional LSTM similarity judgment model to obtain a classification result of 0 or 1, which is the detection result.
[0077] Example 2
[0078] Please see Figure 5 As shown, the present invention provides a security detection method for smart contracts on a power blockchain, comprising:
[0079] S1. Obtain the code of the smart contract of the power blockchain to be tested;
[0080] S2. Based on the code of the smart contract of the power blockchain to be tested, obtain the control flow graph (CFG) and program dependency graph (PDG) of the smart contract of the power blockchain to be tested; based on the control flow graph (CFG) and program dependency graph (PDG), slice the code of the smart contract of the power blockchain to be tested to obtain code fragments; convert the code fragments into word vectors; abstract the features of the control flow graph (CFG) into graph structure vectors, and concatenate the word vectors and graph structure vectors to obtain the detection vector;
[0081] S3. Input the detection vector into the pre-trained bidirectional LSTM similarity judgment model to obtain the detection result;
[0082] S4. Output the detection results.
[0083] In one specific implementation: in the step of slicing the code of the smart contract of the power blockchain to be detected according to the control flow graph CFG and the program dependency graph PDG to obtain code fragments, the smart contract code is sliced according to the control flow graph CFG and the program dependency graph PDG based on the sensitivity function to obtain code fragments.
[0084] In one specific implementation: in the step of converting code snippets into word vectors, the word vector model word2vec is used to convert code snippets into word vectors.
[0085] In one specific implementation: in the step of abstracting the features of the control flow graph (CFG) into a graph structure vector, the structure2vec algorithm is used to abstract the features of the control flow graph (CFG) into a graph structure vector.
[0086] In one specific implementation: the training steps of the pre-trained bidirectional LSTM similarity judgment model include:
[0087] Collect vulnerable smart contract samples from public databases to establish a smart contract security vulnerability database;
[0088] The control flow graph (CFG) and program dependency graph (PDG) of the smart contract are obtained through software, and the functional points and function calls in the smart contract are obtained. Based on the control flow graph (CFG) and program dependency graph (PDG), the smart contract code is sliced according to the sensitive functions to obtain code fragments. The code fragments are cleaned to form a set of classified vulnerability samples.
[0089] For code snippets in the classification vulnerability sample set, the word vector model word2vec is used to convert them into word vectors; for the control flow graph (CFG), the structure2vec algorithm is used to abstract it into graph structure vectors; the word vectors and graph structure vectors are concatenated to obtain training data.
[0090] The bidirectional LSTM similarity judgment model is trained using training data until the loss function reaches a set value or the maximum number of training iterations are reached, thus obtaining a well-trained bidirectional LSTM similarity judgment model.
[0091] Example 3
[0092] Please see Figure 6 As shown, the present invention provides a security detection device for power blockchain smart contracts, comprising:
[0093] The module retrieves the code of the smart contract of the power blockchain to be tested.
[0094] The feature extraction module is used to obtain the control flow graph (CFG) and program dependency graph (PDG) of the smart contract to be detected based on the code of the smart contract; to slice the code of the smart contract to be detected based on the control flow graph (CFG) and program dependency graph (PDG) to obtain code fragments; to convert the code fragments into word vectors; to abstract the features of the control flow graph (CFG) into graph structure vectors; and to concatenate the word vectors and graph structure vectors to obtain the detection vector.
[0095] The detection module is used to input the detection vector into a pre-trained bidirectional LSTM similarity judgment model to obtain the detection result;
[0096] The output module is used to output the detection results.
[0097] In one specific implementation, in the step of the feature extraction module slicing the code of the smart contract of the power blockchain to be detected according to the control flow graph CFG and the program dependency graph PDG to obtain code fragments, the smart contract code is sliced according to the control flow graph CFG and the program dependency graph PDG and the sensitive function to obtain code fragments.
[0098] In the feature extraction module, the word vector model word2vec is used to convert code snippets into word vectors.
[0099] In the step of the feature extraction module to abstract the features of the control flow graph (CFG) into a graph structure vector, the structure2vec algorithm is used to abstract the features of the CFG into a graph structure vector.
[0100] In one specific implementation, the training steps of the pre-trained bidirectional LSTM similarity judgment model include:
[0101] Collect vulnerable smart contract samples from public databases to establish a smart contract security vulnerability database;
[0102] The control flow graph (CFG) and program dependency graph (PDG) of the smart contract are obtained through software, and the functional points and function calls in the smart contract are obtained. Based on the control flow graph (CFG) and program dependency graph (PDG), the smart contract code is sliced according to the sensitive functions to obtain code fragments. The code fragments are cleaned to form a set of classified vulnerability samples.
[0103] For code snippets in the classification vulnerability sample set, the word vector model word2vec is used to convert them into word vectors; for the control flow graph (CFG), the structure2vec algorithm is used to abstract it into graph structure vectors; the word vectors and graph structure vectors are concatenated to obtain training data.
[0104] The bidirectional LSTM similarity judgment model is trained using training data until the loss function reaches a set value or the maximum number of training iterations are reached, thus obtaining a well-trained bidirectional LSTM similarity judgment model.
[0105] Example 4
[0106] Please see Figure 7 As shown, the present invention also provides an electronic device 100 for implementing a security detection method for smart contracts in the power blockchain; the electronic device 100 includes a memory 101, at least one processor 102, a computer program 103 stored in the memory 101 and executable on the at least one processor 102, and at least one communication bus 104.
[0107] The memory 101 can be used to store the computer program 103. The processor 102 implements the steps of the power blockchain smart contract security detection method described in Embodiment 1 or 2 by running or executing the computer program stored in the memory 101 and calling the data stored in the memory 101. The memory 101 may mainly include a program storage area and a data storage area. The program storage area may store the operating system, at least one application program required for a function (such as sound playback function, image playback function, etc.), etc.; the data storage area may store data created according to the use of the electronic device 100 (such as audio data), etc. In addition, the memory 101 may include non-volatile memory, such as hard disk, memory, plug-in hard disk, smart media card (SMC), secure digital (SD) card, flash card, at least one disk storage device, flash memory device, or other non-volatile solid-state storage device.
[0108] The at least one processor 102 may be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The processor 102 may be a microprocessor or any conventional processor. The processor 102 is the control center of the electronic device 100, connecting various parts of the electronic device 100 via various interfaces and lines.
[0109] The memory 101 in the electronic device 100 stores multiple instructions to implement a power blockchain smart contract security detection, and the processor 102 can execute the multiple instructions to achieve the following:
[0110] Obtain the code of the smart contract of the power blockchain to be tested;
[0111] Based on the code of the smart contract of the power blockchain to be tested, the control flow graph (CFG) and program dependency graph (PDG) of the smart contract of the power blockchain to be tested are obtained; based on the control flow graph (CFG) and program dependency graph (PDG), the code of the smart contract of the power blockchain to be tested is sliced to obtain code fragments; the code fragments are converted into word vectors; the features of the control flow graph (CFG) are abstracted into graph structure vectors, and the word vectors and graph structure vectors are concatenated to obtain the detection vector.
[0112] The detection vector is input into a pre-trained bidirectional LSTM similarity judgment model to obtain the detection result;
[0113] Output the detection results.
[0114] Example 5
[0115] If the modules / units integrated in the electronic device 100 are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, all or part of the processes in the methods of the above embodiments can also be implemented by a computer program instructing related hardware. The computer program can be stored in a computer-readable storage medium, and when executed by a processor, it can implement the steps of the various method embodiments described above. The computer program includes computer program code, which can be in the form of source code, object code, executable files, or certain intermediate forms. The computer-readable medium can include: any entity or device capable of carrying the computer program code, a recording medium, a USB flash drive, a portable hard drive, a magnetic disk, an optical disk, a computer memory, and a read-only memory (ROM).
[0116] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0117] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0118] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0119] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0120] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and not to limit it. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that modifications or equivalent substitutions can still be made to the specific implementation of the present invention. Any modifications or equivalent substitutions that do not depart from the spirit and scope of the present invention should be covered within the scope of protection of the claims of the present invention.
Claims
1. A security detection method for smart contracts in the power blockchain, characterized in that, include: Obtain the code of the smart contract of the power blockchain to be tested; Based on the code of the smart contract of the power blockchain to be tested, the control flow graph (CFG) and program dependency graph (PDG) of the smart contract of the power blockchain to be tested are obtained; based on the control flow graph (CFG) and program dependency graph (PDG), the code of the smart contract of the power blockchain to be tested is sliced to obtain code fragments. The code snippet is converted into word vectors; the features of the control flow graph (CFG) are abstracted into graph structure vectors, and the word vectors and graph structure vectors are concatenated to obtain the detection vector; The detection vector is input into a pre-trained bidirectional LSTM similarity judgment model. The bidirectional LSTM similarity judgment model compares the similarity of the power blockchain smart contract program to be detected with the vulnerability template corresponding to a known vulnerable smart contract program. The bidirectional LSTM similarity judgment model includes two direct classification models. Each direct classification model includes two Bi-LSTM layers and an Attention layer. The outputs of the two Attention layers are concatenated and then passed through a classifier to be divided into two categories, 0 and 1, to determine whether the features of the power blockchain smart contract program to be detected have similar features to those of the vulnerable smart contract program, and thus obtain the detection result. Output the detection results; The training steps of the pre-trained bidirectional LSTM similarity judgment model include: A smart contract security vulnerability database is established by collecting vulnerable smart contract samples from public databases. These samples include smart contract code based on datasets from large open-source blockchains such as Ethereum, FISCO BCOS, and Fabric, including bytecode of smart contracts using the `web3.eth.getCode` function, and smart contract vulnerabilities from SWC and DASP vulnerability information platforms. The database uses unified standards to collect and classify vulnerable smart contract code, and stores basic information and characteristic attributes of various types of vulnerable smart contracts. The control flow graph (CFG) and program dependency graph (PDG) of the smart contract are obtained through software, and the functional points and function calls in the smart contract are obtained. Based on the control flow graph (CFG) and program dependency graph (PDG), the smart contract code is sliced according to the sensitive functions to obtain code fragments. The code fragments are cleaned to form a set of classified vulnerability samples. For code snippets in the classification vulnerability sample set, the word vector model word2vec is used to convert them into word vectors; for the control flow graph (CFG), the structure2vec algorithm is used to abstract it into graph structure vectors; the word vectors and graph structure vectors are concatenated to obtain training data. The bidirectional LSTM similarity judgment model is trained using training data until the loss function reaches a set value or the maximum number of training iterations are reached, thus obtaining a well-trained bidirectional LSTM similarity judgment model.
2. The power blockchain smart contract security detection method according to claim 1, characterized in that, In the step of slicing the smart contract code of the power blockchain to be tested according to the control flow graph CFG and the program dependency graph PDG to obtain code fragments, the smart contract code is sliced according to the sensitivity function based on the control flow graph CFG and the program dependency graph PDG to obtain code fragments.
3. The power blockchain smart contract security detection method according to claim 1, characterized in that, In the step of converting code snippets into word vectors, the word vector model word2vec is used to convert code snippets into word vectors.
4. The power blockchain smart contract security detection method according to claim 1, characterized in that, In the step of abstracting the features of the control flow graph (CFG) into a graph structure vector, the structure2vec algorithm is used to abstract the features of the control flow graph (CFG) into a graph structure vector.
5. A power blockchain smart contract security detection device, characterized in that, include: The module retrieves the code of the smart contract of the power blockchain to be tested. The feature extraction module is used to obtain the control flow graph (CFG) and program dependency graph (PDG) of the smart contract to be tested based on its code; and to slice the code of the smart contract to be tested based on the CFG and PDG to obtain code fragments. The code snippet is converted into word vectors; the features of the control flow graph (CFG) are abstracted into graph structure vectors, and the word vectors and graph structure vectors are concatenated to obtain the detection vector; The detection module is used to input the detection vector into a pre-trained bidirectional LSTM similarity judgment model. The bidirectional LSTM similarity judgment model compares the similarity of the power blockchain smart contract program to be detected with the vulnerability template corresponding to a known vulnerable smart contract program. The bidirectional LSTM similarity judgment model includes two direct classification models. Each direct classification model includes two Bi-LSTM layers and an Attention layer. The outputs of the two Attention layers are concatenated and then passed through a classifier to divide them into two categories, 0 and 1, to determine whether the features of the power blockchain smart contract program to be detected have similar features to those of the vulnerable smart contract program, thereby obtaining the detection result. The output module is used to output the detection results; The training steps of the pre-trained bidirectional LSTM similarity judgment model include: A smart contract security vulnerability database is established by collecting vulnerable smart contract samples from public databases. These samples include smart contract code based on datasets from large open-source blockchains such as Ethereum, FISCO BCOS, and Fabric, including bytecode of smart contracts using the `web3.eth.getCode` function, and smart contract vulnerabilities from SWC and DASP vulnerability information platforms. The database uses unified standards to collect and classify vulnerable smart contract code, and stores basic information and characteristic attributes of various types of vulnerable smart contracts. The control flow graph (CFG) and program dependency graph (PDG) of the smart contract are obtained through software, and the functional points and function calls in the smart contract are obtained. Based on the control flow graph (CFG) and program dependency graph (PDG), the smart contract code is sliced according to the sensitive functions to obtain code fragments. The code fragments are cleaned to form a set of classified vulnerability samples. For code snippets in the classification vulnerability sample set, the word vector model word2vec is used to convert them into word vectors; for the control flow graph (CFG), the structure2vec algorithm is used to abstract it into graph structure vectors; the word vectors and graph structure vectors are concatenated to obtain training data. The bidirectional LSTM similarity judgment model is trained using training data until the loss function reaches a set value or the maximum number of training iterations are reached, thus obtaining a well-trained bidirectional LSTM similarity judgment model.
6. The power blockchain smart contract security detection device according to claim 5, characterized in that, In the step of the feature extraction module slicing the code of the smart contract to be tested in the power blockchain to obtain code fragments based on the control flow graph CFG and the program dependency graph PDG, the smart contract code is sliced according to the sensitivity function to obtain code fragments. In the feature extraction module, the word vector model word2vec is used to convert code snippets into word vectors. In the step of the feature extraction module to abstract the features of the control flow graph (CFG) into a graph structure vector, the structure2vec algorithm is used to abstract the features of the CFG into a graph structure vector.
7. An electronic device, characterized in that, It includes a processor and a memory, the processor being used to execute a computer program stored in the memory to implement the power blockchain smart contract security detection method as described in any one of claims 1 to 4.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores at least one instruction, which, when executed by a processor, implements the power blockchain smart contract security detection method as described in any one of claims 1 to 4.
Citation Information
Patent Citations
Ethereum intelligent contract vulnerability detection method and system based on pre-training model
CN114817932A
