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
import-confinement.test.mjsJavaScript222 lines11.1 KiBraw download
// The importers turn files from outside the repository into published content.
// These tests run each one against a throwaway source tree and check that a
// symbolic link is refused before anything is written, that the vault importer
// will not publish a working ledger, and that a withdrawn download leaves the
// public folder with its checksum and signature.
//
// Each importer finds the repository from its own location, so the scripts are
// copied into a temporary "repo" and run from there. The real tree is never
// written to.
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, symlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import test from 'node:test';
import { fileURLToPath } from 'node:url';

const scripts = fileURLToPath(new URL('../scripts/', import.meta.url));
const python = spawnSync('python3', ['--version']).status === 0;
const skip = python ? false : 'python3 is not installed';
const SECRET = 'outside-secret-0f3a9c-must-never-be-published';

function sandbox(t) {
  const root = mkdtempSync(join(tmpdir(), 'dmn-import-'));
  t.after(() => rmSync(root, { recursive: true, force: true }));
  const repo = join(root, 'repo');
  mkdirSync(join(repo, 'scripts'), { recursive: true });
  for (const name of readdirSync(scripts)) if (name.endsWith('.py')) cpSync(join(scripts, name), join(repo, 'scripts', name));
  const secret = join(root, 'secret.txt');
  writeFileSync(secret, `${SECRET}\n`);
  return { root, repo, secret };
}

function write(file, text) {
  mkdirSync(dirname(file), { recursive: true });
  writeFileSync(file, text);
}

function run(repo, script, args = [], env = {}) {
  const result = spawnSync('python3', [join(repo, 'scripts', script), ...args], { encoding: 'utf8', env: { ...process.env, ...env } });
  return { code: result.status, out: `${result.stdout}${result.stderr}` };
}

/** Every regular file under dir whose bytes contain the secret. */
function leaks(dir) {
  const found = [];
  const visit = (current) => {
    for (const name of readdirSync(current)) {
      const p = join(current, name);
      if (statSync(p).isDirectory()) visit(p);
      else if (readFileSync(p).includes(SECRET)) found.push(p);
    }
  };
  if (existsSync(dir)) visit(dir);
  return found;
}

function refuses(result, repo) {
  assert.notEqual(result.code, 0, `the import should have failed:\n${result.out}`);
  assert.match(result.out, /unsafe|symbolic link/i);
  assert.deepEqual(leaks(repo), [], 'the linked file reached the repository');
}

test('the PayloadsAllTheThings importer refuses linked pages, licences and topics', { skip }, (t) => {
  const { root, repo, secret } = sandbox(t);
  const clone = join(root, 'patt');
  write(join(clone, 'SQL Injection', 'README.md'), '# SQL Injection\n\nBody.\n');
  write(join(clone, 'LICENSE'), 'MIT\n');

  const clean = run(repo, 'sync-payloads.py', [clone, 'abc123']);
  assert.equal(clean.code, 0, clean.out);
  const page = join(repo, 'src/content/payloads/sql-injection/index.md');
  assert.ok(existsSync(page));
  assert.equal(readFileSync(join(repo, 'vendor/PayloadsAllTheThings/LICENSE'), 'utf8'), 'MIT\n');

  symlinkSync(secret, join(clone, 'SQL Injection', 'notes.md'));
  refuses(run(repo, 'sync-payloads.py', [clone, 'abc124']), repo);
  assert.ok(existsSync(page), 'a refused sync must leave the existing mirror in place');
  rmSync(join(clone, 'SQL Injection', 'notes.md'));

  rmSync(join(clone, 'LICENSE'));
  symlinkSync(secret, join(clone, 'LICENSE'));
  refuses(run(repo, 'sync-payloads.py', [clone, 'abc125']), repo);
  assert.equal(readFileSync(join(repo, 'vendor/PayloadsAllTheThings/LICENSE'), 'utf8'), 'MIT\n');
  rmSync(join(clone, 'LICENSE'));
  writeFileSync(join(clone, 'LICENSE'), 'MIT\n');

  const outside = join(root, 'outside-topic');
  write(join(outside, 'README.md'), `# Elsewhere\n\n${SECRET}\n`);
  symlinkSync(outside, join(clone, 'Linked Topic'));
  refuses(run(repo, 'sync-payloads.py', [clone, 'abc126']), repo);
});

