Abstract
Penetration testing is a security exercise aimed at assessing the security of a system by simulating attacks against it. So far, penetration testing has been carried out mainly by trained human attackers and its success critically depended on the available expertise. Automating this practice constitutes a non-trivial problem, as the range of actions that a human expert may attempts against a system and the range of knowledge she relies on to take her decisions are hard to capture. In this paper, we focus our attention on simplified penetration testing problems expressed in the form of capture the flag hacking challenges, and we analyze how model-free reinforcement learning algorithms may help to solve them. In modeling these capture the flag competitions as reinforcement learning problems we highlight that a specific challenge that characterize penetration testing is the problem of discovering the structure of the problem at hand. We then show how this challenge may be eased by relying on different forms of prior knowledge that may be provided to the agent. In this way we demonstrate how the feasibility of tackling penetration testing using reinforcement learning may rest on a careful trade-off between model-free and model-based algorithms. By using techniques to inject a priori knowledge, we show it is possible to better direct the agent and restrict the space of its exploration problem, thus achieving solutions more efficiently.
Introduction
Penetration testing (PT) is a critical but labor-intensive security practice that currently relies heavily on human experts. Automating PT is notoriously difficult because the "rules of the game" are often hidden from the attacker (a property termed obscurity), and the target system may change its state or configuration in response to attacks (non-stationarity).
Traditional AI approaches like automated planning require a complete, explicit model of the target system, which is rarely available in real-world hacking. Model-free reinforcement learning (RL) offers a potential solution by allowing agents to learn through trial and error. However, the sheer size of the "hacking" state space makes pure exploration computationally prohibitive. This paper investigates the feasibility of using RL to automate PT through simulated Capture-the-Flag (CTF) challenges, demonstrating that success hinges on a careful trade-off: injecting small amounts of a priori knowledge to help agents navigate inherent environmental challenges.
Methodology
The authors employ Tabular Q-Learning to train agents across five increasingly complex simulations modeled as Markov Decision Processes (MDPs). To make the learning process efficient, they introduce three key techniques for injecting prior knowledge:
- Lazy Loading: Instead of pre-allocating a massive table for every possible state-action pair, the agent only initializes entries as it encounters them, assuming a sparse problem structure.
- State Aggregation: The agent is instructed to treat logically identical states (e.g., interacting with different files using the same protocol) as equivalent, drastically reducing complexity.
- Imitation Learning: The agent is "primed" with demonstrations from a human expert, biasing initial exploration toward promising attack paths.
The study uses five custom-designed CTF challenges, ranging from simple port scanning and exploitation to non-stationary environments and complex website hacking involving file system navigation.
Key Insights: Obscurity and Non-Stationarity
Hacking is fundamentally about discovering structure. If a system is a "max-entropy" environment—providing zero feedback until the final successful exploit—model-free RL is no better than random guessing. Furthermore, when defenders actively change the environment (non-stationarity), the agent's previously learned knowledge can become useless.
The authors argue that prior knowledge is a necessity, not a shortcut. In hacking, the state space is so vast that "pure" model-free learning is often impossible. Techniques like state aggregation represent the "intuition" a human hacker brings to the table, such as understanding that "a file is a file" regardless of its specific name.
Key Results
In the simplest scenario (Simulation 1), the agent successfully learned a two-step "scan-then-exploit" policy, converging in approximately 400 episodes. The resulting Q-matrix shows a clear diagonal structure, indicating the acquisition of a deterministic optimal strategy.
The impact of active defense was evaluated in Simulation 2. As the probability of a defender moving a service ($p$) increased, the number of steps required to find the flag increased dramatically, and the clear structure of the learned Q-matrix dissolved into noise.
Further results demonstrated that Lazy Loading in Simulation 3 allowed the agent to learn a reasonable policy while exploring only a fraction of the theoretical $2 \cdot 10^6$ states. Additionally, Imitation Learning allowed an agent to reach optimal performance an order of magnitude faster than training from scratch.
Conclusion and Future Work
While tabular methods prove the concept, they cannot scale to real-world networks with thousands of nodes. The authors suggest that Deep Reinforcement Learning is the necessary next step to approximate the Q-function in high-dimensional spaces. Future work should also focus on Hierarchical RL to break down penetration tests into sub-tasks (e.g., reconnaissance vs. privilege escalation) and Transfer Learning to improve generalization across unseen environments.
Summary updated