Malicious code detection method and device, equipment and storage medium

By format conversion of the source code and back propagation to calculate the correlation score matrix, the problem of the lack of interpretability of the deep learning model in malicious code detection is solved, and the effective positioning of malicious code and the reliability assessment of the detection results are achieved.

CN118797633APending Publication Date: 2024-10-18CHINA MOBILE GROUP ZHEJIANG +1

Patent Information

Application Number
CN202311382152.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-10-23
Publication Date
2024-10-18

AI Technical Summary

Technical Problem

Existing deep learning models lack interpretability in malicious code detection and cannot determine which parts of the source code are malicious operations, resulting in an inestimable effectiveness and reliability of detection results.

Method used

By converting the source code format to be detected into a grayscale image and inputting it into the pre-trained code detection model, after obtaining the prediction results, backpropagation is performed to calculate the correlation score matrix between feature layers, and detection and interpretation are performed based on the correlation score matrix. Malicious code, locate the malicious operation part.

Benefits of technology

It realizes the detection of malicious code and its type, and determines the reliability and validity of the detection results through reverse index, providing interpretability of the detection process.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118797633A_ABST
    Figure CN118797633A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of deep learning, and provides a malicious code detection method and device, equipment and a storage medium. The method comprises the steps of obtaining a source code to be detected and performing format conversion on the source code to obtain a grayscale image corresponding to the source code; inputting the grayscale image into a pre-trained code detection model to obtain a prediction result of the grayscale image; performing back propagation on the prediction result, and calculating a correlation score matrix between any two adjacent feature layers in the code detection model; and detecting malicious codes in the source code according to the correlation score matrix. Through back propagation of the prediction result, malicious codes contained in the source code and the type of the malicious codes can be detected, the detection process can be explained, the part with high correlation with the detection result can be determined, the malicious codes are positioned through a reverse index, and therefore the codes belonging to malicious operation in the source code are determined. And the reliability and effectiveness of a detection result are ensured.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of deep learning technology, and specifically to a malicious code detection method, device, equipment and storage medium. Background Art

[0002] In the early days, the detection of malicious code that uses browsers and web pages as transmission channels mainly understood program behavior from the grammatical and semantic levels, and the detection method used was mainly to scan and match the source program or binary code of the malicious code library. However, the number and types of malicious codes are increasing, and with the development and application of code obfuscation, code mutation and encryption technology, the workload of collecting, classifying, entering, analyzing and evaluating malicious codes is increasing, and the research direction of malicious code detection has gradually shifted to automated or semi-automated methods. Currently, the more popular methods include two categories, namely machine learning methods based on opcode sequence features, and grayscale images composed of decimal integer data obtained through decompilation, using image feature extraction technology for prediction and classification. However, both methods only consider opcode sequence features or image texture features, and the feature dimension is single, which cannot reflect the impact of internal information compression and encryption on the prediction results. In addition, the "black box" nature of the deep learning model itself leads to the problem that causal reasoning cannot be done between the model's input data and the predicted results, making the prediction results obtained by the deep learning model based on single-dimensional features unexplainable. It can only detect whether the source code contains malicious code and the type of malicious code contained, but cannot determine which codes belong to malicious operations, and thus cannot estimate the effectiveness and reliability of the detection results. Summary of the invention

[0003] The embodiments of the present application provide a malicious code detection method, apparatus, device and storage medium to solve the technical problem that the existing deep learning model is not interpretable for malicious code detection, can only give the type of malicious code detected, but cannot determine which parts of the source code belong to malicious code, resulting in the inability to estimate the validity and reliability of the detection results.

[0004] In a first aspect, an embodiment of the present application provides a malicious code detection method, comprising:

[0005] Acquire the source code to be detected, and convert the format of the source code to obtain a grayscale image corresponding to the source code;

[0006] Inputting the grayscale image into a pre-trained code detection model to obtain a prediction result of the code detection model on the grayscale image;

[0007] Back-propagating the prediction results, and calculating the correlation score matrix between any two adjacent feature layers in the code detection model;

[0008] Malicious code in the source code is detected according to the correlation score matrix.

[0009] In one embodiment, back-propagating the prediction results to calculate the correlation score matrix between any two adjacent feature layers in the code detection model includes:

[0010] Back-propagating the prediction result, and determining a first feature layer in sequence from each feature layer of the code detection model in a back-propagation direction;

[0011] Calculating correlation scores of first target neurons in the first feature layer and second target neurons in the second feature layer, and obtaining a correlation score matrix between the first feature layer and the second feature layer according to the correlation scores;

[0012] The first target neuron is any one in the first feature layer, and the second target neuron is any one in the second feature layer; the second feature layer is adjacent to the first feature layer, and in the reverse propagation direction, the second feature layer is the next layer of the first feature layer;

[0013] The second feature layer is used as the first feature layer, and the step of calculating the correlation score between the first target neuron in the first feature layer and each second target neuron in the second feature layer is returned and executed until the first feature layer is the input layer of the code detection model.

[0014] In one embodiment, calculating the correlation score between the first target neuron in the first feature layer and each second target neuron in the second feature layer includes:

[0015] Acquire a first output value of a first target neuron in the first feature layer, a first parameter matrix of the first feature layer, and a parameter matrix of the second feature layer; the first parameter matrix includes weight values ​​of neurons in the first feature layer, and the second parameter matrix includes weight values ​​of each neuron in the second feature layer;

[0016] Based on the second parameter matrix, calculating the product of the first output value and the weight value of each second target neuron in the second feature layer to obtain a first eigenvalue;

[0017] Based on the first parameter matrix, calculating the weighted sum of the output values ​​of each neuron in the first feature layer to obtain a second eigenvalue;

[0018] A correlation score between the first target neuron and each of the second target neurons is determined according to a ratio of the first eigenvalue to the second eigenvalue.

[0019] In one embodiment, detecting malicious code in the source code according to the correlation score matrix includes:

[0020] Standardizing the correlation score matrix, and constructing a correlation score map corresponding to the grayscale image based on the standardized correlation score matrix;

[0021] Determine, according to the correlation score map, the target area in the grayscale image that has the highest contribution to the prediction result;

[0022] The source code is indexed according to the target area to determine malicious code in the source code.

[0023] In one embodiment, after detecting malicious code in the source code according to the correlation score matrix, the method further includes:

[0024] Determining a target category of the grayscale image according to the prediction result;

