# applejuice8/ctf-agent architecture

> Architecture diagram of the GitHub repository applejuice8/ctf-agent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/applejuice8/ctf-agent
- Repository: https://github.com/applejuice8/ctf-agent

## Overview

#### Purpose and entry points
CTF Agent is a local, materials-first solver for authorised CTF challenges. It inventories supplied challenge files, uses an LLM to classify the task and select a constrained Docker sandbox, iteratively investigates, extracts a flag, and writes logs/writeup artifacts. Primary entry point: `scripts/run_all.sh`; manual invocation support: `scripts/solve_manual.py`.

A separate, pinned live-CTF swarm integration is isolated under `integrations/verialabs-ctf-agent`, launched through `scripts/run_veria_ctf_agent.sh`.

#### Orchestration pipeline
The core control loop is split across:
- `ctf_agent/solver/loop.py`
- `ctf_agent/classifier.py`
- `ctf_agent/solver/flag_extract.py`

The runner first derives a safe material inventory and sends only the description plus metadata to the classifier. The classifier selects a CTF theme and minimal initial sandbox capabilities. The solver loop then gives the model bounded tool turns, preserves evidence, detects a flag, and controls completion or model handoff.

#### Model routing and continuity
`ctf_agent/models/client.py` implements provider access and route rotation across Gemini keys, Groq, and OpenRouter. `ctf_agent/config.py` supplies route, credential, budget, and runtime settings.

When quota, availability, or iteration limits require replacement, `ctf_agent/handoff.py` creates a compact state transfer containing prior assessments, commands, outputs, scope, and next action. This makes model changes a continuation of one solve rather than independent attempts.

#### Sandbox planning and execution boundary
Sandbox scope is planned before execution:
- `ctf_agent/sandbox/planner.py`
- `ctf_agent/sandbox/manager.py`
- `ctf_agent/sandbox/executor.py`

The planner converts theme/capability choices into a manifest; the manager builds or reuses a Docker image keyed by that manifest; the executor runs commands inside it. Challenge materials are mounted read-only, temporary work stays in disposable container `/tmp`, and host-side execution is avoided. The solver can request one logged, evidence-driven capability expansion.

Sandbox images are defined by `docker/Dockerfile.sandbox`. Docker is therefore the principal isolation runtime.

#### Web egress boundary
Web challenges are exceptional: only explicitly declared HTTP(S) targets are reachable. `docker/egress_proxy.py` and `docker/Dockerfile.egress-proxy` provide the private allow-list proxy layer. Connectivity is checked before consuming solver turns; loopback, private, and undeclared destinations are rejected. Normal sandbox operation has no network.

#### Inputs, state, and outputs
Required challenge input is under `challenge_materials/`, including `challenge_materials/CHALLENGE_DESCRIPTION.md`. Shared domain structures are defined in `ctf_agent/types.py`.

Persistent run evidence is written beneath `logs/`: classification decision, sandbox plan, transcript, handoff brief, and generated writeup. `ctf_agent/writeup.py` turns final evidence into a human-readable result. Resume safety is based on matching the description and material inventory, preventing evidence reuse across different challenges.

#### Optional knowledge and external integration
`ctf_agent/skills/build_index.py` and `ctf_agent/skills/router.py` optionally index a local `ctf-skills` checkout and provide post-classification technique hints; this does not affect sandbox selection.

`integrations/verialabs-ctf-agent` is a separate submodule for authorised CTFd competitions, with its own Python/dependency stack, multi-model coordination, and optional flag submission. It is intentionally not part of the local solver’s runtime path.

Main entry points: [`ctf_agent/solver/loop.py`](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/solver/loop.py) (Solver control loop), [`ctf_agent/classifier.py`](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/classifier.py) (Task classifier), [`ctf_agent/sandbox/executor.py`](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/sandbox/executor.py) (Sandbox executor), [`ctf_agent/sandbox/manager.py`](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/sandbox/manager.py) (Sandbox manager)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_local["Local solver"]
  node_run_all["Automated runner<br/>shell entrypoint<br/>[run_all.sh]"]
  node_manual["Manual solver<br/>CLI entrypoint<br/>[solve_manual.py]"]
  node_materials["Challenge materials<br/>input corpus"]
  node_types["Shared types<br/>domain model<br/>[types.py]"]
  node_classifier["Task classifier<br/>LLM classification<br/>[classifier.py]"]
  node_solver_loop{{"Solver control loop<br/>orchestrator<br/>[loop.py]"}}
  node_flag_extract["Flag extraction<br/>result detector<br/>[flag_extract.py]"]
  node_model_client["Model router<br/>provider client<br/>[client.py]"]
  node_config["Runtime settings<br/>configuration<br/>[config.py]"]
  node_handoff["Solve handoff<br/>state transfer<br/>[handoff.py]"]
  node_evidence[("Run evidence<br/>persistent artifacts")]
  node_writeup["Writeup generator<br/>report builder<br/>[writeup.py]"]
