A smart contract vulnerability analysis method based on blockchain
By migrating and fine-tuning the Code2Vec pre-trained model and building a natural-code joint semantic encoder, combining the binary convergence iteration method and the natural-code joint semantic matcher, the limitations of positioning and detection of vulnerabilities in smart contract vulnerability analysis are solved, and effective identification and positioning of any smart contract language and complex vulnerabilities are achieved.
Patent Information
- Application Number
- CN202510216691.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-26
- Publication Date
- 2025-05-13
- Estimated Expiration
- 2045-02-26
AI Technical Summary
Existing smart contract vulnerability analysis methods have limitations in locating and detecting vulnerabilities, especially for niche smart contract languages and complex vulnerabilities involving interactions between multiple lines of code.
By performing migration fine-tuning of the Code2Vec pre-trained model, combining natural language autoencoder and data augmentation technology, a natural-code joint semantic encoder and smart contract vulnerability locator is built, and the closest vulnerability entries are found from the smart contract vulnerability library using the binary convergence iteration method and the natural-code joint semantic matcher.
It realizes the location and identification of vulnerable code segments of any number of lines without relying on semantic extraction tools in the supporting ecosystem of specific smart contract languages, effectively reducing the risk of smart contracts in niche smart contract languages being attacked and able to detect complex multi-line interaction vulnerabilities.
Smart Images

