Intelligent contract vulnerability detection method and system based on vulnerability doubt degree formula
By splitting the smart contract into semantic modules and using the vulnerability suspicion formula to screen suspected vulnerabilities, the problem of low detection accuracy in the existing technology is solved and high-precision smart contract vulnerability detection is achieved.
Patent Information
- Application Number
- CN202410822318.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-25
- Publication Date
- 2024-08-27
AI Technical Summary
Existing smart contract vulnerability detection methods have low detection accuracy and are prone to false positives.
Using a detection method based on the vulnerability suspicion formula, the smart contract is split into semantic modules, the bytecode is decompiled into operation codes and cut into fragments, a vulnerability detection model is formed through convolutional neural network training, and the fragments are compared during the detection process Features and tags, using the vulnerability suspicion formula to screen suspected vulnerabilities.
It improves the accuracy of vulnerability detection, avoids false positives, and enhances the ability to accurately detect smart contract vulnerabilities.
Smart Images

Figure CN118555124A
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of smart contract vulnerability detection, and in particular to a smart contract vulnerability detection method and system based on a vulnerability suspicion formula. Background Art
[0002] A smart contract is a contractual agreement that can be automatically activated on the blockchain by all parties without any other intervention when certain triggering conditions are met. The decentralized and tamper-proof features of blockchain meet the security and reliability requirements of smart contracts. The introduction and innovation of blockchain smart contracts have made blockchain a milestone, and the number of smart contracts published on the blockchain is increasing. Smart contracts are prone to potential vulnerabilities due to their programming characteristics. At the same time, since smart contracts often have financial attributes and carry huge coins, it is very easy for someone to exploit the vulnerabilities of smart contracts to attack and profit from them.
[0003] Existing smart contract vulnerability detection methods mostly use slice training after bytecode compilation to form a vulnerability detection model, and then perform vulnerability detection on the smart contract to be detected; although the above detection method can realize vulnerability detection to a certain extent; however, there is still the phenomenon that the detection accuracy is not high and false alarms are prone to occur; in order to solve the above problems, the present invention provides a smart contract vulnerability detection method and system based on a vulnerability suspicion formula. Summary of the invention
[0004] In view of the above-mentioned technical deficiencies, the purpose of the present invention is to provide a smart contract vulnerability detection method and system based on a vulnerability suspicion formula. During the detection process, not only the slice features corresponding to the vulnerability are compared, but also the labels of the slices are compared to avoid false alarms and achieve high-precision vulnerability detection, thereby solving the technical problem of low detection accuracy and easy false alarms proposed in the background technology.
[0005] To solve the above technical problems, the present invention adopts the following technical solutions: The present invention provides a smart contract vulnerability detection method based on a vulnerability suspicion formula, including: step one: splitting the smart contract in the training set into several semantic modules according to semantics; step two: decompiling the bytecodes in each semantic module into operation codes, and cutting the operation codes to form a fragment training set; step three: inputting the fragment codes in the fragment training set and their corresponding label codes into a convolutional neural network for training to obtain a vulnerability detection model; step four: extracting the bytecodes of the smart contract to be detected; decompiling and cutting the bytecodes, extracting the features of the slice codes to form a fragment feature detection set, and using the fragment feature detection set as the input of the vulnerability detection model to detect whether the smart contract has vulnerabilities.
[0006] Preferably, the splitting of the semantic modules in step one is achieved as one semantic module according to the constraints, and the rest is a semantic module with declarative semantics; the semantic modules are marked with block labels to distinguish different types of semantics; in fact, the vulnerability detection formed by the vulnerability detection model that does not consider semantic splitting is more prone to false positives; and in order to avoid false positives and improve the accuracy of vulnerability detection, the smart contract can be divided into several semantic modules according to semantics during the training phase, and the corresponding labels are marked in the fragments in the following text to identify the semantic modules. When detecting vulnerabilities, not only the fragment feature models are compared, but also the labels of the fragments and the fragment feature models are compared to improve the accuracy of vulnerability detection.
[0007] Preferably, step 2 specifically includes the following process:
[0008] A21: Decompile the bytecode. The decompilation result includes the operation code and operand.
[0009] A22: remove operands and keep opcodes;
[0010] A23: Cut the operation code into several fragments;
[0011] A24: Add corresponding label codes to the fragments;
[0012] A24: The fragments within the same semantic module constitute the fragment training set; in fact, by compiling and cutting into several fragments, the contract detection can be refined and the detection accuracy and correctness can be improved.
[0013] Preferably, the label code in A24 includes a block label and a code label; the block label identifies the semantic block where the fragment is located; and the code label distinguishes different fragments in the same semantic block.
[0014] Preferably, step three specifically includes the following:
[0015] The vulnerability detection model includes a vulnerability frequency table; different types of vulnerabilities are counted in the vulnerability frequency table; the vulnerability includes several fragment feature models; the fragment feature model also marks the corresponding label code as a feature model label code; the feature model label code only includes a block label.
[0016] Preferably, step 4 includes the following process:
[0017] B11: Inputting the fragment feature detection set into the vulnerability detection model;
[0018] B12: Filter out suspected vulnerability directories;
[0019] B13: Determine whether each suspected vulnerability in the suspected vulnerability catalog complies with the verification contract; if so, execute B14; if not, execute B15;
[0020] B14: keep the suspected vulnerability in the suspected vulnerability directory;
[0021] B15: remove the suspected vulnerability from the suspected vulnerability catalog;
[0022] B15: The suspected vulnerability directory is made into a vulnerability detection table and output.
[0023] Preferably, the suspected vulnerability catalogs screened out in B12 include the following:
[0024] Determine whether the fragment features in the fragment feature detection set meet the suspicion threshold of the vulnerability; if so, select it as a suspected vulnerability catalog, and record the relevant fragments and their corresponding label codes; the suspicion interval is 0-100%; the suspicion threshold is a certain value in the suspicion interval.
[0025] Preferably, several fragment feature models in the same vulnerability all have corresponding weights P; when detecting a fragment, its similarity S with all fragment feature models is detected; the vulnerability suspicion formula is:
[0026] F=P 1 *S 1 +P 2 *S 2 ...P n *S n .
[0027] Preferably, the process of determining whether a suspected vulnerability complies with the verification contract in B13 is as follows:
[0028] Determine whether the block labels of the fragments in the suspected vulnerability are completely different from the block labels of the fragment features in the corresponding vulnerability detection model. If so, it does not comply with the verification contract. Otherwise, it complies.
[0029] The blockchain-based smart contract vulnerability detection system includes a semantic splitting module, a compiling and cutting module, a model training module and a vulnerability detection module; the semantic splitting module is used to split the smart contracts in the training set into several semantic modules according to semantics; the compiling and cutting module is used to decompile the bytecodes in each semantic module into operation codes, cut the operation codes to form a fragment training set, and extract the bytecodes from the smart contracts to be detected; after decompiling and cutting the bytecodes, the features of the slice codes are extracted to form a fragment feature detection set;
[0030] The model training module is used to input the fragment codes and their corresponding label codes in the fragment training set into the convolutional neural network for training to obtain a vulnerability detection model; the vulnerability detection module includes a suspected vulnerability screening unit and a contract verification unit, the suspected vulnerability screening unit is used to calculate the vulnerability suspicion of each vulnerability in the fragment feature detection set, and determine whether the fragment features in the fragment feature detection set meet the suspicion threshold of the vulnerability; if so, it is selected as a suspected vulnerability catalog, and the relevant fragments and their corresponding label codes are recorded; the contract verification unit determines whether the block labels of the fragments in the suspected vulnerability are completely different from the block labels of the fragment features in the corresponding vulnerability detection model. If so, it does not comply with the verification contract.
[0031] The beneficial effects of the present invention are:
[0032] 1. The present invention splits the smart contract into several semantic modules, compiles the bytecode, cuts it into fragment sets, and annotates label codes for each fragment; then performs feature training to form a vulnerability detection model; during the detection process, not only the slice features corresponding to the vulnerability are compared, but also the slice labels are compared to avoid false positives and improve vulnerability detection accuracy.
[0033] 2. The present invention preliminarily screens the slices to be detected that meet the requirements through the vulnerability suspicion formula; and then determines whether each suspected vulnerability in the suspected vulnerability catalog meets the verification contract to improve the vulnerability detection accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0035] Figure 1 A flowchart of a smart contract vulnerability detection method based on a vulnerability suspicion formula provided in an embodiment of the present invention.
[0036] Figure 2 The flowchart of bytecode compilation and cutting to form a fragment training set in the present invention.
[0037] Figure 3 It is a flow chart of detecting vulnerabilities in the present invention. DETAILED DESCRIPTION
[0038] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0039] Example: Figures 1 to 3 As shown, the present invention provides a smart contract vulnerability detection method based on a vulnerability suspicion formula, comprising:
[0040] Step 1: Split the smart contracts in the training set into several semantic modules according to semantics. Specifically, the semantic modules are split into a semantic module according to constraints, and the rest are a semantic module based on statement semantics; the semantic modules are annotated with block labels to distinguish different types of semantics; in fact, vulnerability detection formed by vulnerability detection models that do not consider semantic splitting is more prone to false positives; in order to avoid false positives and improve vulnerability detection accuracy, the smart contracts can be divided into several semantic modules according to semantics during the training phase, and the corresponding labels can be annotated in the fragments in the following text to identify the semantic modules. When detecting vulnerabilities, not only the fragment feature models are compared, but also the labels of the fragments and fragment feature models are compared to improve vulnerability detection accuracy.
[0041] Step 2: Decompile the bytecodes in each semantic module into opcodes, and cut the opcodes into fragment training sets. The specific process includes the following:
[0042] A21: Decompile the bytecode. The decompilation result includes the operation code and operand.
[0043] A22: remove operands and keep opcodes;
[0044] A23: Cut the operation code into several fragments;
[0045] A24: Add corresponding label codes to the fragments;
[0046] A24: The fragments within the same semantic module constitute the fragment training set; the label code includes a block label and a code label; the block label identifies the semantic block where the fragment is located; the code label distinguishes different fragments in the same semantic block; by compiling and cutting into several fragments, the contract detection can be refined and the detection accuracy and correctness can be improved.
[0047] Step 3: Input the fragment codes and their corresponding label codes in the fragment training set into the convolutional neural network for training to obtain a vulnerability detection model. Specifically, it includes the following: the vulnerability detection model includes a vulnerability frequency table; different types of vulnerabilities are counted in the vulnerability frequency table; the vulnerability includes several fragment feature models; the fragment feature model also annotates the corresponding label code as a feature model label code; the feature model label code only includes a block label.
[0048] Step 4: Extract bytecodes from the smart contract to be tested; decompile and slice the bytecodes, extract the features of the sliced codes to form a fragment feature detection set, and use the fragment feature detection set as the input of the vulnerability detection model to detect whether there are vulnerabilities in the smart contract. The process includes the following:
[0049] B11: Inputting the fragment feature detection set into the vulnerability detection model;
[0050] B12: Filter out suspected vulnerability directories; the suspected vulnerability directories filtered out include the following:
[0051] Determine whether the fragment features in the fragment feature detection set meet the suspicion threshold of the vulnerability; if so, select it as a suspected vulnerability catalog, and record the relevant fragments and their corresponding label codes; the suspicion interval is 0-100%; the suspicion threshold is a certain value in the suspicion interval.
[0052] B13: Determine whether each suspected vulnerability in the suspected vulnerability catalog complies with the verification contract; if so, execute B14; if not, execute B15;
[0053] B14: keep the suspected vulnerability in the suspected vulnerability directory;
[0054] B15: remove the suspected vulnerability from the suspected vulnerability catalog;
[0055] B15: The suspected vulnerability directory is made into a vulnerability detection table and output.
[0056] Specifically, several fragment feature models in the same vulnerability all have corresponding weights P; when detecting a fragment, its similarity S with all fragment feature models is detected; the vulnerability suspicion formula is:
[0057] F=P1*S1+P2*S2...Pn*Sn.
[0058] In fact, the process of determining whether a suspected vulnerability meets the verification contract is as follows:
[0059] Determine whether the block labels of the fragments in the suspected vulnerability are completely different from the block labels of the fragment features in the corresponding vulnerability detection model. If so, it does not comply with the verification contract. Otherwise, it complies.
[0060] When this embodiment is actually used, the smart contract is split into several semantic modules, the bytecode is compiled and cut into a set of fragments, and the label code is annotated for each fragment; then feature training is performed to form a vulnerability detection model; in the detection process, not only the slice features corresponding to the vulnerability are compared, but also the slice labels are compared to avoid false positives and improve the vulnerability detection accuracy. At the same time, the vulnerability suspicion formula is used to preliminarily screen the slices to be tested that meet the requirements; and then the vulnerability detection accuracy is improved by judging whether each suspected vulnerability in the suspected vulnerability catalog meets the verification contract.
[0061] Embodiment 2:
[0062] The blockchain-based smart contract vulnerability detection system includes a semantic splitting module, a compile and cut module, a model training module and a vulnerability detection module; the semantic splitting module is used to split the smart contract in the training set into several semantic modules according to semantics; the compile and cut module is used to decompile the bytecode in each semantic module into an operation code, cut the operation code to form a fragment training set, and extract the bytecode from the smart contract to be detected; after decompiling and cutting the bytecode, the features of the slice code are extracted to form a fragment feature detection set; the model training module is used to input the fragment code in the fragment training set and its corresponding label code into a convolutional neural network for training to obtain a vulnerability detection model; the vulnerability detection module includes a suspected vulnerability screening unit and a contract verification unit, the suspected vulnerability screening unit is used to calculate the vulnerability suspicion of each vulnerability in the fragment feature detection set, and judge whether the fragment feature in the fragment feature detection set meets the suspicion threshold of the vulnerability; if it meets, it is selected as a suspected vulnerability directory, and the relevant fragments and their corresponding label codes are recorded; the contract verification unit determines whether the block label of the fragment in the suspected vulnerability is completely different from the block label of the fragment feature in the corresponding vulnerability detection model, if so, it does not meet the verification contract. In this embodiment, the smart contract is split into several semantic modules, the bytecode is compiled and cut into fragment sets, and each fragment is labeled with a label code; feature training is then performed to form a vulnerability detection model; during the detection process, not only the slice features corresponding to the vulnerability are compared, but also the slice labels are compared to avoid false alarms and improve the accuracy of vulnerability detection.
[0063] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.
Claims
1. A smart contract vulnerability detection method based on a vulnerability suspicion formula, characterized in that: include: Step 1: Split the smart contracts in the training set into several semantic modules according to semantics; Step 2: Decompile the bytecodes in each semantic module into opcodes, and cut the opcodes into fragment training sets; Step 3: Input the fragment codes and their corresponding label codes in the fragment training set into the convolutional neural network for training to obtain a vulnerability detection model; Step 4: Extract the bytecode of the smart contract to be tested; After decompiling and cutting the bytecode, the features of the sliced code are extracted to form a fragment feature detection set. The fragment feature detection set is used as the input of the vulnerability detection model to detect whether there are vulnerabilities in the smart contract.
2. The smart contract vulnerability detection method based on the vulnerability suspicion formula as claimed in claim 1, characterized in that: In step 1, the semantic modules are split into one semantic module according to the constraints, and the rest are divided into one semantic module based on the declarative semantics; the semantic modules are marked with block labels to distinguish different types of semantics.
3. The smart contract vulnerability detection method based on the vulnerability suspicion formula as described in claim 2 is characterized in that: Step 2 specifically includes the following process: A21: Decompile the bytecode. The decompilation result includes the operation code and operand. A22: remove operands and keep opcodes; A23: Cut the operation code into several fragments; A24: Add corresponding label codes to the fragments; A24: The fragments within the same semantic module constitute the fragment training set.
4. The smart contract vulnerability detection method based on the vulnerability suspicion formula as claimed in claim 3 is characterized in that: The label code in A24 includes a block label and a code label; the block label identifies the semantic block where the fragment is located; and the code label distinguishes different fragments in the same semantic block.
5. The smart contract vulnerability detection method based on the vulnerability suspicion formula as described in claim 4 is characterized in that: Step three specifically includes the following: The vulnerability detection model includes a vulnerability frequency table; different types of vulnerabilities are counted in the vulnerability frequency table; the vulnerability includes several fragment feature models; the fragment feature model also marks the corresponding label code as a feature model label code; the feature model label code only includes a block label.
6. The smart contract vulnerability detection method based on the vulnerability suspicion formula as claimed in claim 5, characterized in that: Step 4 includes the following process: B11: Inputting the fragment feature detection set into the vulnerability detection model; B12: Filter out suspected vulnerability directories; B13: Determine whether each suspected vulnerability in the suspected vulnerability catalog complies with the verification contract; if so, execute B14; if not, execute B15; B14: keep the suspected vulnerability in the suspected vulnerability directory; B15: remove the suspected vulnerability from the suspected vulnerability catalog; B15: The suspected vulnerability directory is made into a vulnerability detection table and output.
7. The smart contract vulnerability detection method based on the vulnerability suspicion formula as described in claim 6 is characterized in that: The suspected vulnerability directories screened out in B12 include the following: Determine whether the fragment features in the fragment feature detection set meet the suspicion threshold of the vulnerability; if so, select it as a suspected vulnerability catalog, and record the relevant fragments and their corresponding label codes; the suspicion interval is 0-100%; the suspicion threshold is a certain value in the suspicion interval.
8. The smart contract vulnerability detection method based on the vulnerability suspicion formula as claimed in claim 8, characterized in that: Several fragment feature models in the same vulnerability all have corresponding weights P; when detecting a fragment, its similarity S with all fragment feature models is detected; the vulnerability suspicion formula is: F=P1*S1+P2*S2...P n *S n 。 9. The smart contract vulnerability detection method based on the vulnerability suspicion formula as claimed in claim 8, characterized in that: The process of determining whether a suspected vulnerability meets the verification contract in B13 is as follows: Determine whether the block labels of the fragments in the suspected vulnerability are completely different from the block labels of the fragment features in the corresponding vulnerability detection model. If so, it does not comply with the verification contract. Otherwise, it complies.
10. The blockchain-based smart contract vulnerability detection system according to any one of claims 1 to 9, characterized in that: It includes semantic splitting module, compilation and cutting module, model training module and vulnerability detection module; The semantic splitting module is used to split the smart contract in the training set into several semantic modules according to semantics; the compilation and cutting module is used to decompile the bytecode in each semantic module into operation code, cut the operation code to form a fragment training set, and extract the bytecode from the smart contract to be detected; after decompiling and cutting the bytecode, the features of the slice code are extracted to form a fragment feature detection set; The model training module is used to input the fragment codes and their corresponding label codes in the fragment training set into the convolutional neural network for training to obtain a vulnerability detection model; The vulnerability detection module includes a suspected vulnerability screening unit and a contract verification unit. The suspected vulnerability screening unit is used to calculate the vulnerability suspicion of each vulnerability in the fragment feature detection set, and determine whether the fragment features in the fragment feature detection set meet the suspicion critical value of the vulnerability; if so, it is selected as a suspected vulnerability directory, and the relevant fragments and their corresponding label codes are recorded; the contract verification unit determines whether the block labels of the fragments in the suspected vulnerability are completely different from the block labels of the fragment features in the corresponding vulnerability detection model. If so, it does not comply with the verification contract.
