2026-06-10 19:20:52 +02:00
|
|
|
"""Deterministic in-memory C2 adapter — used when MIMIC_C2_ADAPTER=fake.
|
|
|
|
|
|
|
|
|
|
Intended for integration tests and local development without a live Mythic instance.
|
2026-06-10 19:34:18 +02:00
|
|
|
Task state is per-instance so parallel tests don't interfere with each other.
|
2026-06-10 19:20:52 +02:00
|
|
|
"""
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from backend.app.services.c2.adapter import (
|
|
|
|
|
C2Adapter,
|
|
|
|
|
C2Callback,
|
2026-06-10 19:56:06 +02:00
|
|
|
C2Error,
|
2026-06-10 19:20:52 +02:00
|
|
|
C2Health,
|
feat(backend): c2 callback history + task import (sprint 8 M4)
Command source decision: extended C2TaskStatus with command: str | None
(default None). Added command_name to _GET_TASK_QUERY so get_task() returns
command in a single round-trip — no separate history fetch needed on import.
4-line change, zero cascading test impact.
adapter.py:
- C2TaskStatus: add command: str | None = None field
- C2HistoricalTask: new dataclass (display_id, command, params, status,
completed, timestamp) for history rows
- C2TaskPage.items: typed as list[C2HistoricalTask] (was list[dict])
mythic.py:
- _GET_TASK_QUERY: add command_name field
- _LIST_CALLBACK_TASKS_QUERY: new query (order_by id desc, limit/offset)
- _COUNT_CALLBACK_TASKS_QUERY: new aggregate query for total
- get_task(): surfaces command_name as status.command
- list_callback_tasks(): two _post() calls (tasks + count), allow_redirects=False
fake.py:
- _FAKE_HISTORY: frozen deterministic history (cb1=12, cb2=0, cb3=5 tasks)
- list_callback_tasks(): serves from _FAKE_HISTORY, pagination applied
- get_task(): returns command from _tasks dict
api/c2.py:
- GET /api/engagements/<eid>/c2/callbacks/<cid>/history: page+page_size
defaults 1/25, cap 100, reject <1, 502 on adapter error
- POST /api/simulations/<sid>/c2/import: idempotent per (sim,mythic_id) pair,
source=import, completed tasks get output+mapping_applied, incomplete tasks
stored for poll-on-read pickup, auto-transition pending→in_progress
60 new tests (456 total); pytest/ruff/mypy all green
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-10 20:09:29 +02:00
|
|
|
C2HistoricalTask,
|
2026-06-10 19:20:52 +02:00
|
|
|
C2TaskPage,
|
|
|
|
|
C2TaskStatus,
|
|
|
|
|
)
|
|
|
|
|
|
feat(backend): c2 callback history + task import (sprint 8 M4)
Command source decision: extended C2TaskStatus with command: str | None
(default None). Added command_name to _GET_TASK_QUERY so get_task() returns
command in a single round-trip — no separate history fetch needed on import.
4-line change, zero cascading test impact.
adapter.py:
- C2TaskStatus: add command: str | None = None field
- C2HistoricalTask: new dataclass (display_id, command, params, status,
completed, timestamp) for history rows
- C2TaskPage.items: typed as list[C2HistoricalTask] (was list[dict])
mythic.py:
- _GET_TASK_QUERY: add command_name field
- _LIST_CALLBACK_TASKS_QUERY: new query (order_by id desc, limit/offset)
- _COUNT_CALLBACK_TASKS_QUERY: new aggregate query for total
- get_task(): surfaces command_name as status.command
- list_callback_tasks(): two _post() calls (tasks + count), allow_redirects=False
fake.py:
- _FAKE_HISTORY: frozen deterministic history (cb1=12, cb2=0, cb3=5 tasks)
- list_callback_tasks(): serves from _FAKE_HISTORY, pagination applied
- get_task(): returns command from _tasks dict
api/c2.py:
- GET /api/engagements/<eid>/c2/callbacks/<cid>/history: page+page_size
defaults 1/25, cap 100, reject <1, 502 on adapter error
- POST /api/simulations/<sid>/c2/import: idempotent per (sim,mythic_id) pair,
source=import, completed tasks get output+mapping_applied, incomplete tasks
stored for poll-on-read pickup, auto-transition pending→in_progress
60 new tests (456 total); pytest/ruff/mypy all green
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-10 20:09:29 +02:00
|
|
|
# Frozen base timestamp — all fake history tasks share this prefix for determinism.
|
|
|
|
|
_BASE_TS = "2026-06-10T00:00:00Z"
|
|
|
|
|
|
|
|
|
|
# Deterministic history for list_callback_tasks:
|
|
|
|
|
# callback 1 → 12 tasks, callback 2 → 0 tasks, callback 3 → 5 tasks.
|
|
|
|
|
# Commands cycle through a fixed set; even-indexed tasks are completed.
|
|
|
|
|
_HISTORY_COMMANDS = ["whoami", "hostname", "id", "ipconfig", "net user", "pwd"]
|
|
|
|
|
|
|
|
|
|
_FAKE_HISTORY: dict[int, list[C2HistoricalTask]] = {
|
|
|
|
|
1: [
|
|
|
|
|
C2HistoricalTask(
|
|
|
|
|
display_id=100 + i,
|
|
|
|
|
command=_HISTORY_COMMANDS[i % len(_HISTORY_COMMANDS)],
|
|
|
|
|
params=None,
|
|
|
|
|
status="completed" if i % 2 == 0 else "submitted",
|
|
|
|
|
completed=i % 2 == 0,
|
|
|
|
|
timestamp=_BASE_TS if i % 2 == 0 else None,
|
|
|
|
|
)
|
|
|
|
|
for i in range(12)
|
|
|
|
|
],
|
|
|
|
|
2: [],
|
|
|
|
|
3: [
|
|
|
|
|
C2HistoricalTask(
|
|
|
|
|
display_id=200 + i,
|
|
|
|
|
command=_HISTORY_COMMANDS[i % len(_HISTORY_COMMANDS)],
|
|
|
|
|
params=None,
|
|
|
|
|
status="completed" if i % 2 == 0 else "submitted",
|
|
|
|
|
completed=i % 2 == 0,
|
|
|
|
|
timestamp=_BASE_TS if i % 2 == 0 else None,
|
|
|
|
|
)
|
|
|
|
|
for i in range(5)
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-10 19:34:18 +02:00
|
|
|
# Three fixed callbacks the test suite can pin against.
|
2026-06-10 19:20:52 +02:00
|
|
|
_FAKE_CALLBACKS = [
|
|
|
|
|
C2Callback(
|
|
|
|
|
display_id=1,
|
|
|
|
|
active=True,
|
|
|
|
|
host="WORKSTATION-01",
|
|
|
|
|
user="jdoe",
|
|
|
|
|
domain="LAB",
|
|
|
|
|
last_checkin="2026-06-10T00:00:00Z",
|
|
|
|
|
),
|
2026-06-10 19:34:18 +02:00
|
|
|
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",
|
|
|
|
|
),
|
2026-06-10 19:20:52 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FakeAdapter(C2Adapter):
|
2026-06-10 19:34:18 +02:00
|
|
|
"""In-memory adapter with deterministic behaviour.
|
|
|
|
|
|
|
|
|
|
Each instance starts with an empty task store and display_ids from 1000.
|
2026-06-10 19:56:06 +02:00
|
|
|
|
|
|
|
|
get_task() state progression per task (keyed by display_id):
|
|
|
|
|
- First call after create_task → submitted, completed=False
|
|
|
|
|
- Second and subsequent calls → completed=True, status="completed"
|
2026-06-10 19:34:18 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
|
self._tasks: dict[int, dict] = {}
|
|
|
|
|
self._next_task_id = 1000
|
2026-06-10 19:56:06 +02:00
|
|
|
# Tracks how many times get_task has been called per display_id.
|
|
|
|
|
self._get_task_calls: dict[int, int] = {}
|
2026-06-10 19:20:52 +02:00
|
|
|
|
|
|
|
|
def test_connection(self) -> C2Health:
|
|
|
|
|
return C2Health(ok=True)
|
|
|
|
|
|
|
|
|
|
def list_callbacks(self) -> list[C2Callback]:
|
|
|
|
|
return list(_FAKE_CALLBACKS)
|
|
|
|
|
|
|
|
|
|
def create_task(
|
|
|
|
|
self,
|
|
|
|
|
callback_display_id: int,
|
|
|
|
|
command: str,
|
|
|
|
|
params: str | None = None,
|
|
|
|
|
) -> int:
|
2026-06-10 19:34:18 +02:00
|
|
|
tid = self._next_task_id
|
|
|
|
|
self._next_task_id += 1
|
|
|
|
|
self._tasks[tid] = {
|
2026-06-10 19:20:52 +02:00
|
|
|
"display_id": tid,
|
|
|
|
|
"callback_display_id": callback_display_id,
|
|
|
|
|
"command": command,
|
|
|
|
|
"params": params,
|
|
|
|
|
"status": "submitted",
|
|
|
|
|
"completed": False,
|
|
|
|
|
"output": None,
|
|
|
|
|
}
|
|
|
|
|
return tid
|
|
|
|
|
|
|
|
|
|
def get_task(self, task_display_id: int) -> C2TaskStatus:
|
2026-06-10 19:56:06 +02:00
|
|
|
"""Deterministic state progression: first call → submitted, second+ → completed.
|
|
|
|
|
|
|
|
|
|
Tracks call count regardless of whether the task was created by this instance,
|
|
|
|
|
so the endpoint poll-on-read flow works across separate adapter instantiations.
|
|
|
|
|
"""
|
|
|
|
|
call_count = self._get_task_calls.get(task_display_id, 0) + 1
|
|
|
|
|
self._get_task_calls[task_display_id] = call_count
|
|
|
|
|
|
2026-06-10 19:34:18 +02:00
|
|
|
task = self._tasks.get(task_display_id)
|
2026-06-10 19:56:06 +02:00
|
|
|
|
|
|
|
|
if call_count >= 2:
|
|
|
|
|
completed = True
|
|
|
|
|
status = "completed"
|
|
|
|
|
if task is not None:
|
|
|
|
|
task["status"] = "completed"
|
|
|
|
|
task["completed"] = True
|
|
|
|
|
else:
|
|
|
|
|
completed = False
|
|
|
|
|
status = task["status"] if task is not None else "submitted"
|
|
|
|
|
|
2026-06-10 19:20:52 +02:00
|
|
|
return C2TaskStatus(
|
|
|
|
|
display_id=task_display_id,
|
2026-06-10 19:56:06 +02:00
|
|
|
status=status,
|
|
|
|
|
completed=completed,
|
feat(backend): c2 callback history + task import (sprint 8 M4)
Command source decision: extended C2TaskStatus with command: str | None
(default None). Added command_name to _GET_TASK_QUERY so get_task() returns
command in a single round-trip — no separate history fetch needed on import.
4-line change, zero cascading test impact.
adapter.py:
- C2TaskStatus: add command: str | None = None field
- C2HistoricalTask: new dataclass (display_id, command, params, status,
completed, timestamp) for history rows
- C2TaskPage.items: typed as list[C2HistoricalTask] (was list[dict])
mythic.py:
- _GET_TASK_QUERY: add command_name field
- _LIST_CALLBACK_TASKS_QUERY: new query (order_by id desc, limit/offset)
- _COUNT_CALLBACK_TASKS_QUERY: new aggregate query for total
- get_task(): surfaces command_name as status.command
- list_callback_tasks(): two _post() calls (tasks + count), allow_redirects=False
fake.py:
- _FAKE_HISTORY: frozen deterministic history (cb1=12, cb2=0, cb3=5 tasks)
- list_callback_tasks(): serves from _FAKE_HISTORY, pagination applied
- get_task(): returns command from _tasks dict
api/c2.py:
- GET /api/engagements/<eid>/c2/callbacks/<cid>/history: page+page_size
defaults 1/25, cap 100, reject <1, 502 on adapter error
- POST /api/simulations/<sid>/c2/import: idempotent per (sim,mythic_id) pair,
source=import, completed tasks get output+mapping_applied, incomplete tasks
stored for poll-on-read pickup, auto-transition pending→in_progress
60 new tests (456 total); pytest/ruff/mypy all green
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-10 20:09:29 +02:00
|
|
|
command=task["command"] if task is not None else None,
|
2026-06-10 19:20:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def get_task_output(self, task_display_id: int) -> str:
|
2026-06-10 19:56:06 +02:00
|
|
|
"""Returns deterministic output once task is completed; raises C2Error before that."""
|
|
|
|
|
# Check call count — completed if get_task was called at least twice.
|
|
|
|
|
if self._get_task_calls.get(task_display_id, 0) < 2:
|
|
|
|
|
# Also allow tasks in _tasks that were explicitly set to completed.
|
|
|
|
|
task = self._tasks.get(task_display_id)
|
|
|
|
|
if task is None or not task.get("completed", False):
|
|
|
|
|
raise C2Error("task not completed")
|
|
|
|
|
|
2026-06-10 19:34:18 +02:00
|
|
|
task = self._tasks.get(task_display_id)
|
2026-06-10 19:56:06 +02:00
|
|
|
command = task["command"] if task is not None else "unknown"
|
|
|
|
|
return f"output for task {task_display_id}: {command}\n"
|
2026-06-10 19:20:52 +02:00
|
|
|
|
|
|
|
|
def list_callback_tasks(
|
|
|
|
|
self,
|
|
|
|
|
callback_display_id: int,
|
|
|
|
|
page: int = 1,
|
|
|
|
|
page_size: int = 25,
|
|
|
|
|
) -> C2TaskPage:
|
feat(backend): c2 callback history + task import (sprint 8 M4)
Command source decision: extended C2TaskStatus with command: str | None
(default None). Added command_name to _GET_TASK_QUERY so get_task() returns
command in a single round-trip — no separate history fetch needed on import.
4-line change, zero cascading test impact.
adapter.py:
- C2TaskStatus: add command: str | None = None field
- C2HistoricalTask: new dataclass (display_id, command, params, status,
completed, timestamp) for history rows
- C2TaskPage.items: typed as list[C2HistoricalTask] (was list[dict])
mythic.py:
- _GET_TASK_QUERY: add command_name field
- _LIST_CALLBACK_TASKS_QUERY: new query (order_by id desc, limit/offset)
- _COUNT_CALLBACK_TASKS_QUERY: new aggregate query for total
- get_task(): surfaces command_name as status.command
- list_callback_tasks(): two _post() calls (tasks + count), allow_redirects=False
fake.py:
- _FAKE_HISTORY: frozen deterministic history (cb1=12, cb2=0, cb3=5 tasks)
- list_callback_tasks(): serves from _FAKE_HISTORY, pagination applied
- get_task(): returns command from _tasks dict
api/c2.py:
- GET /api/engagements/<eid>/c2/callbacks/<cid>/history: page+page_size
defaults 1/25, cap 100, reject <1, 502 on adapter error
- POST /api/simulations/<sid>/c2/import: idempotent per (sim,mythic_id) pair,
source=import, completed tasks get output+mapping_applied, incomplete tasks
stored for poll-on-read pickup, auto-transition pending→in_progress
60 new tests (456 total); pytest/ruff/mypy all green
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-10 20:09:29 +02:00
|
|
|
all_items = _FAKE_HISTORY.get(callback_display_id, [])
|
2026-06-10 19:20:52 +02:00
|
|
|
start = (page - 1) * page_size
|
|
|
|
|
return C2TaskPage(
|
feat(backend): c2 callback history + task import (sprint 8 M4)
Command source decision: extended C2TaskStatus with command: str | None
(default None). Added command_name to _GET_TASK_QUERY so get_task() returns
command in a single round-trip — no separate history fetch needed on import.
4-line change, zero cascading test impact.
adapter.py:
- C2TaskStatus: add command: str | None = None field
- C2HistoricalTask: new dataclass (display_id, command, params, status,
completed, timestamp) for history rows
- C2TaskPage.items: typed as list[C2HistoricalTask] (was list[dict])
mythic.py:
- _GET_TASK_QUERY: add command_name field
- _LIST_CALLBACK_TASKS_QUERY: new query (order_by id desc, limit/offset)
- _COUNT_CALLBACK_TASKS_QUERY: new aggregate query for total
- get_task(): surfaces command_name as status.command
- list_callback_tasks(): two _post() calls (tasks + count), allow_redirects=False
fake.py:
- _FAKE_HISTORY: frozen deterministic history (cb1=12, cb2=0, cb3=5 tasks)
- list_callback_tasks(): serves from _FAKE_HISTORY, pagination applied
- get_task(): returns command from _tasks dict
api/c2.py:
- GET /api/engagements/<eid>/c2/callbacks/<cid>/history: page+page_size
defaults 1/25, cap 100, reject <1, 502 on adapter error
- POST /api/simulations/<sid>/c2/import: idempotent per (sim,mythic_id) pair,
source=import, completed tasks get output+mapping_applied, incomplete tasks
stored for poll-on-read pickup, auto-transition pending→in_progress
60 new tests (456 total); pytest/ruff/mypy all green
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-10 20:09:29 +02:00
|
|
|
items=all_items[start : start + page_size],
|
|
|
|
|
total=len(all_items),
|
2026-06-10 19:20:52 +02:00
|
|
|
page=page,
|
|
|
|
|
page_size=page_size,
|
|
|
|
|
)
|