Intelligent malicious code detection method for long script file

By detecting token and opcode sequences based on the pre-trained language model, the existing WebShell detection methods lack recognition of long script files and insufficient Trojan killing ability is solved, and the detection accuracy is significantly improved degree and scope of application.

CN119026124APending Publication Date: 2024-11-26NANJING UNIV OF POSTS & TELECOMM
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202411121897.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-15
Publication Date
2024-11-26

Smart Images

  • Figure CN119026124A_ABST
    Figure CN119026124A_ABST
Patent Text Reader

Abstract

The invention discloses an intelligent malicious code detection method for a long script file. The intelligent malicious code detection method comprises the following steps: acquiring preprocessed code data of a to-be-detected file, wherein the code data comprises a token code segment sequence and an opcode operation code sequence; inputting the token code segment sequence into a code segment classification model to obtain a code segment classification result; inputting the opcode operation code sequence into an operation code classification model to obtain an operation code classification result; when the code segment classification result and the operation code classification result are not WebShell files, determining that the code data are not malicious codes; otherwise, determining that the code data are malicious codes. According to the method, the token code segment sequence and the opcode operation code sequence are detected respectively based on the code segment classification model and the operation code classification model of the pre-training language model, the application range of an existing WebShell detection method is remarkably expanded, and the detection accuracy and the capability of coping with a Trojan horse killing-free method are improved to a certain extent.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to an intelligent malicious code detection method for a long script file, belonging to the technical field of network security application. Background Art

[0002] WebShell (web backdoor) is the most commonly used tool for network attackers. It is highly concealed and harmful. Accurately detecting WebShell hidden in the server can timely discover and interrupt network attacks. Static detection of WebShell refers to the detection method of identifying WebShell files by analyzing file code segments and other features. The existing WebShell static detection solutions mainly include expert systems based on keyword matching and intelligent analysis systems based on natural language analysis. However, the former has low detection performance and can only identify existing WebShells, which is not practical in most scenarios. The latter also has some shortcomings such as difficulty in dealing with some Trojan horse anti-killing methods, small scope of application, and insufficient feature extraction. Summary of the invention

[0003] The purpose of the present invention is to overcome the shortcomings of the prior art and provide a malicious code intelligent detection method for long script files. The code segment classification model and the opcode classification model based on the pre-trained language model detect the token code segment sequence and the opcode operation code sequence respectively, which significantly expands the application scope of the existing WebShell detection method and improves the detection accuracy and the ability to deal with Trojan horse avoidance methods to a certain extent.

[0004] To achieve the above object, the present invention is implemented by adopting the following technical solutions:

[0005] The present invention discloses a malicious code intelligent detection method for long script files, comprising the following steps:

[0006] Obtaining preprocessed code data of the file to be detected, wherein the preprocessed code data includes a token code segment sequence and an opcode operation code sequence;

[0007] Input the token code segment sequence into a pre-trained code segment classification model to obtain a code segment classification result;

[0008] Inputting the opcode sequence into a pre-trained opcode classification model to obtain an opcode classification result;

[0009] In response to the code segment classification result and the operation code classification result not being a WebShell file, the code data is determined to be non-malicious code; otherwise, the code data is determined to be malicious code;

[0010] Among them, the code segment classification model includes a code segment head base learner, a code segment tail base learner, a code segment meta learner and multiple code segment middle base learners; the operation code classification model includes an operation code terminal base learner, an operation code tail base learner, an operation code meta learner and multiple operation code middle base learners.

[0011] Furthermore, the step of obtaining the preprocessed code data of the file to be detected includes the following steps:

[0012] Obtain the original code data of the file to be detected;

[0013] Perform code segmentation processing on the PHP code data in the original code data to obtain a token code segment sequence;

[0014] Compile the PHP code data in the original code data to obtain an opcode operation code sequence;

[0015] According to the token code segment sequence and the opcode operation code sequence, the preprocessed code data is obtained.

[0016] Furthermore, the classification method of the code segment classification model is as follows:

[0017] In response to the length of the token code segment sequence being less than or equal to a preset code segment length threshold, the token code segment sequence is sent to a code segment head base learner for learning and classification to obtain a corresponding code segment head base learning result, and the code segment head base learning result is used as a final code segment classification result;

[0018] In response to the length of the token code segment sequence being greater than a preset code segment length threshold, the token code segment sequence is sliced ​​to obtain a code segment head slice, a code segment tail slice, and a plurality of code segment middle slices;

[0019] Sending the code segment head slice to a code segment head base learner for learning and classification to obtain a code segment head base learning result;

[0020] The tail slice of the code segment is sent to a code segment tail base learner for learning and classification to obtain a code segment tail base learning result;

[0021] Send each code segment middle slice to the corresponding code segment middle base learner for learning and classification, and obtain multiple code segment middle base learning results;

[0022] Sending the code segment head base learning result, the code segment tail base learning result and the middle base learning results of multiple code segments into the code segment meta-learner for integrated classification to obtain the final code segment classification result;

[0023] The segmentation process of the token code segment sequence includes:

[0024] Cut the first k items of the token code segment sequence into a code segment head slice; cut the last k items of the token code segment sequence into a code segment tail slice;

[0025] Cut every k items of the token code segment sequence except the code segment head slice into a code segment middle slice, until L1 code segment middle slices are obtained; wherein, if the number of items of the token code segment sequence except the code segment head slice is less than k* L1, then fill the token code segment sequence with empty identifiers to k* L1 items, and then cut to obtain L1 code segment middle slices, where k and L1 are both positive integers.

[0026] Furthermore, the code segment head base learner, the code segment middle base learner, and the code segment tail base learner have the same structure; the code segment head base learner, the code segment middle base learner, or the code segment tail base learner includes a code segment base encoder and a code segment base decoder;

[0027] The code segment base encoder comprises:

[0028] A code segment CodeBert module is used to perform deep bidirectional word embedding processing according to the token code segment sequence or the code segment head slice or the code segment middle slice or the code segment tail slice to obtain multiple code segment word vectors;

