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:
N0zoM1z0
2026-07-17 03:05:19 +09:00
committed by N0zoM1z0
parent 86e44bef08
commit be19c3fd12
16 changed files with 181 additions and 11 deletions
+24 -1
View File
@@ -41,6 +41,7 @@ import {
parseGhidraSessionInfo,
type GhidraSessionInfo,
} from "./GhidraSessionValues.js";
import { createGhidraTargetSnapshot } from "./GhidraTargetSnapshot.js";
import {
attachGhidraSocket,
connectGhidraSocketOnce,
@@ -77,6 +78,7 @@ export class GhidraClient {
#launch: GhidraLaunch | undefined;
#process: ProviderProcessSupervisor | undefined;
#runtimeRoot: PrivateRuntimeRoot | undefined;
#snapshotPath: string | undefined;
#token: string | undefined;
#runId: string | undefined;
#nextId = 1;
@@ -232,6 +234,23 @@ export class GhidraClient {
}
if (deadline.signal.aborted) return this.#startupInterrupted(deadline);
const socketPath = join(this.#runtimeRoot.path, "bridge.sock");
try {
const snapshot = await createGhidraTargetSnapshot(
this.#options.targetPath,
this.#runtimeRoot.path,
this.#options.targetSha256,
);
this.#snapshotPath = snapshot.path;
} catch (cause: unknown) {
const failure = this.#failure(
"start",
"Ghidra target snapshot failed admission",
cause,
);
await this.#cleanup();
return err(failure);
}
if (deadline.signal.aborted) return this.#startupInterrupted(deadline);
this.#token = randomBytes(32).toString("hex");
this.#runId = randomUUID();
const launched = await this.#options.launcher
@@ -241,7 +260,8 @@ export class GhidraClient {
socketPath,
token: this.#token,
runId: this.#runId,
targetPath: this.#options.targetPath,
targetPath: this.#snapshotPath,
targetSha256: this.#options.targetSha256,
providerVersion: this.#options.providerVersion,
profileDigest: this.#options.profileDigest,
},
@@ -401,6 +421,7 @@ export class GhidraClient {
runId,
providerVersion: this.#options.providerVersion,
profileDigest: this.#options.profileDigest,
targetSha256: this.#options.targetSha256,
});
if (parsed.ok) return parsed;
return err(
@@ -477,6 +498,7 @@ export class GhidraClient {
const runtimeRoot = this.#runtimeRoot;
this.#runtimeRoot = undefined;
await runtimeRoot?.close();
this.#snapshotPath = undefined;
this.#token = undefined;
this.#runId = undefined;
this.#responseBuffer.reset();
@@ -542,6 +564,7 @@ export class GhidraClient {
const snapshot = this.#process?.snapshot() ?? this.#processSnapshot;
return createGhidraDiagnostics({
targetPath: this.#options.targetPath,
targetSha256: this.#options.targetSha256,
providerVersion: this.#options.providerVersion,
profileDigest: this.#options.profileDigest,
...(this.#runtimeRoot === undefined
+1
View File
@@ -33,6 +33,7 @@ export type GhidraDiagnostic =
export interface GhidraClientOptions {
readonly launcher: GhidraLauncher;
readonly targetPath: string;
readonly targetSha256: string;
readonly providerVersion: string;
readonly profileDigest: string;
readonly requestTimeoutMs?: number;
+1 -1
View File
@@ -17,4 +17,4 @@ export const GHIDRA_MAX_QUEUED_REQUESTS = 32;
/** Maximum encoded response line accepted from the Java bridge. */
export const GHIDRA_MAX_LINE_BYTES = 1024 * 1024;
/** Version of REA's Java bridge handshake and request protocol. */
export const GHIDRA_BRIDGE_VERSION = 3;
export const GHIDRA_BRIDGE_VERSION = 4;
+2
View File
@@ -5,6 +5,7 @@ import type { GhidraLaunch } from "./GhidraLauncher.js";
/** Inputs needed to project bounded diagnostics for one Ghidra runtime. */
export interface GhidraDiagnosticsOptions {
readonly targetPath: string;
readonly targetSha256: string;
readonly providerVersion: string;
readonly profileDigest: string;
readonly runtimeRoot?: string;
@@ -30,6 +31,7 @@ export const createGhidraDiagnostics = (
: value.replaceAll(options.token, "[REDACTED]");
return {
target_path: options.targetPath,
target_sha256: options.targetSha256,
provider_version: options.providerVersion,
profile_digest: options.profileDigest,
...(options.runtimeRoot === undefined
+2
View File
@@ -25,6 +25,7 @@ export interface GhidraLaunchSession {
readonly token: string;
readonly runId: string;
readonly targetPath: string;
readonly targetSha256: string;
readonly providerVersion: string;
readonly profileDigest: string;
}
@@ -89,6 +90,7 @@ export class GhidraHeadlessLauncher implements GhidraLauncher {
socket_path: session.socketPath,
token: session.token,
run_id: session.runId,
target_sha256: session.targetSha256,
provider_version: session.providerVersion,
profile_digest: session.profileDigest,
})}\n`,
+1
View File
@@ -356,6 +356,7 @@ export class GhidraProvider implements AnalysisProviderCandidate {
),
}),
targetPath: target.path,
targetSha256: target.sha256,
providerVersion: prerequisites.value.providerVersion,
profileDigest: committedProfile.digest,
logger: this.logger.child({ layer: "ghidra-bridge" }),
+3
View File
@@ -41,6 +41,7 @@ const sessionInfoSchema = z
compiler_spec_id: z.string().min(1),
image_base: z.string().regex(/^0x[0-9a-f]+$/u),
default_address_space: z.string().min(1),
sha256: z.string().regex(/^[a-f0-9]{64}$/u),
})
.strict(),
})
@@ -56,6 +57,7 @@ export const parseGhidraSessionInfo = (
readonly runId: string;
readonly providerVersion: string;
readonly profileDigest: string;
readonly targetSha256: string;
},
): Result<GhidraSessionInfo, Error> => {
const parsed = sessionInfoSchema.safeParse(value);
@@ -64,6 +66,7 @@ export const parseGhidraSessionInfo = (
parsed.data.run_id !== expected.runId ||
parsed.data.provider.version !== expected.providerVersion ||
parsed.data.profile_digest !== expected.profileDigest ||
parsed.data.target.sha256 !== expected.targetSha256 ||
new Set(parsed.data.capabilities).size !==
GHIDRA_SESSION_CAPABILITIES.length ||
GHIDRA_SESSION_CAPABILITIES.some(
+46
View File
@@ -0,0 +1,46 @@
import { createHash } from "node:crypto";
import { constants, createReadStream } from "node:fs";
import { chmod, copyFile, rm } from "node:fs/promises";
import { extname, join } from "node:path";
/** Immutable target copy admitted to one ephemeral Ghidra import. */
export interface GhidraTargetSnapshot {
readonly path: string;
readonly sha256: string;
}
/** Copy one target and require its bytes to match the parsed target identity. */
export const createGhidraTargetSnapshot = async (
sourcePath: string,
runtimeRoot: string,
expectedSha256: string,
): Promise<GhidraTargetSnapshot> => {
if (!/^[a-f0-9]{64}$/u.test(expectedSha256))
throw new TypeError("Ghidra target SHA-256 commitment is invalid");
const suffix = safeExtension(sourcePath);
const snapshotPath = join(
runtimeRoot,
`target-${expectedSha256.slice(0, 12)}${suffix}`,
);
try {
await copyFile(sourcePath, snapshotPath, constants.COPYFILE_EXCL);
await chmod(snapshotPath, 0o600);
const hash = createHash("sha256");
for await (const chunk of createReadStream(snapshotPath))
hash.update(chunk);
const observedSha256 = hash.digest("hex");
if (observedSha256 !== expectedSha256)
throw new Error(
`Ghidra target snapshot digest mismatch: expected ${expectedSha256}, observed ${observedSha256}`,
);
return { path: snapshotPath, sha256: observedSha256 };
} catch (cause: unknown) {
await rm(snapshotPath, { force: true }).catch(() => undefined);
throw cause;
}
};
const safeExtension = (path: string): string => {
const extension = extname(path);
return /^\.[A-Za-z0-9]{1,12}$/u.test(extension) ? extension : ".bin";
};