Have you ever wondered how hackers automate their security assessments or how artificial intelligence can help enhance penetration testing?. In this tutorial, we’ll dive into how to leverage the power of the CAI (Cybersecurity AI) Python API to tackle the PortSwigger Web Security Labs.
We’ll combine:
Zoom image will be displayed
By the end of this guide, you will:
Cybersecurity AI (CAI) is an open-source, lightweight framework developed by Alias Robotics that enables the creation of specialised AI agents to assist in various cybersecurity tasks, primarily focused on bug bounty hunting, vulnerability validation, and reporting. Its core purpose is to democratize access to AI-powered security tools and augment the capabilities of human security researchers, thereby improving the efficiency and effectiveness of both offensive and defensive cybersecurity operations.
You can find more information about CAI on the GitHub repository and their news hub.
PortSwigger is a leading cybersecurity company primarily known for its web application security products and training. PortSwigger provides the Web Security Academy, a free online platform that offers extensive training and interactive labs for individuals to learn and practice web security skills, ranging from fundamental concepts to advanced exploitation techniques.
To learn more about PortSwigger, you can visit their official website.
To ensure everything runs smoothly, first install the necessary packages in your local environment:
pip install cai-framework pandas selenium python-dotenvCAI specifies in the documentation that it is necessary to set up an .env file in the same folder as the main script. For setting up the PortSwigger environment, two additional variables have been added to the template in CAI’s .env.example. So, we need to create an .env file with the following variables:
PORTSWIGGER_USERNAME='your-portswigger-email'
PORTSWIGGER_PASSWORD='your-portswigger-password'
OPENAI_API_KEY='sk-123'
ANTHROPIC_API_KEY=''
OLLAMA=''
PROMPT_TOOLKIT_NO_CPR=1
CAI_STREAM=false✅ Note on PortSwigger: If you don’t have a PortSwigger Web Academy account, you can create one here.
✅ Note on CAI: If you need more information on setting up the .env file, check out the CAI documentation.
To extract the labs, we start by initialising the PortSwigger bot and loading the username and password from the .env file. Next, we specify the number of labs to retrieve and the type of vulnerability we want to target. Some of the supported vulnerability sections include:
✅ Note on Bot: Want to see all the vulnerability sections in action? Explore the full tutorial in the following Jupyter Notebook.
We should download the folder utils from this link, which contains the PortSwigger bot. We should also add these parameters to our main Python script.
import os
from dotenv import load_dotenv
load_dotenv(override=True)SECTION = "cross-site-scripting"
N_LABS = 3 # If you want to test all the labs in the section, change this to -1
USERNAME = os.getenv("PORTSWIGGER_USERNAME")
PASSWORD = os.getenv("PORTSWIGGER_PASSWORD")
We can now extract the lab information by running the following line of code.
#Portswigger web scraper bot
import utils.portswiggerbot as pb#Initialize bot
bot = pb.Bot()
#Login to Portswigger Academy
bot.login(USERNAME,PASSWORD)
#Get labs urls by section or type of lab
topics = bot.choose_topic(SECTION)
#Get lab metadata (returns a list of dictionaries)
labs = [bot.obtain_lab_information(link) for link in topics[0:N_LABS]]
#format session cookies
cookies = bot.driver.get_cookies()
# Define names of essential cookies
essential_cookies = {'SessionId','Authenticated_UserVerificationId','t'}
# Extract only the essential cookies
essential_cookies = [cookie for cookie in cookies if cookie['name'] in essential_cookies]
An Agent is an intelligent component that interacts with an environment. In cybersecurity, an Agent interacts with systems and networks, using peripherals and network interfaces as sensors, reasoning about the gathered information, and then executing network actions as if they were actuators. In CAI, there are different types of predefined agents. You can take a look here.
For this project, we will use the Red Team Base Agent. First, we need to specify the LLMs we want to use with it. We are going to use GTP 4.0 in this tutorial.
# Import red team agent
from cai.sdk.agents import Runner
from cai.agents.red_teamer import redteam_agent#function to run the agent asynchronously
async def run_agent(message):
response = await Runner.run(redteam_agent, message)
return response
# use gpt 4.0
os.environ["CAI_MODEL"] = "openai/gpt-4o"
CAI Agents work with two types of prompts:
This prompt defines the overall context and instructs the LLMs to assume a specific role. In this case, the model’s goal is to identify and exploit vulnerabilities.
✅ Note: When using the built-in Red Team Agent from CAI, the system prompt is already configured for you, so there is no need to set it manually. You can find the default template for the agent here.
This prompt defines the specific task the agent should perform. For PortSwigger Labs, it should include:
✅ Note: We will use the following user prompt, so it is possible to modify it and interact with or create other types of prompts for LLMs.
prompt_example = f"""
attack **LAB_ENVIRONMENT_URL**
The title is **LAB_TITLE**
The description is **LAB_DESCRIPTION** Please consider the following mandatory rules:
1. You have to connect to the session with the cookies **COOKIES**
2. Do not stop until you try all the commands or exploits you can run and create
3. Write the explanation of what you did in an HTML format easy to understand
4. Write every payload command and the exploits you create in the format <payloadgen>HERE</payloadgen>
5. Do not create files with the payload that you generate because you have to solve the lab by yourself
"""
We can format the user prompt with the lab metadata by running the following code.
user_prompt = (
prompt_example.replace("**LAB_ENVIRONMENT_URL**", lab['environment_url'])
.replace("**LAB_TITLE**", lab['title'])
.replace("**LAB_DESCRIPTION**", lab['description'])
.replace("**COOKIES**", str(essential_cookies))
)It’s time to have some fun hacking PortSwigger Labs!. Just call the function, and your agent will automatically interact with the target data, searching for vulnerabilities and trying to exploit them.
response = await run_agent(message=user_prompt)We will obtain a response like the following:
Zoom image will be displayed
Now that our CAI agent has done its work, let’s break down the results to understand what it found, how it exploited the target, and what the structure of the final response is.
When we run the CAI Red Team Agent, we get back a RunResult object. Think of it as a detailed report of everything the agent did during its hacking session.
✅ Note on the results: If you want to see the complete structure of the results, explore the full tutorial in the following Jupyter Notebook.
Here’s how to read it:
This is the user prompt we gave to the agent.
This shows what the agent produced during the run.
Contains the following information:
This is the raw output from the agent, showing exactly what text the LLM produced.
This is the final, cleaned-up version of the report. In our example, it’s a complete HTML file that explains:
In this article, we learned how to run a complete attack on the PortSwigger vulnerable lab using the CAI Python API.
Now you can:
This shows the power of combining LLM-driven reasoning with real hacking tools. You can now adapt this approach to test other labs, real applications, or integrate it into your red teaming workflows.
Next, try: