File size: 10,389 Bytes
8c486a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fedc25
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
"""End-to-end scripted demo of OpenRange episode lifecycle.

Demonstrates:
    1. Environment reset with a snapshot
    2. Red agent: recon -> exploit -> flag capture
    3. Blue agent: log analysis -> detection -> patching
    4. Reward computation for both roles
    5. Trajectory logging for SFT export

Runs without Docker (uses mock mode) and without any LLM.

Usage::

    python -m examples.demo
    # or
    uv run python examples/demo.py
"""

from __future__ import annotations

import sys
from pathlib import Path
from typing import Any

from open_range.protocols import (
    FlagSpec,
    GoldenPathStep,
    SnapshotSpec,
    TaskSpec,
    TruthGraph,
    Vulnerability,
)
from open_range.models import RangeAction, RangeObservation
from open_range.server.environment import RangeEnvironment
from open_range.training.trajectory import TrajectoryLogger


# ---------------------------------------------------------------------------
# Scripted agents
# ---------------------------------------------------------------------------


class ScriptedAgent:
    """Agent that follows a fixed command list. No LLM needed."""

    def __init__(self, script: list[str], role: str = "red") -> None:
        self.script = list(script)
        self.role = role
        self._step_idx = 0

    def reset(self, briefing: str, role: str) -> None:
        self.role = role
        self._step_idx = 0

    def act(self, observation: str) -> str:
        if self._step_idx < len(self.script):
            cmd = self.script[self._step_idx]
            self._step_idx += 1
            return cmd
        return "echo done"

    @property
    def exhausted(self) -> bool:
        return self._step_idx >= len(self.script)


# ---------------------------------------------------------------------------
# Demo snapshot (matches tier1_basic topology)
# ---------------------------------------------------------------------------


def make_demo_snapshot() -> SnapshotSpec:
    """Build a demo snapshot with a SQL injection vulnerability."""
    return SnapshotSpec(
        topology={
            "hosts": [
                "attacker", "firewall", "web", "mail",
                "db", "files", "ldap", "siem",
            ],
            "zones": {
                "external": ["attacker"],
                "dmz": ["web", "mail"],
                "internal": ["db", "files"],
                "management": ["ldap", "siem"],
            },
            "tier": 1,
        },
        truth_graph=TruthGraph(
            vulns=[
                Vulnerability(
                    id="vuln-sqli-1",
                    type="sqli",
                    host="web",
                    service="php",
                    injection_point="/search?q=",
                    vulnerable_code=(
                        "$conn->query(\"SELECT * FROM products "
                        "WHERE name LIKE '%$_GET[q]%'\");"
                    ),
                    root_cause="Unsanitized input in SQL query",
                    blast_radius="Full database read access",
                    remediation="Use parameterized queries",
                ),
            ],
        ),
        flags=[
            FlagSpec(
                id="flag-1",
                value="FLAG{test_sqli_123}",
                path="/var/flags/flag1.txt",
                host="db",
            ),
        ],
        golden_path=[
            GoldenPathStep(step=1, command="nmap -sV 10.0.1.0/24",
                           expect_in_stdout="80/tcp open http",
                           description="Port scan reveals web server"),
            GoldenPathStep(step=2, command="curl http://web/",
                           expect_in_stdout="Welcome",
                           description="Enumerate web application"),
            GoldenPathStep(step=3, command="curl 'http://web/search?q=test'",
                           expect_in_stdout="products",
                           description="Test search functionality"),
            GoldenPathStep(step=4,
                           command="curl 'http://web/search?q=test' UNION SELECT flag FROM flags--'",
                           expect_in_stdout="FLAG{test_sqli_123}",
                           description="Exploit SQL injection"),
            GoldenPathStep(step=5, command="submit_flag FLAG{test_sqli_123}",
                           expect_in_stdout="Correct",
                           description="Submit captured flag"),
        ],
        task=TaskSpec(
            red_briefing=(
                "Target network has a web application with database backend. "
                "Find vulnerabilities and capture the flag."
            ),
            blue_briefing=(
                "Monitor SIEM for suspicious activity. "
                "Investigate alerts, identify attacks, and apply patches."
            ),
        ),
    )


# ---------------------------------------------------------------------------
# Episode runner
# ---------------------------------------------------------------------------


