Spaces:
Runtime error
Runtime error
File size: 13,763 Bytes
8c486a8 769dd2e 8c486a8 49d1c75 8c486a8 f016eb7 8c486a8 f549fda 80ef9e0 f549fda 8c486a8 49d1c75 8c486a8 49d1c75 8c486a8 49d1c75 8c486a8 7fedc25 8c486a8 769dd2e 8c486a8 769dd2e 8c486a8 f016eb7 f549fda 8c486a8 595e190 8c486a8 7529adc 8c486a8 595e190 8c486a8 595e190 8c486a8 595e190 8c486a8 595e190 8c486a8 83f2912 8c486a8 83f2912 8c486a8 49d1c75 8c486a8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | """Agent protocols and shared Pydantic models for OpenRange.
Three pluggable infrastructure components:
- SnapshotBuilder: generates candidate snapshot specs from manifests
- NPCBehavior: decides NPC response to stimuli
- ValidatorCheck: single admission check in the validation pipeline
"""
from __future__ import annotations
from enum import Enum
from typing import Any, Literal, Protocol, runtime_checkable
from pydantic import AliasChoices, BaseModel, ConfigDict, Field
# ---------------------------------------------------------------------------
# Pydantic models — service lifecycle
# ---------------------------------------------------------------------------
class ReadinessCheck(BaseModel):
"""How to verify a service is ready after starting.
Supports three probe types:
- ``tcp``: connect to *port* on localhost.
- ``http``: GET *url* and expect a 2xx response.
- ``command``: run *command* and expect exit code 0.
"""
type: Literal["tcp", "http", "command"] = "tcp"
port: int = 0
url: str = ""
command: str = ""
timeout_s: int = 30
interval_s: float = 1.0
class ServiceSpec(BaseModel):
"""Declarative service lifecycle for subprocess mode.
Generated by the Renderer alongside docker-compose.yml.
Consumed by ``RangeEnvironment._start_snapshot_services()``.
Each entry describes one daemon that must be running for the snapshot
to function. The *host* field links back to the topology host name
so that stop/restart logic can correlate services to logical hosts.
"""
host: str
daemon: str
packages: list[str] = Field(default_factory=list)
init_commands: list[str] = Field(default_factory=list)
start_command: str
readiness: ReadinessCheck = Field(default_factory=ReadinessCheck)
log_dir: str = ""
env_vars: dict[str, str] = Field(default_factory=dict)
# ---------------------------------------------------------------------------
# Pydantic models — build context & topology
# ---------------------------------------------------------------------------
class BuildContext(BaseModel):
"""Runtime context passed to the Builder on each build() call."""
seed: int | None = None
tier: int = 1
previous_vuln_classes: list[str] = Field(default_factory=list)
red_solve_rate: float = 0.0
blue_detect_rate: float = 0.0
weak_areas: list[str] = Field(default_factory=list)
recent_attack_surfaces: list[str] = Field(default_factory=list)
episode_count: int = 0
# Narrative guidance from curriculum
narrative_hints: list[str] = Field(
default_factory=list,
description="Curriculum-driven hints, e.g. 'include lateral movement via credential reuse'",
)
require_chain_length: int = Field(
default=0,
description="If > 0, force multi-hop exploit chains of at least this length",
)
focus_layer: str = Field(
default="",
description="Which realism layer to emphasize: 'infra', 'app', 'identity', 'process'",
)
class MutationOp(BaseModel):
"""Single typed edit applied to derive a child snapshot from a parent."""
mutation_id: str
op_type: str
target_selector: dict[str, str] = Field(default_factory=dict)
magnitude: int = 1
params: dict[str, Any] = Field(default_factory=dict)
expected_effects: list[str] = Field(default_factory=list)
risk_tags: list[str] = Field(default_factory=list)
class MutationPlan(BaseModel):
"""Ordered list of mutations used to produce a child snapshot."""
parent_snapshot_id: str | None = None
ops: list[MutationOp] = Field(default_factory=list)
predicted_complexity_delta: int = 0
predicted_chain_delta: int = 0
predicted_novelty: float = 0.0
policy_name: str = ""
policy_score: float = 0.0
score_breakdown: dict[str, float] = Field(default_factory=dict)
class LineageMetadata(BaseModel):
"""Lineage and mutation provenance for a stored snapshot."""
snapshot_id: str = ""
parent_snapshot_id: str | None = None
root_snapshot_id: str = ""
manifest_id: str = ""
generation_depth: int = 0
mutation_ids: list[str] = Field(default_factory=list)
mutation_summary: list[str] = Field(default_factory=list)
class Vulnerability(BaseModel):
"""Single planted vulnerability in the truth graph."""
id: str
type: str # e.g. sqli, xss, idor, ssrf
host: str
service: str = ""
injection_point: str = ""
vulnerable_code: str | dict[str, str] = "" # str or {file_path: snippet}
root_cause: str = ""
blast_radius: str = ""
remediation: str = ""
class ExploitStep(BaseModel):
"""Single step in an exploit chain."""
model_config = ConfigDict(populate_by_name=True)
vuln_id: str = Field(validation_alias=AliasChoices("vuln_id", "vuln"))
command: str = Field(validation_alias=AliasChoices("command", "action"))
description: str = Field(default="", validation_alias=AliasChoices("description", "yields"))
class TruthGraph(BaseModel):
"""Ground truth about planted vulnerabilities and exploit chains."""
vulns: list[Vulnerability] = Field(default_factory=list)
exploit_chain: list[ExploitStep] = Field(default_factory=list)
class GoldenPathStep(BaseModel):
"""Single step in the golden path walkthrough."""
model_config = ConfigDict(populate_by_name=True)
step: int
command: str = Field(validation_alias=AliasChoices("command", "cmd"))
expect_in_stdout: str = Field(
default="",
validation_alias=AliasChoices("expect_in_stdout", "expect_stdout"),
)
host: str = "attacker"
description: str = ""
class FlagSpec(BaseModel):
"""Flag definition: value and where it lives."""
id: str
value: str
path: str
host: str
class EvidenceItem(BaseModel):
"""Expected evidence artifact for Blue to find."""
type: str # log_entry, alert, file
location: str
pattern: str = ""
class NPCPersona(BaseModel):
"""NPC persona card for LLM-driven NPC behavior."""
name: str
role: str = ""
department: str = ""
reports_to: str = ""
communication_style: str = ""
security_awareness: float = 0.5 # 0.0-1.0
susceptibility: dict[str, float] = Field(default_factory=dict)
routine: dict[str, Any] = Field(default_factory=dict)
accounts: dict[str, Any] = Field(default_factory=dict)
class NPCTrafficSpec(BaseModel):
"""NPC traffic configuration."""
level: int = 0 # 0=shell scripts, 1=LLM personas
rate_lambda: float = 10.0 # requests/minute
scripts: list[str] = Field(default_factory=list)
class TaskType(str, Enum):
"""Types of tasks agents can be assigned."""
EXPLOIT = "exploit"
INVESTIGATE = "investigate"
PATCH = "patch"
REPORT = "report"
ENDPOINT_QUERY = "endpoint_query"
MULTI_STEP = "multi_step"
class TaskSpec(BaseModel):
"""Agent-facing task descriptions (no leakage of internals)."""
red_briefing: str = ""
blue_briefing: str = ""
task_type: str = "exploit" # Use str not enum for flexibility
milestones: list[str] = Field(default_factory=list) # For multi_step tasks
success_conditions: list[dict[str, Any]] = Field(
default_factory=list,
) # [{type: "flag", value: "..."}, {type: "endpoint", url: "...", expect: "..."}]
class SnapshotSpec(BaseModel):
"""Complete specification for a generated range snapshot."""
topology: dict[str, Any] = Field(default_factory=dict)
truth_graph: TruthGraph = Field(default_factory=TruthGraph)
golden_path: list[GoldenPathStep] = Field(default_factory=list)
flags: list[FlagSpec] = Field(default_factory=list)
evidence_spec: list[EvidenceItem] = Field(default_factory=list)
npc_personas: list[NPCPersona] = Field(default_factory=list)
npc_traffic: NPCTrafficSpec = Field(default_factory=NPCTrafficSpec)
task: TaskSpec = Field(default_factory=TaskSpec)
compose: dict[str, Any] = Field(default_factory=dict) # rendered docker-compose
files: dict[str, str] = Field(default_factory=dict) # path -> content
services: list[ServiceSpec] = Field(default_factory=list) # subprocess-mode daemons
lineage: LineageMetadata = Field(default_factory=LineageMetadata)
mutation_plan: MutationPlan | None = None
class Stimulus(BaseModel):
"""Incoming stimulus for an NPC to react to."""
type: str = "email" # email, chat, file_access, voice
sender: str = ""
subject: str = ""
content: str = ""
attachments: list[str] = Field(default_factory=list)
plausibility: float = 0.5 # 0.0-1.0
class NPCAction(BaseModel):
"""NPC's decided response to a stimulus."""
action: str = "ignore" # click_link, open_attachment, reply, share_credentials,
# ignore, report_to_IT, forward
response_content: str = ""
side_effects: list[str] = Field(default_factory=list)
class CheckResult(BaseModel):
"""Result of a single validator check."""
name: str = ""
passed: bool = False
time_s: float = 0.0
details: dict[str, Any] = Field(default_factory=dict)
error: str = ""
advisory: bool = False # if True, failure triggers retry but never blocks
class ExecResult(BaseModel):
"""Structured command execution result."""
stdout: str = ""
stderr: str = ""
exit_code: int = 0
timed_out: bool = False
@property
def combined_output(self) -> str:
parts = [self.stdout, self.stderr]
return "\n".join(part for part in parts if part).strip()
class ContainerSet(BaseModel):
"""Handle to live Docker containers for a snapshot."""
model_config = ConfigDict(arbitrary_types_allowed=True)
project_name: str = ""
container_ids: dict[str, str] = Field(default_factory=dict) # service -> id
async def exec_run(self, container: str, cmd: str, timeout: float = 30.0) -> ExecResult:
"""Run *cmd* inside *container* and return structured output + status."""
import asyncio
cid = self.container_ids.get(container, container)
proc = await asyncio.create_subprocess_exec(
"docker", "exec", cid, "sh", "-c", cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
try:
stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout=timeout)
except asyncio.TimeoutError:
proc.kill()
try:
await proc.communicate()
except Exception: # noqa: BLE001
pass
return ExecResult(stderr="<timeout>", exit_code=124, timed_out=True)
return ExecResult(
stdout=(stdout or b"").decode(errors="replace"),
stderr=(stderr or b"").decode(errors="replace"),
exit_code=int(proc.returncode or 0),
)
async def exec(self, container: str, cmd: str, timeout: float = 30.0) -> str:
"""Backward-compatible string output helper around ``exec_run``."""
result = await self.exec_run(container, cmd, timeout=timeout)
return result.combined_output
async def is_healthy(self, container: str) -> bool:
"""Return True when *container* is running and its healthcheck passes."""
import asyncio
cid = self.container_ids.get(container, container)
proc = await asyncio.create_subprocess_exec(
"docker",
"inspect",
"--format",
"{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}",
cid,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, _ = await proc.communicate()
status = (stdout or b"").decode().strip()
return status in {"running", "healthy"}
async def cp(self, container: str, src: str, dest: str) -> None:
"""Copy a file into a container: ``docker cp src container:dest``."""
import asyncio
cid = self.container_ids.get(container, container)
proc = await asyncio.create_subprocess_exec(
"docker", "cp", src, f"{cid}:{dest}",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await proc.communicate()
async def restart(self, container: str, timeout: float = 30.0) -> None:
"""Restart *container* via ``docker restart``, restoring pre-patched state."""
import asyncio
cid = self.container_ids.get(container, container)
proc = await asyncio.create_subprocess_exec(
"docker", "restart", cid,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
try:
await asyncio.wait_for(proc.communicate(), timeout=timeout)
except asyncio.TimeoutError:
proc.kill()
# ---------------------------------------------------------------------------
# Protocols
# ---------------------------------------------------------------------------
@runtime_checkable
class SnapshotBuilder(Protocol):
"""Generate a candidate snapshot spec from a manifest."""
async def build(
self,
manifest: dict,
context: BuildContext,
) -> SnapshotSpec: ...
@runtime_checkable
class NPCBehavior(Protocol):
"""Decide how an NPC responds to a stimulus."""
async def decide(
self,
persona: NPCPersona,
stimulus: Stimulus,
) -> NPCAction: ...
@runtime_checkable
class ValidatorCheck(Protocol):
"""Single check in the validator admission pipeline."""
async def check(
self,
snapshot: SnapshotSpec,
containers: ContainerSet,
) -> CheckResult: ...
|