[0029] The code segment CLS module is used to extract the first word vector in each code segment word vector to obtain the code segment global word CLS vector;

[0030] A code segment text convolutional neural network is used to extract short- and medium-range relationship features based on the code segment word vectors to obtain a short- and medium-range relationship feature vector for the code segment;

[0031] A code segment bidirectional long short-term memory network is used to perform long-range relationship feature extraction processing based on the code segment word vector to obtain a code segment long-range relationship feature vector;

[0032] The code segment encoding linear layer is used to perform fusion and dimensionality reduction processing according to the code segment global word CLS vector, the code segment medium- and short-range relationship feature vector, and the code segment long-range relationship feature vector to obtain a comprehensive feature vector of the code segment;

[0033] The code segment based decoder comprises:

[0034] The code segment decoding linear layer is used to perform dimensionality reduction processing according to the comprehensive feature vector of the code segment to obtain the code segment data to be classified;

[0035] The code segment base output layer is used to perform classification learning according to the code segment data to be classified, and obtain the code segment head base learning result or the code segment tail base learning result or the code segment middle base learning result.

[0036] Furthermore, the code segment meta-learner includes:

[0037] A code segment transformer is used to integrate and perform bidirectional deep representation processing on the base learning results of multiple code segments to obtain a global feature vector of the code segment;

[0038] The code segment linear layer is used to perform dimensionality reduction processing according to the code segment global feature vector, the code segment head base learning result and the code segment tail base learning result to obtain the code segment dimensionality reduction feature vector;

[0039] The code segment meta-output layer is used to perform classification processing according to the dimension reduction feature vector of the code segment to obtain the final code segment classification result.

[0040] Furthermore, the classification method of the operation code classification model is as follows:

[0041] In response to the length of the opcode sequence being less than or equal to a preset opcode length threshold, the opcode sequence is sent to an operation terminal part basis learner for learning and classification, and a corresponding operation terminal part basis learning result is obtained, and the operation terminal part basis learning result is used as a final opcode classification result;

[0042] In response to the length of the opcode sequence being greater than a preset opcode length threshold, the opcode sequence is sliced ​​to obtain an opcode end slice, an opcode end slice, and a plurality of opcode middle slices;

[0043] Sending the operation terminal part slices to the operation terminal part base learner for learning and classification to obtain the operation terminal part base learning results;

[0044] Sending the opcode tail slice to an opcode tail base learner for learning and classification to obtain an opcode tail base learning result;

[0045] Send each opcode middle slice to the corresponding opcode middle base learner for learning and classification, and obtain multiple opcode middle base learning results;

[0046] The operation code terminal base learning result, the operation code tail base learning result and the multiple operation code middle base learning results are sent to the operation code meta-learner for integrated classification to obtain the final operation code classification result;

[0047] The step of cutting the opcode sequence includes:

[0048] Cut the first k items of the opcode sequence into an opcode tail slice; cut the last k items of the opcode sequence into an opcode tail slice;

[0049] Cut every k items of the opcode sequence except the terminal partial slice into an opcode middle slice, until L2 opcode middle slices are obtained; wherein, if the number of items of the opcode sequence except the terminal partial slice is less than k* L2, fill the opcode sequence with empty identifiers to k* L2 items, and then cut to obtain L2 opcode middle slices, where k and L2 are both positive integers.

[0050] Furthermore, the structure of the operation code base learner, the operation code middle base learner, and the operation code tail base learner is the same; the operation code base learner, the operation code middle base learner, or the operation code tail base learner includes an operation code base encoder and an operation code base decoder;

[0051] The opcode base encoder comprises:

[0052] An opcode Bert module is used to perform deep bidirectional word embedding processing according to the opcode sequence or the end slice of the opcode or the middle slice of the opcode or the tail slice of the opcode to obtain multiple opcode word vectors;

[0053] The opcode CLS module is used to extract the first word vector in each opcode word vector to obtain the opcode global word CLS vector;

[0054] An operation code text convolutional neural network is used to extract short- and medium-range relationship features based on the operation code word vector to obtain a short- and medium-range relationship feature vector for the operation code;

[0055] An operation code bidirectional long short-term memory network is used to perform long-range relationship feature extraction processing based on the operation code word vector to obtain an operation code long-range relationship feature vector;

[0056] The operation code encoding linear layer is used to perform fusion and dimensionality reduction processing according to the operation code global word CLS vector, the operation code medium- and short-range relationship feature vector, and the operation code long-range relationship feature vector to obtain an operation code comprehensive feature vector;

[0057] The opcode base decoder comprises:

[0058] The operation code decoding linear layer is used to perform dimensionality reduction processing according to the operation code comprehensive feature vector to obtain data to be classified;

[0059] The operation code base output layer is used to perform classification learning according to the operation code to be classified data, and obtain the operation code terminal base learning result or the operation code tail base learning result or the operation code middle base learning result.

[0060] Further, the operational code element learner includes:

[0061] An operation code transformer is used to integrate and perform bidirectional deep representation processing on the base learning results of multiple operation codes to obtain the global feature vector of the operation code;

[0062] The operation code linear layer is used to perform dimensionality reduction processing according to the operation code global feature vector, the operation code terminal basis learning result and the operation code tail basis learning result to obtain the operation code dimensionality reduction feature vector;

[0063] The operation code element output layer is used to perform classification processing according to the operation code dimension reduction feature vector to obtain the final operation code classification result.

[0064] Furthermore, the training method of the code segment classification model is as follows:

[0065] Obtain a PHP code data set, wherein the PHP code data set includes a plurality of PHP code data with classification labels;

[0066] Convert each PHP code data into a token code sequence, cut the token code segment sequence to obtain a code segment head slice, a code segment tail slice and multiple code segment middle slices, and then construct a code segment head training set, a code segment tail training set and multiple code segment middle training sets;

[0067] According to the code segment head training set, a pre-built code segment head base learner is trained to obtain a trained code segment head base learner;

