Skip to content
alpinebusterPublic

About

Transformer-Generated CNNs with Unlabeled Multi-Flow Context and Variable Prototypes for Few-Shot Tor Traffic Classification.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

HyperTor

In this project, we propose a comprehensive framework that integrates multiple components for few-shot darknet (Tor) traffic classification.

Main Diagram

1. Handshake-fingerprint-guided context retrieval. We develop a lightweight, training-free method for selecting candidate auxiliary connections using observable handshake characteristics. The retrieved flows provide unlabeled contextual tokens for task-conditioned weight generation without requiring precise session reconstruction or additional flow association models.

2. TCP sequence driven data augmentation. We perform data augmentation by operating on TCP level sequential characteristics of network traffic. Specifically, we emulate the behavior of the Nagle algorithm to synthesize packet coalescence, duplication, and length perturbations. In addition, we simulate the effects of Retransmission Timeout (RTO) and Fast Retransmit (FR), introducing controlled packet shift and loss within flow to generate diversified yet protocol consistent augmented samples.

3. Variable prototypes. We integrate adaptive prototype allocation with Transformer-based weight generation to accommodate differences in intra-class feature variability. This enables task-conditioned representations with class-dependent prototype capacity under limited labeled supervision.

4. Transformer-Based Layerwise Hypernetwork. We design a hypernetwork that generates task-conditioned CNN parameters from a small labeled support set. Class-specific query tokens aggregate relevant support features through attention to generate class-specific weights, emulating the feature aggregation effect of a single MAML gradient step. The high-capacity Transformer handles much of the task-adaptation complexity, allowing the generated classifier to remain lightweight. The attention mechanism further supports the incorporation of unlabeled traffic into weight generation.

Getting Started

The model is trained end-to-end:

   ┌───────────────────────────┐
   │ (a) Generate a batch of   │
   │     samples for a new     │<─┐
   │         episode           │  │
   └─────────────┬─────────────┘  │
                 │                │
                 ▼                │
   ┌───────────────────────────┐  │
   │ (b) Pass support samples  │  │
   │      to Transformer →     │  │
   │   Transformer generates   │  │
   │      `CNN + VP + PL`      │  │
   └─────────────┬─────────────┘  │
                 │                │
                 ▼                │
   ┌───────────────────────────┐  │
   │ (c) Pass query samples    │  │
   │    through **generated    |  |    Update Variable Prototypes
   |       target model**      │  │ (VP & its associated Transformer)
   │     and compute loss →    │──┘
   │      Backprop to train    │
   │        Transformer        |
   │             ↓             |
   | Small-batch updates to PL |
   |   within the push cycle   |
   └───────────────────────────┘
                 │
                 ▼

Python Poetry

poetry install --extras gpu
poetry env activate
# or
source $(poetry env info -p)/bin/activate

Tor

sudo apt install tor obfs4proxy

# UseBridges 1
# ClientTransportPlugin obfs4 exec /usr/bin/obfs4proxy managed
# Bridge obfs4 57.128.68.206:12069 3D31D10FFF77231A352CA982BD373ED9235FD769 cert=xPPp3sNp0xVeoxF+SL7ixf7cefyotL7eBCgPctKyqosvDZMKD2TvvEccUcR5IFkJyM4qFQ iat-mode=0
# Bridge obfs4 91.134.80.41:59987 ACBB6AD034A3C0712265F1BFEC9F27D33B7EFC20 cert=GjxluYzjUz3H1aPRnwncHk46UYhFXvWjn4SA1wwFTfIAFVuBsfsvuZA5KlUi4obxcSaeIQ iat-mode=0
sudo vi /etc/tor/torrc

sudo systemctl enable tor
sudo systemctl restart tor
sudo journalctl -e -f -u tor@default

Running Examples

Folder scripts-dev contains several training scripts for chosen datasets. They can be executed as follows:

./scripts-dev/darkflow_1shot.sh --data_numpy_dir=./data/to/cache --train_log_dir=./data/to/logs/torch

NOTE: The evaluation script evaluate.py can be executed in parallel to evaluate produced checkpoints while the model is being trained. The evaluation script accepts the same parameters as the training script.

DDP Supports:

# `nproc_per_node` -> Number of GPUs
# `--` -> All the parameters that follow are not `torchrun` but passed to the python
nohup torchrun --nproc_per_node=2 ./hypertor/train.py -- \
  --num_layerwise_features=8 --default_num_channels=8 \
  --samples_transformer=35 --samples_cnn=35 --num_labels=7 --learning_rate=0.02 \
  --train_steps=4000000 --steps_between_saves=1000 --lw_key_query_dim=1.0 \
  --lw_value_dim=1.0 --lw_inner_dim=1.0 --cnn_model_name='maxpool-4-layer' \
  --embedding_dim=32 --num_layers=3 --stride=1 --heads=2 \
  --shared_feature_extractor='3-layer-bn' --shared_input_dim=1 --shared_features_dim=32 \
  --shared_feature_extractor_padding=same --layerwise_generator=joint \
  --nolw_use_nonlinear_feature --lw_weight_allocation=output --nolw_generate_bias \
  --nolw_generate_bn --nouse_decoder --noadd_trainable_weights --flows_shape=28,28 \
  --balanced_batches --per_label_augmentation \
  --smooth_probability=0.0 --negate_probability=0.0 \
  --train_dataset='tcub:0-6' \
  --test_dataset='tcub:7-13' --eval_datasets='tcub' \
  --num_task_evals=1024 --num_eval_batches=16 --eval_batch_size=70 \
  --shuffle_labels_seed=2026 \
  --test_smooth_probability=0.0 --test_negate_probability=0.0 \
  --data_numpy_dir=./data/tcub/cache \
  --train_log_dir=./data/tcub/logs/torch_ddp \
  --use_ddp \
  > tcub_ddp.log 2>&1 &

echo $?

About

Transformer-Generated CNNs with Unlabeled Multi-Flow Context and Variable Prototypes for Few-Shot Tor Traffic Classification.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages