HSMLog: Small Language Model-Assisted Hardware Security Module Log Anomaly Detection with Behavioral Analysis

Chia-Hsuan Wu, Dar-Hsin Dustin Wu, Rui Fang, Yi-Ting Lee, Chia-Chih Lin, Ming-Syan ChenarXiv 2026

TL;DR

HSMLog is a novel two-stage framework that uses a fine-tuned Small Language Model (SLM) and Retrieval-Augmented Generation (RAG) to detect anomalies in Hardware Security Module (HSM) logs. By combining contextual sequence analysis with organization-specific policy validation, it achieves an impressive 97.46% F1 score on complex, stateful cryptographic logs while keeping inference local and secure.

What Problem Does This Paper Address?

Hardware Security Modules (HSMs) are the bedrock of modern digital security, managing cryptographic keys and executing critical operations like encryption, decryption, and digital signing. Naturally, their logs are a goldmine for security auditing.

However, analyzing HSM logs is uniquely challenging. Unlike standard system logs, an HSM log record represents a stateful cryptographic operation. An event that looks completely benign in isolation (e.g., a successful key access) might actually be part of a malicious pattern (like a "low-and-slow" metadata probing attack) when cross-referenced with specific key handles, session roles, and organizational policies.

Traditional log anomaly detectors look only at simple event sequences and miss these deep relational context clues. Meanwhile, deploying massive Large Language Models (LLMs) to scan entire log histories is computationally prohibitive and introduces severe data privacy risks for sensitive cryptographic environments.

Methods

HSMLog tackles these challenges using a smart, two-stage architecture that balances local efficiency with broad contextual awareness.

Fig. 1. Overview of HSMLog. Stage 1 performs SLM-based contextual and policy-guided anomaly detection over structured HSM windows. Stage 2 uses retrieved policies, prior suspicious-key records, and candidate-related log context for conservative review and retrieval-grounded behavioral analysis.

1. Data Preprocessing & Canonicalization

Raw HSM logs are highly complex. HSMLog first condenses them into a compact, text-based representation that preserves critical security fields (timestamps, session IDs, roles, key handles, mechanisms, and result codes). These formatted events are then grouped into overlapping sliding windows (typically 20 events wide).

2. Stage 1: SLM-Based Contextual and Policy-Guided Detection

Each log window is analyzed in parallel by a fine-tuned Small Language Model (google/gemma-4-E4B-it adapted via LoRA):

  • Contextual Detection: The SLM evaluates the window for sequence coherence, looking for structural anomalies like repeated failures or invalid state transitions.
  • Policy-Guided Assessment: The SLM checks the window against organization-specific operational rules.

If either check flags a window as anomalous, it is merged into a "candidate incident."

3. Stage 2: Retrieval-Grounded Candidate Review

Once a candidate incident is flagged, HSMLog uses a Retrieval-Augmented Generation (RAG) pipeline. It retrieves relevant security policies and historical records strictly predating the incident window. This evidence is used to filter out weak or false alerts and automatically generate a detailed behavioral analysis report for security engineers.

Datasets

Because exhaustive, real-world HSM anomaly datasets do not publicly exist due to confidentiality, the authors evaluated HSMLog using real operational background logs from an industrial HSM environment, augmented with 100 near-balanced anomaly cases across five practitioner-informed categories:

  1. Cryptographic Failure Probing: Concentrated failures on a single target (indicative of brute-force or padding oracle attacks).
  2. Object Handle Probing: Unauthorized or unusual searches for token-resident assets.
  3. Lifecycle Violation: Attempting to use a key handle after it has been destroyed.
  4. Policy Violation: Syntactically valid operations that violate deployment-specific rules (tested as an unseen category at inference).
  5. Low-and-Slow Metadata Probing: Intermittent, stealthy queries to unrelated keys mixed into routine background traffic.

Key Insights and Ideas

  • The Power of Hybrid Detection: Relying on the SLM's learned context alone yielded an F1 score of 81.25%. However, combining the SLM with explicit, policy-guided rules caused the F1 score to jump to 96.97%. This proves that machine learning and hardcoded security policies are highly complementary.
  • RAG as a Triage Filter: Stage 2 doesn't act as a secondary detector; instead, it acts as a conservative filter. By pulling historical context to validate alerts, it successfully boosted precision from 97.96% to 98.97% without dropping recall.
  • Window Size Trade-offs: The authors discovered a clear sensitivity curve regarding the observation window size ($w$). Smaller windows ($w=10$) yield high precision but miss broader attack contexts, while overly large windows ($w=50$) dilute localized evidence with background noise.

Key Results

HSMLog was evaluated against seven representative log anomaly detection baselines (including DeepLog, LogBERT, and LogLLM).

  • Top-Tier Performance: HSMLog achieved 98.97% precision, 96.00% recall, and a 97.46% F1 score, outperforming the strongest baseline (PLELog) by 4.75 F1 points.
  • Anomalous-Event Coverage: It successfully covered 98.66% of all anomalous events within the log stream.
  • Zero-Shot Policy Enforcement: On the "Policy Violation" category—which was completely unseen during the SLM's training—HSMLog achieved a perfect 100% case detection rate, thanks to its Stage 1 policy-guided assessment mechanism.

Limitations and Future Work

While highly effective, the authors note a few limitations:

  • Evaluation Scope: The framework was tested on a single industrial HSM environment and schema. Its generalizability across different vendors (e.g., Thales, Utimaco) remains to be validated.
  • Simulated Anomalies: Although the background data was real, the attack scenarios were simulated based on practitioner insights rather than actual, wild production breaches.
  • Human-in-the-Loop Validation: The utility and readability of the automatically generated Stage 2 behavioral reports have not yet been formally evaluated by real-world security operations center (SOC) engineers.

Future work will focus on testing HSMLog across diverse HSM vendor ecosystems and evaluating the practical triage speedup it provides to security teams.

Summary updated

BibTeX
@misc{wu2026hsmlog,
  author = {Chia-Hsuan Wu and Dar-Hsin Dustin Wu and Rui Fang and Yi-Ting Lee and Chia-Chih Lin and Ming-Syan Chen},
  title = {{HSMLog: Small Language Model-Assisted Hardware Security Module Log Anomaly Detection with Behavioral Analysis}},
  year = {2026},
  eprint = {2608.29773},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2608.29773},
}
Figure