[0068] Initialize each code segment middle base learner according to the trained code segment head base learner; for any code segment middle training set, train the corresponding initialized code segment middle base learner to obtain a trained code segment middle base learner;

[0069] According to the code segment tail training set, a pre-constructed code segment tail base learner is trained to obtain a trained code segment tail base learner;

[0070] Based on the trained code segment head base learner, code segment tail base learner and multiple code segment middle base learners, a code segment meta-training set based on code features is obtained;

[0071] The pre-constructed code segment meta-learner is trained according to the code segment meta-training set to obtain a trained code segment meta-learner.

[0072] Furthermore, the training method of the opcode classification model is as follows:

[0073] Obtain a PHP code data set, wherein the PHP code data set includes a plurality of PHP code data with classification labels;

[0074] Convert each PHP code data into an opcode code sequence, cut the opcode sequence to obtain an operation code section slice, an operation code tail slice and multiple operation code middle slices, and then construct an operation code section training set, an operation code tail training set and multiple operation code middle training sets;

[0075] According to the operation terminal part training set, the pre-constructed operation terminal part base learner is trained to obtain a trained operation terminal part base learner;

[0076] Initialize each opcode middle base learner according to the trained opcode middle base learner; for any opcode middle training set, train the corresponding initialized opcode middle base learner to obtain a trained opcode middle base learner;

[0077] According to the opcode tail training set, a pre-constructed opcode tail base learner is trained to obtain a trained opcode tail base learner;

[0078] Based on the trained operation code end base learner, operation code tail base learner and multiple operation code middle base learners, an operation code element training set based on code features is obtained;

[0079] The pre-constructed op-code element learner is trained according to the op-code element training set to obtain a trained op-code element learner.

[0080] Compared with the prior art, the present invention has the following beneficial effects:

[0081] The intelligent malicious code detection method for long script files of the present invention performs WebShell detection on token code segment sequences and opcode operation code sequences based on preset code segment classification models and operation code classification models, significantly expanding the application scope of existing WebShell detection methods, improving detection accuracy and the ability to cope with Trojan horse anti-killing methods to a certain extent, and having good performance in the detection of short, medium and long script files.

[0082] In the design of the base learner, the present invention fully embeds the token code segment sequence / opcode operation code sequence based on the code segment CodeBert module / operation code Bert module, and adopts the text convolutional neural network and the bidirectional long short-term memory network dual channels to respectively learn the medium- and short-range relations and the longer-range relations of the output word vectors, so that the feature mining is sufficient and the classification accuracy is high.

[0083] In the design of the meta-learner, the present invention adopts an integrated learning method of stacking stacking method and designs a meta-learner based on Transformer, which greatly broadens the applicable code length range and solves the problem of insufficient capture of long-distance dependencies and decreased accuracy caused by limited input length in the WebShell classification method based on pre-trained large model. BRIEF DESCRIPTION OF THE DRAWINGS

[0084] Figure 1 It is a flowchart of the intelligent malicious code detection method for long script files provided in Example 1;

[0085] Figure 2 is a schematic diagram of the cutting process provided in Example 1;

[0086] Figure 3 is a schematic diagram of the structure of the code segment base encoder provided in Example 1;

[0087] Figure 4 is a schematic diagram of the structure of the code segment-based decoder provided in Example 1;

[0088] Figure 5 is a schematic diagram of the structure of the code segment meta-learner provided in Example 1;

[0089] Figure 6 is a schematic diagram of the structure of the operation code base encoder provided in Example 1;

[0090] Figure 7 is a schematic diagram of the structure of the operation code base decoder provided in Example 1;

[0091] Figure 8 Schematic diagram of the structure of the operation code element learner provided in Example 1. DETAILED DESCRIPTION

[0092] The present invention will be further described below in conjunction with the accompanying drawings. The following embodiments are only used to more clearly illustrate the technical solution of the present invention, and cannot be used to limit the protection scope of the present invention.

[0093] This embodiment provides a method for intelligently detecting malicious code in long script files. Figure 1 As shown, the following steps are included:

[0094] Obtain preprocessed code data of the file to be detected, the preprocessed code data includes a token code segment sequence and an opcode operation code sequence;

[0095] Input the token code segment sequence into the pre-trained code segment classification model to obtain the code segment classification result;

[0096] Input the opcode sequence into the pre-trained opcode classification model to obtain the opcode classification result;

[0097] In response to the code segment classification result and the operation code classification result not being a WebShell file, the code data is determined to be non-malicious code; otherwise, the code data is determined to be malicious code;

[0098] Among them, the code segment classification model includes a code segment head base learner, a code segment tail base learner, a code segment meta learner and multiple code segment middle base learners; the operation code classification model includes an operation code terminal base learner, an operation code tail base learner, an operation code meta learner and multiple operation code middle base learners.

[0099] The technical concept of the present invention is: based on the preset code segment classification model and operation code classification model, WebShell detection is performed on the token code sequence and the opcode operation code sequence respectively, which significantly expands the application scope of the existing WebShell detection method, improves the detection accuracy and the ability to deal with Trojan horse anti-killing methods to a certain extent, and has good performance in the detection of short, medium and long script files.

[0100] The method steps of the present invention are as follows:

[0101] Step 1: Construction and training of code segment classification model.

[0102] 1.1、Construction of code segment classification model.

[0103] Since the features at the beginning, middle and end of a token code segment sequence are often different, the present invention cuts the input token code segment sequence according to actual conditions and designs base learners at different positions accordingly.

[0104] Therefore, the code segment classification model in the present invention includes a code segment head base learner, a code segment tail base learner, a code segment meta learner and multiple code segment middle base learners. Among them, the code segment head base learner, the code segment middle base learner and the code segment tail base learner have the same structure; the code segment head base learner or the code segment middle base learner or the code segment tail base learner includes a code segment base encoder and a code segment base decoder.

[0105] (1) Code segment base encoder.

[0106] like Figure 3 As shown, the code segment base encoder includes:

[0107] The code segment CodeBert module is used to perform deep bidirectional word embedding processing according to the token code segment sequence or the code segment head slice or the code segment middle slice or the code segment tail slice to obtain multiple code segment word vectors;

