Simulating SQL injection vulnerability exploitation using Q-learning reinforcement learning agents

László Erdödi, Åvald Åslaugson Sommervoll, Fabio Massimo Zennaro · DOI

Abstract

While machine learning has been extensively applied to the detection of SQL injection (SQLi) attacks, the automation of the exploitation phase remains relatively under-explored. This paper investigates the feasibility of using reinforcement learning (RL) agents to autonomously discover and exploit SQLi vulnerabilities. By modeling the exploitation process as a Markov Decision Process (MDP) within a "Capture the Flag" (CTF) framework, we demonstrate that both Tabular Q-Learning and Deep Q-Learning (DQN) agents can learn optimal exploitation strategies. Our results show that these agents can reach theoretical efficiency limits, successfully exfiltrating data in a minimal number of steps across diverse, synthetically generated environments.

Introduction

SQL injection (SQLi) has consistently ranked among the most severe web vulnerabilities for decades. Traditional security research focuses heavily on detection—identifying malicious patterns in incoming queries. However, the exploitation phase—where an attacker probes a system, interprets responses, and iterates on payloads—is typically a manual task performed by human penetration testers.

This study explores the potential for autonomous penetration testing by training intelligent agents to navigate this trial-and-error process. The goal is to move beyond static scripts toward agents capable of "thinking" like an attacker to find the most efficient path to sensitive data (the "flag").

Problem Formulation

The researchers frame the SQLi exploitation process as a reinforcement learning problem using the following MDP components:

  • State Space: Defined by the history of actions taken by the agent and the corresponding responses received from the server.
  • Action Space: A set of 51 discrete SQL strings. To improve tractability, these are partitioned into three functional categories:
  • Escape Actions: Probing for syntax to break out of the original query (e.g., ', ").
  • Column Actions: Identifying the number of columns required for a successful UNION SELECT statement.
  • Injection Actions: Executing the final payload to retrieve the target data.
  • Reward Function: A sparse reward structure where the agent receives +10 for capturing the flag and -1 for every failed attempt, incentivizing the discovery of the shortest exploitation path.

Methodology

To ensure the agents learn generalizable strategies rather than memorizing specific database structures, the authors developed a custom synthetic environment. In each training episode, the environment generates a unique website and database configuration with varying table names, column counts, and data types. The vulnerable query templates also vary, using different logical operators (=, >, BETWEEN) and input wrapping.

Two primary RL algorithms were evaluated:

  1. Tabular Q-Learning: A classic approach that stores state-action values in a lookup table. While highly interpretable, it faces scalability issues as the action history grows.
  2. Deep Q-Learning (DQN): Utilizes a neural network to approximate Q-values. This approach offers significantly better memory efficiency and the potential to scale to more complex state representations.

Results and Discussion

The experimental results indicate that RL agents are highly effective at mastering the SQLi exploitation game.

Efficiency and Convergence

Both Tabular and DQN agents converged to a near-optimal policy, typically requiring only 4 to 5 steps to exploit a vulnerability. This performance matches the theoretical lower bound for the defined environment. Interestingly, the DQN agent, when trained with larger batch sizes, occasionally achieved exploitation in as few as 2 or 3 steps by correctly inferring the environment's underlying logic early in the episode.

Scalability and Memory

A key finding was the difference in resource consumption between the two methods. The Tabular Q-Learning agent's memory usage ballooned to nearly 1 GB to track the expanding history of actions. In contrast, the DQN agent achieved comparable performance with a model size of only a few hundred kilobytes. This demonstrates that deep learning is a prerequisite for applying these techniques to more complex, real-world scenarios with larger action spaces.

Heuristic Discovery

The agents did not merely perform random searches; they learned a logical sequence of operations. Specifically, they discovered that identifying the correct "escape character" first significantly reduces the remaining search space. This mimics the heuristic approach used by human attackers, where a single successful syntax break provides the foundation for the rest of the exploit.

Limitations and Future Work

While promising, the current simulation operates under several simplifying assumptions:

  • Lack of Input Validation: The environment does not simulate Web Application Firewalls (WAFs) or input sanitization.
  • Transparent Feedback: The agent receives clear success/failure signals. Real-world "blind" SQLi requires agents to infer results from side channels like timing or subtle HTML changes.
  • Restricted Action Space: The current model uses a limited set of SQL commands, whereas real-world attacks involve complex bypass techniques and database-specific syntax.

Future research will focus on expanding the action space to include combinatorial attacks and training agents to parse raw HTML responses, moving closer to fully autonomous penetration testing in non-preprocessed environments.

Conclusion

This research demonstrates that reinforcement learning is a viable framework for automating the exploitation of SQL injection vulnerabilities. By treating security as a game, agents can autonomously learn efficient, human-like strategies for data exfiltration. The transition from tabular methods to deep reinforcement learning is essential for managing the complexity of web-based attack surfaces, paving the way for more sophisticated autonomous security tools.

Summary updated

BibTeX
@article{erdodi2021simulating,
  author = {László Erdödi and Åvald Åslaugson Sommervoll and Fabio Massimo Zennaro},
  title = {{Simulating SQL injection vulnerability exploitation using Q-learning reinforcement learning agents}},
  journal = {J. Inf. Secur. Appl.},
  year = {2021},
  doi = {10.1016/j.jisa.2021.102903},
}
Figure