Intelligent contract vulnerability detection method and system based on improved PGD adversarial network
Through improved PGD adversarial network and multi-objective optimization algorithm, the robustness and false positive rate problems of smart contract vulnerability detection in complex scenarios are solved, and more accurate and reliable vulnerability detection is achieved.
Patent Information
- Application Number
- CN202411360416.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-27
- Publication Date
- 2025-02-11
AI Technical Summary
When facing complex smart contract vulnerability detection methods, the existing smart contract vulnerability detection methods are poorly robust, have low reliability in detection results, and have high false alarm rate, making it difficult to effectively analyze complex smart contract source code and extract accurate and effective vulnerability characteristics.
The improved PGD adversarial network is adopted to train the generated adversarial network through the multi-objective optimized PGD algorithm to generate more diverse and complex adversarial samples, improve the generalization ability and robustness of the discriminator, and solve the problem of high false positive rates.
It improves the accuracy and comprehensiveness of smart contract vulnerability detection, makes the detection results more reliable, can better adapt to the detection of new vulnerabilities, and reduces the false alarm rate.
Smart Images

Figure CN119416222A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of blockchain security technology, and in particular relates to a smart contract vulnerability detection method and system based on an improved PGD adversarial network. Background Art
[0002] Smart contracts are one of the core applications of blockchain technology, boasting excellent features such as programmability and automatic execution. They have been widely adopted in finance, supply chain, the Internet of Things, and other fields, achieving promising results. Smart contracts play a crucial role in blockchain transactions, but like other applications, they also harbor potential security vulnerabilities, making them vulnerable to cyberattacks. Since smart contracts cannot be modified once deployed on-chain, even if vulnerabilities are discovered, they are difficult to remediate online. This makes smart contracts particularly vulnerable to security threats, potentially leading to serious consequences such as fund theft and service interruptions, resulting in economic losses and a loss of trust. Therefore, it is crucial to detect smart contract vulnerabilities before deployment on-chain.
[0003] Smart contract vulnerabilities refer to potential security risks that may occur during the design, development, or deployment phase of a smart contract. Common smart contract vulnerabilities include reentrancy attacks, timestamp dependency vulnerabilities, denial of service vulnerabilities, integer underflow and overflow vulnerabilities, and others. Currently, commonly used detection methods for detecting common smart contract vulnerabilities include methods based on symbolic execution, fuzz testing, static analysis, and deep learning, with deep learning-based detection methods being the most commonly used. However, with the increasing complexity of smart contracts, on the one hand, there are relatively few existing complex smart contract samples, making it difficult to comprehensively analyze highly complex smart contract samples. Consequently, the robustness of the detection model is poor, and the reliability of the detection results is low. On the other hand, existing smart contract vulnerability detection methods are unable to effectively analyze complex smart contract source code, making it difficult to extract accurate and effective vulnerability features, resulting in a high false positive rate. Summary of the Invention
[0004] To address the deficiencies of the above-mentioned prior art, the present invention provides a smart contract vulnerability detection method and system based on an improved PGD adversarial network, which adopts a multi-objective optimized PGD algorithm (Projected Gradient Descent) to train a generative adversarial network, and uses the generator in the trained generative adversarial network to generate more diverse and complex adversarial samples, thereby solving the problem of the lack of existing complex vulnerability contract samples. On this basis, through multi-objective optimization of the loss function and PGD algorithm optimization training, the generalization ability and robustness of the discriminator and the accuracy of detecting various smart contract vulnerabilities are effectively improved, thereby solving the problem of high false alarm rate of existing detection methods and making smart contract vulnerability detection more comprehensive and reliable.
[0005] In the first aspect, the present invention provides a smart contract vulnerability detection method based on an improved PGD adversarial network.
[0006] A smart contract vulnerability detection method based on an improved PGD adversarial network, including:
[0007] Obtain the source code data of the Ethereum smart contract to be tested;
[0008] Preprocess the acquired source code data to generate a word sequence;
[0009] The discriminator in the generative adversarial network trained by the multi-objective optimization PGD algorithm is used as the vulnerability detection model, the word sequence is input into the vulnerability detection model, and the smart contract vulnerability detection result is output; wherein, the generative adversarial network includes a generator and a discriminator, the generator is used to generate complex adversarial samples that are similar to the original input samples and have vulnerabilities, and the discriminator is used to determine the probability of each input sample having a smart contract vulnerability.
[0010] In a further technical solution, the training process of the generative adversarial network includes:
[0011] Obtain a variety of real smart contract source code data with vulnerabilities, extract word sequences from the source code data as original samples, and construct an original sample dataset;
[0012] Construct multi-objective optimization loss functions for the generator and discriminator respectively, use the original samples as the input of the generator in the generative adversarial network, train the generator and discriminator in the generative adversarial network, and use the PGD gradient descent method to iteratively update the network parameters;
[0013] Based on the original sample data set, after multiple iterative training, the discriminator's accuracy in judging vulnerabilities reaches the preset effect, completing the training of the generative adversarial network.
[0014] In a further technical solution, the multi-objective optimization loss function of the generator is a weighted sum of the generator's target deviation and the perturbation amplitude of the generated adversarial sample; wherein the generator's target deviation is represented by a cross-entropy loss, and the generator's perturbation amplitude of the generated adversarial sample is represented by the difference in eigenvalues between the original sample and the generated adversarial sample;
[0015] The objectives of the multi-objective optimization loss function of the generator are to minimize the perturbation amplitude and minimize the target deviation of the generator.
[0016] In a further technical solution, the multi-objective optimization loss function of the discriminator is the difference between the target deviation of the discriminator and the confidence of the classification result output by the discriminator;
[0017] The objective of the multi-objective optimization loss function of the discriminator is to maximize the target deviation of the discriminator and minimize the confidence of the classifier.
[0018] In the second aspect, the present invention provides a smart contract vulnerability detection system based on an improved PGD adversarial network.
[0019] A smart contract vulnerability detection system based on an improved PGD adversarial network, including:
[0020] The data acquisition module is used to obtain the source code data of the Ethereum smart contract to be tested;
[0021] A data preprocessing module is used to preprocess the acquired source code data and generate a word sequence;
[0022] The vulnerability detection module is used to use the discriminator in the generative adversarial network trained using the multi-objective optimization PGD algorithm as a vulnerability detection model, input the word sequence into the vulnerability detection model, and output the smart contract vulnerability detection result; wherein, the generative adversarial network includes a generator and a discriminator, the generator is used to generate complex adversarial samples that are similar to the original input samples and have vulnerabilities, and the discriminator is used to determine the probability of each input sample containing a smart contract vulnerability.
[0023] In a third aspect, the present invention further provides an electronic device comprising a memory and a processor, and computer instructions stored in the memory and executed on the processor, wherein when the computer instructions are executed by the processor, the steps of the method described in the first aspect are completed.
[0024] In a fourth aspect, the present invention further provides a computer-readable storage medium for storing computer instructions, which, when executed by a processor, complete the steps of the method described in the first aspect.
[0025] One or more of the above technical solutions have the following beneficial effects:
[0026] 1. The present invention provides a smart contract vulnerability detection method and system based on an improved PGD adversarial network. The multi-objective optimized PGD algorithm is used to train the generative adversarial network, which can not only dynamically generate diverse adversarial samples and simulate more complex attack scenarios, but also continuously optimize the model during the adversarial process to improve the detection ability of unknown attacks. The generator in the trained generative adversarial network is used to generate more diverse and complex adversarial samples, solving the problem of the lack of existing complex vulnerability contract samples. On this basis, through multi-objective optimization of the loss function and PGD algorithm optimization training, the generalization ability of the discriminator and the accuracy of detecting various smart contract vulnerabilities are effectively improved, solving the problem of high false alarm rate of existing detection methods, making smart contract vulnerability detection more comprehensive and reliable. Compared with the traditional smart contract vulnerability detection method, which requires manual addition of new rules or features to detect new vulnerabilities and lacks adaptability, the present invention adopts multi-objective optimization of the PGD adversarial network. Through adversarial training, the adaptability and detection accuracy of the model in the face of new vulnerabilities can be better improved.
[0027] 2. The smart contract vulnerability detection method proposed in the present invention introduces a PGD adversarial network, that is, the PGD algorithm is used to train a generative adversarial network. Compared with ordinary adversarial networks, by introducing the PGD projected gradient descent method, more aggressive adversarial samples can be generated, and the sample features are optimized through multiple iterations, so that the generated adversarial samples are more accurate and more deceptive; and the gradual optimization process of PGD also makes the adversarial training more stable, solves the gradient vanishing problem that may occur in ordinary adversarial networks, and enables the trained generative adversarial network to have better model performance and robustness; using the discriminator in the generative adversarial network as a vulnerability detection model, more accurate identification of complex vulnerability contracts can be achieved.
[0028] 3. Considering that the PGD adversarial network mainly focuses on the single goal of maximizing the discriminator loss function, this may cause the generated adversarial samples to overfit in a specific feature space, thereby reducing the generalization ability of the discriminator in detecting multiple vulnerabilities. Moreover, single-target optimization may cause the samples generated by the generator to lack diversity, making it difficult to fully simulate the complexity of smart contract vulnerabilities. To address the above problems, the present invention also introduces a multi-objective optimization mechanism based on the PGD adversarial network. Through multi-objective optimization, the generator not only optimizes the discriminator's loss function, but also improves the diversity of samples and the robustness to different complex vulnerability types. In this way, the generator can generate more diverse and complex adversarial samples, improve sample diversity and the breadth of feature distribution, and further improve the generalization ability and detection accuracy of the discriminator, thereby solving the limitations of the PGD adversarial network and ensuring the comprehensiveness and reliability of the final vulnerability detection results.
[0029] 4. In the smart contract vulnerability detection method based on the multi-objective optimized PGD adversarial network proposed in the present invention, since DistilBERT has a more powerful text generation capability than other traditional methods, the present invention uses the DistilBERT model as a discriminator to better generate more confusing smart contract vulnerability samples, and BiLSTM can perform sequence learning in two time step directions. Using BiLSTM as a discriminator can more accurately identify smart contract vulnerabilities. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] The accompanying drawings, which constitute a part of the present invention, are used to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute improper limitations on the present invention.
[0031] Figure 1 This is a flowchart of the smart contract vulnerability detection method based on the improved PGD adversarial network according to an embodiment of the present invention;
[0032] Figure 2 This is a flowchart of training the improved PGD adversarial network in an embodiment of the present invention. DETAILED DESCRIPTION
[0033] It should be noted that the following detailed descriptions are exemplary only and are intended to describe specific embodiments and provide further explanation of the present invention, and are not intended to limit the exemplary embodiments according to the present invention. Unless otherwise indicated, all technical and scientific terms used herein have the same meanings as those commonly understood by those of ordinary skill in the art to which the present invention belongs. In addition, it should be understood that when the terms "comprise" and / or "include" are used in this specification, they indicate the presence of features, steps, operations, devices, components and / or combinations thereof.
[0034] Example 1
[0035] In order to solve the problems of high false alarm rate and low robustness in traditional smart contract vulnerability detection methods, this embodiment proposes a smart contract vulnerability detection method based on an improved PGD adversarial network. First, the PGD adversarial network is introduced, that is, the PGD algorithm is used to train a generative adversarial network. The neural network improves model performance and robustness through adversarial training, and improves the robustness and accuracy of the model under adversarial attacks. On this basis, considering the adverse effects of the PGD adversarial network focusing only on the single goal of maximizing the discriminator loss during training, this embodiment also introduces a multi-objective optimization mechanism on the basis of the PGD adversarial network to solve the limitations of the PGD adversarial network. Through this multi-objective optimization mechanism, the generator not only optimizes the loss function of the discriminator, but also improves the diversity of samples and robustness to different vulnerability types. The generative adversarial network obtained by training the above method proposed in this embodiment can generate more diverse and complex adversarial samples, and the generalization ability of the discriminator and the detection accuracy of various vulnerabilities are also effectively improved, making the network model more comprehensive and reliable for the detection of smart contract vulnerabilities.
[0036] The smart contract vulnerability detection method based on the improved PGD adversarial network proposed in this embodiment is as follows: Figure 1 As shown, the specific steps include:
[0037] Step S1: Obtain source code data of the Ethereum smart contract to be tested;
[0038] Step S2: pre-processing the acquired source code data to generate a word sequence;
[0039] Step S3: Using the discriminator in the generative adversarial network trained by the multi-objective optimization PGD algorithm as a vulnerability detection model, inputting the word sequence into the vulnerability detection model, and outputting the smart contract vulnerability detection result; wherein, the generative adversarial network includes a generator and a discriminator, the generator is used to generate complex adversarial samples that are similar to the input original samples and have vulnerabilities, and the discriminator is used to determine the probability of each input sample having a smart contract vulnerability.
[0040] The following content provides a more detailed introduction to the smart contract vulnerability detection method based on the improved PGD adversarial network proposed in this embodiment.
[0041] In step S1 and step S2, the source code data of the Ethereum smart contract to be detected is obtained, and the obtained source code data is preprocessed to generate a word sequence.
[0042] The preprocessing of source code data is as follows: first, using the Solidity compiler, the acquired source code data is parsed and compiled into an Abstract Syntax Tree (AST); second, the Abstract Syntax Tree AST nodes are selected, that is, the AST nodes containing smart contract elements such as function definitions, variable declarations, and operators are selected, and these AST nodes are converted into tokens to generate a token sequence {Tok1, Tok2, Tok3, ..., Tokq}, and finally the token sequence is output as a word sequence.
[0043] In step S3, the multi-objective optimization PGD algorithm is used to train the generative adversarial network, and the discriminator in the trained generative adversarial network is used as the vulnerability detection model. The word sequence is input into the vulnerability detection model, and the smart contract vulnerability detection result is output.
[0044] In this embodiment, the process of training the generative adversarial network using the multi-objective optimization PGD algorithm is as follows: Figure 2 As shown, the specific steps include:
[0045] Step S3.1: Obtain a variety of real smart contract source code data with vulnerabilities, extract word sequences from the source code data as original samples, and construct an original sample dataset.
[0046] First, we obtain the source code data of multiple Ethereum smart contracts with vulnerability labels and construct a dataset. In this example, we use the open source project SmartBugs smart contract vulnerability dataset, which contains a series of smart contract source codes with vulnerabilities.
[0047] Secondly, each source code data in the dataset is preprocessed to generate a word sequence annotated with the vulnerability type. Specifically, traditional smart contract vulnerability annotation tools Slither and Mythril are used to annotate the vulnerabilities in the smart contract source code in turn to prevent missed or misjudgment. The annotated dataset is then split, with 70% of the smart contracts in the dataset used as the training set for model training, and the remaining 30% as the validation set for testing the model's effectiveness. On this basis, the Solidity compiler is used to parse and compile the annotated smart contract source code into an abstract syntax tree (AST) and extract AST nodes from it. These AST nodes are converted into a token sequence {Tok1, Tok2, Tok3, ..., Tokq}, generating a word sequence annotated with the vulnerabilities and their types.
[0048] Finally, the extracted word sequences are used as original samples to construct the original sample dataset.
[0049] Step S3.2: Build a generative adversarial network, construct multi-objective optimization loss functions for the generator and discriminator respectively, use the original samples as the input of the generator in the generative adversarial network, train the generator and discriminator in the generative adversarial network, and use the PGD gradient descent method to continuously iteratively update the network parameters.
[0050] Since the traditional PGD adversarial network mainly focuses on the single goal of maximizing the discriminator loss function, this may cause the generated adversarial samples to overfit in a specific feature space, thereby reducing the generalization ability of the discriminator in detecting multiple vulnerabilities. In addition, single-objective optimization may cause the samples generated by the generator to lack diversity, making it difficult to fully simulate the complexity of smart contract vulnerabilities. For this reason, this embodiment improves the PGD adversarial network and introduces a multi-objective optimization mechanism to diversify the network's loss function, thereby improving the diversity of samples generated by the generator and the robustness of the discriminator to different complex vulnerability types.
[0051] Step S3.2.1, build a multi-objective optimized PGD adversarial network model. In this embodiment, DistilBERT is used as a generator to generate complex contract adversarial samples, making it more difficult for the discriminator to determine whether there are vulnerabilities in the smart contract; BiLSTM (Bidirectional Long Short-Term Memory, bidirectional long short-term memory network) is used as a discriminator to identify vulnerability information in input samples, including original samples and adversarial samples. Through adversarial training, the discriminator's ability to discriminate smart contract vulnerabilities is gradually enhanced, and the adversarial samples generated by the generator are also more realistic, so that the model has higher robustness when facing attacks.
[0052] Step S3.2.2: Loading a pre-trained model. In this embodiment, the pre-trained DistilBERT model is loaded using the Hugging Face Transformers library.
[0053] Step S3.2.3. Define the multi-objective loss function of the generator and discriminator.
[0054] (1) For the generator, a multi-objective optimization loss function is constructed and utilized. The multi-objective optimization loss function of the generator is the weighted sum of the generator's target deviation and the perturbation amplitude of the generated adversarial sample. The formula is:
[0055]
[0056] in, is the cross entropy loss, the target deviation of the generator is represented by the cross entropy loss, and Y represents the true label. represents the predicted probability of the discriminator for generating adversarial samples, n represents the number of samples, z irepresents the i-th original sample of the input generator, G(z i ) represents the generator G’s response to the input original sample z i Generated adversarial examples, D(G(z i )) is the discriminator D’s response to the generated adversarial sample G(z i ), which represents the probability that the generated sample is classified as a vulnerable contract by the discriminator; ‖ΔX G ‖ represents the perturbation amplitude between the adversarial sample generated by the generator and the original sample, which is used to measure the deviation of the generated adversarial sample. The perturbation amplitude is expressed by the difference in the eigenvalues of the original sample and the generated adversarial sample. Its calculation formula is:
[0057]
[0058] in, represents the i-th adversarial sample generated by the generator, X i represents the i-th true sample, ΔX G Each component in is the difference between the corresponding eigenvalues of the adversarial sample and the original sample. The square root of the sum of squares is the perturbation amplitude.
[0059] By minimizing the above multi-objective loss function, that is, minimizing the perturbation amplitude and minimizing the target deviation of the generator, the generator can generate adversarial samples that are similar to the original samples and closer to the real complex vulnerability contracts, while reducing the deviation of the generated samples.
[0060] (2) For the discriminator, a multi-objective optimization loss function is constructed and used. The multi-objective optimization loss function of the discriminator is the difference between the target deviation of the discriminator and the confidence of the classification result output by the discriminator. The formula is:
[0061]
[0062] in, is the cross entropy loss, and the target deviation of the discriminator is expressed by the cross entropy loss, z i represents the i-th original sample, Y i is the corresponding true label, 1 means that the contract sample contains a vulnerability, 0 means that it does not contain a vulnerability, D(z i ) represents the discriminator’s predicted probability of whether the real vulnerability contract sample (i.e., the original sample) has a vulnerability, D(G(z i )) is the discriminator D’s response to the generated adversarial sample G(z i ) prediction probability; α is the adjustment coefficient, which is used to control the influence of confidence in the loss function. Indicates the confidence of the discriminator in the classification result.
[0063] By maximizing the above multi-objective loss function, that is, minimizing the confidence of the classifier and maximizing the target deviation of the discriminator, after multiple iterative training, the discriminator can reduce the confidence of incorrect predictions while maintaining high accuracy, thereby improving the robustness of the model.
[0064] Step S3.2.4: Use the original sample as the input of the generator in the generative adversarial network to train the generator and discriminator in the generative adversarial network, and use the PGD gradient descent method to iteratively update the network parameters. This step specifically includes:
[0065] First, the original sample is fed into the generator, which generates complex adversarial samples that are similar to the original sample and contain vulnerabilities. Specifically, the original sample is fed into the generator. The generator's goal is to generate adversarial samples that are similar to the characteristics of real smart contracts with vulnerabilities, making it impossible for the discriminator to distinguish whether the generated contract has vulnerabilities.
[0066] Secondly, the original sample and the adversarial sample are input into the discriminator as input samples, and the probability of each input sample containing a smart contract vulnerability is determined, and the smart contract vulnerability detection result is output. Specifically, the discriminator's goal is to correctly distinguish whether there is a vulnerability in the contract, and to maintain high accuracy and robustness when facing the adversarial samples generated by PGD. At the same time, the adversarial sample generated by the generator is input into the discriminator to obtain the discriminator's predicted probability of the generated sample contract. Calculate the loss function of the discriminator And update the parameters of the discriminator.
[0067] Afterwards, the PGD gradient descent method is used to iteratively update the adversarial samples generated by the generator, and the updated adversarial samples are input into the discriminator for training, and the loss function and parameters of the discriminator are updated.
[0068] In the above iterative training process using the PGD gradient descent method, the PGD algorithm generates adversarial samples z' by iteratively optimizing the input original sample z to minimize the discriminator's loss function and minimize the perturbation amplitude and classifier confidence. Specifically, the PGD algorithm iteratively adjusts the sample's features and determines the discriminator loss function based on the output of the current discriminator. Calculate the gradient of the discriminator loss function on the features of the current input adversarial sample, use the sign function to indicate the direction of the gradient, and make the direction of the gradient gradually closer to the direction that causes the maximum loss of the discriminator, so as to update the generator to generate adversarial samples that are similar to the smart contract with vulnerabilities, but more complex and difficult to detect. After each PGD iteration to generate a new adversarial sample (that is, update the adversarial sample), the sample will be input into the discriminator for judgment and training, and the parameters and loss function of the discriminator will be updated. After multiple adversarial tests, the discriminator will have a stronger smart contract vulnerability detection capability and stronger robustness. At the same time, since the update of the discriminator loss function will also affect the generator loss function, the generator's ability to generate more confusing smart contracts with vulnerabilities is enhanced by minimizing the generator's target deviation and perturbation amplitude. The formula of the above PGD algorithm is:
[0069]
[0070] Among them, z t represents the adversarial sample at the tth iteration; z t+2 represents the adversarial sample after the t+1th iteration, which is the adversarial sample after the gradient update; η represents the step size, which is the amplitude of the sample update at each iteration; sign() is a sign function that returns the sign of the gradient direction; represents the discriminator loss function About adversarial sample z t The gradient of , which indicates the sensitivity of the loss function to the sample; Clip ε () is a clipping function that limits the updated sample results to the allowed perturbation range ε, ensuring that the adversarial sample is within the legal range.
[0071] Finally, after multiple iterations of updating the adversarial sample training, the training of the generative adversarial network based on the current original sample is completed until the set number of iterations is reached. As another implementation, the iterative training of the current sample can be terminated when the discriminator's accuracy reaches the optimal value or reaches a set value.
[0072] Specifically, the training process of the generator and discriminator is repeated, that is: first, the generator generates adversarial samples, inputs the adversarial samples into the discriminator, and then calculates the prediction results of the generator and discriminator for the real original samples and adversarial samples, that is, whether they can correctly judge whether there are vulnerabilities in the contract, and calculates the corresponding loss function value; uses the gradient descent method to update the parameters of the generator and discriminator to minimize the loss function; after multiple rounds of adversarial training, the generator will generate more aggressive samples, and the discriminator will better identify vulnerabilities in smart contracts.
[0073] Step S3.3: Based on the original sample data set, multiple iterations of training are performed until the discriminator reaches a preset accuracy rate in identifying vulnerabilities. Training is terminated, and the training of the generative adversarial network is completed. In this embodiment, the preset accuracy rate is a preset accuracy rate.
[0074] Based on the trained discriminator, the word sequence of the smart contract source code in the test set is input into the discriminator to determine whether there are vulnerabilities. The confusion matrix is used to obtain more accurate smart contract vulnerability prediction results based on the discriminator's output.
[0075] Example 2
[0076] This embodiment provides a smart contract vulnerability detection system based on an improved PGD adversarial network, including:
[0077] The data acquisition module is used to obtain the source code data of the Ethereum smart contract to be tested;
[0078] A data preprocessing module is used to preprocess the acquired source code data and generate a word sequence;
[0079] The vulnerability detection module is used to use the discriminator in the generative adversarial network trained using the multi-objective optimization PGD algorithm as a vulnerability detection model, input the word sequence into the vulnerability detection model, and output the smart contract vulnerability detection result; wherein, the generative adversarial network includes a generator and a discriminator, the generator is used to generate complex adversarial samples that are similar to the original input samples and have vulnerabilities, and the discriminator is used to determine the probability of each input sample containing a smart contract vulnerability.
[0080] Example 3
[0081] This embodiment provides an electronic device, including a memory and a processor, and computer instructions stored in the memory and executed on the processor. When the computer instructions are executed by the processor, the steps of the smart contract vulnerability detection method based on the improved PGD adversarial network are completed as described above.
[0082] Example 4
[0083] This embodiment also provides a computer-readable storage medium for storing computer instructions. When the computer instructions are executed by a processor, the steps in the smart contract vulnerability detection method based on the improved PGD adversarial network are completed as described above.
[0084] The steps involved in the above embodiments 2 to 4 correspond to those in the method embodiment 1. For detailed implementation, please refer to the relevant description of embodiment 1. The term "computer-readable storage medium" should be understood as a single medium or multiple media that includes one or more instruction sets; it should also be understood to include any medium that can store, encode, or carry an instruction set for execution by a processor and cause the processor to perform any method of the present invention.
[0085] Those skilled in the art will appreciate that the modules or steps of the present invention described above can be implemented using a general-purpose computer device. Alternatively, they can be implemented using program code executable by a computing device, which can then be stored in a storage device and executed by the computing device. Alternatively, they can be fabricated into separate integrated circuit modules, or multiple modules or steps can be fabricated into a single integrated circuit module for implementation. The present invention is not limited to any specific combination of hardware and software.
[0086] The above description is only a preferred embodiment of the present invention. Although the specific implementation of the present invention is described in conjunction with the accompanying drawings, it does not limit the scope of protection of the present invention. Those skilled in the art should understand that on the basis of the technical solution of the present invention, various modifications or variations that can be made by those skilled in the art without creative work are still within the scope of protection of the present invention.
Claims
1. A smart contract vulnerability detection method based on an improved PGD adversarial network, characterized in that: include: Obtain the source code data of the Ethereum smart contract to be tested; Preprocess the acquired source code data to generate a word sequence; The discriminator in the generative adversarial network trained by the multi-objective optimization PGD algorithm is used as the vulnerability detection model, the word sequence is input into the vulnerability detection model, and the smart contract vulnerability detection result is output; wherein, the generative adversarial network includes a generator and a discriminator, the generator is used to generate complex adversarial samples that are similar to the original input samples and have vulnerabilities, and the discriminator is used to determine the probability of each input sample having a smart contract vulnerability.
2. A smart contract vulnerability detection method based on an improved PGD adversarial network as claimed in claim 1, characterized in that: The training process of the generative adversarial network includes: Obtain a variety of real smart contract source code data with vulnerabilities, extract word sequences from the source code data as original samples, and construct an original sample dataset; Construct multi-objective optimization loss functions for the generator and discriminator respectively, use the original samples as the input of the generator in the generative adversarial network, train the generator and discriminator in the generative adversarial network, and use the PGD gradient descent method to iteratively update the network parameters; Based on the original sample data set, after multiple iterative training, the accuracy of the discriminator in judging vulnerabilities reaches the preset effect, completing the training of the generative adversarial network.
3. A smart contract vulnerability detection method based on an improved PGD adversarial network as claimed in claim 2, characterized in that: The multi-objective optimization loss function of the generator is a weighted sum of the target deviation of the generator and the perturbation amplitude of the generated adversarial sample; wherein the target deviation of the generator is represented by the cross entropy loss, and the perturbation amplitude of the generated adversarial sample of the generator is represented by the difference in the eigenvalues of the original sample and the generated adversarial sample; The objectives of the multi-objective optimization loss function of the generator are to minimize the perturbation amplitude and minimize the target deviation of the generator.
4. A smart contract vulnerability detection method based on an improved PGD adversarial network as claimed in claim 2, characterized in that: The multi-objective optimization loss function of the discriminator is the difference between the target deviation of the discriminator and the confidence of the classification result output by the discriminator; The objective of the multi-objective optimization loss function of the discriminator is to maximize the target deviation of the discriminator and minimize the confidence of the classifier.
5. A smart contract vulnerability detection method based on an improved PGD adversarial network as claimed in claim 2, characterized in that: The original sample is used as the input of the generator in the generative adversarial network to train the generator and discriminator in the generative adversarial network, and the PGD gradient descent method is used to iteratively update the network parameters, including: Input the original sample into the generator to generate a complex adversarial sample that is similar to the original sample but has vulnerabilities; Input the original sample and the adversarial sample as input samples into the discriminator, determine the probability of each input sample having a smart contract vulnerability, and output the smart contract vulnerability detection result; The PGD gradient descent method is used to iteratively update the adversarial samples generated by the generator, and the updated adversarial samples are then input into the discriminator for training to update the parameters of the discriminator. After multiple iterations of updating the training of adversarial samples, the training of the generative adversarial network based on the current original samples is completed.
6. A smart contract vulnerability detection method based on an improved PGD adversarial network as claimed in claim 5, characterized in that: The PGD gradient descent method is used for iterative training, which is: Determine the discriminator loss function based on the output result of the current discriminator; Calculate the gradient of the discriminator loss function on the current input adversarial sample, use the sign function to indicate the direction of the gradient, and make the direction of the gradient closer to the direction of the maximum discriminator loss, so as to update the adversarial sample generated by the generator.
7. A smart contract vulnerability detection system based on an improved PGD adversarial network, characterized in that: include: A data acquisition module is used to obtain source code data of the Ethereum smart contract to be tested; A data preprocessing module, used for preprocessing the acquired source code data to generate a word sequence; The vulnerability detection module is used to use the discriminator in the generative adversarial network trained by the multi-objective optimization PGD algorithm as a vulnerability detection model, input the word sequence into the vulnerability detection model, and output the smart contract vulnerability detection result; wherein, the generative adversarial network includes a generator and a discriminator, the generator is used to generate a complex adversarial sample that is similar to the input original sample and has vulnerabilities, and the discriminator is used to determine the probability of each input sample having a smart contract vulnerability.
8. A smart contract vulnerability detection system based on an improved PGD adversarial network as claimed in claim 7, characterized in that: The training process of the generative adversarial network includes: Obtain a variety of real smart contract source code data with vulnerabilities, extract word sequences from the source code data as original samples, and construct an original sample dataset; Construct multi-objective optimization loss functions for the generator and discriminator respectively, use the original samples as the input of the generator in the generative adversarial network, train the generator and discriminator in the generative adversarial network, and use the PGD gradient descent method to iteratively update the network parameters; Based on the original sample data set, after multiple iterative training, the accuracy of the discriminator in judging vulnerabilities reaches the preset effect, completing the training of the generative adversarial network.
9. An electronic device, characterized in that: It includes a memory and a processor, and computer instructions stored in the memory and executed on the processor. When the computer instructions are executed by the processor, the steps of a smart contract vulnerability detection method based on an improved PGD adversarial network as described in any one of claims 1 to 6 are completed.
10. A computer-readable storage medium, characterized in that: Used to store computer instructions, which, when executed by a processor, complete the steps of a smart contract vulnerability detection method based on an improved PGD adversarial network as described in any one of claims 1-6.
Citation Information
Cited By
Method and device for constructing 0DAY vulnerability detection model based on AI large model
CN119961941A
A method and device for constructing a 0DAY vulnerability detection model based on an AI large model
CN119961941B