[0108] The code segment CLS module is used to extract the first word vector in each code segment word vector to obtain the code segment global word CLS vector;

[0109] The code segment text convolutional neural network is used to extract short- and medium-range relationship features based on the code segment word vectors to obtain the short- and medium-range relationship feature vectors of the code segment;

[0110] The code segment bidirectional long short-term memory network is used to extract long-range relationship features based on the code segment word vector to obtain the code segment long-range relationship feature vector;

[0111] The code segment encoding linear layer is used to fuse and reduce the dimension of the code segment global word CLS vector, the code segment medium- and short-range relationship feature vector, and the code segment long-range relationship feature vector to obtain a comprehensive feature vector of the code segment.

[0112] In the structure of the code segment base encoder, the code segment CodeBert module is used to perform word embedding operations on the input sequence; the code segment text convolutional neural network uses the textCNN network, and the code segment bidirectional long short-term memory network uses the Bi-LSTM network, which are used for further feature extraction. The former focuses on local features within a smaller range, and the latter can capture more global dependencies over longer distances. The input sequence can be represented as a one-dimensional vector, which is convenient for downstream integration and classification tasks.

[0113] (2) Code segment based decoder.

[0114] like Figure 4 As shown, the code segment base decoder includes:

[0115] The code segment decoding linear layer is used to perform dimensionality reduction processing based on the comprehensive feature vector of the code segment to obtain the code segment data to be classified;

[0116] The code segment base output layer is used to perform classification learning according to the code segment data to be classified, and obtain the code segment head base learning result, the code segment tail base learning result, or the code segment middle base learning result.

[0117] (3) Code snippet meta-learner.

[0118] In order to integrate the features of the comprehensive feature vectors of multiple code segments, the present invention adopts a stacking ensemble learning method, such as Figure 5 As shown, the code snippet meta-learner includes:

[0119] A code segment transformer is used to integrate and perform bidirectional deep representation processing on the base learning results of multiple code segments to obtain a global feature vector of the code segment;

[0120] The code segment linear layer is used to perform dimensionality reduction processing according to the global feature vector of the code segment, the base learning result of the head of the code segment, and the base learning result of the tail of the code segment to obtain a reduced-dimensional feature vector of the code segment;

[0121] The code segment meta-output layer is used to perform classification processing based on the reduced-dimensional feature vector of the code segment to obtain the final code segment classification result.

[0122] The code segment transformer in this embodiment uses the Transformer architecture to capture the temporal relationship between the features learned by several base learners. Through the self-attention mechanism, it can simultaneously focus on all positions in the sequence, thereby capturing global information and long-term dependencies. For code data sets with greatly different lengths, the meta-learner will receive a large amount of padding input, and the mask of the code segment transformer can minimize the impact of the padding input on the output. The output of the code segment transformer is connected in parallel with the output vectors of the code segment head base learner and the code segment tail base learner through a linear layer and then a softmax operation is performed to output the final code segment classification result.

[0123] 1.2. Training of code segment classification model.

[0124] The code snippet classification model is trained as follows:

[0125] (1) Get the PHP code dataset. The PHP code dataset includes multiple PHP code data with classification labels. The expression is as follows:

[0126]

[0127] Where S represents the PHP code dataset; (code n ,opcoden ,label n ) represents the nth PHP code data; code n Indicates the nth token code sequence; opcode n Indicates the nth opcode operation code sequence; label n Indicates the data label corresponding to the nth token code sequence or opcode operation code sequence. The value is 1 for WebShell files and 0 for other files.

[0128] (2) Convert each PHP code data into a token code sequence, and cut the token code segment sequence, such as Figure 2 As shown, including:

[0129] Cut the first k items of the token code segment sequence into a code segment head slice; cut the last k items of the token code segment sequence into a code segment tail slice;

[0130] Cut every k items of the token code segment sequence except the code segment head slice into a code segment middle slice, until L1 code segment middle slices are obtained; if the number of items of the token code segment sequence except the code segment head slice is less than k* L1, then fill the token code segment sequence with empty identifiers to k* L1 items, and then cut to obtain L1 code segment middle slices, where k and L1 are both positive integers.

[0131] In this embodiment, the preset code segment length threshold is 512. Considering that the input of the code segment CodeBert module is 512, excluding the start identifier CLS and the end identifier SEP, there are 510 valid input bits in total, so the k value is 510.

[0132] Then, we obtain the code segment head slice, code segment tail slice and multiple code segment middle slices, and construct the code segment head training set, code segment tail training set and multiple code segment middle training sets; the expression of each code segment slice is as follows:

[0133]

[0134] In the formula, code i Represents the i-th token code sequence; head-piece i The head slice of the code segment representing the i-th token code sequence; tail-piece i The tail slice of the code segment representing the i-th token code sequence; body-piece iL1 Represents the middle slice of the L1th code segment of the i-th token code sequence.

[0135] (3) According to the code segment head training set, the pre-constructed code segment head base learner is trained to obtain a trained code segment head base learner.

[0136] According to the trained code segment head base learner, each code segment middle base learner is initialized; that is, according to the parameters of the trained code segment head base learner, the code segment middle base learner is initialized to avoid the problem of insufficient training due to insufficient number of middle slices.

[0137] For any code segment middle training set, the corresponding initialized code segment middle base learner is trained to obtain a trained code segment middle base learner.

[0138] According to the code segment tail training set, the pre-constructed code segment tail base learner is trained to obtain a trained code segment tail base learner.

[0139] (4) Based on the trained code segment head base learner, code segment tail base learner and multiple code segment middle base learners, a code segment meta-training set based on code features is obtained, including:

[0140] The code segment head training set is input into the trained code segment head base learner, the code segment tail training set is input into the trained code segment tail base learner, and any code segment middle training set is input into the corresponding trained code segment tail base learner, and the obtained results are combined into the code segment meta-training set.

