repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
sheets.tsTypeScript112 lines4.3 KiBraw download
import { getCollection, type CollectionEntry } from 'astro:content';
import { CATEGORIES } from './taxonomy';
import reportGuides from '../data/report-guides.json';

export type Sheet = CollectionEntry<'sheets'>;

export async function allSheets(): Promise<Sheet[]> {
  const sheets = await getCollection('sheets', ({ data }) => data.draft !== true);
  return sheets.sort((a, b) => a.data.title.localeCompare(b.data.title));
}

export function sheetHref(entry: Sheet): string {
  return `sheets/${entry.id}`;
}

export function sheetCategories(entry: Sheet): string[] {
  return [...new Set([entry.data.category, ...entry.data.alsoIn])];
}

/** Sheets grouped by category, in taxonomy order. */
export async function sheetsByCategory(): Promise<{ slug: string; items: Sheet[] }[]> {
  const sheets = await allSheets();
  return CATEGORIES.map((c) => ({
    slug: c.slug,
    items: sheets.filter((s) => sheetCategories(s).includes(c.slug)),
  }));
}

export async function categoryCounts(): Promise<Record<string, number>> {
  const sheets = await allSheets();
  const counts: Record<string, number> = {};
  for (const s of sheets) {
    for (const category of sheetCategories(s)) counts[category] = (counts[category] ?? 0) + 1;
  }
  return counts;
}

/** Previous / next sheet within the same category. */
export function prevNext(items: Sheet[], current: Sheet): { prev?: Sheet; next?: Sheet } {
  const i = items.findIndex((s) => s.id === current.id);
  return { prev: i > 0 ? items[i - 1] : undefined, next: i >= 0 && i < items.length - 1 ? items[i + 1] : undefined };
}

/** Distinct tags with usage counts, most-used first. */
export async function allTags(): Promise<{ tag: string; count: number }[]> {
  const sheets = await allSheets();
  const map = new Map<string, number>();
  for (const s of sheets) for (const t of s.data.tags) map.set(t, (map.get(t) ?? 0) + 1);
  return [...map.entries()].map(([tag, count]) => ({ tag, count })).sort((a, b) => b.count - a.count || a.tag.localeCompare(b.tag));
}

/** Tools are compared without case: the sheets write both `Hashcat` and
 *  `hashcat`, and those are one tool. */
export function toolKey(tool: string): string {
  return tool.trim().toLowerCase();
}

/** Distinct tools with usage counts, most-used first. Case variants are
 *  merged under whichever spelling the sheets use most. */
export async function allTools(): Promise<{ tool: string; key: string; count: number }[]> {
  const sheets = await allSheets();
  const map = new Map<string, { count: number; spellings: Map<string, number> }>();
  for (const s of sheets) {
    for (const key of new Set(s.data.tools.map(toolKey))) {
      const hit = map.get(key) ?? map.set(key, { count: 0, spellings: new Map() }).get(key)!;
      hit.count += 1;
    }
    for (const t of s.data.tools) {
      const spellings = map.get(toolKey(t))!.spellings;
      spellings.set(t.trim(), (spellings.get(t.trim()) ?? 0) + 1);
    }
  }
  return [...map.entries()]
    .map(([key, { count, spellings }]) => ({
      key,
      count,
      tool: [...spellings.entries()].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))[0][0],
    }))
    .sort((a, b) => b.count - a.count || a.tool.localeCompare(b.tool));
}

/** Where a tool name links to: the all-sheets index, narrowed to that tool. */
export function toolHref(tool: string): string {
  return `sheets?tool=${encodeURIComponent(toolKey(tool))}`;
}

/** What the sheet filter (scripts/sheet-filter.ts) needs to know about a row.
 *  `sec` is the row's group on the page it is listed on: a section on a
 *  shelf, the shelf itself on the all-sheets and tag pages. */
export type SheetFacets = { text: string; tags: string[]; tools: string[]; diff: string; sec: string };

export function sheetFacets(entry: Sheet, sec = ''): SheetFacets {
  const d = entry.data;
  const tags = [...new Set(d.tags.map((t) => t.toLowerCase()))];
  const tools = [...new Set(d.tools.map(toolKey))];
  return {
    text: [d.title, d.description, d.subcategory ?? '', ...tags, ...tools].join(' ').toLowerCase(),
    tags,
    tools,
    diff: d.difficulty,
    sec,
  };
}

export function tagSlug(tag: string): string {
  return tag.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
}

/** Membership is public; chronology exists only in the encrypted index. */
export function isReportGuide(entry: Sheet): boolean {
  return reportGuides.includes(entry.id);
}