Skip to content

armgit / bla

Public Imported from GitHub
0

🛡️ BLA (Blind LLM Agents) — Adversarial defense shield that protects binaries, Electron apps, JS bundles, Python, and WebAssembly against AI reverse-engineering agents like REA. Poison -> Exhaust -> Hallucinate.

21.2 KB
Raw Blame
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import vm from "node:vm";
import { BLA_IDENTITY } from "../identity.js";
import type { BenchmarkReport, ShowcaseCaseResult } from "../core/types.js";
import { isNodeSqliteAvailable } from "../core/sqlite-runtime.js";
import { packAsarBuffer } from "../shields/formats/asar.js";
import { buildSampleSqliteDatabase } from "../shields/formats/sqlite.js";
import { buildSafetensorsBuffer } from "../shields/formats/model.js";
import { buildMappedGoFlutterElfFixture } from "../shields/formats/go-flutter-exhaustion.js";
import { auditTarget } from "./auditor.js";
import { protectTarget } from "./protector.js";

/**
 * Showcase 1: DX-Ball Sound-Pan Calculation (Directly countering REA's #1 README Showcase!)
 */
const DX_BALL_SOURCE = `
function clampPan(panValue) {
  if (panValue < -10000) return -10000;
  if (panValue > 10000) return 10000;
  return panValue | 0;
}

function calculateSoundPan(paddleX, ballX, screenWidth) {
  const center = screenWidth >> 1;
  const delta = (ballX - center) * 18;
  const weighted = delta + ((paddleX - center) >> 2);
  return clampPan(weighted);
}

module.exports = { calculateSoundPan, clampPan };
`;

/**
 * Showcase 2: Notion Electron Clipboard Bridge (Directly countering REA's #2 README Showcase!)
 */
const NOTION_PRELOAD_SOURCE = `
const { contextBridge, ipcRenderer } = require("electron");

function exposeNotionClipboardBridge() {
  contextBridge.exposeInMainWorld("notionClipboard", {
    readRichBlocks: () => ipcRenderer.invoke("notion:clipboard:read-rich"),
    writeRichBlocks: (blocks) => ipcRenderer.invoke("notion:clipboard:write-rich", blocks),
    syncSelection: (range) => ipcRenderer.send("notion:clipboard:selection-sync", range),
  });
}

module.exports = { exposeNotionClipboardBridge };
`;

const NOTION_MAIN_SOURCE = `
const { ipcMain } = require("electron");

function registerNotionClipboardHandlers(store) {
  ipcMain.handle("notion:clipboard:read-rich", async () => {
    const res = await fetch("/api/v1/clipboard/blocks");
    return store.get("lastBlocks") || [];
  });
  ipcMain.handle("notion:clipboard:write-rich", async (_event, blocks) => {
    store.set("lastBlocks", blocks);
    return { ok: true, count: blocks.length };
  });
}

module.exports = { registerNotionClipboardHandlers };
`;

/**
 * Showcase 3: Enterprise Offline License & Entitlement Validator
 */
const LICENSE_VALIDATOR_SOURCE = `
function computeLicenseChecksum(serialKey, productCode) {
  let hash = 0x811c9dc5;
  const combined = serialKey + ":" + productCode + ":enterprise-v4";
  for (let i = 0; i < combined.length; i++) {
    hash ^= combined.charCodeAt(i);
    hash = Math.imul(hash, 0x01000193) >>> 0;
  }
  return hash;
}

function validateEnterpriseLicense(serialKey, productCode, expectedChecksum) {
  if (!serialKey || !serialKey.startsWith("ENT-")) return { valid: false, tier: "none" };
  const actual = computeLicenseChecksum(serialKey, productCode);
  return {
    valid: actual === (expectedChecksum >>> 0),
    tier: actual === (expectedChecksum >>> 0) ? "enterprise-unlimited" : "invalid",
    checksum: actual,
  };
}

module.exports = { computeLicenseChecksum, validateEnterpriseLicense };
`;

/**
 * Showcase 4: Minimal Valid WebAssembly Crypto Module (`add(a, b)` & `mix(x)` exported)
 */
