Malicious code detection device and method based on image feature multi-attention learning
Through a malicious code detection device based on multi-attention learning of image features, the convolutional neural network and the attention module are used to extract the deep features of malicious images, which solves the problems of model complexity and insufficient feature extraction capabilities in the existing technology, and achieves Efficient malicious code identification.
Patent Information
- Application Number
- CN202210408579.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-19
- Publication Date
- 2024-08-23
- Estimated Expiration
- 2042-04-19
Smart Images

Figure CN114896594B
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of malicious code recognition, and in particular to a malicious code detection device and method based on image feature multi-attention learning. Background Art
[0002] The rapid development of information technology has brought more complex challenges to corporate network security. Among them, malware is one of the major threats to network security. Malware refers to software or code that is installed and run on a user's computer or other terminal without explicit notification or permission from the user, infringing on the user's legitimate rights and interests. Malicious code detection aims to identify malicious programs in computers or terminals to prevent them from causing greater harm.
[0003] Traditional malicious code detection methods are divided into static detection and dynamic detection. Static detection is to analyze the file content and structure of malicious code, including bytecode, assembly instructions, functions, etc. The disadvantage of static detection is that it is difficult to identify complex variants of malware, such as file encryption, packaging, and deformation. Dynamic detection is to identify malware by executing malware and analyzing its behavior. This method needs to be performed during the activation of malware, and the cost of time and hardware resources needs to be considered. In short, both static and dynamic detection require a lot of time, labor, and hardware resources, and it is difficult to meet the needs of quickly and efficiently identifying malicious code.
[0004] With the development of deep learning, some researchers have proposed a malicious code detection method based on image processing. This method gets rid of the time-consuming and labor-intensive shortcomings of traditional methods. By converting malicious code into images, the convolutional neural network is used to classify the images, thereby detecting malicious code. The current image classification task is the core of computer vision, and it mainly applies convolutional neural networks in deep learning to classify images. The main task is to give the convolutional neural network an input image, and the convolutional neural network assigns it to a label in a known mixed category. Many existing convolutional neural network models can handle image classification tasks well and achieve good accuracy. However, in the malicious code detection neural network model based on image processing, the neural network model structure is too complex. For example Figure 1 The VGGNet-16 network model structure and Figure 2The ResNet-50 network model structure shown in the figure, each block represents an operation in a convolutional neural network, Input represents data input, Conv represents convolution operation, for example, Conv3x3, 64 represents the convolution kernel size used in the convolution operation is 3x3, and the number of channels of the neural network input data is 64. MaxPool and AvgPool represent the maximum pooling operation and average pooling operation in the convolutional neural network. The FC layer and Softmax constitute the classifier of the convolutional neural network, which can classify the data into a specific category. In complex networks such as VGGNet-16 and ResNet-50, the overly complex network structure leads to more computation. Moreover, the image converted from malicious code, that is, the malicious image, has the following characteristics: First, the malicious image has key features and non-key features. When the malicious code is converted into an image, the end of the binary file is usually filled with 0 and converted to black. Therefore, the black part has nothing to do with the original code. Therefore, the area converted from the code into black, white and gray is usually called the key feature, for example Figure 3 The image features in the white frame area are key features, and the remaining black areas are non-key features. Second, malicious code has semantic information and code correlation. Since the original code has semantic correlation, the semantic information can make the code work. When the code is converted into pixels, the pixels are arranged in sequence, so there is also correlation between the pixels. Therefore, after the code is converted into an image, the pixel correlation in the malicious image maps the association between the malicious codes. However, the existing malware classification method based on image processing is not designed according to the characteristics of malicious images, and cannot extract the key features of malicious images and the correlation features between pixels at a deeper level, thereby improving the recognition accuracy of the malicious image classification model. Summary of the invention
[0005] In view of the problems in the above-mentioned prior art that the neural network model used to classify malicious images is too complex, has a large amount of calculation, and has a weak ability to extract deep features of malicious images, the present invention provides a malicious code detection device and method based on image feature multi-attention learning, aiming to improve the accuracy of malicious code recognition while reducing the complexity of the neural network model used to classify images through the system and method.
[0006] The technical solution adopted by the present invention is:
[0007] A first aspect of the present invention provides a malicious code detection device based on image feature multi-attention learning, the device comprising:
[0008] A code-image converter, used for converting an input original malicious code file into a grayscale image, and defining the grayscale image as a malicious image and sending it to a feature extractor;
[0009] A feature extractor is used to extract key features and correlation features between pixels from the malicious image it receives, so as to obtain deep features in the malicious image;
[0010] The classifier is used to classify malicious images according to the deep features extracted by the feature extractor and classify the malicious images into specific malware categories.
[0011] Furthermore, according to the malicious code detection device based on image feature multi-attention learning, the feature extractor is based on a convolutional neural network and includes three structures: a CNN module, a spatial attention module, and a self-attention module.
[0012] Further, according to the malicious code detection device based on image feature multi-attention learning, the feature extractor specifically includes:
[0013] The first CNN module is used to receive the malicious image sent by the code-image converter, firstly extract low-level semantic features in the input image through the convolution layer, then put the extracted low-level semantic features into the pooling layer through the activation function, generate feature F after reducing the dimension of the feature through the maximum pooling operation, and send the feature F to the spatial attention module;
[0014] The spatial attention module is used to extract key features from feature F. Specifically, it first further extracts high-order features from feature F through a convolutional layer and assigns weights to the extracted features according to the principle that key features have higher weights; then it uses maximum pooling and average pooling operations to aggregate the channel information of the features in the spatial dimension to generate two two-dimensional feature maps F. avg and F max , and through the splicing operation, F avg and F max Stack them together to obtain the key features with weight information after compressing the data channel; finally, multiply the key features with feature F to obtain the key features F′ with weights assigned to feature F, and send the key features F′ to the first self-attention module;
[0015] The first self-attention module is used to extract the correlation feature of the pixel on the key feature F′. Specifically, the feature F′ is first convolved to obtain three feature matrices Q, K, and V by linear mapping; the output of the matrix Q is transposed and multiplied with the output of the matrix K to obtain a correlation matrix; the correlation matrix is then multiplied with the matrix V to obtain a new matrix; finally, the new matrix with weight information and correlation features is dot-multiplied with the key feature F′ received from the spatial attention module, the original key feature F′ is given pixel correlation, the pixel correlation feature F″ in the image is obtained, and the pixel correlation feature F″ is sent to the second CNN module;
[0016] The second CNN module is used to extract higher-order features from the feature F″. The feature F″ is first subjected to a convolutional layer to extract high-level semantic features. The extracted high-level semantic features are then passed to a pooling layer through an activation function. The feature M is obtained after the feature dimension is reduced through a maximum pooling operation. The feature M is then sent to the second self-attention module.
[0017] The second self-attention module is used to extract the correlation features of pixels on the feature M. Specifically, first, a convolution operation is performed on the feature M to linearly map it to obtain three feature matrices Q, K, and V; then the output of the matrix Q is transposed and multiplied with the output of the matrix K to obtain a correlation matrix; then this correlation matrix is multiplied with the matrix V to obtain a new matrix; finally, the new matrix with correlation features is dot-multiplied with the high-order feature M received from the second CNN module to give the feature M pixel correlation, obtain the pixel correlation feature M′ in the image, and send the pixel correlation feature M′ to the classifier.
[0018] Furthermore, according to the malicious code detection device based on image feature multi-attention learning, the classifier consists of at least one linear layer and one softmax classifier.
[0019] Furthermore, according to the malicious code detection device based on image feature multi-attention learning, the classifier is composed of 3 linear layers connected together and 1 softmax classifier.
[0020] A second aspect of the present invention provides a malicious code detection method based on image feature multi-attention learning, the method comprising the following steps:
[0021] Step 100: converting the original malicious code file to be detected into a grayscale image, and defining the grayscale image as a malicious image;
[0022] Step 200: extracting low-level semantic features from the malicious image to obtain features F;
[0023] Step 300: extracting key features F′ from features F;
[0024] Step 400: extracting pixel correlation features F″ on the key features F′;
[0025] Step 500: extracting a higher-order feature from the feature F″ to obtain a feature M;
[0026] Step 600: extracting pixel correlation feature M′ from feature M;
[0027] Step 700: Map the deep-level image features M′ to the sample label space, thereby classifying the malicious image into a specific malware category.
[0028] Further, according to the malicious code detection method based on multi-attention learning of image features, the method of extracting low-level semantic features from the malicious image to obtain feature F in step 200 is the same as the method of performing higher-order feature extraction on feature F″ to obtain feature M in step 500, specifically: first, a semantic feature is extracted from the malicious image or feature F″ through a convolutional layer, and then the extracted semantic feature is passed to the pooling layer through an activation function, and the feature dimension is reduced through a maximum pooling operation, and finally feature F or feature M is generated.
[0029] Further, according to the malicious code detection method based on image feature multi-attention learning, the method of extracting the key feature F′ from the feature F in step 300 is: first, further extracting high-order features from the feature F through a convolution layer and assigning weights to the extracted features according to the principle that key features have higher weights; then, using the maximum pooling operation and the average pooling operation to aggregate the channel information of the features in the spatial dimension, generating two two-dimensional feature maps F avg and F max , and through the splicing operation, F avg and F max They are stacked together to obtain the key features with weight information after compressing the data channel; finally, the key features with weight information after compressing the data channel are multiplied by feature F to obtain the key features F′ to which the weights have been assigned on feature F.
[0030] Further, according to the malicious code detection method based on multi-attention learning of image features, the method of extracting the pixel correlation feature F″ on the key feature F′ in step 400 is the same as the method of extracting the pixel correlation feature M′ from the feature M in step 600, specifically: first, a convolution operation is performed on the feature F′ or the feature M to linearly map the feature F′ or the feature M to obtain three feature matrices Q, K, and V; then the output of the matrix Q is transposed and multiplied with the output of the matrix K to obtain a correlation matrix; then the correlation matrix is multiplied with the matrix V to obtain a new matrix; finally, the new matrix with weight information and correlation features is dot-multiplied with the key feature F′ or feature M, and the original key feature F′ or feature M is given pixel correlation to obtain the pixel correlation feature F″ or pixel correlation feature M′ in the image.
[0031] Furthermore, in any of the above-described malicious code detection devices or methods based on image feature multi-attention learning, the key features refer to features within the region containing three chromaticities of black, white and gray in the grayscale image converted from the malicious code.
[0032] The beneficial effect of adopting the above technical solution is that: the device of the present invention is based on a convolutional neural network and combines spatial attention and two self-attention modules to form a neural network model to extract deep features of the image to classify malicious images. Although the model contains several modules, the structure of the module is very simple and effective. Each module uses only a small number of convolutional layers and pooling layers, which greatly reduces the complexity and calculation amount of the model compared with the stacking of a large number of convolutional layers and pooling layers used by VGGNet and ResNet networks. Each module can extract deep features in malicious images, that is, key features in the image and pixel correlation features, which solves the problem of complex neural network models for classifying malicious images and insufficient ability to extract deep features of images. The recognition accuracy rate obtained by the system and method of the present invention on the public Malimg malicious code data set experimentally reached 96.38%, exceeding 96.10% of VGGNet, which proves that the device and method of the present invention have a higher recognition rate while reducing the complexity of the convolutional neural network model. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] Figure 1 This is a schematic diagram of the VGGNet-16 network model structure;
[0034] Figure 2 This is a schematic diagram of the ResNet-50 network model structure;
[0035] Figure 3 This is an example of key features in malicious images;
[0036] Figure 4 This is a schematic diagram of the structure of a malicious code detection device based on image feature multi-attention learning in this embodiment;
[0037] Figure 5 Schematic diagram of the process of malicious code detection method based on multi-attention learning of image features in this embodiment;
[0038] Figure 6 (a) is the image converted by malicious code example 1 belonging to the Fakerean family; (b) is the image converted by malicious code example 2 belonging to the Fakerean family; (c) is the image converted by malicious code example 3 belonging to the Fakerean family;
[0039] Figure 7 (a) is the image converted from malicious code example 1 belonging to the Dontovo family; (b) is the image converted from malicious code example 2 belonging to the Dontovo family; (c) is the image converted from malicious code example 3 belonging to the Dontovo family;
[0040] Figure 8 This is a malware family classification result diagram of the system of the present invention on the Malimg dataset; DETAILED DESCRIPTION
[0041] In order to facilitate the understanding of the present application, the present application will be described more fully below with reference to the relevant drawings. The preferred embodiments of the present application are given in the drawings. However, the present application can be implemented in many different forms and is not limited to the embodiments described herein. On the contrary, the purpose of providing these embodiments is to make the disclosure of the present application more thoroughly and comprehensively understood.
[0042] In this embodiment, the software environment is WINDOWS 10 system, and the simulation environment is PyCharm 2021.3.3x64. The malicious code file sample adopts the Malimg dataset, which includes 25 malware families, that is, 25 malware types, and the number of samples of each type is shown in Table 1. In an embodiment of the present invention, the 25 malware families are represented by numbers 0-24, respectively, for the purpose of finally outputting specific numbers to represent each malware family through the classifier in the device of the present invention. In this embodiment, the correspondence between the names of the 25 malware families and the numbers 0-24 is as follows: "Allaple.L": 0; "Allaple.A": 1; "Yuner.A": 2; "Lolyda.AA1": 3; "Lolyda.AA2": 4; "Lolyda.AA3": 5; "C2LOP.P": 6; "C2LOP.gen!g": 7; "Instantaccess": 8; "Swizzor.gen!I": 9; "Swizzor.gen!E": 10; "VB.AT": 11;"Fakerean":12;"Alueron.gen!J":13;"Malex.gen!J":14;"Lolyda.AT":15;"Adialer.C":16;"Wintrim.BX":17;"Dialpl atform.B":18; "Dontovo.A":19; "Obfuscator.AD":20; "Agent.FYI":21; "Autorun.K":22; "Rbot!gen":23; "Skintrim.N":24.
[0043] Table 1 Malware types and sample numbers in the Malimg dataset
[0044]
[0045] Figure 4 Schematic diagram of the structure of the malicious code detection device based on image feature multi-attention learning in this embodiment. Figure 4As shown, the device includes three parts: a code-image converter, a feature extractor and a classifier. The code-image converter is used to convert the input original malicious code file into a grayscale image, and define the grayscale image as a malicious image and send it to the feature extractor; the feature extractor is used to extract key features and correlation features from the malicious image it receives, so as to obtain deep-level features in the malicious image; the classifier is used to classify the malicious image according to the deep-level features extracted by the feature extractor, and divide the malicious image into specific malware family categories. The feature extractor and the classifier constitute a neural network model in the device of the present invention, which is called a FA model (Fusion Attention Module).
[0046] like Figure 4 As shown, the feature extractor is based on a convolutional neural network and includes three structures: a CNN module, a spatial attention module, and a self-attention module, specifically including:
[0047] The first CNN module is used to receive the malicious image sent by the code-image converter, firstly extract low-level semantic features in the input image through the convolution layer, then pass the extracted low-level semantic features to the pooling layer through the activation function, perform feature dimension reduction through the maximum pooling operation, reduce network model parameters, and finally generate feature F, and send feature F to the spatial attention module;
[0048] The spatial attention module is used to extract key features from feature F. Specifically, it first further extracts high-order features from feature F through a convolutional layer and assigns weights to the extracted features according to the principle that key features have higher weights; then it uses maximum pooling and average pooling operations to aggregate the channel information of the features in the spatial dimension to generate two two-dimensional feature maps F. avg and F max , and through the splicing operation, F avg and F max Stack them together to obtain the key features with weight information after compressing the data channel; finally, multiply the key features with weight information after compressing the data channel by feature F to obtain the key feature F′ with weight assigned to feature F, and send the key feature F′ to the first self-attention module;
[0049] The first self-attention module is used to extract the correlation features of pixels on the key feature F′. Specifically, firstly, the feature F′ is convolved and linearly mapped to obtain three feature matrices Q, K, and V. The difference between the three feature matrices Q, K, and V is only the size of the output channels. After obtaining the three feature matrices, the output of the matrix Q is transposed and multiplied by the output of the matrix K to obtain a correlation matrix. Then, this correlation matrix is multiplied with the matrix V to obtain a new matrix. At this time, each pixel in the new matrix is related to the key feature F′ received from the spatial attention module, and the new matrix contains weight information. Finally, the new matrix with weight information and correlation features is dot-multiplied with the key feature F′ received from the spatial attention module to give the original key feature F′ pixel correlation, obtain the pixel correlation feature F″ in the image, and send the pixel correlation feature F″ to the second CNN module.
[0050] The second CNN module is used to extract higher-order features from the feature F″. The feature F″ is first passed through the convolution layer to extract high-level semantic features, and then the extracted high-level semantic features are passed to the pooling layer through the activation function. The feature dimension is reduced through the maximum pooling operation to reduce the network model parameters, and finally the feature M is generated and sent to the second self-attention module;
[0051] The second self-attention module is used to extract the correlation features of pixels on the feature M. Specifically, first, the feature M is convolved to complete the linear mapping to obtain three feature matrices Q, K, and V; after obtaining the three feature matrices, the output of the matrix Q is transposed and multiplied with the output of the matrix K to obtain a correlation matrix; then this correlation matrix is multiplied with the matrix V to obtain a new matrix. At this time, each pixel in the new matrix is related to the high-order feature M received from the second CNN module. Finally, the new matrix with correlation features is dot-multiplied with the high-order feature M received from the second CNN module, and the feature M is given pixel correlation, and the pixel correlation feature M′ in the image is obtained, and the pixel correlation feature M′ is sent to the classifier.
[0052] like Figure 4As shown, the classifier is composed of at least one linear layer and one softmax classifier. In this embodiment, the classifier is composed of three linear layers connected together and one softmax classifier, which is used to map the deep image features extracted by the feature extractor to the sample label space, that is, to classify the deep image features received by the classifier. The linear layer is a model classification structure in the deep learning framework Pytorch, and its function is to linearly transform the obtained deep image features to generate the input features required by softmax. The function of the three linear layers in this embodiment is that each linear layer reduces the dimension of the features, and the neural network parameters decrease in sequence. The softmax classifier can normalize these features and assign weights, and finally assign each feature matrix to a specific malicious family category, generate a corresponding number representing the malicious family category, and thus obtain the corresponding malware family category. Table 2 shows the detailed parameters of each layer in the FA model in this embodiment.
[0053] Table 2 Detailed parameters of each layer in the FA model
[0054]
[0055] Figure 5 The flowchart of the malicious code detection method based on image feature multi-attention learning is intended to train and verify the malicious code detection device based on image feature multi-attention learning, including the following steps:
[0056] Step 1: Convert the original malicious code file into grayscale images, normalize all grayscale images, and divide them into training set and test set according to a certain ratio;
[0057] Step 1.1: Convert the original malicious code file into a grayscale image;
[0058] In this embodiment, as described above, the original malicious code file sample uses the Malimg data set. The Malimg data set is obtained. The original malicious code file in the Malimg data set contains a binary bit string, such as 011100110101100101101101010. The binary bit string in the malicious code file is converted into a grayscale image according to the following method: First, these binary bit strings are extracted and written out and stored in the computer using the open and write functions in the Python language. Taking each byte in the malicious code file as a unit, each binary bit string is split and read as an 8-bit vector, that is, the stored binary bit string is extracted according to 8 bits, and then these vectors are converted into decimal unsigned integers through the base conversion operation in the Python language, and mapped to the space of 0-255. The base conversion calculation formula for each byte is: Where I is the mapping value. For example: 01100000, 10101100, the corresponding mapping values are 96, 172 calculated by the base conversion formula. In this way, all codes can be mapped to values between 0-255, and finally converted into grayscale images in the range of 0 (black)-255 (white) to obtain image sample data sets, such as Figure 6 and Figure 7 shown.
[0059] Step 1.2: Normalize all grayscale images and construct an image sample data set;
[0060] The image is processed into a fixed size of uniform size. The reason for this is that the FA model (Fusion Attention Module) composed of a feature extractor and a classifier requires image input of uniform size, and small-size images have better recognition effects. In order to prevent overfitting of the FA model, the RandomCrop method in the deep learning framework pytorch is used to randomly crop the image data, which can play a role in data enhancement. The data is normalized using the Normalization operation, which can map the data to the range of 0-1, reduce the amount of calculation of the FA model, and improve the accuracy and convergence speed of the FA model.
[0061] Step 1.3: Divide the image sample data set into a training set and a test set according to a certain ratio;
[0062] After converting all malicious code files into grayscale images, an image sample data set is formed. In order to train and test the built model later, the image sample data set needs to be divided. In this embodiment, the image sample data set is divided into a training set and a test set in a ratio of 8:2 using the Python language. Then the data set is traversed using a for loop, and a new training set folder and a test set folder are formed using the os.path.join method in the Python dependency package os, and the allocated images are added to the two folders.
[0063] Step 2: Input the image sample data in the training set into Figure 4 In the malicious code detection device based on image feature multi-attention learning shown, forward propagation is performed on the malicious code detection device based on image feature multi-attention learning to obtain a prediction result.
[0064] Step 2.1: Extract low-level semantic features from the input image through the first CNN module to obtain feature F;
[0065] In this embodiment, specifically, the malicious image in the training set is input into the first CNN module: in the first CNN module, the low-level semantic features in the input image are first extracted through the convolution layer, and then the extracted low-level semantic features are transferred to the pooling layer through the activation function, and the dimension of the features is reduced through the maximum pooling operation, and finally the feature F is generated;
[0066] Step 2.2: Extract the key feature F′ from the feature F through the spatial attention module;
[0067] In the spatial attention module of this embodiment, firstly, a convolution layer is used to further extract high-order features from feature F and weights are assigned to the extracted features according to the principle that key features have higher weights; then, the maximum pooling operation and the average pooling operation are used to aggregate the channel information of the features in the spatial dimension to generate two two-dimensional feature maps Favg and Fmax, and Favg and Fmax are stacked together through a splicing operation to obtain key features with weight information after compressing the data channel; finally, the key features with weight information after compressing the data channel are multiplied by feature F to obtain key features F′ to which weights have been assigned on feature F;
[0068] The calculation formula for feature extraction of the spatial attention module is:
[0069]
[0070]
[0071] Among them, M s represents the entire operation of the spatial attention module to extract features; F is the input image; σ represents the activation function; f 7x7 represents the convolution operation using a 7x7 convolution kernel; AvgPool and MaxPool represent average pooling and maximum pooling, respectively, in formula (2). as well as Indicates; R represents the feature matrix, 1×H×W represents the feature size of a channel with a length of H and a width of W;
[0072] Step 2.3: Extract the pixel correlation feature F″ on the key feature F′ through the first self-attention module;
[0073] In the first self-attention module of this embodiment, the feature F′ is first convolved and linearly mapped to obtain three feature matrices Q, K, and V; then the output of the matrix Q is transposed and multiplied with the output of the matrix K to obtain a correlation matrix; then the correlation matrix is multiplied with the matrix V to obtain a new matrix; finally, the new matrix with weight information and correlation features is dot-multiplied with the key feature F′ received from the spatial attention module to obtain the pixel correlation feature F″ in the image;
[0074] The operation formula of the self-attention module is:
[0075] Attention(Q,K,V)=softmax(B i,j )V·F (3)
[0076] B i,j =Q(x i ) T K(x j ) (4)
[0077] Among them, softmax represents the activation function; Q, K, and V represent three feature matrices; B i,j Used to represent i th Position pair generation th The relationship weight of the position, that is, the relationship weight between different pixels; F represents the original input feature; Q(x) and K(x) represent the feature matrices Q and K generated after the convolution operation; Q(x i ) T Represents the transposed matrix of the generated feature matrix Q.
[0078] Step 2.4: Use the second CNN module to extract higher-order features from feature F″ to obtain feature M;
[0079] In the second CNN module of this embodiment, high-level semantic features are first extracted from the feature F″ through the convolution layer, and then the extracted high-level semantic features are put into the pooling layer through the activation function, and the feature dimension is reduced through the maximum pooling operation to finally generate the feature M.
[0080] Step 2.5: Extract pixel correlation feature M′ from feature M through the second self-attention module;
[0081] In the second self-attention module of this embodiment, the feature M is first convolved to complete linear mapping to obtain three feature matrices Q, K, and V; then the output of the matrix Q is transposed and multiplied with the output of the matrix K to obtain a correlation matrix; then this correlation matrix is multiplied with the matrix V to obtain a new matrix; finally, the new matrix with correlation features is dot-multiplied with the high-order feature M received from the second CNN module to give the feature M pixel correlation and obtain the pixel correlation feature M′ in the image.
[0082] Step 2.6: Map the deep image features M′ to the sample label space through the classifier, that is, classify the features.
[0083] In the classifier of this embodiment, the feature M′ is firstly reduced in dimension through three linear layers continuously, so that the parameters of the neural network are successively reduced; then, the features are normalized and weighted through the softmax classifier, and finally each corresponding feature matrix is assigned to a specific malicious family category, and a corresponding number representing the malicious family category is generated, thereby obtaining the corresponding malware family category.
[0084] Step 3: Calculate the loss value based on the prediction result obtained by forward propagation of the malicious code detection device based on image feature multi-attention learning, and perform back propagation to update the parameters of the malicious code detection device based on image feature multi-attention learning.
[0085] This embodiment uses the stochastic gradient descent method to update the parameters of the malicious code detection device based on image feature multi-attention learning. The loss function formula used in training is as follows:
[0086]
[0087] Where p=[p0,……,p c-1 ] represents a probability distribution, each element p i represents the probability that the sample belongs to the i-th type of malware; y=[y0,……,y c-1 ] is the vector representation of the sample label. When the sample belongs to category i, y i =1, otherwise y i =0; c represents the sample label.
[0088] During training, the network layer uses the Initialization method to initialize the network parameters, the training batch size is set to 128, the initial learning rate is set to 0.005, and the training rounds are set to 100. During the training process, the device will be tested on the test set every 10 rounds of training, the test results will be output to the log file, and the device parameter file will be saved in the pth file.
[0089] Step 4: Use ACC as the measurement index of detection performance, that is, the ratio of predicted correct samples to total samples. Use the test set to test and evaluate the trained malicious code detection device based on multi-attention learning of image features, and adjust the parameters of the current device according to the evaluation results, and retrain the device according to the method of step 2. Test and evaluate the retrained device again, and repeat the training and test evaluation operations on the device until the optimal device that meets the measurement index is obtained, and the device is used as the final malicious code detection device based on multi-attention learning of image features.
[0090] In order to better illustrate the experimental results and performance improvement of the device, the experimental results of different models on the Malimg dataset are compared, and the specific results are shown in Table 3. These include the classic image classification network VGGNet and some basic CNN models. The model of the feature extractor and classifier combination in the device of the present invention is referred to as the FA model (FusionAttention Module, fusion attention model).
[0091] Table 3 Comparison of experimental results of different models
[0092]
[0093]
[0094] It can be seen from Table 3 that the recognition effect of the FA model has exceeded that of the basic CNN model, and the FA model is comparable to the complex network VGGNet on the Malimg dataset. This shows that the device of the present invention is effective in obtaining deep-level features of the image while reducing the complexity of the neural network model.
[0095] In addition to comparing the accuracy between models, Figure 8 The family classification results of the device of the present invention on the Malimg dataset are shown in FIG. Figure 8 The horizontal axis represents the true label, and the vertical axis represents the predicted label. When the horizontal axis and the vertical axis correspond to 0, it means that the prediction is correct, and the other numbers represent the number of families on the vertical axis predicted as the horizontal axis families. It can be seen that the vast majority of families can be well predicted, maintaining 100% recognition accuracy, and only a small number of samples are predicted incorrectly. There are 4 samples in the Allaple.L class that are predicted to be the Allaple.A class, and 5 samples in the Allaple.A class that are predicted to be the Allaple.L class; 3 samples in the Lolyda.AA2 class that are predicted to be the Lolyda.AA1 class; 7 samples in the C2LOP.P class that are predicted to be the C2LOP.gen! g class; 6 samples in the Swizzor.gen! I class that are predicted to be the Swizzor.gen! E class; it can be found that the prediction error occurs between two subclasses of the same large class. Due to the homology between the subclasses, the differences between them are very small. This shows that the FA model in the device of the present invention captures the characteristics of the same family well, and extracting features through pixel correlation is conducive to the model's recognition of the same family.
[0096] In summary, the device and method of the present invention achieve the requirements of efficient and accurate malicious code identification. It solves the problems of high time cost, labor cost and resource cost of traditional malicious code identification. In the existing image-based methods, it solves the problems of complex models and insufficient ability of models to extract deep-level features of malicious code.
[0097] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some or all of the technical features therein. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope defined by the claims of the present invention.
Claims
1. A malicious code detection device based on image feature multi-attention learning, characterized in that: The device includes: A code-image converter, used for converting an input original malicious code file into a grayscale image, and defining the grayscale image as a malicious image and sending it to a feature extractor; A feature extractor is used to extract key features and correlation features between pixels from the malicious image it receives, so as to obtain deep features in the malicious image; A classifier is used to classify malicious images into specific malware categories based on the deep features extracted by the feature extractor; The feature extractor specifically comprises: The first CNN module is used to receive the malicious image sent by the code-image converter, firstly extract low-level semantic features in the input image through the convolution layer, then put the extracted low-level semantic features into the pooling layer through the activation function, generate feature F after reducing the dimension of the feature through the maximum pooling operation, and send the feature F to the spatial attention module; The spatial attention module is used to extract key features from feature F. Specifically, it first further extracts high-order features from feature F through a convolutional layer and assigns weights to the extracted features according to the principle that key features have higher weights; then it uses maximum pooling and average pooling operations to aggregate the channel information of the features in the spatial dimension to generate two two-dimensional feature maps F. avg and F max , and through the splicing operation, F avg and F max Stack them together to obtain the key features with weight information after compressing the data channel; finally, multiply the key features with feature F to obtain the key features F' with weights assigned to feature F, and send the key features F' to the first self-attention module; The first self-attention module is used to extract the correlation feature of the pixel on the key feature F'. Specifically, firstly, the feature F' is convolved to obtain three feature matrices Q, K, and V by linear mapping; then the output of the matrix Q is transposed and multiplied with the output of the matrix K to obtain a correlation matrix; then the correlation matrix is multiplied with the matrix V to obtain a new matrix; finally, the new matrix with weight information and correlation features is dot-multiplied with the key feature F' received from the spatial attention module, the original key feature F' is given pixel correlation, and the pixel correlation feature F' in the image is obtained, and the pixel correlation feature F' is sent to the second CNN module; The second CNN module is used to extract higher-order features from feature F'. Feature F' is first passed through the convolution layer to extract high-level semantic features, and then the extracted high-level semantic features are passed to the pooling layer through the activation function. The feature M is obtained after the feature dimension is reduced through the maximum pooling operation, and the feature M is sent to the second self-attention module; The second self-attention module is used to extract the correlation features of pixels on the feature M. Specifically, firstly, a convolution operation is performed on the feature M to linearly map it to obtain three feature matrices Q, K, and V; then the output of the matrix Q is transposed and multiplied with the output of the matrix K to obtain a correlation matrix; then this correlation matrix is multiplied with the matrix V to obtain a new matrix; finally, the new matrix with correlation features is dot-multiplied with the high-order feature M received from the second CNN module to give the feature M pixel correlation, obtain the pixel correlation feature M' in the image, and send the pixel correlation feature M' to the classifier.
2. The malicious code detection device based on image feature multi-attention learning according to claim 1 is characterized in that: The feature extractor is based on a convolutional neural network and includes three structures: a CNN module, a spatial attention module, and a self-attention module.
3. The malicious code detection device based on image feature multi-attention learning according to claim 1 is characterized in that: The classifier consists of at least one linear layer and one softmax classifier.
4. The malicious code detection device based on image feature multi-attention learning according to claim 1 is characterized in that: The classifier consists of 3 linear layers connected together and 1 softmax classifier.
5. A malicious code detection method based on the malicious code detection device according to claim 1, characterized in that: The method comprises the following steps: Step 100: converting the original malicious code file to be detected into a grayscale image, and defining the grayscale image as a malicious image; Step 200: extracting low-level semantic features from the malicious image to obtain features F; Step 300: extracting key features F' from features F; Step 400: extracting pixel correlation features F″ on the key features F′; Step 500: extracting a higher-order feature from feature F″ to obtain feature M; Step 600: extracting pixel correlation feature M' from feature M; Step 700: Map the deep-level image features M' to the sample tag space, thereby classifying the malicious image into a specific malware category.
6. The malicious code detection method based on image feature multi-attention learning according to claim 5 is characterized in that: The method of extracting low-level semantic features from the malicious image to obtain feature F in step 200 is the same as the method of extracting higher-order features from feature F" to obtain feature M in step 500, specifically: first, semantic features are extracted from the malicious image or feature F" through a convolutional layer, and then the extracted semantic features are passed to the pooling layer through an activation function, and feature dimensionality reduction is performed through a maximum pooling operation to finally generate feature F or feature M.
7. The malicious code detection method based on image feature multi-attention learning according to claim 5 is characterized in that: The method for extracting the key feature F' from the feature F in step 300 is: firstly, a convolution layer is used to further extract high-order features from the feature F and weights are assigned to the extracted features according to the principle that the key features have higher weights; then, a maximum pooling operation and an average pooling operation are used to aggregate the channel information of the features in the spatial dimension to generate two two-dimensional feature maps F avg and F max , and through the splicing operation, F avg and F max They are stacked together to obtain the key features with weight information after compressing the data channel; finally, the key features with weight information after compressing the data channel are multiplied by feature F to obtain the key features F' to which the weights have been assigned on feature F.
8. The malicious code detection method based on image feature multi-attention learning according to claim 5 is characterized in that: The method of extracting the pixel correlation feature F" on the key feature F' described in step 400 is the same as the method of extracting the pixel correlation feature M' on the feature M described in step 600, specifically: first, perform a convolution operation on the feature F' or feature M to linearly map the feature F' or feature M to obtain three feature matrices Q, K, and V; then transpose the output of the matrix Q and multiply it with the output of the matrix K to obtain a correlation matrix; then multiply this correlation matrix with the matrix V to obtain a new matrix; finally, perform a dot multiplication on the new matrix with weight information and correlation features with the key feature F' or feature M, assign pixel correlation to the original key feature F' or feature M, and obtain the pixel correlation feature F" or pixel correlation feature M' in the image.
9. The malicious code detection method based on image feature multi-attention learning according to claim 5 is characterized in that: The key features refer to features within the region containing three chromaticities of black, white and gray in the grayscale image converted from malicious code.
