feat(backend): c2 callbacks + execute endpoints (sprint 8 M2)

- Add C2Error exception to adapter ABC
- Add promote_to_in_progress() helper to simulation_workflow (pending→in_progress)
- Flesh out MythicAdapter: list_callbacks() (GraphQL query) + create_task() (mutation)
- Expand FakeAdapter to 3 deterministic callbacks; switch task store to per-instance
- Add GET /api/engagements/<id>/c2/callbacks — lists active callbacks via adapter
- Add POST /api/simulations/<id>/c2/execute — issues tasks, stores C2Task rows,
  auto-transitions pending→in_progress, blocks on done (409)
- Both endpoints: SOC=403, 503 no-key, 502 adapter error, sanitized error messages
- Add requests-mock==1.12.1 to requirements.txt
- 42 new tests (342 total, 300 M1 baseline preserved green)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Knacky
2026-06-10 19:34:18 +02:00
parent 9a9c98beab
commit 53755a31d6
14 changed files with 983 additions and 23 deletions

View File

@@ -1,6 +1,7 @@
"""Deterministic in-memory C2 adapter — used when MIMIC_C2_ADAPTER=fake.
Intended for integration tests and local development without a live Mythic instance.
Task state is per-instance so parallel tests don't interfere with each other.
"""
from __future__ import annotations
@@ -12,6 +13,7 @@ from backend.app.services.c2.adapter import (
C2TaskStatus,
)
# Three fixed callbacks the test suite can pin against.
_FAKE_CALLBACKS = [
C2Callback(
display_id=1,
@@ -21,14 +23,34 @@ _FAKE_CALLBACKS = [
domain="LAB",
last_checkin="2026-06-10T00:00:00Z",
),
C2Callback(
display_id=2,
active=True,
host="SERVER-DC01",
user="svc_backup",
domain="LAB",
last_checkin="2026-06-10T00:01:00Z",
),
C2Callback(
display_id=3,
active=True,
host="LAPTOP-RT",
user="admin",
domain="LAB",
last_checkin="2026-06-10T00:02:00Z",
),
]
_FAKE_TASKS: dict[int, dict] = {}
_next_task_id = 100
class FakeAdapter(C2Adapter):
"""In-memory adapter with deterministic behaviour."""
"""In-memory adapter with deterministic behaviour.
Each instance starts with an empty task store and display_ids from 1000.
"""
def __init__(self) -> None:
self._tasks: dict[int, dict] = {}
self._next_task_id = 1000
def test_connection(self) -> C2Health:
return C2Health(ok=True)
@@ -42,10 +64,9 @@ class FakeAdapter(C2Adapter):
command: str,
params: str | None = None,
) -> int:
global _next_task_id
tid = _next_task_id
_next_task_id += 1
_FAKE_TASKS[tid] = {
tid = self._next_task_id
self._next_task_id += 1
self._tasks[tid] = {
"display_id": tid,
"callback_display_id": callback_display_id,
"command": command,
@@ -57,7 +78,7 @@ class FakeAdapter(C2Adapter):
return tid
def get_task(self, task_display_id: int) -> C2TaskStatus:
task = _FAKE_TASKS.get(task_display_id)
task = self._tasks.get(task_display_id)
if task is None:
return C2TaskStatus(display_id=task_display_id, status="unknown", completed=False)
return C2TaskStatus(
@@ -67,7 +88,7 @@ class FakeAdapter(C2Adapter):
)
def get_task_output(self, task_display_id: int) -> str:
task = _FAKE_TASKS.get(task_display_id)
task = self._tasks.get(task_display_id)
if task is None:
return ""
return task.get("output") or ""
@@ -79,7 +100,7 @@ class FakeAdapter(C2Adapter):
page_size: int = 25,
) -> C2TaskPage:
items = [
t for t in _FAKE_TASKS.values()
t for t in self._tasks.values()
if t["callback_display_id"] == callback_display_id
]
start = (page - 1) * page_size