[0141] (5) According to the code segment meta-training set, the pre-constructed opcode meta-learner is trained by calculating the gradient and updating the parameters until the preset termination condition is reached. The training is completed and the trained opcode meta-learner is obtained.

[0142] Step 2: Construction and training of opcode classification model.

[0143] 2.1. Construction of operation code classification model.

[0144] Since the features at the beginning, middle and end of an opcode sequence are often different, the present invention cuts the input opcode sequence according to actual conditions and designs base learners at different positions accordingly.

[0145] Therefore, the opcode classification model in the present invention includes an opcode base learner, an opcode tail base learner, an opcode element learner, and a plurality of opcode middle base learners. Among them, the opcode base learner, the opcode middle base learner, and the opcode tail base learner have the same structure; the opcode base learner, the opcode middle base learner, or the opcode tail base learner includes an opcode base encoder and an opcode base decoder.

[0146] (1) Operation code base encoder.

[0147] like Figure 6 As shown, the opcode base encoder includes:

[0148] The opcode Bert module is used to perform deep bidirectional word embedding processing according to the opcode sequence or the end slice of the opcode or the middle slice of the opcode or the tail slice of the opcode to obtain multiple opcode word vectors;

[0149] The opcode CLS module is used to extract the first word vector in each opcode word vector to obtain the opcode global word CLS vector;

[0150] The operation code text convolutional neural network is used to extract short- and medium-range relationship features based on the operation code word vectors to obtain the operation code short- and medium-range relationship feature vectors;

[0151] The operation code bidirectional long short-term memory network is used to extract long-range relationship features based on the operation code word vector to obtain the operation code long-range relationship feature vector;

[0152] The opcode encoding linear layer is used to fuse and reduce the dimension of the opcode global word CLS vector, the opcode medium- and short-range relationship feature vector, and the opcode long-range relationship feature vector to obtain the opcode comprehensive feature vector.

[0153] In the structure of the opcode base encoder, the opcode Bert module is used to perform word embedding operations on the input sequence; the opcode text convolutional neural network uses the textCNN network, and the opcode bidirectional long short-term memory network uses the Bi-LSTM network, which are used for further feature extraction. The former focuses on local features within a smaller range, and the latter can capture more global dependencies over longer distances. The input sequence can be represented as a one-dimensional vector, which is convenient for downstream integration and classification tasks.

[0154] (2) Operation code base decoder.

[0155] like Figure 7 As shown, the opcode base decoder includes:

[0156] The opcode decoding linear layer is used to perform dimensionality reduction processing based on the opcode comprehensive feature vector to obtain the data to be classified;

[0157] The opcode base output layer is used to perform classification learning according to the opcode data to be classified, and obtain the opcode terminal base learning result, the opcode tail base learning result, or the opcode middle base learning result.

[0158] (3) Operational code element learner.

[0159] In order to integrate the features of multiple opcodes, the present invention adopts a stacking ensemble learning method, such as Figure 8 As shown, the operational code element learner includes:

[0160] An operation code transformer is used to integrate and perform bidirectional deep representation processing on the base learning results of multiple operation codes to obtain the global feature vector of the operation code;

[0161] The opcode linear layer is used to perform dimensionality reduction processing according to the opcode global feature vector, the operation terminal basis learning result and the operation code tail basis learning result to obtain the opcode reduced dimension feature vector;

[0162] The operation code element output layer is used to perform classification processing according to the operation code dimension reduction feature vector to obtain the final operation code classification result.

[0163] The opcode converter in this embodiment uses the Transformer architecture to capture the temporal relationship between the features learned by several base learners. Through the self-attention mechanism, it can simultaneously focus on all positions in the sequence, thereby capturing global information and long-term dependencies. For code data sets with greatly different lengths, the meta-learner will receive a large amount of padding input, and the mask of the opcode converter can minimize the impact of the padding input on the output. The output of the opcode converter is connected in parallel with the output vectors of the opcode terminal base learner and the opcode tail base learner through a linear layer and then a softmax operation is performed to output the final opcode classification result.

[0164] 2.2. Training of opcode classification model.

[0165] The training method of the opcode classification model is as follows:

[0166] (1) Obtain a PHP code dataset, which includes multiple PHP code data with classification labels.

[0167] (2) Convert each PHP code data into an opcode code sequence, and perform segmentation processing on the opcode operation code sequence, including:

[0168] The opcode sequence is cut and processed, including:

[0169] Cut the first k items of the opcode sequence into an opcode tail slice; cut the last k items of the opcode sequence into an opcode tail slice;

[0170] Cut every k items of the opcode sequence except the terminal partial slice into an opcode middle slice, until L2 opcode middle slices are obtained; if the number of items of the opcode sequence except the terminal partial slice is less than k* L2, fill the opcode sequence with empty identifiers to k* L2 items, and then cut to obtain L2 opcode middle slices, where k and L2 are both positive integers.

[0171] In this embodiment, the preset operation code length threshold is 512. Considering that the input of the operation code Bert module is 512, excluding the start identifier CLS and the end identifier SEP, there are 510 valid input bits in total, so the k value is 510.

[0172] Then, we obtain the operation code end slice, operation code tail slice and multiple operation code middle slices, and construct the operation code end training set, operation code tail training set and multiple operation code middle training sets; the expression of each operation code slice is as follows:

[0173]

[0174] In the formula, opcode i Indicates the i-th opcode sequence; head-oppiece i Represents the tail-oppiece of the i-th opcode sequence; i opcode tail slice representing the i-th opcode sequence; body-oppiece iL2 Represents the L2th opcode middle slice of the i-th opcode sequence.

[0175] (3) According to the operation terminal training set, the pre-constructed operation terminal base learner is trained to obtain a trained operation terminal base learner;

[0176] Initialize each operation code middle base learner according to the trained operation code partial base learner; that is, initialize the operation code middle base learner according to the parameters of the trained operation code partial base learner to avoid the problem of insufficient training due to insufficient number of middle slices.

[0177] For any opcode middle training set, the corresponding initialized opcode middle base learner is trained to obtain a trained opcode middle base learner;

