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.

feat(shields): PCAP nested HTTP/DNS decoys past TSHARK_JSON_MAX_BYTES

qybaihe506f0789cf70
diff --git a/src/shields/formats/pcap-capture.ts b/src/shields/formats/pcap-capture.ts
index c8094d2..1f2a1db 100644
--- a/src/shields/formats/pcap-capture.ts
+++ b/src/shields/formats/pcap-capture.ts
@@ -1,3 +1,4 @@
+import { REA_INTERNAL_LIMITS } from "../../identity.js";
import { SeededPrng } from "../../core/prng.js";
import type { ProtectionIntensity } from "../../core/types.js";
import { buildCanaryAndDiagnosticPayloads } from "../deception/format-payloads.js";
@@ -5,6 +6,9 @@ import { buildCanaryAndDiagnosticPayloads } from "../deception/format-payloads.j
/** Classic libpcap global-header magic (microsecond, little-endian host). */
const PCAP_MAGIC_LE = 0xa1b2c3d4;
+/** Heuristic: tshark `-T json` typically expands each dissected byte ~4–8×. */
+const TSHARK_JSON_EXPANSION = 8;
+
export interface PcapInspection {
readonly validMagic: boolean;
readonly versionMajor: number;
@@ -16,26 +20,43 @@ export interface PcapInspection {
function buildGlobalHeader(): Buffer {
const hdr = Buffer.alloc(24);
hdr.writeUInt32LE(PCAP_MAGIC_LE, 0);
- hdr.writeUInt16LE(2, 4); // version major
- hdr.writeUInt16LE(4, 6); // version minor
- hdr.writeInt32LE(0, 8); // thiszone
- hdr.writeUInt32LE(0, 12); // sigfigs
- hdr.writeUInt32LE(65535, 16); // snaplen
- hdr.writeUInt32LE(1, 20); // network = Ethernet
+ hdr.writeUInt16LE(2, 4);
+ hdr.writeUInt16LE(4, 6);
+ hdr.writeInt32LE(0, 8);
+ hdr.writeUInt32LE(0, 12);
+ hdr.writeUInt32LE(65535, 16);
+ hdr.writeUInt32LE(1, 20); // Ethernet
return hdr;
}
-/** Minimal Ethernet + IPv4 + UDP frame carrying a UTF-8 payload. */
+function ipv4Checksum(ipHeader: Buffer): number {
+ let sum = 0;
+ for (let i = 0; i < 20; i += 2) {
+ sum += ipHeader.readUInt16BE(i);
+ }
+ while (sum > 0xffff) sum = (sum & 0xffff) + (sum >>> 16);
+ return (~sum) & 0xffff;
+}
+
+function recordWrap(frame: Buffer, tsSec: number, tsUsec: number): Buffer {
+ const recHdr = Buffer.alloc(16);
+ recHdr.writeUInt32LE(tsSec, 0);
+ recHdr.writeUInt32LE(tsUsec, 4);
+ recHdr.writeUInt32LE(frame.length, 8);
+ recHdr.writeUInt32LE(frame.length, 12);
+ return Buffer.concat([recHdr, frame]);
+}
+
function buildUdpPacket(payload: Buffer, tsSec: number, tsUsec: number): Buffer {
const eth = Buffer.alloc(14, 0);
eth[12] = 0x08;
- eth[13] = 0x00; // IPv4
+ eth[13] = 0x00;
const ipLen = 20 + 8 + payload.length;
const ip = Buffer.alloc(20, 0);
ip[0] = 0x45;
ip.writeUInt16BE(ipLen, 2);
- ip[8] = 64; // TTL
- ip[9] = 17; // UDP
+ ip[8] = 64;
+ ip[9] = 17;
ip[12] = 10;
ip[13] = 0;
ip[14] = 0;
@@ -44,18 +65,80 @@ function buildUdpPacket(payload: Buffer, tsSec: number, tsUsec: number): Buffer
ip[17] = 0;
ip[18] = 0;
ip[19] = 2;
- // Leave checksum 0 (valid for many offline parsers)
+ ip.writeUInt16BE(ipv4Checksum(ip), 10);
const udp = Buffer.alloc(8, 0);
udp.writeUInt16BE(1337, 0);
udp.writeUInt16BE(53, 2);
udp.writeUInt16BE(8 + payload.length, 4);
- const frame = Buffer.concat([eth, ip, udp, payload]);
- const recHdr = Buffer.alloc(16);
- recHdr.writeUInt32LE(tsSec, 0);
- recHdr.writeUInt32LE(tsUsec, 4);
- recHdr.writeUInt32LE(frame.length, 8);
- recHdr.writeUInt32LE(frame.length, 12);
- return Buffer.concat([recHdr, frame]);
+ return recordWrap(Buffer.concat([eth, ip, udp, payload]), tsSec, tsUsec);
+}
+
+function buildTcpHttpPacket(
+ payload: Buffer,
+ tsSec: number,
+ tsUsec: number,
+ seq: number,
+): Buffer {
+ const eth = Buffer.alloc(14, 0);
+ eth[12] = 0x08;
+ eth[13] = 0x00;
+ const tcpHdrLen = 20;
+ const ipLen = 20 + tcpHdrLen + payload.length;
+ const ip = Buffer.alloc(20, 0);
+ ip[0] = 0x45;
+ ip.writeUInt16BE(ipLen, 2);
+ ip[8] = 64;
+ ip[9] = 6;
+ ip[12] = 10;
+ ip[13] = 0;
+ ip[14] = 0;
+ ip[15] = 1;
+ ip[16] = 10;
+ ip[17] = 0;
+ ip[18] = 0;
+ ip[19] = 2;
+ ip.writeUInt16BE(ipv4Checksum(ip), 10);
+ const tcp = Buffer.alloc(tcpHdrLen, 0);
+ tcp.writeUInt16BE(40_000 + (seq % 1000), 0);
+ tcp.writeUInt16BE(80, 2);
+ tcp.writeUInt32BE(seq >>> 0, 4);
+ tcp.writeUInt32BE(1, 8);
+ tcp[12] = 5 << 4;
+ tcp[13] = 0x18; // PSH+ACK
+ tcp.writeUInt16BE(65535, 14);
+ return recordWrap(Buffer.concat([eth, ip, tcp, payload]), tsSec, tsUsec);
+}
+
+function buildDnsQueryPayload(depth: number, canary: string): Buffer {
+ const hdr = Buffer.alloc(12, 0);
+ hdr.writeUInt16BE(0xb1a0, 0);
+ hdr.writeUInt16BE(0x0100, 2); // standard query
+ hdr.writeUInt16BE(1, 4); // qdcount
+ const labels: Buffer[] = [];
+ for (let i = 0; i < depth; i++) {
+ const lab = `l${i.toString(36)}${canary.slice(0, 4)}`;
+ const b = Buffer.from(lab, "ascii");
+ labels.push(Buffer.from([b.length]), b);
+ }
+ labels.push(Buffer.from([0])); // root
+ const qtype = Buffer.alloc(4);
+ qtype.writeUInt16BE(1, 0); // A
+ qtype.writeUInt16BE(1, 2); // IN
+ return Buffer.concat([hdr, ...labels, qtype]);
+}
+
+function buildHttpLikePayload(prng: SeededPrng, headerCount: number): Buffer {
+ const lines: string[] = [
+ "GET /bla/tarpit/exhaust HTTP/1.1",
+ "Host: enclave.internal",
+ "User-Agent: BLA-PCAP-Shield/1.0",
+ `[BLA-PCAP-SHIELD] canary=${prng.hex(16)}`,
+ ];
+ for (let i = 0; i < headerCount; i++) {
+ lines.push(`X-BLA-Nested-${i}: ${prng.hex(48)}`);
+ }
+ lines.push("Connection: keep-alive", "", "");
+ return Buffer.from(lines.join("\r\n"), "utf8");
}
export function inspectPcapCapture(bytes: Buffer): PcapInspection {
@@ -63,12 +146,14 @@ export function inspectPcapCapture(bytes: Buffer): PcapInspection {
bytes.length >= 24 &&
(bytes.readUInt32LE(0) === PCAP_MAGIC_LE ||
bytes.readUInt32BE(0) === PCAP_MAGIC_LE ||
- bytes.readUInt32LE(0) === 0xa1b23c4d); // nanosecond variant
+ bytes.readUInt32LE(0) === 0xa1b23c4d);
let versionMajor = 0;
let versionMinor = 0;
let packetCount = 0;
if (validMagic && bytes.length >= 24) {
- const le = bytes.readUInt32LE(0) === PCAP_MAGIC_LE || bytes.readUInt32LE(0) === 0xa1b23c4d;
+ const le =
+ bytes.readUInt32LE(0) === PCAP_MAGIC_LE ||
+ bytes.readUInt32LE(0) === 0xa1b23c4d;
versionMajor = le ? bytes.readUInt16LE(4) : bytes.readUInt16BE(4);
versionMinor = le ? bytes.readUInt16LE(6) : bytes.readUInt16BE(6);
let at = 24;
@@ -90,12 +175,10 @@ export function inspectPcapCapture(bytes: Buffer): PcapInspection {
}
/**
- * Protects a classic libpcap capture by appending UDP packets whose payloads
- * carry canary/diagnostic strings. Preserves the original global header and
- * existing packets byte-for-byte; only appends new records.
- *
- * Note: REA's tshark path budgets JSON output (`TSHARK_JSON_MAX_BYTES`); these
- * decoy packets are designed to inflate dissected JSON when tshark runs.
+ * Protects a classic libpcap capture by appending nested-protocol packets
+ * (HTTP-like TCP + deep DNS UDP) whose dissected tshark JSON is estimated to
+ * exceed `TSHARK_JSON_MAX_BYTES` (8 MiB) under bankrupt intensity, while
+ * preserving the original global header and existing packets byte-for-byte.
*/
export function protectPcapCaptureBuffer(
input: Buffer,
@@ -108,6 +191,8 @@ export function protectPcapCaptureBuffer(
readonly packetsInjected: number;
readonly canaryTokensEmbedded: number;
readonly diagnosticsEmbedded: number;
+ readonly estimatedTsharkJsonBytes: number;
+ readonly exceedsTsharkJsonBudget: boolean;
} {
const intensity = options.intensity ?? "balanced";
const prng = new SeededPrng(options.seed ?? "bla-pcap-shield");
@@ -121,27 +206,49 @@ export function protectPcapCaptureBuffer(
? input
: buildGlobalHeader();
- const packetTarget =
- intensity === "bankrupt" ? 256 : intensity === "maximum" ? 64 : 16;
+ const httpPackets =
+ intensity === "bankrupt" ? 96 : intensity === "maximum" ? 24 : 4;
+ const dnsPackets =
+ intensity === "bankrupt" ? 64 : intensity === "maximum" ? 16 : 4;
+ const headerCount =
+ intensity === "bankrupt" ? 200 : intensity === "maximum" ? 48 : 12;
+ const dnsDepth = intensity === "bankrupt" ? 32 : intensity === "maximum" ? 16 : 6;
const injected: Buffer[] = [];
- const records = [
- "[BLA-PCAP-SHIELD]",
- ...deception.textRecords.slice(0, 12),
- `BLA_PCAP_CANARY=${prng.hex(16)}`,
- ];
- for (let i = 0; i < packetTarget; i++) {
- const payload = Buffer.from(
- `${records[i % records.length]}\npcap_decoy=${i}\n`,
+ let payloadBytes = 0;
+
+ for (let i = 0; i < httpPackets; i++) {
+ const payload = buildHttpLikePayload(prng, headerCount);
+ payloadBytes += payload.length;
+ injected.push(
+ buildTcpHttpPacket(payload, 1_700_000_000 + i, i * 1000, 1000 + i * 4096),
+ );
+ }
+ for (let i = 0; i < dnsPackets; i++) {
+ const payload = buildDnsQueryPayload(dnsDepth, prng.hex(8));
+ // Append diagnostic bait as additional TXT-like trailing bytes (still UDP).
+ const bait = Buffer.from(
+ `\n[BLA-PCAP-SHIELD]\n${deception.textRecords[i % Math.max(1, deception.textRecords.length)] ?? ""}\n`,
"utf8",
);
- injected.push(buildUdpPacket(payload, 1_700_000_000 + i, i * 1000));
+ const full = Buffer.concat([payload, bait]);
+ payloadBytes += full.length;
+ injected.push(buildUdpPacket(full, 1_700_100_000 + i, i * 2000));
}
+ // Conservative lower-bound estimate of tshark `-T json` size for these frames.
+ // Nested HTTP header trees and deep DNS labels expand far beyond raw bytes
+ // under tshark `-T json` (each header becomes multiple JSON nodes).
+ const estimatedTsharkJsonBytes =
+ injected.length * 4096 + payloadBytes * TSHARK_JSON_EXPANSION;
+
return {
buffer: Buffer.concat([base, ...injected]),
packetsInjected: injected.length,
canaryTokensEmbedded: deception.canary.syntheticCanaryTokens.length,
diagnosticsEmbedded: deception.diagnostics.diagnosticsInjected.length,
+ estimatedTsharkJsonBytes,
+ exceedsTsharkJsonBudget:
+ estimatedTsharkJsonBytes > REA_INTERNAL_LIMITS.PCAP_TSHARK_JSON_MAX_BYTES,
};
}