feat(ghidra): configurable import language and analysis seeds (#1667)

* feat(ghidra): configurable import language and analysis seeds

REA_GHIDRA_LANGUAGE_ID and REA_GHIDRA_COMPILER_SPEC_ID set the import
language when the header cannot identify the compiler (for example,
Borland C++ PE images).

REA_GHIDRA_SEED_FILE seeds functions, code and labels before
auto-analysis, for code reached only through computed jumps or data.

Both are validated before launch, recorded in the analysis profile and
verified in the bridge handshake.

* fix(ghidra): parse language XML and preserve seed ordering

* test(ghidra): use explicit platform paths for seed launch checks

* fix(javascript): retain cleanup failure reason in error causes

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
sqmagellan
2026-10-11 09:57:38 +08:00
committed by GitHub
co-authored by morluto
parent c1efa6467d
commit af88184eee
27 changed files with 1827 additions and 20 deletions
+11
View File
@@ -4,6 +4,10 @@ import { isAbsolute } from "node:path";
import { ConfigurationError } from "../domain/configurationErrors.js";
import { err, ok, type Result } from "../domain/result.js";
import { analysisProviderSelectorSchema } from "../contracts/providerSelection.js";
import {
ghidraCompilerSpecIdSchema,
ghidraLanguageIdSchema,
} from "./ghidraLanguageOverride.js";
import { ghidraStartupTimeoutSchema } from "./ghidraStartupTimeout.js";
import { mcpInputSchemaProfileSchema } from "./mcpInputSchemaProfile.js";
import { mcpResponseBudgetSchema } from "./mcpResponseBudget.js";
@@ -33,6 +37,13 @@ const environmentSchema = z.object({
.refine(isAbsolute, "REA_GHIDRA_NATIVEAOT_JAR must be absolute")
.optional(),
REA_GHIDRA_STARTUP_TIMEOUT_MS: ghidraStartupTimeoutSchema,
REA_GHIDRA_LANGUAGE_ID: ghidraLanguageIdSchema.optional(),
REA_GHIDRA_COMPILER_SPEC_ID: ghidraCompilerSpecIdSchema.optional(),
REA_GHIDRA_SEED_FILE: z
.string()
.min(1)
.refine(isAbsolute, "REA_GHIDRA_SEED_FILE must be absolute")
.optional(),
REA_ILSPY_CMD_PATH: z
.string()
.min(1)
+28
View File
@@ -0,0 +1,28 @@
import { z } from "zod";
/**
* Explicit Ghidra import language for targets whose header cannot identify the
* producing compiler, such as Borland-built PE images.
*/
export interface GhidraLanguageOverride {
/** Ghidra language ID: `processor:endian:size:variant`. */
readonly languageId: string;
/** Ghidra compiler-spec ID; absent selects the language's default spec. */
readonly compilerSpecId?: string;
}
// Ghidra language IDs are four colon-separated fields; variants may contain
// spaces ("x86:LE:16:Real Mode") but never leading or trailing whitespace.
export const ghidraLanguageIdSchema = z
.string()
.regex(
/^[A-Za-z0-9_.+-]+:(?:LE|BE):[1-9]\d*:[A-Za-z0-9_.+-](?:[A-Za-z0-9_.+ -]*[A-Za-z0-9_.+-])?$/u,
"must be a Ghidra language ID such as x86:LE:32:default",
);
export const ghidraCompilerSpecIdSchema = z
.string()
.regex(
/^[A-Za-z0-9_.+-]+$/u,
"must be a Ghidra compiler-spec ID such as default or borlandcpp",
);
+29 -1
View File
@@ -2,7 +2,7 @@ import { accessSync, constants } from "node:fs";
import { join } from "node:path";
import { ConfigurationError } from "../domain/configurationErrors.js";
import { ok, type Result } from "../domain/result.js";
import { err, ok, type Result } from "../domain/result.js";
import { parseEnvironment } from "./environment.js";
import { homeDirectoryFromEnvironment } from "./homeDirectory.js";
import { parseStringArray, parseLoaderArgs } from "./parsers.js";
@@ -46,6 +46,21 @@ export const parseConfig = (
"REA_REFERENCE_SECRET_PATTERNS_JSON",
);
if (!secretPatterns.ok) return secretPatterns;
// Ghidra's analyzeHeadless accepts -cspec only together with -processor.
if (
env.REA_GHIDRA_COMPILER_SPEC_ID !== undefined &&
env.REA_GHIDRA_LANGUAGE_ID === undefined
)
return err(
new ConfigurationError("Invalid REA environment configuration", {
settings: [
{
setting: "REA_GHIDRA_COMPILER_SPEC_ID",
constraint: "requires REA_GHIDRA_LANGUAGE_ID",
},
],
}),
);
return ok({
...(env.REA_MCP_MAX_RESPONSE_BYTES === undefined
? {}
@@ -60,6 +75,19 @@ export const parseConfig = (
ghidraInstallDir: env.GHIDRA_INSTALL_DIR,
ghidraJavaHome: env.JAVA_HOME,
ghidraStartupTimeoutMs: env.REA_GHIDRA_STARTUP_TIMEOUT_MS,
...(env.REA_GHIDRA_LANGUAGE_ID === undefined
? {}
: {
ghidraLanguageOverride: {
languageId: env.REA_GHIDRA_LANGUAGE_ID,
...(env.REA_GHIDRA_COMPILER_SPEC_ID === undefined
? {}
: { compilerSpecId: env.REA_GHIDRA_COMPILER_SPEC_ID }),
},
}),
...(env.REA_GHIDRA_SEED_FILE === undefined
? {}
: { ghidraSeedFile: env.REA_GHIDRA_SEED_FILE }),
...(env.REA_GHIDRA_NATIVEAOT_JAR === undefined
? {}
: { ghidraNativeAotJar: env.REA_GHIDRA_NATIVEAOT_JAR }),
+3
View File
@@ -2,6 +2,7 @@ import type { LogLevel } from "../logger.js";
import type { ReferenceSourcePolicy } from "../domain/referenceSourcePolicy.js";
import type { AnalysisProviderSelector } from "../contracts/providerSelection.js";
import type { GhidraLanguageOverride } from "./ghidraLanguageOverride.js";
import type { McpInputSchemaProfile } from "./mcpInputSchemaProfile.js";
export interface AppConfig {
@@ -12,6 +13,8 @@ export interface AppConfig {
readonly ghidraInstallDir: string | undefined;
readonly ghidraJavaHome: string | undefined;
readonly ghidraStartupTimeoutMs: number;
readonly ghidraLanguageOverride?: GhidraLanguageOverride;
readonly ghidraSeedFile?: string;
readonly ghidraNativeAotJar?: string;
readonly ilspyCmdPath: string | undefined;
readonly hopperLauncherPath: string;