feat(ghidra): launch bounded Windows headless sessions
Invoke analyzeHeadless.bat through an absolute cmd.exe with fixed interpreter switches, quoted arguments, delayed expansion disabled, and fail-closed metacharacter and length checks. Isolate Windows home and temporary coordinates, hide provider windows, and terminate the P0 launcher tree through bounded taskkill cleanup. Advertise explicit limitations that this preview does not provide Job Object ownership, DACL enforcement, reparse-point authority, or hostile-target safety.
This commit is contained in:
+2
@@ -0,0 +1,2 @@
|
||||
@echo off
|
||||
node "%~dp0captureGhidraLaunch.mjs" %*
|
||||
+3
@@ -14,7 +14,10 @@ await writeFile(
|
||||
arguments: arguments_,
|
||||
environment: {
|
||||
HOME: process.env.HOME,
|
||||
USERPROFILE: process.env.USERPROFILE,
|
||||
TMPDIR: process.env.TMPDIR,
|
||||
TEMP: process.env.TEMP,
|
||||
TMP: process.env.TMP,
|
||||
XDG_CACHE_HOME: process.env.XDG_CACHE_HOME,
|
||||
XDG_CONFIG_HOME: process.env.XDG_CONFIG_HOME,
|
||||
XDG_DATA_HOME: process.env.XDG_DATA_HOME,
|
||||
|
||||
@@ -7,15 +7,23 @@ import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
ghidraHeadlessArguments,
|
||||
ghidraHeadlessCommand,
|
||||
GhidraHeadlessLauncher,
|
||||
} from "../src/ghidra/GhidraLauncher.js";
|
||||
|
||||
const fixturePath = fileURLToPath(
|
||||
new URL("./fixtures/captureGhidraLaunch.mjs", import.meta.url),
|
||||
new URL(
|
||||
process.platform === "win32"
|
||||
? "./fixtures/captureGhidraLaunch.cmd"
|
||||
: "./fixtures/captureGhidraLaunch.mjs",
|
||||
import.meta.url,
|
||||
),
|
||||
);
|
||||
const roots: string[] = [];
|
||||
|
||||
beforeAll(() => chmod(fixturePath, 0o755));
|
||||
beforeAll(async () => {
|
||||
if (process.platform !== "win32") await chmod(fixturePath, 0o755);
|
||||
});
|
||||
afterEach(async () => {
|
||||
vi.unstubAllEnvs();
|
||||
await Promise.all(
|
||||
@@ -57,6 +65,48 @@ describe("Ghidra headless launcher", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("wraps the Windows batch launcher without enabling a Node shell", () => {
|
||||
const command = ghidraHeadlessCommand({
|
||||
platform: "win32",
|
||||
analyzeHeadlessPath:
|
||||
"C:\\Program Files\\Ghidra 12.1.2\\support\\analyzeHeadless.bat",
|
||||
arguments: [
|
||||
"C:\\REA Runtime\\project",
|
||||
"rea-project",
|
||||
"-import",
|
||||
"C:\\REA Runtime\\target.exe",
|
||||
],
|
||||
comSpec: "C:\\Windows\\System32\\cmd.exe",
|
||||
});
|
||||
|
||||
expect(command).toEqual({
|
||||
command: "C:\\Windows\\System32\\cmd.exe",
|
||||
arguments: [
|
||||
"/d",
|
||||
"/e:on",
|
||||
"/v:off",
|
||||
"/s",
|
||||
"/c",
|
||||
'""C:\\Program Files\\Ghidra 12.1.2\\support\\analyzeHeadless.bat" "C:\\REA Runtime\\project" "rea-project" "-import" "C:\\REA Runtime\\target.exe""',
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
"C:\\targets\\%TEMP%\\fixture.exe",
|
||||
"C:\\targets\\fixture & calc.exe",
|
||||
'C:\\targets\\fixture".exe',
|
||||
])("rejects a Windows command-interpreter path: %s", (targetPath) => {
|
||||
expect(() =>
|
||||
ghidraHeadlessCommand({
|
||||
platform: "win32",
|
||||
analyzeHeadlessPath: "C:\\Ghidra\\support\\analyzeHeadless.bat",
|
||||
arguments: ["-import", targetPath],
|
||||
comSpec: "C:\\Windows\\System32\\cmd.exe",
|
||||
}),
|
||||
).toThrow(/metacharacters/u);
|
||||
});
|
||||
|
||||
it("keeps authority in a private descriptor and isolates Ghidra state", async () => {
|
||||
vi.stubEnv("GHIDRA_JAVA_OPTIONS", "-javaagent:/unapproved/agent.jar");
|
||||
vi.stubEnv("JAVA_TOOL_OPTIONS", "-Duser.home=/unapproved/home");
|
||||
@@ -65,9 +115,11 @@ describe("Ghidra headless launcher", () => {
|
||||
const runtimeRoot = await mkdtemp(join(tmpdir(), "rea-launcher-test-"));
|
||||
roots.push(runtimeRoot);
|
||||
const token = "secret-token-that-must-not-leak";
|
||||
const javaHome =
|
||||
process.platform === "win32" ? "C:\\Java\\jdk-21" : "/opt/jdk-21";
|
||||
const launcher = new GhidraHeadlessLauncher({
|
||||
analyzeHeadlessPath: fixturePath,
|
||||
javaHome: "/opt/jdk-21",
|
||||
javaHome,
|
||||
bridgeScriptPath: "/package/bridge/ReaGhidraBridge.java",
|
||||
});
|
||||
const launched = await launcher.launch({
|
||||
@@ -91,10 +143,17 @@ describe("Ghidra headless launcher", () => {
|
||||
expect(encodedArguments).not.toContain(token);
|
||||
expect(encodedEnvironment).not.toContain(token);
|
||||
expect(capture).toMatchObject({
|
||||
descriptor_mode: 0o600,
|
||||
...(process.platform === "win32" ? {} : { descriptor_mode: 0o600 }),
|
||||
descriptor_has_token: true,
|
||||
environment: {
|
||||
HOME: join(runtimeRoot, "home"),
|
||||
...(process.platform === "win32"
|
||||
? {
|
||||
USERPROFILE: join(runtimeRoot, "home"),
|
||||
TEMP: join(runtimeRoot, "tmp"),
|
||||
TMP: join(runtimeRoot, "tmp"),
|
||||
}
|
||||
: {}),
|
||||
TMPDIR: join(runtimeRoot, "tmp"),
|
||||
XDG_CACHE_HOME: join(runtimeRoot, "cache"),
|
||||
XDG_CONFIG_HOME: join(runtimeRoot, "config"),
|
||||
@@ -104,17 +163,22 @@ describe("Ghidra headless launcher", () => {
|
||||
JAVA_TOOL_OPTIONS: "",
|
||||
JDK_JAVA_OPTIONS: "",
|
||||
_JAVA_OPTIONS: "",
|
||||
JAVA_HOME: "/opt/jdk-21",
|
||||
JAVA_HOME: javaHome,
|
||||
REA_PROCESS_RUN_ID: "d6fcbb66-e829-4ff6-a535-0035aec63139",
|
||||
},
|
||||
});
|
||||
expect(capture.environment.PATH).toMatch(/^\/opt\/jdk-21\/bin:/u);
|
||||
expect(capture.environment.PATH).toMatch(
|
||||
process.platform === "win32"
|
||||
? /^C:\\Java\\jdk-21\\bin;/u
|
||||
: /^\/opt\/jdk-21\/bin:/u,
|
||||
);
|
||||
expect(capture.environment.GHIDRA_HEADLESS_JAVA_OPTIONS).toContain(
|
||||
`-Duser.home=${join(runtimeRoot, "home")}`,
|
||||
);
|
||||
expect((await stat(join(runtimeRoot, "ownership.json"))).mode & 0o777).toBe(
|
||||
0o600,
|
||||
);
|
||||
if (process.platform !== "win32")
|
||||
expect(
|
||||
(await stat(join(runtimeRoot, "ownership.json"))).mode & 0o777,
|
||||
).toBe(0o600);
|
||||
|
||||
const cleaned = await launched.value.cleanup?.();
|
||||
expect(cleaned).toMatchObject({ cleaned: true });
|
||||
|
||||
@@ -144,6 +144,17 @@ describe("Ghidra provider", () => {
|
||||
managed: false,
|
||||
},
|
||||
});
|
||||
expect(ghidra.capabilities()).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
operation: "list_procedures",
|
||||
limitations: expect.arrayContaining([
|
||||
expect.stringContaining("bounded taskkill termination"),
|
||||
expect.stringContaining("private DACL enforcement"),
|
||||
]),
|
||||
}),
|
||||
]),
|
||||
);
|
||||
expect(
|
||||
ghidra.inspectTargetSupport({
|
||||
...nativeApplication,
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
cleanupOwnedProcessGroup,
|
||||
cleanupWindowsProcessTree,
|
||||
observeOwnedProcessGroup,
|
||||
parseProcessEnvironment,
|
||||
selectCapturedProcessGroupIds,
|
||||
type ProcessOwnershipHost,
|
||||
type WindowsProcessTreeHost,
|
||||
} from "../src/process/ProcessOwnership.js";
|
||||
|
||||
const ownership = {
|
||||
@@ -224,3 +226,39 @@ describe("owned process-group cleanup", () => {
|
||||
expect(signalGroup).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Windows P0 process-tree cleanup", () => {
|
||||
it("reports whether taskkill signaled or found an exited tree", async () => {
|
||||
const terminated: WindowsProcessTreeHost = {
|
||||
terminateTree: () => Promise.resolve("terminated"),
|
||||
};
|
||||
const missing: WindowsProcessTreeHost = {
|
||||
terminateTree: () => Promise.resolve("missing"),
|
||||
};
|
||||
|
||||
await expect(cleanupWindowsProcessTree(42, terminated)).resolves.toEqual({
|
||||
cleaned: true,
|
||||
signaled: true,
|
||||
});
|
||||
await expect(cleanupWindowsProcessTree(42, missing)).resolves.toEqual({
|
||||
cleaned: true,
|
||||
signaled: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps invalid identity and termination failures explicit", async () => {
|
||||
const failing: WindowsProcessTreeHost = {
|
||||
terminateTree: () => Promise.reject(new Error("taskkill failed")),
|
||||
};
|
||||
|
||||
await expect(cleanupWindowsProcessTree(0, failing)).resolves.toEqual({
|
||||
cleaned: false,
|
||||
reason: "Windows process-tree PID is invalid",
|
||||
});
|
||||
await expect(cleanupWindowsProcessTree(42, failing)).resolves.toEqual({
|
||||
cleaned: false,
|
||||
reason:
|
||||
"Windows P0 process-tree termination failed; Job Object ownership is unavailable",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user