def run_demo(
    env: RangeEnvironment | None = None,
    max_steps: int = 20,
    quiet: bool = False,
) -> dict[str, Any]:
    """Run a complete demo episode with scripted Red and Blue agents.

    Args:
        env: Optional pre-configured environment. If None, creates one
             in mock mode (no Docker required).
        max_steps: Maximum steps before forced termination.
        quiet: If True, suppress printed output.

    Returns:
        Dict with episode results and trajectory logger.
    """
    if env is None:
        env = RangeEnvironment(docker_available=False, max_steps=max_steps)

    snapshot = make_demo_snapshot()

    # Build scripted agents
    red = ScriptedAgent(
        script=[
            "nmap -sV 10.0.1.0/24",
            "curl http://web/",
            "curl 'http://web/search?q=test'",
            "curl 'http://web/search?q=test%27+UNION+SELECT+flag+FROM+flags--'",
            "submit_flag FLAG{test_sqli_123}",
        ],
        role="red",
    )

    blue = ScriptedAgent(
        script=[
            "tail -n 50 /var/log/siem/web_access.log",
            "grep -i 'union' /var/log/siem/web_access.log",
            "submit_finding SQLi attack detected from attacker targeting /search endpoint",
            "patch web /var/www/html/search.php",
        ],
        role="blue",
    )

    # Trajectory logger
    traj = TrajectoryLogger()

    def _print(msg: str) -> None:
        if not quiet:
            print(msg)

    # --- Episode start ---
    _print("=" * 60)
    _print("  OPENRANGE DEMO -- Scripted Red vs Blue Episode")
    _print("=" * 60)

    obs = env.reset(snapshot=snapshot, episode_id="demo-001")
    traj.start_episode(
        episode_id="demo-001",
        snapshot_id="tier1-sqli-demo",
        tier=1,
    )

    red.reset(briefing=obs.stdout, role="red")
    blue.reset(briefing=obs.stdout, role="blue")

    _print(f"\n{obs.stdout}\n")
    _print("-" * 60)

    step = 0
    last_obs = obs

    while not last_obs.done and step < max_steps:
        # Red's turn
        if red.exhausted:
            break

        red_cmd = red.act(last_obs.stdout)
        _print(f"\n[Step {step + 1}] RED >> {red_cmd}")
        red_obs = env.step(RangeAction(command=red_cmd, mode="red"))
        reward = red_obs.reward if red_obs.reward is not None else 0.0

        traj.log_turn(
            role="red",
            observation=last_obs.stdout,
            action=red_cmd,
            reward=float(reward),
        )

        _print(f"         stdout: {red_obs.stdout[:120]}{'...' if len(red_obs.stdout) > 120 else ''}")
        if red_obs.flags_captured:
            _print(f"         FLAGS CAPTURED: {red_obs.flags_captured}")
        _print(f"         reward: {reward:.4f}  done: {red_obs.done}")

        last_obs = red_obs
        step += 1

        if last_obs.done:
            break

        # Blue's turn
        if blue.exhausted:
            continue

        blue_cmd = blue.act(last_obs.stdout)
        _print(f"\n[Step {step + 1}] BLUE >> {blue_cmd}")
        blue_obs = env.step(RangeAction(command=blue_cmd, mode="blue"))
        reward = blue_obs.reward if blue_obs.reward is not None else 0.0

        traj.log_turn(
            role="blue",
            observation=last_obs.stdout,
            action=blue_cmd,
            reward=float(reward),
        )

        _print(f"          stdout: {blue_obs.stdout[:120]}{'...' if len(blue_obs.stdout) > 120 else ''}")
        if blue_obs.alerts:
            _print(f"          alerts: {blue_obs.alerts}")
        _print(f"          reward: {reward:.4f}  done: {blue_obs.done}")

        last_obs = blue_obs
        step += 1

    # Determine outcome
    if env.state.flags_found:
        outcome = "flag_captured"
    elif step >= max_steps:
        outcome = "timeout"
    else:
        outcome = "completed"

    episode = traj.end_episode(
        outcome=outcome,
        metrics={
            "steps": step,
            "flags_found": list(env.state.flags_found),
            "tier": env.state.tier,
        },
    )

    # --- Summary ---
    _print("\n" + "=" * 60)
    _print("  EPISODE SUMMARY")
    _print("=" * 60)
    _print(f"  Outcome:          {outcome}")
    _print(f"  Steps:            {step}")
    _print(f"  Flags found:      {env.state.flags_found}")
    _print(f"  Red total reward: {episode.total_red_reward:.4f}")
    _print(f"  Blue total reward:{episode.total_blue_reward:.4f}")
    _print(f"  Red turns:        {len(episode.red_turns)}")
    _print(f"  Blue turns:       {len(episode.blue_turns)}")
    _print("=" * 60)

    return {
        "outcome": outcome,
        "steps": step,
        "flags_found": list(env.state.flags_found),
        "episode": episode,
        "trajectory_logger": traj,
        "env": env,
    }


# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------


def main() -> None:
    """Run the demo and optionally export trajectories."""
    result = run_demo()

    # Export trajectories to JSONL
    traj: TrajectoryLogger = result["trajectory_logger"]
    out_path = Path("demo_trajectories.jsonl")
    count = traj.export_jsonl(out_path, reward_threshold=0.0)
    print(f"\nExported {count} trajectory records to {out_path}")


if __name__ == "__main__":
    main()