File size: 11,157 Bytes
21cee38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4948eb1
21cee38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4948eb1
 
 
 
 
 
 
21cee38
 
 
 
4948eb1
 
 
 
 
 
 
99953d3
4948eb1
21cee38
 
 
 
4948eb1
 
 
 
 
 
 
 
 
 
 
21cee38
 
 
 
4948eb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
596f8cb
 
4948eb1
 
 
 
21cee38
 
 
 
 
 
 
4948eb1
21cee38
 
4948eb1
21cee38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4948eb1
21cee38
4948eb1
21cee38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4948eb1
21cee38
 
 
 
 
 
 
 
 
 
 
 
4948eb1
 
 
 
 
 
 
 
21cee38
 
 
 
 
 
 
 
 
 
 
 
4948eb1
 
 
 
 
 
21cee38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
StateStrike Inference Script
============================
Runs an LLM agent against all 3 StateStrike tasks and emits
structured [START]/[STEP]/[END] logs for automated scoring.

Environment variables:
  API_BASE_URL      LLM endpoint (default: https://router.huggingface.co/v1)
  MODEL_NAME        Model identifier (default: Qwen/Qwen2.5-72B-Instruct)
  HF_TOKEN          Hugging Face API token (required, no default)
  LOCAL_IMAGE_NAME  Docker image name if using from_docker_image()
"""

from __future__ import annotations

import asyncio
import json
import os
import re
import textwrap
from typing import List, Optional

from openai import OpenAI

from statestrike_env.environment import StateStrikeEnv
from statestrike_env.models import EndpointChoice, PayloadStrategy, StateStrikeAction

API_KEY = os.getenv("HF_TOKEN") or os.getenv("API_KEY")
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")
BENCHMARK = "statestrike"


def log_start(task: str, env: str, model: str) -> None:
    print(f"[START] task={task} env={env} model={model}", flush=True)


def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
    error_val = error if error else "null"
    print(f"[STEP] step={step} action={action} reward={reward:.2f} done={str(done).lower()} error={error_val}", flush=True)


def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
    rewards_str = ",".join(f"{r:.2f}" for r in rewards)
    print(f"[END] success={str(success).lower()} steps={steps} score={score:.3f} rewards={rewards_str}", flush=True)


TASK_CONFIGS = {
    "endpoint_discovery": {"max_steps": 10, "success_threshold": 0.6},
    "vulnerability_probe": {"max_steps": 15, "success_threshold": 0.5},
    "exploit_chain": {"max_steps": 30, "success_threshold": 0.75},
}


SYSTEM_PROMPTS = {
    "endpoint_discovery": textwrap.dedent(
        """
        You are a security agent discovering API endpoints. Try each endpoint ONCE.
        VALID endpoint values (use EXACTLY as written):
          "POST /users", "GET /users/{id}", "POST /orders", "GET /orders", "GET /health"
        VALID payload_strategy values: "valid", "redos", "oversized", "malformed"
        Reply with ONLY a JSON object like:
        {"endpoint": "POST /users", "payload_strategy": "valid", "target_user_id": null}
        Do NOT repeat endpoints you have already tried.
        """
    ).strip(),
    "vulnerability_probe": textwrap.dedent(
        """
        You are a security agent finding API vulnerabilities.
        VALID endpoint values (use EXACTLY):
          "POST /users", "GET /users/{id}", "POST /orders", "GET /orders", "GET /health"
        VALID payload_strategy values: "valid", "redos", "oversized", "malformed"
        Two vulnerabilities:
        1. ReDoS: call "POST /users" with payload_strategy "redos"
        2. DB slow path: first call "POST /orders" 21 times with "valid", then "GET /orders"
        Once you have confirmed ReDoS (latency > 500ms on POST /users redos), switch to building orders: do POST /orders 21 times, then GET /orders.
        Reply with ONLY JSON: {"endpoint": "POST /users", "payload_strategy": "redos", "target_user_id": null}
        """
    ).strip(),
    "exploit_chain": textwrap.dedent(
        """
        You are a security agent executing a 4-step exploit chain.
        VALID endpoint values (use EXACTLY):
          "POST /users", "GET /users/{id}", "POST /orders", "GET /orders", "GET /health"
        VALID payload_strategy values: "valid", "redos", "oversized", "malformed"
        Follow this EXACT sequence:
          Step 1: {"endpoint": "POST /users", "payload_strategy": "valid", "target_user_id": null}
          Steps 2-22: {"endpoint": "POST /orders", "payload_strategy": "valid", "target_user_id": <id from step 1>}
          Step 23: {"endpoint": "GET /orders", "payload_strategy": "valid", "target_user_id": <same id>}
          Step 24+: {"endpoint": "POST /users", "payload_strategy": "redos", "target_user_id": null}
        The observation tells you the current order_count and user_created status.
        Reply with ONLY the JSON for your NEXT action.
        """
    ).strip(),
}

ENDPOINT_ALIASES = {
    "GET /users/{user_id}": "GET /users/{id}",
    "GET /users/:id": "GET /users/{id}",
    "GET /user": "GET /users/{id}",
}

STRATEGY_ALIASES = {
    "none": "valid",
    "attack": "redos",
    "normal": "valid",
    "invalid": "malformed",
}


def build_user_prompt(step: int, last_obs: dict, history: list[str], task_name: str) -> str:
    history_block = "\n".join(history[-4:]) if history else "None"

    order_count = last_obs.get("session_order_count", 0)
    endpoints_found = last_obs.get("endpoints_discovered", [])
    vulns_found = last_obs.get("vulnerabilities_found", [])
    task_progress = last_obs.get("task_progress", 0.0)

    guidance = ""
    if task_name == "endpoint_discovery":
        remaining = [
            e
            for e in [
                "POST /users",
                "GET /users/{id}",
                "POST /orders",
                "GET /orders",
                "GET /health",
            ]
            if e not in endpoints_found
        ]
        guidance = f"Endpoints not yet tried: {remaining}"
    elif task_name == "vulnerability_probe":
        guidance = f"Vulns found so far: {vulns_found}. Order count: {order_count}."
    elif task_name == "exploit_chain":
        guidance = (
            f"order_count={order_count}/21, "
            f"user_created={'POST /users' in endpoints_found}, "
            f"vulns={vulns_found}. "
            f"If order_count < 21, keep doing POST /orders. "
            f"If order_count >= 21 and 'db_degradation' not in vulns, do GET /orders. "
            f"If 'redos' not in vulns, do POST /users with redos."
        )

    return textwrap.dedent(
        f"""
        Step: {step}
        Task progress: {task_progress:.1%}
        {guidance}
        Last response: status={last_obs.get('http_status')} latency={last_obs.get('latency_ms', 0):.0f}ms
        History:
        {history_block}
        What is your next action? Reply with JSON only.
        """
    ).strip()


def _normalize_action_data(data: dict, task_name: str, created_user_id: int | None) -> dict:
    endpoint = str(data.get("endpoint", "")).strip()
    if re.fullmatch(r"GET\s+/users/\d+", endpoint):
        endpoint = "GET /users/{id}"
    endpoint = ENDPOINT_ALIASES.get(endpoint, endpoint)
    if endpoint:
        data["endpoint"] = endpoint

    strategy = str(data.get("payload_strategy", "")).strip().lower()
    if strategy:
        data["payload_strategy"] = STRATEGY_ALIASES.get(strategy, strategy)

    if task_name == "exploit_chain" and created_user_id:
        ep = str(data.get("endpoint", ""))
        if "orders" in ep.lower():
            data["target_user_id"] = created_user_id

    return data


def get_agent_action(
    client: OpenAI,
    task_name: str,
    step: int,
    last_obs: dict,
    history: List[str],
    created_user_id: int | None = None,
) -> StateStrikeAction:
    system = SYSTEM_PROMPTS[task_name]
    user_msg = build_user_prompt(step=step, last_obs=last_obs, history=history, task_name=task_name)

    fallback = StateStrikeAction(
        endpoint=EndpointChoice.HEALTH,
        payload_strategy=PayloadStrategy.VALID,
    )

    try:
        completion = client.chat.completions.create(
            model=MODEL_NAME,
            messages=[
                {"role": "system", "content": system},
                {"role": "user", "content": user_msg},
            ],
            temperature=0.7,
            max_tokens=100,
        )
        text = (completion.choices[0].message.content or "").strip()
        text = text.removeprefix("```json").removeprefix("```").removesuffix("```").strip()
        data = json.loads(text)
        data = _normalize_action_data(data, task_name=task_name, created_user_id=created_user_id)
        return StateStrikeAction(**data)
    except Exception as exc:
        print(f"[DEBUG] Action parse failed: {exc}", flush=True)
        return fallback


async def run_task(
    env: StateStrikeEnv,
    client: OpenAI,
    task_name: str,
) -> float:
    config = TASK_CONFIGS[task_name]
    max_steps = config["max_steps"]
    success_threshold = config["success_threshold"]

    rewards: List[float] = []
    steps_taken = 0
    score = 0.0
    success = False
    history: List[str] = []
    created_user_id: int | None = None

    log_start(task=task_name, env=BENCHMARK, model=MODEL_NAME)

    try:
        result = await env.reset(task_name=task_name)
        obs = result.observation
        last_obs_dict = obs.model_dump()

        for step in range(1, max_steps + 1):
            if result.done:
                break

            action = get_agent_action(
                client,
                task_name,
                step,
                last_obs_dict,
                history,
                created_user_id=created_user_id,
            )
            action_str = f"{action.endpoint}+{action.payload_strategy}"

            result = await env.step(action)
            obs = result.observation
            reward = result.reward or 0.0
            done = result.done
            error = result.info.get("error") if isinstance(result.info, dict) else None

            rewards.append(reward)
            steps_taken = step
            last_obs_dict = obs.model_dump()

            if task_name == "exploit_chain":
                body = obs.response_body or {}
                maybe_id = body.get("id") if isinstance(body, dict) else None
                if isinstance(maybe_id, int) and created_user_id is None:
                    created_user_id = maybe_id

            log_step(step=step, action=action_str, reward=reward, done=done, error=error)
            history.append(
                f"Step {step}: {action_str} -> status={obs.http_status} "
                f"latency={obs.latency_ms:.0f}ms reward={reward:.2f}"
            )

            if done:
                break

        score = min(max(obs.task_progress, 0.0), 1.0)
        success = score >= success_threshold

    except Exception as exc:
        print(f"[DEBUG] Task {task_name} failed: {exc}", flush=True)
    finally:
        log_end(success=success, steps=steps_taken, score=score, rewards=rewards)

    return score


async def main() -> None:
    client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)

    if LOCAL_IMAGE_NAME:
        env = await StateStrikeEnv.from_docker_image(LOCAL_IMAGE_NAME)
    else:
        env = StateStrikeEnv()

    scores = {}
    for task_name in ["endpoint_discovery", "vulnerability_probe", "exploit_chain"]:
        score = await run_task(env, client, task_name)
        scores[task_name] = score

    await env.close()

    print(f"\n[DEBUG] Final scores: {scores}", flush=True)
    avg = sum(scores.values()) / len(scores)
    print(f"[DEBUG] Average score: {avg:.3f}", flush=True)


if __name__ == "__main__":
    asyncio.run(main())