end

subgraph group_sandbox["Docker isolation"]
  node_planner["Sandbox planner<br/>capability planner<br/>[planner.py]"]
  node_manager["Sandbox manager<br/>image lifecycle<br/>[manager.py]"]
  node_executor["Sandbox executor<br/>command runner<br/>[executor.py]"]
  node_sandbox_image["Sandbox image<br/>Docker image definition<br/>[Dockerfile.sandbox]"]
  node_egress_proxy{{"Allow-list egress proxy<br/>network boundary<br/>[egress_proxy.py]"}}
  node_egress_image["Proxy image<br/>Docker image definition"]
end

subgraph group_optional["Optional systems"]
  node_skills["Skills hints<br/>optional knowledge router<br/>[router.py]"]
  node_build_index["Skills index builder<br/>local indexer<br/>[build_index.py]"]
  node_veria{{"Veria live-CTF swarm<br/>isolated integration"}}
  node_veria_runner["Veria launcher<br/>shell entrypoint"]
end

node_run_all -->|"inventories"| node_materials
node_manual -->|"starts"| node_solver_loop
node_materials -->|"description + metadata"| node_classifier
node_types -.->|"uses"| node_classifier
node_classifier -->|"theme + capabilities"| node_planner
node_classifier -.->|"post-classification hints"| node_skills
node_build_index -.->|"builds local index"| node_skills
node_planner -->|"sandbox manifest"| node_manager
node_manager -->|"builds or reuses"| node_sandbox_image
node_manager -->|"prepared image"| node_executor
node_solver_loop -->|"bounded model turns"| node_model_client
node_config -.->|"routes and budgets"| node_model_client
node_solver_loop -->|"logged commands"| node_executor
node_executor -.->|"read-only mount"| node_materials
node_executor -.->|"web challenge traffic"| node_egress_proxy
node_egress_image -->|"defines runtime"| node_egress_proxy
node_solver_loop -->|"candidate evidence"| node_flag_extract
node_model_client -.->|"quota or limit replacement"| node_handoff
node_handoff -->|"continuation state"| node_solver_loop
node_solver_loop -->|"transcript and decisions"| node_evidence
node_flag_extract -->|"final evidence"| node_writeup
node_writeup -->|"generated writeup"| node_evidence
node_veria_runner -->|"launches separately"| node_veria

