feat(ghidra): enable Windows x64 read-only analysis (#699)
* feat(ghidra): enable Windows x64 read-only analysis * test(ghidra): exercise native Windows runtime boundaries * test(ghidra): verify current Windows inventory and isolate ACL observer * fix(ghidra): align Windows admission and address P0 review
This commit is contained in:
@@ -5,6 +5,8 @@ import { fileURLToPath } from "node:url";
|
||||
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
import { createGhidraTestRuntime } from "../../../fixtures/ghidraRuntime.js";
|
||||
import type { PrivateRuntimeRoot } from "../../../../src/process/PrivateRuntimeRoot.js";
|
||||
|
||||
import {
|
||||
ghidraHeadlessArguments,
|
||||
@@ -21,6 +23,7 @@ const fixturePath = fileURLToPath(
|
||||
),
|
||||
);
|
||||
const roots: string[] = [];
|
||||
const runtimes: PrivateRuntimeRoot[] = [];
|
||||
|
||||
const expectIsolatedEnvironment = (
|
||||
environment: Record<string, string>,
|
||||
@@ -32,6 +35,8 @@ const expectIsolatedEnvironment = (
|
||||
...(process.platform === "win32"
|
||||
? {
|
||||
USERPROFILE: join(runtimeRoot, "home"),
|
||||
APPDATA: join(runtimeRoot, "config"),
|
||||
LOCALAPPDATA: join(runtimeRoot, "cache"),
|
||||
TEMP: join(runtimeRoot, "tmp"),
|
||||
TMP: join(runtimeRoot, "tmp"),
|
||||
}
|
||||
@@ -42,7 +47,10 @@ const expectIsolatedEnvironment = (
|
||||
XDG_DATA_HOME: join(runtimeRoot, "data"),
|
||||
GHIDRA_JAVA_OPTIONS: "",
|
||||
JAVA_TOOL_OPTIONS: "",
|
||||
JDK_JAVA_OPTIONS: "",
|
||||
JDK_JAVA_OPTIONS:
|
||||
process.platform === "win32"
|
||||
? `"-Duser.home=${join(runtimeRoot, "home")}" "-Djava.io.tmpdir=${join(runtimeRoot, "tmp")}" "-XX:-UsePerfData"`
|
||||
: "",
|
||||
_JAVA_OPTIONS: "",
|
||||
JAVA_HOME: javaHome,
|
||||
REA_PROCESS_RUN_ID: "d6fcbb66-e829-4ff6-a535-0035aec63139",
|
||||
@@ -59,6 +67,7 @@ beforeAll(async () => {
|
||||
});
|
||||
afterEach(async () => {
|
||||
vi.unstubAllEnvs();
|
||||
await Promise.all(runtimes.splice(0).map((runtime) => runtime.close()));
|
||||
await Promise.all(
|
||||
roots.splice(0).map((path) => rm(path, { recursive: true, force: true })),
|
||||
);
|
||||
@@ -191,8 +200,11 @@ describe("Ghidra headless launcher", () => {
|
||||
vi.stubEnv("JAVA_TOOL_OPTIONS", "-Duser.home=/unapproved/home");
|
||||
vi.stubEnv("JDK_JAVA_OPTIONS", "-XX:MaxRAMPercentage=99");
|
||||
vi.stubEnv("_JAVA_OPTIONS", "-Xmx99G");
|
||||
const runtimeRoot = await createTestTempDirectory("rea-launcher-test-");
|
||||
roots.push(runtimeRoot);
|
||||
const parent = await createTestTempDirectory("rea-launcher-test-");
|
||||
roots.push(parent);
|
||||
const runtime = await createGhidraTestRuntime(parent);
|
||||
runtimes.push(runtime);
|
||||
const runtimeRoot = runtime.path;
|
||||
const token = "secret-token-that-must-not-leak";
|
||||
const javaHome =
|
||||
process.platform === "win32" ? "C:\\Java\\jdk-21" : "/opt/jdk-21";
|
||||
@@ -226,16 +238,11 @@ describe("Ghidra headless launcher", () => {
|
||||
descriptor_has_token: true,
|
||||
});
|
||||
expectIsolatedEnvironment(capture.environment, runtimeRoot, javaHome);
|
||||
expect(capture.environment.GHIDRA_HEADLESS_JAVA_OPTIONS).toContain(
|
||||
`-Duser.home=${join(runtimeRoot, "home")}`,
|
||||
);
|
||||
expect(capture.environment.GHIDRA_HEADLESS_JAVA_OPTIONS).toBe(
|
||||
`-Duser.home=${join(runtimeRoot, "home")} -Djava.io.tmpdir=${join(runtimeRoot, "tmp")}`,
|
||||
process.platform === "win32"
|
||||
? ""
|
||||
: `-Duser.home=${join(runtimeRoot, "home")} -Djava.io.tmpdir=${join(runtimeRoot, "tmp")}`,
|
||||
);
|
||||
if (process.platform === "win32")
|
||||
expect(capture.environment.GHIDRA_HEADLESS_JAVA_OPTIONS).not.toContain(
|
||||
'"',
|
||||
);
|
||||
if (process.platform !== "win32")
|
||||
expect(
|
||||
(await stat(join(runtimeRoot, "ownership.json"))).mode & 0o777,
|
||||
|
||||
@@ -5,18 +5,38 @@ import { join } from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
import { createGhidraTestRuntime } from "../../../fixtures/ghidraRuntime.js";
|
||||
import type { PrivateRuntimeRoot } from "../../../../src/process/PrivateRuntimeRoot.js";
|
||||
|
||||
import { createGhidraTargetSnapshot } from "../../../../src/ghidra/GhidraTargetSnapshot.js";
|
||||
|
||||
const roots: string[] = [];
|
||||
const runtimes: PrivateRuntimeRoot[] = [];
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(runtimes.splice(0).map((runtime) => runtime.close()));
|
||||
await Promise.all(
|
||||
roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
|
||||
);
|
||||
});
|
||||
|
||||
describe("Ghidra target snapshot", () => {
|
||||
it("uses the selected installation platform instead of the ambient host", async () => {
|
||||
const root = await createTestTempDirectory("rea-ghidra-platform-");
|
||||
roots.push(root);
|
||||
const source = join(root, "source.exe");
|
||||
const bytes = Buffer.from("platform fixture");
|
||||
const sha256 = createHash("sha256").update(bytes).digest("hex");
|
||||
await writeFile(source, bytes);
|
||||
const snapshot = await createGhidraTargetSnapshot(source, root, sha256, {
|
||||
platform: "linux",
|
||||
});
|
||||
expect(snapshot.admission).toBeUndefined();
|
||||
await expect(readFile(snapshot.path)).resolves.toEqual(bytes);
|
||||
await expect(
|
||||
createGhidraTargetSnapshot(source, root, sha256, { platform: "win32" }),
|
||||
).rejects.toThrow(/No native private runtime owns/u);
|
||||
});
|
||||
it("copies an exact digest-bound target into the private runtime", async () => {
|
||||
const root = await createTestTempDirectory("rea-ghidra-snapshot-");
|
||||
roots.push(root);
|
||||
@@ -24,27 +44,39 @@ describe("Ghidra target snapshot", () => {
|
||||
const bytes = Buffer.from("native PE fixture");
|
||||
const sha256 = createHash("sha256").update(bytes).digest("hex");
|
||||
await writeFile(source, bytes);
|
||||
const runtime = await createGhidraTestRuntime(root);
|
||||
runtimes.push(runtime);
|
||||
|
||||
const snapshot = await createGhidraTargetSnapshot(source, root, sha256);
|
||||
const snapshot = await createGhidraTargetSnapshot(
|
||||
source,
|
||||
runtime.path,
|
||||
sha256,
|
||||
);
|
||||
|
||||
expect(snapshot).toEqual({
|
||||
path: join(root, `target-${sha256.slice(0, 12)}.exe`),
|
||||
expect(snapshot).toMatchObject({
|
||||
path: join(runtime.path, `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 () => {
|
||||
it("rejects a mismatched digest and removes the failed runtime snapshot", async () => {
|
||||
const root = await createTestTempDirectory("rea-ghidra-snapshot-");
|
||||
roots.push(root);
|
||||
const source = join(root, "source with unsafe extension.%PATH%");
|
||||
await writeFile(source, "changed target");
|
||||
const runtime = await createGhidraTestRuntime(root);
|
||||
runtimes.push(runtime);
|
||||
const expected = "a".repeat(64);
|
||||
const snapshotPath = join(root, `target-${expected.slice(0, 12)}.bin`);
|
||||
const snapshotPath = join(
|
||||
runtime.path,
|
||||
`target-${expected.slice(0, 12)}.bin`,
|
||||
);
|
||||
|
||||
await expect(
|
||||
createGhidraTargetSnapshot(source, root, expected),
|
||||
createGhidraTargetSnapshot(source, runtime.path, expected),
|
||||
).rejects.toThrow(/digest mismatch/u);
|
||||
await runtime.close();
|
||||
await expect(access(snapshotPath)).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
|
||||
@@ -4,12 +4,19 @@ import { join } from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
import {
|
||||
createGhidraTestRuntime,
|
||||
publishGhidraTestEndpoint,
|
||||
} from "../../../fixtures/ghidraRuntime.js";
|
||||
import type { PrivateRuntimeRoot } from "../../../../src/process/PrivateRuntimeRoot.js";
|
||||
|
||||
import { observeGhidraEndpoint } from "../../../../src/ghidra/GhidraTransport.js";
|
||||
|
||||
const roots: string[] = [];
|
||||
const runtimes: PrivateRuntimeRoot[] = [];
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(runtimes.splice(0).map((runtime) => runtime.close()));
|
||||
await Promise.all(
|
||||
roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
|
||||
);
|
||||
@@ -33,9 +40,10 @@ describe("Ghidra local transport", () => {
|
||||
it("accepts only an exact IPv4 loopback endpoint record", async () => {
|
||||
const root = await createTestTempDirectory("rea-ghidra-transport-");
|
||||
roots.push(root);
|
||||
const path = join(root, "bridge-endpoint.json");
|
||||
await writeFile(
|
||||
path,
|
||||
const runtime = await createGhidraTestRuntime(root);
|
||||
runtimes.push(runtime);
|
||||
const path = await publishGhidraTestEndpoint(
|
||||
runtime,
|
||||
`${JSON.stringify({ host: "127.0.0.1", port: 49152 })}\n`,
|
||||
);
|
||||
|
||||
@@ -59,8 +67,12 @@ describe("Ghidra local transport", () => {
|
||||
async (value) => {
|
||||
const root = await createTestTempDirectory("rea-ghidra-transport-");
|
||||
roots.push(root);
|
||||
const path = join(root, "bridge-endpoint.json");
|
||||
await writeFile(path, JSON.stringify(value));
|
||||
const runtime = await createGhidraTestRuntime(root);
|
||||
runtimes.push(runtime);
|
||||
const path = await publishGhidraTestEndpoint(
|
||||
runtime,
|
||||
JSON.stringify(value),
|
||||
);
|
||||
|
||||
await expect(
|
||||
observeGhidraEndpoint({
|
||||
|
||||
Reference in New Issue
Block a user