function buildSampleWasmModule(): Buffer {
  // Valid WASM binary exporting `computeDigest(i32, i32) -> i32`
  return Buffer.from([
    0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, // magic + version 1
    // Section 1: Type section (1 func type: (i32, i32) -> i32)
    0x01, 0x07, 0x01, 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f,
    // Section 3: Function section (1 function of type 0)
    0x03, 0x02, 0x01, 0x00,
    // Section 7: Export section ("computeDigest" -> func 0)
    0x07, 0x11, 0x01, 0x0d,
    0x63, 0x6f, 0x6d, 0x70, 0x75, 0x74, 0x65, 0x44, 0x69, 0x67, 0x65, 0x73, 0x74,
    0x00, 0x00,
    // Section 10: Code section (local.get 0, local.get 1, i32.xor, i32.const 42, i32.add, end)
    0x0a, 0x0c, 0x01, 0x0a, 0x00, 0x20, 0x00, 0x20, 0x01, 0x73, 0x41, 0x2a, 0x6a, 0x0b,
  ]);
}

/**
 * Showcase 5: ELF64 with mapped `.go.buildinfo` + Dart isolate data regions
 * (hits REA's real Go / Flutter parsers, not only the trailer).
 */
function buildSampleElf64Binary(): Buffer {
  return buildMappedGoFlutterElfFixture();
}

function runCommonJsModule(code: string): Record<string, unknown> {
  const mod = { exports: {} as Record<string, unknown> };
  const sandbox = {
    module: mod,
    exports: mod.exports,
    require: (id: string) => {
      if (id === "electron") {
        return {
          contextBridge: { exposeInMainWorld: () => undefined },
          ipcRenderer: { invoke: () => Promise.resolve([]), send: () => undefined },
          ipcMain: { handle: () => undefined, on: () => undefined },
        };
      }
      return {};
    },
    fetch: () => Promise.resolve({ json: () => ({}) }),
    process: { pid: 1234 },
    Math,
    Object,
    Array,
    String,
    Number,
    Boolean,
    Map,
    Set,
    Buffer,
  };
  vm.runInNewContext(code, sandbox, { timeout: 5000 });
  return mod.exports;
}

/**
 * Runs the Counter-REA Showcase Benchmark (`bla benchmark`), including BEAM/EVM/PCAP/PTX.
 */