test('the InternalAllTheThings importer refuses linked pages', { skip }, (t) => {
  const { root, repo, secret } = sandbox(t);
  const clone = join(root, 'iatt');
  write(join(clone, 'docs', 'cloud', 'aws.md'), '# AWS\n\nBody.\n');
  write(join(clone, 'docs', 'DISCLAIMER.md'), '# Disclaimer\n\nBody.\n');

  const clean = run(repo, 'sync-internal.py', [clone, 'abc123']);
  assert.equal(clean.code, 0, clean.out);
  const page = join(repo, 'src/content/internal/cloud/aws.md');
  assert.ok(existsSync(page));

  symlinkSync(secret, join(clone, 'docs', 'cloud', 'notes.md'));
  refuses(run(repo, 'sync-internal.py', [clone, 'abc124']), repo);
  assert.ok(existsSync(page), 'a refused sync must leave the existing mirror in place');
  rmSync(join(clone, 'docs', 'cloud', 'notes.md'));

  symlinkSync(secret, join(clone, 'docs', 'ROOT.md'));
  refuses(run(repo, 'sync-internal.py', [clone, 'abc125']), repo);
});

test('the HackTricks importer refuses linked pages and does not enter linked folders', { skip }, (t) => {
  const { root, repo, secret } = sandbox(t);
  const clone = join(root, 'hacktricks');
  write(join(clone, 'src', 'pentesting-web', 'xss.md'), '# XSS\n\nBody.\n');

  const outside = join(root, 'outside-folder');
  write(join(outside, 'leak.md'), `# Elsewhere\n\n${SECRET}\n`);
  symlinkSync(outside, join(clone, 'src', 'pentesting-web', 'linked'));
  const clean = run(repo, 'sync-hacktricks.py', [clone, 'abc123']);
  assert.equal(clean.code, 0, clean.out);
  assert.ok(existsSync(join(repo, 'src/content/hacktricks/pentesting-web/xss.md')));
  assert.deepEqual(leaks(repo), [], 'a page behind a linked folder was imported');

  symlinkSync(secret, join(clone, 'src', 'pentesting-web', 'notes.md'));
  refuses(run(repo, 'sync-hacktricks.py', [clone, 'abc124']), repo);
  assert.ok(existsSync(join(repo, 'src/content/hacktricks/pentesting-web/xss.md')));
});

function vault(t) {
  const box = sandbox(t);
  const source = join(box.root, 'vault');
  const listed = spawnSync('python3', ['-c', [
    'import importlib.util, json, sys',
    'spec = importlib.util.spec_from_file_location("m", sys.argv[1])',
    'm = importlib.util.module_from_spec(spec); spec.loader.exec_module(m)',
    'print(json.dumps({"slugs": m.SLUGS, "never": sorted(m.NEVER_PUBLISH)}))',
  ].join('\n'), join(box.repo, 'scripts', 'sync-pentest-workflow.py')], { encoding: 'utf8' });
  assert.equal(listed.status, 0, listed.stderr);
  const { slugs, never } = JSON.parse(listed.stdout);
  for (const rel of Object.keys(slugs)) write(join(source, rel), `# ${rel}\n\nBody.\n`);
  const first = Object.keys(slugs)[0];
  const sync = () => run(box.repo, 'sync-pentest-workflow.py', [], { PENTEST_WORKFLOW_SOURCE: source });
  return { ...box, source, slugs, never, first, sync, sheet: (rel) => join(box.repo, 'src/content/sheets/pentest-workflow', `${slugs[rel]}.md`) };
}

