| # Cybench: A Framework for Evaluating Cybersecurity Capabilities and Risk | |
| This repository contains code used in the Cybench project ([website](https://cybench.github.io), [paper](https://arxiv.org/abs/2408.08926)), which serves to evaluate agent capabilities and risks on cybersecurity tasks. | |
| Cybench includes the following features: | |
| - Collection of 40 tasks from four distinct Capture the Flag (CTF) competitions | |
| - Subtasks for more gradated evaluation | |
| - Cybersecurity agent for evaluating models on tasks | |
| [Video demo](https://drive.google.com/file/d/1pFOqSRLxhOxE0srICyT27ZRqTLKFR4NB/view?usp=sharing) of an unguided run. | |
| ## | |
| There's an all-new, real-world [BountyBench](https://bountybench.github.io/) that evaluates offensive and defensive cybersecurity agents on vulnerability detection, exploitation, and patching with dollar impact. Check it out [here](https://bountybench.github.io/). | |
| ## Quick Start | |
| ### Environment Setup | |
| #### Set secrets (API Keys) | |
| First you need to add a HELM or model-specific API key so that the agent can call an underlying model. Add API keys to the `.env` file in root (see `.env.example` as a template). | |
| The HELM API provides a unified way to access various language models, enabling consistent evaluation across different models. For more information, please visit [HELM](https://github.com/stanford-crfm/helm). | |
| ```bash | |
| HELM_API_KEY={HELM_API_KEY} | |
| OPENAI_API_KEY={OPENAI_API_KEY} | |
| AZURE_OPENAI_API_KEY={AZURE_OPENAI_API_KEY} | |
| AZURE_OPENAI_ENDPOINT={AZURE_OPENAI_ENDPOINT} | |
| ANTHROPIC_API_KEY={ANTHROPIC_API_KEY} | |
| GOOGLE_API_KEY={GOOGLE_API_KEY}} | |
| TOGETHER_API_KEY={TOGETHER_API_KEY} | |
| ``` | |
| ##### Azure OpenAI Setup | |
| The `--azure` flag is used to run OpenAI models using the Azure OpenAI API. See the [Azure OpenAI docs](https://learn.microsoft.com/en-us/azure/ai-services/openai/chatgpt-quickstart?tabs=command-line,python-new&pivots=programming-language-studio) for instructions on how to configure the following environment variables. | |
| ```bash | |
| AZURE_OPENAI_ENDPOINT=https://cybench.openai.azure.com | |
| AZURE_OPENAI_API_KEY={AZURE_OPENAI_API_KEY} | |
| ``` | |
| Take care to remove the trailing slash `/` from the `AZURE_OPENAI_ENDPOINT` variable. Ensure the deployment name matches the model name (i.e. gpt-4o-2024-05-13). | |
| #### Add a New Model | |
| Refer to [docs/add_model](https://github.com/andyzorigin/cybench/blob/main/docs/add_model.md). | |
| #### Add a New Task | |
| Refer to [docs/add_task](https://github.com/andyzorigin/cybench/blob/main/docs/add_task.md). | |
| #### Python Setup | |
| You need to have `python3` installed where `python3 --version` is between `3.9` and `3.10` and install project dependencies. | |
| ```bash | |
| pip3 install -r requirements.txt | |
| ``` | |
| You can do this via a virtual environment | |
| ```bash | |
| python3 -m venv venv | |
| source venv/bin/activate | |
| pip3 install -r requirements.txt | |
| ``` | |
| ### Running a single task | |
| There are two modes of runs: unguided and subtask mode. Unguided mode has a single objective and returns a single performance metric: _unguided performance_ for success or failure on the task. Subtask mode has multiple subtasks and returns two performance metrics: _subtask performance_, which measures the performance on the subtasks, and outputs a fractional score based on the fraction of subtasks solved (e.g. $\frac{4}{5}$) and _subtask-guided performance_, which measures the performance on the final subtask only, and outputs a binary score (e.g. $1$). | |
| Both modes are run using the `run_task.sh` script: | |
| Usage: | |
| ```bash | |
| ./run_task.sh --task_dir <path> --max_iterations <number> --model <model> [--unguided_mode] [--max_input_tokens <number>] [--interactive_mode] [--extend_iterations_from_log <logfile>] | |
| ``` | |
| - `--task_dir`:the directory that contains the task, e.g. "benchmark/hackthebox/cyber-apocalypse-2024/crypto/[Very Easy] Dynastic"; a list of the directories of all tasks are available at task_list.txt and for tasks with subtasks at subtask_list.csv | |
| - `--max_iterations`:the maximum number of iterations for a given task for unguided runs, and for each subtask for subtask runs, e.g. 15 | |
| - `--model`: the deployment name of the model to run, e.g. "openai/gpt-4o-2024-05-13"; a list of all available models is available here: https://github.com/andyzorigin/cybench/blob/main/agent/dataclasses/agent_spec.py | |
| - `--unguided_mode`: whether to run on unguided mode | |
| - `--max_input_tokens`: the limit on the number of input tokens sent to the model; truncates if it exceeds this value. e.g. 6000 | |
| - `--max_output_tokens`: the limit on the number of output tokens that can be received from the model; e.g. 2000 | |
| - `--interactive_mode`: whether to run on interactive mode | |
| - `--extend_iterations_from_log`: whether to continue running based on an existing log | |
| - `--helm`: whether to run using HELM API or model-specific API | |
| - `--azure`: whether to run OpenAI models using the Azure OpenAI API or default OpenAI API | |
| At the end of the run, it will output a json file containing information about the run, including inputs, outputs, metadata, and score. This path is provided at the end of the run, and will be under the `logs` directory tracked by model, task, and datetime. | |
| #### Example Unguided Mode Run | |
| ```bash | |
| ./run_task.sh --task_dir "benchmark/hackthebox/cyber-apocalypse-2024/crypto/[Very Easy] Dynastic" --max_iterations 15 --unguided_mode --max_input_tokens 6000 --model openai/gpt-4o-2024-05-13 | |
| ``` | |
| #### Example Subtask Mode Run | |
| ```bash | |
| ./run_task.sh --task_dir "benchmark/hackthebox/cyber-apocalypse-2024/crypto/[Very Easy] Dynastic" --max_iterations 5 --max_input_tokens 6000 --model openai/gpt-4o-2024-05-13 | |
| ``` | |
| ## Running the benchmark | |
| To run the entire benchmark (which includes all tasks), you can use the `run_benchmark.py` script. The script takes the following arguments: | |
| ```bash | |
| python3 --model <model> run_benchmark.py [--unguided_mode] [--max_iterations <number>] [--max_input_tokens <number>] [--max_output_tokens <number>] [--task_list <str>] | |
| ``` | |
| ### Sample Unguided Mode 'Run Benchmark' Command | |
| ```bash | |
| python3 run_benchmark.py --max_iterations 15 --unguided_mode --max_input_tokens 6000 --max_output_tokens 2000 --model openai/gpt-4-turbo-2024-04-09 | |
| ``` | |
| ### Sample Subtask Mode 'Run Benchmark' Command | |
| ```bash | |
| python3 run_benchmark.py --max_iterations 5 --max_input_tokens 6000 --max_output_tokens 2000 --model openai/gpt-4-turbo-2024-04-09 | |
| ``` | |
| The `run_benchmark.py` script will: | |
| 1. Load in the list of tasks from the `task_list` file passed in or either the `task_list.txt` or `subtask_list.txt` files depending on the type of run. | |
| 2. Run each task with the provided arguments (`--unguided_mode`, `--max_iterations`, `--max_input_tokens`, `--max_output_tokens`) and the specified model (`--model`). | |
| 3. Output a log for each run under the `/logs/` directory. | |
| After the benchmark run is complete, all logs will be present in the `/logs/` directory. To view the results of your benchmark, run the `grade_benchmark.py` script. | |
| The available arguments for `run_benchmark.py` are: | |
| - `--model`: the deployment name of the model to run, e.g. "openai/gpt-4o-2024-05-13"; a list of all available models is available here: `https://github.com/andyzorigin/cybench/blob/main/agent/dataclasses/agent_spec.py` | |
| - `--max_iterations`:the maximum number of iterations for a given task for unguided runs, and for each subtask for subtask runs, e.g. 15 | |
| - `--unguided_mode`: whether to run on unguided mode | |
| - `--max_input_tokens`: the limit on the number of input tokens sent to the model; truncates if it exceeds this value. e.g. 6000 | |
| - `--max_output_tokens`: the limit on the number of output tokens that can be received from the model; e.g. 2000 | |
| - `--task_list`: the list of tasks to run on, e.g. `task_list.txt` | |
| - `--helm`: whether to run using HELM API or model-specific API | |
| - `--azure`: whether to run OpenAI models using the Azure OpenAI API or default OpenAI API | |
| Make sure to have the required environment variables set in the `.env` file before running the benchmark. | |
| ## Grading the benchmark | |
| To grade the benchmark, you can use the `grade_benchmark.py` script. Simply run: | |
| `python3 grade_benchmark.py` | |
| to output the results of any runs which have logs. | |
| ## Debugging | |
| ### Docker Debugging | |
| To debug, use the command to exec into the container | |
| ```bash | |
| docker run -it --network=shared_net --name cybench --entrypoint /usr/local/bin/dockerd-entrypoint.sh -v "$(pwd)/agent":/app/agent -v "$(pwd)/run_task.py":/app/run_task.py --env-file .env --rm --privileged --cgroupns host cybench/cybench:latest /bin/bash | |
| ``` | |