[0025] Acquire a template image corresponding to the target category and a correlation score matrix corresponding to the template image; the template image is a sample image with the highest prediction probability under the target category selected from the training samples based on the training results of the code detection model;

[0026] Calculating the Hessian vector product between the correlation score matrix corresponding to the grayscale image and the correlation score matrix of the template image to determine the similarity between the grayscale image and the template image;

[0027] A first comparison area in the grayscale image is marked to obtain comparison interpretation information of the grayscale image; wherein the similarity between the first comparison area and the second comparison area in the template image is higher than a preset threshold.

[0028] In one embodiment, converting the source code to obtain a grayscale image corresponding to the source code includes:

[0029] Decompiling the source code to generate a binary file corresponding to the source code;

[0030] Extracting the binary file, and converting the extracted binary data into decimal data in groups of a preset number of bytes;

[0031] Based on the number of bytes corresponding to the preset width, converting the one-dimensional vector corresponding to the decimal data into a two-dimensional matrix;

[0032] A grayscale image corresponding to the source code is generated based on the two-dimensional matrix.

[0033] In one embodiment, the malicious code detection method further includes:

[0034] Acquire a sample code file; the sample code file includes a malicious code file, the source code in the malicious code file includes known malicious code; the sample code file includes label data, the label data is used to characterize the category of the sample code file;

[0035] Generate a hash value of the sample code file, build a code file library based on the sample code file and the hash value, and build a tag database based on the hash value and tag data of the sample code file;

[0036] Performing format conversion on the sample code file based on the code file library to obtain a sample grayscale image corresponding to the sample code file;

[0037] Constructing a sample data set based on the sample grayscale image and the label database, and iteratively training a preset basic detection model using the sample data set to obtain a first detection model;

[0038] The first detection model is re-parameterized to obtain a pre-trained code detection model.

[0039] In a second aspect, an embodiment of the present application provides a malicious code detection device, including:

[0040] A conversion module is used to obtain the source code to be detected, and perform format conversion on the source code to obtain a grayscale image corresponding to the source code;

[0041] A prediction module, used for inputting the grayscale image into a pre-trained code detection model to obtain a prediction result of the code detection model on the grayscale image;

[0042] An interpretation module, used for back-propagating the prediction results and calculating a correlation score matrix between any two adjacent feature layers in the code detection model;

[0043] A detection module is used to detect malicious code in the source code according to the correlation score matrix.

[0044] In a third aspect, an embodiment of the present application provides an electronic device, comprising a processor and a memory storing a computer program, wherein the processor implements the steps of the malicious code detection method described in the first aspect when executing the program.

[0045] In a fourth aspect, an embodiment of the present application provides a non-transitory computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the steps of the malicious code detection method described in the first aspect are implemented.

[0046] In a fifth aspect, an embodiment of the present application provides a computer program product, including a computer program, which, when executed by a processor, implements the steps of the malicious code detection method described in the first aspect.

[0047] The malicious code detection method, device, equipment and storage medium provided in the embodiment of the present application obtain the source code to be detected, and convert the format of the source code to obtain the grayscale image corresponding to the source code and input it into the pre-trained code detection model to obtain the prediction result of the grayscale image; the prediction result is back-propagated to calculate the correlation score matrix between any two adjacent feature layers in the code detection model; according to the obtained correlation score matrix, the malicious code in the source code is detected. By back-propagating the prediction result, not only the malicious code and its type contained in the source code can be detected, but also the detection process can be explained, the part with high correlation with the detection result can be determined, and the malicious code can be located through the reverse index, so as to determine which codes in the source code belong to malicious operations, and ensure the reliability and effectiveness of the detection result. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] In order to more clearly illustrate the technical solutions in the present application or the prior art, a brief introduction will be given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0049] Figure 1 This is one of the flowcharts of the malicious code detection method provided in the embodiment of the present application;

[0050] Figure 2 It is a schematic diagram of an explanation process of the prediction process of the code detection model provided by an embodiment of the present application;

[0051] Figure 3 It is a schematic diagram of the comparative interpretation process of the code detection model provided in an embodiment of the present application;

[0052] Figure 4 It is a schematic diagram of the format conversion process of source code provided in an embodiment of the present application;

[0053] Figure 5 This is the second flow chart of the malicious code detection method provided in the embodiment of the present application;

[0054] Figure 6 This is the third flow chart of the malicious code detection method provided in the embodiment of the present application;

[0055] Figure 7It is a schematic diagram of the structure of a malicious code detection device provided in an embodiment of the present application;

[0056] Figure 8 It is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0057] In order to make the purpose, technical solutions and advantages of this application clearer, the technical solutions in this application will be clearly and completely described below in conjunction with the drawings in the embodiments of this application. Obviously, the described embodiments are part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0058] Reference Figure 1 , Figure 1 A flowchart of a malicious code detection method provided in an embodiment of the present application. Figure 1 The malicious code detection method provided in the embodiment of the present application may include:

[0059] Step 100, obtaining a source code to be detected, and converting the format of the source code to obtain a grayscale image corresponding to the source code;

[0060] First, the source code to be detected is obtained, which can exist in the form of a code file or a code text. The obtained source code to be detected is formatted to obtain a grayscale image corresponding to the source code. Specifically, the source code is mapped by decompiling and other methods, and the source code is mapped to a binary or decimal number, and the number is used as a pixel value to form a grayscale image.

[0061] Step 200, inputting the grayscale image into a pre-trained code detection model to obtain a prediction result of the code detection model on the grayscale image;

[0062] The grayscale image is input into a pre-trained code detection model to obtain the prediction result of the code detection model for the grayscale image. The prediction result indicates whether there is malicious code in the source code corresponding to the grayscale image, and the type of malicious code contained therein. The types of malicious code include backdoors, botnets, downloaders, spyware, launchers, kernel kits, worms or computer viruses, etc. For the pre-trained code detection model, it can be obtained by pre-training a preset basic detection model based on a sample data set constructed based on a code file containing malicious code. The pre-training training method includes but is not limited to iterative training and recursive training, etc. The preset basic detection model can be a deep learning model, such as a VGG19 model, etc., and no specific limitation is made to the basic detection model and its training method.

[0063] Step 300, back-propagating the prediction results, and calculating the correlation score matrix between any two adjacent feature layers in the code detection model;

[0064] The prediction results are back-propagated to calculate the correlation score matrix between any two adjacent feature layers in the code detection model. For deep learning models, multiple feature layers are generally included, including input layers, output layers, and multiple convolutional layers. Generally, the feature transfer direction of the deep learning model during detection is taken as the forward propagation direction, that is, the direction from the input layer to the output layer is forward propagation, and the direction from the output layer to the input layer is reverse propagation. According to the reverse propagation direction, the prediction results are back-propagated, and the LRP (Layer-wise RelevancePropagation, correlation score layer-by-layer propagation) technology is used. The prediction results are used as the starting point for calculation, and the correlation score matrix between any feature layer and the previous layer in the forward propagation direction is calculated in turn.

[0065] Furthermore, the correlation score matrix is ​​used to characterize the correlation between two adjacent feature layers, and the distribution of the correlation size in the neurons of the feature layer. Each eigenvalue in the correlation score matrix is ​​the correlation score corresponding to each neuron in the feature layer, and the correlation score is used to characterize the correlation size corresponding to the neuron. That is, based on the correlation score matrix corresponding to any feature layer, it is possible to determine the correlation size of the output value of each neuron in the feature layer with the output result of the previous feature layer, and the correlation size with the output result of the next feature layer during the feature transfer process.

[0066] Step 400: Detect malicious code in the source code according to the correlation score matrix.

[0067] According to the correlation score matrix between any two adjacent feature layers in the code detection model, malicious code in the source code can be detected. The obtained correlation score matrix can explain the prediction process of the code detection model to obtain the prediction result. The LRP technology starts with the output of the model and back-propagates to the input of the model, explaining the model prediction caused by the input features. At the same time, based on the correlation score matrix, the part with high correlation between the prediction result and each feature layer can be determined, that is, the main basis for the code detection model to derive the prediction result. The part with high correlation is indexed, and the part of malicious operation in the source code is determined by data scaling and other methods, so as to detect malicious code in the source code.

[0068] Compared with the existing malicious code detection method, which can only give whether the source code contains malicious code and the type of malicious code contained, in this embodiment, the LRP technology is used to determine the part with high correlation with the prediction result, and then the malicious code is determined by reverse indexing the part with high correlation. On the basis of detecting the malicious code and its type, not only can the detection process of the code detection model be explained, but also the specific location of the malicious code in the source code can be analyzed and located through reverse indexing, that is, the malicious code is extracted from the source code, and then it is determined whether the detection result is reliable and effective, so as to estimate the reliability and effectiveness of the detection result. Exemplarily, the part with high correlation with the prediction result is matched with the known malicious code by feature matching, so as to determine the matching degree of the part with high correlation with the prediction result and the feature of the known malicious code, so as to determine whether the part with high correlation with the prediction result is reliable and effective, and the effectiveness and reliability of the prediction result can be evaluated.

[0069] In this embodiment, by obtaining the source code to be detected and converting the format of the source code, a grayscale image corresponding to the source code is obtained and input into a pre-trained code detection model to obtain a prediction result of the grayscale image; the prediction result is back-propagated to calculate the correlation score matrix between any two adjacent feature layers in the code detection model; and malicious code in the source code is detected according to the obtained correlation score matrix. By back-propagating the prediction result, not only the malicious code and its type contained in the source code can be detected, but also the detection process can be explained, the part with a high correlation with the detection result can be determined, and the malicious code can be located through the reverse index, so as to determine which codes in the source code belong to malicious operations, and ensure the reliability and effectiveness of the detection result.

[0070] Preferably, in step 300, back propagation is performed on the prediction result of the code detection model to calculate the correlation score matrix between any two adjacent feature layers in the code detection model, specifically including:

[0071] Step 201, back-propagating the prediction result, and determining the first feature layer in order from each feature layer of the code detection model in the back-propagation direction;

[0072] Step 202, calculating the correlation scores between the first target neurons in the first feature layer and the second target neurons in the second feature layer, and obtaining a correlation score matrix between the first feature layer and the second feature layer according to the correlation scores;

[0073] The first target neuron is any one in the first feature layer, and the second target neuron is any one in the second feature layer; the second feature layer is adjacent to the first feature layer, and in the reverse propagation direction, the second feature layer is the next layer of the first feature layer;

[0074] Step 203, taking the second feature layer as the first feature layer, returning to and executing the step of calculating the correlation score between the first target neuron in the first feature layer and each second target neuron in the second feature layer, until the first feature layer is the input layer of the code detection model.

[0075] The code detection model includes multiple feature layers, each of which includes multiple neurons. The prediction results of the code detection model are back-propagated, that is, starting from the output layer of the prediction results, in the back-propagation direction, the first feature layer is determined in order from each feature layer of the code detection model, and then the correlation score between the first target neuron in the first feature layer and each second target neuron in the second feature layer is calculated, and the correlation score is used to characterize the correlation between the first target neuron and the second target neuron. Based on the correlation score between any neuron in the first feature layer and each neuron in the second feature layer, the correlation score matrix between the first feature layer and the second feature layer is obtained. Among them, the first target neuron is any neuron in the first feature layer, the second target neuron is any neuron in the second feature layer, and the second feature layer is a feature layer in the code detection model adjacent to the second feature layer in the back-propagation direction, that is, in the back-propagation direction of the code detection model, the second feature layer is the next layer of the first feature layer, and in the forward propagation direction of the code detection model, the second feature layer is the previous layer of the first feature layer.

[0076] The second feature layer is taken as the first feature layer, that is, the next layer of the current first feature layer in the back propagation direction of the model is taken as the new first feature layer, and the above steps of calculating the correlation scores of the first target neurons in the first feature layer and each second target neuron in the second feature layer are repeated until the first feature layer is the input layer of the code detection model, that is, the last feature layer of the code detection model, and the correlation score matrix between any two adjacent feature layers of the code detection model is obtained.

[0077] For example, refer to Figure 2 The LRP technology shown in FIG. 1 is a flowchart for explaining the prediction process of the code detection model. Figure 2 In , the grayscale image corresponding to the source code is input as the input image to the pre-trained code detection model to obtain the corresponding prediction result, which is back-propagated to calculate the correlation score matrix between two adjacent feature layers in the code detection model layer by layer, and finally the correlation score image is obtained as the output image. Figure 2 In it, R is the correlation score of propagation, j and k are two consecutive layers of neural network.

[0078] Preferably, in step 302, calculating the correlation score between the first target neuron in the first feature layer and each second target neuron in the second feature layer may further include:

