An open set network attack identification method fusing supervised contrastive learning and mahalanobis distance

CN122933164APending Publication Date: 2026-10-16王腾彪
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611158576.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-07-31
Publication Date
2026-10-16

AI Technical Summary

Technical Problem

[0004]为解决闭集假设带来的问题,现有技术通过对比学习提取流量特征,再对正常流量特征进行聚类得到特征簇,通过计算待检测特征与聚类中心的欧氏空间距离进行异常判定,这类方案存在以下局限:欧氏距离将特征空间的各个方向同等对待,未考虑各类别自身分布的形状与离散程度,对于在方向上贴近某类别中心、但已落在该类别实际分布范围之外的流量,其欧氏距离仍然较小,从而产生漏报;通常这类方法采用自监督方式训练编码器,其正样本对由同一样本的数据增强视图构成,编码器仅学习到对增强变换的不变性,未能利用已有的类别标签信息,所得特征空间的类间可分性有限;另外这类方案仅以正常流量建立单一基线,只能区分正常与异常两种状态,无法在识别异常的同时给出已知攻击的具体类别

Benefits of technology

[0013] 1. By using supervised contrastive learning to pre-train the encoder, the model already possesses a discriminative feature space for class clustering and dissimilar class separation before entering the classification task. This significantly improves the ability to identify small sample attack categories and provides a good feature foundation for subsequent distance-based anomaly detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122933164A_ABST
    Figure CN122933164A_ABST
Patent Text Reader

Abstract

In view of the problems that the existing method is insufficient in identifying rare attacks when the flow categories are seriously unbalanced, and the classifier cannot recognize new network attacks, the present application provides an open set network attack recognition method fusing supervised contrast learning and Mahalanobis distance. It comprises constructing a neural network containing an encoder, a projection head and a classification head; pre-training the encoder by balanced batch sampling and supervised contrast loss, so as to make the same type of flow features gather and different types of flow features separate; then fine-tuning the classification head by a class-weighted focus loss; fitting the class center vector and the inverse matrix of the within-class covariance for each class; taking the minimum value of the Mahalanobis distance of the to-be-recognized sample to each known class as the anomaly score, and the sample with a score exceeding a threshold is recognized as abnormal flow and an alarm is given, otherwise the known attack category given by the classification head. In this way, the accuracy of network attack recognition and the recognition ability of abnormal flow are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of cyberspace security technology, and in particular to an open set network attack identification method that integrates supervised contrastive learning and Mahalanobis distance. Background Technology

[0002] Network intrusion detection systems are one of the core technologies for ensuring cybersecurity. Their goal is to identify malicious attacks from massive amounts of network traffic in a timely and accurate manner, assisting security operations personnel in quickly locating and handling threats. As network attack methods continue to evolve, attack traffic is becoming increasingly complex in terms of characteristics, and new types of attacks that did not appear during the training phase are constantly emerging, placing higher demands on the accuracy and rejection capabilities of identification methods.

[0003] Currently, network attack identification mainly employs supervised learning-based classification methods. These methods train multi-layer neural networks using labeled traffic data, exhibiting good discriminative ability against known attack categories. However, they suffer from two prominent problems. First, network traffic data suffers from severe class imbalance. The sample size of normal traffic and common attacks is often thousands of times larger than that of rare attacks. During training, the model is dominated by the large sample class, resulting in a significant deficiency in discriminative ability against the small sample attack class. Existing methods often use the inverse of the sample size as the class weight, but this weight range is too large when the sample size differs by thousands of times, easily leading to training divergence or model collapse. Oversampling to supplement rare class samples can easily introduce synthetic noise and disrupt the original feature distribution. Second, classifiers trained based on cross-entropy are based on the closed-set assumption. Their normalized exponential function output must be normalized to each known class, thus forcing any input into a known class. When encountering traffic that did not appear during the training phase, the model will still output a known class label, lacking the ability to reject such traffic.

[0004] To address the problems arising from the closed-set assumption, existing technologies extract traffic features through contrastive learning, then cluster normal traffic features to obtain feature clusters. Anomaly detection is then performed by calculating the Euclidean distance between the feature to be detected and the cluster center. This approach has the following limitations: The Euclidean distance treats all directions in the feature space equally, failing to consider the shape and dispersion of each category's distribution. For traffic that is close to a category center in a direction but falls outside the actual distribution range of that category, its Euclidean distance is still small, leading to false negatives. Typically, these methods use a self-supervised approach to train the encoder, where positive sample pairs consist of augmented views of the same sample. The encoder only learns invariance to augmentation transformations and fails to utilize existing category label information, resulting in limited inter-class separability in the obtained feature space. Furthermore, these approaches establish only a single baseline based on normal traffic, distinguishing only between normal and abnormal states, and cannot simultaneously identify the specific category of a known attack. These factors collectively contribute to the low accuracy and rejection capability of existing network attack identification methods. Summary of the Invention

