Transformer-based classification of MITRE ATT&CK tactics from SSH/Bash command sequences using BERT and UniXcoder.
This project explores the application of Natural Language Processing and Transformer models to cybersecurity data.
SSH sessions collected from a honeypot are represented as sequences of Bash words, and the objective is to classify each word according to its corresponding MITRE ATT&CK tactic.
The complete workflow includes:
- Dataset characterization and class distribution analysis
- Bash command tokenization analysis
- Preprocessing of long and obfuscated command sequences
- Fine-tuning of pretrained Transformer models
- Comparison between pretrained and randomly initialized BERT
- Comparison between BERT and UniXcoder
- Partial model freezing to reduce the number of trainable parameters
- Inference on previously unlabeled SSH sessions
- Behavioral analysis through MITRE ATT&CK fingerprints
The project was developed in Python using PyTorch and Hugging Face Transformers.
SSH honeypot sessions contain valuable information about attacker behavior, but Bash commands cannot always be classified independently.
The same command may correspond to different MITRE ATT&CK tactics depending on its context.
For example, echo can be associated with:
- Execution, when it is used as part of a pipeline that executes code
- Persistence, when it is used to modify credentials or authorized SSH keys
- Discovery, when it participates in information-processing commands
This makes the problem suitable for contextual language models capable of analyzing complete command sequences rather than isolated words.
The first stage studies the distribution of MITRE ATT&CK labels and the characteristics of the SSH sessions.
The dataset presents a strong class imbalance, with some tactics appearing significantly more frequently than others. This imbalance directly affects model performance, particularly for minority classes.
The number of Bash words per session was also analyzed. Most sessions are relatively short, although some contain long or highly obfuscated payloads.
Two Transformer tokenizers were compared:
- BERT
- UniXcoder
Both models divide Bash commands into subword tokens, but their behavior differs significantly for technical strings, URLs, encoded payloads and shell syntax.
Long encoded strings can generate thousands of tokens. In the most extreme session analyzed:
| Tokenizer | Maximum tokens |
|---|---|
| BERT | 1,889 |
| UniXcoder | 28,920 |
To reduce this problem, Bash words longer than 30 characters were truncated before tokenization.
After preprocessing:
| Model | Average tokens/session | Maximum tokens |
|---|---|---|
| BERT | 128.34 | 920 |
| UniXcoder | 110.42 | 824 |
Only approximately 2.4% of the training sessions exceeded the maximum sequence length of 512 tokens after preprocessing.
Several training strategies were evaluated to study the importance of pretraining and domain-specific representations.
A pretrained BERT model was fine-tuned for token classification.
Despite using only 251 labeled training sessions, the model achieved:
| Epoch | Precision | Recall | F1 | Accuracy |
|---|---|---|---|---|
| 1 | 0.525 | 0.668 | 0.588 | 0.682 |
| 2 | 0.541 | 0.776 | 0.638 | 0.751 |
| 3 | 0.587 | 0.711 | 0.643 | 0.764 |
The results show that pretrained linguistic representations can transfer effectively to cybersecurity tasks even when the labeled dataset is relatively small.
To evaluate the importance of transfer learning, the same architecture was also trained from randomly initialized weights.
| Epoch | Precision | Recall | F1 | Accuracy |
|---|---|---|---|---|
| 1 | 0.257 | 0.026 | 0.047 | 0.429 |
| 2 | 0.471 | 0.664 | 0.551 | 0.662 |
| 3 | 0.443 | 0.658 | 0.530 | 0.688 |
Performance decreased significantly compared with pretrained BERT, highlighting the importance of pretrained representations when only limited labeled data are available.
UniXcoder was evaluated because it is pretrained on source-code-like data and therefore provides representations that are potentially better suited to Bash commands than models trained primarily on natural language.
The experiments showed improved performance for several command patterns. In particular, the F1 score for the Execution tactic increased from approximately 0.62 with BERT to 0.71 with UniXcoder.
This suggests that code-oriented pretraining can provide useful prior knowledge for SSH and Bash data.
Different numbers of UniXcoder layers were fine-tuned in order to study the trade-off between computational cost and classification performance.
| Strategy | Total parameters | Trainable parameters |
|---|---|---|
| Full fine-tuning | 125,344,519 | 125,344,519 |
| Last 2 layers + classifier | 125,344,519 | 14,181,127 |
| Classifier head only | 125,344,519 | 5,383 |
Training only the final two encoder layers and the classifier reduced the number of trainable parameters to approximately 11% of the complete model while maintaining strong performance.
| Strategy | Validation F1 | Test F1 |
|---|---|---|
| Last 2 layers + classifier | 0.828 | 0.730 |
| Classifier head only | 0.396 | 0.360 |
These results indicate that most of the useful representations are already learned during pretraining, although adapting part of the encoder remains important for this task.
The final models were used to predict MITRE ATT&CK tactics for individual Bash words inside complete SSH sessions.
The results demonstrate that the classification of a command strongly depends on its context.
For example:
cat /proc/cpuinfo | grep name | wc -lis naturally associated with Discovery, since the attacker is inspecting information about the target system.
However:
echo "ssh-rsa ..." >> authorized_keysis associated with Persistence, since the command can be used to maintain long-term SSH access to the compromised system.
This context dependency illustrates why analyzing complete command sequences is more informative than assigning fixed labels to individual commands.
The predicted word-level MITRE ATT&CK tactics were also transformed into session fingerprints.
Each fingerprint represents the sequence of predicted tactics observed during an SSH session.
This representation makes it possible to identify:
- Recurring attack patterns
- Frequently repeated automated scripts
- Changes in attacker behavior over time
- Bursts of identical attack sequences
- Potential automated or coordinated attack campaigns
The fingerprint visualization shows several persistent attack patterns appearing throughout the observation period, as well as large groups of identical sessions concentrated around specific dates.
These patterns suggest the presence of automated attack scripts and potentially coordinated bot activity.
The main conclusions of the project are:
- Transformer models can be successfully applied to SSH honeypot telemetry.
- Pretraining is particularly important when only a small labeled dataset is available.
- Pretrained BERT clearly outperforms the same architecture trained from scratch.
- UniXcoder benefits from its code-oriented pretraining when processing Bash commands.
- Truncating extremely long Bash words significantly reduces tokenization problems.
- Fine-tuning only the last two UniXcoder layers preserves most of the model performance while reducing the number of trainable parameters by almost 90%.
- Context is essential for assigning MITRE ATT&CK tactics to shell commands.
- Session-level MITRE fingerprints provide a useful representation for identifying recurring attacker behavior.
- Python
- PyTorch
- Hugging Face Transformers
- BERT
- UniXcoder
- Pandas
- NumPy
- Scikit-learn
- Matplotlib
- Google Colab
- MITRE ATT&CK
mitre-attack-classification-transformers/
│
├── README.md
├── mitre_attack_classification.ipynb
│
├── mitre_attack_classification_report.pdf
│
└── results/
├── BERT_(trunc30).png
├── F1_score.png
├── F1_score_per_MITRE_tactic.png
├── Fingerprint_patterns.png
├── MITRE_label_distribution.png
├── Number_of_words.png
└── UniXcoder_(trunc30).png
The original SSH honeypot dataset is not included in this repository due to its size.
The Jupyter notebook contains the preprocessing, tokenization, training, evaluation and inference pipeline used in the project, while the repository includes the main experimental results and the complete project report.
Because the dataset is not included, the notebook cannot be executed directly without providing the original data and updating the corresponding dataset paths.
A detailed description of the methodology, experiments and results is available in:
mitre_attack_classification_report.pdf
The main limitation of the system is the strong imbalance between MITRE ATT&CK classes.
The most frequent tactics are learned significantly better than minority classes such as Persistence, Discovery, Defense Evasion or Impact.
Future improvements could therefore explore:
- Class-weighted loss functions
- Oversampling or augmentation of minority classes
- Larger labeled datasets
- Additional code-specific Transformer architectures
- Hierarchical session-level classification
- More advanced temporal analysis of attack fingerprints
This project demonstrates how Transformer-based NLP models can be adapted to cybersecurity telemetry.
By combining contextual token classification with MITRE ATT&CK labels, SSH sessions can be transformed into structured representations of attacker behavior.
The experiments also highlight the importance of transfer learning and domain-specific pretraining: UniXcoder provides stronger representations for Bash-like inputs, while partial fine-tuning achieves competitive performance with substantially fewer trainable parameters.
Finally, converting command-level predictions into session fingerprints provides an additional layer of behavioral analysis that can help identify recurring attack strategies and automated campaigns.


.png)
.png)

