feat(firmware): integrate Binwalk and Unblob analysis

* feat(firmware): integrate Binwalk and Unblob analysis

* fix(firmware): verify published files and namespace source references

* test(firmware): isolate extractor dependency failure from process cleanup

* fix(firmware): retain actionable report and cleanup diagnostics

* test: assert retained batch output failure diagnostics
This commit is contained in:
N0zoM1z0
2026-10-06 14:16:04 +00:00
committed by GitHub
parent 8d7071da4c
commit 5a7c769692
58 changed files with 3230 additions and 16 deletions
+129
View File
@@ -0,0 +1,129 @@
import { createHash } from "node:crypto";
import { mkdir, readFile, writeFile, symlink } from "node:fs/promises";
import { join } from "node:path";
const args = process.argv.slice(2);
const mode = process.env.REA_FIRMWARE_FIXTURE_MODE;
const engine = process.env.REA_FIRMWARE_FIXTURE_ENGINE;
if (args.includes("--version")) {
console.log(
mode === "version"
? "unsupported"
: engine === "binwalk"
? "binwalk 3.1.0"
: "26.6.4",
);
} else if (mode === "stall") {
setInterval(() => {}, 1000);
} else if (mode === "startup") {
console.error("Landlock sandbox is not available on this fixture host");
process.exitCode = 1;
} else if (mode === "missing-report") {
console.error("Producer forgot its report");
} else {
const input = args.at(-1);
const bytes = await readFile(input);
const sha256 = createHash("sha256").update(bytes).digest("hex");
const reportPath =
args[args.indexOf(engine === "binwalk" ? "--log" : "--report") + 1];
if (engine === "binwalk") {
await writeFile(
reportPath,
JSON.stringify([
{
Analysis: {
file_path: mode === "wrong-input" ? "/tmp/another.bin" : input,
file_map: [
{
id: "hit",
offset: 2,
size: mode === "bounds" ? bytes.length : 4,
name: "gzip",
confidence: 250,
description: "fixture signature",
always_display: false,
extraction_declined: false,
},
],
extractions: {},
},
},
]),
);
} else {
const output = args[args.indexOf("--extract-dir") + 1];
await mkdir(output, { recursive: true });
const child =
mode === "reserved-name"
? join(output, "$input")
: join(output, "rootfs", "config");
await mkdir(join(output, "rootfs"));
const content = Buffer.from("firmware=true\n");
if (mode !== "missing-file")
await writeFile(child, mode === "budget" ? Buffer.alloc(10000) : content);
if (mode === "link") await symlink("/etc/passwd", join(output, "escape"));
const childHash = createHash("sha256").update(content).digest("hex");
const depth =
mode === "depth" ? Number(args[args.indexOf("--depth") + 1]) : 1;
const stat = (path, size) => ({
__typename__: "StatReport",
path,
size,
is_file: true,
is_dir: false,
is_link: false,
link_target: null,
});
const task = (path, level, id) => ({
path,
depth: level,
blob_id: id,
is_multi_file: false,
});
const hash = (value) => ({ __typename__: "HashReport", sha256: value });
const childTask = task(child, depth, "gzip-id");
const records = [
{
task: childTask,
reports: [
stat(child, content.length),
...(mode === "depth"
? []
: [hash(mode === "hash" ? "a".repeat(64) : childHash)]),
],
subtasks: [],
},
{
task: task(input, 0, ""),
reports: [
stat(input, bytes.length),
hash(sha256),
{
__typename__: "ChunkReport",
id: "gzip-id",
start_offset: 0,
end_offset: bytes.length,
size: bytes.length,
handler_name: "gzip",
is_encrypted: false,
extraction_reports:
mode === "dependency"
? [
{
__typename__: "ExtractorDependencyNotFoundReport",
severity: "ERROR",
dependencies: ["sasquatch"],
},
]
: [],
},
],
subtasks: [childTask],
},
];
await writeFile(
reportPath,
mode === "malformed" ? "[" : JSON.stringify(records),
);
if (mode === "dependency") process.exitCode = 1;
}
}
+94
View File
@@ -0,0 +1,94 @@
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { access, rm, writeFile } from "node:fs/promises";
import { expect, onTestFinished } from "vitest";
import { FirmwareProvider } from "../../../src/firmware/FirmwareProvider.js";
import { FirmwareAnalysisService } from "../../../src/application/FirmwareAnalysisService.js";
import { spawnOwnedProviderProcess } from "../../../src/process/ProviderProcess.js";
import { createTestTempDirectory } from "../temporaryDirectory.js";
/** Exercise real subprocess ownership with synthetic pinned producer representations. */
export const firmwareFixture = async (mode = "normal") => {
const root = await createTestTempDirectory("rea-firmware-test-");
const path = join(root, "input.bin");
await writeFile(path, Buffer.from("fixture-input-data"));
onTestFinished(() => rm(root, { recursive: true, force: true }));
const launches: {
cwd: string | undefined;
pid: number | undefined;
args: readonly string[];
}[] = [];
const provider = new FirmwareProvider(
{
...process.env,
REA_BINWALK_COMMAND: "/usr/bin/true",
REA_UNBLOB_COMMAND: process.execPath,
},
async (options) => {
const start = options.arguments.indexOf("--") + 2;
const args = options.arguments.slice(start);
const engine =
options.arguments[start - 1]?.endsWith("/true") === true
? "binwalk"
: "unblob";
const spawned = await spawnOwnedProviderProcess({
...options,
command: process.execPath,
expectedCommand: process.execPath,
arguments: [
fileURLToPath(new URL("./producer.mjs", import.meta.url)),
...args,
],
env: {
...options.env,
REA_FIRMWARE_FIXTURE_MODE: mode,
REA_FIRMWARE_FIXTURE_ENGINE: engine,
},
});
launches.push({
cwd: options.cwd,
pid: spawned.process.pid,
args: options.arguments,
});
if (mode === "cleanup-failure") {
onTestFinished(async () => {
if (options.cwd !== undefined)
await rm(options.cwd, { recursive: true, force: true });
});
return {
...spawned,
cleanup: async () => ({
cleaned: false,
reason: "injected ownership failure",
}),
};
}
return spawned;
},
);
return {
provider,
service: new FirmwareAnalysisService(provider),
path,
output: join(root, "output"),
root,
launches,
};
};
/** Verify only the acquired process groups and private runtime directories. */
export const assertFirmwareCleanup = async (
launches: Awaited<ReturnType<typeof firmwareFixture>>["launches"],
): Promise<void> => {
for (const launch of launches) {
const pid = launch.pid;
if (pid !== undefined)
expect(() => process.kill(-pid, 0)).toThrowError(
expect.objectContaining({ code: "ESRCH" }),
);
if (launch.cwd !== undefined)
await expect(access(launch.cwd)).rejects.toMatchObject({
code: "ENOENT",
});
}
};