feat(ghidra): add DOS MZ analysis and complete function extents (#556)
Validate DOS MZ load modules and import them with Ghidra's 16-bit real-mode profile. Preserve complete observed inclusive function body ranges through inventory, function analysis, Evidence and snapshots. Add a public source-generated DOS CLI/MCP verification lane and regressions for malformed headers, body membership and cache profiles. Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing) Assisted-by: Suika (gensokyo-skills:suika-scatter-gather-planning)
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { classifyArtifactContent } from "../../../src/application/ArtifactGraphConstruction.js";
|
||||
import { classifyRoot } from "../../../src/application/ArtifactInventory/classify.js";
|
||||
import { ARTIFACT_CLASSIFICATION_PREFIX_BYTES } from "../../../src/application/ArtifactInventory/hash.js";
|
||||
import { scanCanonicalArtifactInventory } from "../../../src/application/ArtifactInventory/scanCanonical.js";
|
||||
import { targetFormatSchema } from "../../../src/contracts/toolOutputSchemaPrimitives.js";
|
||||
import { artifactInventoryResultSchema } from "../../../src/domain/artifactGraph.js";
|
||||
import { createEvidence } from "../../../src/domain/evidence.js";
|
||||
import { jsonValueSchema } from "../../../src/domain/jsonValue.js";
|
||||
import { identifyRuntimes } from "../../../src/domain/runtimeIdentification.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
describe("MZ artifact classification", () => {
|
||||
it("rejects a Windows header pointer overlapping the DOS header", () => {
|
||||
expect(classifyArtifactContent("invalid.exe", peFixture(32))).toEqual({
|
||||
kind: "unknown",
|
||||
format: "unknown",
|
||||
});
|
||||
});
|
||||
|
||||
it("distinguishes DOS, observed PE, malformed MZ and unavailable prefix evidence", () => {
|
||||
const dos = dosFixture();
|
||||
expect(classifyArtifactContent("LEGACY.EXE", dos)).toEqual({
|
||||
kind: "executable",
|
||||
format: "dos-mz",
|
||||
});
|
||||
expect(
|
||||
classifyArtifactContent("LEGACY.EXE", dos.subarray(0, 16), dos.length),
|
||||
).toEqual({ kind: "unknown", format: "unknown" });
|
||||
expect(classifyArtifactContent("bad.exe", Buffer.from("MZbroken"))).toEqual(
|
||||
{ kind: "unknown", format: "unknown" },
|
||||
);
|
||||
expect(classifyArtifactContent("NATIVE.EXE", peFixture(128))).toMatchObject(
|
||||
{
|
||||
format: "pe",
|
||||
},
|
||||
);
|
||||
const distantPe = peFixture(ARTIFACT_CLASSIFICATION_PREFIX_BYTES + 64);
|
||||
expect(
|
||||
classifyArtifactContent(
|
||||
"NATIVE.EXE",
|
||||
distantPe.subarray(0, ARTIFACT_CLASSIFICATION_PREFIX_BYTES),
|
||||
distantPe.length,
|
||||
),
|
||||
).toEqual({ kind: "unknown", format: "unknown" });
|
||||
expect(classifyArtifactContent("NATIVE.EXE", distantPe).format).toBe("pe");
|
||||
const distantRelocations = dosWithDistantRelocations();
|
||||
expect(
|
||||
classifyArtifactContent("LEGACY.EXE", distantRelocations).format,
|
||||
).toBe("dos-mz");
|
||||
expect(
|
||||
classifyArtifactContent(
|
||||
"LEGACY.EXE",
|
||||
distantRelocations.subarray(0, ARTIFACT_CLASSIFICATION_PREFIX_BYTES),
|
||||
distantRelocations.length,
|
||||
).format,
|
||||
).toBe("unknown");
|
||||
expect(targetFormatSchema.parse("dos-mz")).toBe("dos-mz");
|
||||
});
|
||||
|
||||
it("keeps standalone and embedded DOS identities through inventory and runtime evidence", async () => {
|
||||
const directory = await createTestTempDirectory("rea-mz-inventory-");
|
||||
const dosPath = join(directory, "LEGACY.EXE");
|
||||
const pePath = join(directory, "NATIVE.EXE");
|
||||
await writeFile(dosPath, dosFixture());
|
||||
await writeFile(pePath, peFixture(128));
|
||||
expect(await classifyRoot(dosPath, false)).toBe("dos-mz");
|
||||
expect(await classifyRoot(pePath, false)).toBe("pe");
|
||||
const standalone = artifactInventoryResultSchema.parse(
|
||||
await scanCanonicalArtifactInventory(dosPath),
|
||||
);
|
||||
expect(standalone.manifest.root_format).toBe("dos-mz");
|
||||
expect(standalone.nodes).toContainEqual(
|
||||
expect.objectContaining({ kind: "executable", format: "dos-mz" }),
|
||||
);
|
||||
|
||||
const tree = join(directory, "tree");
|
||||
await mkdir(tree);
|
||||
await writeFile(join(tree, "LEGACY.EXE"), dosFixture());
|
||||
await writeFile(join(tree, "NATIVE.EXE"), peFixture(128));
|
||||
const inventory = artifactInventoryResultSchema.parse(
|
||||
await scanCanonicalArtifactInventory(tree),
|
||||
);
|
||||
expect(inventory.nodes.map(({ format }) => format)).toEqual(
|
||||
expect.arrayContaining(["dos-mz", "pe"]),
|
||||
);
|
||||
const evidence = createEvidence(
|
||||
{
|
||||
path: tree,
|
||||
sha256: inventory.manifest.root_sha256,
|
||||
format: "directory",
|
||||
},
|
||||
{ id: "inventory-fixture", name: "Inventory fixture", version: null },
|
||||
{
|
||||
operation: "inventory_artifact",
|
||||
parameters: {},
|
||||
result: jsonValueSchema.parse(inventory),
|
||||
},
|
||||
);
|
||||
const runtimes = identifyRuntimes({ inventory_evidence: [evidence] });
|
||||
expect(runtimes.runtimes).toContainEqual(
|
||||
expect.objectContaining({
|
||||
family: "native",
|
||||
inspection: "provider-selection-required",
|
||||
observations: expect.arrayContaining([
|
||||
expect.objectContaining({ path: "LEGACY.EXE", format: "dos-mz" }),
|
||||
expect.objectContaining({ path: "NATIVE.EXE", format: "pe" }),
|
||||
]),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
const dosFixture = (): Buffer => {
|
||||
const bytes = Buffer.alloc(512);
|
||||
bytes.write("MZ", 0, "ascii");
|
||||
bytes.writeUInt16LE(1, 4);
|
||||
bytes.writeUInt16LE(2, 8);
|
||||
bytes.writeUInt16LE(28, 24);
|
||||
return bytes;
|
||||
};
|
||||
|
||||
const peFixture = (offset: number): Buffer => {
|
||||
const bytes = Buffer.alloc(Math.max(512, offset + 24));
|
||||
bytes.write("MZ", 0, "ascii");
|
||||
bytes.writeUInt16LE(4, 8);
|
||||
bytes.writeUInt16LE(64, 24);
|
||||
bytes.writeUInt32LE(offset, 60);
|
||||
bytes.set([0x50, 0x45, 0, 0], offset);
|
||||
return bytes;
|
||||
};
|
||||
|
||||
const dosWithDistantRelocations = (): Buffer => {
|
||||
const bytes = Buffer.alloc(16_384);
|
||||
bytes.write("MZ", 0, "ascii");
|
||||
bytes.writeUInt16LE(32, 4);
|
||||
bytes.writeUInt16LE(1, 6);
|
||||
bytes.writeUInt16LE(768, 8);
|
||||
bytes.writeUInt16LE(ARTIFACT_CLASSIFICATION_PREFIX_BYTES + 2, 24);
|
||||
return bytes;
|
||||
};
|
||||
@@ -4,7 +4,11 @@ import { join } from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver.js";
|
||||
import { pe, thinMach } from "../../../src/domain/binaryTarget.fixture.js";
|
||||
import {
|
||||
dosMz,
|
||||
pe,
|
||||
thinMach,
|
||||
} from "../../../src/domain/binaryTarget.fixture.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
describe("binary target I/O", () => {
|
||||
@@ -158,3 +162,21 @@ describe("binary target I/O", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("DOS binary target I/O", () => {
|
||||
it("validates a complete DOS file beyond the initial metadata probe", async () => {
|
||||
const directory = await createTestTempDirectory("rea-dos-target-");
|
||||
const path = join(directory, "legacy.exe");
|
||||
const bytes = dosMz(8192);
|
||||
await writeFile(path, bytes);
|
||||
expect(await parseBinaryTarget(path)).toMatchObject({
|
||||
ok: true,
|
||||
value: { format: "dos-mz", architecture: "x86", kind: "executable" },
|
||||
});
|
||||
await writeFile(path, bytes.subarray(0, bytes.length - 1));
|
||||
expect(await parseBinaryTarget(path)).toMatchObject({
|
||||
ok: false,
|
||||
error: { message: "Cannot open artifact: truncated DOS MZ load module" },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -65,6 +65,27 @@ afterEach(async () => {
|
||||
});
|
||||
|
||||
describe("Ghidra headless launcher", () => {
|
||||
it("forces the admitted real-mode language and loader for DOS MZ", () => {
|
||||
const arguments_ = ghidraHeadlessArguments({
|
||||
projectRoot: "/tmp/project",
|
||||
targetPath: "/tmp/target",
|
||||
bridgeScriptPath: "/package/bridge/ReaGhidraBridge.java",
|
||||
descriptorPath: "/tmp/session.json",
|
||||
ghidraLogPath: "/tmp/ghidra.log",
|
||||
scriptLogPath: "/tmp/script.log",
|
||||
dosMz: true,
|
||||
});
|
||||
expect(arguments_.slice(4, 10)).toEqual([
|
||||
"-loader",
|
||||
"MzLoader",
|
||||
"-processor",
|
||||
"x86:LE:16:Real Mode",
|
||||
"-cspec",
|
||||
"default",
|
||||
]);
|
||||
expect(arguments_).toContain("-readOnly");
|
||||
expect(arguments_).toContain("-deleteProject");
|
||||
});
|
||||
it("builds a read-only import in deterministic order", () => {
|
||||
expect(
|
||||
ghidraHeadlessArguments({
|
||||
|
||||
@@ -21,6 +21,16 @@ afterEach(async () => {
|
||||
});
|
||||
|
||||
describe("Hopper analysis profiles", () => {
|
||||
it("does not map DOS MZ to the 32-bit Hopper loader", () => {
|
||||
expect(hopperLoaderArgsForTarget(target("dos-mz", "x86"))).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
_tag: "ProviderAdapterError",
|
||||
operation: "resolve_analysis_profile",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
[target("mach-o", "arm64"), ["-l", "Mach-O", "--aarch64"]],
|
||||
[target("elf", "x86_64"), ["-l", "ELF", "--intel-64"]],
|
||||
@@ -143,7 +153,7 @@ describe("Hopper analysis profiles", () => {
|
||||
});
|
||||
|
||||
const target = (
|
||||
format: "mach-o" | "elf" | "pe",
|
||||
format: "mach-o" | "elf" | "pe" | "dos-mz",
|
||||
architecture: "x86" | "x86_64" | "arm" | "arm64",
|
||||
): BinaryTarget =>
|
||||
format === "pe"
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/** Build a public, source-owned 16-bit DOS MZ fixture without a compiler or binary seed. */
|
||||
export function buildDosMzFixture() {
|
||||
const headerBytes = 64;
|
||||
const moduleBytes = 128;
|
||||
const bytes = Buffer.alloc(headerBytes + moduleBytes);
|
||||
const word = (offset, value) => bytes.writeUInt16LE(value, offset);
|
||||
word(0, 0x5a4d); // MZ, not a PE stub.
|
||||
word(2, bytes.length); // Bytes in the final 512-byte page.
|
||||
word(4, 1); // One page.
|
||||
word(6, 1); // One relocation.
|
||||
word(8, headerBytes / 16);
|
||||
word(10, 0);
|
||||
word(12, 0xffff);
|
||||
word(14, 8); // SS relative to load segment.
|
||||
word(16, 0x100); // SP.
|
||||
word(20, 0); // IP.
|
||||
word(22, 0); // CS relative to load segment.
|
||||
word(24, 0x1c); // Relocation table in the header.
|
||||
word(0x1c, 9); // Far-call segment word at module+9.
|
||||
word(0x1e, 0);
|
||||
// Entry: MOV AX,1234h; near CALL +20h; relocated far CALL 0004:0000;
|
||||
// Conditional JZ to a separated terminal block, else RET. A conditional edge
|
||||
// keeps the tail in entry rather than creating an unconditional tail-call thunk.
|
||||
// A RET after DOS exit bounds static analysis even without interrupt semantics.
|
||||
// REA never executes this code.
|
||||
bytes.set(
|
||||
[
|
||||
0xb8, 0x34, 0x12, 0xe8, 0x1a, 0x00, 0x9a, 0x00, 0x00, 0x04, 0x00, 0x74,
|
||||
0x53, 0xc3,
|
||||
],
|
||||
headerBytes,
|
||||
);
|
||||
bytes.set([0xb8, 0x00, 0x4c, 0xcd, 0x21, 0xc3], headerBytes + 0x60);
|
||||
bytes.set([0xfe, 0x00, 0x40, 0xc3], headerBytes + 0x20); // INC byte [BX+SI]; INC AX; RET.
|
||||
bytes.set([0x05, 0x02, 0x00, 0xcb], headerBytes + 0x40); // ADD AX,2; RETF.
|
||||
return {
|
||||
bytes,
|
||||
header_bytes: headerBytes,
|
||||
module_bytes: moduleBytes,
|
||||
load_segment: 0x1000,
|
||||
entry_offset: 0,
|
||||
near_offset: 0x20,
|
||||
far_offset: 0x40,
|
||||
near_call_offset: 3,
|
||||
far_call_offset: 6,
|
||||
relocation_offset: 9,
|
||||
entry_first_bytes: 14,
|
||||
entry_tail_offset: 0x60,
|
||||
entry_tail_bytes: 6,
|
||||
near_body_bytes: 4,
|
||||
far_body_bytes: 4,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user