test('the vault importer never publishes the loot ledger, a SENSITIVE note or a private key', { skip }, (t) => {
  const v = vault(t);
  assert.ok(v.never.includes('loot.md'));
  assert.ok(!Object.keys(v.slugs).includes('loot.md'), 'loot.md must not be on the publication list');

  write(join(v.source, 'loot.md'), '> [!warning]+ SENSITIVE — Live Credential Store\n\nsvc_backup : Winter2026!\n');
  const clean = v.sync();
  assert.equal(clean.code, 0, clean.out);
  assert.ok(!existsSync(join(v.repo, 'src/content/sheets/pentest-workflow/loot.md')));

  const original = readFileSync(join(v.source, v.first), 'utf8');
  const before = readFileSync(v.sheet(v.first), 'utf8');
  writeFileSync(join(v.source, v.first), `> [!danger] SENSITIVE: engagement secrets\n\n${original}`);
  const banner = v.sync();
  assert.notEqual(banner.code, 0);
  assert.match(banner.out, /Refusing to publish/);
  assert.equal(readFileSync(v.sheet(v.first), 'utf8'), before, 'a refused run must not rewrite the published sheet');

  const marker = (edge) => `-----${edge} OPENSSH ${'PRIVATE'} KEY-----`;
  const key = [marker('BEGIN'), ...Array.from({ length: 6 }, () => 'b3BlbnNzaC1rZXktdjEA'.repeat(3)), marker('END')].join('\n');
  writeFileSync(join(v.source, v.first), `${original}\n\`\`\`\n${key}\n\`\`\`\n`);
  const keyed = v.sync();
  assert.notEqual(keyed.code, 0);
  assert.match(keyed.out, /private-key block/);

  // A bare marker (a search dork, a grep pattern) is ordinary cheatsheet content.
  writeFileSync(join(v.source, v.first), `${original}\n\`grep -r "${marker('BEGIN')}" /home\`\n`);
  assert.equal(v.sync().code, 0);
});

test('the vault importer refuses linked notes and attachments', { skip }, (t) => {
  const v = vault(t);
  writeFileSync(join(v.source, v.first), '# Dashboard\n\n![[attachments/tool.exe]]\n');
  write(join(v.source, 'attachments', 'tool.exe'), 'real tool bytes\n');
  assert.equal(v.sync().code, 0);
  const downloads = join(v.repo, 'public/downloads/pentest-workflow');
  assert.equal(readFileSync(join(downloads, 'tool.exe'), 'utf8'), 'real tool bytes\n');

  rmSync(join(v.source, 'attachments', 'tool.exe'));
  symlinkSync(v.secret, join(v.source, 'attachments', 'tool.exe'));
  refuses(v.sync(), v.repo);
  assert.equal(readFileSync(join(downloads, 'tool.exe'), 'utf8'), 'real tool bytes\n');

  rmSync(join(v.source, 'attachments', 'tool.exe'));
  writeFileSync(join(v.source, 'attachments', 'tool.exe'), 'real tool bytes\n');
  const last = Object.keys(v.slugs).at(-1);
  rmSync(join(v.source, last));
  symlinkSync(v.secret, join(v.source, last));
  refuses(v.sync(), v.repo);
});

test('a withdrawn download leaves the public folder, and signatures are linked only when present', { skip }, (t) => {
  const v = vault(t);
  writeFileSync(join(v.source, v.first), '# Dashboard\n\n![[attachments/keep.exe]]\n\n![[attachments/old.exe]]\n\n[[attachments/SHA256SUMS.txt]]\n');
  write(join(v.source, 'attachments', 'keep.exe'), 'keep\n');
  write(join(v.source, 'attachments', 'old.exe'), 'old\n');
  assert.equal(v.sync().code, 0);
  const downloads = join(v.repo, 'public/downloads/pentest-workflow');
  assert.deepEqual(readdirSync(downloads).sort(), ['SHA256SUMS', 'keep.exe', 'keep.exe.sha256', 'old.exe', 'old.exe.sha256']);
  assert.doesNotMatch(readFileSync(v.sheet(v.first), 'utf8'), /\.asc\)/, 'no signature exists yet, so none may be linked');

  for (const name of ['SHA256SUMS.asc', 'keep.exe.sha256.asc', 'old.exe.sha256.asc']) writeFileSync(join(downloads, name), 'signature\n');
  writeFileSync(join(v.source, v.first), '# Dashboard\n\n![[attachments/keep.exe]]\n\n[[attachments/SHA256SUMS.txt]]\n');
  const second = v.sync();
  assert.equal(second.code, 0, second.out);
  // old.exe is gone with its checksum and signature; the manifest changed, so
  // its old signature no longer covers it; keep.exe is unchanged and stays signed.
  assert.deepEqual(readdirSync(downloads).sort(), ['SHA256SUMS', 'keep.exe', 'keep.exe.sha256', 'keep.exe.sha256.asc']);
  const sheet = readFileSync(v.sheet(v.first), 'utf8');
  assert.match(sheet, /keep\.exe\.sha256\.asc\)/);
  assert.doesNotMatch(sheet, /SHA256SUMS\.asc/);
  const catalogue = JSON.parse(readFileSync(join(v.repo, 'src/data/pentest-workflow-downloads.json'), 'utf8'));
  assert.deepEqual(catalogue.map((entry) => entry.name), ['keep.exe']);
});