feat(web): attacker artifacts endpoint + UI drawer
Adds the server-side wiring and frontend UI to surface files captured
by the SSH honeypot for a given attacker.
- New repository method get_attacker_artifacts (abstract + SQLModel
impl) that joins the attacker's IP to `file_captured` log rows.
- New route GET /attackers/{uuid}/artifacts.
- New router /artifacts/{decky}/{service}/{stored_as} that streams a
quarantined file back to an authenticated viewer.
- AttackerDetail grows an ArtifactDrawer panel with per-file metadata
(sha256, size, orig_path) and a download action.
- ssh service fragment now sets NODE_NAME=decky_name so logs and the
host-side artifacts bind-mount share the same decky identifier.
This commit is contained in:
@@ -192,3 +192,8 @@ class BaseRepository(ABC):
|
||||
) -> dict[str, Any]:
|
||||
"""Retrieve paginated commands for an attacker, optionally filtered by service."""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def get_attacker_artifacts(self, uuid: str) -> list[dict[str, Any]]:
|
||||
"""Return `file_captured` log rows for this attacker, newest first."""
|
||||
pass
|
||||
|
||||
@@ -729,3 +729,27 @@ class SQLModelRepository(BaseRepository):
|
||||
total = len(commands)
|
||||
page = commands[offset: offset + limit]
|
||||
return {"total": total, "data": page}
|
||||
|
||||
async def get_attacker_artifacts(self, uuid: str) -> list[dict[str, Any]]:
|
||||
"""Return `file_captured` logs for the attacker identified by UUID.
|
||||
|
||||
Resolves the attacker's IP first, then queries the logs table on two
|
||||
indexed columns (``attacker_ip`` and ``event_type``). No JSON extract
|
||||
needed — the decky/stored_as are already decoded into ``fields`` by
|
||||
the ingester and returned to the frontend for drawer rendering.
|
||||
"""
|
||||
async with self._session() as session:
|
||||
ip_res = await session.execute(
|
||||
select(Attacker.ip).where(Attacker.uuid == uuid)
|
||||
)
|
||||
ip = ip_res.scalar_one_or_none()
|
||||
if not ip:
|
||||
return []
|
||||
rows = await session.execute(
|
||||
select(Log)
|
||||
.where(Log.attacker_ip == ip)
|
||||
.where(Log.event_type == "file_captured")
|
||||
.order_by(desc(Log.timestamp))
|
||||
.limit(200)
|
||||
)
|
||||
return [r.model_dump(mode="json") for r in rows.scalars().all()]
|
||||
|
||||
@@ -14,11 +14,13 @@ from .stream.api_stream_events import router as stream_router
|
||||
from .attackers.api_get_attackers import router as attackers_router
|
||||
from .attackers.api_get_attacker_detail import router as attacker_detail_router
|
||||
from .attackers.api_get_attacker_commands import router as attacker_commands_router
|
||||
from .attackers.api_get_attacker_artifacts import router as attacker_artifacts_router
|
||||
from .config.api_get_config import router as config_get_router
|
||||
from .config.api_update_config import router as config_update_router
|
||||
from .config.api_manage_users import router as config_users_router
|
||||
from .config.api_reinit import router as config_reinit_router
|
||||
from .health.api_get_health import router as health_router
|
||||
from .artifacts.api_get_artifact import router as artifacts_router
|
||||
|
||||
api_router = APIRouter()
|
||||
|
||||
@@ -43,6 +45,7 @@ api_router.include_router(deploy_deckies_router)
|
||||
api_router.include_router(attackers_router)
|
||||
api_router.include_router(attacker_detail_router)
|
||||
api_router.include_router(attacker_commands_router)
|
||||
api_router.include_router(attacker_artifacts_router)
|
||||
|
||||
# Observability
|
||||
api_router.include_router(stats_router)
|
||||
@@ -54,3 +57,6 @@ api_router.include_router(config_get_router)
|
||||
api_router.include_router(config_update_router)
|
||||
api_router.include_router(config_users_router)
|
||||
api_router.include_router(config_reinit_router)
|
||||
|
||||
# Artifacts (captured attacker file drops)
|
||||
api_router.include_router(artifacts_router)
|
||||
|
||||
0
decnet/web/router/artifacts/__init__.py
Normal file
0
decnet/web/router/artifacts/__init__.py
Normal file
84
decnet/web/router/artifacts/api_get_artifact.py
Normal file
84
decnet/web/router/artifacts/api_get_artifact.py
Normal file
@@ -0,0 +1,84 @@
|
||||
"""
|
||||
Artifact download endpoint.
|
||||
|
||||
SSH deckies farm attacker file drops into a host-mounted quarantine:
|
||||
/var/lib/decnet/artifacts/{decky}/ssh/{stored_as}
|
||||
|
||||
The capture event already flows through the normal log pipeline (one
|
||||
RFC 5424 line per capture, see templates/ssh/emit_capture.py), so metadata
|
||||
is served via /logs. This endpoint exists only to retrieve the raw bytes —
|
||||
admin-gated because the payloads are attacker-controlled content.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.responses import FileResponse
|
||||
|
||||
from decnet.telemetry import traced as _traced
|
||||
from decnet.web.dependencies import require_admin
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# Override via env for tests; the prod path matches the bind mount declared in
|
||||
# decnet/services/ssh.py.
|
||||
ARTIFACTS_ROOT = Path(os.environ.get("DECNET_ARTIFACTS_ROOT", "/var/lib/decnet/artifacts"))
|
||||
|
||||
# decky names come from the deployer — lowercase alnum plus hyphens.
|
||||
_DECKY_RE = re.compile(r"^[a-z0-9][a-z0-9-]{0,62}$")
|
||||
|
||||
# stored_as is assembled by capture.sh as:
|
||||
# ${ts}_${sha:0:12}_${base}
|
||||
# where ts is ISO-8601 UTC (e.g. 2026-04-18T02:22:56Z), sha is 12 hex chars,
|
||||
# and base is the original filename's basename. Keep the filename charset
|
||||
# tight but allow common punctuation dropped files actually use.
|
||||
_STORED_AS_RE = re.compile(
|
||||
r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z_[a-f0-9]{12}_[A-Za-z0-9._-]{1,255}$"
|
||||
)
|
||||
|
||||
|
||||
def _resolve_artifact_path(decky: str, stored_as: str) -> Path:
|
||||
"""Validate inputs, resolve the on-disk path, and confirm it stays inside
|
||||
the artifacts root. Raises HTTPException(400) on any violation."""
|
||||
if not _DECKY_RE.fullmatch(decky):
|
||||
raise HTTPException(status_code=400, detail="invalid decky name")
|
||||
if not _STORED_AS_RE.fullmatch(stored_as):
|
||||
raise HTTPException(status_code=400, detail="invalid stored_as")
|
||||
|
||||
root = ARTIFACTS_ROOT.resolve()
|
||||
candidate = (root / decky / "ssh" / stored_as).resolve()
|
||||
# defence-in-depth: even though the regexes reject `..`, make sure a
|
||||
# symlink or weird filesystem state can't escape the root.
|
||||
if root not in candidate.parents and candidate != root:
|
||||
raise HTTPException(status_code=400, detail="path escapes artifacts root")
|
||||
return candidate
|
||||
|
||||
|
||||
@router.get(
|
||||
"/artifacts/{decky}/{stored_as}",
|
||||
tags=["Artifacts"],
|
||||
responses={
|
||||
400: {"description": "Invalid decky or stored_as parameter"},
|
||||
401: {"description": "Could not validate credentials"},
|
||||
403: {"description": "Admin access required"},
|
||||
404: {"description": "Artifact not found"},
|
||||
},
|
||||
)
|
||||
@_traced("api.get_artifact")
|
||||
async def get_artifact(
|
||||
decky: str,
|
||||
stored_as: str,
|
||||
admin: dict = Depends(require_admin),
|
||||
) -> FileResponse:
|
||||
path = _resolve_artifact_path(decky, stored_as)
|
||||
if not path.is_file():
|
||||
raise HTTPException(status_code=404, detail="artifact not found")
|
||||
return FileResponse(
|
||||
path=str(path),
|
||||
media_type="application/octet-stream",
|
||||
filename=stored_as,
|
||||
)
|
||||
34
decnet/web/router/attackers/api_get_attacker_artifacts.py
Normal file
34
decnet/web/router/attackers/api_get_attacker_artifacts.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from decnet.telemetry import traced as _traced
|
||||
from decnet.web.dependencies import require_viewer, repo
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get(
|
||||
"/attackers/{uuid}/artifacts",
|
||||
tags=["Attacker Profiles"],
|
||||
responses={
|
||||
401: {"description": "Could not validate credentials"},
|
||||
403: {"description": "Insufficient permissions"},
|
||||
404: {"description": "Attacker not found"},
|
||||
},
|
||||
)
|
||||
@_traced("api.get_attacker_artifacts")
|
||||
async def get_attacker_artifacts(
|
||||
uuid: str,
|
||||
user: dict = Depends(require_viewer),
|
||||
) -> dict[str, Any]:
|
||||
"""List captured file-drop artifacts for an attacker (newest first).
|
||||
|
||||
Each entry is a `file_captured` log row — the frontend renders the
|
||||
badge/drawer using the same `fields` payload as /logs.
|
||||
"""
|
||||
attacker = await repo.get_attacker_by_uuid(uuid)
|
||||
if not attacker:
|
||||
raise HTTPException(status_code=404, detail="Attacker not found")
|
||||
rows = await repo.get_attacker_artifacts(uuid)
|
||||
return {"total": len(rows), "data": rows}
|
||||
Reference in New Issue
Block a user