[0005] To address the problems of existing technologies, this invention proposes an open set network attack identification method that integrates supervised contrastive learning and Mahalanobis distance. Supervised contrastive learning constructs a discriminative feature space that is compact within classes and separate between classes. Mahalanobis distance is then used in this feature space to perform anomaly detection based on distribution shape. This improves the accuracy of identifying known attack categories while also enabling the rejection of abnormal traffic that does not belong to any known category. This solves the problems of insufficient identification of small-sample attacks under class imbalance and the lack of anomaly rejection capability in closed-set classifiers.

[0006] An open-set network attack identification method that integrates supervised contrastive learning and Mahalanobis distance, comprising:

[0007] A neural network comprising an encoder, a projection head, and a classification head is constructed, enabling the network traffic feature space output by the encoder to simultaneously serve known category discrimination and open set anomaly detection. The encoder consists of multiple cascaded encoding blocks, with the end mapping the block outputs to a predetermined feature dimension and projecting them onto a unit hypersphere after L2 normalization, yielding a network flow feature representation shared by supervised contrastive learning and Mahalanobis distance calculation. The projection head further maps the encoder output features to a lower-dimensional contrastive space and performs L2 normalization again. The classification head maps the encoder features to logical values ​​for each known category, used for determining known attack categories.

[0008] A balanced batch sampling strategy is adopted to construct batches from the training set, with samples of the same class as positive sample pairs and samples of different classes as negative sample pairs. The features are mapped to a low-dimensional space through a projection head, and the supervised contrast loss is calculated and minimized, so that traffic samples of the same attack class are clustered together and different attack classes are far apart. After training to convergence, the projection head is discarded to obtain the pre-trained encoder weights.

[0009] Load the pre-trained encoder weights and connect them to the classification head. Use the reciprocal of the square root of the number of samples in each known category as the initial category weights. Substitute the category weights into the focusing loss to perform supervised fine-tuning of the parameters of the entire network. After training until convergence, the target recognition model is obtained.

[0010] The fine-tuned encoder extracts features from samples of each known category in the training set. The class center vector and the intra-class covariance matrix are calculated for each category. For known categories with more samples than the feature dimension, the inverse of the intra-class covariance matrix is ​​obtained by superimposing a regularization term on the main diagonal. For known categories with fewer samples than the feature dimension, the inverse of the covariance matrix is ​​taken as the identity matrix. Finally, the Mahalanobis distance detector is constructed using the class center vector and the inverse of the covariance matrix.

[0011] For network traffic samples to be identified, after the encoder extracts features, the classification head provides candidate categories, and the Mahalanobis distance anomaly detector calculates anomaly scores. When the anomaly score exceeds the judgment threshold, it is determined to be abnormal traffic; otherwise, the candidate category is output as the identification result.

[0012] The present invention has the following beneficial effects:

[0013] 1. By using supervised contrastive learning to pre-train the encoder, the model already possesses a discriminative feature space for class clustering and dissimilar class separation before entering the classification task. This significantly improves the ability to identify small sample attack categories and provides a good feature foundation for subsequent distance-based anomaly detection.

[0014] 2. Mahalanobis distance is used as the anomaly criterion. Compared with the confidence criterion that only relies on the logical output of the classifier, Mahalanobis distance takes into account both the distribution center and distribution shape of each known category. It can identify abnormal traffic that is close to the known category in the feature direction but falls outside the distribution of the known category. This effectively reduces the situation where abnormal traffic is misclassified as the known category and improves the accuracy of anomaly identification.

[0015] 3. By combining the reciprocal of the square root class weights with rare class compensation and using focusing loss, the model can alleviate severe class imbalance while avoiding training instability caused by weight explosion, thus achieving balanced recognition performance in both large and small classes. Attached Figure Description

[0016] Figure 1 This is a flowchart illustrating the open set network attack identification method that integrates supervised contrastive learning and Mahalanobis distance according to the present invention.

[0017] Figure 2 This is a schematic diagram of the neural network structure of the present invention;