click node_run_all "https://github.com/applejuice8/ctf-agent/blob/main/scripts/run_all.sh"
click node_manual "https://github.com/applejuice8/ctf-agent/blob/main/scripts/solve_manual.py"
click node_types "https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/types.py"
click node_classifier "https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/classifier.py"
click node_skills "https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/skills/router.py"
click node_build_index "https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/skills/build_index.py"
click node_solver_loop "https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/solver/loop.py"
click node_flag_extract "https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/solver/flag_extract.py"
click node_model_client "https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/models/client.py"
click node_config "https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/config.py"
click node_handoff "https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/handoff.py"
click node_planner "https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/sandbox/planner.py"
click node_manager "https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/sandbox/manager.py"
click node_executor "https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/sandbox/executor.py"
click node_sandbox_image "https://github.com/applejuice8/ctf-agent/blob/main/docker/Dockerfile.sandbox"
click node_egress_proxy "https://github.com/applejuice8/ctf-agent/blob/main/docker/egress_proxy.py"
click node_egress_image "https://github.com/applejuice8/ctf-agent/blob/main/docker/Dockerfile.egress-proxy"
click node_writeup "https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/writeup.py"
click node_veria "https://github.com/applejuice8/ctf-agent/tree/main/integrations/verialabs-ctf-agent"
click node_veria_runner "https://github.com/applejuice8/ctf-agent/blob/main/scripts/run_veria_ctf_agent.sh"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_run_all,node_manual,node_materials,node_types,node_classifier,node_solver_loop,node_flag_extract,node_model_client,node_config,node_handoff,node_evidence,node_writeup toneBlue
class node_planner,node_manager,node_executor,node_sandbox_image,node_egress_proxy,node_egress_image toneAmber
class node_skills,node_build_index,node_veria,node_veria_runner toneMint
```

## Components

### Local solver

Materials-first CTF solve pipeline.

- [Automated runner](https://github.com/applejuice8/ctf-agent/blob/main/scripts/run_all.sh): shell entrypoint · `scripts/run_all.sh`
- [Manual solver](https://github.com/applejuice8/ctf-agent/blob/main/scripts/solve_manual.py): CLI entrypoint · `scripts/solve_manual.py`
- **Challenge materials**: input corpus · Includes the required challenge description.
- [Shared types](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/types.py): domain model · `ctf_agent/types.py`
- [Task classifier](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/classifier.py): LLM classification · Uses description and inventory metadata only. · `ctf_agent/classifier.py`
- [Solver control loop](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/solver/loop.py): orchestrator · Bounds tool turns, preserves evidence, and controls completion. · `ctf_agent/solver/loop.py`
- [Flag extraction](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/solver/flag_extract.py): result detector · `ctf_agent/solver/flag_extract.py`
- [Model router](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/models/client.py): provider client · Rotates Gemini, Groq, and OpenRouter routes. · `ctf_agent/models/client.py`
- [Runtime settings](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/config.py): configuration · Supplies routes, credentials, budgets, and runtime settings. · `ctf_agent/config.py`
- [Solve handoff](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/handoff.py): state transfer · Carries assessments, evidence, scope, and next action to a replacement model. · `ctf_agent/handoff.py`
- **Run evidence**: persistent artifacts · Stores decisions, plans, transcript, handoff brief, and writeup.
- [Writeup generator](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/writeup.py): report builder · `ctf_agent/writeup.py`

### Docker isolation

Planned, disposable execution boundary.

- [Sandbox planner](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/sandbox/planner.py): capability planner · Converts theme and capability choices into a manifest. · `ctf_agent/sandbox/planner.py`
- [Sandbox manager](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/sandbox/manager.py): image lifecycle · Builds or reuses manifest-keyed Docker images. · `ctf_agent/sandbox/manager.py`
- [Sandbox executor](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/sandbox/executor.py): command runner · Runs commands with read-only materials and disposable /tmp work. · `ctf_agent/sandbox/executor.py`
- [Sandbox image](https://github.com/applejuice8/ctf-agent/blob/main/docker/Dockerfile.sandbox): Docker image definition · `docker/Dockerfile.sandbox`
- [Allow-list egress proxy](https://github.com/applejuice8/ctf-agent/blob/main/docker/egress_proxy.py): network boundary · Permits only declared HTTP(S) targets for web challenges. · `docker/egress_proxy.py`
- [Proxy image](https://github.com/applejuice8/ctf-agent/blob/main/docker/Dockerfile.egress-proxy): Docker image definition · `docker/Dockerfile.egress-proxy`

### Optional systems

Not on the local solver runtime path.

- [Skills hints](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/skills/router.py): optional knowledge router · Provides post-classification technique hints only. · `ctf_agent/skills/router.py`
- [Skills index builder](https://github.com/applejuice8/ctf-agent/blob/main/ctf_agent/skills/build_index.py): local indexer · `ctf_agent/skills/build_index.py`
- [Veria live-CTF swarm](https://github.com/applejuice8/ctf-agent/tree/main/integrations/verialabs-ctf-agent): isolated integration · Separate pinned CTFd integration with its own runtime. · `integrations/verialabs-ctf-agent`
- [Veria launcher](https://github.com/applejuice8/ctf-agent/blob/main/scripts/run_veria_ctf_agent.sh): shell entrypoint · `scripts/run_veria_ctf_agent.sh`

## Connections

- Automated runner → Challenge materials: inventories
- Manual solver → Solver control loop: starts
- Challenge materials → Task classifier: description + metadata
- Shared types → Task classifier: uses
- Task classifier → Sandbox planner: theme + capabilities
- Task classifier → Skills hints: post-classification hints
- Skills index builder → Skills hints: builds local index
- Sandbox planner → Sandbox manager: sandbox manifest
- Sandbox manager → Sandbox image: builds or reuses
- Sandbox manager → Sandbox executor: prepared image
- Solver control loop → Model router: bounded model turns
- Runtime settings → Model router: routes and budgets
- Solver control loop → Sandbox executor: logged commands
- Sandbox executor → Challenge materials: read-only mount
- Sandbox executor → Allow-list egress proxy: web challenge traffic
- Proxy image → Allow-list egress proxy: defines runtime
- Solver control loop → Flag extraction: candidate evidence
- Model router → Solve handoff: quota or limit replacement
- Solve handoff → Solver control loop: continuation state
- Solver control loop → Run evidence: transcript and decisions
- Flag extraction → Writeup generator: final evidence
- Writeup generator → Run evidence: generated writeup
- Veria launcher → Veria live-CTF swarm: launches separately