[0178] According to the opcode tail training set, the pre-built opcode tail base learner is trained to obtain a trained opcode tail base learner;

[0179] (4) Based on the trained operation code base learner, operation code tail base learner and multiple operation code middle base learners, obtain the operation code element training set based on code features, including:

[0180] The operation terminal part training set is input into the trained operation terminal part base learner, the operation code tail training set is input into the trained operation code tail base learner, and any operation code middle training set is input into the corresponding trained operation code tail base learner, and the obtained results are combined into the operation code element training set.

[0181] (5) According to the opcode training set, the pre-constructed opcode learner is trained to obtain a trained opcode learner.

[0182] Step 3: Testing the model.

[0183] According to the intelligent malicious code detection method for long script files of the present invention, a hierarchical collaborative WebShell detection system architecture in Windows environment is designed and implemented, and the feasibility and effectiveness of the proposed architecture and technology are verified through a series of system function tests. Test data shows that the system meets the requirements of WebShell detection, tracking, execution, third-party cloud engine collaborative detection based on integrated learning and language analysis large models in Windows environment, and realizes efficient malicious code detection and analysis services.

[0184] In order to verify the usability of the developed system, a WebShell collaborative detection system was deployed in a private Windows system computer developed by the laboratory. A large amount of test data was used for verification. The detection capability of the WebShell collaborative detection system was tested from multiple angles, including static code analysis, behavior detection, dynamic code execution, network behavior tracking, and file detection.

[0185] The main language used in system development is Python 3.8.19, and the front-end and back-end are separated. The back-end uses the Django framework and DRF development framework, the authentication service uses JWT authority authentication, and the front-end is developed using vue3. At the same time, a server monitoring panel is deployed to support real-time server resources of windows servers for status monitoring. The framework used for deep learning model training is Pytorch, the development environment is Windows, and the development tool is Pycharm 2023. The MySQL database is used as the detection history and detection file storage database.

[0186] The specific steps of static code analysis are as follows:

[0187] 3.1. Obtain the preprocessed code data of the file to be detected, where the preprocessed code data includes a token code segment sequence and an opcode operation code sequence.

[0188] Obtaining the preprocessed code data of the file to be detected includes the following steps:

[0189] Get the original code data of the file to be detected. The original code data data includes the token code sequence and the opcode operation code sequence, data={token-arr,opcode-arr}, where token-arr represents the token code sequence to be detected; opcode-arr represents the opcode operation code to be detected.

[0190] The original code data may contain comments that affect recognition and need to be cleaned as follows:

[0191] Use the tokenizer to segment the PHP code data in the original code data into code segments to obtain a token code segment sequence;

[0192] Call the Zend engine to compile the PHP code data in the original code data to obtain the opcode sequence;

[0193] According to the token code segment sequence and the opcode operation code sequence, the preprocessed code data is obtained.

[0194] 3.2. Input the token code segment sequence into the pre-trained code segment classification model to obtain the code segment classification result.

[0195] The classification method of the code segment classification model includes the following steps:

[0196] In response to the length of the token code segment sequence being less than or equal to a preset code segment length threshold, the token code segment sequence is sent to the code segment head base learner for learning and classification to obtain the corresponding code segment head base learning result, and the code segment head base learning result is used as the final code segment classification result.

[0197] In response to the length of the token code segment sequence being greater than a preset code segment length threshold, the token code segment sequence is cut to obtain a code segment head slice, a code segment tail slice, and a plurality of code segment middle slices;

[0198] Send the code segment head slice to the code segment head base learner for learning and classification to obtain the code segment head base learning result;

[0199] The tail slice of the code segment is sent to the code segment tail base learner for learning and classification to obtain the code segment tail base learning result;

[0200] Send each code segment middle slice to the corresponding code segment middle base learner for learning and classification, and obtain multiple code segment middle base learning results;

[0201] The base learning results of the code segment head, the base learning results of the code segment tail and the base learning results of the middle parts of multiple code segments are sent to the code segment meta-learner for integrated classification to obtain the final code segment classification result.

[0202] 3.3. Input the opcode sequence into the pre-trained opcode classification model to obtain the opcode classification result.

[0203] The classification method of the operation code classification model includes the following steps:

[0204] In response to the length of the opcode sequence being less than or equal to a preset opcode length threshold, the opcode sequence is sent to an operation terminal part basis learner for learning and classification, and a corresponding operation terminal part basis learning result is obtained, and the operation terminal part basis learning result is used as a final opcode classification result;

[0205] In response to the length of the opcode sequence being greater than a preset opcode length threshold, the opcode sequence is sliced ​​to obtain an opcode end slice, an opcode end slice, and a plurality of opcode middle slices;

[0206] The operation dock part slices are sent to the operation dock part base learner for learning and classification to obtain the operation dock part base learning results;

[0207] The opcode tail slice is sent to the opcode tail base learner for learning and classification to obtain the opcode tail base learning result;

[0208] Send each opcode middle slice to the corresponding opcode middle base learner for learning and classification, and obtain multiple opcode middle base learning results;

[0209] The operation code terminal base learning results, the operation code tail base learning results and the multiple operation code middle base learning results are sent to the operation code meta-learner for integrated classification to obtain the final operation code classification result.

[0210] 3.4. In response to the code segment classification result and the operation code classification result not being a WebShell file, the code data is determined to be non-malicious code; otherwise, the code data is determined to be malicious code.

[0211] It should be noted that in practice, it was found that the opcode sequences corresponding to some PHP codes were empty sequences, so it was set that only opcode sequences with a length greater than or equal to 5 were involved in decision-making.

[0212] In summary, the present invention adopts a stacking integration method for multiple base learners based on a large language model and dual-channel feature extraction, and simultaneously detects token code segment sequences and opcode operation code sequences, which significantly expands the application scope of the existing WebShell detection method, improves the detection accuracy and the ability to deal with Trojan horse anti-killing methods to a certain extent, and has good performance in the detection of short, medium and long script files, and can achieve an accuracy of 99.95%+ on the test set.

