// The breakdown panel: what a set of techniques is made of, drawn as marks you
// can press. Pure — no DOM, no framework — so the same functions build the
// server-rendered panel on the collection pages and the home page, and the
// live one the catalog island repaints on every filter change.
//
// Two mark types, picked by the shape of the facet rather than by taste:
// split one bar cut into segments, for facets where a technique holds at
// most one value (context, collection, availability), so the
// segments really are parts of a whole;
// rank one bar per value, for facets a technique can hold several of
// (platform, capability, access, service), where segments would
// overlap and a split bar would lie about the total.
import { EMPTY_FILTERS, filtersToSearch, type CatalogFilters, type FacetKey } from './techniques';
import { AVAILABILITY_META, CONTEXT_META, PLATFORM_META, SOURCE_META, type Accent, type Platform, type SourceId } from './taxonomy';
import { escapeHtml as esc } from './highlight';
import { url } from './url';
/** The rail's group headings — the long, unambiguous name of each facet. */
export const FACET_TITLE: Record<FacetKey, string> = {
platform: 'Operating system / target',
capability: 'Capability',
source: 'Collection',
context: 'Execution context',
requires: 'Required access',
services: 'Service',
environment: 'Environment',
availability: 'Availability',
verification: 'Evidence',
};
/** The one-word name a filter chip is prefixed with. */
export const FACET_SHORT: Record<FacetKey, string> = {
platform: 'Target',
capability: 'Capability',
source: 'Collection',
context: 'Context',
requires: 'Access',
services: 'Service',
environment: 'Environment',
availability: 'Availability',
verification: 'Evidence',
};
/** The breakdown's block headings. */
const BLOCK_TITLE: Record<FacetKey, string> = {
platform: 'Target',
capability: 'Capabilities that recur',
source: 'Collection',
context: 'Execution context',
requires: 'Access you hold',
services: 'Service',
environment: 'Environment',
availability: 'Availability',
verification: 'Evidence',
};
/** How a facet value is written for a reader (the stored value stays the key). */
export function facetLabel(key: FacetKey, value: string): string {
if (key === 'platform' && value === 'ActiveDirectory') return 'Active Directory';
if (key === 'source') return SOURCE_META[value as SourceId]?.label ?? value;
return value;
}
/** Facets where a technique holds at most one value: the only honest splits. */
const SPLIT_KEYS: ReadonlySet<FacetKey> = new Set(['context', 'source', 'availability', 'verification']);
type Hue = Accent | 'muted';
function hueOf(key: FacetKey, value: string): Hue {
if (key === 'platform') return PLATFORM_META[value as Platform]?.accent ?? 'foam';
if (key === 'source') return SOURCE_META[value as SourceId]?.accent ?? 'foam';
if (key === 'context') return CONTEXT_META[value]?.accent ?? 'muted';
if (key === 'availability') return AVAILABILITY_META[value]?.accent ?? 'foam';
if (key === 'verification') return value === 'Upstream reference' ? 'foam' : 'gold';
return 'foam';
}
const hueVar = (hue: Hue) => (hue === 'muted' ? 'var(--fg-faint)' : `var(--${hue})`);
export type Counts = Partial<Record<FacetKey, Record<string, number>>>;
export type ActiveFacets = Partial<Record<FacetKey, string[]>>;
export interface BlockItem { value: string; label: string; n: number; hue: Hue }
export interface Block { key: FacetKey; title: string; kind: 'split' | 'rank'; items: BlockItem[]; sum: number; max: number }
export interface Breakdown { left: Block[]; right: Block | null }
export interface BlockOptions {
/** Fewest distinct values worth drawing. A static page wants 2 (one value is
* not a breakdown); the live catalog wants 1, so a block does not vanish
* from under the pointer when a filter narrows it. */
min?: number;
/** Most rows a ranked block shows. Active values always stay visible. */
limit?: number;
active?: ActiveFacets;
}
export function buildBlock(key: FacetKey, counts: Counts, { min = 2, limit = Infinity, active = {} }: BlockOptions = {}): Block | null {
const on = active[key] || [];
const all = Object.entries(counts[key] || {})
.filter(([value, n]) => n > 0 || on.includes(value))
.map(([value, n]) => ({ value, label: facetLabel(key, value), n, hue: hueOf(key, value) }))
.sort((a, b) => b.n - a.n || a.label.localeCompare(b.label));
if (all.length < min) return null;
const kind = SPLIT_KEYS.has(key) ? 'split' : 'rank';
// A split shows every part; a ranking shows the top of the list plus
// whatever is switched on, so a pressed bar is never out of sight.
const items = kind === 'split' ? all : [...all.slice(0, limit), ...all.slice(limit).filter((i) => on.includes(i.value))];
return {
key, kind, items, title: BLOCK_TITLE[key],
sum: all.reduce((n, i) => n + i.n, 0),
max: all[0]?.n ?? 0,
};
}
export interface BreakdownConfig extends BlockOptions {
/** Candidates for the left column, in order of preference. */
left: readonly FacetKey[];
/** How many of them to draw. */
maxLeft?: number;
/** The ranked list on the right. */
right: FacetKey;
}
export function buildBreakdown(counts: Counts, cfg: BreakdownConfig): Breakdown {
const left: Block[] = [];
for (const key of cfg.left) {
if (left.length >= (cfg.maxLeft ?? 2)) break;
const b = buildBlock(key, counts, { min: cfg.min, active: cfg.active });
if (b) left.push(b);
}
return { left, right: buildBlock(cfg.right, counts, { min: cfg.min, limit: cfg.limit ?? 6, active: cfg.active }) };
}
/** Every facet value a breakdown can be pressed for, as `key` → values. */
export function breakdownValues(b: Breakdown): Partial<Record<FacetKey, string[]>> {
const out: Partial<Record<FacetKey, string[]>> = {};
for (const block of [...b.left, ...(b.right ? [b.right] : [])]) out[block.key] = block.items.map((i) => i.value);
return out;
}
// ---- rendering -------------------------------------------------------------
/** Ledger numerals: `06`, `42`, `1,451`. */
export const ledger = (n: number) => (n < 10 ? String(n).padStart(2, '0') : n.toLocaleString('en-US'));
/** The catalog URL that shows exactly one facet value (or any filter set). */
export function catalogHref(filters: Partial<CatalogFilters>): string {
const qs = filtersToSearch({ ...EMPTY_FILTERS, ...filters });
return url('catalog') + (qs ? `?${qs}` : '');
}
export interface RenderOptions {
/** `button` marks toggle a filter on the page they are on (the page's own
* script reads `data-facet` / `data-value`); `link` marks open the catalog
* filtered to that value, and work without JavaScript. */
mode: 'button' | 'link';
active?: ActiveFacets;
/** What is being counted, for the tooltip and the screen-reader name. */
unit?: string;
/** A line under the panel: what the numbers are and what pressing does. */
note?: string;
}
function control(mode: RenderOptions['mode'], cls: string, key: FacetKey, item: BlockItem, on: boolean, attrs: string, inner: string): string {
const data = `data-facet="${key}" data-value="${esc(item.value)}"`;
return mode === 'link'
? `<a class="${cls}" href="${esc(catalogHref({ [key]: [item.value] }))}" ${data} ${attrs}>${inner}</a>`
: `<button type="button" class="${cls}" aria-pressed="${on}" ${data} ${attrs}>${inner}</button>`;
}
function renderBlock(b: Block, o: RenderOptions): string {
const on = o.active?.[b.key] || [];
const unit = o.unit || 'techniques';
const tip = (i: BlockItem, share?: number) =>
`title="${esc(`${i.label} — ${i.n.toLocaleString('en-US')} ${unit}${share === undefined ? '' : ` · ${share < 1 && i.n ? '<1' : Math.round(share)}%`}`)}"`;
const head = `<h3 class="bd__h">${esc(b.title)}</h3>`;
const state = on.length ? ' data-narrowed' : '';
if (b.kind === 'split') {
// The bar is the picture; the keys under it are the controls. The segments
// are pressable too, but hidden from assistive tech and the tab order so a
// value is announced (and tabbed to) once, not twice.
const segs = b.items.map((i) => {
const share = b.sum ? (i.n / b.sum) * 100 : 0;
return control(o.mode, 'bd-seg', b.key, i, on.includes(i.value),
`style="--w:${Math.max(share, 0.01).toFixed(2)};--seg:${hueVar(i.hue)};" tabindex="-1" aria-hidden="true" ${tip(i, share)}`, '');
}).join('');
const keys = b.items.map((i) => {
const share = b.sum ? (i.n / b.sum) * 100 : 0;
return `<li>${control(o.mode, 'bd-key', b.key, i, on.includes(i.value), tip(i, share),
`<i style="--seg:${hueVar(i.hue)};" aria-hidden="true"></i><span>${esc(i.label)}</span><b>${ledger(i.n)}</b>`)}</li>`;
}).join('');
return `<div class="bd-block"${state}>${head}<div class="bd-split">${segs}</div><ul class="bd-keys">${keys}</ul></div>`;
}
const rows = b.items.map((i) => {
const w = b.max ? (i.n / b.max) * 100 : 0;
return `<li>${control(o.mode, 'bd-bar', b.key, i, on.includes(i.value), tip(i),
`<span class="bd-bar__lbl">${esc(i.label)}</span><span class="bd-bar__track" aria-hidden="true"><i style="--w:${w.toFixed(2)}%;"></i></span><b class="bd-bar__n">${ledger(i.n)}</b>`)}</li>`;
}).join('');
return `<div class="bd-block"${state}>${head}<ol class="bd-rank">${rows}</ol></div>`;
}
/** The panel's inner markup: a column of splits/short rankings, then the long ranking. */
export function renderBreakdown(b: Breakdown, o: RenderOptions): string {
if (!b.left.length && !b.right) return '';
const left = b.left.length ? `<div class="bd__col">${b.left.map((x) => renderBlock(x, o)).join('')}</div>` : '';
const right = b.right ? `<div class="bd__col">${renderBlock(b.right, o)}</div>` : '';
const note = o.note ? `<p class="bd__note">${esc(o.note)}</p>` : '';
return `<div class="bd__cols${left && right ? '' : ' bd__cols--one'}">${left}${right}</div>${note}`;
}
/** The catalog's panel: where it runs, and what it holds, then what it does. */
export const CATALOG_BREAKDOWN: BreakdownConfig = { left: ['platform', 'context'], right: 'capability', min: 1, limit: 6 };
/** A collection page's panel: the first two facets that actually vary in that deck. */
export const DECK_BREAKDOWN: BreakdownConfig = { left: ['platform', 'context', 'source', 'requires', 'services', 'availability'], right: 'capability', min: 2, limit: 6 };
// ---- quick starts ----------------------------------------------------------
/** One-press filter sets for the questions the catalog is most often asked. */
export const QUICK_STARTS: { label: string; accent: Accent; filters: Partial<CatalogFilters> }[] = [
{ label: 'Linux · sudo → root', accent: 'gold', filters: { platform: ['Linux'], context: ['sudo'], capability: ['Privilege Escalation'] } },
{ label: 'Linux · SUID', accent: 'love', filters: { platform: ['Linux'], context: ['suid'] } },
{ label: 'Reverse shells', accent: 'love', filters: { capability: ['Reverse/Bind Shell'] } },
{ label: 'Windows · download', accent: 'iris', filters: { platform: ['Windows'], capability: ['File Download'] } },
{ label: 'Windows · AWL bypass', accent: 'iris', filters: { capability: ['AWL / Policy Bypass'] } },
{ label: 'AD · no credentials', accent: 'gold', filters: { environment: ['Active Directory'], requires: ['No credentials'] } },
{ label: 'AD · Kerberos', accent: 'gold', filters: { services: ['Kerberos'] } },
{ label: 'macOS · persistence', accent: 'rose', filters: { platform: ['macOS'], capability: ['Persistence'] } },
];