Leveraging large language models for SQL behavior-based database intrusion detection

Meital Shlezinger, Shay Akirav, Lei Zhou, Liang Guo, Avi Kessel, Guoliang LiarXiv 2025 · DOI · arXiv

Abstract

This paper presents a novel two-tier anomaly detection framework utilizing DistilBERT to secure databases against both external and internal threats. By integrating unsupervised ensemble learning for "out-of-scope" external attacks and supervised role-based classification for "in-scope" internal masqueraders, the system achieves high precision, reaching an F1 score of 0.98. The approach emphasizes semantic understanding of SQL queries over traditional syntax-based methods.

Introduction

Database security remains a critical challenge as traditional detection methods—syntax, context, or statistics-based—often lack the granularity required to identify sophisticated attacks. These methods frequently fail to distinguish between external threats and "masquerade attacks," where an insider or compromised account uses legitimate credentials for unauthorized actions. The core issue is the inability of existing tools to grasp the semantic intent of SQL queries. This research addresses this gap by proposing a system that understands the "what" and "why" of a query rather than just its structural form.

Methodology: A Two-Tiered Approach

The proposed framework leverages DistilBERT, a lightweight Transformer model, to process SQL queries. The architecture is divided into two distinct tiers to handle different threat profiles.

The two-tier detection architecture combining unsupervised out-of-scope detection and supervised in-scope classification.

Tier 1: Unsupervised Out-of-Scope Detection

Targeting external attackers, this tier converts SQL queries into 768-dimensional embedding vectors. These embeddings are analyzed by an ensemble of three outlier detectors: Principal Component Analysis (PCA), Autoencoders (AE), and One-Class Support Vector Machines (OCSVM). Queries that deviate significantly from learned normal patterns are flagged as anomalies.

Tier 2: Supervised In-Scope Classification

Designed to combat insider threats, this tier uses role-labeled data to identify behavior atypical for specific user groups (e.g., Finance vs. HR). Detection occurs through role mismatch identification or when the model's confidence score for a query falls below a specific probability threshold.

To manage long SQL sequences (up to 1,900 tokens), the researchers implemented a chunking strategy where embeddings from segments of a long query are averaged to maintain overall semantic integrity.

Datasets and Evaluation

The framework was tested on two datasets:

  • Short Sequence Dataset: A simulated environment with three user groups (HR, Finance, DBA) featuring ~100 unique normalized queries per group with overlapping access areas.
  • Long Sequence Dataset: A real-world open-source CRM system (PostgreSQL) with ~500 unique queries per user, averaging 200 tokens, representing a complex production environment.

Key Insights

  • Semantic Analysis: Transformer-based models allow for finer detection by understanding the context of a query rather than just matching keywords.
  • SetFit Efficiency: Sentence Transformer Fine-tuning (SetFit) outperformed standard LLM fine-tuning, proving more accurate and faster, especially in few-shot learning scenarios.
  • Model Selection: Smaller, focused models like DistilBERT often outperformed massive models like LLaMA for these specific SQL classification tasks, likely due to the limited data and specific nature of the domain.

Results

The supervised SetFit model demonstrated exceptional performance, achieving an F1 score of 0.98 on short sequences and 0.97 on long sequences. In the unsupervised tier, the ensemble approach effectively separated malicious "out-of-scope" queries from normal traffic.

Anomaly scores across different detection algorithms showing clear distinction between anomalies (queries 0-15) and normal behavior.

As shown in the figure above, the average anomaly score drops sharply once the sequence transitions from malicious queries to normal user behavior, validating the ensemble's detection capabilities.

Limitations and Future Outlook

Despite high accuracy, challenges remain regarding the scarcity of high-quality, labeled real-world threat data. Additionally, the sensitivity of learned thresholds must be carefully balanced to avoid alert fatigue in production environments. Future work will focus on real-time deployment and testing within more complex, dynamic enterprise schemas.

Summary updated

BibTeX
@misc{shlezinger2025leveraging,
  author = {Meital Shlezinger and Shay Akirav and Lei Zhou and Liang Guo and Avi Kessel and Guoliang Li},
  title = {{Leveraging large language models for SQL behavior-based database intrusion detection}},
  year = {2025},
  eprint = {2508.05690},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2508.05690},
}
Figure