[0018] Figure 3 This is a detailed flowchart illustrating the supervised contrastive learning pre-training process of the present invention.

[0019] Figure 4 This is a detailed flowchart illustrating the focus loss fine-tuning process of the present invention;

[0020] Figure 5 This is a detailed flowchart of the Mahalanobis distance anomaly detection method of the present invention. Detailed Implementation

[0021] The present invention will be further described below with reference to specific embodiments. It should be understood that the following embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit the scope of protection of the present invention. Equivalent substitutions, modifications, or combinations made by those skilled in the art without departing from the technical concept of the present invention should all fall within the scope of protection of the present invention.

[0022] Figure 1 shows a flowchart of the open set network attack identification method of the present invention, which integrates supervised contrastive learning and Mahalanobis distance. The method includes the following steps:

[0023] Step 1: This embodiment uses the CIC-IDS2017 network intrusion detection dataset as the raw data source. Considering the severe imbalance in network traffic data categories and the extreme scarcity of samples for some attack categories, the raw data is preprocessed as follows:

[0024] s1.1 Read the original traffic records, delete leaked fields related to the flow identifier, including source address, source port, destination address, destination port, timestamp, and other fields that may lead to tag leakage, and encode the tags as integers.

[0025] s1.2. Designate attack categories with extremely small sample sizes as anomalous categories. These anomalous categories only appear in the test set and do not participate in training. They are used to simulate anomalous traffic that does not belong to any known training categories. The remaining categories are used as known categories in training.

[0026] s1.3 Divide the known category data into training and test sets, fix the random number seed to ensure the repeatability of the experiment, and then standardize all features.

[0027] Step 2: In this embodiment, a neural network is constructed that includes an encoder, a projection head, and a classification head. The encoder consists of several cascaded coding blocks and a feature mapping layer, and outputs a feature vector that is L2 normalized.

[0028] Step 3: In this embodiment, the neural network constructed in Step 2 undergoes a two-stage representation learning process, as follows:

[0029] S3.1. A balanced batch sampling strategy is adopted to construct training batches, with samples of the same class as positive sample pairs and samples of different classes as negative sample pairs. After being mapped to a low-dimensional contrast space by a projection head, the supervised contrast loss is minimized. After training until convergence, the projection head is discarded to obtain the pre-trained encoder weights.

[0030] S3.2 Load the pre-trained encoder weights and connect them to the classification head. Calculate the class weights based on the number of samples in each known class and substitute them into the focusing loss. Perform supervised fine-tuning of the entire network parameters to obtain the target recognition model.

[0031] Step 4: In this embodiment, the encoder of the target recognition model is used to extract features from the known category samples in the training set, and the class center vector and the inverse matrix of the intra-class covariance are calculated for each class to construct the Mahalanobis distance detector.

[0032] Step 5: In this embodiment, network traffic samples to be identified are collected and input into the target identification model. The encoder outputs the target feature vector, and the classification head outputs the candidate category.

[0033] Step 6: In this embodiment, the Mahalanobis distance from the target feature vector to each known category is calculated, and the minimum value is taken as the anomaly score.

[0034] Step 7: Network traffic samples with anomaly scores greater than a preset threshold are determined to not belong to any known category, identified as abnormal traffic, and an alarm signal is generated. For network traffic samples with anomaly scores not greater than the threshold, the candidate category is output as its attack category identification result.

[0035] Figure 2 shows a schematic diagram of the neural network structure of the present invention. The encoder consists of multiple cascaded coding blocks and a feature mapping layer. Each coding block sequentially includes a linear layer, a batch normalization layer, an activation function, and a random deactivation layer. The processing procedure is as follows:

[0036]

[0037]

[0038] The processing procedures for the projection head and the classification head are as follows:

[0039]

[0040] For example, the input traffic feature dimension is 83, and the total number of coding blocks is... The output dimensions of the three coding blocks are 512, 256, and 128 respectively, with a random inactivation probability of 0.2. The activation function is a modified linear unit. The feature mapping layer maps the 128-dimensional code to a feature dimension of 128 and then performs L2 normalization to obtain the feature vector located on a 128-dimensional unit hypersphere. The first linear layer of the projection head is The second linear layer is Output a 64-dimensional contrast vector. The classification head is a single-layer linear layer. Output the logical values ​​of 8 known categories.