Figure CN119720224B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of smart contract vulnerability analysis, and specifically refers to a smart contract vulnerability analysis method based on blockchain. Background Art
[0002] Smart contract vulnerability analysis method refers to the method of conducting security analysis on smart contracts running on blockchain platforms. Since smart contracts are difficult to modify once deployed on the blockchain, it is crucial to conduct a thorough security analysis and vulnerability detection before the contract is released.
[0003] Existing smart contract vulnerability analysis methods mainly include two technical routes: formal verification and deep learning. Formal verification formally verifies the contract source code by defining its logical language. It can determine whether the code complies with the given formal specification description. However, due to the rapid changes in smart contract versions and the difficulty in deriving formal specifications, the vulnerability detection technical methods based on formal verification are not very universal and scalable.
[0004] Existing deep learning-based vulnerability detection has good scalability and adaptability, but most of them cannot locate possible vulnerabilities or corresponding lines of code like other detection methods. The few deep learning models represented by the ContractCheck model that claim to be able to perform fine-grained vulnerability detection also contain two problems. The first is that they over-rely on semantic extraction tools in the supporting ecosystem of specific smart contract languages, but many niche smart contract languages do not have supporting semantic extraction tools, and therefore cannot provide vulnerability detection for smart contracts based on niche smart contract languages, ultimately putting those smart contracts based on niche smart contract languages at risk of being attacked. The second is that the positioning is limited to a single line of code, which will result in missing more complex vulnerabilities involving interactions between multiple lines of code, ultimately leaving hidden dangers of smart contract attacks. Summary of the invention
[0005] In view of the above situation, in order to overcome the defects of the prior art, the present invention provides a smart contract vulnerability analysis method based on blockchain. In view of the problems in the prior art, it is proposed to migrate and fine-tune the Code2Vec pre-trained model to extract code semantic information, and use the smart contract vulnerability locator to detect vulnerability code segments of any number of lines using the binary convergence iteration method. Finally, a natural-code joint semantic matcher is used to find the vulnerability entry closest to the target vulnerability from the smart contract vulnerability library. The present invention organically combines transfer learning with model fine-tuning technology, and realizes smart contract vulnerability analysis that can locate vulnerability code segments of any number of lines without relying on semantic extraction tools.
[0006] The technical solution adopted by the present invention is as follows: The present invention provides a smart contract vulnerability analysis method based on blockchain, and the method comprises the following steps:
[0007] Step S1: Access the smart contract vulnerability library and use data enhancement technology to develop a smart contract vulnerability dataset;
[0008] Step S2: Build a natural language autoencoder and use the smart contract vulnerability dataset to train the natural language autoencoder. The natural language autoencoder includes a Word2Vec pre-trained model, an autoencoder, a bottleneck layer, and an autodecoder.
[0009] Step S3: Load the Code2Vec pre-trained model and use the natural language autoencoder to perform migration fine-tuning on the Code2Vec pre-trained model;
[0010] Step S4: Use the natural language autoencoder and Code2Vec pre-trained model to build a natural-code joint semantic encoder and use the smart contract vulnerability dataset to further train the natural-code joint semantic encoder;
[0011] Step S5: construct a smart contract vulnerability locator using a natural-code joint semantic encoder and train the smart contract vulnerability locator using a smart contract vulnerability dataset;
[0012] Step S6: Use the smart contract vulnerability locator to locate the vulnerability of the smart contract using the binary convergence iteration method to obtain the target vulnerability code segment and the vulnerability type of the target vulnerability code segment;
[0013] Step S7: construct a natural-code joint semantic matcher using a natural-code joint semantic encoder;
[0014] Step S8: Use the nature-code joint semantic matcher to find the vulnerability entry closest to the target vulnerability from the smart contract vulnerability library.
[0015] Furthermore, the step S1 specifically includes the following steps:
[0016] Step S11: access the smart contract vulnerability library, collect all the smart contract codes, vulnerability code segments, vulnerability types and vulnerability descriptions corresponding to each smart contract vulnerability entry from the smart contract vulnerability library, and summarize them into original vulnerability instances;
[0017] Step S12: Collect smart contracts without vulnerabilities, randomly intercept code segments from the smart contracts without vulnerabilities, aggregate all smart contract codes of the smart contracts without vulnerabilities and the randomly intercepted code segments into negative sample vulnerability instances, fill the vulnerability type and vulnerability description of the negative sample vulnerability instances with 0, add vulnerability attributes to the negative sample vulnerability instances, and mark the vulnerability attributes of the negative sample vulnerability instances as non-vulnerable code segments;
[0018] Step S13: Perform data enhancement by randomly hiding all the smart contract codes of the original vulnerability instance and the negative sample vulnerability instance, and generate a data enhanced vulnerability instance: randomly generate a context hidden number for each original vulnerability instance and the negative sample vulnerability instance, where the context hidden number is an integer. When the context hidden number is a positive number, delete the code lines from the first line of the entire smart contract code downwards, which is equal to the absolute value of the context hidden number. When the context hidden number is a negative number, delete the code lines from the last line of the entire smart contract code upwards, which is equal to the absolute value of the context hidden number, to generate a data enhanced vulnerability instance, and add vulnerability attributes to the original vulnerability instance and the data enhanced vulnerability instance. The vulnerability attributes of the original vulnerability instance and the data enhanced vulnerability instance are vulnerable code segments;
[0019] Step S14: Integrate the original vulnerability instances, negative sample vulnerability instances, and data enhanced vulnerability instances into a smart contract vulnerability dataset.
[0020] Furthermore, the step S2 specifically includes the following steps:
[0021] Step S21: Load the Word2Vec pre-trained model and construct an autoencoder, wherein the autoencoder includes an autoencoder, a bottleneck layer and an autodecoder, and connect the Word2Vec pre-trained model to the autoencoder to construct a natural language autoencoder;
[0022] Step S22: specify the entire smart contract code of the smart contract vulnerability dataset as the input of the Word2Vec pre-training model, specify the vulnerability description of the smart contract vulnerability dataset as the target output of the automatic decoder, and train the natural language autoencoder.
[0023] Furthermore, the step S3 specifically includes the following steps:
[0024] Step S31: Divide the smart contract vulnerability dataset into different batches in equal proportions, load the Code2Vec pre-trained model and download the Code2Vec pre-trained dataset, extract the automatic decoder from the natural language autoencoder, connect the Code2Vec pre-trained model to the automatic decoder, set the maximum original task accuracy drop value, and test the Code2Vec pre-trained model with the Code2Vec pre-trained dataset to obtain the initial original task accuracy;
[0025] Step S32: specify all the smart contract codes in the smart contract vulnerability dataset as input, and the vulnerability description as the target output, and train the Code2Vec pre-trained model. During the training process, after each batch of training is completed, the Code2Vec pre-trained model is tested with the Code2Vec pre-trained dataset to obtain the original task accuracy after the completion of this batch of training and the original task accuracy reduction value caused by this batch of training;
[0026] Step S33: If the original task accuracy after all batches of training are completed is less than the value obtained by subtracting the maximum original task accuracy drop from the initial original task accuracy, then roll back the weights of the Code2Vec pre-training model to the state before the start of training and disable the batch with the highest original task accuracy drop from the subsequent training of the Code2Vec pre-training model;
[0027] Step S34: Repeat steps S32 and S33 until the original task accuracy after all batches of training are completed is higher than the value obtained by subtracting the maximum original task accuracy drop value from the initial original task accuracy.
[0028] Furthermore, the step S4 specifically includes the following steps:
[0029] Step S41: resetting the weights of the autoencoder and the autodecoder in the natural language autoencoder to construct a concatenated fusion layer;
[0030] Step S42: connect the natural language autoencoder to the splicing fusion layer after truncating the automatic decoder, connect the Code2Vec pre-trained model to the splicing fusion layer, and construct a natural-code joint semantic encoder;
[0031] Step S43: Connect the concatenated fusion layer to the automatic decoder, specify the entire smart contract code in the smart contract vulnerability dataset as the input of the Code2Vec pre-trained model and the Word2Vec pre-trained model, specify the vulnerability code segment in the smart contract vulnerability dataset as the target output of the automatic decoder, and train the natural-code joint semantic encoder.
[0032] Furthermore, the step S5 specifically includes the following steps:
[0033] Step S51: construct a first multi-layer perceptron and a second multi-layer perceptron, disconnect the splicing fusion layer from the automatic decoder, connect the splicing fusion layer to the first multi-layer perceptron and the second multi-layer perceptron, and construct a smart contract vulnerability locator;
[0034] Step S52: Encode the vulnerability attribute of the smart contract vulnerability data set, the code segment with a vulnerability is set to 1, the code segment without a vulnerability is set to 0, and the vulnerability type of the smart contract vulnerability data set is one-hot encoded;
[0035] Step S53: Specify the loss function of the first multi-layer perceptron as bianry_crossentropy, specify the loss function of the second multi-layer perceptron as categorical_crossentropy, specify the entire smart contract code as the input of the Code2Vec pre-trained model and the Word2Vec pre-trained model, specify the vulnerability attribute as the target output of the first multi-layer perceptron, specify the vulnerability type as the target output of the second multi-layer perceptron, and train the smart contract vulnerability locator.
[0036] Furthermore, the step S6 specifically includes the following steps:
[0037] Step S61: Use the smart contract vulnerability locator to perform an initial detection on the entire code of the smart contract. If the first multi-layer perceptron determines that the entire code of the smart contract is a vulnerable code segment, start binary detection;
[0038] Step S62: Perform binary detection: Perform the first binary detection on the vulnerable code segment, and input the two binary code segments into the smart contract vulnerability locator to detect whether they are vulnerable code segments. If there are still unconfirmed vulnerable code segments after the first binary detection, continue to perform the second binary detection, and divide the vulnerable sub-segment into two segments again, and continue to detect until the two binary code segments are determined as non-vulnerable code segments by the first multi-layer perceptron;
[0039] Step S63: Splicing the two code segments after the binary division to generate a spliced code segment, and designating the first line of the spliced code segment as a code segment that converges from top to bottom;
[0040] Step S64: top-to-bottom convergence detection: the top-to-bottom convergence code segment is input into the smart contract vulnerability locator. If the first multi-layer perceptron determines that the top-to-bottom convergence code segment is a non-vulnerable code segment, the next line after the last line in the top-to-bottom convergence code segment is added to the top-to-bottom convergence code segment. This step is repeated until the top-to-bottom convergence code segment is determined to be a vulnerable code segment.
[0041] Step S65: designate the last line of the top-to-bottom convergent code segment as the bottom-to-top convergent code segment;
[0042] Step S66: bottom-to-top convergence detection: the bottom-to-top convergent code segment is input into the smart contract vulnerability locator. If the first multi-layer perceptron determines that the bottom-to-top convergent code segment is a non-vulnerable code segment, the previous line of the first line in the bottom-to-top code segment is added to the bottom-to-top convergent code segment. This step is repeated until the bottom-to-top convergent code segment is determined to be a vulnerable code segment.
[0043] Step S67: taking the bottom-to-top convergent code segment as the target vulnerability code segment, and the output of the second multi-layer perceptron at this time is the vulnerability type of the target vulnerability code segment.
[0044] Furthermore, the step S7 specifically includes the following steps:
[0045] Step S71: construct a cosine similarity merging layer to create a first natural-code joint semantic encoder instance and a second natural-code joint semantic encoder instance;
[0046] Step S72: Connect the first natural-code joint semantic encoder instance and the second natural-code joint semantic encoder instance to the cosine similarity merging layer to construct a natural-code joint semantic matcher.
[0047] Furthermore, the step S8 specifically includes the following steps:
[0048] Step S81: Select smart contract vulnerability entries with the same vulnerability type as the target vulnerability code segment from the smart contract vulnerability library, and aggregate them into a matching vulnerability entry set;
[0049] Step S82: select a matching vulnerability entry from the matching vulnerability entry set in alphabetical order of the title, designate the target vulnerability code segment as the input of the first natural-code joint semantic encoder, designate the vulnerability code segment of the matching vulnerability entry as the input of the second natural-code joint semantic encoder, and the natural-code joint semantic matcher outputs the natural-code joint cosine matching degree, and repeat this step until each vulnerability entry in the matching vulnerability entry set has a corresponding natural-code joint cosine matching degree;
[0050] Step S83: Select the vulnerability entry with the highest natural-code joint cosine matching degree as the closest vulnerability entry.
[0051] The beneficial effects achieved by the present invention using the above scheme are as follows:
[0052] (1) The present invention provides a smart contract vulnerability analysis method based on blockchain. In view of the problems in the prior art, it is proposed to transfer and fine-tune the Code2Vec pre-trained model to extract code semantic information, and use a smart contract vulnerability locator to detect vulnerability code segments of any number of lines using a binary convergence iteration method. Finally, a natural-code joint semantic matcher is used to find the vulnerability entry closest to the target vulnerability from the smart contract vulnerability library. The present invention organically combines transfer learning with model fine-tuning technology to achieve smart contract vulnerability analysis that can locate vulnerability code segments of any number of lines without relying on semantic extraction tools.
[0053] (2) Since the Code2Vec pre-trained model is trained using Java code, and the language used in smart contracts is somewhat different from Java, the present invention is based on the latent space theory. By first constructing a natural language autoencoder, the reconstruction function of the decoder in the natural language autoencoder is then used to align the code semantic representation of the Code2Vec pre-trained model with the latent space of the smart contract. At the same time, the hard constraint on the maximum accuracy drop of the original task is implemented in the transfer learning process through the screening of the training set and the weight rollback method, which effectively prevents catastrophic forgetting in the transfer learning process. The present invention fine-tunes the Code2Vec pre-trained model through migration, generalizes the Code2Vec pre-trained model to the smart contract language, and retains the code semantic extraction capability, thereby realizing code semantic extraction of the code of any smart contract language, and finally realizing accurate identification of smart contract vulnerabilities, effectively reducing the risk of smart contracts based on niche smart contract languages being attacked.
[0054] (3) The nature-code joint semantic encoder proposed in the present invention combines the autoencoder, data augmentation technology and transfer learning technology to extract the nature-code joint semantic vector from the code segment of any length, laying the foundation for the realization of the smart contract vulnerability locator and the nature-code joint semantic matcher. It is combined with the binary convergence iteration method proposed in the present invention to finally locate the vulnerable code segment of any length at any position and determine its vulnerability type without relying on the semantic extraction tools in the supporting ecosystem of the specific smart contract language, and effectively detect more complex vulnerabilities involving interactions between multiple code lines.
[0055] (4) The nature-code joint semantic matcher proposed in the present invention is based on the principle of comparing the cosine similarity between the nature-code joint semantic vectors of two vulnerable code segments. Compared with the existing hash value matching, it has better interpretability and does not rely on the semantic extraction tools in the supporting ecosystem of a specific smart contract language. It achieves interpretable vulnerability matching that is universal to any language, providing an effective basis for repairing smart contract vulnerabilities. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] Figure 1 Schematic diagram of a natural language autoencoder;
[0057] Figure 2 Schematic diagram of migration and fine-tuning of the Code2Vec pre-trained model;
[0058] Figure 3 Schematic diagram of the connection structure between the natural-code joint semantic encoder and the automatic decoder;
[0059] Figure 4 This is a schematic diagram of the smart contract vulnerability locator;
[0060] Figure 5 It is a schematic diagram of the bisection convergence iteration method;
[0061] Figure 6 Schematic diagram of the nature-code joint semantic matcher.
[0062] The accompanying drawings are used to provide further understanding of the present invention and constitute a part of the specification. They are used to explain the present invention together with the embodiments of the present invention and do not constitute a limitation of the present invention. DETAILED DESCRIPTION
[0063] The technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments; based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.
[0064] Embodiment 1, the present invention provides a smart contract vulnerability analysis method based on blockchain, the method comprising the following steps:
[0065] Step S1: access the smart contract vulnerability database and use data enhancement technology to formulate a smart contract vulnerability dataset. In this embodiment, the smart contract vulnerability database is Smart Contract VulnDB;
[0066] Step S2: Build a natural language autoencoder and use the smart contract vulnerability dataset to train the natural language autoencoder. The natural language autoencoder includes a Word2Vec pre-trained model, an autoencoder, a bottleneck layer, and an autodecoder.
[0067] Step S3: Load the Code2Vec pre-trained model and use the natural language autoencoder to perform migration fine-tuning on the Code2Vec pre-trained model;
[0068] Step S4: Use the natural language autoencoder and Code2Vec pre-trained model to build a natural-code joint semantic encoder and use the smart contract vulnerability dataset to further train the natural-code joint semantic encoder;
[0069] Step S5: construct a smart contract vulnerability locator using a natural-code joint semantic encoder and train the smart contract vulnerability locator using a smart contract vulnerability dataset;
[0070] Step S6: Use the smart contract vulnerability locator to locate the vulnerability of the smart contract using the binary convergence iteration method to obtain the target vulnerability code segment and the vulnerability type of the target vulnerability code segment;
[0071] Step S7: construct a natural-code joint semantic matcher using a natural-code joint semantic encoder;
[0072] Step S8: Use the nature-code joint semantic matcher to find the vulnerability entry closest to the target vulnerability from the smart contract vulnerability library.
[0073] Embodiment 2: This embodiment is based on the above embodiment, and step S1 specifically includes the following steps:
[0074] Step S11: access the smart contract vulnerability library, collect all the smart contract codes, vulnerability code segments, vulnerability types and vulnerability descriptions corresponding to each smart contract vulnerability entry from the smart contract vulnerability library, and summarize them into original vulnerability instances;
[0075] Step S12: Collect smart contracts without vulnerabilities, randomly intercept code segments from the smart contracts without vulnerabilities, aggregate all smart contract codes of the smart contracts without vulnerabilities and the randomly intercepted code segments into negative sample vulnerability instances, fill the vulnerability type and vulnerability description of the negative sample vulnerability instances with 0, add vulnerability attributes to the negative sample vulnerability instances, and mark the vulnerability attributes of the negative sample vulnerability instances as non-vulnerable code segments;
[0076] Step S13: Perform data enhancement by randomly hiding all the smart contract codes of the original vulnerability instance and the negative sample vulnerability instance, and generate a data enhanced vulnerability instance: randomly generate a context hidden number for each original vulnerability instance and the negative sample vulnerability instance, where the context hidden number is an integer. When the context hidden number is a positive number, delete the code lines from the first line of the entire smart contract code downwards, which is equal to the absolute value of the context hidden number. When the context hidden number is a negative number, delete the code lines from the last line of the entire smart contract code upwards, which is equal to the absolute value of the context hidden number, to generate a data enhanced vulnerability instance, and add vulnerability attributes to the original vulnerability instance and the data enhanced vulnerability instance. The vulnerability attributes of the original vulnerability instance and the data enhanced vulnerability instance are vulnerable code segments;
[0077] Step S14: Integrate the original vulnerability instances, negative sample vulnerability instances, and data enhanced vulnerability instances into a smart contract vulnerability dataset.
[0078] Example 3, see Figure 1 This embodiment is based on the above embodiment, and step S2 specifically includes the following steps:
[0079] Step S21: Load the Word2Vec pre-trained model and construct an autoencoder, wherein the autoencoder includes an autoencoder, a bottleneck layer and an autodecoder, and connect the Word2Vec pre-trained model to the autoencoder to construct a natural language autoencoder;
[0080] Step S22: specify the entire smart contract code of the smart contract vulnerability dataset as the input of the Word2Vec pre-training model, specify the vulnerability description of the smart contract vulnerability dataset as the target output of the automatic decoder, and train the natural language autoencoder.
[0081] Example 4, see Figure 2 This embodiment is based on the above embodiment, and step S3 specifically includes the following steps:
[0082] Step S31: Divide the smart contract vulnerability dataset into different batches in equal proportion, load the Code2Vec pre-trained model and download the Code2Vec pre-trained dataset, extract the automatic decoder from the natural language autoencoder, connect the Code2Vec pre-trained model to the automatic decoder, set the maximum original task accuracy drop value, and test the Code2Vec pre-trained model with the Code2Vec pre-trained dataset to obtain the initial original task accuracy. In this embodiment, the Code2Vec pre-trained dataset is java14m_data. In this embodiment, a total of 100 batches are divided, and the maximum original task accuracy drop value is set to 0.05;
[0083] Step S32: specify all the smart contract codes in the smart contract vulnerability dataset as input, and the vulnerability description as the target output, and train the Code2Vec pre-trained model. During the training process, after each batch of training is completed, the Code2Vec pre-trained model is tested with the Code2Vec pre-trained dataset to obtain the original task accuracy after the completion of this batch of training and the original task accuracy reduction value caused by this batch of training;
[0084] Step S33: If the original task accuracy after all batches of training are completed is less than the value obtained by subtracting the maximum original task accuracy drop from the initial original task accuracy, then roll back the weights of the Code2Vec pre-training model to the state before the start of training and disable the batch with the highest original task accuracy drop from the subsequent training of the Code2Vec pre-training model;
[0085] Step S34: Repeat steps S32 and S33 until the original task accuracy after all batches of training are completed is higher than the value obtained by subtracting the maximum original task accuracy drop value from the initial original task accuracy.
[0086] Example 5, see Figure 3 This embodiment is based on the above embodiment, and step S4 specifically includes the following steps:
[0087] Step S41: resetting the weights of the autoencoder and the autodecoder in the natural language autoencoder to construct a concatenated fusion layer;
[0088] Step S42: connect the natural language autoencoder to the splicing fusion layer after truncating the automatic decoder, connect the Code2Vec pre-trained model to the splicing fusion layer, and construct a natural-code joint semantic encoder;
[0089] Step S43: Connect the concatenated fusion layer to the automatic decoder, specify the entire smart contract code in the smart contract vulnerability dataset as the input of the Code2Vec pre-trained model and the Word2Vec pre-trained model, specify the vulnerability code segment in the smart contract vulnerability dataset as the target output of the automatic decoder, and train the natural-code joint semantic encoder.
[0090] Example 6, see Figure 4 This embodiment is based on the above embodiment, and step S5 specifically includes the following steps:
[0091] Step S51: construct a first multi-layer perceptron and a second multi-layer perceptron, disconnect the splicing fusion layer from the automatic decoder, connect the splicing fusion layer to the first multi-layer perceptron and the second multi-layer perceptron, and construct a smart contract vulnerability locator;
[0092] Step S52: Encode the vulnerability attribute of the smart contract vulnerability data set, the code segment with a vulnerability is set to 1, the code segment without a vulnerability is set to 0, and the vulnerability type of the smart contract vulnerability data set is one-hot encoded;
[0093] Step S53: Specify the loss function of the first multi-layer perceptron as bianry_crossentropy, specify the loss function of the second multi-layer perceptron as categorical_crossentropy, specify the entire smart contract code as the input of the Code2Vec pre-trained model and the Word2Vec pre-trained model, specify the vulnerability attribute as the target output of the first multi-layer perceptron, specify the vulnerability type as the target output of the second multi-layer perceptron, and train the smart contract vulnerability locator.
[0094] Embodiment 7, see Figure 5 This embodiment is based on the above embodiment, and step S6 specifically includes the following steps:
[0095] Step S61: Use the smart contract vulnerability locator to perform an initial detection on the entire code of the smart contract. If the first multi-layer perceptron determines that the entire code of the smart contract is a vulnerable code segment, binary detection begins. In this embodiment, the entire code of the smart contract is as follows:
[0096] @public
[0097] def __init__():
[0098] self.owner: address = msg.sender
[0099] self.prize: uint256 = 1000
[0100] self.last_time: uint256 = block.timestamp
[0101] @public
[0102] @view
[0103] def get_last_time() ->uint256:
[0104] return self.last_time
[0105] @public
[0106] def enter():
[0107] # User participation in the lottery
[0108] require(msg.value>0, "Must send some ether to participate")
[0109] self.prize += msg.value
[0110] @public
[0111] def pick_winner():
[0112] # Check if it is the contract owner
[0113] require(msg.sender == self.owner, "Only owner can pick a winner")
[0114] # Check if enough time has passed
[0115] require(block.timestamp - self.last_time>60 seconds, "Too soon to pick a winner")
[0116] # Start of the vulnerable code segment
[0117] random_number: uint256 = block.timestamp % 100
[0118] winner: address = self.generate_winner(random_number)
[0119] # End of the vulnerability code segment
[0120] # Send prize money to the winner
[0121] self.prize -= self.prize
[0122] self.last_time = block.timestamp
[0123] raw_call(winner, self.prize, max_outsize=0, value=self.prize);
[0124] Step S62: Perform binary detection: Perform the first binary detection on the vulnerable code segment, and input the two binary code segments into the smart contract vulnerability locator to detect whether they are vulnerable code segments. If there are still unconfirmed vulnerable code segments after the first binary detection, continue to perform the second binary detection, and divide the vulnerable sub-segment into two segments again, and continue to detect until the two binary code segments are determined as non-vulnerable code segments by the first multi-layer perceptron;
[0125] Step S63: Splicing the two code segments after the binary division to generate a spliced code segment, and designating the first line of the spliced code segment as a code segment that converges from top to bottom;
[0126] Step S64: top-to-bottom convergence detection: the top-to-bottom convergence code segment is input into the smart contract vulnerability locator. If the first multi-layer perceptron determines that the top-to-bottom convergence code segment is a non-vulnerable code segment, the next line after the last line in the top-to-bottom convergence code segment is added to the top-to-bottom convergence code segment. This step is repeated until the top-to-bottom convergence code segment is determined to be a vulnerable code segment.
[0127] Step S65: designate the last line of the top-to-bottom convergent code segment as the bottom-to-top convergent code segment;
[0128] Step S66: bottom-to-top convergence detection: the bottom-to-top convergent code segment is input into the smart contract vulnerability locator. If the first multi-layer perceptron determines that the bottom-to-top convergent code segment is a non-vulnerable code segment, the previous line of the first line in the bottom-to-top code segment is added to the bottom-to-top convergent code segment. This step is repeated until the bottom-to-top convergent code segment is determined to be a vulnerable code segment.
[0129] Step S67: The bottom-to-top convergent code segment is used as the target vulnerability code segment. At this time, the output of the second multi-layer perceptron is the vulnerability type of the target vulnerability code segment. In this embodiment, the binary detection is performed twice.
[0130] Embodiment 8, see Figure 6 This embodiment is based on the above embodiment, and step S7 specifically includes the following steps:
[0131] Step S71: construct a cosine similarity merging layer to create a first natural-code joint semantic encoder instance and a second natural-code joint semantic encoder instance. In this embodiment, the cosine similarity merging layer is implemented by using the Merge layer with a mode parameter of cos in Keras;
[0132] Step S72: Connect the first natural-code joint semantic encoder instance and the second natural-code joint semantic encoder instance to the cosine similarity merging layer to construct a natural-code joint semantic matcher.
[0133] Embodiment 9: This embodiment is based on the above embodiment, and step S8 specifically includes the following steps:
[0134] Step S81: Select smart contract vulnerability entries with the same vulnerability type as the target vulnerability code segment from the smart contract vulnerability library, and aggregate them into a matching vulnerability entry set;
[0135] Step S82: select a matching vulnerability entry from the matching vulnerability entry set in alphabetical order of the title, designate the target vulnerability code segment as the input of the first natural-code joint semantic encoder, designate the vulnerability code segment of the matching vulnerability entry as the input of the second natural-code joint semantic encoder, and the natural-code joint semantic matcher outputs the natural-code joint cosine matching degree, and repeat this step until each vulnerability entry in the matching vulnerability entry set has a corresponding natural-code joint cosine matching degree;
[0136] Step S83: Select the vulnerability entry with the highest natural-code joint cosine matching degree as the closest vulnerability entry.
[0137] Embodiment 10. This embodiment is based on the above embodiment. The present invention runs in the Windows operating system environment, relies on Anaconda3, and uses Keras as the framework of the smart contract vulnerability locator and the nature-code joint semantic matcher, and uses the Pandas library and the Numpy library to complete the construction of the smart contract vulnerability dataset.
[0138] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or apparatus that includes a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or apparatus.
[0139] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.
[0140] The present invention and its embodiments are described above, which is not restrictive. What is shown in the accompanying drawings is only one of the embodiments of the present invention, and the actual structure is not limited thereto. In short, if ordinary technicians in this field are inspired by it and do not deviate from the purpose of the invention, they can creatively design structural methods and embodiments similar to the technical scheme, which should all fall within the scope of protection of the present invention.
Claims
1. A blockchain-based smart contract vulnerability analysis method, characterized by: The method comprises the following steps: Step S1: Access the smart contract vulnerability library and use data enhancement technology to develop a smart contract vulnerability dataset; Step S2: Build a natural language autoencoder and use the smart contract vulnerability dataset to train the natural language autoencoder. The natural language autoencoder includes a Word2Vec pre-trained model, an autoencoder, a bottleneck layer, and an autodecoder. Step S3: Load the Code2Vec pre-trained model and use the natural language autoencoder to perform migration fine-tuning on the Code2Vec pre-trained model; Step S4: Use the natural language autoencoder and Code2Vec pre-trained model to build a natural-code joint semantic encoder and use the smart contract vulnerability dataset to further train the natural-code joint semantic encoder; Step S5: construct a smart contract vulnerability locator using a natural-code joint semantic encoder and train the smart contract vulnerability locator using a smart contract vulnerability dataset; Step S6: Use the smart contract vulnerability locator to locate the vulnerability of the smart contract using the binary convergence iteration method to obtain the target vulnerability code segment and the vulnerability type of the target vulnerability code segment; Step S7: construct a natural-code joint semantic matcher using a natural-code joint semantic encoder; Step S8: Use the nature-code joint semantic matcher to find the vulnerability entry closest to the target vulnerability from the smart contract vulnerability library.
2. According to a blockchain-based smart contract vulnerability analysis method according to claim 1, it is characterized by: The step S1 specifically includes the following steps: Step S11: access the smart contract vulnerability library, collect all the smart contract codes, vulnerability code segments, vulnerability types and vulnerability descriptions corresponding to each smart contract vulnerability entry from the smart contract vulnerability library, and summarize them into original vulnerability instances; Step S12: Collect smart contracts without vulnerabilities, randomly intercept code segments from the smart contracts without vulnerabilities, aggregate all smart contract codes of the smart contracts without vulnerabilities and the randomly intercepted code segments into negative sample vulnerability instances, fill the vulnerability type and vulnerability description of the negative sample vulnerability instances with 0, add vulnerability attributes to the negative sample vulnerability instances, and mark the vulnerability attributes of the negative sample vulnerability instances as non-vulnerable code segments; Step S13: Perform data enhancement by randomly hiding all the smart contract codes of the original vulnerability instance and the negative sample vulnerability instance to generate a data enhanced vulnerability instance; Step S14: Integrate the original vulnerability instances, negative sample vulnerability instances, and data enhanced vulnerability instances into a smart contract vulnerability dataset.
3. According to a blockchain-based smart contract vulnerability analysis method according to claim 2, it is characterized by: The step S3 specifically comprises the following steps: Step S31: Divide the smart contract vulnerability dataset into different batches in equal proportions, load the Code2Vec pre-trained model and download the Code2Vec pre-trained dataset, extract the automatic decoder from the natural language autoencoder, connect the Code2Vec pre-trained model to the automatic decoder, set the maximum original task accuracy drop value, and test the Code2Vec pre-trained model with the Code2Vec pre-trained dataset to obtain the initial original task accuracy; Step S32: specify all the smart contract codes in the smart contract vulnerability dataset as input, and the vulnerability description as the target output, and train the Code2Vec pre-trained model. During the training process, after each batch of training is completed, the Code2Vec pre-trained model is tested with the Code2Vec pre-trained dataset to obtain the original task accuracy after the completion of this batch of training and the original task accuracy reduction value caused by this batch of training; Step S33: If the original task accuracy after all batches of training are completed is less than the value obtained by subtracting the maximum original task accuracy drop from the initial original task accuracy, then roll back the weights of the Code2Vec pre-training model to the state before the start of training and disable the batch with the highest original task accuracy drop from the subsequent training of the Code2Vec pre-training model; Step S34: Repeat steps S32 and S33 until the original task accuracy after all batches of training are completed is higher than the value obtained by subtracting the maximum original task accuracy drop value from the initial original task accuracy.
4. According to a blockchain-based smart contract vulnerability analysis method according to claim 3, it is characterized by: The step S4 specifically comprises the following steps: Step S41: resetting the weights of the autoencoder and the autodecoder in the natural language autoencoder to construct a concatenated fusion layer; Step S42: connect the natural language autoencoder to the splicing fusion layer after truncating the automatic decoder, connect the Code2Vec pre-trained model to the splicing fusion layer, and construct a natural-code joint semantic encoder; Step S43: Connect the concatenated fusion layer to the automatic decoder, specify the entire smart contract code in the smart contract vulnerability dataset as the input of the Code2Vec pre-trained model and the Word2Vec pre-trained model, specify the vulnerability code segment in the smart contract vulnerability dataset as the target output of the automatic decoder, and train the natural-code joint semantic encoder.
5. According to a blockchain-based smart contract vulnerability analysis method according to claim 4, it is characterized by: The step S5 specifically comprises the following steps: Step S51: construct a first multi-layer perceptron and a second multi-layer perceptron, disconnect the splicing fusion layer from the automatic decoder, connect the splicing fusion layer to the first multi-layer perceptron and the second multi-layer perceptron, and construct a smart contract vulnerability locator; Step S52: Encode the vulnerability attribute of the smart contract vulnerability data set, the code segment with a vulnerability is set to 1, the code segment without a vulnerability is set to 0, and the vulnerability type of the smart contract vulnerability data set is one-hot encoded; Step S53: Specify the loss function of the first multi-layer perceptron as bianry_crossentropy, specify the loss function of the second multi-layer perceptron as categorical_crossentropy, specify the entire smart contract code as the input of the Code2Vec pre-trained model and the Word2Vec pre-trained model, specify the vulnerability attribute as the target output of the first multi-layer perceptron, specify the vulnerability type as the target output of the second multi-layer perceptron, and train the smart contract vulnerability locator.
6. According to a blockchain-based smart contract vulnerability analysis method according to claim 5, it is characterized by: The step S6 specifically comprises the following steps: Step S61: Use the smart contract vulnerability locator to perform preliminary detection on the entire code of the smart contract. If the first multi-layer perceptron determines that the entire code of the smart contract is a vulnerable code segment, start binary detection; Step S62: Perform binary detection: divide the vulnerable code segment into two, and input the two divided code segments into the smart contract vulnerability locator to detect whether they are vulnerable code segments, and repeat this step until the two divided code segments are determined as non-vulnerable code segments by the first multi-layer perceptron; Step S63: Splicing the two code segments after the binary division to generate a spliced code segment, and designating the first line of the spliced code segment as a code segment that converges from top to bottom; Step S64: Convergence detection from top to bottom; Step S65: designate the last line of the top-to-bottom convergent code segment as the bottom-to-top convergent code segment; Step S66: Convergence detection from bottom to top; Step S67: taking the bottom-to-top convergent code segment as the target vulnerability code segment, and the output of the second multi-layer perceptron at this time is the vulnerability type of the target vulnerability code segment.
Citation Information
Patent Citations
Intelligent contract code clone semantic similarity detection method based on pre-training model
CN118227204A
Deep learning-based smart contract vulnerability detection method
WO2021114093A1