feat(ghidra): bind imports to admitted target bytes
Copy each selected target into the ephemeral Ghidra runtime and reject the snapshot unless its SHA-256 matches the provider-neutral target identity. Advance the Java bridge protocol to v4, require Ghidra's imported executable digest to match again, and include that digest in the authenticated session handshake and diagnostics. Preserve bounded streaming hashes and remove mismatched snapshots before launch.
This commit is contained in:
Vendored
+7
-2
@@ -7,6 +7,7 @@ const [
|
||||
runId,
|
||||
providerVersion,
|
||||
profileDigest,
|
||||
targetSha256,
|
||||
mode = "success",
|
||||
] = process.argv.slice(2);
|
||||
|
||||
@@ -15,7 +16,8 @@ if (
|
||||
token === undefined ||
|
||||
runId === undefined ||
|
||||
providerVersion === undefined ||
|
||||
profileDigest === undefined
|
||||
profileDigest === undefined ||
|
||||
targetSha256 === undefined
|
||||
) {
|
||||
process.exit(64);
|
||||
}
|
||||
@@ -79,6 +81,7 @@ else {
|
||||
providerVersion:
|
||||
mode === "wrong_identity" ? "0.0.0" : providerVersion,
|
||||
profileDigest,
|
||||
targetSha256,
|
||||
timedOut: mode === "analysis_timeout",
|
||||
});
|
||||
const response = `${JSON.stringify({ id: request.id, ok: true, result })}\n`;
|
||||
@@ -132,10 +135,11 @@ const sessionInfo = ({
|
||||
runId: sessionRunId,
|
||||
providerVersion: version,
|
||||
profileDigest: digest,
|
||||
targetSha256: sha256,
|
||||
timedOut,
|
||||
}) => ({
|
||||
name: "REA Ghidra bridge",
|
||||
bridge_version: 3,
|
||||
bridge_version: 4,
|
||||
run_id: sessionRunId,
|
||||
profile_digest: digest,
|
||||
provider: { id: "ghidra", version },
|
||||
@@ -170,6 +174,7 @@ const sessionInfo = ({
|
||||
compiler_spec_id: "gcc",
|
||||
image_base: "0x1000",
|
||||
default_address_space: "ram",
|
||||
sha256,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { spawn, type ChildProcess } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { getEventListeners } from "node:events";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { access, mkdir } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
@@ -22,6 +24,9 @@ const fixturePath = fileURLToPath(
|
||||
);
|
||||
const PROFILE_DIGEST = "a".repeat(64);
|
||||
const PROVIDER_VERSION = "12.1.2";
|
||||
const TARGET_SHA256 = createHash("sha256")
|
||||
.update(readFileSync(fixturePath))
|
||||
.digest("hex");
|
||||
|
||||
type FixtureMode =
|
||||
| "success"
|
||||
@@ -62,6 +67,7 @@ class FixtureLauncher implements GhidraLauncher {
|
||||
session.runId,
|
||||
session.providerVersion,
|
||||
session.profileDigest,
|
||||
session.targetSha256,
|
||||
this.mode,
|
||||
],
|
||||
{ stdio: ["ignore", "pipe", "pipe"] },
|
||||
@@ -88,7 +94,8 @@ const clientFor = (
|
||||
): GhidraClient => {
|
||||
const client = new GhidraClient({
|
||||
launcher,
|
||||
targetPath: "/tmp/fixture",
|
||||
targetPath: fixturePath,
|
||||
targetSha256: TARGET_SHA256,
|
||||
providerVersion: PROVIDER_VERSION,
|
||||
profileDigest: PROFILE_DIGEST,
|
||||
startupTimeoutMs: options.startupTimeoutMs ?? 1_000,
|
||||
@@ -382,7 +389,7 @@ describe("GhidraClient", () => {
|
||||
if (result.ok) return;
|
||||
|
||||
const encoded = JSON.stringify(result.error.diagnostics);
|
||||
expect(encoded).toContain("/tmp/fixture");
|
||||
expect(encoded).toContain(fixturePath);
|
||||
expect(encoded).toContain(PROFILE_DIGEST);
|
||||
expect(encoded).not.toContain(launcher.tokens[0] ?? "missing-token");
|
||||
expect(events).toContainEqual(
|
||||
|
||||
@@ -76,6 +76,7 @@ describe("Ghidra headless launcher", () => {
|
||||
token,
|
||||
runId: "d6fcbb66-e829-4ff6-a535-0035aec63139",
|
||||
targetPath: "/tmp/fixture",
|
||||
targetSha256: "b".repeat(64),
|
||||
providerVersion: "12.1.2",
|
||||
profileDigest: "a".repeat(64),
|
||||
});
|
||||
|
||||
@@ -31,7 +31,10 @@ describe("Ghidra MCP and shared CLI composition", () => {
|
||||
it("preserves provider evidence, composed parity, and capability routing", async () => {
|
||||
const calls: GhidraOperation[] = [];
|
||||
const factory: GhidraProviderClientFactory = (options) => ({
|
||||
start: () => Promise.resolve(ok(sessionInfo(options.profileDigest))),
|
||||
start: () =>
|
||||
Promise.resolve(
|
||||
ok(sessionInfo(options.profileDigest, options.targetSha256)),
|
||||
),
|
||||
callTool: (operation, input) => {
|
||||
calls.push(operation);
|
||||
return Promise.resolve(ok(resultFor(operation, input)));
|
||||
@@ -301,9 +304,9 @@ const installationHost = (): GhidraInstallationHost => ({
|
||||
}),
|
||||
});
|
||||
|
||||
const sessionInfo = (profileDigest: string) => ({
|
||||
const sessionInfo = (profileDigest: string, targetSha256: string) => ({
|
||||
name: "REA Ghidra bridge" as const,
|
||||
bridge_version: 3 as const,
|
||||
bridge_version: 4 as const,
|
||||
run_id: "11111111-1111-4111-8111-111111111111",
|
||||
profile_digest: profileDigest,
|
||||
provider: { id: "ghidra" as const, version: "12.1.2" },
|
||||
@@ -317,6 +320,7 @@ const sessionInfo = (profileDigest: string) => ({
|
||||
compiler_spec_id: "gcc",
|
||||
image_base: "0x400000",
|
||||
default_address_space: "ram",
|
||||
sha256: targetSha256,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -370,7 +370,7 @@ describe("Ghidra provider", () => {
|
||||
|
||||
const sessionInfo = () => ({
|
||||
name: "REA Ghidra bridge" as const,
|
||||
bridge_version: 3 as const,
|
||||
bridge_version: 4 as const,
|
||||
run_id: "11111111-1111-4111-8111-111111111111",
|
||||
profile_digest: "a".repeat(64),
|
||||
provider: { id: "ghidra" as const, version: "12.1.2" },
|
||||
@@ -384,6 +384,7 @@ const sessionInfo = () => ({
|
||||
compiler_spec_id: "gcc",
|
||||
image_base: "0x1000",
|
||||
default_address_space: "ram",
|
||||
sha256: "a".repeat(64),
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { access, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { createGhidraTargetSnapshot } from "../src/ghidra/GhidraTargetSnapshot.js";
|
||||
|
||||
const roots: string[] = [];
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(
|
||||
roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
|
||||
);
|
||||
});
|
||||
|
||||
describe("Ghidra target snapshot", () => {
|
||||
it("copies an exact digest-bound target into the private runtime", async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), "rea-ghidra-snapshot-"));
|
||||
roots.push(root);
|
||||
const source = join(root, "source.exe");
|
||||
const bytes = Buffer.from("native PE fixture");
|
||||
const sha256 = createHash("sha256").update(bytes).digest("hex");
|
||||
await writeFile(source, bytes);
|
||||
|
||||
const snapshot = await createGhidraTargetSnapshot(source, root, sha256);
|
||||
|
||||
expect(snapshot).toEqual({
|
||||
path: join(root, `target-${sha256.slice(0, 12)}.exe`),
|
||||
sha256,
|
||||
});
|
||||
await expect(readFile(snapshot.path)).resolves.toEqual(bytes);
|
||||
});
|
||||
|
||||
it("removes a snapshot whose digest differs from admission", async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), "rea-ghidra-snapshot-"));
|
||||
roots.push(root);
|
||||
const source = join(root, "source with unsafe extension.%PATH%");
|
||||
await writeFile(source, "changed target");
|
||||
const expected = "a".repeat(64);
|
||||
const snapshotPath = join(root, `target-${expected.slice(0, 12)}.bin`);
|
||||
|
||||
await expect(
|
||||
createGhidraTargetSnapshot(source, root, expected),
|
||||
).rejects.toThrow(/digest mismatch/u);
|
||||
await expect(access(snapshotPath)).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user