feat(phase-27): add advanced ATT&CK Navigator-style heatmap with layers, filters and export (T-221 to T-223)
This commit is contained in:
@@ -1,197 +1,287 @@
|
||||
import { useState, useMemo } from "react";
|
||||
import { useState, useMemo, useCallback } from "react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { Loader2, AlertCircle, Filter, X } from "lucide-react";
|
||||
import { getTechniques, type TechniqueSummary } from "../api/techniques";
|
||||
import AttackMatrix from "../components/AttackMatrix";
|
||||
import type { TechniqueStatus } from "../types/models";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { Loader2, AlertCircle, Download, ZoomIn, ZoomOut } from "lucide-react";
|
||||
import {
|
||||
getHeatmapCoverage,
|
||||
getHeatmapThreatActor,
|
||||
getHeatmapDetectionRules,
|
||||
getHeatmapCampaign,
|
||||
exportNavigatorJSON,
|
||||
type HeatmapLayer,
|
||||
type HeatmapFilters as HeatmapFilterParams,
|
||||
} from "../api/heatmap";
|
||||
import AdvancedHeatmap from "../components/heatmap/AdvancedHeatmap";
|
||||
import HeatmapLayerSelector, {
|
||||
type LayerType,
|
||||
} from "../components/heatmap/HeatmapLayerSelector";
|
||||
import HeatmapFiltersComponent from "../components/heatmap/HeatmapFilters";
|
||||
import HeatmapLegend from "../components/heatmap/HeatmapLegend";
|
||||
|
||||
const STATUS_OPTIONS: { value: TechniqueStatus | "all"; label: string; color: string }[] = [
|
||||
{ value: "all", label: "All Statuses", color: "text-gray-400" },
|
||||
{ value: "validated", label: "Validated", color: "text-green-400" },
|
||||
{ value: "partial", label: "Partial", color: "text-yellow-400" },
|
||||
{ value: "in_progress", label: "In Progress", color: "text-blue-400" },
|
||||
{ value: "not_covered", label: "Not Covered", color: "text-red-400" },
|
||||
{ value: "not_evaluated", label: "Not Evaluated", color: "text-gray-400" },
|
||||
const TACTIC_ORDER = [
|
||||
"reconnaissance",
|
||||
"resource-development",
|
||||
"initial-access",
|
||||
"execution",
|
||||
"persistence",
|
||||
"privilege-escalation",
|
||||
"defense-evasion",
|
||||
"credential-access",
|
||||
"discovery",
|
||||
"lateral-movement",
|
||||
"collection",
|
||||
"command-and-control",
|
||||
"exfiltration",
|
||||
"impact",
|
||||
];
|
||||
|
||||
const PLATFORM_OPTIONS = ["all", "windows", "linux", "macos", "cloud", "network"] as const;
|
||||
type ZoomLevel = "compact" | "normal" | "expanded";
|
||||
|
||||
export default function MatrixPage() {
|
||||
const [statusFilter, setStatusFilter] = useState<TechniqueStatus | "all">("all");
|
||||
const [platformFilter, setPlatformFilter] = useState<string>("all");
|
||||
const [tacticFilter, setTacticFilter] = useState<string>("all");
|
||||
const navigate = useNavigate();
|
||||
|
||||
// Layer selection state
|
||||
const [activeLayer, setActiveLayer] = useState<LayerType>("coverage");
|
||||
const [selectedActorId, setSelectedActorId] = useState<string | null>(null);
|
||||
const [selectedCampaignId, setSelectedCampaignId] = useState<string | null>(null);
|
||||
|
||||
// Filter state
|
||||
const [platforms, setPlatforms] = useState<string[]>([]);
|
||||
const [selectedTactics, setSelectedTactics] = useState<string[]>([]);
|
||||
const [minScore, setMinScore] = useState(0);
|
||||
|
||||
// Zoom
|
||||
const [zoom, setZoom] = useState<ZoomLevel>("normal");
|
||||
|
||||
// Export dropdown
|
||||
const [showExportMenu, setShowExportMenu] = useState(false);
|
||||
|
||||
// Build filter params
|
||||
const filterParams: HeatmapFilterParams = useMemo(
|
||||
() => ({
|
||||
platforms: platforms.length > 0 ? platforms.join(",") : undefined,
|
||||
tactics: selectedTactics.length > 0 ? selectedTactics.join(",") : undefined,
|
||||
min_score: minScore > 0 ? minScore : undefined,
|
||||
}),
|
||||
[platforms, selectedTactics, minScore],
|
||||
);
|
||||
|
||||
// Build query key based on active layer + selection
|
||||
const queryKey = useMemo(() => {
|
||||
const base = ["heatmap", activeLayer, filterParams];
|
||||
if (activeLayer === "threat-actor") return [...base, selectedActorId];
|
||||
if (activeLayer === "campaign") return [...base, selectedCampaignId];
|
||||
return base;
|
||||
}, [activeLayer, filterParams, selectedActorId, selectedCampaignId]);
|
||||
|
||||
// Fetch the active layer data
|
||||
const {
|
||||
data: techniques,
|
||||
data: layerData,
|
||||
isLoading,
|
||||
error,
|
||||
} = useQuery({
|
||||
queryKey: ["techniques"],
|
||||
queryFn: () => getTechniques(),
|
||||
} = useQuery<HeatmapLayer>({
|
||||
queryKey,
|
||||
queryFn: () => {
|
||||
switch (activeLayer) {
|
||||
case "coverage":
|
||||
return getHeatmapCoverage(filterParams);
|
||||
case "threat-actor":
|
||||
if (!selectedActorId) return Promise.resolve({ name: "", versions: { attack: "", navigator: "", layer: "" }, domain: "", description: "", filters: { platforms: [] }, gradient: { colors: [], minValue: 0, maxValue: 0 }, techniques: [] } as HeatmapLayer);
|
||||
return getHeatmapThreatActor(selectedActorId, filterParams);
|
||||
case "detection-rules":
|
||||
return getHeatmapDetectionRules(filterParams);
|
||||
case "campaign":
|
||||
if (!selectedCampaignId) return Promise.resolve({ name: "", versions: { attack: "", navigator: "", layer: "" }, domain: "", description: "", filters: { platforms: [] }, gradient: { colors: [], minValue: 0, maxValue: 0 }, techniques: [] } as HeatmapLayer);
|
||||
return getHeatmapCampaign(selectedCampaignId, filterParams);
|
||||
default:
|
||||
return getHeatmapCoverage(filterParams);
|
||||
}
|
||||
},
|
||||
enabled:
|
||||
activeLayer === "coverage" ||
|
||||
activeLayer === "detection-rules" ||
|
||||
(activeLayer === "threat-actor" && !!selectedActorId) ||
|
||||
(activeLayer === "campaign" && !!selectedCampaignId),
|
||||
});
|
||||
|
||||
// Extract unique tactics from techniques
|
||||
const availableTactics = useMemo(() => {
|
||||
if (!techniques) return [];
|
||||
const tactics = new Set<string>();
|
||||
for (const tech of techniques) {
|
||||
if (tech.tactic) {
|
||||
tech.tactic.split(",").forEach((t) => tactics.add(t.trim().toLowerCase()));
|
||||
const techniques = layerData?.techniques || [];
|
||||
|
||||
// Handle cell click - navigate to technique detail
|
||||
const handleCellClick = useCallback(
|
||||
(techniqueId: string) => {
|
||||
navigate(`/techniques/${techniqueId}`);
|
||||
},
|
||||
[navigate],
|
||||
);
|
||||
|
||||
// Handle export
|
||||
const handleExport = async (type: "download" | "url") => {
|
||||
setShowExportMenu(false);
|
||||
|
||||
const layerId =
|
||||
activeLayer === "threat-actor"
|
||||
? selectedActorId ?? undefined
|
||||
: activeLayer === "campaign"
|
||||
? selectedCampaignId ?? undefined
|
||||
: undefined;
|
||||
|
||||
if (type === "download") {
|
||||
try {
|
||||
const blob = await exportNavigatorJSON(activeLayer, layerId, filterParams);
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = `aegis_${activeLayer}_layer.json`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
} catch {
|
||||
console.error("Failed to export Navigator JSON");
|
||||
}
|
||||
} else {
|
||||
// Copy Navigator URL
|
||||
const navigatorUrl = `https://mitre-attack.github.io/attack-navigator/#layerURL=${encodeURIComponent(
|
||||
window.location.origin + `/api/v1/heatmap/export-navigator?layer=${activeLayer}${layerId ? `&layer_id=${layerId}` : ""}`
|
||||
)}`;
|
||||
navigator.clipboard.writeText(navigatorUrl);
|
||||
}
|
||||
return Array.from(tactics).sort();
|
||||
}, [techniques]);
|
||||
|
||||
// Apply filters
|
||||
const filteredTechniques = useMemo(() => {
|
||||
if (!techniques) return [];
|
||||
|
||||
return techniques.filter((tech: TechniqueSummary) => {
|
||||
// Status filter
|
||||
if (statusFilter !== "all" && tech.status_global !== statusFilter) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Tactic filter
|
||||
if (tacticFilter !== "all") {
|
||||
const techTactics = tech.tactic?.split(",").map((t) => t.trim().toLowerCase()) || [];
|
||||
if (!techTactics.includes(tacticFilter)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Platform filter is handled client-side since we don't have platform in summary
|
||||
// For now we show all - platform filtering would need the full technique data
|
||||
|
||||
return true;
|
||||
});
|
||||
}, [techniques, statusFilter, tacticFilter]);
|
||||
|
||||
const hasActiveFilters = statusFilter !== "all" || tacticFilter !== "all" || platformFilter !== "all";
|
||||
|
||||
const clearFilters = () => {
|
||||
setStatusFilter("all");
|
||||
setPlatformFilter("all");
|
||||
setTacticFilter("all");
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex h-64 items-center justify-center">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-cyan-400" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
// Zoom controls
|
||||
const zoomIn = () => {
|
||||
if (zoom === "compact") setZoom("normal");
|
||||
else if (zoom === "normal") setZoom("expanded");
|
||||
};
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="flex h-64 flex-col items-center justify-center gap-2">
|
||||
<AlertCircle className="h-10 w-10 text-red-400" />
|
||||
<p className="text-red-400">Failed to load techniques</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
const zoomOut = () => {
|
||||
if (zoom === "expanded") setZoom("normal");
|
||||
else if (zoom === "normal") setZoom("compact");
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="space-y-4">
|
||||
{/* Header */}
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold text-white">ATT&CK Matrix</h1>
|
||||
<p className="mt-1 text-sm text-gray-400">
|
||||
Interactive MITRE ATT&CK coverage matrix — click any technique for details
|
||||
Advanced heatmap with multiple layers, filters, and ATT&CK Navigator export
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Filters */}
|
||||
<div className="flex flex-wrap items-center gap-4 rounded-xl border border-gray-800 bg-gray-900 p-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<Filter className="h-4 w-4 text-gray-400" />
|
||||
<span className="text-sm font-medium text-gray-400">Filters:</span>
|
||||
{/* Toolbar: Layer Selector + Filters + Export + Zoom */}
|
||||
<div className="rounded-xl border border-gray-800 bg-gray-900 p-4 space-y-3">
|
||||
<div className="flex flex-wrap items-center justify-between gap-4">
|
||||
{/* Layer selector */}
|
||||
<HeatmapLayerSelector
|
||||
activeLayer={activeLayer}
|
||||
onLayerChange={setActiveLayer}
|
||||
selectedActorId={selectedActorId}
|
||||
onActorChange={setSelectedActorId}
|
||||
selectedCampaignId={selectedCampaignId}
|
||||
onCampaignChange={setSelectedCampaignId}
|
||||
/>
|
||||
|
||||
{/* Right side: Export + Zoom */}
|
||||
<div className="flex items-center gap-2">
|
||||
{/* Export dropdown */}
|
||||
<div className="relative">
|
||||
<button
|
||||
onClick={() => setShowExportMenu(!showExportMenu)}
|
||||
className="flex items-center gap-1.5 rounded-lg border border-gray-700 bg-gray-800 px-3 py-1.5 text-xs font-medium text-gray-300 hover:border-cyan-500/50 hover:text-white"
|
||||
>
|
||||
<Download className="h-3.5 w-3.5" />
|
||||
Export
|
||||
</button>
|
||||
{showExportMenu && (
|
||||
<div className="absolute right-0 top-full z-30 mt-1 w-52 rounded-lg border border-gray-700 bg-gray-900 py-1 shadow-xl">
|
||||
<button
|
||||
onClick={() => handleExport("download")}
|
||||
className="w-full px-3 py-2 text-left text-xs text-gray-300 hover:bg-gray-800"
|
||||
>
|
||||
Export Navigator JSON
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleExport("url")}
|
||||
className="w-full px-3 py-2 text-left text-xs text-gray-300 hover:bg-gray-800"
|
||||
>
|
||||
Copy Navigator URL
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Zoom controls */}
|
||||
<div className="flex items-center rounded-lg border border-gray-700 bg-gray-800">
|
||||
<button
|
||||
onClick={zoomOut}
|
||||
disabled={zoom === "compact"}
|
||||
className="px-2 py-1.5 text-gray-400 hover:text-white disabled:opacity-30"
|
||||
>
|
||||
<ZoomOut className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
<span className="border-x border-gray-700 px-2 py-1 text-[10px] font-medium uppercase text-gray-400">
|
||||
{zoom}
|
||||
</span>
|
||||
<button
|
||||
onClick={zoomIn}
|
||||
disabled={zoom === "expanded"}
|
||||
className="px-2 py-1.5 text-gray-400 hover:text-white disabled:opacity-30"
|
||||
>
|
||||
<ZoomIn className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Status filter */}
|
||||
<select
|
||||
value={statusFilter}
|
||||
onChange={(e) => setStatusFilter(e.target.value as TechniqueStatus | "all")}
|
||||
className="rounded-lg border border-gray-700 bg-gray-800 px-3 py-1.5 text-sm text-gray-200 focus:border-cyan-500 focus:outline-none"
|
||||
>
|
||||
{STATUS_OPTIONS.map((opt) => (
|
||||
<option key={opt.value} value={opt.value}>
|
||||
{opt.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
|
||||
{/* Tactic filter */}
|
||||
<select
|
||||
value={tacticFilter}
|
||||
onChange={(e) => setTacticFilter(e.target.value)}
|
||||
className="rounded-lg border border-gray-700 bg-gray-800 px-3 py-1.5 text-sm text-gray-200 focus:border-cyan-500 focus:outline-none"
|
||||
>
|
||||
<option value="all">All Tactics</option>
|
||||
{availableTactics.map((tactic) => (
|
||||
<option key={tactic} value={tactic}>
|
||||
{tactic
|
||||
.split("-")
|
||||
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
|
||||
.join(" ")}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
|
||||
{/* Platform filter */}
|
||||
<select
|
||||
value={platformFilter}
|
||||
onChange={(e) => setPlatformFilter(e.target.value)}
|
||||
className="rounded-lg border border-gray-700 bg-gray-800 px-3 py-1.5 text-sm text-gray-200 focus:border-cyan-500 focus:outline-none"
|
||||
>
|
||||
{PLATFORM_OPTIONS.map((platform) => (
|
||||
<option key={platform} value={platform}>
|
||||
{platform === "all" ? "All Platforms" : platform.charAt(0).toUpperCase() + platform.slice(1)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
|
||||
{hasActiveFilters && (
|
||||
<button
|
||||
onClick={clearFilters}
|
||||
className="flex items-center gap-1 rounded-lg border border-gray-700 bg-gray-800 px-3 py-1.5 text-sm text-gray-400 hover:border-red-500/50 hover:text-red-400"
|
||||
>
|
||||
<X className="h-3.5 w-3.5" />
|
||||
Clear
|
||||
</button>
|
||||
)}
|
||||
|
||||
<div className="ml-auto text-sm text-gray-500">
|
||||
Showing {filteredTechniques.length} of {techniques?.length || 0} techniques
|
||||
</div>
|
||||
{/* Filters */}
|
||||
<HeatmapFiltersComponent
|
||||
platforms={platforms}
|
||||
onPlatformsChange={setPlatforms}
|
||||
selectedTactics={selectedTactics}
|
||||
onTacticsChange={setSelectedTactics}
|
||||
minScore={minScore}
|
||||
onMinScoreChange={setMinScore}
|
||||
availableTactics={TACTIC_ORDER}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Matrix */}
|
||||
<AttackMatrix techniques={filteredTechniques} />
|
||||
{/* Stats bar */}
|
||||
{layerData && (
|
||||
<div className="flex items-center gap-4 text-xs text-gray-500">
|
||||
<span>
|
||||
Layer: <span className="text-gray-300">{layerData.name}</span>
|
||||
</span>
|
||||
<span>
|
||||
Techniques:{" "}
|
||||
<span className="text-gray-300">
|
||||
{techniques.filter((t) => t.enabled).length} active
|
||||
</span>{" "}
|
||||
/ {techniques.length} total
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Loading / Error / Heatmap */}
|
||||
{isLoading ? (
|
||||
<div className="flex h-64 items-center justify-center">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-cyan-400" />
|
||||
</div>
|
||||
) : error ? (
|
||||
<div className="flex h-64 flex-col items-center justify-center gap-2">
|
||||
<AlertCircle className="h-10 w-10 text-red-400" />
|
||||
<p className="text-red-400">Failed to load heatmap data</p>
|
||||
</div>
|
||||
) : (
|
||||
<AdvancedHeatmap
|
||||
techniques={techniques}
|
||||
onCellClick={handleCellClick}
|
||||
zoom={zoom}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Legend */}
|
||||
<div className="flex flex-wrap items-center gap-4 rounded-xl border border-gray-800 bg-gray-900 p-4">
|
||||
<span className="text-sm font-medium text-gray-400">Legend:</span>
|
||||
{STATUS_OPTIONS.filter((s) => s.value !== "all").map((status) => (
|
||||
<div key={status.value} className="flex items-center gap-2">
|
||||
<div
|
||||
className={`h-3 w-3 rounded ${
|
||||
status.value === "validated"
|
||||
? "bg-green-500"
|
||||
: status.value === "partial"
|
||||
? "bg-yellow-500"
|
||||
: status.value === "in_progress"
|
||||
? "bg-blue-500"
|
||||
: status.value === "not_covered"
|
||||
? "bg-red-500"
|
||||
: "bg-gray-600"
|
||||
}`}
|
||||
/>
|
||||
<span className="text-xs text-gray-400">{status.label}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<HeatmapLegend layerType={activeLayer} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user