Abstract
With increasing numbers of vulnerabilities exposed on the internet, autonomous penetration testing (pentesting) has emerged as a promising research area. Reinforcement learning (RL) is a natural fit for studying this topic. However, two key challenges limit the applicability of RL-based autonomous pentesting in real-world scenarios: (a) training environment dilemma -- training agents in simulated environments is sample-efficient while ensuring their realism remains challenging; (b) poor generalization ability -- agents' policies often perform poorly when transferred to unseen scenarios, with even slight changes potentially causing significant generalization gap. To this end, we propose GAP, a generalizable autonomous pentesting framework that aims to realize efficient policy training in realistic environments and train generalizable agents capable of drawing inferences about other cases from one instance. GAP introduces a Real-to-Sim-to-Real pipeline that (a) enables end-to-end policy learning in unknown real environments while constructing realistic simulations; (b) improves agents' generalization ability by leveraging domain randomization and meta-RL learning. Specially, we are among the first to apply domain randomization in autonomous pentesting and propose a large language model-powered domain randomization method for synthetic environment generation. We further apply meta-RL to improve agents' generalization ability in unseen environments by leveraging synthetic environments. The combination of two methods effectively bridges the generalization gap and improves agents' policy adaptation performance. Experiments are conducted on various vulnerable virtual machines, with results showing that GAP can enable policy learning in various realistic environments, achieve zero-shot policy transfer in similar environments, and realize rapid policy adaptation in dissimilar environments.
Introduction
Autonomous penetration testing (PT) seeks to automate the identification and exploitation of network vulnerabilities. While Reinforcement Learning (RL) provides a framework for decision-making in these complex environments, it faces a significant hurdle: the generalization gap. Agents trained on a specific host configuration often fail when deployed against a target with even minor variations, such as a different operating system version or a non-standard port.
This "reality gap" is compounded by the training environment dilemma. High-fidelity emulated environments (e.g., full virtual machines) are realistic but computationally expensive and slow for RL training. Conversely, abstract simulations are fast but lack the nuance required for real-world transfer. As shown in Figure 1, standard RL agents achieve high rewards in training but suffer a massive drop in performance when moved to a testing environment, highlighting the need for more robust training methodologies.
Methodology: The GAP Framework
To address these challenges, we propose GAP, a framework centered on a Real-to-Sim-to-Real pipeline. This approach bridges the gap between efficient simulation and realistic deployment.
Real-to-Sim-to-Real Pipeline
The pipeline consists of several key stages:
- Real-to-Sim: The agent interacts with a real or emulated vulnerable host to gather configuration data (ports, services, OS details), which is used to automatically construct a JSON-based simulation.
- LLM-powered Domain Randomization: To prevent overfitting to a single configuration, we utilize a Large Language Model (LLM) to perform "semantic" domain randomization. By combining the base simulation with official CVE descriptions, the LLM generates logically consistent synthetic variants (e.g., varying OS versions or port numbers).
- Meta-Reinforcement Learning (Meta-RL): The agent is trained across these diverse synthetic environments using the Model-Agnostic Meta-Learning (MAML) algorithm. This allows the agent to learn a meta-policy that can quickly adapt to new, unseen tasks.
Agent Architecture
The agent operates on an Observe-Orient-Decide-Act (OODA) cycle. It utilizes industry-standard tools like Nmap and Metasploit. Raw text observations are processed into embeddings using Sentence-BERT, which are then fed into a Proximal Policy Optimization (PPO) policy.
Experimental Evaluation
We evaluated GAP using 20 distinct vulnerability environments from Vulhub, covering web frameworks (Drupal, Struts), databases (PostgreSQL, Redis), and management tools (GitLab, Jenkins).
Key Results
- Zero-Shot Transfer: In environments with the same vulnerability but different configurations, GAP achieved a success rate of over 90%, significantly outperforming baseline models like "April" (14%) and standard PPO (44%).
- Generalization Gap Reduction: The "GenGap" metric—the reward difference between training and testing—was reduced by over 90% compared to baseline models.
- Rapid Adaptation: For entirely dissimilar environments, GAP-Transfer reduced training time by approximately 40% and provided a higher initial "jumpstart" reward.
Conclusion
The GAP framework demonstrates that the "reality gap" in autonomous pentesting can be effectively bridged by combining LLM-powered domain randomization with Meta-RL. By exposing agents to a wide variety of semantically consistent synthetic environments, they learn to focus on the core exploit logic rather than host-specific "noise." Future work will focus on increasing environment complexity, including network defenses like firewalls and honeypots, and exploring lifelong learning mechanisms.
Summary updated