[0079] Step 3021, obtaining a first output value of a first target neuron in the first feature layer, a first parameter matrix of the first feature layer, and a parameter matrix of the second feature layer; the first parameter matrix includes weight values ​​of neurons in the first feature layer, and the second parameter matrix includes weight values ​​of each neuron in the second feature layer;

[0080] Step 3022: Calculate the product of the first output value and the weight value of each second target neuron in the second feature layer based on the second parameter matrix to obtain a first eigenvalue;

[0081] Step 3023: Calculate the weighted sum of the output values ​​of each neuron in the first feature layer based on the first parameter matrix to obtain a second eigenvalue;

[0082] Step 3024: Determine the correlation score between the first target neuron and each of the second target neurons according to the ratio of the first eigenvalue to the second eigenvalue.

[0083] When calculating the correlation score between the first target neuron and the second target neuron, first obtain the output value of the first target neuron in the first feature layer, that is, the first output value, and obtain the first parameter matrix of the first feature layer and the second parameter matrix of the second feature layer. The first parameter matrix is ​​composed of the weight values ​​of each neuron in the first feature layer. Similarly, the second parameter matrix is ​​composed of the weight values ​​of each neuron in the second feature layer.

[0084] To obtain the output value of the neuron and the parameter matrix of the feature layer, when the code detection model is forward propagated, the output value of each neuron in each feature layer during forward propagation is saved, and the weight value of each neuron in each feature layer is extracted to form the parameter matrix of the feature layer.

[0085] Based on the second parameter matrix, the product of the output value of the first target neuron and the weight value of each neuron in the second feature layer is calculated to obtain the first eigenvalue. Based on the first parameter matrix, the weighted sum of the output values ​​of each neuron in the first feature layer is calculated to obtain the second eigenvalue. According to the ratio of the first eigenvalue to the second eigenvalue, the correlation score between the first target neuron and each second target neuron in the second feature layer is determined. For each neuron in the first feature layer, the correlation score between each neuron and any neuron in the previous feature layer is calculated. The specific calculation method is the ratio of the product of the output value of the neuron and the weight of any neuron in the second feature layer to the weighted sum of the output value of the neuron. The calculation direction is the back propagation direction of the code detection model, starting from the output layer and ending at the input layer. The correlation score matrix between each feature layer and the adjacent feature layer is calculated layer by layer to determine the part of the input grayscale image that is mainly related to the prediction result of the code detection model.

[0086] Furthermore, in step 400, detecting malicious code in the source code according to the obtained correlation score matrix may also include:

[0087] Step 401, normalizing the correlation score matrix, and constructing a correlation score map corresponding to the grayscale image based on the normalized correlation score matrix;

[0088] Step 402, determining the target area in the grayscale image that has the highest contribution to the prediction result according to the correlation score map;

[0089] Step 403: index the source code according to the target area to determine malicious code in the source code.

[0090] When detecting malicious code in the source code based on the obtained correlation score matrix, the correlation score matrix is ​​first standardized, and a correlation score map corresponding to the grayscale image is constructed based on the standardized correlation score matrix. The correlation score map can be a heat map corresponding to the grayscale image. In the correlation score map, different marking methods are used to mark areas with different correlations. According to the correlation score map, the part of the grayscale image with a high correlation to the prediction result can be intuitively determined, that is, the target area with the highest contribution to the prediction result. The source code is indexed according to the area with high correlation, the malicious code in the source code is determined, and the detection result of the malicious code is obtained.

[0091] Preferably, in step 400, after detecting the source code in the source code according to the correlation score matrix, the following may also be included:

[0092] Step 501, determining the target category of the grayscale image according to the prediction result;

[0093] Step 502, obtaining a template image corresponding to the target category and a correlation score matrix corresponding to the template image; the template image is a sample image with the highest prediction probability under the target category selected from the training samples based on the training result of the code detection model;

[0094] Step 503, calculating the Hessian vector product between the correlation score matrix corresponding to the grayscale image and the correlation score matrix of the template image to determine the similarity between the grayscale image and the template image;

[0095] Step 504 , marking the first comparison area in the grayscale image to obtain comparison interpretation information of the grayscale image; wherein the similarity between the first comparison area and the second comparison area in the template image is higher than a preset threshold.

[0096] After obtaining the correlation score map, the detection results of malicious code can also be interpreted by image classification comparison based on the correlation score map. Specifically, first determine the target category corresponding to the grayscale image according to the prediction result, then obtain the template image corresponding to the target category and the correlation score matrix corresponding to the template image, wherein the template image is a sample image with the highest prediction probability under the target category selected from the training samples based on the training results of the code detection model. Calculate the Hessian vector product between the correlation score matrix corresponding to the grayscale image and the correlation score matrix corresponding to the template image to obtain the similarity between the grayscale image and the template image, mark the area in the grayscale image where the similarity is higher than the preset threshold, and obtain the comparison interpretation information of the grayscale image. The marking method includes but is not limited to connecting the areas in the grayscale image and the template image where the similarity is higher than the preset threshold, and using different line segments to distinguish different similarities. For example, the higher the similarity, the thicker the line segment used for connection or the darker the color.

[0097] Preferably, refer to Figure 3 The schematic diagram of the comparative interpretation process shown in FIG. 1 is a schematic diagram of the comparative interpretation process, wherein the correlation score map corresponding to the grayscale image of the source code and the obtained template image are respectively used as the input images x and x', φ1 to φ L is the eigenvector corresponding to the input image, LRP is the correlation score matrix corresponding to the input image, It means calculating the Hessian vector product of the correlation score matrix of the input images, and obtaining the BiLRP (Bilinearity LRP) value corresponding to the similarity of the two input images, so as to compare and interpret the correlation score map.

[0098] Preferably, in step 100, the source code is formatted to obtain a grayscale image corresponding to the source code, which specifically includes:

[0099] Step 101, decompile the source code to generate a binary file corresponding to the source code;

[0100] Step 102, extracting the binary file, and converting the extracted binary data into decimal data in groups of preset bytes;

[0101] Step 103, based on the number of bytes corresponding to the preset width, convert the one-dimensional vector corresponding to the decimal data into a two-dimensional matrix;

[0102] Step 104: Generate a grayscale image corresponding to the source code based on the two-dimensional matrix.

