Encrypted network application identification method based on self-attention mechanism
By combining the Transformer Encoder and the convolutional neural network, the self-attention mechanism and external information are used to identify encrypted network traffic, which solves the accuracy and timeliness problems of encrypted traffic identification in the existing technology and achieves efficient network application identification.
Patent Information
- Application Number
- CN202410586148.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-11
- Publication Date
- 2024-09-03
AI Technical Summary
Existing technologies have problems such as reduced accuracy, timeliness and poor model generalization performance in encrypted network traffic identification. It is especially difficult to effectively identify unknown applications and encrypted traffic.
Combined with the Transformer Encoder and the convolutional neural network, the self-attention mechanism is used to extract global information and local features, and the external information of the network traffic (packet length and window value) is used for identification to avoid privacy and security issues caused by intrusive data loads, and Improve the recognition performance of the model through technologies such as multi-head attention mechanism and residual connection.
It achieves high-accuracy recognition of encrypted network applications, solves the problem of poor model generalization performance and forgetting, and can quickly converge and obtain excellent recognition performance.
Smart Images

Figure CN118590262A
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of encrypted network application traffic identification, and more specifically to an encrypted network application identification method based on a self-attention mechanism. Background Art
[0002] Traditional network traffic identification methods, such as port-based detection and deep packet inspection, were the main methods for early traffic identification. However, with the use of dynamic port technology, the accuracy of port-based identification methods has been declining. Deep packet inspection is based on the analysis of available information in the application layer payload of the data packet, using string matching or regular expressions and other methods to match signatures with predefined fingerprint libraries. This method has good accuracy for unencrypted network traffic, but it cannot effectively identify unknown applications and encrypted traffic. Moreover, this method has serious timeliness issues. Whenever a new application appears, the fingerprint library needs to be updated in a timely manner.
[0003] In recent years, machine learning and deep learning have demonstrated powerful capabilities in network traffic identification tasks. Many researchers have applied the technologies required for network traffic identification to machine learning and deep learning. Some researchers use the packet payload as data input and use convolutional neural networks for classification tasks. The data input form used is the byte content of a packet after removing the Ethernet layer, and the IP address and port number are masked using zero padding technology to prevent the model from learning these simple information. However, the sequence number and confirmation number information in the TCP protocol are used, and the sequence number and confirmation number are randomly generated when the session is established. As the communication process continues to increase, since the sequence number and confirmation number occupy 4 bytes in the TCP protocol header respectively, and these two fields are adjacent, the model can easily achieve good results by learning these contents, but the model trained in this way will have poor generalization performance.
[0004] Some other researchers have also used packet length information that is not related to the encryption protocol, and used LSTM models to identify application traffic. Although good results have been achieved, there are still some problems. The model they used cannot be calculated in parallel, and there is a forgetfulness problem when the data sequence is too long. In addition, they extracted the length of the first 200 packets of a stream, and used zero padding for alignment for streams with less than 200 packets. This may result in more useless 0 values in the input data, and the data sequence is too long, and the model is more likely to forget. In addition, when multiple applications have similar values in the packet length sequence, it may not be possible to effectively classify them based solely on the packet length sequence. Therefore, their method cannot cope with this situation. Summary of the invention
[0005] In order to overcome the defects and deficiencies in the above-mentioned prior art, the present invention provides an encrypted network application identification method based on a self-attention mechanism. The purpose of the present invention is to solve the problems existing in the prior art in the identification of encrypted network applications. The present invention effectively combines Transformer Encoder and convolutional neural network, and uses the ability of Transformer Encoder's self-attention mechanism to extract global information to effectively associate the connections between input data sequences, so that each data item contains all the information of the context. Then, with the help of the ability of convolutional neural network to extract local spatial features, the key local features in the network traffic are extracted from the global context information, which makes up for the shortcomings of Transformer Encoder in capturing local spatial features. In addition, Transformer Encoder can solve the forgetting problem and the defect of inability to parallel calculation caused by the long input data of recurrent neural network, and can achieve better results in combination with convolutional neural network. This method uses only the external information of network traffic, namely the packet length and window value, to identify network applications, which not only avoids the privacy and security problems caused by the intrusion data load, but also enables the model to converge quickly and obtain excellent recognition performance.
[0006] In order to solve the above problems existing in the prior art, the present invention is implemented through the following technical solutions.
[0007] The present invention provides an encrypted network application identification method based on a self-attention mechanism, the method comprising the following steps:
[0008] S1, after collecting network traffic from the network card, aggregate the network traffic in a session manner according to the five-tuple information;
[0009] S2, dividing the session flow obtained after aggregation in step S1 into multiple sub-flows according to the set number of packets;
[0010] S3, extracting the packet length and TCP window value in each sub-flow to form the original input data sequence;
[0011] S4, use the word embedding model to embed the original input data sequence into a higher dimensional representation;
[0012] S5. Use the higher-dimensional representation as the input of the Transformer Encoder module, and use the Transformer Encoder module to extract global features from the original input data sequence to obtain context information;
[0013] S6. Use 2D-CNN to extract local features from global features containing contextual information and extract local spatial features. Use a two-layer fully connected neural network for linear transformation, use the Softmax function to calculate the predicted probability value of each category, and output the index with the maximum probability as the predicted category.
[0014] S7, using the loss function to calculate the loss between the predicted category and the true category, and deriving the loss function to find the gradient value of the model parameter, and using the back propagation algorithm to update the weight parameters of the model;
[0015] S8. After the model training is completed, different applications are identified through input features.
[0016] Further preferably, in step S5, a Transformer Encoder module is used to extract global features from the original input data sequence to obtain context information, which specifically includes the following sub-steps:
[0017] S501. For the higher dimensional representation E of the input, respectively with three learnable weight matrices W Q , W K , W V Perform linear transformation to generate Q (Query), K (Key), and V (Value) matrices respectively, where Q represents the query matrix, K represents the key matrix, and V represents the value matrix. d k =d v =d model / h,d model Same meaning as embedding dimension e, d k is the dimension of data processed by each head, and h refers to the number of multiple heads;
[0018] S502: Perform matrix operations on the query matrix Q and the key matrix K, and divide the resulting values by Scaling is performed to obtain a scaled matrix;
[0019] S503, normalizing the scaled matrix through the Softmax function so that the attention score is distributed between 0 and 1;
[0020] S504, performing matrix operations on the calculated attention scores and the value matrix V, applying the attention scores to different positions of the input sequence, and generating a new data representation in a weighted summation manner;
[0021] S505: perform h times of scaled dot product attention calculation on the original input data, then concatenate the h heads to form a data representation containing more information, and finally pass the weight matrix WO Perform a linear transformation to map the concatenated vector to the final output dimension d model .
[0022] More preferably, in step S5, the Transformer Encoder module introduces a residual connection module and a layer normalization method to add the input data E and the output of the sub-module.
[0023] Further preferably, for the higher dimensional representation E of the input, the output after processing by the multi-head attention mechanism is MultiHead Attention(E), and then the residual connection and layer normalization method are used to obtain the output Layer(E+MultiHead Attention(E)), and then the residual connection is used to add the output of the multi-head attention layer and the output of the feedforward neural network layer, that is, Layer(E+FFN(E)), where FFN(x)=max(0,xW1+b1)W2+b2, where W1 and W2 represent weight vectors, and b1 and b2 represent bias terms.
[0024] More preferably, two layers of fully connected neural networks are used in the feedforward neural network layer to convert the input. The first layer of the fully connected network uses the Relu activation function as a nonlinear transformation to map negative numbers to 0 to solve the gradient disappearance problem. The second layer is a conventional fully connected network used to convert the output data dimension to the same dimension as the input data.
[0025] More preferably, in step S5, a three-layer Transformer Encoder module is used, and d model =128, feedforward=1024, h=4.
[0026] Further preferably, step S6 includes the following sub-steps:
[0027] S601, the data outputted in step S5 is increased in dimension, that is, E=E.unsqueeze(1), where E=E.unsqueeze(1) indicates increasing the dimension of a multidimensional vector, and parameter 1 means that the increased dimension is the first dimension;
[0028] S602, use 64 convolution kernels Conv2d with a convolution size of 2 to slide on the data E with a step size of 2 to extract multiple different local features, that is, E = Conv2d (E), and the dimension of the data E after convolution becomes E∈R 512×64×32×64 ;
[0029] S603, using the Relu activation function to perform nonlinear changes on the output data E after convolution, that is, E=Relu(E), mapping negative values to 0, and keeping positive values unchanged;
[0030] S604, add a maximum pooling layer MaxPooling with a window size of 2 after the convolution layer to reduce the number of neurons, that is, E = MaxPooling (E); after the maximum pooling layer, the input data dimension becomes E∈R 512×64×16×32 ;
[0031] S605, use 128 convolution kernels with a convolution size of 2 to further extract local spatial features, and use the ReLU function for nonlinear transformation. After two layers of convolution and maximum pooling, the data dimension becomes E∈R 512×128×4×8 ;
[0032] S606, use Flatten to expand the multidimensional data, and the expanded data is in the form of E∈R 512×4096 ;
[0033] S607, use a two-layer fully connected neural network to perform linear transformation;
[0034] S608, using the Softmax function to calculate the predicted probability value of each category of the model, the calculation formula is as follows:
[0035]
[0036] where p i represents the probability that the model predicts the sample to be in category i, z i It is the i-th component of the output vector of the output layer, and then the index with the maximum probability is output as the predicted category.
[0037] More preferably, in step S607, the first fully connected layer is Linear1(4096,1024), and the second fully connected layer is Linear2(1024,C), where C represents the number of classification categories.
[0038] Further preferably, in step S7, a cross entropy loss function is used to calculate the loss between the true value and the predicted value, and the calculation formula is as follows:
[0039]
[0040] Among them, M represents all training samples; C represents the number of categories; y i,j Indicates the value of the jth category in the actual label of sample i, represented by 0 or 1; p i,j It represents the probability that the predicted category of sample i is j.
[0041] Further preferably, in step S4, assuming that the embedding dimension e=128, the word embedding model is used to embed the original input data sequence into a higher dimensional representation as shown in the following formula:
[0042] Where E represents the data representation embedded in a higher dimension, m is the number of packets per sub-flow, and e is the embedding dimension.
[0043] Further preferably, in step S2, it is assumed that a flow consists of n data packets, that is, f = {p1, p2, ..., p n}, f is divided into a sub-flow according to every m = 32 data packets, then the composition of each sub-flow is as follows:
[0044]
[0045] The number of packets in the last flow is The number of packets in other sub-flows is m.
[0046] More preferably, in step S3, the length of the data packet in the substream and the TCP window value are obtained. Since the UDP datagram has no window value, it is set to 0; in this way, the original input data sequence is obtained:
[0047] Input={L1,L2,…,L 2m}, where L1, L2, …, L m Indicates the length of each data packet, L m+1 ,L m+2 ,…,L 2m Indicates the TCP window value for each data packet.
[0048] Compared with the prior art, the beneficial technical effects brought by the present invention are as follows:
[0049] 1. The encrypted network application identification method based on the self-attention mechanism of the present invention effectively combines TransformerEncoder and convolutional neural network, and uses the ability of Transformer Encoder's self-attention mechanism to extract global information to effectively associate the connection between input data sequences, so that each data item contains all the information of the context. Then, with the help of the ability of convolutional neural network to extract local spatial features, the key local features in the network traffic are extracted from the global context information, which makes up for the shortcomings of Transformer Encoder in capturing local spatial features. In addition, Transformer Encoder can solve the forgetting problem and the defect of inability to parallel calculation caused by the long input data of recurrent neural network, and can achieve better results in combination with convolutional neural network. This method uses only the external information of network traffic, namely the packet length and window value, to identify network applications, which not only avoids the privacy and security problems caused by intrusive data load, but also enables the model to converge quickly and obtain excellent recognition performance.
[0050] 2. In the present invention, each network flow is aggregated in a session manner according to the five-tuple information. The network data packets in the session flow aggregated in the session manner either have the same five-tuple information or are data packets that exchange the source IP address and destination IP address, source port and destination port, that is, (destination IP address, destination port, source IP address, source port, protocol). A session represents the combination of data packets generated by the communicating parties during the interaction process, and can carry richer information for feature extraction.
[0051] 3. For encrypted application traffic, its load content becomes random and irregular, and there is no effective information for identifying the application. Starting from external features, the present invention extracts the frame length and window value of each data packet from a stream. Through the word embedding method, each element in the frame length sequence and the window sequence can be mapped to a continuous vector space. Through word embedding, the sequence data can be converted into a high-dimensional data containing more information, which is conducive to the model learning some potential characteristics.
[0052] 4. In the present invention, the Transformer Encoder module uses the Q (Query) and K (Key) matrices to calculate the attention score. By performing matrix operations on Q and K, the correlation between each position and other positions can be obtained, and then the information of different positions can be weighted according to these correlations. Since the inner product of the vector may be too large when performing matrix operations on Q and K, resulting in the gradient disappearance problem, in order to make the attention score distribution more stable, it is divided by The value of the dot product can be scaled so that it is distributed within a reasonable range. The attention score is used to represent the correlation between different elements in the sequence. This correlation allows each element to incorporate the information of other items. This achieves the purpose of extracting global information. The calculated attention score is then matrix-operated with the value matrix V. This is to apply the attention score to different positions of the input sequence and generate a new data representation in a weighted summation manner. This new representation can contain global information in the input sequence. In this way, the model can pay attention to information at different positions in the input sequence and capture long-distance dependencies more flexibly when processing sequence data.
[0053] 5. In the present invention, in order to reduce the network degradation and gradient vanishing problems that occur in the deep network during training and improve the training stability of the model, the Transformer Encoder module introduces a residual connection module and a layer normalization method to add the input data E and the output sublayer(E) of the submodule.
[0054] 6. In the present invention, the encoder can capture the dependencies and global information of sequence data through the self-attention mechanism. However, the encoder is still insufficient in the ability to extract local features. In order to make up for its shortcomings in extracting local features, its 2D-CNN is combined. After the encoder extracts global information, the context information is obtained, and then the 2D-CNN extracts local features of the data containing the context information. Through traffic slicing and TECNN model processing, different network application traffic can be quickly and accurately identified. BRIEF DESCRIPTION OF THE DRAWINGS
[0055] Figure 1 is a flow chart of the method of the present invention;
[0056] Figure 2 This is a model structure diagram of the encrypted network application identification method based on the self-attention mechanism of the present invention;
[0057] Figure 3 This is a flow chart of network application traffic identification of the present invention;
[0058] Figure 4 This is a framework diagram for online application traffic identification according to the present invention. DETAILED DESCRIPTION
[0059] The following is an exemplary embodiment of the present invention that helps to fully understand the claims and their equivalents in conjunction with the accompanying drawings, wherein the specific details will be considered as exemplary only, and do not limit the scope of the present invention. Therefore, those of ordinary skill in the art can make various changes and modifications to the embodiments without departing from the scope and spirit of the present invention.
[0060] Example 1
[0061] As a preferred embodiment of the present invention, refer to the attached specification Figure 1 As shown, this embodiment discloses a method for identifying encrypted network applications based on a self-attention mechanism, the method comprising the following steps:
[0062] S1, after collecting network traffic from the network card, aggregate the network traffic in a session manner according to the five-tuple information;
[0063] S2, dividing the session flow obtained after aggregation in step S1 into multiple sub-flows according to the set number of packets;
[0064] S3, extracting the packet length and TCP window value in each sub-flow to form the original input data sequence;
[0065] S4, use the word embedding model to embed the original input data sequence into a higher dimensional representation;
[0066] S5. Use the higher-dimensional representation as the input of the Transformer Encoder module, and use the Transformer Encoder module to extract global features from the original input data sequence to obtain context information;
[0067] S6. Use 2D-CNN to extract local features from global features containing contextual information and extract local spatial features. Use a two-layer fully connected neural network for linear transformation, use the Softmax function to calculate the predicted probability value of each category, and output the index with the maximum probability as the predicted category.
[0068] S7, using the loss function to calculate the loss between the predicted category and the true category, and deriving the loss function to find the gradient value of the model parameter, and using the back propagation algorithm to update the weight parameters of the model;
[0069] S8. After the model training is completed, different applications are identified through input features.
[0070] Example 2
[0071] As another preferred embodiment of the present invention, this embodiment is based on the above-mentioned embodiment 1, and further supplements and elaborates on the technical solution of the present invention in detail. In this embodiment, in step S5, the TransformerEncoder module is used to extract global features of the original input data sequence to obtain context information, which specifically includes the following sub-steps:
[0072] S501. For the higher dimensional representation E of the input, respectively with three learnable weight matrices W Q , W K , W V Perform linear transformation to generate Q (Query), K (Key), and V (Value) matrices respectively, where Q represents the query matrix, K represents the key matrix, and V represents the value matrix. d k =d v =d model / h,d model Same meaning as embedding dimension e, d k is the dimension of data processed by each head, and h refers to the number of multiple heads;
[0073] S502: Perform matrix operations on the query matrix Q and the key matrix K, and divide the resulting values by Scaling is performed to obtain a scaled matrix;
[0074] S503, normalizing the scaled matrix through the Softmax function so that the attention score is distributed between 0 and 1;
[0075] S504, performing matrix operations on the calculated attention scores and the value matrix V, applying the attention scores to different positions of the input sequence, and generating a new data representation in a weighted summation manner;
[0076] S505: perform h times of scaled dot product attention calculation on the original input data, then concatenate the h heads to form a data representation containing more information, and finally pass the weight matrix W O Perform a linear transformation to map the concatenated vector to the final output dimension d model .
[0077] As a preferred implementation of this embodiment, in step S5, the Transformer Encoder module introduces a residual connection module and a layer normalization method to add the input data E and the output of the sub-module.
[0078] Specifically, for the higher dimensional representation E of the input, the output after processing by the multi-head attention mechanism is MultiHead Attention(E), and then the residual connection and layer normalization method are used to obtain the output Layer(E+MultiHeadAttention(E)), and then the residual connection is used to add the output of the multi-head attention layer and the output of the feedforward neural network layer, that is, Layer(E+FFN(E)), where FFN(x)=max(0,xW1+b1)W2+b2, where W1 and W2 represent weight vectors, and b1 and b2 represent bias terms.
[0079] As an example, two layers of fully connected neural networks are used in the feedforward neural network layer to transform the input. The first layer of the fully connected network uses the Relu activation function as a nonlinear transformation to map negative numbers to 0 to solve the gradient disappearance problem. The second layer is a conventional fully connected network used to transform the output data dimension to the same dimension as the input data.
[0080] As another example, the above process uses a 3-layer Transformer Encoder module and sets d model =128, feedforward=1024, h=4.
[0081] Example 3
[0082] As another preferred embodiment of the present invention, this embodiment further supplements and elaborates on the technical solution of the present invention on the basis of the above-mentioned embodiment 1 or embodiment 2. In this embodiment, step S6 includes the following sub-steps:
[0083] S601, the data outputted in step S5 is increased in dimension, that is, E=E.unsqueeze(1), where E=E.unsqueeze(1) indicates increasing the dimension of a multidimensional vector, and parameter 1 means that the increased dimension is the first dimension;
[0084] S602, use 64 convolution kernels Conv2d with a convolution size of 2 to slide on the data E with a step size of 2 to extract multiple different local features, that is, E = Conv2d (E), and the dimension of the data E after convolution becomes E∈R 512×64×32×64 ;
[0085] S603, using the Relu activation function to perform nonlinear changes on the output data E after convolution, that is, E=Relu(E), mapping negative values to 0, and keeping positive values unchanged;
[0086] S604, add a maximum pooling layer MaxPooling with a window size of 2 after the convolution layer to reduce the number of neurons, that is, E = MaxPooling (E); after the maximum pooling layer, the input data dimension becomes E∈R 512×64×16×32 ;
[0087] S605, use 128 convolution kernels with a convolution size of 2 to further extract local spatial features, and use the ReLU function for nonlinear transformation. After two layers of convolution and maximum pooling, the data dimension becomes E∈R 512×128×4×8 ;
[0088] S606, use Flatten to expand the multidimensional data, and the expanded data is in the form of E∈R 512×4096 ;
[0089] S607, use a two-layer fully connected neural network for linear transformation;
[0090] S608, using the Softmax function to calculate the predicted probability value of each category of the model, the calculation formula is as follows:
[0091]
[0092] where p i represents the probability that the model predicts the sample to be in category i, z i It is the i-th component of the output vector of the output layer, and then the index with the maximum probability is output as the predicted category.
[0093] More preferably, in step S607, the first fully connected layer is Linear1(4096,1024), and the second fully connected layer is Linear2(1024,C), where C represents the number of classification categories.
[0094] In step S7, the cross entropy loss function is used to calculate the loss between the true value and the predicted value. The calculation formula is as follows:
[0095]
[0096] Among them, M represents all training samples; C represents the number of categories; y i,j Indicates the value of the jth category in the actual label of sample i, represented by 0 or 1; p i,j It represents the probability that the predicted category of sample i is j.
[0097] Further preferably, in step S4, assuming that the embedding dimension e=128, the word embedding model is used to embed the original input data sequence into a higher dimensional representation as shown in the following formula:
[0098] Where E represents the data representation embedded in a higher dimension, m is the number of packets per sub-flow, and e is the embedding dimension.
[0099] Further preferably, in step S2, it is assumed that a flow consists of n data packets, that is, f = {p1, p2, ..., p n}, f is divided into a sub-flow according to every m = 32 data packets, then the composition of each sub-flow is as follows:
[0100]
[0101] The number of packets in the last flow is The number of packets in other sub-flows is m.
[0102] More preferably, in step S3, the length of the data packet in the substream and the TCP window value are obtained. Since the UDP datagram has no window value, it is set to 0; in this way, the original input data sequence is obtained:
[0103] Input={L1,L2,…,L 2m}, where L1, L2, …, L m Indicates the length of each data packet, L m+1 ,L m+2 ,…,L 2m Indicates the TCP window value for each data packet.
[0104] Example 4
[0105] As another preferred embodiment of the present invention, this embodiment provides an encrypted network application identification method based on the self-attention mechanism, which is implemented based on the encrypted network application identification model TECNN. It mainly consists of a word embedding module, a Transformer Encoder module, a 2D-CNN module and a classifier module. The model structure is as follows Figure 2 As shown in the figure, the model has the ability to extract global information and local spatial features, has stronger learning ability and complex feature representation ability, and can accurately identify different encrypted network applications. The processing flow of the model is as follows: Figure 3 The detailed process is as follows:
[0106] After collecting network traffic from the network card, we first use open source tools to aggregate the network traffic in a session-based manner according to the five-tuple information (source IP address, source port, destination IP address, destination port, protocol). The network data packets in the session flow aggregated in a session-based manner either have the same five-tuple information or are data packets that have exchanged the source IP address and destination IP address, source port and destination port, that is, (destination IP address, destination port, source IP address, source port, protocol). A session represents the combination of data packets generated by the communicating parties during the interaction process, and can carry richer information for feature extraction. For encrypted application traffic, its payload content becomes random and irregular, and there is no effective information for identifying the application. We start with external features and extract the frame length and window value of each data packet from a stream to form a packet length sequence ps = {p1, p2, ..., p m} and the window sequence ws={w1,w2,...w m}. In general, the length of an Ethernet frame does not exceed 1518 bytes, and the window size is between 0 and 65535. Therefore, the word embedding method can map each element in the frame length sequence and the window sequence to a continuous vector space. For example, the input data X = {x1, x2, ..., x N}, the dimension of word embedding is E, then after word embedding, each element x i Transformed into an E-dimensional vector x′ i , that is, x′ i ∈R E ,X'={x′1,x′2,...,x′ N}∈R N×E Through word embedding, sequence data can be converted into high-dimensional data containing more information, which is conducive to the model learning some potential features.
[0107] Then, we slice the network traffic according to the number of packets. Assume that a flow consists of n packets, that is, f = {p1, p2, ..., p n}, f is divided into a sub-flow according to every m=32 data packets. Then the composition of each sub-flow is as follows:
[0108]
[0109] p (i-1)m+1 is the first data packet of the ith substream; except that the number of packets of the last substream may be less than m, that is, the number of packets of the last substream is The number of packets in other sub-flows is m.
[0110] Then extract the packet length and TCP window value in the substream. Since the UDP datagram has no window value, set it to 0. In this way, the original input data sequence is obtained:
[0111] Input={L1,L2,…,L 2m}
[0112] Then the original data sequence is converted to a high-dimensional tensor representation through the word embedding model to obtain richer information. Assuming the embedding dimension e = 128, the converted data representation is as follows:
[0113]
[0114] Then the feature matrix E after the word embedding model transformation is input into the Transformer Encoder module. We use a 3-layer Encoder module and set d model =128, feedforward=1024, h=4. Specifically, the MultiHead Attention of the Encoder module first processes the input data. The formula of the self-attention mechanism is For the input data E, respectively with 3 learnable weight matrices W Q ,W K ,W V Perform linear transformation to generate Q (Query), K (Key), and V (Value) matrices, where Q is the query matrix, K is the key matrix, and V is the value matrix. d k =d v =d model / h,d model Same meaning as embedding dimension e, d k is the dimension of data processed by each head, and h refers to the number of heads. The Q and K matrices are used to calculate the attention score. In the self-attention mechanism module, by performing matrix operations on Q and K, the correlation between each position and other positions can be obtained, and then the information of different positions can be weighted according to these correlations. Since the inner product of the vector may be too large when performing matrix operations on Q and K, resulting in the gradient disappearance problem, in order to make the attention score distribution more stable, it is divided by The dot product value can be scaled so that it is distributed within a reasonable range.
[0115] definition but Where q and k are vectors in Q and K respectively. Since it is assumed that each element in q and k is independent and generated according to a distribution with mean 0 and variance 1, then When calculating the variance, qi and k i refers to the random variable to which it belongs. So dividing by The result of the vector dot product is scaled. The scaled matrix is normalized by the Softmax function so that the attention score is distributed between 0 and 1. The attention score is used to represent the correlation between different elements in the sequence. This correlation allows each element to incorporate the information of other items, thereby achieving the purpose of extracting global information.
[0116] The calculated attention scores are then matrix-operated with the value matrix V to apply the attention scores to different positions of the input sequence and generate a new data representation in a weighted summation manner. This new representation can contain global information in the input sequence. In this way, the model can pay attention to information at different positions in the input sequence and capture long-distance dependencies more flexibly when processing sequence data.
[0117] Since multi-head attention is used, it is also necessary to perform h scaled dot product attention calculations on the input data.
[0118] Head i =Attention(EW Q ,E.W. K ,E.W. K )
[0119] Each attention head can learn to focus on information at different positions in the input sequence, thereby improving the model's ability to express the input. Then, the h heads are concatenated to form a data representation containing more information, and finally a weight matrix W is used. O Perform a linear transformation to map the concatenated vector to the final output dimension d model The combined result is shown below:
[0120] MultiHead(Q,K,V)=Concat(head1,head2,…,head h )W o
[0121] in
[0122] In order to reduce the network degradation and gradient vanishing problems that occur in deep networks during training and improve the training stability of the model, Transformer Encoder introduces a residual connection module and a layer normalization method to add the input data E and the output sublayer(E) of the submodule. First, the output of the input data E after being processed by the multi-head attention mechanism is MultiHead Attention(E), and then the residual connection and layer normalization method are used to obtain the output Layer(E+MultiHeadAttention(E)). Next, two layers of fully connected neural networks are used in the feedforward neural network layer to transform the input. The first layer of the fully connected network uses the Relu activation function as a nonlinear transformation to map negative numbers to 0 to solve the gradient vanishing problem. The second layer is a conventional fully connected network used to convert the output data dimension to the same dimension as the input data. Then, a residual connection is used to add the output of the multi-head attention layer to the output of the feedforward neural network layer, that is
[0123] Layer(E+FFN(E)), where FFN(x)=max(0,xW1+b1)W2+b2
[0124] After the superposition of multiple layers of Encoder modules, the output data E contains rich contextual information. Through the self-attention mechanism, the Encoder can capture the dependencies and global information of sequence data. However, the Encoder is still insufficient in extracting local features. In order to make up for its shortcomings in extracting local features, its 2D-CNN is combined. After the Encoder extracts global information, the context information is obtained, and then the 2D-CNN extracts local features of the data containing contextual information. Then the data output by the Encoder is dimensioned, that is, E = E.unsqueeze (1), so that E becomes a single-channel data form, and then 64 convolution kernels Conv2d with a convolution size of 2 are used to slide on the data E with a step size of 2 to extract multiple different local features, that is, E = Conv2d (E), then the dimension of the data E after convolution becomes E∈R 512×64×32×64 (Here the batch size is set to 512), and then the Relu activation function is used to perform nonlinear changes on the output data E after convolution, that is, E = Relu (E), mapping negative values to 0 and keeping positive values unchanged, to enhance the model's ability to learn and represent complex data, and to help solve the gradient vanishing problem. Then, a maximum pooling layer MaxPooling with a window size of 2 is added after the convolution layer to significantly reduce the number of neurons and retain key features to the greatest extent, that is, E = MaxPooling (E). After the maximum pooling layer, the input data dimension becomes E∈R 512×64×16×32Then, 128 convolution kernels with a convolution size of 2 are used to further extract local spatial features, and the Relu function is used for nonlinear transformation. The converted data also uses the maximum pooling MaxPooling to reduce the number of neurons. After two layers of convolution and maximum pooling, the data dimension becomes E∈R 512×128×4×8 In this way, the data is transformed from the original single channel to the final 128 channels, extracting more types and richer spatial feature representations. Then use Flatten to expand the multidimensional data. The expanded data form E∈R 512×4096 Finally, a two-layer fully connected neural network is used for linear transformation, where the first fully connected layer is Linear1(4096,1024) and the second fully connected layer is Linear2(1024,C), where C represents the number of categories. Finally, the Softmax function is used to calculate the predicted probability value of each category of the model. The calculation formula is as follows:
[0125]
[0126] where p i represents the probability that the model predicts the sample to be in category i, z i is the i-th component of the output vector of the output layer, and then the index with the maximum probability is output as the predicted category. The model continuously calculates the loss between the predicted category and the true category, and derives the loss function to find the gradient value of the model parameters, and then uses the back propagation algorithm to update the weight parameters of the model, so that the model can predict more accurately. Generally, the cross entropy loss function is used to calculate the loss between the true value and the predicted value. The calculation formula is as follows:
[0127]
[0128] Among them, M represents all training samples, C represents the number of categories, and y i,j represents the value of the jth category in the actual label of sample i (0 or 1), p i,j It represents the probability that the predicted category of sample i is j.
[0129] After the model is trained, it can effectively identify different applications through input features. In order to meet the online traffic identification task, we give the following Figure 4 The online traffic identification architecture shown in Figure 1 is shown in Figure 1. We perform port mirroring at the egress of network traffic, introduce network traffic into the server where the TECNN model is deployed, and then use the real-time collected network session traffic as follows: Figure 3 Through traffic slicing and TECNN model processing, different network application traffic can be quickly and accurately identified.
Claims
1. A method for identifying encrypted network applications based on a self-attention mechanism, characterized in that: The method comprises the following steps: S1, after collecting network traffic from the network card, aggregate the network traffic in a session manner according to the five-tuple information; S2, dividing the session flow obtained after aggregation in step S1 into multiple sub-flows according to the set number of packets; S3, extracting the packet length and TCP window value in each sub-flow to form the original input data sequence; S4, use the word embedding model to embed the original input data sequence into a higher dimensional representation; S5. Use the higher-dimensional representation as the input of the Transformer Encoder module, and use the Transformer Encoder module to extract global features from the original input data sequence to obtain context information. S6. Use 2D-CNN to extract local features from global features containing contextual information and extract local spatial features. Use a two-layer fully connected neural network for linear transformation, use the Softmax function to calculate the predicted probability value of each category, and output the index with the maximum probability as the predicted category. S7, using the loss function to calculate the loss between the predicted category and the true category, and deriving the loss function to find the gradient value of the model parameter, and using the back propagation algorithm to update the weight parameters of the model; S8. After the model training is completed, different applications are identified through input features.
2. The method for identifying encrypted network applications based on a self-attention mechanism as claimed in claim 1, characterized in that: In step S5, the Transformer Encoder module is used to extract global features from the original input data sequence to obtain context information, which specifically includes the following sub-steps: S501. For the higher dimensional representation E of the input, respectively with three learnable weight matrices W Q , W k , W V Perform linear transformation to generate Q (Query), K (Key), and V (Value) matrices respectively, where Q represents the query matrix, K represents the key matrix, and V represents the value matrix. d k =d v =d model / h,d model Same meaning as embedding dimension e, d k is the dimension of data processed by each head, and h refers to the number of multiple heads; S502: Perform matrix operations on the query matrix Q and the key matrix K, and divide the resulting values by Scaling is performed to obtain a scaled matrix; S503, normalizing the scaled matrix through the Softmax function so that the attention score is distributed between 0 and 1; S504, performing matrix operations on the calculated attention scores and the value matrix V, applying the attention scores to different positions of the input sequence, and generating a new data representation in a weighted summation manner; S505: perform h times of scaled dot product attention calculation on the original input data, then concatenate the h heads to form a data representation containing more information, and finally pass the weight matrix W O Perform a linear transformation to map the concatenated vector to the final output dimension d model .
3. The method for identifying encrypted network applications based on a self-attention mechanism as claimed in claim 2, characterized in that: In step S5, the Transformer Encoder module introduces a residual connection module and a layer normalization method to add the input data E and the output of the sub-module.
4. The method for identifying encrypted network applications based on a self-attention mechanism as claimed in claim 3, characterized in that: For the higher dimensional representation E of the input, the output after processing by the multi-head attention mechanism is MultiHeadAttention(E), and then the residual connection and layer normalization method are used to obtain the output Layer(E+MultiHead Attention(E)), and then the residual connection is used to add the output of the multi-head attention layer and the output of the feedforward neural network layer, that is, Layer(E+FFN(E)), where FFN(x)=max(0,xW1+b1)W2+b2, where W1 and W2 represent weight vectors, and b1 and b2 represent bias terms.
5. The method for identifying encrypted network applications based on a self-attention mechanism as claimed in claim 4, characterized in that: In the feedforward neural network layer, two layers of fully connected neural networks are used to convert the input. The first layer of the fully connected network uses the Relu activation function as a nonlinear transformation to map negative numbers to 0 to solve the gradient disappearance problem. The second layer is a conventional fully connected network used to convert the output data dimension to the same dimension as the input data.
6. A method for identifying encrypted network applications based on a self-attention mechanism as described in any one of claims 2 to 5, characterized in that: In step S5, a 3-layer Transformer Encoder module is used and d model =128, feedforward=1024, h=4.
7. A method for identifying encrypted network applications based on a self-attention mechanism as described in any one of claims 1 to 5, characterized in that: Step S6 includes the following sub-steps: S601, the data outputted in step S5 is increased in dimension, that is, E=E.unsqueeze(1), where E=E.unsqueeze(1) indicates increasing the dimension of a multidimensional vector, and parameter 1 means that the increased dimension is the first dimension; S602, use 64 convolution kernels Conv2d with a convolution size of 2 to slide on the data E with a step size of 2 to extract multiple different local features, that is, E = Conv2d (E), and the dimension of the data E after convolution becomes E∈R 512×64×32×64 ; S603, using the Relu activation function to perform nonlinear changes on the output data E after convolution, that is, E=Relu(E), mapping negative values to 0, and keeping positive values unchanged; S604, add a maximum pooling layer MaxPooling with a window size of 2 after the convolution layer to reduce the number of neurons, that is, E = MaxPooling (E); after the maximum pooling layer, the input data dimension becomes E∈R 512×64×16×32 ; S605, use 128 convolution kernels with a convolution size of 2 to further extract local spatial features, and use the ReLU function for nonlinear transformation. After two layers of convolution and maximum pooling, the data dimension becomes E∈R 512×128×4×8 ; S606, use Flatten to expand the multidimensional data, and the expanded data is in the form of E∈R 512×4096 ; S607, use a two-layer fully connected neural network to perform linear transformation; S608, using the Softmax function to calculate the predicted probability value of each category of the model, the calculation formula is as follows: where p i represents the probability that the model predicts the sample to be in category i, z i It is the i-th component of the output vector of the output layer, and then the index with the maximum probability is output as the predicted category.
8. The method for identifying encrypted network applications based on a self-attention mechanism as claimed in claim 7, characterized in that: In step S607, the first fully connected layer is Linear1(4096,1024), and the second fully connected layer is Linear2(1024,C), where C represents the number of categories.
9. A method for identifying encrypted network applications based on a self-attention mechanism as described in any one of claims 1 to 5, characterized in that: In step S7, the cross entropy loss function is used to calculate the loss between the true value and the predicted value. The calculation formula is as follows: Among them, M represents all training samples; C represents the number of categories; y i,j Indicates the value of the jth category in the actual label of sample i, represented by 0 or 1; p i,j It represents the probability that the predicted category of sample i is j.
10. A method for identifying encrypted network applications based on a self-attention mechanism according to any one of claims 1 to 5, characterized in that: In step S4, assuming that the embedding dimension e = 128, the word embedding model is used to embed the original input data sequence into a higher-dimensional representation as shown below: Where E represents the data representation embedded in a higher dimension, m is the number of packets per sub-flow, and e is the embedding dimension.
Citation Information
Cited By
High-precision gas-liquid two-phase flow pattern identification method based on data conversion and multi-head attention mechanism
CN119337776A
A high-precision gas-liquid two-phase flow pattern recognition method based on data conversion and multi-head attention mechanism
CN119337776B