[0041] The separate structure of the encoder, projector, and classifier stems from the feature space requirements of downstream Mahalanobis distance detection. Classifiers trained based on cross-entropy or focusing loss only focus on the decision boundary; as long as a sample falls on the correct side of the boundary, its loss is small. They do not require similar samples to be compactly clustered in the feature space. Therefore, the distribution of each category in the feature space obtained from single-stage training may be loose, elongated, or even multimodal. In this case, characterizing the category distribution with the mean vector and covariance matrix will produce a significant bias, and the discriminative power of Mahalanobis distance will decrease accordingly. The supervised contrastive learning explicitly brings similar samples closer together and pushes dissimilar samples further apart, enabling each known category to form a well-structured distribution in the feature space that can be accurately characterized by the mean and covariance, thus providing a reliable metric basis for the Mahalanobis distance detector.

[0042] The projection head only participates in forward computation during the supervised contrastive learning phase and is discarded after this phase. The supervised contrastive loss imposes a strong distribution constraint on the network layer it directly affects. By setting the projection head, this constraint is applied to the projection head rather than directly to the encoder's output layer, allowing the encoder's output layer to retain a more general and complete representation. At the same time, since the projection head does not participate in computation during the inference phase, its introduction does not increase the computational overhead during deployment.

[0043] Figure 3 illustrates a detailed flowchart of the supervised contrastive learning pre-training process of the present invention, including: setting the number of samples per batch per class. Total number of batches in each round Iterate through all known categories and extract from each. There are 10 samples, of which the number of remaining usable samples is no less than 100. The categories were randomly selected without replacement, and the number of remaining usable samples was less than [number missing]. The sample index of each category is reshuffled and then sampled cyclically until complete; the sampling of all categories is combined to obtain a batch size of The training batches are processed; the supervised contrast loss is calculated after mapping the encoder and the projector head; the encoder and projector head weights are updated via backpropagation; the above process is repeated until the preset training rounds are reached, then the projector head is discarded and the encoder weights are saved.

[0044] For example, setting Total number of known categories Each training batch contains Each sample is set for each round. The training consists of 200 rounds of training in 500 batches. A certain training set contains only 589 samples for the botnet category. Assuming 16 samples are drawn per batch, one round of 500 batches requires 8000 samples. Therefore, the sample index for this category is reused approximately 13.6 times per round, ensuring that this rare category has 16 samples in each batch for comparative learning, thus forming a... There are only 0.14 positive sample pairs. If conventional random sampling is used, and the category accounts for about 0.1% of the training set, the expected number of samples for this category is less than 0.14 when the batch size is 128. The vast majority of batches do not contain samples of this category or contain only one sample, which cannot form a positive sample pair. Therefore, this rare attack category cannot participate in the contrastive learning.

[0045] The supervised comparison loss is calculated using the following formula:

[0046]

[0047] Anchor samples whose positive sample index set is empty are not included in the loss, and the supervised comparison loss is the average of all valid anchor samples.

[0048] For example, setting temperature overparameters A training batch contains 128 samples in total, with 16 samples from each of 8 categories. For an anchor sample belonging to the port scan category, its positive sample index set is... Includes the remaining 15 port scan samples within the same batch. It contains 127 samples excluding itself. Compared with self-supervised contrastive learning, the self-supervised approach... This example contains only one view obtained by data augmentation of the sample itself, with a positive sample pair of only one, and the positive sample pair only reflects the invariance under the augmentation transformation; while this embodiment... It contains 15 real samples of the same category, increasing the number of positive sample pairs by 15 times, and directly constrains different traffic samples of the same attack category to move closer to each other in the feature space, thereby significantly improving the inter-class separability of the feature space.

[0049] Figure 4 illustrates a detailed flowchart of the focusing loss fine-tuning of the present invention, including: loading pre-trained encoder weights and connecting the classification head; counting the number of samples for each known category in the training set; calculating the initial weights based on the reciprocal of the square root of the sample count; and using the minimum initial weights... The initial weights are pruned to a multiple of the maximum value; a compensation coefficient is applied to categories with fewer samples than the rare category threshold; the weights are normalized; the logical value is calculated during forward propagation; the focusing loss is calculated; the network parameters are updated during backpropagation; the above process is repeated until the preset number of training rounds is reached, and the target recognition model is output.

[0050] The category weights are calculated using the following formula:

[0051]

