repositories

daemon-sec-lotl

DÆMONBins: GTFOBins × LOLBAS × WADComs × LOOBins in one filterable catalog

clonegit clone https://git.daemon-sec.xyz/daemon-sec-lotl.git
catalog-workspace.tsTypeScript35 lines2.2 KiBraw download
import { FACET_KEYS, facetValues, matches, type Technique, type CatalogFilters, type FacetKey } from './techniques';
export type SortOrder = 'tool' | 'source' | 'reviewed';
export function sortTechniques(rows: Technique[], order: SortOrder, query = ''): Technique[] {
  const needle = query.trim().toLowerCase();
  return [...rows].sort((a, b) => {
    const exact = Number(b.toolName.toLowerCase() === needle) - Number(a.toolName.toLowerCase() === needle);
    if (exact) return exact;
    if (order === 'reviewed') { const date = (b.reviewedAt || '').localeCompare(a.reviewedAt || ''); if (date) return date; }
    if (order === 'source') { const source = a.source.localeCompare(b.source); if (source) return source; }
    return a.toolName.localeCompare(b.toolName, 'en', { numeric: true, sensitivity: 'base' }) || a.id.localeCompare(b.id);
  });
}
export function groupTechniques(rows: Technique[]): Technique[][] {
  const groups = new Map<string, Technique[]>();
  for (const t of rows) { const group = groups.get(t.toolId) || []; group.push(t); groups.set(t.toolId, group); }
  return [...groups.values()];
}
export function contextualCounts(rows: Technique[], filters: CatalogFilters): Record<FacetKey, Record<string, number>> {
  return Object.fromEntries(FACET_KEYS.map(key => {
    const counts: Record<string, number> = {};
    for (const t of rows) if (matches(t, { ...filters, [key]: [] })) for (const value of facetValues(t, key)) counts[value] = (counts[value] || 0) + 1;
    return [key, counts];
  })) as Record<FacetKey, Record<string, number>>;
}
export function quoteArgument(value: string, shell: 'posix' | 'powershell'): string {
  if (/[\r\n\0]/.test(value)) throw new Error('Use a single-line value.');
  return shell === 'powershell' ? `'${value.replace(/'/g, "''")}'` : `'${value.replace(/'/g, "'\"'\"'")}'`;
}
export function configureCommand(template: NonNullable<Technique['template']>, values: Record<string, string>): string {
  const keys = new Set(template.variables.map(v => v.key));
  return template.command.replace(/\{\{([a-z][a-z0-9_]*)\}\}/g, (_, key: string) => {
    if (!keys.has(key)) throw new Error(`Unknown variable: ${key}`);
    return quoteArgument(values[key] ?? template.variables.find(v => v.key === key)!.default, template.shell);
  });
}