Cross-Tor version domain small sample website fingerprint identification method based on comparative learning
By employing a pre-training and fine-tuning strategy based on contrastive learning, combined with convolutional feature extraction and traffic attention modules, the problem of insufficient accuracy and generalization ability in cross-version domain few-sample website fingerprint recognition in Tor networks is solved, achieving efficient recognition in different Tor network environments.
Patent Information
- Application Number
- CN202511406752.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-29
- Publication Date
- 2026-01-20
AI Technical Summary
Existing technologies for cross-version domain small-sample website fingerprinting in the Tor network suffer from insufficient accuracy and generalization ability, especially due to poor model adaptability caused by differences in Tor browser user configurations and changes in network conditions.
We employ a pre-training strategy based on contrastive learning, combining a convolutional feature extractor with a flow attention module. We design a fine-tuning strategy with dual-branch confidence alignment and a prototype-driven classification strategy. By combining supervised contrastive learning with cross-entropy loss and weighted contrastive loss, we enhance feature discriminativeness and semantic consistency, achieving cross-domain transferability.
With very limited sample data, the model achieves accurate identification of target websites in different versions of the Tor network environment, improving the model's adaptability and recognition accuracy, and is applicable to both closed and open world scenarios.
Smart Images

Figure CN121365292A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of network communication security and privacy protection, and is particularly suitable for small sample website fingerprint identification tasks across version domains in a Tor network. The present application can be applied to network management, malicious activity detection and anonymous communication traffic analysis, and provides technical support for enhancing security evaluation and countermeasure protection of anonymous communication systems. BACKGROUND
[0002] With the continuous development of Internet technology, users' demand for privacy protection and anonymous communication is increasing. Tor (The Onion Router) is one of the most widely used anonymous communication systems at present, which effectively hides the real identity and communication path of users through multi-layer encryption, random path selection and node relay. However, Tor network is not absolutely secure. Although the communication content is encrypted, there is still a risk that the traffic features will be analyzed and utilized. Among them, website fingerprint identification has become an important attack means against Tor network. Website fingerprint identification does not directly decrypt the communication content, but extracts and models the features of encrypted traffic, and analyzes them with the help of a classification model, so as to infer the category of the website visited by the user, which has important significance in the security evaluation and countermeasure research of anonymous communication systems.
[0003] In recent years, artificial intelligence technology has been widely introduced into the field of website fingerprint identification research. Early research methods are usually based on machine learning framework, which extracts statistical features from network traffic manually, such as packet size direction, time interval, etc., and then uses traditional classifiers for training and identification. With the development of deep learning, researchers further apply it to website fingerprint identification tasks. The method based on deep learning can automatically extract features from traffic data and learn potential patterns, thus avoiding the shortcomings of traditional methods relying on manually designed features. However, existing methods usually require a large amount of training data, which not only brings huge collection and labeling costs, but also is difficult to maintain in the long term in actual application. On the other hand, existing methods usually imply a premise assumption that the training data and test data come from the same network environment. However, in actual application scenarios, this assumption is often difficult to meet. Due to the differences in Tor browser user configuration, changes in network conditions and updates in website content, etc., the Tor network traffic pattern will drift significantly over time and environment. Influenced by this, the model trained based on source domain data will have decreased recognition performance when migrated to new network conditions, thus resulting in poor model adaptability and difficulty in maintaining stable recognition effect in real environment.
[0004] To this end, the academic community proposes the research direction of cross-domain small sample website fingerprint identification, that is, in the pre-training stage, the source domain data is used, and in the fine-tuning stage, only a small number of labeled samples of the target domain are relied on to realize model migration. Existing research mainly focuses on two technical paths: one is to generate additional samples through data enhancement to alleviate the shortage of small samples, but this method is difficult to cover the complex and variable traffic distribution, resulting in limited generalization performance of the model in the target domain; the second is to reduce the difference between the source domain and the target domain through feature alignment, but often ignores the preservation of the class semantic structure, thereby affecting the identification accuracy. Therefore, the existing method still has the problems of insufficient accuracy and generalization ability in the cross-version domain small sample scene.
[0005] In view of the above problems, the present application proposes a cross-Tor version domain small sample website fingerprint identification method based on contrast learning. Compared with the existing research, the innovation points of the present application mainly lie in three aspects: first, a pre-training strategy based on supervised contrast learning is proposed, a convolution feature extractor and a traffic attention module are combined to construct a representation space with cross-domain migration ability, thereby effectively enhancing the discriminability of the features; second, a fine-tuning strategy of double-branch confidence alignment is designed, based on the freezing of part of the structure, the cross-entropy loss and the weighted contrast loss are optimized jointly, the semantic consistency of the target domain is maintained and the model adaptability is improved; finally, a prototype-driven classification strategy is introduced, which can realize high-precision website identification in both closed world and open world scenarios by using class prototypes, thereby enhancing the cross-version domain generalization ability under small sample conditions. SUMMARY
[0006] The present application aims at the challenges existing in the existing small sample website fingerprint identification method, through in-depth analysis of the existing technology and research method, a cross-Tor version domain small sample website fingerprint identification method based on contrast learning is proposed to realize accurate identification of the target website in different versions of Tor network environment through very few sample data.
[0007] In order to achieve the above purpose, the technical scheme of the present application is as follows: a cross-Tor version domain small sample website fingerprint identification method based on contrast learning, as shown in Figure 1 The present application is divided into three steps: designing a pre-training strategy based on contrast learning, designing a fine-tuning strategy based on double-branch confidence alignment, and designing a prototype-driven traffic classification strategy.
[0008] S1: Design a pre-training strategy based on contrast learning. In the pre-training stage, the source domain traffic data is combined with the supervised contrast learning mechanism to learn fine-grained representations and provide a transferable feature basis for subsequent cross-domain tasks. This process is divided into four steps: traffic data preprocessing, designing a feature extractor, designing a traffic attention module, and pre-training the feature extractor.
[0009] S11: Traffic data preprocessing. The direction sequence is extracted from the original traffic data as the model input. This process is specifically divided into three steps: extracting bidirectional flow, extracting direction information, and adjusting the sequence.
[0010] (1) Extracting bidirectional flow. Read the original traffic file in Pcap format, and extract bidirectional flow according to the five-tuple: source IP address, destination IP address, source port number, destination port number, and protocol type.
[0011] (2) Extracting direction information. Analyze the data packets in the bidirectional flow one by one, and extract the direction information of each data packet.
[0012] (3) Adjusting the sequence. The data packet direction sequence is uniformly fixed to a length of L. Sequences longer than L will be truncated, and sequences shorter than L will be padded with zeros to ensure that all samples have the same length.
[0013] S12: Designing feature extractor. To effectively capture different levels of features in traffic data, a multi-layer convolutional module is built as a feature extractor using a convolutional neural network to enhance its feature expression ability. This process is specifically divided into three steps: designing the main convolutional layer, designing the depth separable convolution, and designing the residual connection.
[0014] (1) Designing the main convolutional layer. The main convolutional layer is composed of four bottleneck convolution modules stacked in turn. Each bottleneck convolution module is composed of a convolution layer, a batch normalization layer, a nonlinear activation function, and a pooling layer, which is used to extract higher-level semantic features step by step. The convolution kernel size is uniformly set to conv_ks, and the number of channels is expanded to fn, 2×fn, 4×fn, and 8×fn in different modules, respectively, so as to continuously enhance the feature expression ability in the process of level advancement. The dilation mechanism is introduced in the convolution structure, and the dilation rates of different modules are ds, 2×ds, 4×ds, and 8×ds, respectively, to ensure that the network can effectively expand the receptive field within a limited depth and capture long-range dependencies. A pooling layer is connected at the end of each module to further shorten the sequence length and extract higher-dimensional abstract features.
[0015] (2) Designing the depth separable convolution. In the bottleneck convolution module, the depth separable convolution structure is combined, which is specifically divided into three steps: compressing the number of channels, extracting sequence features, and restoring the number of channels.
[0016] a) Compressing the number of channels. In order to provide a more compact input feature representation, point-wise convolution is used to linearly transform the input features, compressing the number of channels to a lower dimension to reduce the subsequent computational overhead and promote cross-channel information interaction.
[0017] b) Extract sequence features. A one-dimensional deep convolutional structure is adopted, and the number of groups is set to be consistent with the number of channels, so that each channel can independently complete the convolution operation to realize the extraction of sequence patterns channel by channel; at the same time, the dilation rate is introduced to expand the convolution receptive field under the condition of maintaining the calculation efficiency, so that the model can effectively perceive the flow pattern of multiple granularities and improve the representation ability of complex sequence features.
[0018] c) Restore the number of channels. The number of channels is restored to the original dimension by point-by-point convolution, and the expansion of the feature space is completed by combining the use of batch normalization and nonlinear activation function.
[0019] (3) Design residual connection. In order to alleviate the problem of gradient disappearance and effectively preserve low-level feature information, a shortcut branch is set up by point-by-point convolution combined with batch normalization operation, which maps the input features to the channel dimension consistent with the output of the main branch. Then the output obtained by the main branch convolution is added to the mapping result of the shortcut branch element by element to realize residual fusion, so as to enhance the training stability of the model.
[0020] S13: Design flow attention module. The flow attention module is designed to adaptively adjust the weights of different features to enhance the model's ability to capture key features. This process consists of three steps: aggregating features, generating channel attention, and weighted fusion.
[0021] (1) Aggregated features. As shown in equation (1), the output of the i-th convolutional block is where B represents the batch size, C i represents the number of channels. By global average pooling operation, the sequence dimension is compressed to obtain the global feature representation of the convolutional block in the channel dimension On this basis, as shown in equation (2), the global feature is linearly transformed and batch normalized to improve the stability of feature mapping, where BN i represents the normalization operation of the i-th convolutional block feature, W i and b i are the learning weights and bias of the i-th convolutional block. As shown in equation (3), the normalized features S i of each convolutional block are aggregated to form a multi-scale feature representation I FA , where n is the index of the convolutional block, q is the number of convolutional layers used, and the rich input is provided for subsequent attention modeling.
[0022]
[0023]
[0024] (2) Generate channel attention. As shown in equation (4), the integrated channel features are nonlinearly transformed by a multilayer perceptron to generate the attention weight a of each channel. Wherein, σ is the Sigmoid activation function, and ReLU is the linear rectifier function.
[0025] a = σ (W2 ReLU (W1 I FA + b1) + b2) (4)
[0026] (3) Weighted fusion. Based on the generated attention weight, the feature map is weighted and fused. By element-wise multiplication of the feature map of each channel and the corresponding attention weight, the key semantic information is highlighted, the influence of important features is enhanced, and the influence of irrelevant features is reduced.
[0027] S14: Feature extractor pre-training. In order to obtain feature embedding representation with strong domain discrimination ability, the source domain traffic data is used for training, and the model performance is improved by optimizing the supervised contrastive learning loss function. The specific process includes 4 steps: calculating the supervised contrastive learning loss, selecting the hyperparameters, batch training and parameter updating, and weight saving.
[0028] (1) Calculate the supervised contrastive learning loss. In each training batch, the supervised contrastive learning loss is calculated using the source domain data constructed in step S11 to optimize the representation space. This process is divided into 3 steps: embedding generation, construction of positive and negative sample sets, and loss calculation.
[0029] a) Embedding generation. The source domain data is mapped to the contrastive embedding space by the feature extractor to obtain the embedding of the in-batch samples
[0030] b) Construction of positive and negative sample sets. For each anchor sample i in the batch, according to the class label, construct its positive sample index set P(i), and the samples of other classes as the negative sample index set N(i).
[0031] c) Loss calculation. As shown in equation (5), for each anchor sample embedding z i , the cosine similarity sim(z i ,z j ) is calculated with the sample embedding in its positive sample set P(i), and is scaled by the temperature coefficient τ to enhance the discriminability of the similarity distribution. Based on the scaled similarity, the pairwise loss is calculated, and the average loss of the anchor and its positive sample set is taken. The average loss of all anchors in the entire batch is summarized to form the final supervised contrastive loss.
[0032]
[0033] (2) Hyperparameter selection. A grid search method is used to perform multiple samplings within the pre-defined hyperparameter search space in Table 1. After evaluating the model performance each time, the optimal hyperparameter combination is selected for subsequent training to ensure that the model can achieve the best performance under different training conditions.
[0034] (3) Batch training and parameter updating. The model processes all source domain training samples in batches. In each training iteration, the loss function is calculated by forward propagation to evaluate the gap between the model output and the target. Based on the loss function, the gradient is calculated by the back propagation algorithm, and the model parameters are updated. The training process will continue until the loss value calculated on the validation set no longer decreases significantly, ensuring that the model performance reaches the best state.
[0035] (4) Weight saving. When the feature extractor training is completed, the model weight file obtained during the training process is saved to ensure that the model can continue to be used in the subsequent fine-tuning stage.
[0036] Table 1: Pre-training hyperparameter list
[0037]
[0038] This step avoids the limitations of existing methods that rely only on traditional supervised classification pre-training. By introducing a supervised contrastive learning mechanism, the discriminability and transferability of feature representation are improved, which is embodied as follows:
[0039] (1) Supervised contrastive learning mechanism. Constructing positive and negative samples to improve the discriminability and intra-class consistency of the representation space, effectively enhancing the cross-domain feature transfer ability.
[0040] (2) Hierarchical architecture design. Through the synergistic effect of multi-layer backbone convolution and attention mechanism, multi-granularity modeling of traffic data is realized, which not only captures global and local features, but also highlights key information, improving the model's expression ability for complex traffic patterns.
[0041] (3) Efficient training optimization. Through residual connection and depth separable convolution structure, the computational complexity of the model is reduced, and the gradient vanishing problem is alleviated, improving the training stability.
[0042] S2: Design a fine-tuning strategy based on dual-branch confidence alignment. As shown in Figure 2 , in the fine-tuning stage, a dual-branch confidence alignment (DCA) strategy is used to reduce the semantic drift between source domain data and target domain data. This process consists of three steps: local fine-tuning, calculating DCA composite loss function, and training and parameter updating.
[0043] S21: Local fine-tuning. To maximize the transferable structure representation obtained in the pre-training stage, the backbone feature extractor is frozen, and only the flow attention module is opened for optimization. This process is divided into three steps: model weight loading, freezing feature extractor parameters, and updating flow attention module parameters.
[0044] (1) Model weight loading. Load the pre-trained model weights saved in step S14 into the new model to retain the model's learning ability on the source domain data.
[0045] (2) Freezing feature extractor parameters. Set both the weights and bias terms of the feature extractor to be non-updatable to alleviate catastrophic forgetting and preserve the flow structure representation learned in the pre-training stage.
[0046] (3) Update flow attention module parameters. Calculate the loss by forward propagation and perform backpropagation to update only the trainable parameters in the flow attention module, ensuring that the model can effectively learn the key features of the target domain.
[0047] S22: Design DCA composite loss function. Use the DCA composite loss function to optimize the model to enhance website category discrimination and maintain structural consistency. The DCA loss includes cross-entropy loss and weighted contrastive loss The specific calculation process is divided into three steps: calculating the cross-entropy loss, calculating the weighted contrastive loss, and calculating the fusion loss.
[0048] (1) Calculate the cross-entropy loss. Calculate the classification accuracy of the target domain data to maximize the separability between categories. The calculation process of the cross-entropy loss is shown in formula (6), where N is the total number of samples in the batch, C is the total number of categories, y i is the true label of the sample, is the predicted probability of the sample.
[0049]
[0050] (2) Calculate the weighted contrastive loss. Maximize the similarity of samples of the same class and minimize the similarity of samples of different classes to improve the within-class consistency and between-class discrimination in the embedding space. This process is divided into three steps: determining the hardest positive sample, calculating the focal weight, and constructing the weighted loss.
[0051] a) Determine the hardest positive sample. The hardest positive sample is the positive sample with the smallest similarity to the anchor sample. For each sample z i , as shown in formula (7), determine its hardest positive sample
[0052]
[0053] b) Calculate focal weights. As shown in equation (8), the weights of difficult-to-distinguish samples are adjusted, where γ is the focal parameter, used to adjust the focus of the model on difficult samples. A larger γ value will increase the loss weight of low similarity sample pairs, so that the model pays more attention to difficult-to-distinguish samples during training.
[0054]
[0055] c) Build weighted loss. As shown in equation (9), in order to enhance the discriminant ability of the model to difficult-to-distinguish samples, the focal weight is combined with the supervised contrastive loss to improve the discriminability of the embedding space.
[0056]
[0057] (3) Calculate fusion loss. As shown in equation (10), the weight λ is adjusted to proportionally fuse the cross-entropy loss and the weighted contrastive loss to balance the optimization objectives of classification accuracy and feature structure preservation.
[0058]
[0059] S23: Training and parameter updating. Use the target domain dataset constructed in step S11 to train and update the traffic attention module in the model by optimizing the DCA loss function. This process is divided into four steps: dataset division, hyperparameter selection, batch training and parameter updating, and weight file saving.
[0060] (1) Dataset division. Divide the target domain samples into support set and query set, where the first s samples of each class are used as the support set and the last q samples are used as the query set.
[0061] (2) Hyperparameter selection. Within the hyperparameter search space listed in Table 2 for the fine-tuning stage, perform multiple rounds of sampling and evaluation, using the classification accuracy on the target domain query set as the evaluation metric, and select the optimal combination of hyperparameters as the configuration for subsequent training.
[0062] (3) Batch training and parameter updating. Forward propagate the target domain data through the model to calculate the DCA composite loss function. According to the total loss calculated, calculate the gradient through the backpropagation algorithm, and update the trainable parameters in the traffic attention module. Training continues until the loss function on the target domain query set stabilizes and does not decrease further, obtaining the best fine-tuned model.
[0063] (4) Weight file saving. Save the weight file obtained during the fine-tuning process to ensure that the model continues to be used in step S3.
[0064] Table 2 Fine-tuning hyperparameter list
[0065]
[0066] This step overcomes the catastrophic forgetting and semantic drift problems caused by directly updating the entire model in the fine-tuning stage of existing methods, and improves the adaptability of the model under small sample conditions through a double-branch confidence alignment mechanism, which is embodied as follows:
[0067] (1) Local fine-tuning mechanism. Freeze the backbone feature extractor and only update the traffic attention module to maximize the preservation of cross-domain transfer features.
[0068] (2) Double-branch confidence alignment loss. Combine cross-entropy and weighted contrastive loss, and introduce a focal mechanism to enhance the modeling ability of difficult-to-distinguish samples, thereby enhancing the generalization performance of the model.
[0069] S3: Design a prototype-driven traffic classification strategy. The prototype-driven classification method classifies the test data of the target domain by comparing it with the prototypes of the support set samples. This process is divided into three steps: generating prototypes, classifying test samples, and model testing.
[0070] S31: Generate prototypes. To perform accurate classification, the prototype representation of each website category needs to be calculated. This process is divided into three steps: obtaining embedding representation, calculating category prototype, and determining prototype radius.
[0071] (1) Obtain embedding representation. Use the feature extractor and projection head of the S23 fine-tuning stage to obtain the embedding representation of the target domain data with semantic features of each website category.
[0072] (2) Calculate category prototype. As shown in equation (11), the category prototype is determined by calculating the mean of the feature embedding of each support sample in the category, where is the embedding representation of all support samples in category w.
[0073]
[0074] (3) Determine the prototype radius. To ensure that the prototype has a suitable classification boundary in the embedding space, construct the radius of each category according to the characteristics of the website category. The specific process is divided into two steps: calculating the prototype radius and adjusting the prototype radius.
[0075] a) Calculate the prototype radius. Initialize a fixed radius r fixed as the unified category radius; when there are multiple support samples in each category, calculate the cosine distance of each sample to the prototype, and estimate the category radius through the median absolute deviation (MAD). The calculation of the prototype radius is shown in equation (12)
[0076] , where sim(z,cw represents the cosine similarity between sample z and class prototype c. w
[0077]
[0078] b) Adjusting prototype radius. Calculate the distance between each pair of class prototypes, and dynamically adjust the radius of each class according to the distance to ensure that the prototypes have enough separation within the embedding space and avoid overlap between classes. If there is overlap between the radius of two classes and the distance between their prototypes, adjust according to formula (13), where, and are the prototype radii of classes w i and w j , respectively, and d is the cosine distance between the two class prototypes.
[0079]
[0080] S32: Test sample classification. Use the fine-tuned model and the pre-computed class prototypes and class radii to classify the test sample. The specific process is divided into two steps: calculating the test sample embedding vector and using the prototype classification decision.
[0081] (1) Calculate the test sample embedding vector. Input the test sample into the model trained in the S23 fine-tuning stage, and after forward propagation, obtain its embedding vector.
[0082] (2) Use the prototype classification decision. Read the class prototypes and their corresponding radii saved in step S31, as shown in formula (14), calculate the distance between the test sample z t and each class prototype c w .
[0083] d t = 1 - sim(z t , c w ) (14)
[0084] a) Closed world scenario. For closed world multi-classification tasks, calculate the cosine distance between the test sample and all class prototypes, subtract the prototype radius corresponding to each class from the distance of each class to correct the discrimination boundary between classes, and finally find the minimum value in all corrected distances and the corresponding class as the predicted class of the test sample.
[0085] b) Open world scenario. For open world binary classification tasks, if the embedding vector of the test sample is greater than the preset threshold from the prototype closest to it, as shown in formula (15), it is considered that the sample belongs to the non-target website. If the distance d t r w Then the sample is considered to belong to the target website. Wherein, r * is the prototype radius of the corresponding category, and ε is a preset tolerance threshold.
[0086] |d t -r * |<ε (15)
[0087] S33: Model testing. The results of the test samples are counted. The specific process is divided into two steps: selecting test indicators and counting test indicators.
[0088] (1) Select test indicators. Select appropriate evaluation indicators according to different scenarios. The closed world scenario mainly uses accuracy to measure the classification ability of the model to the target website category. The accuracy calculation is shown in formula (16), wherein TP (True Positive) represents the number of samples correctly classified as the target category, TN (True Negative) represents the number of samples correctly classified as the non-target category, FP (False Positive) represents the number of non-target samples incorrectly classified as the target category, and FN (False Negative) represents the number of target samples incorrectly classified as the non-target category. The open world scenario uses the area under the curve (AUC) as the evaluation indicator. AUC is calculated based on the receiver operating characteristic (ROC) curve, wherein the vertical axis is the true positive rate (TPR) and the horizontal axis is the false positive rate (FPR). The calculation of TPR and FPR is shown in formulas (17) and (18). By counting the number of TP, FP, TN, and TN of each category, TPR and FPR are calculated and the ROC curve is drawn, and then the AUC value is obtained by the area under the curve, so as to evaluate the distinguishing ability of the model to the target website category and the non-target website category in the open world scenario.
[0089]
[0090] (2) Count test indicators. Repeat the test multiple times using different random seeds, and count the selected indicators in the closed world scenario and the open world scenario respectively. The corresponding indicator values are recorded each time, and the average value of all test results is finally calculated to comprehensively evaluate the classification performance of the model under different conditions.
[0091] Compared with the traditional fixed classifier method, this step fully utilizes the semantic structure formed in the pre-training and fine-tuning stages, and realizes efficient recognition of cross-Tor version traffic through a prototype-driven classification mechanism. The specific manifestations are as follows:
[0092] (1) Prototype-driven classification mechanism. Based on support set samples, class prototypes are generated to effectively utilize existing semantic embeddings and improve classification stability and accuracy under small sample conditions.
[0093] (2) Adaptive adjustment of prototype radius. The inter-class distance adjustment mechanism is used to dynamically optimize the class boundary and reduce the risk of class overlap.
[0094] (3) Open world adaptability. Distance threshold discrimination is introduced in the classification process, which can handle closed world multi-classification tasks and distinguish target and non-target classes in open world scenarios.
[0095] Compared with the prior art, the advantages of the present application are as follows:
[0096] The present application proposes a cross-Tor version domain small sample website fingerprint identification method based on contrast learning. Compared with other small sample website fingerprint identification methods for Tor network, the advantages of the present application are as follows:
[0097] (1) The present application designs a pre-training strategy based on contrast learning. First, in step S11, the original traffic data is analyzed and preprocessed to obtain a normalized direction sequence; then in step S12, a backbone convolution layer is constructed to extract deep semantic features, thereby improving the feature discrimination ability; secondly, in step S13, the traffic attention mechanism is used to adaptively weight different features to highlight key semantic information; finally, in step S14, supervised contrast learning is combined to obtain a representation space with cross-version domain transferability.
[0098] (2) The present application designs a fine-tuning strategy based on double-branch confidence alignment. First, in step S21, a partial freezing strategy is adopted to update the parameters of the traffic attention module only, maximizing the structural stability in the pre-training phase; secondly, in step S22, the cross-entropy loss and the weighted contrast loss are fused into a composite loss function, effectively reducing the intra-class difference and expanding the inter-class interval, and strengthening the consistency of the class semantic distribution; finally, in step S23, fine-tuning training is used to realize rapid adaptation to the target domain, while preserving the class semantic structure and improving the cross-domain recognition performance.
[0099] (3) The present application designs a prototype-driven traffic classification strategy. In step S31, class prototypes are generated by support set samples to depict representative features of different website categories. At the same time, prototype radius is constructed and dynamically adjusted for each category to establish a distinguishing boundary and avoid class overlap; in step S32, the classification criteria defined by the prototype are used to fully utilize the class semantic embedding and realize accurate identification of website categories. BRIEF DESCRIPTION OF DRAWINGS
[0100] Figure 1Cross-version domain small sample website fingerprint identification method schematic diagram;
[0101] Figure 2 DCA composite loss function schematic diagram;
[0102] Figure 3 Flow preprocessing flowchart;
[0103] Figure 4 Hyperparameter selection flowchart;
[0104] Figure 5 Prototype generation flowchart;
[0105] Figure 6 Sample test flowchart. DETAILED DESCRIPTION
[0106] The technical solutions in the embodiments will be described in detail below with reference to the accompanying drawings of the present application. Obviously, the embodiments described below are only one of the methods of the present application, not all. Based on the following embodiments, all other embodiments obtained by those of ordinary skill in the art without creative labor shall fall within the scope of the present application.
[0107] The implementation process of the present application is as shown in Figure 1 A cross-Tor version domain small sample website fingerprint identification method based on contrast learning, which is divided into three main steps: implementing a pre-training strategy based on contrast learning, implementing a fine-tuning strategy based on double-branch confidence alignment, and implementing a prototype-driven traffic classification strategy.
[0108] The method proposed in the present patent is deployed on a set of device platforms with network communication function. The hardware configuration of the platform includes: Intel Core i9-12900K processor, 64GB DDR4 memory, NVIDIA GeForce RTX 309024GB graphics card, and 2TB SSD storage. The operating system is Ubuntu 24.04.2LTS. In terms of software environment, the platform runs Python 3.8.20 and is implemented based on the PyTorch 2.4.1 deep learning framework.
[0109] S1: Implement a pre-training strategy based on contrast learning. The implementation process is as shown in Figure 1 , which contains four steps: traffic data preprocessing, implementing a feature extractor, implementing a traffic attention module, and pre-training the feature extractor.
[0110] S11: Traffic data preprocessing. Write a Python script to parse and process the original traffic dataset in Table 3 by combining the Python traffic processing library Scapy and the SplitCap tool. The implementation process is as shown in Figure 3As shown, it is specifically divided into three steps: extracting bidirectional flow, extracting direction information, and adjusting sequence.
[0111] (1) Extracting bidirectional flow. This embodiment first calls the scapy.rdpcap() function to read the original Pcap data file and parses the data packet. Then, the SplitCap tool is used to divide the data packet into five tuples, thereby extracting the bidirectional flow. The five tuples include source IP address, source port, destination IP address, destination port, and protocol type.
[0112] (2) Extracting direction information. After the bidirectional flow is extracted, the data packets of each bidirectional flow are traversed. By calling the extract_direction() function, the flow direction of the data packet is determined according to the source IP address and the destination IP address. If the source IP address is the client IP, it is marked as "+1" indicating a sending data packet; if the source IP address is the IP address of the Tor proxy node, it is marked as "-1" indicating a receiving data packet.
[0113] (3) Adjusting sequence. To ensure the consistency of the sample dimension of the input model, the adjust_sequence() function is called for each direction sequence to normalize the length. Each data packet direction sequence is uniformly processed to a fixed length L = 5000. For sequences longer than 5000, they are truncated; for sequences shorter than 5000, they are padded to 5000 by zero. The processed data packet direction sequence is saved as a.npy file by calling save_directions(), which facilitates subsequent model input.
[0114] Table 3 Original data set
[0115]
[0116] S12: Implementing a feature extractor. In this embodiment, it is specifically divided into three steps: implementing a backbone convolutional layer, implementing a depth separable convolution, and implementing a residual connection.
[0117] (1) Implementing a backbone convolutional layer. Four bottleneck convolutional modules are stacked in turn, each consisting of nn.Conv1d(), nn.BatchNorm1d(), nn.functional.elu(), and nn.MaxPool1d(). After each module, the channel number is expanded to [32, 64, 128, 256] in turn, the convolution kernel size is uniformly set to 8, and the expansion rate is set to [1, 2, 4, 8] in nn.Conv1d().
[0118] (2) Implementing a depth separable convolution. This process is specifically divided into three steps: compressing the channel number, extracting sequence features, and restoring the channel number.
[0119] a) Compress the number of channels. Calling nn.Conv1d(kernel_size=1) implements point-wise convolution, which compresses the input feature channels, reduces the dimension, and reduces the subsequent computational complexity.
[0120] b) Extract sequence features. Calling nn.Conv1d() and setting groups equal to the number of input channels allows each channel to perform convolution independently. The depth convolution inherits the dilation rate [1, 2, 4, 8] set in the backbone convolution layer, ensuring that the internal convolution is consistent with the overall architecture, effectively capturing multi-scale context dependencies.
[0121] c) Restore the number of channels. Again, calling nn.Conv1d(kernel_size=1) performs point-wise convolution to restore the number of channels to the original dimension, and concatenating nn.BatchNorm1d() and nn.functional.elu() after the convolution layer completes the expansion of the feature space and non-linear enhancement.
[0122] (3) Implement residual connection. In each bottleneck convolution module, a shortcut branch is additionally constructed. This branch calls nn.Conv1d() with a kernel size of 1, a stride of 1, and a "same" padding method. The input feature map is mapped to the same channel dimension as the output of the main branch, and then the output of the main branch convolution is added element-wise to the result of the shortcut branch, forming a residual connection.
[0123] S13: Implement the flow attention module. In this embodiment, it is specifically divided into three steps: aggregating features, generating channel attention, and weighted fusion.
[0124] (1) Aggregate features. Calling F.adaptive_avg_pool1d() on the current layer's feature map performs global average pooling operation, compressing it into a fixed-length feature vector. Through nn.Linear(), the global feature vector is linearly transformed to a lower dimension to reduce computational complexity. Then, nn.BatchNorm1d() is called to normalize the mapped vector to improve numerical stability and suppress the scale difference between different channels.
[0125] (2) Generate channel attention. The intermediate representation of all input features is weighted and summed to obtain a fusion feature vector. This fusion vector is input to nn.Linear(), and then mapped to the [0, 1] interval through the nn.Sigmoid() activation function to obtain the attention weight coefficient corresponding to each channel, achieving adaptive modeling at the channel level.
[0126] (3) Weighted fusion. The input channels are weighted using att_weights attention weights. The feature representation of each channel is multiplied element-wise with the corresponding weight to strengthen the influence of key features and reduce the contribution of unimportant features.
[0127] S14: Feature Extractor Pre-training. This implementation process consists of four steps: calculating supervised contrastive learning loss, hyperparameter selection, batch training and parameter updating, and weight saving.
[0128] (1) Calculate the supervised contrastive learning loss. This process consists of three steps: embedding generation, constructing a set of positive and negative samples, and loss calculation.
[0129] a) Embedding generation. The model.forward() method is called to input the source domain data into the feature extractor, obtaining the embedding representation of each sample in the batch.
[0130] b) Construct positive and negative sample sets. Based on the batch label information, call the build_pos_neg_sets() function to compare the sample categories. Construct a positive sample set P(i) for samples belonging to the same category, and a negative sample set N(i) for other categories.
[0131] c) Loss Calculation. The `cosine_similarity()` function is called to calculate the cosine similarity between sample pairs. The similarity is scaled by a temperature coefficient τ, and the contrastive loss between each pair of positive samples and the anchor sample is calculated. Then, `torch.mean()` is used to average all losses within the batch to obtain the supervised contrastive learning loss.
[0132] (2) Hyperparameter selection. The implementation process is as follows: Figure 4 As shown in Table 1, within the predefined hyperparameter search range, a grid search strategy is used to systematically traverse all possible combinations of hyperparameters. For each hyperparameter combination, the corresponding model is trained, and the accuracy is evaluated and recorded using `accuracy_score()` on the validation set. The hyperparameter combination with the best performance is selected as the final model training configuration. The optimal hyperparameter combinations selected in this embodiment are shown in Table 4.
[0133] (3) Batch training and parameter update. Call load_source_data() to load training samples in batches, with 256 samples per batch input into the model for training. In each training iteration, the supervised contrastive learning loss function is calculated through forward propagation, then the gradient is calculated through backpropagation, and the model parameters are updated until the loss value of the model on the validation set stabilizes and no longer decreases.
[0134] (4) Weight saving. After the completion of the feature extractor training, the model weights are saved to the specified path using the torch.save() method.
[0135] Table 4 Pre-training hyperparameter search results
[0136]
[0137] S2: Implement the fine-tuning strategy based on the double-branch confidence alignment. This embodiment maintains the semantic consistency of the source domain data and the target domain data through the DCA composite loss function. The implementation process includes 3 steps: local fine-tuning, calculating the DCA composite loss function, training and parameter updating.
[0138] S21: Local fine-tuning. This implementation process is specifically divided into 3 steps: model weight loading, freezing feature extractor parameters, and updating flow attention module parameters.
[0139] (1) Model weight loading. The feature extractor weight file saved in step S14 is loaded into a new model instance through the torch.load() function, ensuring that the model can utilize the feature representation capabilities learned in the pre-training phase.
[0140] (2) Freezing feature extractor parameters. This embodiment calls the model.named_parameters() function to traverse all the parameters of the feature extractor. By checking the name of each parameter, it identifies the parameters belonging to the feature extractor and sets the requires_grad attribute of these parameters to False, thereby prohibiting the updating of these parameters during fine-tuning.
[0141] (3) Updating flow attention module parameters. In order to adjust only the parameters of the flow attention module in the fine-tuning phase, set param.requires_grad=True and set the parameters with "flow_attention" in the name to a trainable state.
[0142] S22: Calculate the DCA composite loss function. The DCA loss function combines the cross-entropy loss and the weighted contrastive loss, and improves the model performance by optimizing these two parts of the loss. This implementation process is specifically divided into 3 steps: calculating the cross-entropy loss, calculating the weighted contrastive loss, and calculating the fusion loss. Figure 2
[0143] (1) Calculate the cross-entropy loss. Initialize the loss function object by calling torch.nn.CrossEntropyLoss(), then call model.forward() to get the logits of the model output, pass logits and true labels into the loss function, and calculate the cross-entropy loss value
[0144] (2) Calculate the weighted contrastive loss. This process is specifically divided into 3 steps: determine the hardest positive sample, calculate the focal weighting, and construct the weighted loss.
[0145] a) Determine the hardest positive sample. Call cosine_similarity() to calculate the cosine similarity between sample pairs, and use torch.argmin() to select the positive sample with the lowest similarity to the anchor sample in the similarity list.
[0146] b) Calculate the focal weighting. Call torch.pow() to adjust the similarity difference to weight the loss weight of each pair of samples, and obtain the focal weight coefficient.
[0147] c) Construct the weighted loss. Call the supervised_contrastive_loss() function to calculate the supervised contrastive loss term and multiply it by the focal weight element by element. Then call torch.mean() to average the weighted loss of all anchor samples to form the final weighted contrastive loss function
[0148] (3) Calculate the fusion loss. The cross-entropy loss and the weighted contrastive loss are fused by the hyperparameter λ to calculate the composite loss function
[0149] S23: Training and parameter updating. Use the target domain dataset constructed in step S11 to fine-tune the model, thereby improving the model's adaptability to the target task. This implementation process is specifically divided into 4 steps: dataset division, hyperparameter selection, batch training and parameter updating, and weight file saving.
[0150] (1) Dataset division. In this embodiment, the samples of each class are divided into support set and query set. The first s
[0151] samples of each class are used as the support set, and the last q samples are used as the query set, where s is taken from {1, 5, 10, 15, 20} respectively, and q is fixed at 20.
[0152] (2) Hyperparameter selection. Within the predefined hyperparameter search space in Table 2, multiple rounds of sampling are performed and the performance of each round of model is evaluated, and the best hyperparameter combination is selected for training. In this embodiment, the selected optimal hyperparameter combination is shown in Table 5.
[0153] (3) Batch training and parameter updating. In this embodiment, the model contains two output heads: the projection head G c, which is constructed by sequentially calling nn.Linear(), nn.BatchNorm1d(), and nn.ReLU() layers; and the classifier head G y , which is implemented by calling nn.Linear(). At the beginning of training, the weights of the projection head and the classifier head are randomly initialized by calling torch.nn.init.xavier_uniform(). The target domain data is input into the model in batches by load_target_data(), and the forward propagation and the composite loss function are calculated. The parameters are updated iteratively by back propagation and optimizer until the loss on the query set converges.
[0154] (4) Weight file saving. After the model fine-tuning is completed, the model weight is saved to the specified path by calling the torch.save() method.
[0155] Table 5 Fine-tuning hyperparameter search results
[0156]
[0157] S3: Implement the prototype-driven traffic classification strategy. This implementation process includes 3 steps: generating prototypes, testing sample classification, and model testing.
[0158] S31: Generating prototypes. As shown in Figure 5 , this process specifically includes 3 steps: obtaining embedding representation, calculating class prototype, and determining prototype radius.
[0159] (1) Obtaining embedding representation. The feature extractor and projection head weight files saved in the fine-tuning stage of step S23 are loaded by calling torch.load(), and the model is placed in evaluation mode by executing model.eval(). Then the target domain data is input into the model, and the feature embedding representation is obtained by forward propagation, and is stored in the dictionary embs_pool according to the category index. The embedding space dimension is fixed at 128.
[0160] (2) Calculating class prototype. For each class, the embedding vector is converted to a matrix by calling np.array(), and the mean value is calculated along the sample dimension by np.mean() to obtain the prototype vector of the class. All class prototype vectors are stored in the array webs_centroid and used as the center point for subsequent classification.
[0161] (3) Determining prototype radius. This implementation process specifically includes 2 steps: calculating prototype radius and adjusting prototype radius.
[0162] a) Calculating prototype radius. In the 1-shot scenario, a fixed radius r fixedis 0.03. When the category contains multiple support samples, cosine_similarity() is called to calculate the similarity between each support sample and the prototype of this category, and is converted to the cosine distance. Then median_absolute_deviation() is called to calculate the radius r of the category w .
[0163] b) Adjust the prototype radius. cosine_similarity() is called to calculate the cosine similarity between different category prototypes, and is converted to the distance between prototypes; compare the distance with the sum of the corresponding category radius, if greater than the sum of the radius, keep it unchanged; if less than or equal to, it indicates that there is overlap between the two categories, by calculating the overlap between the two categories, and proportionally distributing to the radius of the two categories, so as to dynamically reduce the radius of each category, avoid the intersection of category discrimination boundary.
[0164] S32: Test sample classification. The implementation process is as shown in Figure 6 , and the specific process is divided into two steps: calculating the embedding vector of the test sample and using the prototype to make a classification decision.
[0165] (1) Calculate the embedding vector of the test sample. Use torch.load() to load the model weight obtained in step S23, and then call load_eval_data() to input the test sample into the model. After being mapped to the embedding space by the feature extractor and the projection head, the fixed 128-dimensional embedding vector is output as the representation of the test sample in the feature space.
[0166] (2) Use the prototype to make a classification decision. Call cosine_similarity() to calculate the cosine distance between the embedding vector z t of the test sample and the prototype c w of each category.
[0167] a) Closed world scenario. In the closed world scenario, the test sample can only be classified into a category of the target website set. The model will calculate the distance between the test sample and all category prototypes, and use np.argmin() to select the category corresponding to the minimum distance as the prediction result. If the distance is less than the prototype radius of the category, the sample is determined as the category.
[0168] b) Open world scenario. In the open world scenario, in addition to classifying into target website categories, it is also necessary to determine whether the test sample belongs to a non-target website category. The model first calculates the distance of each test sample from the nearest prototype. If the minimum distance is greater than the preset threshold value ε, the sample is marked as a non-target website set category by np.where(). If it does not exceed ε, the nearest category prototype index is found using np.argmin(), and the test sample is classified into the target website category. In this embodiment, the threshold value ε is set to 0.01.
[0169] S33: Model testing. The implementation process is specifically divided into two steps: selecting test indicators and statistical test indicators.
[0170] (1) Selecting test indicators. The method of the present application is evaluated in the closed world scenario and the open world scenario in this embodiment. The accuracy is used as the evaluation indicator in the closed world scenario, and the AUC value is used as the evaluation indicator in the open world scenario.
[0171] (2) Statistical test indicators. In this embodiment, repeated testing is performed by setting 5 different random number seeds using np.random.seed(). For each random number seed, first call the data loader to shuffle the data order by shuffle=True; then perform forward inference on the model, call the validation function validation_metrics() to perform statistics on the predicted results and true labels, and output the final values of each indicator. The test results are shown in Tables 6 and 7, which fully illustrate the superiority of the method of the present application in the cross-version domain small sample website fingerprint attack scenario.
[0172] Table 6 Test indicator values of the embodiment in the closed world scenario
[0173]
[0174] Table 7 Test indicator values of the embodiment in the open world scenario
[0175]
[0176] The above description is only for the preferred embodiments of the method of the present application, and the present application is not limited to the above embodiments. It should be noted that any person skilled in the art, under the guidance of the present description, all equivalent replacements, obvious modifications made, fall within the scope of the present description, and should be protected by the present application.
Claims
1. A cross-Tor version domain small sample website fingerprinting method based on contrastive learning, characterized in that, The method comprises the following steps, S1, design a pre-training strategy based on contrastive learning, which is divided into four steps: traffic data preprocessing, designing a feature extractor, designing a traffic attention module, and pre-training the feature extractor, S2, design a fine-tuning strategy based on double-branch confidence alignment, which is divided into three steps: local fine-tuning, calculating the DCA composite loss function, training and parameter updating, S3, design a traffic classification strategy driven by prototypes; it is divided into three steps: generating prototypes, testing sample classification, and model testing.
2. The cross-Tor version domain few-shot website fingerprinting method based on contrastive learning according to claim 1, characterized in that, In step S1, S11: traffic data preprocessing, extracting direction sequences from original traffic data as model input, which is divided into three steps: extracting bidirectional flow, extracting direction information, and adjusting sequence, (1) Extract bidirectional flow, read the original traffic file in Pcap format, and extract bidirectional flow according to the five-tuple: source IP address, destination IP address, source port number, destination port number, and protocol type, (2) Extract direction information, parse each data packet in the bidirectional flow one by one, and extract the direction information of each data packet, (3) Adjust the sequence, unify the data packet direction sequence to a fixed length L, truncate the sequence that exceeds the length L, and pad the sequence that is less than L with zeros to ensure the length consistency of all samples, S12: Design a feature extractor, which is divided into three steps: design a main convolution layer, design a deep separable convolution, and design a residual connection, (1) Design a main convolution layer, the main convolution layer is stacked by four bottleneck convolution modules, each bottleneck convolution module is composed of a convolution layer, a batch normalization layer, a nonlinear activation function and a pooling layer, which is used to extract higher-level semantic features step by step, the convolution kernel size is uniformly set as conv_ks, and the channel number is expanded to fn, 2*fn, 4*fn, 8*fn in different modules, so that the feature expression ability is continuously enhanced in the process of level advancement, the dilation mechanism is introduced in the convolution structure, and the dilation rates of different modules are ds, 2*ds, 4*ds, 8*ds respectively, so that the network can effectively expand the receptive field in a limited depth and capture long-range dependencies, and a pooling layer is connected at the end of each module to further shorten the sequence length and extract higher-dimensional abstract features, (2) Design a deep separable convolution, combine a deep separable convolution structure in the bottleneck convolution module, which is divided into three steps: compressing the channel number, extracting sequence features, and restoring the channel number, a) Compress the channel number, in order to provide a more compact input feature representation, use point-wise convolution to linearly transform the input features, compress the channel number to a lower dimension, reduce the subsequent calculation overhead, and promote cross-channel information interaction, b) Extract sequence features, use one-dimensional deep convolution structure, and set the group number to be consistent with the channel number, so that each channel can independently complete convolution operation to realize sequence pattern extraction in each channel; at the same time, introduce the dilation rate to expand the convolution receptive field under the condition of maintaining the calculation efficiency, so that the model can effectively perceive multi-granularity traffic patterns and improve the representation ability of complex sequence features. c) restoring the number of channels, the number of channels is restored to the original dimension by point-by-point convolution, combined with the use of batch normalization and nonlinear activation function, the expansion of the feature space is completed, (3) Designing residual connection, setting a shortcut branch through point-by-point convolution combined with batch normalization operation, mapping the input features to the channel dimension consistent with the output of the main branch, then adding the output of the main branch convolution and the mapping result of the shortcut branch element by element, realizing residual fusion to enhance the training stability of the model.
3. The cross-Tor version domain few-shot website fingerprinting method based on contrastive learning according to claim 2, characterized in that, In step S1, S13: Designing flow attention module, by adaptively adjusting the weight of different features, enhancing the model's ability to capture key features, which is divided into 3 steps: aggregated features, generating channel attention, and weighted fusion, (1) the output of the i-th convolutional block is aggregated as shown in formula (1) where B represents the batch size, C i represents the number of channels, and the sequence dimension is compressed by a global average pooling operation to obtain a global feature representation of the convolutional block in the channel dimension On this basis, the global feature is linearly transformed and batch normalized as shown in formula (2) to improve the stability of the feature mapping, where BN i represents the normalization operation on the i-th convolutional block feature, W i and b i are the learning weights and bias corresponding to the i-th convolutional block, and the normalized features S i of each convolutional block are aggregated to form a multi-scale feature representation I FA , where n is the index of the convolutional block and q is the number of convolutional layers used, providing rich input for subsequent attention modeling, (2) Generating channel attention, as shown in formula (4), the integrated channel features are nonlinearly transformed by multilayer perceptron to generate the attention weight α of each channel, where σ is the Sigmoid activation function, and ReLU is the linear rectifier function, a = σ(W2 - ReLU(W1 - I FA + b1) + b2) (4) (3) Weighted fusion, based on the generated attention weight, the feature mapping is weighted and fused, by multiplying each channel's feature mapping with the corresponding attention weight element by element, to highlight key semantic information, enhance the influence of important features, and reduce the influence of irrelevant features, S14: Feature extractor pre-training, the specific process includes 4 steps: calculating supervised contrast learning loss, hyperparameter selection, batch training and parameter updating, and weight saving, (1) Calculate the supervised contrast learning loss, in each training batch, calculate the supervised contrast learning loss using the source domain data constructed in step S11 to optimize the representation space, which is divided into 3 steps: embedding generation, constructing positive and negative sample sets, and loss calculation, a) Embedding generation, mapping the source domain data to the contrast embedding space through the feature extractor to obtain the embedding of the in-batch samples b) Constructing positive and negative sample sets, for each anchor sample i in the batch, constructing its positive sample index set P(i) according to the class label, and the samples of other classes as the negative sample index set N(i), c) Loss calculation, as shown in formula (5), embeds z for each anchor point sample. i The cosine similarity sim(z) is calculated between the sample embeddings in its positive sample set P(i) and the sample embeddings. i ,z j The similarity distribution is scaled using a temperature coefficient τ to enhance its discriminative power. Based on the scaled similarity, pairwise loss is calculated, and the loss results of each anchor point and its positive sample set are averaged. The average loss of all anchor points in the entire batch is then summed to form the final supervised contrastive loss. (2) Hyperparameter selection, using grid search method, performing multiple sampling in the pre-defined hyperparameter search space, and after evaluating the model performance each time, selecting the optimal hyperparameter combination to perform subsequent training to ensure that the model can obtain the best performance under different training conditions, (3) Batch training and parameter updating, the model processes all source domain training samples in batches, in each training iteration, the loss function is calculated by forward propagation to evaluate the difference between the model output and the target, based on the loss function, the gradient is calculated by the back propagation algorithm, and the model parameters are updated, the training process will continue until the loss value calculated on the validation set no longer decreases significantly, ensuring that the model performance reaches the best state, (4) Weight saving, when the feature extractor training is completed, save the model weight file obtained during the training process to ensure that the model can continue to be used in the subsequent fine-tuning stage.
4. The cross-Tor version domain few-shot website fingerprinting method based on contrastive learning of claim 3, wherein, In step S2, S21: Local fine-tuning, which is divided into 3 steps: model weight loading, freezing feature extractor parameters, and updating flow attention module parameters, (1) Model weight loading, load the pre-trained model weight saved in step S14 into the new model to retain the learning ability of the model on the source domain data, (2) Freeze feature extractor parameters, set both the weights and bias terms of the feature extractor to be non-updatable to alleviate catastrophic forgetting and retain the learned traffic structure representation in the pre-training phase, (3) Update traffic attention module parameters, calculate the loss by forward propagation and perform back propagation to update only the trainable parameters in the traffic attention module, ensuring that the model can effectively learn the key features of the target domain, S22: design a DCA composite loss function, use the DCA composite loss function to optimize the model, so as to enhance the website category discrimination and keep the structural consistency, the DCA loss includes cross entropy loss and weighted contrastive loss The specific calculation process is divided into three steps: calculating cross entropy loss, calculating weighted contrastive loss, and calculating fusion loss. (1) Calculate the cross-entropy loss to calculate the classification accuracy of the target domain data and promote the maximum separability between classes. The calculation process of the cross-entropy loss is shown in formula (6), where N is the total number of samples in the batch, C is the total number of classes, y i is the real label of the sample, is the predicted probability of the sample, (2) Calculate the weighted contrastive loss, which consists of three steps: determine the hardest positive sample, calculate the focal weighting, and construct the weighted loss, a) determining the hardest positive sample, the hardest positive sample refers to the positive sample with the smallest similarity with the anchor sample, for each sample z i As shown in formula (7), the hardest positive sample is determined b) Calculate the focal weighting, as shown in equation (8), adjust the weight of the difficult-to-distinguish sample, where γ is the focal parameter, to adjust the model's focus on difficult samples, a larger γ value will increase the loss weight of low similarity sample pairs, making the model pay more attention to difficult-to-distinguish samples during training, c) Construct the weighted loss, as shown in equation (9), to enhance the model's discrimination ability for difficult-to-distinguish samples, combine the focal weight with the supervised contrastive loss to improve the discriminability of the embedding space, (3) Calculate the fusion loss, as shown in equation (10), adjust the weight λ to proportionally fuse the cross-entropy loss and the weighted contrastive loss to balance the optimization objectives of classification accuracy and feature structure preservation, S23: Training and parameter updating, which consists of four steps: dataset division, hyperparameter selection, batch training and parameter updating, and weight file saving, (1) Dataset division, divide the target domain samples into support set and query set, where the first s samples of each class are used as the support set and the last q samples are used as the query set, (2) Hyperparameter selection, within the hyperparameter search space in the fine-tuning phase, perform multiple rounds of sampling and evaluation, using the classification accuracy on the target domain query set as the evaluation metric, and select the optimal hyperparameter combination as the configuration for subsequent training, (3) Batch training and parameter updating, forward propagate the target domain data through the model to calculate the DCA composite loss function, calculate the gradient through the back propagation algorithm based on the total loss obtained, and update the trainable parameters in the traffic attention module. Training continues until the loss function on the target domain query set stabilizes and no longer decreases, obtaining the best fine-tuned model, (4) Weight file saving, save the weight file obtained during the fine-tuning process to ensure that the model continues to be used in step S3.
5. The cross-Tor version domain few-shot website fingerprinting method based on contrastive learning according to claim 4, characterized in that, In step S3, S31: Generate prototypes, which consists of three steps: obtain embedding representation, calculate class prototypes, and determine prototype radius, (1) Obtain embedding representation, obtain the embedding representation of the target domain data with semantic features of each website category by using the feature extractor and projection head in the S23 fine-tuning phase, (2) Compute class prototypes, as shown in equation (11), determine class prototypes by computing the mean of the feature embeddings of the support samples for each class, where is the embedding representation for all support samples of class w, (3) Determine the prototype radius, to ensure that the prototype has a suitable classification boundary in the embedding space, construct the radius of each class according to the characteristics of the website category, the specific process consists of two steps: calculate the prototype radius and adjust the prototype radius, a) Compute prototype radius, initialize a fixed radius r fixed as the unified class radius; when each class has multiple support samples, the cosine distance between each sample and the prototype is calculated, and the class radius is estimated by the Median Absolute Deviation (MAD), and the prototype radius is calculated as shown in formula (12), wherein sim(z, c w ) represents the cosine similarity between sample z and class prototype c w b) adjusting the prototype radius, calculating the distance between each pair of class prototypes, and dynamically adjusting the radius of each class according to the distance to ensure that the prototypes have sufficient spacing in the embedding space and avoid overlap between classes, if the radius of two classes and the distance between the prototypes of the two classes overlap, adjust according to formula (13), wherein, and are the prototype radii of the classes w i and w j respectively, d is the cosine distance between the two class prototypes.
6. The cross-Tor version domain few-shot website fingerprinting method based on contrastive learning according to claim 5, characterized in that, In step S3, S32: test sample classification, using the fine-tuned model and the pre-calculated class prototype and class radius, classify the test sample, the specific process is divided into two steps: calculating the test sample embedding vector and using the prototype classification decision, (1) Calculate the test sample embedding vector, input the test sample into the model trained in the S23 fine-tuning stage, and obtain its embedding vector after forward propagation, (2) Using the prototype classification decision, read the class prototype and its corresponding radius saved in step S31, calculate the distance between the test sample z and each class prototype c t and each class prototype c w , as shown in formula (14) d t = 1 - sim(z t ,c w ) (14) a) Closed world scenario, for the closed world multi-classification task, calculate the cosine distance between the test sample and all class prototypes, subtract the prototype radius of each class from the distance of each class to correct the discrimination boundary between classes, and finally find the minimum value in all corrected distances, and the corresponding class is the predicted class of the test sample, b) Open world scenario, for the binary classification task of open world, if the distance between the embedding vector of the test sample and the closest prototype is greater than the preset threshold, it is considered that the sample belongs to the non-target website, if the distance d t of the test sample is less than the prototype radius r w of the corresponding category, it is considered that the sample belongs to the target website, where r * is the prototype radius of the corresponding category, and ε is a preset tolerance threshold, |d t -r * |<ε (15).
7. The cross-Tor version domain few-shot website fingerprinting method based on contrastive learning according to claim 6, characterized in that, In step S3, S33: Model testing, statistics of the results of test samples, the specific process is divided into two steps: selecting test indicators, and statistics of test indicators, (1) Select test indicators, select appropriate evaluation indicators according to different scenarios, accuracy calculation as shown in formula (16), wherein, TP (True Positive) represents the number of samples correctly classified as the target class, TN (True Negative) represents the number of samples correctly classified as the non-target class, FP (False Positive) represents the number of non-target samples incorrectly classified as the target class, and FN (False Negative) represents the number of target samples incorrectly classified as the non-target class, the area under the curve (Area Under the Curve, AUC) is used as the evaluation indicator in the open world scenario, AUC is calculated based on the receiver operating characteristic curve (Receiver Operating Characteristic, ROC), wherein the vertical axis is the true positive rate (TPR), and the horizontal axis is the false positive rate (FPR), TPR and FPR are calculated as shown in formulas (17) and (18), by counting the number of TP, FP, TN and FN of each class, TPR and FPR are calculated and the ROC curve is drawn, and then the AUC value is obtained by the area under the curve, so as to evaluate the ability of the model to distinguish between target website classes and non-target website classes in the open world scenario, (2) Statistics of test indicators, repeat the test multiple times using different random seeds, and count the selected indicators in the closed world scenario and the open world scenario, respectively. Record the corresponding indicator values for each test, and finally calculate the average value of all test results to comprehensively evaluate the classification performance of the model under different conditions.
8. An electronic device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, The processor implements the program to realize the cross-Tor version domain small sample website fingerprint identification method based on contrast learning as claimed in any one of claims 1 to 7.
9. A computer readable storage medium having stored thereon computer instructions, wherein, The computer instructions are executed by the processor to realize the cross-Tor version domain small sample website fingerprint identification method based on contrast learning as claimed in any one of claims 1 to 7.
Citation Information
Cited By
Multi-unmanned aerial vehicle radio frequency signal-oriented type identification method, device and equipment, and storage medium
CN121705897A
Large model annealing training method and device and electronic equipment
CN121981216A