[0052] For example, setting the maximum weighting ratio Rare category threshold Rare category compensation coefficient Number of normal traffic category samples in a certain training set Number of samples by botnet category If the weights are calculated using the reciprocal of the sample size, then the ratio of the weights of the two classes is... However, by using the reciprocal of the square root of the sample size, the ratio of the two class weights decreased to... The weight span is compressed to approximately one-sixteenth, effectively avoiding drastic gradient fluctuations caused by excessively large weights for rare classes. Furthermore, the class with the smallest weight corresponds to the class with the largest number of samples; let it be... Then the upper limit of weight For categories with fewer than 15 samples, the weights are pruned to the upper limit to prevent extreme weights; for categories with fewer than 100 samples, the weights are multiplied by a compensation coefficient of 1.5 for mild compensation; finally, normalization is performed so that the mean of all weights is 1, ensuring that the loss scale remains stable under different dataset configurations.

[0053] The focusing loss is calculated using the following formula:

[0054]

[0055] For example, setting focus parameters For an easily distinguishable sample, the model predicts the probability of its true class. Then the modulation factor The loss contribution of this sample is compressed to one ten-thousandth of the weighted cross-entropy; for a hard-to-separate sample, Then the modulation factor Its loss contribution is close to half of the weighted cross-entropy. Therefore, the gradient during training mainly comes from samples that are difficult to distinguish, and is related to the class weights. The combined effect enables the model to achieve balanced discriminative performance across classes with sample sizes that differ by hundreds of times.

[0056] Figure 5 illustrates a detailed flowchart of the Mahalanobis distance detection process of the present invention, including: extracting L2 normalized features from known category samples in the training set using the encoder of the target recognition model; calculating the class center vector and the intra-class covariance matrix for each category; for categories with more samples than the feature dimension, inverting the covariance matrix by superimposing regularization terms on the main diagonal of the covariance matrix to obtain the inverse covariance matrix; for categories with fewer samples than the feature dimension, taking the inverse covariance matrix as the identity matrix; using the class center vector and the inverse covariance matrix as detector parameters; calculating the Mahalanobis distance to each known category after extracting features from the sample to be identified and taking the minimum value as the anomaly score; comparing the anomaly score with the judgment threshold to output the anomaly judgment result or the known attack category.

[0057] The Mahalanobis distance detector is fitted and inferred using the following formula:

[0058]

[0059]

[0060] For example, setting the feature dimension to 128 and the regularization coefficient... For the distributed denial-of-service category with 76,115 samples in the training set, the number of samples far exceeds the feature dimension of 128, allowing for the estimation of a full-rank covariance matrix. Therefore, the covariance matrix can be superimposed on the main diagonal. The inverse is obtained by taking the inverse. The regularization term is used to ensure the invertibility of the covariance matrix and suppress numerical ill-conditioning. For categories with fewer than 129 samples, the sample size is insufficient to estimate... Forcibly inverting the full-rank covariance matrix will yield numerically unstable results. Therefore, we take its covariance inverse as the identity matrix. In this case, the Mahalanobis distance degenerates into the Euclidean distance, ensuring that the method is still usable on rare classes.

[0061] Mahalanobis distance introduces an inverse covariance matrix to adaptively scale the distance metric along the principal directions of each known class distribution. If the feature distribution of a known class is a narrow ellipsoid in the feature space, then the variance of that class is large along the major axis and small along the minor axis. Euclidean distance treats all directions equally; a sample deviating from the class center along the minor axis may still have a small Euclidean distance. Mahalanobis distance, however, applies a larger scaling factor to the minor axis through the inverse covariance matrix, significantly increasing the Mahalanobis distance of the sample, thus correctly identifying it as falling outside the class distribution.

[0062] For example, in experiments using the CIC-IDS2017 dataset, the direction of web attack traffic in the feature space is close to the center of the file transfer protocol brute-force category, both exhibiting high-frequency short-connection brute-force attempts. When using the maximum normalized exponential probability or energy score as the confidence criterion, the classifier still assigns a high maximum probability to this web attack traffic, leading to misclassification as a known category. Using the Euclidean distance criterion, because the traffic is close to the class center in direction, its Euclidean distance is smaller than the maximum distribution radius of that category, resulting in false negatives. However, when using the Mahalanobis distance criterion of this invention, although the web attack traffic is close to the file transfer protocol brute-force category in direction, it significantly deviates from the covariance ellipsoid of that category in several low-variance dimensions, resulting in a significantly increased Mahalanobis distance, thus correctly identifying it as abnormal traffic.

