feat(ghidra): add private headless provider session
This commit is contained in:
+36
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { readFile, stat, writeFile } from "node:fs/promises";
|
||||
import { dirname, join } from "node:path";
|
||||
|
||||
const arguments_ = process.argv.slice(2);
|
||||
const descriptorPath = arguments_.at(-1);
|
||||
if (descriptorPath === undefined) process.exit(64);
|
||||
const descriptor = JSON.parse(await readFile(descriptorPath, "utf8"));
|
||||
const runtimeRoot = dirname(descriptorPath);
|
||||
await writeFile(
|
||||
join(runtimeRoot, "launch-capture.json"),
|
||||
`${JSON.stringify({
|
||||
arguments: arguments_,
|
||||
environment: {
|
||||
HOME: process.env.HOME,
|
||||
TMPDIR: process.env.TMPDIR,
|
||||
XDG_CACHE_HOME: process.env.XDG_CACHE_HOME,
|
||||
XDG_CONFIG_HOME: process.env.XDG_CONFIG_HOME,
|
||||
XDG_DATA_HOME: process.env.XDG_DATA_HOME,
|
||||
GHIDRA_HEADLESS_MAXMEM: process.env.GHIDRA_HEADLESS_MAXMEM,
|
||||
GHIDRA_HEADLESS_JAVA_OPTIONS: process.env.GHIDRA_HEADLESS_JAVA_OPTIONS,
|
||||
GHIDRA_JAVA_OPTIONS: process.env.GHIDRA_JAVA_OPTIONS,
|
||||
JAVA_TOOL_OPTIONS: process.env.JAVA_TOOL_OPTIONS,
|
||||
JDK_JAVA_OPTIONS: process.env.JDK_JAVA_OPTIONS,
|
||||
_JAVA_OPTIONS: process.env._JAVA_OPTIONS,
|
||||
JAVA_HOME: process.env.JAVA_HOME,
|
||||
PATH: process.env.PATH,
|
||||
REA_PROCESS_RUN_ID: process.env.REA_PROCESS_RUN_ID,
|
||||
},
|
||||
descriptor_mode: (await stat(descriptorPath)).mode & 0o777,
|
||||
descriptor_has_token: typeof descriptor.token === "string",
|
||||
})}\n`,
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
setInterval(() => undefined, 1_000);
|
||||
Vendored
+5
-1
@@ -62,6 +62,7 @@ interface FakeOptions {
|
||||
readonly webMcpChildLeavesScope?: boolean;
|
||||
readonly electronFileUrl?: string;
|
||||
readonly duplicateElectronInventory?: boolean;
|
||||
readonly urlShapedAllowedTitle?: boolean;
|
||||
}
|
||||
|
||||
/** Start a real HTTP/WebSocket fake at the same seams as a user-owned browser. */
|
||||
@@ -225,7 +226,10 @@ const targets = (
|
||||
{
|
||||
id: "allowed-page",
|
||||
type: "page",
|
||||
title: "Inspectable application",
|
||||
title:
|
||||
options.urlShapedAllowedTitle === true
|
||||
? `http://127.0.0.1:${String(port)}/app?startup=title-secret#fragment`
|
||||
: "Inspectable application",
|
||||
url: `http://127.0.0.1:${String(port)}/app?token=page-secret#fragment`,
|
||||
attached: false,
|
||||
...(options.omitTargetWebSocket === true
|
||||
|
||||
Vendored
+128
@@ -0,0 +1,128 @@
|
||||
import { rm } from "node:fs/promises";
|
||||
import { createServer } from "node:net";
|
||||
|
||||
const [
|
||||
socketPath,
|
||||
token,
|
||||
runId,
|
||||
providerVersion,
|
||||
profileDigest,
|
||||
mode = "success",
|
||||
] = process.argv.slice(2);
|
||||
|
||||
if (
|
||||
socketPath === undefined ||
|
||||
token === undefined ||
|
||||
runId === undefined ||
|
||||
providerVersion === undefined ||
|
||||
profileDigest === undefined
|
||||
) {
|
||||
process.exit(64);
|
||||
}
|
||||
|
||||
if (mode === "exit") process.exit(73);
|
||||
if (mode === "silent") setInterval(() => undefined, 1_000);
|
||||
else {
|
||||
await rm(socketPath, { force: true });
|
||||
const server = createServer((socket) => {
|
||||
socket.setEncoding("utf8");
|
||||
let buffer = "";
|
||||
let pings = 0;
|
||||
socket.on("data", (chunk) => {
|
||||
buffer += chunk;
|
||||
let newline = buffer.indexOf("\n");
|
||||
while (newline >= 0) {
|
||||
const line = buffer.slice(0, newline);
|
||||
buffer = buffer.slice(newline + 1);
|
||||
newline = buffer.indexOf("\n");
|
||||
const request = JSON.parse(line);
|
||||
if (request.token !== token) {
|
||||
socket.end(
|
||||
`${JSON.stringify({
|
||||
id: request.id,
|
||||
ok: false,
|
||||
error: { code: "auth", message: "Authentication failed" },
|
||||
})}\n`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if (request.method === "ping") {
|
||||
pings += 1;
|
||||
if (mode === "malformed") {
|
||||
socket.write("{invalid\n");
|
||||
continue;
|
||||
}
|
||||
if (mode === "contradictory") {
|
||||
socket.write(
|
||||
`${JSON.stringify({
|
||||
id: request.id,
|
||||
ok: false,
|
||||
result: { unexpected: true },
|
||||
error: { code: "failure", message: "Failed" },
|
||||
})}\n`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if (mode === "oversized_whitespace") {
|
||||
socket.write(`${" ".repeat(1024 * 1024 + 1)}\n`);
|
||||
continue;
|
||||
}
|
||||
if (mode === "future_id") {
|
||||
socket.write(
|
||||
`${JSON.stringify({ id: request.id + 10, ok: true, result: null })}\n`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if (mode === "hang_after_start" && pings > 1) continue;
|
||||
const result = sessionInfo({
|
||||
runId,
|
||||
providerVersion:
|
||||
mode === "wrong_identity" ? "0.0.0" : providerVersion,
|
||||
profileDigest,
|
||||
timedOut: mode === "analysis_timeout",
|
||||
});
|
||||
const response = `${JSON.stringify({ id: request.id, ok: true, result })}\n`;
|
||||
if (mode === "fragmented") {
|
||||
const split = Math.floor(response.length / 2);
|
||||
socket.write(response.slice(0, split));
|
||||
setTimeout(() => socket.write(response.slice(split)), 5);
|
||||
} else socket.write(response);
|
||||
continue;
|
||||
}
|
||||
if (request.method === "shutdown") {
|
||||
socket.end(
|
||||
`${JSON.stringify({
|
||||
id: request.id,
|
||||
ok: true,
|
||||
result: { shutdown: true, project_ephemeral: true },
|
||||
})}\n`,
|
||||
);
|
||||
server.close(() => process.exit(0));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
server.listen(socketPath);
|
||||
}
|
||||
|
||||
const sessionInfo = ({
|
||||
runId: sessionRunId,
|
||||
providerVersion: version,
|
||||
profileDigest: digest,
|
||||
timedOut,
|
||||
}) => ({
|
||||
name: "REA Ghidra bridge",
|
||||
bridge_version: 1,
|
||||
run_id: sessionRunId,
|
||||
profile_digest: digest,
|
||||
provider: { id: "ghidra", version },
|
||||
read_only: true,
|
||||
analysis_complete: !timedOut,
|
||||
analysis_timed_out: timedOut,
|
||||
capabilities: ["ping", "shutdown"],
|
||||
target: {
|
||||
name: "fixture",
|
||||
language_id: "x86:LE:64:default",
|
||||
compiler_spec_id: "gcc",
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user