[0213] The intelligent malicious code detection method for long script files of the present invention performs WebShell detection on token code segment sequences and opcode operation code sequences based on preset code segment classification models and operation code classification models, significantly expanding the application scope of existing WebShell detection methods, improving detection accuracy and the ability to cope with Trojan horse anti-killing methods to a certain extent, and having good performance in the detection of short, medium and long script files.

[0214] In the design of the base learner, the present invention fully embeds the token code segment sequence / opcode operation code sequence based on the code segment CodeBert module / operation code Bert module, and adopts the text convolutional neural network and the bidirectional long short-term memory network dual channels to respectively learn the medium- and short-range relations and the longer-range relations of the output word vectors, so that the feature mining is sufficient and the classification accuracy is high.

[0215] In the design of the meta-learner, the present invention adopts an integrated learning method of stacking stacking method and designs a meta-learner based on Transformer, which greatly broadens the applicable code length range and solves the problem of insufficient capture of long-distance dependencies and decreased accuracy caused by limited input length in the WebShell classification method based on pre-trained large model.

[0216] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.

[0217] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0218] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0219] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0220] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principle of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.

Claims

1. A malicious code intelligent detection method for long script files, characterized in that: The steps include: Obtaining preprocessed code data of the file to be detected, wherein the preprocessed code data includes a token code segment sequence and an opcode operation code sequence; Input the token code segment sequence into a pre-trained code segment classification model to obtain a code segment classification result; Inputting the opcode sequence into a pre-trained opcode classification model to obtain an opcode classification result; In response to the code segment classification result and the operation code classification result not being a WebShell file, the code data is determined to be non-malicious code; otherwise, the code data is determined to be malicious code; Among them, the code segment classification model includes a code segment head base learner, a code segment tail base learner, a code segment meta learner and multiple code segment middle base learners; the operation code classification model includes an operation code terminal base learner, an operation code tail base learner, an operation code meta learner and multiple operation code middle base learners.

2. The method for intelligently detecting malicious code in long script files according to claim 1 is characterized in that: The step of obtaining the preprocessed code data of the file to be detected comprises the following steps: Obtain the original code data of the file to be detected; Perform code segmentation processing on the PHP code data in the original code data to obtain a token code segment sequence; Compile the PHP code data in the original code data to obtain an opcode operation code sequence; According to the token code segment sequence and the opcode operation code sequence, the preprocessed code data is obtained.

3. The intelligent malicious code detection method for long script files according to claim 1 is characterized in that: The classification method of the code segment classification model is as follows: In response to the length of the token code segment sequence being less than or equal to a preset code segment length threshold, the token code segment sequence is sent to a code segment head base learner for learning and classification to obtain a corresponding code segment head base learning result, and the code segment head base learning result is used as a final code segment classification result; In response to the length of the token code segment sequence being greater than a preset code segment length threshold, the token code segment sequence is sliced ​​to obtain a code segment head slice, a code segment tail slice, and a plurality of code segment middle slices; Sending the code segment head slice to a code segment head base learner for learning and classification to obtain a code segment head base learning result; The tail slice of the code segment is sent to a code segment tail base learner for learning and classification to obtain a code segment tail base learning result; Send each code segment middle slice to the corresponding code segment middle base learner for learning and classification, and obtain multiple code segment middle base learning results; Sending the code segment head base learning result, the code segment tail base learning result and the middle base learning results of multiple code segments into the code segment meta-learner for integrated classification to obtain the final code segment classification result; The segmentation process of the token code segment sequence includes: Cut the first k items of the token code segment sequence into a code segment head slice; cut the last k items of the token code segment sequence into a code segment tail slice; Cut every k items of the token code segment sequence except the code segment head slice into a code segment middle slice, until L1 code segment middle slices are obtained; wherein, if the number of items of the token code segment sequence except the code segment head slice is less than k* L1, then fill the token code segment sequence with empty identifiers to k* L1 items, and then cut to obtain L1 code segment middle slices, where k and L1 are both positive integers.

4. The intelligent malicious code detection method for long script files according to claim 3 is characterized in that: The code segment head base learner, the code segment middle base learner, and the code segment tail base learner have the same structure; the code segment head base learner, the code segment middle base learner, or the code segment tail base learner includes a code segment base encoder and a code segment base decoder; The code segment base encoder comprises: A code segment CodeBert module is used to perform deep bidirectional word embedding processing according to the token code segment sequence or the code segment head slice or the code segment middle slice or the code segment tail slice to obtain multiple code segment word vectors; The code segment CLS module is used to extract the first word vector in each code segment word vector to obtain the code segment global word CLS vector; A code segment text convolutional neural network is used to extract short- and medium-range relationship features based on the code segment word vectors to obtain a short- and medium-range relationship feature vector for the code segment; A code segment bidirectional long short-term memory network is used to perform long-range relationship feature extraction processing based on the code segment word vector to obtain a code segment long-range relationship feature vector; The code segment encoding linear layer is used to perform fusion and dimensionality reduction processing according to the code segment global word CLS vector, the code segment medium- and short-range relationship feature vector, and the code segment long-range relationship feature vector to obtain a comprehensive feature vector of the code segment; The code segment based decoder comprises: The code segment decoding linear layer is used to perform dimensionality reduction processing according to the comprehensive feature vector of the code segment to obtain the code segment data to be classified; The code segment base output layer is used to perform classification learning according to the code segment data to be classified, and obtain the code segment head base learning result or the code segment tail base learning result or the code segment middle base learning result.

5. The method for intelligently detecting malicious code in long script files according to claim 3 is characterized in that: The code snippet meta-learner includes: A code segment transformer is used to integrate and perform bidirectional deep representation processing on the base learning results of multiple code segments to obtain a global feature vector of the code segment; The code segment linear layer is used to perform dimensionality reduction processing according to the code segment global feature vector, the code segment head base learning result and the code segment tail base learning result to obtain the code segment dimensionality reduction feature vector; The code segment meta-output layer is used to perform classification processing according to the dimension reduction feature vector of the code segment to obtain the final code segment classification result.