[0063] The judgment threshold is obtained by searching on the verification data: several candidate thresholds are selected at equal intervals between the minimum and maximum values ​​of the anomaly scores, and a false alarm rate constraint sequence consisting of several false alarm rate upper limits is set; for each false alarm rate upper limit, a comprehensive index is calculated from the candidate thresholds whose false alarm rate does not exceed the upper limit using the following formula:

[0064]

[0065] After iterating through all false positive rate caps, take The largest candidate threshold is used as the decision threshold; when the ratio of abnormal samples to known class samples is extremely unbalanced, causing the overall index of all candidate thresholds to be zero, the threshold is changed to the one that maximizes recall under the most lenient upper limit constraint on false positive rate. The largest candidate threshold is used as the decision threshold.

[0066] For example, setting the number of candidate thresholds to 500, and the false alarm rate constraint sequence as follows: In one experiment, the median score of the known category samples was 4.02, and the 95th percentile was 9.20. The 5th percentile score of the abnormal samples was 6.61, and the median was 9.82. This shows that the lower bound of the abnormal sample score exceeded the median score of the known category samples. Under the constraint of a 5% upper limit for the false positive rate, the resulting decision threshold was 9.76. The recall fallback strategy is used to address scenarios with extremely few abnormal samples: when there are only a few hundred abnormal samples in the test set while there are hundreds of thousands of known category samples, even with a false positive rate as low as 4%, the absolute number of false positives can still reach tens of thousands, leading to a decrease in precision and recall. If the value approaches zero, it will be impossible to select an effective threshold without a backoff strategy. Therefore, the threshold is selected based on the recall rate.

[0067] To evaluate the method's ability to identify abnormal traffic, the area under the receiver operating characteristic (ROC) curve is used as the primary evaluation metric. This metric is independent of the decision threshold and reflects the ability of the anomaly score to rank known category samples against abnormal samples. The false positive rate (false positive rate) at a true positive rate of 95% is used as a secondary evaluation metric. These two metrics constitute a standard evaluation combination in the field of open-set anomaly detection, objectively reflecting the detector's discriminative ability even under conditions of extreme class imbalance.

[0068] For example, on the CIC-IDS2017 dataset, the known categories were set as normal traffic, botnets, distributed denial-of-service, denial-of-service, FTP brute-force attacks, port scanning, port scanning after penetration, and SSH brute-force attacks, totaling eight categories. Web attacks, penetration attacks, and heartbleed attacks were designated as anomalous categories and placed only in the test set. Under the same encoder structure and training configuration, when using a criterion that integrates energy score and cosine distance, the area under the receiver operating characteristic (AUC) curve was 0.8648. After replacing the anomalous criterion with the Mahalanobis distance criterion of this invention, this metric improved to 0.9390, and the macro-average F1 score for known categories remained above 0.98. These results demonstrate that this invention, by introducing intra-class covariance, significantly improves the ability to identify anomalous traffic without compromising the performance of known categories.

[0069] The method, while outputting the anomaly determination result, retains the candidate categories given by the classification header, thus possessing two capabilities within the same framework: for traffic determined to be non-anomaly, it outputs the specific known attack category to which it belongs, achieving fine-grained multi-category identification; for traffic determined to be anomaly, it outputs anomaly alarms without forcibly classifying it into any known category, achieving open set rejection. Compared with solutions that only establish a single baseline based on normal traffic and can only distinguish between normal and anomaly states, this embodiment can provide security operations personnel with more specific handling criteria.

[0070] The embodiments of the present invention have been described in detail above with reference to the accompanying drawings, but this does not constitute a limitation on the scope of protection of the present invention. For those skilled in the art, various changes, modifications, substitutions, and variations can be made to these embodiments, including components, without departing from the principles and spirit of the present invention, and these variations still fall within the scope of protection of the present invention.

Claims