[0103] When converting the source code format, the source code is first decompiled, the source code is converted into a binary file, the binary file is extracted, and the extracted binary data is converted into decimal data with a preset number of bytes, such as 8 bits as a group. Based on the number of bytes corresponding to the preset width, for example, with a fixed width of 256 bytes, the one-dimensional vector corresponding to the decimal data is converted into a two-dimensional matrix, and a grayscale image corresponding to the source code is generated based on the two-dimensional matrix.

[0104] Reference Figure 4 The schematic diagram of the source code format conversion process shown in the figure decompiles the source code, converts the file corresponding to the source code into a binary file, and then converts the binary file into decimal data, thereby generating a grayscale image.

[0105] Preferably, in one embodiment, the malicious code detection method provided in the embodiment of the present application further includes a training phase for the code detection model, specifically, including the following steps:

[0106] Step 001, obtaining a sample code file; the sample code file includes a malicious code file, the source code in the malicious code file includes a known malicious code; the sample code file includes label data, the label data is used to characterize the category of the sample code file;

[0107] Step 002, generating a hash value of the sample code file, building a code file library based on the code file and the hash value, and building a tag database based on the hash value and tag data of the sample code file;

[0108] Step 003, converting the format of the sample code file based on the sample code file library to obtain a sample grayscale image corresponding to the sample code file;

[0109] Step 004, constructing a sample data set based on the sample grayscale image and the label database, and iteratively training a preset basic detection model using the sample data set to obtain a first detection model;

[0110] Step 005: re-parameterize the first detection model to obtain a pre-trained code detection model.

[0111] A sample code file is obtained, wherein the sample code file includes a malicious code file and a normal code file, wherein the malicious code file contains known malicious code, and the obtained sample code file contains label data, and the label data is used to characterize the category of the sample code file, that is, whether the sample code file is a malicious code file, and the classification of the malicious code.

[0112] Generate a hash value of the sample code file. The hash value is a hash value. The hash value can be mapped and generated by a preset mapping engine, and a corresponding hash function is set in the mapping engine. Based on the sample code file and its hash value, a code file library is constructed, and a tag database is constructed based on the hash value of the sample code file and its label data. Based on the constructed code file library, the sample code file is formatted and a sample grayscale image corresponding to the sample code file is obtained. The specific format conversion method is the same as the source code, which will not be repeated here.

[0113] Based on the generated sample grayscale image and the constructed labeling database, a sample data set is constructed, and the preset basic detection model is iteratively trained using the constructed sample data set to obtain a first detection model, and the first detection model is re-parameterized to obtain a pre-trained code detection model.

[0114] Preferably, the preset basic detection model takes VGG19 as an example, and the VGG19 model is trained based on the constructed sample data set, that is, the sample data set constructed based on the sample grayscale image is trained, the loss is calculated using the cross entropy loss function, the gradient is calculated using the Adam algorithm, and the trained VGG19 model is saved. The re-parameterized trained VGG19 model is obtained to obtain a single-chain derivation model, that is, the trained VGG19 model is re-parameterized, and the parameters of the multi-branch structure in each backbone network are scaled and merged, so that the inference model is converted into a single-chain propagation method to obtain a code detection model.

[0115] Optionally, refer to Figure 5 , Figure 5 Another flowchart of the malicious code detection method provided in the embodiment of the present application is based on Figure 5 ,When detecting malicious codes, it mainly includes the model building stage and the ,model application stage.

[0116] Specifically, in the model building phase, a malicious code library is first built: the currently known malicious codes are sorted, and at the same time, the same number of normal codes as malicious codes are added according to a preset ratio, such as a 1:1 ratio. After being marked, they are stored in a preset file database for building a training data set for the code detection model.

[0117] Then the code file is converted to generate the corresponding grayscale image: decompile the code files in the code file library to generate binary files, perform binary encoding on all the code files in the code file library, generate binary files and save them; convert the binary files into decimal data, extract the binary files from the file database, generate decimal files in groups of 8 bits, and store them in the corresponding file database again; generate grayscale images from decimal files, extract decimal files from the file database, and convert them into a two-dimensional matrix with a fixed width of 256 bytes to form a grayscale image, take out the label data of the code file, and form a mapping relationship between the grayscale image and the label data.

[0118] Train VGG19: Build a sample data set based on grayscale images for model training, use the cross entropy loss function to calculate the loss, use the Adam algorithm for gradient calculation, and save the trained model.

[0119] Re-parameterize the trained VGG19 model to obtain a single-chain inference model: re-parameterize the trained VGG19 model, scale and merge the parameters of the multi-branch structure in each backbone network, and transform the inference model into a single-chain propagation mode.

[0120] In the model application stage, the LRP technology is used to explain the reasoning process of image classification: the LRP explanatory technology is used to provide a process explanation for the classification prediction results of the grayscale image to be predicted. Specifically, the trained re-parameterized model is first used for forward propagation to obtain the predicted value of the grayscale image to be predicted, and all intermediate values ​​in the prediction process are retained. Then the first prediction result is back-propagated from back to front, and the relevance score (RelevanceScore) of each feature layer is calculated layer by layer, and finally transmitted to the input layer. The relevance score Relevance Score corresponding to the input layer can be used to explain the prediction results of the code detection model. For example, a heat map corresponding to the grayscale image to be predicted is drawn to show the key parts of the model prediction basis.

[0121] Finally, the BiLRP technology is used to provide a comparative explanation of the classification of the grayscale image to be predicted: using the BiLRP explanatory technology, in the model training stage, one or more grayscale images of malicious code of known categories are extracted from each category as template images under this category. After each template image is analyzed using the LRP technology, in the model application stage, the similarity between the relevance score Relevance Score of the template image and the relevance score Relevance Score of the grayscale image to be predicted is calculated by polling or traversal, and the part whose similarity with the template image exceeds the threshold is the comparative explanation of the grayscale image to be predicted.

[0122] Further, refer to Figure 6 , Figure 6 Another flowchart of the malicious code detection method provided in the embodiment of the present application is based on Figure 6 When building a malicious code library, the malicious code files corresponding to known malicious codes can be obtained by collecting from open source malicious code public libraries. In the open source code library, malicious code data sets that are spread through Web applications or browsers are collected, and each malicious code is labeled with a type, such as backdoors, botnets, downloaders, spyware, launchers, kernel kits, worms or computer viruses. The number of training samples of malicious codes can also be expanded through network intrusion code samples and their labels in actual production environments to make them more practical. The normal code files corresponding to normal codes can be obtained by collecting in the actual production process. The obtained sample code files are mapped to generate code file hash values, and a code file library and a tag database are constructed through a mapping engine to construct a training data set for the model. A hash function is introduced in the mapping engine to generate a hash value for the obtained code file as the unique index number of the code file in the file database, and the code file preservation link and its hash value are stored in the code file library, and the category of the code file and its hash value are stored in the tag database.