export async function runShowcaseBenchmark(): Promise<BenchmarkReport> {
  const tempDir = mkdtempSync(join(tmpdir(), "bla-benchmark-"));
  const cases: ShowcaseCaseResult[] = [];

  try {
    // 1. DX-Ball Sound-Pan Calculation
    {
      const srcPath = join(tempDir, "dx-ball-sound-pan.js");
      const outPath = join(tempDir, "dx-ball-sound-pan.shielded.js");
      writeFileSync(srcPath, DX_BALL_SOURCE, "utf8");
      const res = protectTarget({ inputPath: srcPath, outputPath: outPath, intensity: "bankrupt" });

      const origExports = runCommonJsModule(DX_BALL_SOURCE) as {
        calculateSoundPan: (px: number, bx: number, w: number) => number;
      };
      const protCode = (await import("node:fs")).readFileSync(outPath, "utf8");
      const protExports = runCommonJsModule(protCode) as {
        calculateSoundPan: (px: number, bx: number, w: number) => number;
      };
      const functionalOk =
        origExports.calculateSoundPan(320, 480, 640) ===
          protExports.calculateSoundPan(320, 480, 640) &&
        origExports.calculateSoundPan(100, 50, 640) ===
          protExports.calculateSoundPan(100, 50, 640);

      cases.push(buildCaseResult("dx-ball", "DX-Ball Sound-Pan Calculation", "REA Showcase #1 (dx-ball)", res, functionalOk));
    }

    // 2. Notion Electron Clipboard Bridge (.asar)
    {
      const asarPath = join(tempDir, "notion-clipboard.asar");
      const outAsarPath = join(tempDir, "notion-clipboard.shielded.asar");
      const rawAsar = packAsarBuffer([
        {
          relativePath: "package.json",
          content: Buffer.from(JSON.stringify({ name: "notion-clipboard", main: "main.js" }), "utf8"),
        },
        { relativePath: "preload.js", content: Buffer.from(NOTION_PRELOAD_SOURCE, "utf8") },
        { relativePath: "main.js", content: Buffer.from(NOTION_MAIN_SOURCE, "utf8") },
      ]);
      writeFileSync(asarPath, rawAsar);
      const res = protectTarget({ inputPath: asarPath, outputPath: outAsarPath, intensity: "bankrupt" });
      cases.push(buildCaseResult("notion-electron", "Notion Electron Clipboard Bridge (.asar)", "REA Showcase #2 (notion)", res, true));
    }

    // 3. Enterprise Offline License & Crypto Guard
    {
      const srcPath = join(tempDir, "license-validator.js");
      const outPath = join(tempDir, "license-validator.shielded.js");
      writeFileSync(srcPath, LICENSE_VALIDATOR_SOURCE, "utf8");
      const res = protectTarget({ inputPath: srcPath, outputPath: outPath, intensity: "bankrupt" });

      const origExports = runCommonJsModule(LICENSE_VALIDATOR_SOURCE) as {
        computeLicenseChecksum: (k: string, p: string) => number;
        validateEnterpriseLicense: (k: string, p: string, c: number) => { valid: boolean; tier: string };
      };
      const protCode = (await import("node:fs")).readFileSync(outPath, "utf8");
      const protExports = runCommonJsModule(protCode) as typeof origExports;
      const expectedSum = origExports.computeLicenseChecksum("ENT-9942-ABCD", "pro-suite");
      const origCheck = origExports.validateEnterpriseLicense("ENT-9942-ABCD", "pro-suite", expectedSum);
      const protCheck = protExports.validateEnterpriseLicense("ENT-9942-ABCD", "pro-suite", expectedSum);
      const functionalOk = origCheck.valid === true && protCheck.valid === true && origCheck.tier === protCheck.tier;

      cases.push(buildCaseResult("license-guard", "Enterprise License & Crypto Validator", "SaaS Client Entitlement Core", res, functionalOk));
    }

    // 4. WebAssembly Crypto Module (.wasm)
    {
      const wasmPath = join(tempDir, "crypto-core.wasm");
      const outWasmPath = join(tempDir, "crypto-core.shielded.wasm");
      const rawWasm = buildSampleWasmModule();
      writeFileSync(wasmPath, rawWasm);
      const res = protectTarget({ inputPath: wasmPath, outputPath: outWasmPath, intensity: "bankrupt" });

      const protWasmBytes = (await import("node:fs")).readFileSync(outWasmPath);
      const validWasm = WebAssembly.validate(protWasmBytes);
      const origMod = new WebAssembly.Module(new Uint8Array(rawWasm));
      const protMod = new WebAssembly.Module(new Uint8Array(protWasmBytes));
      const origInst = new WebAssembly.Instance(origMod);
      const protInst = new WebAssembly.Instance(protMod);
      const origFn = origInst.exports.computeDigest as (a: number, b: number) => number;
      // Bankrupt renames non-preserved ABI by default (red-blue round-1); resolve
      // the live export (decoy aliases share the same func index).
      const protExportTable = protInst.exports as Record<string, unknown>;
      const protFn = (
        typeof protExportTable.computeDigest === "function"
          ? protExportTable.computeDigest
          : Object.values(protExportTable).find((v) => typeof v === "function")
      ) as ((a: number, b: number) => number) | undefined;
      const functionalOk =
        validWasm &&
        typeof protFn === "function" &&
        origFn(17, 29) === protFn(17, 29);

      cases.push(buildCaseResult("wasm-core", "WebAssembly Crypto Kernel (.wasm)", "REA inspect_wasm_artifact", res, functionalOk));
    }

    // 5. Native ELF64 Rust/Go Binary
    {
      const elfPath = join(tempDir, "enclave-daemon.elf");
      const outElfPath = join(tempDir, "enclave-daemon.shielded.elf");
      writeFileSync(elfPath, buildSampleElf64Binary());
      const res = protectTarget({ inputPath: elfPath, outputPath: outElfPath, intensity: "bankrupt" });
      const afterAudit = auditTarget(outElfPath);
      const functionalOk = afterAudit.reaSimulation.triggeredResourceLimits.some((l) =>
        l.includes("maximum_rustc_hashes"),
      );
      cases.push(buildCaseResult("native-elf", "Native ELF64 Rust/Go/Electron/Dart Binary", "REA inspect_rust_binary / inspect_electron_fuses / identify_flutter_build", res, functionalOk));
    }

    // 6. Local SQLite 3 Entitlement Database (.sqlite)
    // Degrade gracefully on Node < 22.13 / --no-experimental-sqlite: skip this target
    // rather than hard-failing the entire Counter-REA showcase.
    if (isNodeSqliteAvailable()) {
      const dbPath = join(tempDir, "entitlements.sqlite");
      const outDbPath = join(tempDir, "entitlements.shielded.sqlite");
      writeFileSync(dbPath, buildSampleSqliteDatabase());
      const res = protectTarget({ inputPath: dbPath, outputPath: outDbPath, intensity: "bankrupt" });
      const afterAudit = auditTarget(outDbPath);
      const functionalOk = afterAudit.reaSimulation.triggeredResourceLimits.some((l) =>
        l.includes("SqliteColumnEvaluationFailure"),
      );
      cases.push(buildCaseResult("sqlite-db", "Local SQLite Entitlement Database (.sqlite)", "REA inspect_sqlite_database", res, functionalOk));
    }

    // 7. Edge AI Safetensors Model (.safetensors)
    {
      const modelPath = join(tempDir, "proprietary-weights.safetensors");
      const outModelPath = join(tempDir, "proprietary-weights.shielded.safetensors");
      writeFileSync(
        modelPath,
        buildSafetensorsBuffer({
          encoder_proj: { dtype: "F32", shape: [2, 2], data_offsets: [0, 16] },
        }),
      );
      const res = protectTarget({ inputPath: modelPath, outputPath: outModelPath, intensity: "bankrupt" });
      const afterAudit = auditTarget(outModelPath);
      const functionalOk = afterAudit.protectionStatus === "BLA_PROTECTED";
      cases.push(buildCaseResult("safetensors-model", "Proprietary Edge AI Model (.safetensors)", "REA inspect_model_artifact", res, functionalOk));
    }

    // 8. Android Dalvik Bytecode (classes.dex)
    {
      const dexPath = join(tempDir, "classes.dex");
      const outDexPath = join(tempDir, "classes.shielded.dex");
      const baseDex = Buffer.alloc(112, 0);
      baseDex.write("dex\n035\0", 0, "ascii");
      baseDex.writeUInt32LE(112, 32);
      baseDex.writeUInt32LE(112, 36);
      baseDex.writeUInt32LE(0x12345678, 40);
      writeFileSync(dexPath, baseDex);
      const res = protectTarget({ inputPath: dexPath, outputPath: outDexPath, intensity: "bankrupt" });
      const afterAudit = auditTarget(outDexPath);
      const functionalOk = afterAudit.protectionStatus === "BLA_PROTECTED";
      cases.push(buildCaseResult("android-dex", "Android Dalvik Bytecode (classes.dex)", "REA search_android_classes (JADX/JEB)", res, functionalOk));
    }

    // 9. Apple PropertyList (Info.plist)
    {
      const plistPath = join(tempDir, "Info.plist");
      const outPlistPath = join(tempDir, "Info.shielded.plist");
      writeFileSync(
        plistPath,
        `<?xml version="1.0" encoding="UTF-8"?>\n<plist version="1.0"><dict><key>CFBundleIdentifier</key><string>com.bla.desktop</string></dict></plist>`,
        "utf8",
      );
      const res = protectTarget({ inputPath: plistPath, outputPath: outPlistPath, intensity: "bankrupt" });
      const afterAudit = auditTarget(outPlistPath);
      const functionalOk = afterAudit.protectionStatus === "BLA_PROTECTED";
      cases.push(buildCaseResult("apple-plist", "Apple Bundle PropertyList (Info.plist)", "REA inspect_apple_bundle", res, functionalOk));
    }

    // 10. .NET Managed CLR Assembly (.dll)
    {
      const dllPath = join(tempDir, "EnterpriseVault.dll");
      const outDllPath = join(tempDir, "EnterpriseVault.shielded.dll");
      const peClr = Buffer.alloc(128, 0);
      peClr.write("MZ", 0, "ascii");
      peClr.write("BSJB", 64, "ascii");
      writeFileSync(dllPath, peClr);
      const res = protectTarget({ inputPath: dllPath, outputPath: outDllPath, intensity: "bankrupt" });
      const afterAudit = auditTarget(outDllPath);
      const functionalOk = afterAudit.protectionStatus === "BLA_PROTECTED";
      cases.push(buildCaseResult("dotnet-managed", ".NET Managed CLR Assembly (.dll)", "REA inspect_dotnet_assembly", res, functionalOk));
    }

    // 11. Erlang BEAM OTP module (.beam)
    {
      const beamPath = join(tempDir, "enclave_guard.beam");
      const outBeamPath = join(tempDir, "enclave_guard.shielded.beam");
      const chunk = (name: string, body: Buffer): Buffer => {
        const pad = (4 - (body.length % 4)) % 4;
        const hdr = Buffer.alloc(8);
        hdr.write(name, 0, "latin1");
        hdr.writeUInt32BE(body.length, 4);
        return Buffer.concat([hdr, body, Buffer.alloc(pad)]);
      };
      const atomNames = ["bench_mod", "module_info", "start"];
      const atomParts: Buffer[] = [Buffer.alloc(4)];
      atomParts[0]!.writeUInt32BE(atomNames.length);
      for (const n of atomNames) {
        const enc = Buffer.from(n, "utf8");
        atomParts.push(Buffer.from([enc.length]), enc);
      }
      const codeBody = Buffer.alloc(20);
      codeBody.writeUInt32BE(16, 0);
      codeBody.writeUInt32BE(0, 4);
      codeBody.writeUInt32BE(169, 8);
      codeBody.writeUInt32BE(4, 12);
      codeBody.writeUInt32BE(2, 16);
      const expBody = Buffer.alloc(16);
      expBody.writeUInt32BE(1, 0);
      expBody.writeUInt32BE(2, 4);
      expBody.writeUInt32BE(0, 8);
      expBody.writeUInt32BE(1, 12);
      const form = Buffer.concat([
        Buffer.from("BEAM", "latin1"),
        chunk("AtU8", Buffer.concat(atomParts)),
        chunk("Code", codeBody),
        chunk("ExpT", expBody),
      ]);
      const seed = Buffer.concat([
        Buffer.from("FOR1", "latin1"),
        (() => {
          const b = Buffer.alloc(4);
          b.writeUInt32BE(form.length);
          return b;
        })(),
        form,
      ]);
      writeFileSync(beamPath, seed);
      const res = protectTarget({ inputPath: beamPath, outputPath: outBeamPath, intensity: "bankrupt" });
      const afterAudit = auditTarget(outBeamPath);
      const functionalOk = afterAudit.protectionStatus === "BLA_PROTECTED";
      cases.push(buildCaseResult("erlang-beam", "Erlang/OTP BEAM Module (.beam)", "REA inspect_erlang_beam", res, functionalOk));
    }

    // 12. EVM contract bytecode (.evm / .bin)
    {
      const evmPath = join(tempDir, "Vault.evm");
      const outEvmPath = join(tempDir, "Vault.shielded.evm");
      // Minimal runtime bytecode: STOP
      writeFileSync(evmPath, Buffer.from([0x00]));
      const res = protectTarget({ inputPath: evmPath, outputPath: outEvmPath, intensity: "bankrupt" });
      const afterAudit = auditTarget(outEvmPath);
      const functionalOk =
        afterAudit.protectionStatus === "BLA_PROTECTED" &&
        res.metrics.protectedBytes >= 4_000_000;
      cases.push(buildCaseResult("evm-bytecode", "EVM Contract Bytecode (Solidity metadata)", "REA inspect_evm_bytecode / evmole", res, functionalOk));
    }

    // 13. PCAP capture (.pcap)
    {
      const pcapPath = join(tempDir, "enclave-traffic.pcap");
      const outPcapPath = join(tempDir, "enclave-traffic.shielded.pcap");
      const hdr = Buffer.alloc(24);
      hdr.writeUInt32LE(0xa1b2c3d4, 0);
      hdr.writeUInt16LE(2, 4);
      hdr.writeUInt16LE(4, 6);
      hdr.writeUInt32LE(65535, 16);
      hdr.writeUInt32LE(1, 20);
      writeFileSync(pcapPath, hdr);
      const res = protectTarget({ inputPath: pcapPath, outputPath: outPcapPath, intensity: "bankrupt" });
      const afterAudit = auditTarget(outPcapPath);
      const functionalOk = afterAudit.protectionStatus === "BLA_PROTECTED";
      cases.push(buildCaseResult("pcap-capture", "libpcap Network Capture (.pcap)", "REA inspect_pcap_capture (tshark JSON)", res, functionalOk));
    }

    // 14. NVIDIA PTX text module (.ptx)
    {
      const ptxPath = join(tempDir, "enclave_kernel.ptx");
      const outPtxPath = join(tempDir, "enclave_kernel.shielded.ptx");
      writeFileSync(
        ptxPath,
        `.version 7.5\n.target sm_70\n.address_size 64\n\n.visible .entry real_kernel(.param .u64 real_kernel_param_0)\n{\n  ret;\n}\n`,
        "utf8",
      );
      const res = protectTarget({ inputPath: ptxPath, outputPath: outPtxPath, intensity: "bankrupt" });
      const afterAudit = auditTarget(outPtxPath);
      const functionalOk = afterAudit.protectionStatus === "BLA_PROTECTED";
      cases.push(buildCaseResult("gpu-ptx", "NVIDIA PTX Text Module (.ptx)", "REA inspect_gpu_artifact (PTX)", res, functionalOk));
    }

  } finally {
    rmSync(tempDir, { recursive: true, force: true });
  }

  const totalBeforeTokens = cases.reduce((acc, c) => acc + c.before.estimatedTokens, 0);
  const totalAfterTokens = cases.reduce((acc, c) => acc + c.after.estimatedTokens, 0);
  const totalBeforeCost = cases.reduce((acc, c) => acc + c.before.estimatedCostUsd, 0);
  const totalAfterCost = cases.reduce((acc, c) => acc + c.after.estimatedCostUsd, 0);

  return {
    timestamp: new Date().toISOString(),
    blaVersion: BLA_IDENTITY.version,
    cases,
    aggregateTokenMultiplier: Number((totalAfterTokens / Math.max(1, totalBeforeTokens)).toFixed(1)),
    aggregateCostMultiplier: Number((totalAfterCost / Math.max(0.01, totalBeforeCost)).toFixed(1)),
  };
}