1. A method for identifying open-set network attacks that integrates supervised contrastive learning and Mahalanobis distance, characterized in that, include: A neural network comprising an encoder, a projection head, and a classification head is constructed. The network traffic feature space output by the encoder is used simultaneously for known category discrimination and open set anomaly detection. The encoder consists of multiple cascaded encoding blocks and a feature mapping layer. The feature mapping layer maps the output of the encoding blocks to a preset feature dimension, performs L2 normalization, and projects it onto a unit hypersphere to obtain a network flow feature representation shared by supervised contrastive learning and Mahalanobis distance calculation. The projection head further maps the features output by the encoder to a lower-dimensional contrastive space and performs L2 normalization again. The classification head maps the encoder features to logical values ​​of each known category for determining known attack categories. A balanced batch sampling strategy is used to construct training batches, ensuring that the number of samples of each known category is equal in each training batch. Samples of the same category within the same training batch are paired as positive samples, and samples of different categories are paired as negative samples. After being mapped to a low-dimensional contrast space by the encoder and the projector, the supervised contrast loss is calculated. The weights of the encoder and the projector are iteratively updated. After training until convergence, the projector is discarded to obtain the pre-trained encoder weights. The pre-trained encoder weights are loaded and connected to the classification head. The class weights are calculated based on the number of samples of each known class in the training set. The class weights are substituted into the focusing loss to perform supervised fine-tuning of the parameters of the entire network. After training until convergence, the target recognition model is obtained. The encoder of the target recognition model extracts features from samples of each known category in the training set. The class center vector and the intra-class covariance matrix are calculated for each category. For known categories with more samples than the feature dimension, a regularization term is superimposed on the main diagonal of the intra-class covariance matrix and then inverted to obtain the inverse covariance matrix. For known categories with fewer samples than the feature dimension, the inverse covariance matrix is ​​taken as the identity matrix. The class center vector and the inverse covariance matrix are used to form a Mahalanobis distance detector. The network traffic samples to be identified are collected and input into the target identification model, the encoder outputs the target feature vector, and the classification head outputs the candidate category; Based on the class center vector and the covariance inverse matrix, calculate the Mahalanobis distance from the target feature vector to each known class, and determine the minimum Mahalanobis distance among all known classes as the anomaly score; The anomaly score is compared with a preset judgment threshold. Network traffic samples with an anomaly score greater than the judgment threshold are judged as not belonging to any known category, identified as abnormal traffic, and an alarm signal is generated. For network traffic samples with an anomaly score not greater than the judgment threshold, the candidate category is output as its attack category identification result.

2. The open set network attack identification method integrating supervised contrastive learning and Mahalanobis distance as described in claim 1, characterized in that, The encoder processes the input samples as follows: in, This represents the input flow feature vector. Indicates the hierarchical index of the encoded block. Indicates the total number of coded blocks. Indicates the first The output of each coded block, and They represent the first The weight matrix and bias vector of the linear layer in each coding block Indicates batch normalization, This represents the activation function. Indicates random inactivation. This represents the output of the feature mapping layer. This represents the eigenvectors located on the unit hypersphere after L2 normalization. Represents the L2 norm; The projection head on the feature vector The processing procedure is as follows: in, , and , These represent the weight matrix and bias vector of the first linear layer and the second linear layer, respectively. Represents the contrast vector in a low-dimensional contrast space; The classification head pairs the feature vector The processing procedure is as follows: in, The weight matrix and bias vector of the classification head are represented. This represents the total number of known categories. Represents the logical values ​​of each known category without being processed by a normalized exponential function; The projection head only participates in forward computation during the supervised contrastive learning stage, so that the constraint of the supervised contrastive loss on the feature distribution acts on the projection head rather than directly on the output layer of the encoder, thus preserving the universality of the encoder's output features; the L2 normalization makes all feature vectors lie on the same unit hypersphere, eliminating the influence of feature magnitude on the metric, making the inner product operation in the supervised contrastive loss equivalent to cosine similarity, and making the Mahalanobis distance calculated on a feature space of a uniform scale.

3. The open set network attack identification method integrating supervised contrastive learning and Mahalanobis distance as described in claim 1, characterized in that, The balanced batch sampling strategy includes: Set the number of samples for each known class in each training batch to be [number]. The total number of batches in each training round is Then the batch size of each training batch is ,in The total number of known categories; For each training batch, iterate through all... Each of the known categories is extracted. There are 10 samples, of which the number of remaining usable samples is no less than 100. The known categories were randomly selected without replacement, and the number of remaining usable samples was less than [number missing]. The known categories are reshuffled with their sample indices and then repeatedly sampled until complete. This ensures that the few-sample attack class receives the same number of occurrences as the large-sample class in each training batch, and that each known class is represented within each training batch. One positive sample pair.