[0123] Furthermore, all code files are extracted from the code file library, and the code files are converted into binary, and the obtained binary files are saved. Binary files are extracted, and a group of 8 bits is used as a group. A group of binary data is converted into a decimal data (byte), thereby converting all binary files into decimal files. The fixed width is 256 bytes, and the one-dimensional vector corresponding to the decimal data is converted into a two-dimensional matrix, thereby generating a grayscale image.

[0124] A sample data set is constructed based on the generated grayscale image, and the VGG19 model is trained. Preferably, in this embodiment, the backbone part of the basic VGG19 model structure is adjusted. Specifically, a parallel feature extraction network is added to the backbone part of the model during the training phase: in addition to the 3×3 convolution kernel and batch standard layer, each convolution layer also has a 1×1 convolution kernel and a batch standard layer, as well as a parallel computing network with no convolution kernel and only a batch standard layer, so as to improve the extraction amount and abstraction level of multi-dimensional features.

[0125] After the VGG19 model training is completed, the trained VGG19 model is reparameterized, mainly for the parallel convolution layers of each backbone structure. For example, the parameters of the 3×3 convolution kernel are kept unchanged, and the parameters of the 1×1 convolution kernel are filled with a layer of 0. For the layer without convolution kernel, 4 additional 3×3 convolution kernel parameter matrices are generated. In the first convolution kernel parameter matrix of the first channel, the position (1,1) is 1, and the other positions are 0. The same is true for the second convolution kernel of the second channel. The other convolution kernel parameter matrices are all zero matrices. Based on this, according to the calculation formula of the convolution kernel, the parameter matrices of the three parallel convolution kernels can be added to obtain a 3×3 convolution kernel layer. Because the batch standard layer only serves to unify the scattered data and has no parameters, there is no need to reparameterize it. As a result, a single-chain derivation model can be obtained.

[0126] In the model application stage, the source code to be detected is obtained, and the grayscale image corresponding to the source code is generated through format conversion such as decompilation. The grayscale image is input into the re-parameterized model for prediction to obtain the corresponding prediction result. The output of each convolution layer during forward propagation is saved, that is, the output value of each neuron is retained, and the weight value of each neuron is extracted to obtain the parameter matrix of the convolution kernel. The correlation score between each neuron and any neuron in the previous layer is calculated by multiplying its output value by the weight of any neuron to the ratio of the weighted sum of its output value. The calculation direction is the reverse propagation direction of the code detection model until the correlation score matrix of the input layer is calculated. After the correlation score matrix of the input layer is standardized, it is visualized by a heat map. The place with a higher "temperature" in the heat map is the main basis for the model to derive the prediction result. According to the index of the basis part, the malicious operation part in the source code, that is, the malicious code, can be found through data scaling.

[0127] Furthermore, based on the correlation score map calculated by the LRP interpretation technology, in the model training stage, the trained re-parameterized VGG19 model is used to calculate the sample grayscale image with the highest prediction probability belonging to each category in the training data set as the template image of the category, and the correlation score matrix of each template image is calculated by the LRP interpretation technology. Finally, in the model application stage, the BiLRP value between the grayscale image to be predicted and each template image is polled. The calculation method is to calculate the Hessian Vector Product between the correlation score matrix of the grayscale image to be predicted and the template image to obtain the similarity between the grayscale image to be predicted and the template image as its BiLRP value, and connect the places with higher similarity scores. The darker the color of the connecting line segment, or the thicker the line segment, the greater the similarity between the grayscale image to be predicted and the template image.

[0128] In the existing technical solutions, on the one hand, when binary decompilation is mapped to decimal data to generate a grayscale image, the obtained grayscale image can only simply show the internal structure of the file, resulting in limited image features that the model can extract, which is not conducive to the reasoning of the types of malicious code, making the model limited. On the other hand, the internal information compression part is not effectively amplified or expanded. When abstracting features, some features are easily ignored in the abstraction process. At the same time, the encrypted information is not decrypted. Like the internal compressed information, it is easy to be lost during feature extraction, resulting in a "feature blind spot". In addition, the training depth of the deep learning model is prone to cause problems such as excessive memory access cost and excessive computational parallelism. Specifically, due to the high training depth of the deep learning model during the training process, the multi-branch training method in the model will be accompanied by multiple data accesses, resulting in excessive memory access cost (MAC), which cannot meet the timeliness requirements of detection. Furthermore, the multi-branch training method in the model makes the parallel training amount in each layer training 3 to 4 times that of the single-chain model, which not only makes the memory of the running device less than 100% utilization, but also causes the waiting time to be too long during the process of waiting for the polynomial feature fusion calculation, thereby increasing the detection time. The deep learning model has a "black box" nature, which only provides prediction results, but cannot provide the basis for its prediction results, and cannot locate the key operation codes in the malicious code based on its prediction results.

[0129] In this embodiment, the malicious code detection method based on interpretable deep learning is to conduct model training by establishing a code file library and a tag database to construct the training data of the code detection model, and re-parameterize the trained model to convert it into a single-chain derivation model; finally, by adopting the LRP interpretation technology and the BiLRP interpretation technology, the process explanation and the comparison explanation of the prediction results are provided. On the one hand, an interpretable technology for model derivation and prediction result comparison is provided to eliminate the "black box" of the model, highlight the key determining factors of the prediction results on the heat map, and map the key parts of the prediction results and the prediction basis data of the model, providing researchers with two explanations of the prediction results of the model, which is conducive to the location analysis of malicious code and finding the key operation codes in the malicious code. On the other hand, the backbone layer in the deep learning model is reparameterized by using a reparameterization method. In the training phase, a multi-branch method is used to perform multi-dimensional abstraction of features. In the reasoning phase, the model is reparameterized to make it a chain structure, reducing the amount of access to memory data, thereby reducing the parallel computing degree of each network, speeding up the derivation of the model, and improving memory utilization, thereby improving the accuracy of model predictions and the timeliness of detection.

[0130] The malicious code detection device provided in the embodiment of the present application is described below. The malicious code detection device described below and the malicious code detection method described above can be referenced to each other.

[0131] Reference Figure 7 , the malicious code detection device provided in the embodiment of the present application includes:

