Malicious traffic detection method based on masked autoencoder pre-training
Through the mask autoencoder pre-training method, the problem of incomplete data utilization and large data volume in malicious traffic detection is solved, the identity information of the traffic data is extracted, the identification efficiency is improved and the model migration cost is reduced, achieving the goal of detecting malicious traffic in a small amount. Efficient identification under data.
Patent Information
- Application Number
- CN202410844803.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-27
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2044-06-27
AI Technical Summary
Existing technologies have deficiencies such as imperfect data utilization, large amount of labeled data required, and long training time in malicious traffic detection, resulting in low identification efficiency.
Using a method based on mask autoencoder pre-training, the traffic data is converted into a two-dimensional representation matrix through data preprocessing, features are extracted using mask enhancement and traffic feature encoding, and comparative learning is performed through the projection module to reduce data volume requirements and Model migration issues.
It effectively extracts the identity information of traffic data, reduces the demand for data volume, improves identification efficiency, and has good transfer learning capabilities to achieve excellent identification accuracy with a small amount of data.
Smart Images

Figure CN118400195B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of cyberspace security and artificial intelligence technology, and in particular to a malicious traffic detection method based on masked autoencoder pre-training. Background Art
[0002] How to more effectively identify malicious traffic has always been the focus of researchers. Currently, the research methods for malicious traffic identification are mainly divided into three categories: rule-based methods, machine learning methods, and deep learning methods. Traffic identification methods have also experienced a development from rule-based methods to machine learning methods, deep learning methods, and then to multiple methods in parallel. Early research mainly used rule-based methods to identify malicious traffic, that is, to use the characteristics of basic attributes such as communication protocols, port numbers, and message lengths of traffic data to manually formulate security rules to check abnormal behavior. With the development of the network environment, rule-based methods have exposed defects such as high demand for expert knowledge, long time consumption, and difficulty in responding to new traffic encryption and anonymity technologies, and cannot meet the needs of security personnel for traffic analysis.
[0003] In order to adapt to the complex traffic data in the new network environment, researchers have introduced algorithms in the field of machine learning to identify the high-dimensional features of traffic data. For example, AppScanner uses random forest (RF) to analyze various statistical features (maximum and minimum values, average, variance, etc.) generated by traffic; CUMUL classifies traffic by inputting 104 statistical features calculated from the original traffic data into the support vector machine (SVM). Machine learning methods can be used to effectively analyze complex traffic data, but these methods still rely on traffic statistical features designed by experts, and the performance of features in different actual application scenarios is unstable and requires manual selection and adjustment.
[0004] Different from the above methods, the deep learning-based method does not require pre-set artificial features when analyzing traffic. Instead, it uses deep learning methods to directly perform end-to-end analysis and classification. It has become the research focus of automatically extracting traffic features and improving traffic detection effects. The applied deep learning methods include convolutional neural networks (CNN), recurrent neural networks (RNN), long short-term memory networks (LSTM), and deep learning methods. )wait.
[0005] Deep learning methods in traffic identification can ignore the complex encryption of random traffic and minimize the loss of manpower and model migration caused by the use of expert knowledge. However, such methods currently have defects such as imperfect data utilization, large amounts of labeled data required, and long training time, which hinder the practical application of these methods. Summary of the invention
[0006] The purpose of the present invention is to solve the defect that it is difficult to effectively detect malicious traffic in the prior art, and to provide a malicious traffic detection method based on masked autoencoder pre-training to solve the above problem.
[0007] In order to achieve the above object, the technical solution of the present invention is as follows:
[0008] A malicious traffic detection method based on mask autoencoder pre-training, comprising the following steps:
[0009] Data preprocessing: Classify traffic data into unlabeled data and labeled data, and preprocess the unlabeled data into a two-dimensional representation matrix;
[0010] Setting of mask autoencoder: Setting of mask autoencoder includes three parts: mask module, traffic feature encoding module and projection module;
[0011] Pre-train the masked autoencoder: Use a two-dimensional representation matrix to pre-train the masked autoencoder; use the mask module to generate random masks to construct enhanced samples, and then use the traffic feature encoding module to extract features to obtain feature vectors; then use the projection module to input the feature vectors and project them onto the unit sphere for comparative learning;
[0012] Fine-tuning of masked autoencoders: Fine-tuning the pre-trained masked autoencoders using labeled data;
[0013] Acquisition of traffic to be detected and detection of malicious traffic: The traffic to be detected is obtained to form unlabeled data to be detected, and the fine-tuned mask automatic encoder is input to obtain the detection results of malicious traffic.
[0014] The data preprocessing comprises the following steps:
[0015] Divide the original traffic data into flows according to the IP address, port number and protocol type, then delete the encapsulation information at the flow level and above, set the port number to zero, and replace each IP with a random address in the sending and receiving directions to generate traffic data;
[0016] Classify traffic data into unlabeled data and labeled data;
[0017] Preprocessing of unlabeled data:
[0018] According to the characteristics of traffic data, the representation matrix is divided into traffic header rows and load rows. The traffic header row only contains the bytes of the header part of the traffic, while the load row only contains the bytes of the load part of the traffic.
[0019] Process each traffic packet:
[0020] Preset the packet-level matrix size parameters N, H, and W, and use the first N data packets in each flow to generate a two-dimensional matrix with a fixed size of H*W as the representation matrix of the data flow;
[0021] For each flow, take the first N data packets, and generate a packet-level matrix of fixed size H * W for each data packet. This packet-level matrix consists of a header matrix and a payload matrix of fixed size and position. If the number of valid bytes or data packets in the payload matrix is insufficient, the insufficient position is filled with zeros to ensure that the size of each packet-level matrix is consistent;
[0022] The obtained N packet-level matrices are stacked vertically in sequence to form the final two-dimensional representation matrix M of the entire flow. If the flow data packet is less than N, the matrix is filled with zeros.
[0023] The pre-training of the mask autoencoder comprises the following steps:
[0024] For the two-dimensional representation matrix M, the enhanced samples are constructed by generating random masks to perform data enhancement processing:
[0025] The mask rate is preset to k, and a two-dimensional mask matrix mask with the same size as the input matrix is generated. K zeros are filled in random positions, and the remaining positions are all 1. Mask samples are generated multiple times to expand the number of representation matrices; mask samples The generation of is expressed as follows:
[0026] ,
[0027] Among them, the multiplication sign is the Hadamard product;
[0028] The traffic feature encoding module is set to include an embedding module, a packet-level extraction model and a flow-level extraction module;
[0029] The embedding module is used to embed the shielded samples Input gets its patch vector,
[0030] The packet-level extraction model is used to input the patch vector generated by the embedding module into the self-attention encoder of the packet-level extraction module to obtain packet-level features.
[0031] The stream-level extraction module is used to add stream-level features to the packet-level features output by the packet-level extraction module, pool the features of the output patches of the packet-level extraction module row by row to generate row patches, then input the row patches into the self-attention encoder to obtain column-level features, and then pool the column-level features column by column to obtain the final feature vector;
[0032] The embed module will block the sample Split into non-overlapping two-dimensional matrix blocks of fixed size, and then use the linear layer of the neural network to map these non-overlapping two-dimensional matrix blocks into D-dimensional embedded patch vectors, where D is the preset vector dimension, and finally add the non-overlapping two-dimensional blocks to the embedded patch vector xl at the position of the representation matrix to maintain the position information. The obtained embedded patch vector is divided into a header patch or a payload patch;
[0033] The self-attention encoder of the packet-level extraction module is composed of a multi-head self-attention layer MSA and a feedforward layer alternating with each other. The embedded patch vector obtained by the embedding module is input into the packet-level extraction module to extract packet-level features.
[0034] The multi-head self-attention layer MSA uses the attention function to calculate the head of each embedded patch vector, and learns the degree of association between different patches based on the head of each embedded patch vector. The feedforward layer extracts deeper features by linear transformation, first mapping to high-dimensional space and then mapping to low-dimensional space;
[0035] remember is the preset attention function output scale, W Q , W K , W V ∈ is The learned network parameters, To embed the patch vector, the attention function The calculation is as follows:
[0036]
[0037] ,
[0038] in, is the intermediate process matrix of the attention function;
[0039] The stream-level extraction module pools the embedded patch vector features output by the packet-level extraction module row by row to generate row patches;
[0040] Re-input the row patches into the self-attention encoder of the packet-level extraction module to extract column-level features of the embedded patch vector;
[0041] Finally, the stream-level extraction module performs column-by-column pooling on the obtained column-level features to obtain the final feature vector of the entire representation matrix, denoted as Lf;
[0042] Set up the projection module,
[0043] The projection module is set to include a fully connected layer consisting of two linear layers and a ReLU activation function layer, and the data is normalized before input and after output;
[0044] The projection module performs maximum and minimum normalization on the final feature vector Lf to form a standardized vector representation, and then outputs the embedded feature representation, that is, the output vector, through the fully connected layer, and then normalizes the output vector to a vector on the unit sphere;
[0045] The cosine similarity between two vectors on the unit sphere is recorded as the dot product of the two vectors, and the following loss function is defined to complete the training:
[0046] Given sample x as an anchor point, record is the augmented data from the same sample as x, is the augmented data from samples different from x. To ensure similar sample pairs (x, ) are close to each other, while different sample pairs (x, ) are far away from each other, and the contrast loss function is calculated based on the cosine similarity. Defined as:
[0047]
[0048] The anchor point r represents the sample x after being projected onto the unit sphere by the projection module. Its different subscripts represent samples from different sets. i represents samples from the entire sample set, and p represents samples from the anchor point r. i All similar sample pairs (x, ), a represents the set P(i) from the anchor point r i The set A(i) of all sample pairs is composed of , and the dot product in the loss formula is used to calculate the cosine similarity;
[0049] The contrast loss function compares the cosine similarity of positive and negative sample pairs, making the positive sample pair (x, ) The cosine similarity is the largest, the negative sample pair (x, ) with the smallest cosine similarity to identify traffic;
[0050] The projection module maps the feature vector Lf to a point on the unit sphere, and then uses the cosine similarity obtained by the vector dot product to measure the similarity between the two vectors: the higher the similarity, the smaller the distance between the feature vectors corresponding to the samples on the unit sphere;
[0051] Complete the pre-training of the mask autoencoder and save the obtained parameters.
[0052] The fine-tuning of the mask autoencoder includes the following steps:
[0053] Input the labeled data into the pre-trained mask autoencoder for fine-tuning training, passing through the mask module, traffic feature encoding module and projection module;
[0054] The last fully connected layer in the projection module of the masked autoencoder performs traffic classification, and the contrast loss function is set in the projection stage The maximum value is used to identify traffic, that is, the cosine similarity of the sample pairs with the same label is the largest and the cosine similarity of the sample pairs with different labels is the smallest.
[0055] Beneficial Effects
[0056] Compared with the prior art, the malicious traffic detection method based on masked autoencoder pre-training of the present invention adopts a more effective traffic data processing method to retain more flow information, uses masked enhanced data to significantly reduce the model's demand for data volume, and more effectively extracts data features in two dimensions, packet level and flow level, in traffic feature encoding. A contrastive learning method including masking and projection is used to fully extract the distinguishing features of different types of traffic, and a pre-training method is used to enhance the model's migration capability, thereby reducing the actual operating overhead and effectively identifying malicious traffic.
[0057] The present invention uses a pre-training method to effectively convert traffic data into a two-dimensional representation matrix and then input it into a pre-training model. It extracts features through mask enhancement and traffic feature encoding, and projects them onto a unit sphere to complete comparative learning, retaining pre-training parameters for downstream tasks. The method described in the present invention can greatly reduce the demand for data volume in traditional deep learning, effectively extract identity information from traffic data, achieve excellent recognition accuracy using a small amount of data, and have good transfer learning capabilities while improving traffic recognition efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0058] Figure 1 is a method sequence diagram of the present invention;
[0059] Figure 2 It is the overall framework diagram of the present invention;
[0060] Figure 3 Generating a graph for a two-dimensional representation matrix involved in the present invention;
[0061] Figure 4 is the mask enhancement image involved in the present invention;
[0062] Figure 5 This is an application scenario diagram of the present invention. DETAILED DESCRIPTION
[0063] In order to have a further understanding and recognition of the structural features and the effects achieved by the present invention, a preferred embodiment and accompanying drawings are used for detailed description as follows:
[0064] like Figure 1 and Figure 2As shown, the malicious traffic detection method based on masked autoencoder pre-training described in the present invention takes into account that the various traffic detection methods based on machine learning and deep learning that are currently commonly used have a huge demand for labeled data. We apply the pre-training method to traffic identification. By extracting the intrinsic information of unlabeled traffic data during pre-training, the demand for data volume in traditional deep learning is greatly reduced, and the extracted features are effective, so that traffic can be effectively identified after migration.
[0065] It includes the following steps:
[0066] The first step is data preprocessing: classify the traffic data into unlabeled data and labeled data, and preprocess the unlabeled data into a two-dimensional representation matrix.
[0067] Most existing traffic detection methods directly intercept a fixed number of bytes in the first part of the flow, form a two-dimensional matrix and input it into a deep learning network for classification. The disadvantage of this traffic processing method is that it intercepts too much payload data, and these bytes are usually highly encrypted and difficult to understand. At the same time, when the first data packet of some flows is too large, it will occupy the entire matrix, resulting in serious loss of flow-level information, affecting the overall operating efficiency and accuracy of the model. Therefore, the present invention adopts a new method to generate a multi-level flow representation using raw traffic data. The overall process is as follows: Figure 3 shown.
[0068] (1) Divide the original traffic data into streams according to the IP address, port number and protocol type, then delete the encapsulation information at the stream level and above, set the port number to zero, and replace each IP with a random address in the sending and receiving directions to generate traffic data.
[0069] (2) Classify the traffic data into unlabeled data and labeled data. In terms of data, all pre-training uses unlabeled data, and then the data in the fine-tuning stage is a small amount of labeled data, and the rest is unlabeled data to be tested. It is generally required that the data used in the fine-tuning stage and the data to be tested come from the same data set to enhance the recognition accuracy of the model.
[0070] (3) Preprocessing of unlabeled data:
[0071] A1) According to the characteristics of traffic data, the representation matrix is divided into traffic header rows and load rows. The traffic header row only contains the bytes of the traffic header part, while the load row only contains the bytes of the traffic load part;
[0072] A2) Process each traffic packet:
[0073] Preset the packet-level matrix size parameters N, H, and W, and use the first N data packets in each flow to generate a two-dimensional matrix with a fixed size of H*W as the representation matrix of the data flow;
[0074] For each flow, take the first N data packets, and generate a packet-level matrix of fixed size H * W for each data packet. This packet-level matrix consists of a header matrix and a payload matrix of fixed size and position. If the number of valid bytes or data packets in the payload matrix is insufficient, the insufficient position is filled with zeros to ensure that the size of each packet-level matrix is consistent;
[0075] A3) The obtained N packet-level matrices are stacked vertically in sequence to form the final two-dimensional representation matrix M of the entire flow. If the flow data packet is less than N, the matrix is filled with zeros. Here, the packet-level matrices need to be stacked vertically. If they are arranged horizontally, the internal characteristics of the traffic will be destroyed, resulting in reduced recognition accuracy of the traffic.
[0076] The representation matrix generated in this way has the advantage of fixed information location and can more comprehensively retain the internal information contained in the stream data compared to direct interception.
[0077] The second step is to set up the masked autoencoder: The masked autoencoder includes three parts: the mask module, the traffic feature encoding module, and the projection module. The mask module generates masked samples by using masks to enhance the data. The learning task is constructed with the masking task in the pre-training, and the amount of data is increased in the fine-tuning stage. The traffic feature encoding module is divided into an embedding module, a packet-level extraction model, and a flow-level extraction module, which can effectively extract traffic features from multiple angles. The projection module increases the distance between different types of traffic by projecting the traffic features onto the unit sphere, thereby improving the accuracy of traffic recognition.
[0078] The third step is to pre-train the mask autoencoder: use the two-dimensional representation matrix to pre-train the mask autoencoder; use the mask module to generate random masks to construct enhanced samples, and then use the traffic feature encoding module to extract features to obtain feature vectors; then use the projection module to project the feature vectors onto the unit sphere for comparative learning.
[0079] After obtaining the two-dimensional representation matrix generated by the traffic data, the present invention uses masking and projection to enhance the data to avoid the actual problem of insufficient data and improve the model effect, and finally obtains the feature vector of the traffic and completes the pre-training. The model obtained after pre-training can effectively identify the traffic, and then retains the model parameters to the migration task to improve the efficiency of practical applications.
[0080] (1) For the two-dimensional representation matrix M, an enhanced sample is constructed by generating a random mask to perform data enhancement processing:
[0081] The mask rate is preset to k, and a two-dimensional mask matrix mask with the same size as the input matrix is generated. K zeros are filled in random positions, and the remaining positions are all 1. Mask samples are generated multiple times to expand the number of representation matrices; mask samples The generation of is expressed as follows:
[0082] ,
[0083] Here, the multiplication sign is the Hadamard product.
[0084] When enhancing data, researchers usually use methods such as scaling, rotation, cropping, and adding Gaussian noise to generate sample pairs of source data to expand the number of samples. However, directly applying these methods to traffic data may cause the internal structure of traffic data to be destroyed. At the same time, the process of converting the format of traffic data and constructing enhanced sample pairs may be very time-consuming. Therefore, the present invention constructs enhanced samples by generating random masks. The transformation process is as follows: Figure 4 shown.
[0085] (2) Setting the traffic feature encoding module includes an embedding module, a packet-level extraction model, and a flow-level extraction module;
[0086] The embedding module is used to embed the shielded samples Input gets its patch vector,
[0087] The packet-level extraction model is used to input the patch vector generated by the embedding module into the self-attention encoder of the packet-level extraction module to obtain packet-level features.
[0088] The stream-level extraction module is used to add stream-level features to the packet-level features output by the packet-level extraction module, pool the features of the output patches of the packet-level extraction module row by row to generate row patches, then input the row patches into the self-attention encoder to obtain column-level features, and then pool the column-level features column by column to obtain the final feature vector;
[0089] B1) The embed module will block the sample Split into non-overlapping two-dimensional matrix blocks of fixed size, and then use the linear layer of the neural network to map these non-overlapping two-dimensional matrix blocks into D-dimensional embedded patch vectors, where D is the preset vector dimension, and finally add the non-overlapping two-dimensional blocks to the embedded patch vector xl at the position of the representation matrix to maintain the position information. The obtained embedded patch vector is divided into a header patch or a payload patch;
[0090] B2) The self-attention encoder of the packet-level extraction module is composed of a multi-head self-attention layer MSA and a feed-forward layer alternating with each other. The embedded patch vector obtained by the embedding module is input into the packet-level extraction module to extract packet-level features.
[0091] The multi-head self-attention layer MSA uses the attention function to calculate the head of each embedded patch vector, and learns the degree of association between different patches based on the head of each embedded patch vector. The feedforward layer extracts deeper features by linear transformation, first mapping to high-dimensional space and then mapping to low-dimensional space;
[0092] remember is the preset attention function output scale, W Q , W K , W V ∈ is The learned network parameters, To embed the patch vector, the attention function The calculation is as follows:
[0093]
[0094] ,
[0095] in, is the intermediate process matrix of the attention function;
[0096] B3) The stream-level extraction module pools the embedded patch vector features output by the packet-level extraction module row by row to generate row patches;
[0097] B4) Re-input the row patch into the self-attention encoder of the packet-level extraction module to extract the column-level features of the embedded patch vector;
[0098] B5) Finally, the stream-level extraction module performs column-by-column pooling on the obtained column-level features to obtain the final feature vector of the entire representation matrix, denoted as Lf.
[0099] Since the method completely ignores the patch features of other data packets and only extracts the packet-level features in the packet-level extraction module, the missing flow-level information is supplemented in the flow-level extraction module. This method has lower time complexity and can improve the efficiency of traffic analysis.
[0100] (3) Setting up the projection module. Here, we use the idea of contrastive learning to construct the projection module, and further enhance the data by constructing positive and negative sample pairs to complete contrastive learning. The extracted feature vector is mapped to a point on the unit sphere in three-dimensional space, which is used to calculate the similarity score of the sample: the higher the similarity, the smaller the distance between the feature vectors corresponding to the sample on the unit sphere. First, the projection module normalizes the feature vector to form a standard representation vector, then imports the vector into a fully connected layer to obtain the representation feature, and finally normalizes the output to the projection on the unit sphere. We use the cosine similarity obtained by the vector dot product to measure the similarity between two vectors. If the angle between the two vectors is zero, the cosine similarity is 1; if the angle is 180°, it is -1. We use alternating linear layers and ReLU activation function layers to build the actual projection module.
[0101] C1) The projection module is set to include a fully connected layer consisting of two linear layers and a ReLU activation function layer, and the data is normalized before input and after output;
[0102] The projection module performs maximum and minimum normalization on the final feature vector Lf to form a standardized vector representation, and then outputs the embedded feature representation, that is, the output vector, through the fully connected layer, and then normalizes the output vector to a vector on the unit sphere;
[0103] C2) Let the cosine similarity between two vectors be the dot product of the two vectors, and define the following loss function to complete the training:
[0104] Given sample x as an anchor point, record is the augmented data from the same sample as x, is the augmented data from samples different from x. To ensure similar sample pairs (x, ) are close to each other, while different sample pairs (x, ) are far away from each other, and the contrast loss function is calculated based on the cosine similarity. Defined as:
[0105]
[0106] The anchor point r represents the sample x after being projected onto the unit sphere by the projection module. Its different subscripts represent samples from different sets. i represents samples from the entire sample set, and p represents samples from the anchor point r. i All similar sample pairs (x, ), a represents the set P(i) from the anchor point r i The set A(i) of all sample pairs is composed of , and the dot product in the loss formula is used to calculate the cosine similarity;
[0107] The contrast loss function compares the cosine similarity of positive and negative sample pairs, making the positive sample pair (x, ) The cosine similarity is the largest, the negative sample pair (x, ) with the smallest cosine similarity to identify traffic;
[0108] C3) The projection module maps the feature vector Lf to a point on the unit sphere, and then uses the cosine similarity obtained by the vector dot product to measure the similarity between the two vectors: the feature vectors corresponding to the samples with higher similarity have smaller distances on the unit sphere. The unit sphere can be a Z-dimensional space (three-dimensional space) or a higher dimension, which may reach tens of dimensions, which is specifically determined by the input flow data. In the present invention, the sum of the squares of each element in the unit sphere directivity is 1, and is not limited to three dimensions.
[0109] (4) Complete the pre-training of the masked autoencoder and save the obtained parameters. The network structure and parameters of the model obtained by pre-training are saved, and the pre-training process is repeated using a small amount of labeled data sets of downstream tasks. The fine-tuning of different masked autoencoders can quickly modify the parameters to adapt them to the different characteristics of the downstream task data, and then effectively process the large amount of unlabeled data that needs to be classified in the downstream tasks.
[0110] The fourth step is fine-tuning the mask autoencoder: use the labeled data to fine-tune the pre-trained mask autoencoder. Here, the labeled data and the data to be detected are preferably from the same dataset to ensure that the parameters obtained by fine-tuning training can effectively extract features.
[0111] In the pre-training stage, mask enhancement and flow converter are used to extract matrix features, project them to the unit sphere to complete comparative learning, and finally realize the recognition of flow. After pre-training, the encoder will be used for transfer learning of downstream tasks. By sharing the obtained encoder parameters with the encoder of downstream tasks, downstream tasks only need to input actual data into the encoder to fine-tune the parameters, which can effectively speed up the convergence speed and reduce the model size compared with traditional methods.
[0112] (1) The labeled data is input into the pre-trained mask autoencoder for fine-tuning training, which passes through the mask module, traffic feature encoding module and projection module.
[0113] (2) The last fully connected layer in the projection module of the masked autoencoder performs traffic classification and sets the contrast loss function in the projection stage The maximum value is used to identify traffic, that is, the cosine similarity of the sample pairs with the same label is the largest and the cosine similarity of the sample pairs with different labels is the smallest.
[0114] Step 5: Acquisition of traffic to be detected and detection of malicious traffic: Acquire the traffic to be detected to form unlabeled data to be detected, and input the fine-tuned mask automatic encoder to obtain the detection results of malicious traffic.
[0115] The intelligent network traffic identification device using the method of the present invention can integrate functions such as traffic data packet capture, malicious traffic detection method based on mask autoencoder pre-training, and real-time alarm mechanism. The device can be deployed between the gateway node and the firewall, and can efficiently detect malicious traffic in real time by capturing traffic and give an alarm.
[0116] With the expansion of the Internet and the development of network technology, the frequency and severity of network attacks have gradually increased. Accurately identifying malicious traffic among a large number of encrypted traffic has become a huge challenge. The method proposed in this invention uses a large amount of unlabeled data collected in advance to first generate a random mask to construct an enhanced sample, then input the feature into the traffic feature encoding module to extract the feature, and then input the feature into the projection module to complete the pre-training. Specific defense methods are as follows: Figure 5 As shown, this method retains the pre-trained model and parameters and directly applies them to actual downstream tasks. It can be efficiently deployed on intelligent security devices between the firewall and the external network, so as to achieve accurate and rapid detection of unknown traffic and real-time protection of inbound and outbound traffic after fine-tuning with actual data to avoid malicious intrusions.
[0117] The above shows and describes the basic principles, main features and advantages of the present invention. Those skilled in the art should understand that the present invention is not limited to the above embodiments. The above embodiments and descriptions only describe the principles of the present invention. The present invention may be subject to various changes and improvements without departing from the spirit and scope of the present invention. These changes and improvements fall within the scope of the present invention. The scope of protection claimed by the present invention is defined by the attached claims and their equivalents.
Claims
1. A malicious traffic detection method based on masked autoencoder pre-training, characterized in that: The following steps are involved: 11) Data preprocessing: Classify the traffic data into unlabeled data and labeled data, and preprocess the unlabeled data into a two-dimensional representation matrix; The data preprocessing comprises the following steps: 111) Divide the original traffic data into streams according to the IP address, port number and protocol type, then delete the encapsulation information at the stream level and above, set the port number to zero, and replace each IP with a random address in the sending and receiving directions to generate traffic data; 112) Classify traffic data into unlabeled data and labeled data; 113) Preprocessing of unlabeled data: 1131) According to the characteristics of the traffic data, the representation matrix is divided into a traffic header row and a load row, the traffic header row only contains the bytes of the header part of the traffic, and the load row only contains the bytes of the load part of the traffic; 1132) Process each traffic packet: Preset the packet-level matrix size parameters N, H, and W, and use the first N data packets in each flow to generate a two-dimensional matrix with a fixed size of H*W as the representation matrix of the data flow; For each flow, take the first N data packets, and generate a packet-level matrix of fixed size H * W for each data packet. This packet-level matrix consists of a header matrix and a payload matrix of fixed size and position. If the number of valid bytes or data packets in the payload matrix is insufficient, the insufficient position is filled with zeros to ensure that the size of each packet-level matrix is consistent; 1133) stacking the obtained N packet-level matrices vertically in sequence to form a final two-dimensional representation matrix M of the entire flow. If the flow data packets are less than N, fill the matrix with zeros; 12) Setting of mask autoencoder: Setting of mask autoencoder includes three parts: mask module, traffic feature encoding module and projection module; 13) Pre-training the mask autoencoder: Use the two-dimensional representation matrix to pre-train the mask autoencoder; use the mask module to generate random masks to construct enhanced samples, and then use the traffic feature encoding module to extract features to obtain feature vectors; then use the projection module to input the feature vectors and project them onto the unit sphere for comparative learning; 14) Fine-tuning of the mask autoencoder: Fine-tune the pre-trained mask autoencoder using labeled data; 15) Acquisition of traffic to be detected and detection of malicious traffic: The traffic to be detected is obtained to form unlabeled data to be detected, and the data is input into the fine-tuned mask autoencoder to obtain the detection results of malicious traffic.
2. The malicious traffic detection method based on masked autoencoder pre-training according to claim 1 is characterized in that: The pre-training of the mask autoencoder comprises the following steps: 21) For the two-dimensional representation matrix M, enhance samples are constructed by generating random masks to perform data enhancement processing: The mask rate is preset to k, and a two-dimensional mask matrix mask with the same size as the input matrix is generated. K zeros are filled in random positions, and the remaining positions are all 1. Mask samples are generated multiple times to expand the number of representation matrices; mask samples The generation of is expressed as follows: , Among them, the multiplication sign is the Hadamard product; 22) Setting the traffic feature encoding module includes an embedding module, a packet-level extraction model and a flow-level extraction module; The embedding module is used to embed the shielded samples Input gets its patch vector, The packet-level extraction model is used to input the patch vector generated by the embedding module into the self-attention encoder of the packet-level extraction module to obtain packet-level features. The stream-level extraction module is used to add stream-level features to the packet-level features output by the packet-level extraction module, pool the features of the output patches of the packet-level extraction module row by row to generate row patches, then input the row patches into the self-attention encoder to obtain column-level features, and then pool the column-level features column by column to obtain the final feature vector; 221) Embed module will block samples Split into non-overlapping two-dimensional matrix blocks of fixed size, and then use the linear layer of the neural network to map these non-overlapping two-dimensional matrix blocks into D-dimensional embedded patch vectors, where D is the preset vector dimension, and finally add the non-overlapping two-dimensional blocks to the embedded patch vector xl at the position of the representation matrix to maintain the position information. The obtained embedded patch vector is divided into a header patch or a payload patch; 222) The self-attention encoder of the packet-level extraction module is composed of a multi-head self-attention layer MSA and a feedforward layer alternating with each other. The embedded patch vector obtained by the embedding module is input into the packet-level extraction module to extract packet-level features. The multi-head self-attention layer MSA uses the attention function to calculate the head of each embedded patch vector, and learns the degree of association between different patches based on the head of each embedded patch vector. The feedforward layer extracts deeper features by linear transformation, first mapping to high-dimensional space and then mapping to low-dimensional space; remember is the preset attention function output scale, W Q , W K , W V ∈ is The learned network parameters, To embed the patch vector, the attention function The calculation is as follows: ; , in, is the intermediate process matrix of the attention function; 223) The stream-level extraction module pools the embedded patch vector features output by the packet-level extraction module row by row to generate row patches; 224) Re-input the row patch into the self-attention encoder of the packet-level extraction module to extract the column-level features of the embedded patch vector; 225) Finally, the stream-level extraction module performs column-by-column pooling on the obtained column-level features to obtain the final feature vector of the entire representation matrix, denoted as Lf; 23) Set up the projection module, 231) The projection module is set to include a fully connected layer consisting of two linear layers and a ReLU activation function layer, and the data is normalized before input and after output; The projection module performs maximum and minimum normalization on the final feature vector Lf to form a standardized vector representation, and then outputs the embedded feature representation, that is, the output vector, through the fully connected layer, and then normalizes the output vector to a vector on the unit sphere; 232) Let the cosine similarity between two vectors on the unit sphere be the dot product of the two vectors, and define the following loss function to complete the training: Given sample x as an anchor point, record is the augmented data from the same sample as x, is the augmented data from samples different from x. To ensure similar sample pairs (x, ) are close to each other, while different sample pairs (x, ) are far away from each other, and the contrast loss function is calculated based on the cosine similarity. Defined as: ; The anchor point r represents the sample x after being projected onto the unit sphere by the projection module. Its different subscripts represent samples from different sets. i represents samples from the entire sample set, and p represents samples from the anchor point r. i All similar sample pairs (x, ), a represents the set P(i) from the anchor point r i The set A(i) of all sample pairs is composed of , and the dot product in the loss formula is used to calculate the cosine similarity; The contrast loss function compares the cosine similarity of positive and negative sample pairs, making the positive sample pair (x, ) The cosine similarity is the largest, the negative sample pair (x, ) with the smallest cosine similarity to identify traffic; The projection module maps the feature vector Lf to a point on the unit sphere, and then uses the cosine similarity obtained by the vector dot product to measure the similarity between the two vectors: the higher the similarity, the smaller the distance between the feature vectors corresponding to the samples on the unit sphere; 24) Complete the pre-training of the mask autoencoder and save the obtained parameters.
3. The malicious traffic detection method based on masked autoencoder pre-training according to claim 1 is characterized in that: The fine-tuning of the mask autoencoder includes the following steps: 31) Input the labeled data into the pre-trained mask autoencoder for fine-tuning training, passing through the mask module, traffic feature encoding module and projection module; 32) Perform traffic classification in the last fully connected layer of the projection module of the masked autoencoder, and let the contrast loss function be The maximum value is used to identify traffic, that is, the cosine similarity of the sample pairs with the same label is the largest and the cosine similarity of the sample pairs with different labels is the smallest.