6. The method for intelligently detecting malicious code in long script files according to claim 1, characterized in that: The classification method of the operation code classification model is as follows: In response to the length of the opcode sequence being less than or equal to a preset opcode length threshold, the opcode sequence is sent to an operation terminal part basis learner for learning and classification, and a corresponding operation terminal part basis learning result is obtained, and the operation terminal part basis learning result is used as a final opcode classification result; In response to the length of the opcode sequence being greater than a preset opcode length threshold, the opcode sequence is sliced ​​to obtain an opcode end slice, an opcode end slice, and a plurality of opcode middle slices; Sending the operation terminal part slices to the operation terminal part base learner for learning and classification to obtain the operation terminal part base learning results; Sending the opcode tail slice to an opcode tail base learner for learning and classification to obtain an opcode tail base learning result; Send each opcode middle slice to the corresponding opcode middle base learner for learning and classification, and obtain multiple opcode middle base learning results; The operation code terminal base learning result, the operation code tail base learning result and the multiple operation code middle base learning results are sent to the operation code meta-learner for integrated classification to obtain the final operation code classification result; The step of cutting the opcode sequence includes: Cut the first k items of the opcode sequence into an opcode tail slice; cut the last k items of the opcode sequence into an opcode tail slice; Cut every k items of the opcode sequence except the terminal partial slice into an opcode middle slice, until L2 opcode middle slices are obtained; wherein, if the number of items of the opcode sequence except the terminal partial slice is less than k* L2, fill the opcode sequence with empty identifiers to k* L2 items, and then cut to obtain L2 opcode middle slices, where k and L2 are both positive integers.

7. The method for intelligently detecting malicious code in long script files according to claim 6 is characterized in that: The structure of the operation code base learner, the operation code middle base learner and the operation code tail base learner is the same; the operation code base learner, the operation code middle base learner or the operation code tail base learner includes an operation code base encoder and an operation code base decoder; The opcode base encoder comprises: An opcode Bert module is used to perform deep bidirectional word embedding processing according to the opcode sequence or the end slice of the opcode or the middle slice of the opcode or the tail slice of the opcode to obtain multiple opcode word vectors; The opcode CLS module is used to extract the first word vector in each opcode word vector to obtain the opcode global word CLS vector; An operation code text convolutional neural network is used to extract short- and medium-range relationship features based on the operation code word vector to obtain a short- and medium-range relationship feature vector for the operation code; An operation code bidirectional long short-term memory network is used to perform long-range relationship feature extraction processing based on the operation code word vector to obtain an operation code long-range relationship feature vector; The operation code encoding linear layer is used to perform fusion and dimensionality reduction processing according to the operation code global word CLS vector, the operation code medium- and short-range relationship feature vector, and the operation code long-range relationship feature vector to obtain an operation code comprehensive feature vector; The opcode base decoder comprises: The operation code decoding linear layer is used to perform dimensionality reduction processing according to the operation code comprehensive feature vector to obtain data to be classified; The operation code base output layer is used to perform classification learning according to the operation code to be classified data, and obtain the operation code terminal base learning result or the operation code tail base learning result or the operation code middle base learning result.

8. The method for intelligently detecting malicious code in long script files according to claim 6 is characterized in that: The operational code element learner comprises: An operation code transformer is used to integrate and perform bidirectional deep representation processing on the base learning results of multiple operation codes to obtain the global feature vector of the operation code; The operation code linear layer is used to perform dimensionality reduction processing according to the operation code global feature vector, the operation code terminal basis learning result and the operation code tail basis learning result to obtain the operation code dimensionality reduction feature vector; The operation code element output layer is used to perform classification processing according to the operation code dimension reduction feature vector to obtain the final operation code classification result.

9. The method for intelligently detecting malicious code in long script files according to claim 1, characterized in that: The training method of the code segment classification model is as follows: Obtain a PHP code data set, wherein the PHP code data set includes a plurality of PHP code data with classification labels; Convert each PHP code data into a token code sequence, cut the token code segment sequence to obtain a code segment head slice, a code segment tail slice and multiple code segment middle slices, and then construct a code segment head training set, a code segment tail training set and multiple code segment middle training sets; According to the code segment head training set, a pre-built code segment head base learner is trained to obtain a trained code segment head base learner; Initialize each code segment middle base learner according to the trained code segment head base learner; for any code segment middle training set, train the corresponding initialized code segment middle base learner to obtain a trained code segment middle base learner; According to the code segment tail training set, a pre-constructed code segment tail base learner is trained to obtain a trained code segment tail base learner; Based on the trained code segment head base learner, code segment tail base learner and multiple code segment middle base learners, a code segment meta-training set based on code features is obtained; The pre-constructed code segment meta-learner is trained according to the code segment meta-training set to obtain a trained code segment meta-learner.

10. The method for intelligently detecting malicious code in long script files according to claim 1, characterized in that: The training method of the opcode classification model is as follows: Obtain a PHP code data set, wherein the PHP code data set includes a plurality of PHP code data with classification labels; Convert each PHP code data into an opcode code sequence, cut the opcode sequence to obtain an operation code section slice, an operation code tail slice and multiple operation code middle slices, and then construct an operation code section training set, an operation code tail training set and multiple operation code middle training sets; According to the operation terminal part training set, the pre-constructed operation terminal part base learner is trained to obtain a trained operation terminal part base learner; Initialize each opcode middle base learner according to the trained opcode middle base learner; for any opcode middle training set, train the corresponding initialized opcode middle base learner to obtain a trained opcode middle base learner; According to the opcode tail training set, a pre-constructed opcode tail base learner is trained to obtain a trained opcode tail base learner; Based on the trained operation code end base learner, operation code tail base learner and multiple operation code middle base learners, an operation code element training set based on code features is obtained; The pre-constructed op-code element learner is trained according to the op-code element training set to obtain a trained op-code element learner.

Citation Information

Cited By

  • Code generation method and device based on large model

    CN120929061A

  • A large model-based code generation method and device

    CN120929061B