[0132] The conversion module 10 is used to obtain the source code to be detected, and perform format conversion on the source code to obtain a grayscale image corresponding to the source code;

[0133] A prediction module 20, configured to input the grayscale image into a pre-trained code detection model to obtain a prediction result of the code detection model on the grayscale image;

[0134] An interpretation module 30, configured to perform back propagation on the prediction result and calculate a correlation score matrix between any two adjacent feature layers in the code detection model;

[0135] The detection module 40 is used to detect malicious code in the source code according to the correlation score matrix.

[0136] In one embodiment, the interpretation module 20 is further configured to:

[0137] Back-propagating the prediction result, and determining a first feature layer in order from each feature layer of the code detection model in the back-propagation direction;

[0138] Calculating correlation scores of first target neurons in the first feature layer and second target neurons in the second feature layer, and obtaining a correlation score matrix between the first feature layer and the second feature layer according to the correlation scores;

[0139] The first target neuron is any one in the first feature layer, and the second target neuron is any one in the second feature layer; the second feature layer is adjacent to the first feature layer, and in the reverse propagation direction, the second feature layer is the next layer of the first feature layer;

[0140] The second feature layer is used as the first feature layer, and the step of calculating the correlation score between the first target neuron in the first feature layer and each second target neuron in the second feature layer is returned and executed until the first feature layer is the input layer of the code detection model.

[0141] In one implementation, the interpretation module 20 is further configured to:

[0142] Acquire a first output value of a first target neuron in the first feature layer, a first parameter matrix of the first feature layer, and a parameter matrix of the second feature layer; the first parameter matrix includes weight values ​​of neurons in the first feature layer, and the second parameter matrix includes weight values ​​of each neuron in the second feature layer;

[0143] Based on the second parameter matrix, calculating the product of the first output value and the weight value of each second target neuron in the second feature layer to obtain a first eigenvalue;

[0144] Based on the first parameter matrix, calculating the weighted sum of the output values ​​of each neuron in the first feature layer to obtain a second eigenvalue;

[0145] A correlation score between the first target neuron and each of the second target neurons is determined according to a ratio of the first eigenvalue to the second eigenvalue.

[0146] In one embodiment, the detection module 40 is further configured to:

[0147] Standardizing the correlation score matrix, and constructing a correlation score map corresponding to the grayscale image based on the standardized correlation score matrix;

[0148] Determine, according to the correlation score map, the target area in the grayscale image that has the highest contribution to the prediction result;

[0149] The source code is indexed according to the target area to determine malicious code in the source code.

[0150] In one embodiment, the interpretation module 40 is further configured to:

[0151] Determining a target category of the grayscale image according to the prediction result;

[0152] Acquire a template image corresponding to the target category and a correlation score matrix corresponding to the template image; the template image is a sample image with the highest prediction probability under the target category selected from the training samples based on the training results of the code detection model;

[0153] Calculating the Hessian vector product between the correlation score matrix corresponding to the grayscale image and the correlation score matrix of the template image to determine the similarity between the grayscale image and the template image;

[0154] A first comparison area in the grayscale image is marked to obtain comparison interpretation information of the grayscale image; wherein the similarity between the first comparison area and the second comparison area in the template image is higher than a preset threshold.

[0155] In one embodiment, the conversion module 10 is further used for:

[0156] Decompiling the source code to generate a binary file corresponding to the source code;

[0157] Extracting the binary file, and converting the extracted binary data into decimal data in groups of a preset number of bytes;

[0158] Based on the number of bytes corresponding to the preset width, converting the one-dimensional vector corresponding to the decimal data into a two-dimensional matrix;

[0159] A grayscale image corresponding to the source code is generated based on the two-dimensional matrix.

[0160] In one embodiment, the malicious code detection device further includes a pre-training module for:

[0161] Acquire a sample code file; the sample code file includes a malicious code file, the source code in the malicious code file includes known malicious code; the sample code file includes label data, the label data is used to characterize the category of the sample code file;

[0162] Generate a hash value of the sample code file, build a code file library based on the sample code file and the hash value, and build a tag database based on the hash value and tag data of the sample code file;

[0163] Performing format conversion on the sample code file based on the code file library to obtain a sample grayscale image corresponding to the sample code file;

[0164] Constructing a sample data set based on the sample grayscale image and the label database, and iteratively training a preset basic detection model using the sample data set to obtain a first detection model;

[0165] The first detection model is re-parameterized to obtain a pre-trained code detection model.

[0166] Figure 8 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 8 As shown, the electronic device may include: a processor 810, a communication interface 820, a memory 830 and a communication bus 840, wherein the processor 810, the communication interface 820, and the memory 830 communicate with each other through the communication bus 840. The processor 810 may call a computer program in the memory 830 to execute the steps of the malicious code detection method, for example including:

[0167] Acquire the source code to be detected, and convert the format of the source code to obtain a grayscale image corresponding to the source code;

[0168] Inputting the grayscale image into a pre-trained code detection model to obtain a prediction result of the code detection model on the grayscale image;

[0169] Back-propagating the prediction results, and calculating the correlation score matrix between any two adjacent feature layers in the code detection model;

[0170] Malicious code in the source code is detected according to the correlation score matrix.

[0171] In addition, the logic instructions in the above-mentioned memory 830 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art, and the computer software product is stored in a storage medium, including several instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a disk or an optical disk.

[0172] On the other hand, an embodiment of the present application further provides a computer program product, the computer program product including a computer program, the computer program can be stored on a non-transitory computer-readable storage medium, and when the computer program is executed by a processor, the computer can perform the steps of the malicious code detection method provided in the above embodiments, for example, including:

[0173] Acquire the source code to be detected, and convert the format of the source code to obtain a grayscale image corresponding to the source code;

[0174] Inputting the grayscale image into a pre-trained code detection model to obtain a prediction result of the code detection model on the grayscale image;

[0175] Back-propagating the prediction results, and calculating the correlation score matrix between any two adjacent feature layers in the code detection model;

[0176] Malicious code in the source code is detected according to the correlation score matrix.

[0177] On the other hand, an embodiment of the present application further provides a processor-readable storage medium, wherein the processor-readable storage medium stores a computer program, wherein the computer program is used to enable the processor to execute the steps of the malicious code detection method provided in the above embodiments, for example, including:

[0178] Acquire the source code to be detected, and convert the format of the source code to obtain a grayscale image corresponding to the source code;