4. The open set network attack identification method integrating supervised contrastive learning and Mahalanobis distance according to claim 3, characterized in that, The supervised comparison loss is calculated using the following formula: in, This represents the set of indices for all samples in the current training batch. Indicates the index of the anchor sample. This represents the set of all sample indices within the training batch, excluding the anchor sample itself. This represents the set of positive sample indices within the training batch that have the same class label as the anchor sample. This indicates the number of elements in the positive sample index set. Indicates that the index is The comparison vector output by the projection head of the sample. Indicates temperature hyperparameter; The positive sample index set The category labels are directly determined, rather than being composed of views obtained from the same sample through data augmentation. This allows the encoder to learn not the invariance to the augmentation transformation, but the commonalities between different traffic samples of the same attack category. As a result, the known categories in the feature space form a compact distribution within each category and a separation between each category. Anchor samples whose positive sample index set is an empty set are not included in the supervised contrast loss. The supervised contrast loss is the average of all valid anchor samples.

5. The open set network attack identification method integrating supervised contrastive learning and Mahalanobis distance according to claim 1, characterized in that, The calculation of the category weights includes: Statistics of known categories Number of samples in the training set Using the reciprocal of the square root of the sample size as the initial weights, the weight span under extreme class imbalance conditions is compressed: With minimum initial weight The initial weights are pruned using a multiple as the upper limit for weights, ensuring that the ratio of the maximum weight to the minimum weight does not exceed a certain value. To prevent excessive weighting of rare classes from causing training divergence: For samples with a number less than a preset rare category threshold Given the known categories, multiply their pruned weights by the rare category compensation coefficient. Compensation shall be provided for samples with a sample size of not less than [number missing]. The known categories retain their clipped weights; The weights of all known categories are normalized to a mean of 1 to stabilize the loss scale. 。 6. The open set network attack identification method integrating supervised contrastive learning and Mahalanobis distance according to claim 5, characterized in that, The focusing loss is calculated using the following formula: in, This indicates the number of samples in the current batch. Indicates the first The sample at the th Logical values ​​for known categories, The model represents the first True category of each sample The predicted probability, This represents the class weight of the true class obtained by the method described in claim 5. Indicates the focus parameter; The modulation factor The probability approaches zero as the prediction probability increases and approaches 1 as the prediction probability decreases, thereby reducing the loss contribution of easily classified samples and increasing the loss contribution of difficult-to-classify samples, which, together with the class weights, alleviates class imbalance.

7. The open set network attack identification method integrating supervised contrastive learning and Mahalanobis distance according to claim 1, characterized in that, The class center vector, the intra-class covariance matrix, and the Mahalanobis distance are calculated using the following formulas: in, Indicates the first The feature vector output by the encoder for each training sample Indicates the first Class center vectors of known categories, Indicates the first The within-class covariance matrix of known categories. Denotes the regularization coefficients used to ensure matrix invertibility and suppress numerical ill-conditioning. Represents the identity matrix. Denotes the inverse covariance matrix. Indicates the features of the sample to be identified To the Mahalanobis distance for known categories, Indicates abnormal scores; The Mahalanobis distance incorporates the inverse covariance matrix during its calculation. By applying a larger scaling factor to the direction with smaller variance in each known category distribution, samples that deviate from the class center along the low variance direction obtain larger distance values, thereby identifying network traffic samples that are close to a certain known category in the feature direction but fall outside the shape of the known category distribution.

8. The open set network attack identification method integrating supervised contrastive learning and Mahalanobis distance according to claim 1, characterized in that, The determination threshold is obtained by searching on the verification data, including: Several candidate thresholds are selected at equal intervals between the minimum and maximum values ​​of the anomaly scores of all validation samples, and a false alarm rate constraint sequence consisting of several false alarm rate upper limits is set. For each false positive rate cap, the proportion of samples with anomaly scores greater than the candidate threshold in the known category validation samples is taken as the false positive rate under that candidate threshold. Within the candidate threshold where the false positive rate does not exceed the cap, the comprehensive anomaly identification index is calculated using the following formula: in, This indicates the number of abnormal samples whose anomaly scores are greater than the candidate threshold. This indicates the number of abnormal samples whose abnormal scores are not greater than the candidate threshold. This indicates the number of known category samples whose anomaly scores are greater than the candidate threshold; After iterating through all false positive rate limits, the aforementioned comprehensive indicator will be... The candidate threshold that yields the maximum value is determined as the judgment threshold; when the ratio of abnormal samples to known class samples is extremely unbalanced, causing the overall index of all candidate thresholds to be zero, the threshold that maximizes recall is selected under the most lenient upper limit constraint on false positive rate. The candidate threshold that yields the maximum value is used as the decision threshold.

9. A computer-readable storage medium having a computer program stored thereon, which, when executed in a computer, causes the computer to perform the method of any one of claims 1 to 8.