A malicious code analysis method and system based on PGD attack countermeasure training
By using an adversarial training method based on PGD attacks, a malicious code detection model MadInG adv is generated, which solves the problems of insufficient data and imbalanced class distribution, and achieves higher accuracy and robustness in malicious code detection.
Patent Information
- Application Number
- CN202411801510.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-09
- Publication Date
- 2026-02-06
- Estimated Expiration
- 2044-12-09
AI Technical Summary
Existing malware detection models suffer from low accuracy and difficulty in effectively identifying malware variants when data volume is limited and data category distribution is unbalanced.
We employ an adversarial training method based on PGD attacks. By extracting opcode sequences from the disassembled files of malicious code, we generate Markov images and train them using a generative adversarial network (GAN) MadInG adv. This builds a malicious code detection model and enhances its detection capabilities in scenarios with imbalanced data class distributions and malicious code variants.
It improves the accuracy and generalization ability of malware detection, enhances the robustness of the model, and can more effectively identify malware variants.
Smart Images

Figure CN119720197B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of malicious code detection, and particularly relates to a malicious code analysis method and system based on PGD attack adversarial training. BACKGROUND
[0002] Malicious code detection is an important part of network security. However, with the rapid development of computer technology, the number of malicious code adversarial samples and new malicious code variants increases, the evasion ability of malicious code is enhanced, and malicious code detection faces great challenges. In recent years, intelligent malicious code detection methods have gradually been valued and good results have been achieved. However, the training samples used to construct the malicious code detection model are prone to problems such as small data volume and unbalanced data class distribution, which affect the accuracy of model decision-making. SUMMARY
[0003] Therefore, the present application aims to provide a malicious code analysis method and system based on PGD attack adversarial training, which realizes malicious code detection from the perspective of image analysis and improves the detection accuracy in scenarios such as unbalanced data class distribution and malicious code variants by combining the idea of adversarial learning.
[0004] To achieve the above-mentioned purpose, the present application adopts the following technical solution: a malicious code analysis method based on PGD attack adversarial training, comprising the following steps:
[0005] Step S1, extracting a malicious code opcode sequence from a disassembled asm file of the malicious code;
[0006] Step S2, generating a corresponding Markov image according to the obtained opcode sequence;
[0007] Step S3, taking the generated Markov image as input, training a malicious code detection model MadInG adv based on image analysis and generative adversarial network, and realizing malicious code detection through the trained model.
[0008] In a preferred embodiment, in step S1, the malicious code opcode sequence is extracted from the disassembled asm file of the malicious code, specifically comprising the following steps:
[0009] Step S1A1, finding the code section position in the disassembled asm file by matching Segmenttype:Pure code, and obtaining the code section name;
[0010] Step S1A2, matching the code section area content according to the obtained code section name, and reading the content in the code section by line;
[0011] Step S1A3, match the content in the code section by regular expression\s([A-F0-9]{2}\s)+\s*([a-z]+), obtain the operation code contained in the code section;
[0012] Step S1A4, repeat step S1A2-step S1A3 until the file reading is completed, obtain the operation code sequence of the malicious code.
[0013] In a preferred embodiment, in step S2, the corresponding Markov image is generated according to the obtained operation code sequence, and the specific steps include:
[0014] Step S2A1, calculate the frequency of operation code b i followed by b j ;
[0015] Step S2A2, calculate the probability of operation code b i followed by b j ;
[0016] Step S2A3, calculate the Markov image pixel value according to the Markov transition probability matrix, and the calculation method is as follows:
[0017]
[0018] Wherein, TM is the transition probability matrix, TM[i][j] represents the probability of operation code b i followed by b j , MP is the maximum probability in the transition probability matrix TM, and mod is the remainder function.
[0019] In a preferred embodiment, step S3 includes the following specific steps:
[0020] Step S3A1, input the Markov image data set T, initialize the generator G and its parameters θ in MadInG adv model, and the discriminator D and its discriminator parameters ω;
[0021] Step S3A2, PGD attack is carried out on the discriminator, and the adversarial sample X adv is obtained, and the specific formula of generating the adversarial sample X adv is as follows:
[0022]
[0023] X adv ={x′1x′2,...,x′ m}
[0024] Wherein, α represents the size of disturbance when updating the adversarial sample each time, J(x′ i , y) represents the loss function of the model, and clipx,ε is a projection operation, is a gradient of the loss function J(x′ i , y) with respect to the input x′ i , which indicates the direction of increasing loss, represents only keeping the direction of the gradient, while ignoring the size, so as to control each update to be a fixed step size a, x′ i is the adversarial sample generated in the i-th iteration in the PGD attack process, X adv is the Markov image dataset T = {(x1, y1), (x2, y2),..., (x m , y m )} after multiple iterations in the PGD attack;
[0025] Step S3A3, fix the generator parameters, train the discriminator using the adversarial sample X adv , the real sample X real and the generated sample X fake , update the discriminator parameters ω, and the specific formula of the parameter update is as follows:
[0026] L S = E[log P(S = real | X real )] + E[log P(S = fake | X fake )] + E[log P(S = fake | X adv )]
[0027]
[0028] Where S is the output true or false result of the discriminator input data, C is the output classification result of the discriminator input data, E represents expectation, c is the correct class of the input sample, L S is the cost function of data authenticity, L C is the cost function of data classification accuracy, g ω is the gradient of (L S + L C ) with respect to ω, Adam is the optimizer of the generator and discriminator in MadInG adv, and the goal of the optimizer is to maximize L S + L C ;
[0029] Step S3A4, fix the discriminator parameters, train the generator using the generated sample, update the generator parameters θ, and the specific formula of the parameter update is as follows:
[0030]
[0031] θ ← Adam (g θ , θ, a, β1, β2)
[0032] wherein g θ is (L S -L C ) the gradient with respect to theta, the objective of the optimizer is to be able to minimize L S -L C ;
[0033] Step S3A5, repeat steps S3A2-S3A4 until the iteration number is reached, and a trained discriminator D' is obtained.
[0034] Step S3A6, construct a malicious code detector D0 with the same structure and parameters as the discriminator, and realize malicious code detection through D0.
[0035] The application also provides a malicious code analysis system based on PGD attack adversarial training.
[0036] The application also provides a computer readable storage medium, comprising:
[0037] The computer readable storage medium stores a computer program.
[0038] The computer program is executed by a processor to perform the malicious code analysis method based on PGD attack adversarial training.
[0039] Compared with the prior art, the application has the following beneficial effects: introducing a PGD attack adversarial training module in model training, thereby having a higher malicious code detection accuracy in scenarios such as data class distribution imbalance and malicious code variation. BRIEF DESCRIPTION OF DRAWINGS
[0040] Fig. 1 The method is the overall framework of the preferred embodiment of the application.
[0041] Fig. 2 The Markov image generation process is the preferred embodiment of the application.
[0042] Fig. 3 The acquisition process of the malicious code operation code sequence is the preferred embodiment of the application. DETAILED DESCRIPTION
[0043] The application will be further described below in conjunction with the drawings and embodiments.
[0044] It should be noted that the following detailed description is exemplary and is intended to provide further explanation of the present application. Unless otherwise specified, all technical and scientific terms used herein have the same meaning as generally understood by those skilled in the art to which the present application belongs.
[0045] It is to be noted that the terms used herein are only intended to describe specific embodiments and are not intended to limit the exemplary embodiments according to the present application; as used herein, the singular form is intended to include the plural form as well, unless the context clearly indicates otherwise, and it should also be understood that the terms "comprise" and / or "include" as used in the specification, indicate the presence of features, steps, operations, devices, components and / or combinations thereof.
[0046] The present application provides a malicious code analysis method based on projected gradient descent (PGD) attack against adversarial training, as shown in the accompanying Figs. 1-3 The specific steps are as follows:
[0047] The present application improves the accuracy of the model in the scene of data class distribution imbalance and malicious code variation, and enhances the generalization ability and robustness of the malicious code detector.
[0048] The specific steps are as follows:
[0049] Step S1, extracting the malicious code opcode sequence from the disassembled asm file of the malicious code;
[0050] Step S2, generating a corresponding Markov image according to the obtained opcode sequence;
[0051] Step S3, taking the generated Markov image as input, training a malicious code detection model MadInG adv based on image analysis and generative adversarial network, and realizing malicious code detection through the trained model.
[0052] In an embodiment of the present application, in step S1, the malicious code opcode sequence is extracted from the disassembled asm file of the malicious code, as shown in the accompanying Fig. 2 The specific steps are as follows:
[0053] Step S1A1, find the code section position in the disassembled asm file by matching Segment type: Pure code, and obtain the code section name;
[0054] Step S1A2, according to the obtained code section name, matching the code section area content, reading the content in the code section by line;
[0055] Step S1A3, matching the content in the code section by regular expression\s([A-F0-9]{2}\s)+\s*([a-z]+), obtaining the operation code contained in the code section;
[0056] Step S1A4, repeating S1A2-S1A3 until the file reading is completed, obtaining the operation code sequence of the malicious code;
[0057] In an embodiment of the present application, in step S2, a corresponding Markov image is generated according to the obtained operation code sequence, and the process is as shown in the attached Fig. 3 , and the specific steps include:
[0058] Step S2A1, calculating the frequency of occurrence of operation code b i followed by b j ;
[0059] Step S2A2, calculating the probability of operation code b i followed by b j ;
[0060] Step S2A3, calculating the Markov image pixel value according to the Markov transition probability matrix, and the calculation method is as follows:
[0061]
[0062] Wherein, TM is the transition probability matrix, TM[i][j] represents the probability of operation code b i followed by b j . MP is the maximum probability in the transition probability matrix TM, and mod is the remainder function. The specific algorithm of generating Markov image based on operation code is shown in Algorithm 1: Algorithm 1. Markov image generation algorithm based on operation code
[0063]
[0064] In step S3, the generated Markov image is taken as input to train the malicious code detection model MadInG adv based on image analysis and generative adversarial network, and the malicious code detection is realized through the trained model, and the specific steps include:
[0065] Step S3A1, inputting the Markov image data set T, initializing the generator G and its parameters θ in the MadInG adv model, the discriminator D and its parameters ω;
[0066] Step S3A2, PGD attack is performed on the discriminator to obtain the adversarial sample X adv , and the generation sample Xadv The specific formula is as follows:
[0067]
[0068] X adv ={x′1,x′2,...,x′ m}
[0069] Wherein, alpha represents the size of disturbance when updating the adversarial sample each time, J(x′ i , y) represents the loss function of the model, clip x,ε is the projection operation, is the sign of the gradient of the loss function J(x′ i , y) with respect to the input x′ i , used to indicate the direction of increasing loss, represents only keeping the direction of the gradient, and ignoring the size, so as to control each update to be a fixed step size alpha, x′ i is the adversarial sample generated in the i-th iteration in the PGD attack process, X adv is the Markov image data set T={(x1, y1), (x2, y2),..., (x m , y m )} after multiple iterations in the PGD attack, the specific process of PGD attack on the discriminator is shown in algorithm 2:
[0070]
[0071]
[0072] Step S3A3, fix the generator parameters, use the adversarial sample X adv , the real sample X real and the generated sample X fake to train the discriminator, update the discriminator parameters omega, and the specific formula of parameter update is as follows:
[0073] L S =E[logP(S=real|X real )]+E[logP(S=fake|X fake )]+E[logP(S=fake|X adv )]
[0074]
[0075] Wherein, S is the output true or false result of the discriminator input data, C is the output classification result of the discriminator input data, E represents expectation, c is the correct class of the input sample, L S is the cost function of data authenticity, LC is the cost function of data classification accuracy, g ω is the (L S +L C ) gradient with respect to ω, Adam is the optimizer of the generator and discriminator in MadInG adv, and the goal of the optimizer is to maximize L S +L C ;
[0076] Step S3A4, fix the discriminator parameters, train the generator using the generated samples, update the generator parameters θ, and the specific formula of the parameter update is as follows:
[0077]
[0078] θ←Adam(g θ , θ, α, β1, β2)
[0079] Wherein, g θ is the (L S -L C ) gradient with respect to θ, and the goal of the optimizer is to minimize L S -L C ;
[0080] Step S3A5, repeat S3A2-S3A4 until the iteration number is reached, and obtain the trained discriminator D';
[0081] Step S3A6, construct a malicious code detector D0 with the same structure and parameters as the discriminator, and realize malicious code detection through D0.
[0082] The whole process of model training involved in step S3 is shown in algorithm 3:
[0083]
[0084] The above is only a preferred embodiment of the present application, and is not intended to limit the present application in other forms. Any skilled person in the art can modify or change the above disclosed technical content to obtain equivalent embodiments. However, any simple modification, equivalent change and modification of the above embodiments without departing from the technical solution of the present application, and according to the technical essence of the present application, still belong to the protection scope of the present application.
Claims
1. A method for malicious code analysis based on PGD attack countermeasure training, characterized by, Comprising the following steps: Step S1, extracting the malicious code opcode sequence from the disassembled asm file of the malicious code; Step S2, generating a corresponding Markov image according to the obtained opcode sequence; Step S3, taking the generated Markov image as input, training a malicious code detection model MadInG adv based on image analysis and generative adversarial network, and realizing malicious code detection through the trained model; In step S1, the malicious code opcode sequence is extracted from the disassembled asm file of the malicious code, which specifically includes the following steps: Step S1A1, find the code section position in the disassembled asm file by matching Segment type: Pure code, and obtain the code section name; Step S1A2, match the code section content according to the obtained code section name, and read the content in the code section by line; Step S1A3, match the content in the code section by regular expression\s([A-F0-9]{2}\s)+\s*([a-z]+), and obtain the opcodes contained in the code section; Step S1A4, repeat steps S1A2-S1A3 until the file reading is completed, and obtain the opcode sequence of the malicious code; Step S3 includes the following steps: Step S3A1, inputting a Markov image dataset T, initializing the generator G and its parameters in the MadInG adv model , the discriminator D and its discriminator parameters ; Step S3A2, PGD attack is performed on the discriminator to obtain the adversarial sample , the specific formula of generating the adversarial sample is as follows: wherein, denotes the size of the perturbation when updating the adversarial sample each time, denotes the loss function of the model, is a projection operation, is a loss function with respect to the input indicating the direction of increasing loss, denotes only keeping the direction of the gradient, while ignoring the size, so as to control each update to be a fixed step , is the adversarial sample generated in the i-th iteration in the PGD attack process, is a Markov image data set T= adversarial sample obtained after multiple iterations in the PGD attack; Step S3A3, fixing generator parameters, using adversarial samples , real samples and generated samples training discriminators, updating discriminator parameters The specific formula of parameter update is as follows: wherein, is the output authenticity result of the discriminator input data, is the output classification result of the discriminator input data, E denotes expectation, and c is the correct class of the input sample, is the cost function of data authenticity, is the cost function of data classification accuracy, is the gradient of , Adam is the optimizer of the generator and the discriminator in MadInG adv, and the objective of the optimizer is to maximize ; Step S3A4, fixing the discriminator parameter, training the generator using the generated samples, updating the generator parameter The specific formula of the parameter update is as follows: wherein, is with respect to the gradient of ; Step S3A5, repeat step S3A2-step S3A4 until the iteration number is reached, obtaining the trained discriminator ; Step S3A6, constructing a malware detector having the same structure and parameters as the discriminator , by implementing malware detection.
2. The method of claim 1, wherein the method is characterized by, In step S2, a corresponding Markov image is generated according to the obtained opcode sequence, and the specific steps include: Step S2A1, calculating operation code followed by frequency of occurrence; Step S2A2, compute operation code followed by the probability that Step S2A3, according to the Markov transition probability matrix, calculate the Markov image pixel value, and the calculation method is as follows: where TM is a transition probability matrix, an operation code is followed by the probability, is the maximum probability in the transition probability matrix TM, and mod is the modulo function.
3. A malicious code analysis system based on PGD attack countermeasure training, characterized by Run the malicious code analysis method based on PGD attack countermeasure training according to any one of claims 1-2.
4. A computer-readable storage medium, characterized in that, Comprising: The computer readable storage medium stores a computer program; The computer program is run by the processor to execute the malicious code analysis method based on PGD attack countermeasure training according to any one of claims 1-2.
Citation Information
Patent Citations
Malicious code image-oriented interpretability evaluation method
CN117313091A
Malicious code detection method and device, electronic equipment and storage medium
CN117540382A