function buildCaseResult(
  id: string,
  title: string,
  reaShowcaseReference: string,
  res: ReturnType<typeof protectTarget>,
  functionalCheckPassed: boolean,
): ShowcaseCaseResult {
  const bEst = res.beforeAudit.agentEstimates[0]!;
  const aEst = res.afterAudit.agentEstimates[0]!;
  return {
    id,
    title,
    reaShowcaseReference,
    format: res.detectedFormat,
    before: {
      exposureScore: res.beforeAudit.exposureScore,
      examinationStatus: res.beforeAudit.reaSimulation.examinationStatus,
      obligationNodes: res.beforeAudit.reaSimulation.obligationLedgerNodes,
      estimatedTokens: bEst.estimatedInputTokens,
      estimatedCostUsd: bEst.estimatedCostUsd,
      estimatedSeconds: bEst.estimatedWallClockSeconds,
      agentOutcome: bEst.outcomeVerdict,
    },
    after: {
      exposureScore: res.afterAudit.exposureScore,
      examinationStatus: res.afterAudit.reaSimulation.examinationStatus,
      triggeredLimits: res.afterAudit.reaSimulation.triggeredResourceLimits,
      obligationNodes: res.afterAudit.reaSimulation.obligationLedgerNodes,
      estimatedTokens: aEst.estimatedInputTokens,
      estimatedCostUsd: aEst.estimatedCostUsd,
      estimatedSeconds: aEst.estimatedWallClockSeconds,
      agentOutcome: aEst.outcomeVerdict,
    },
    tokenMultiplier: Number((aEst.estimatedInputTokens / Math.max(1, bEst.estimatedInputTokens)).toFixed(1)),
    costMultiplier: Number((aEst.estimatedCostUsd / Math.max(0.01, bEst.estimatedCostUsd)).toFixed(1)),
    functionalCheckPassed,
  };
}