[0179] Inputting the grayscale image into a pre-trained code detection model to obtain a prediction result of the code detection model on the grayscale image;

[0180] Back-propagating the prediction results, and calculating the correlation score matrix between any two adjacent feature layers in the code detection model;

[0181] Malicious code in the source code is detected according to the correlation score matrix.

[0182] The processor-readable storage medium can be any available medium or data storage device that can be accessed by the processor, including but not limited to magnetic storage (such as floppy disks, hard disks, magnetic tapes, magneto-optical disks (MO), etc.), optical storage (such as CD, DVD, BD, HVD, etc.), and semiconductor storage (such as ROM, EPROM, EEPROM, non-volatile memory (NANDFLASH), solid-state drive (SSD)), etc.

[0183] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0184] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0185] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application 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 of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.

Claims

1. A malicious code detection method, characterized in that: include: Acquire the source code to be detected, and convert the format of the source code to obtain a grayscale image corresponding to the source code; Inputting the grayscale image into a pre-trained code detection model to obtain a prediction result of the code detection model on the grayscale image; Back-propagating the prediction results, and calculating the correlation score matrix between any two adjacent feature layers in the code detection model; Malicious code in the source code is detected according to the correlation score matrix.

2. The malicious code detection method according to claim 1, characterized in that: The back propagation of the prediction result to calculate the correlation score matrix between any two adjacent feature layers in the code detection model includes: Back-propagating the prediction result, and determining a first feature layer in order from each feature layer of the code detection model in the back-propagation direction; Calculating correlation scores of first target neurons in the first feature layer and second target neurons in the second feature layer, and obtaining a correlation score matrix between the first feature layer and the second feature layer according to the correlation scores; The first target neuron is any one in the first feature layer, and the second target neuron is any one in the second feature layer; the second feature layer is adjacent to the first feature layer, and in the reverse propagation direction, the second feature layer is the next layer of the first feature layer; The second feature layer is used as the first feature layer, and the step of calculating the correlation score between the first target neuron in the first feature layer and each second target neuron in the second feature layer is returned and executed until the first feature layer is the input layer of the code detection model.

3. The malicious code detection method according to claim 2, characterized in that: The calculating the correlation score between the first target neuron in the first feature layer and each second target neuron in the second feature layer includes: Acquire a first output value of a first target neuron in the first feature layer, a first parameter matrix of the first feature layer, and a parameter matrix of the second feature layer; the first parameter matrix includes weight values ​​of neurons in the first feature layer, and the second parameter matrix includes weight values ​​of each neuron in the second feature layer; Based on the second parameter matrix, calculating the product of the first output value and the weight value of each second target neuron in the second feature layer to obtain a first eigenvalue; Based on the first parameter matrix, calculating the weighted sum of the output values ​​of each neuron in the first feature layer to obtain a second eigenvalue; A correlation score between the first target neuron and each of the second target neurons is determined according to a ratio of the first eigenvalue to the second eigenvalue.

4. The malicious code detection method according to claim 1, characterized in that: The detecting malicious code in the source code according to the correlation score matrix includes: Standardizing the correlation score matrix, and constructing a correlation score map corresponding to the grayscale image based on the standardized correlation score matrix; Determine, according to the correlation score map, the target area in the grayscale image that has the highest contribution to the prediction result; The source code is indexed according to the target area to determine malicious code in the source code.

5. The malicious code detection method according to claim 4, characterized in that: After detecting malicious code in the source code according to the correlation score matrix, the method further includes: Determining a target category of the grayscale image according to the prediction result; Acquire a template image corresponding to the target category and a correlation score matrix corresponding to the template image; the template image is a sample image with the highest prediction probability under the target category selected from the training samples based on the training results of the code detection model; Calculating the Hessian vector product between the correlation score matrix corresponding to the grayscale image and the correlation score matrix of the template image to determine the similarity between the grayscale image and the template image; A first comparison area in the grayscale image is marked to obtain comparison interpretation information of the grayscale image; wherein the similarity between the first comparison area and the second comparison area in the template image is higher than a preset threshold.

6. The malicious code detection method according to claim 1, characterized in that: The step of converting the source code into a format to obtain a grayscale image corresponding to the source code includes: Decompiling the source code to generate a binary file corresponding to the source code; Extracting the binary file, and converting the extracted binary data into decimal data in groups of a preset number of bytes; Based on the number of bytes corresponding to the preset width, converting the one-dimensional vector corresponding to the decimal data into a two-dimensional matrix; A grayscale image corresponding to the source code is generated based on the two-dimensional matrix.

7. The malicious code detection method according to claim 1, characterized in that: The malicious code detection method further includes: Acquire a sample code file; the sample code file includes a malicious code file, the source code in the malicious code file includes known malicious code; the sample code file includes label data, the label data is used to characterize the category of the sample code file; Generate a hash value of the sample code file, build a code file library based on the sample code file and the hash value, and build a tag database based on the hash value and tag data of the sample code file; Performing format conversion on the sample code file based on the code file library to obtain a sample grayscale image corresponding to the sample code file; Constructing a sample data set based on the sample grayscale image and the label database, and iteratively training a preset basic detection model using the sample data set to obtain a first detection model; The first detection model is re-parameterized to obtain a pre-trained code detection model.

8. A malicious code detection device, characterized in that: include: A conversion module is used to obtain the source code to be detected, and perform format conversion on the source code to obtain a grayscale image corresponding to the source code; A prediction module, used for inputting the grayscale image into a pre-trained code detection model to obtain a prediction result of the code detection model on the grayscale image; An interpretation module, used for back-propagating the prediction results and calculating a correlation score matrix between any two adjacent feature layers in the code detection model; A detection module is used to detect malicious code in the source code according to the correlation score matrix.

9. An electronic device comprising a processor and a memory storing a computer program, characterized in that: When the processor executes the computer program, the steps of the malicious code detection method according to any one of claims 1 to 7 are implemented.

10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the malicious code detection method according to any one of claims 1 to 7 are implemented.

Citation Information

Patent Citations

  • Convolutional neural network classification result oriented visualization algorithm

    CN109858506A

  • Method for explaining pancreatitis diagnosis model by using hierarchical correlation propagation

    CN115761364A

  • Malicious code detection method and device based on interpretable deep learning

    CN115828248A

Cited By

  • Method for preventing malicious code execution by hook kernel function in Linux kernel based on kprobe / ftrace technology

    CN121302367A