Files
Agent-Workbench/scripts/research-fixtures.mjs
2026-10-30 05:21:17 +08:00

54 lines
3.6 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
// Self-owned inert artifact fixtures. These bytes are only parsed by REA;
// neither the JavaScript bootstrap nor the synthetic PE image is executed.
export function ownedAsar(files) {
let offset = 0; const directory = { files: {} }, bodies = [];
for (const [name, value] of Object.entries(files)) {
const bytes = Buffer.from(value, 'utf8'); directory.files[name] = { size: bytes.length, offset: String(offset) };
offset += bytes.length; bodies.push(bytes);
}
const json = Buffer.from(JSON.stringify(directory)), header = Buffer.alloc(8 + Math.ceil(json.length / 4) * 4);
header.writeUInt32LE(header.length - 4, 0); header.writeUInt32LE(json.length, 4); json.copy(header, 8);
const size = Buffer.alloc(8); size.writeUInt32LE(4, 0); size.writeUInt32LE(header.length, 4);
return Buffer.concat([size, header, ...bodies]);
}
export function ownedPe() {
const bytes = Buffer.alloc(1024), pe = 0x80, optional = pe + 24, section = optional + 0xf0;
bytes.write('MZ'); bytes.writeUInt32LE(pe, 0x3c); bytes.write('PE\0\0', pe, 'ascii');
bytes.writeUInt16LE(0x8664, pe + 4); bytes.writeUInt16LE(1, pe + 6); bytes.writeUInt16LE(0xf0, pe + 20); bytes.writeUInt16LE(0x2022, pe + 22);
bytes.writeUInt16LE(0x20b, optional); bytes.writeBigUInt64LE(0x140000000n, optional + 24);
bytes.writeUInt32LE(0x1000, optional + 32); bytes.writeUInt32LE(0x200, optional + 36);
bytes.writeUInt16LE(6, optional + 40); bytes.writeUInt16LE(6, optional + 48);
bytes.writeUInt32LE(0x2000, optional + 56); bytes.writeUInt32LE(0x200, optional + 60); bytes.writeUInt16LE(3, optional + 68);
bytes.writeUInt32LE(16, optional + 108);
bytes.writeUInt32LE(0x1000, optional + 120); bytes.writeUInt32LE(40, optional + 124);
bytes.writeUInt32LE(0x1100, optional + 128); bytes.writeUInt32LE(0x80, optional + 132);
bytes.write('.rdata', section, 'ascii'); bytes.writeUInt32LE(512, section + 8); bytes.writeUInt32LE(0x1000, section + 12);
bytes.writeUInt32LE(512, section + 16); bytes.writeUInt32LE(0x200, section + 20); bytes.writeUInt32LE(0x40000040, section + 36);
const raw = rva => rva - 0x1000 + 0x200;
bytes.writeUInt32LE(0x1040, raw(0x1000)); bytes.writeUInt32LE(0x1080, raw(0x1000) + 12); bytes.writeUInt32LE(0x1060, raw(0x1000) + 16);
bytes.writeBigUInt64LE(0x1090n, raw(0x1040)); bytes.writeBigUInt64LE(0x1090n, raw(0x1060));
bytes.write('KERNEL32.dll\0', raw(0x1080), 'ascii'); bytes.write('ExitProcess\0', raw(0x1090) + 2, 'ascii');
for (const [rva, name, child] of [[0x1100, 10, 0x80000020], [0x1120, 1, 0x80000040], [0x1140, 0x409, 0x60]]) {
bytes.writeUInt16LE(1, raw(rva) + 14); bytes.writeUInt32LE(name, raw(rva) + 16); bytes.writeUInt32LE(child, raw(rva) + 20);
}
bytes.writeUInt32LE(0x1170, raw(0x1160)); bytes.writeUInt32LE(8, raw(0x1160) + 4); bytes.write('REA-DATA', raw(0x1170), 'ascii');
return bytes;
}
export function createResearchSamples(directory) {
fs.mkdirSync(directory, { recursive: true });
const marker = path.join(directory, 'bootstrap-executed.txt');
const asar = path.join(directory, 'owned-agent.asar'), pe = path.join(directory, 'owned-imports-resources.dll');
fs.writeFileSync(asar, ownedAsar({
'package.json': JSON.stringify({ name: 'owned-agent-fixture', version: '1.0.0', main: 'main.js' }),
'main.js': "const fs = require('node:fs');\nfs.writeFileSync(" + JSON.stringify(marker) + ", 'must not execute');\nexports.inspect = async file => fs.promises.readFile(file, 'utf8');\n",
'preload.js': "export const contract = { approval: 'per-call', replay: false };\n",
}), { flag: 'wx' });
fs.writeFileSync(pe, ownedPe(), { flag: 'wx' });
return { asar, pe, marker };
}