// Shared Technique model + pure filter/serialize logic. Imported by the Astro
// pages at build time and by the client-side catalog island — the functions
// have no framework or DOM dependency, so they run in both.
import { SOURCE_ROUTE, NS_SOURCE } from './taxonomy';
import type { Capability, Platform, SourceId } from './taxonomy';
// Route helpers — a tool lives at /<deckRoute>/<toolSlug>. Both the deck and
// the slug come from the colon-namespaced toolId ("gtfo:vim" -> gtfobins/vim,
// "wadcoms:Certipy" -> wadcoms/certipy), never from a per-technique source, so
// a mixed upstream+daemon family always resolves to one page. Lowercased so the
// path is case-stable on GitHub Pages.
export function toolSlug(toolId: string): string {
return toolId.split(':').slice(1).join('-').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
}
export function toolNamespace(toolId: string): string {
return toolId.split(':')[0];
}
export function routeSourceOf(toolId: string): SourceId {
return NS_SOURCE[toolNamespace(toolId)] ?? 'DAEMON';
}
export function toolRoute(toolId: string): string {
return `${SOURCE_ROUTE[routeSourceOf(toolId)]}/${toolSlug(toolId)}`;
}
export interface Detection { type: string; value: string }
export interface Technique {
id: string;
toolId: string;
toolName: string;
name?: string;
source: SourceId;
platform: Platform[];
capability: Capability[];
nativeCategory: string[];
command: string;
description?: string;
usecase?: string;
mitre: string[];
privilege?: string;
context?: string;
requires?: string[];
services?: string[];
fullPath?: string[];
toolType?: string;
detection?: Detection[];
references: string[];
added?: boolean;
verifyNote?: string;
environment?: string[];
aliases?: string[];
availability?: string;
verification?: string;
reviewedAt?: string;
compatibility?: string;
expected?: string;
troubleshooting?: string;
sideEffects?: string;
restore?: string;
template?: { command: string; shell: 'posix' | 'powershell'; variables: { key: string; label: string; default: string }[] };
}
export interface Tool {
id: string;
name: string;
source: SourceId;
platform: Platform[];
toolType?: string;
aliases?: string[];
fullPath?: string[];
author?: string;
created?: string;
contributors?: string[];
references: string[];
count: number;
}
export interface CatalogFilters {
platform: string[];
capability: string[];
source: string[];
query: string;
addedOnly: boolean;
context?: string[];
requires?: string[];
services?: string[];
environment?: string[];
availability?: string[];
verification?: string[];
}
export const EMPTY_FILTERS: CatalogFilters = {
platform: [], capability: [], source: [], query: '', addedOnly: false,
};
// OR within a facet (any ticked value present on the technique), AND across
// facets — the conventional faceted-search behaviour.
const inFacet = (values: string[], selected: string[]) =>
selected.length === 0 || selected.some((s) => values.includes(s));
export function searchText(t: Technique): string {
return [t.toolName, t.name, t.command, t.description, t.usecase, t.context, t.privilege,
...(t.aliases || []), ...(t.requires || []), ...(t.services || []), ...(t.environment || []), ...(t.nativeCategory || []), ...(t.mitre || [])]
.filter(Boolean)
.join(' ')
.toLowerCase();
}
export function matchesQuery(t: Technique, q: string): boolean {
const hay = searchText(t);
return q.toLowerCase().trim().split(/\s+/).every(word => hay.includes(word));
}
export const FACET_KEYS = ['platform', 'capability', 'source', 'context', 'requires', 'services', 'environment', 'availability', 'verification'] as const;
export type FacetKey = (typeof FACET_KEYS)[number];
export function facetValues(t: Technique, key: FacetKey): string[] {
const v = key === 'context' ? (t.context || t.privilege) : t[key];
return Array.isArray(v) ? v : v ? [v] : [];
}
export function matches(t: Technique, f: CatalogFilters): boolean {
if (f.addedOnly && !t.added) return false;
if (!inFacet(t.platform, f.platform)) return false;
if (!inFacet(t.capability, f.capability)) return false;
if (!inFacet([t.source], f.source)) return false;
for (const key of FACET_KEYS.slice(3)) if (!inFacet(facetValues(t, key), f[key] || [])) return false;
if (!matchesQuery(t, f.query)) return false;
return true;
}
export function filterTechniques(techniques: Technique[], f: CatalogFilters): Technique[] {
return techniques.filter((t) => matches(t, f));
}
export function countActive(f: CatalogFilters): number {
return FACET_KEYS.reduce((n, k) => n + (f[k]?.length || 0), 0);
}
export function isEmpty(f: CatalogFilters): boolean {
return countActive(f) === 0 && !f.query && !f.addedOnly;
}
// URL <-> filters. Comma-joined multi-values so a narrowed view is shareable.
const PARAM_KEYS = { platform: 'p', capability: 'c', source: 's', context: 'ctx', requires: 'access', services: 'svc', environment: 'env', availability: 'available', verification: 'verified' } as const;
export function filtersToSearch(f: CatalogFilters): string {
const params = new URLSearchParams();
for (const [key, param] of Object.entries(PARAM_KEYS) as [keyof typeof PARAM_KEYS, string][]) {
const vals = f[key];
if (vals?.length) params.set(param, vals.join(','));
}
if (f.addedOnly) params.set('new', '1');
const q = f.query.trim();
if (q) params.set('q', q);
return params.toString();
}
export function filtersFromSearch(search: string): CatalogFilters {
const params = new URLSearchParams(search.startsWith('?') ? search.slice(1) : search);
const list = (k: string) => (params.get(k) || '').split(',').map((s) => s.trim()).filter(Boolean);
return {
...Object.fromEntries(Object.entries(PARAM_KEYS).slice(3).filter(([, param]) => list(param).length).map(([key, param]) => [key, list(param)])),
platform: list(PARAM_KEYS.platform),
capability: list(PARAM_KEYS.capability),
source: list(PARAM_KEYS.source),
query: params.get('q') || '',
addedOnly: params.get('new') === '1',
};
}