
Practical black-box adversarial packet generation against encrypted traffic classification with minimal overhead and full packet recoverability.
Paper: PacketPatch: Practical Generation and Deployment of Adversarial Packets for Byte-Feature-Based Encrypted Traffic Classification Authors: Yuwei Xu, Yuanyuan Xu, Yunpeng Bai, Jiahui Chen, Kehui Song, Jie Cao, Qiao Xiang, Guang Cheng Affiliation: Southeast University, Purple Mountain Laboratories, Tiangong University, Queen's University, Xiamen University Status: Accepted/Under Review (Elsevier)
PacketPatch is a practical scheme for generating adversarial network packets against Byte-feature-based Encrypted Traffic Classification (B-ETC) models, aiming to protect user privacy. Unlike existing methods that rely on unrealistic white-box assumptions or introduce excessive overhead, PacketPatch operates under strict black-box conditions while maintaining real-time performance and packet usability.

Pad1 → arg min (maximally disrupts original features)Pad2 → arg max (generates features aligned with random header)PacketPatch/
├── README.md # Project overview (this file)
├── LICENSE # MIT License
├── requirements.txt # Python dependencies
├── bert_base_config.json # BERT model configuration (12 layers, 768-dim, 12 heads)
│
├── packet_adv/ # ★ Core experimental code
│ ├── README.md # Usage guide for core modules
│ ├── adv_fine_tuning_cls.py # Step 1: Train PatchGenerator (SpanBERT)
│ ├── generate.py # Step 2: Generate adversarial packets
│ └── gen_onnx.py # Step 3 (optional): ONNX/TensorRT acceleration
│
├── uer/ # UER (Universal Encoder Representations) framework
│ ├── README.md # Framework documentation
│ ├── encoders/ # Encoder implementations (Transformer, RNN, CNN)
│ ├── layers/ # Network layers (Embedding, Attention, FFN)
│ ├── targets/ # Training targets (MLM, NSP, Span-MBM, etc.)
│ ├── models/ # Model definition
│ ├── utils/ # Utilities (tokenizers, optimizers, data loading)
│ ├── model_builder.py # Model construction
│ ├── model_loader.py # Pretrained weight loading
│ ├── model_saver.py # Model checkpoint saving
│ ├── trainer.py # Training loop implementation
│ └── opts.py # CLI argument definitions
│
├── dataset/ # Dataset documentation
│ └── README.md # Dataset descriptions, download links, preprocessing
│
├── image/ # Architecture and result figures
│ └── README.md # Figure descriptions
│
└── tools/ # Utility tools
└── README.md # Tool descriptions and usage
# Clone this repository
git clone https://github.com/your-org/PacketPatch.git
cd PacketPatch
# Install dependencies
pip install -r requirements.txt
Requirements:
PacketPatch is built upon ET-BERT. Download the following from the ET-BERT repository:
pretrained_model.bin — ET-BERT pretrained weightsencryptd_vocab.txt — Vocabulary file for byte-pair encodingDatasets: We evaluate on three public datasets. See dataset/README.md for download links and preprocessing instructions.
| Dataset | Classes | Scenario |
|---|---|---|
| ISCX-TOR | 16 | Tor anonymous traffic |
| ISCX-VPN | 11 | VPN encrypted tunnels |
| USTC | 20 | Malware + benign traffic |
cd packet_adv
# Edit adv_fine_tuning_cls.py first:
# - Update dataset paths (lines 47-50)
# - Update pretrained model path (lines 73-75)
# - Set correct num_classes (line 56)
# - Set correct training set size (line 878)
python adv_fine_tuning_cls.py
The trained checkpoint will be saved in the specified log directory (e.g., span_2seg_cls_log/).
cd packet_adv
# Edit generate.py first:
# - Update test data paths (lines 42-44)
# - Update --load_model_path to your trained checkpoint (line 68-69)
# - Set BWO (bandwidth overhead ratio, line 60, default 0.1)
# - Choose generation strategy via 'fun' parameter (line 50)
python generate.py
cd packet_adv
# Edit gen_onnx.py first:
# - Update model checkpoint path (line 132)
# - Update output paths (lines 145, 207)
# - Select precision mode: 'fp16' or 'int8' (line 21)
python gen_onnx.py
| Parameter | File | Line | Description | Default |
|---|---|---|---|---|
BWO | generate.py | 60 | Bandwidth overhead ratio | 0.1 (10%) |
fun | generate.py | 50 | Generation strategy | 'fun5' |
n_epochs | adv_fine_tuning_cls.py | 53 | Training epochs | 50 |
batch_size | adv_fine_tuning_cls.py | 54 | Batch size | 32 |
num_classes | adv_fine_tuning_cls.py | 56 | Number of traffic classes | Dataset-dependent |
learning_rate | adv_fine_tuning_cls.py | 94-95 | Learning rate | 2e-5 |
seq_length | adv_fine_tuning_cls.py | 92-93 | Input sequence length | 256 |
fun parameter)