Validate DOS MZ load modules and import them with Ghidra's 16-bit real-mode profile. Preserve complete observed inclusive function body ranges through inventory, function analysis, Evidence and snapshots. Add a public source-generated DOS CLI/MCP verification lane and regressions for malformed headers, body membership and cache profiles. Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing) Assisted-by: Suika (gensokyo-skills:suika-scatter-gather-planning)
448 lines
15 KiB
TypeScript
448 lines
15 KiB
TypeScript
import { fileURLToPath } from "node:url";
|
|
|
|
import {
|
|
createAnalysisExecution,
|
|
type AnalysisClient,
|
|
type AnalysisClientContext,
|
|
type AnalysisOperation,
|
|
type AnalysisProfileResolutionOptions,
|
|
type AnalysisProviderCandidate,
|
|
type CapabilityDescriptor,
|
|
type ProviderAvailability,
|
|
type ProviderIdentity,
|
|
type ProviderTargetSupport,
|
|
} from "../application/AnalysisProvider.js";
|
|
import type { AppConfig } from "../config.js";
|
|
import type { AnalysisProfileCommitment } from "../domain/analysisProfile.js";
|
|
import type { BinaryTarget } from "../domain/binaryTarget.js";
|
|
import { jsonObjectSchema } from "../domain/jsonValue.js";
|
|
import {
|
|
AnalysisCancelledError,
|
|
AnalysisCapabilityUnavailableError,
|
|
AnalysisInputError,
|
|
type AnalysisError,
|
|
AnalysisTimeoutError,
|
|
ProviderAdapterError,
|
|
} from "../domain/errors.js";
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
import type { Logger } from "../logger.js";
|
|
import { GhidraClient } from "./GhidraClient.js";
|
|
import type { GhidraClientOptions } from "./GhidraClientTypes.js";
|
|
import { GHIDRA_STARTUP_TIMEOUT_MS } from "./GhidraDefaults.js";
|
|
import {
|
|
isGhidraFunctionOperation,
|
|
parseGhidraFunctionInput,
|
|
parseGhidraFunctionResult,
|
|
} from "./GhidraFunctionValues.js";
|
|
import {
|
|
isGhidraInventoryOperation,
|
|
parseGhidraInventoryInput,
|
|
parseGhidraInventoryResult,
|
|
} from "./GhidraInventoryValues.js";
|
|
import {
|
|
ghidraInstallationDiagnostics,
|
|
inspectGhidraInstallation,
|
|
type GhidraInstallationHost,
|
|
type GhidraInstallationInspection,
|
|
} from "./GhidraInstallation.js";
|
|
import { GhidraHeadlessLauncher } from "./GhidraLauncher.js";
|
|
import { resolveGhidraAnalysisProfile } from "./GhidraAnalysisProfile.js";
|
|
import type { GhidraSessionError } from "./GhidraSessionError.js";
|
|
import {
|
|
CAPABILITIES,
|
|
WINDOWS_P0_CAPABILITIES,
|
|
GHIDRA_PROVIDER_IDENTITY,
|
|
GHIDRA_PROVIDER_TOOL_CONTRACTS,
|
|
healthLimitations,
|
|
windowsP0Limitations,
|
|
limitationsFor,
|
|
} from "./GhidraProviderCapabilities.js";
|
|
import {
|
|
WINDOWS_NATIVE_AUTHORITY_UNAVAILABLE_REASON,
|
|
hasWindowsNativeAuthority,
|
|
windowsNativeCapabilities,
|
|
} from "../process/WindowsAuthority.js";
|
|
|
|
export { GHIDRA_PROVIDER_IDENTITY, GHIDRA_PROVIDER_TOOL_CONTRACTS };
|
|
|
|
const SUPPORTED_ARCHITECTURES = new Set(["x86", "x86_64", "arm", "arm64"]);
|
|
|
|
/** Production seam for exercising provider projection without a real process. */
|
|
export type GhidraProviderClientFactory = (
|
|
options: GhidraClientOptions,
|
|
) => Pick<GhidraClient, "start" | "callTool" | "close"> &
|
|
Partial<Pick<GhidraClient, "runtimeLineage">>;
|
|
|
|
/** Ghidra candidate backed by an isolated read-only headless import. */
|
|
export class GhidraProvider implements AnalysisProviderCandidate {
|
|
#installation: GhidraInstallationInspection | undefined;
|
|
|
|
constructor(
|
|
private readonly config: AppConfig,
|
|
private readonly logger: Logger,
|
|
private readonly installationHost?: GhidraInstallationHost,
|
|
private readonly clientFactory: GhidraProviderClientFactory = (options) =>
|
|
new GhidraClient(options),
|
|
) {}
|
|
|
|
identity(): ProviderIdentity {
|
|
return GHIDRA_PROVIDER_IDENTITY;
|
|
}
|
|
|
|
capabilities(): readonly CapabilityDescriptor[] {
|
|
return (this.installationHost?.platform ?? process.platform) === "win32"
|
|
? WINDOWS_P0_CAPABILITIES
|
|
: CAPABILITIES;
|
|
}
|
|
|
|
inspectAvailability(): ProviderAvailability {
|
|
const installation = this.#inspectInstallation();
|
|
const diagnostics = ghidraInstallationDiagnostics(installation);
|
|
if (
|
|
installation.status === "available" &&
|
|
installation.platform === "win32" &&
|
|
!hasWindowsNativeAuthority(installation.platform)
|
|
)
|
|
return {
|
|
status: "unavailable",
|
|
code: "unsupported_host",
|
|
reason: WINDOWS_NATIVE_AUTHORITY_UNAVAILABLE_REASON,
|
|
diagnostics: {
|
|
...diagnostics,
|
|
windows_security: jsonObjectSchema.parse(
|
|
windowsNativeCapabilities(installation.platform),
|
|
),
|
|
},
|
|
};
|
|
return installation.status === "available"
|
|
? {
|
|
status: "available",
|
|
code: null,
|
|
reason: null,
|
|
diagnostics,
|
|
}
|
|
: {
|
|
status: "unavailable",
|
|
code: installation.rejection.code,
|
|
reason: installation.rejection.reason,
|
|
diagnostics,
|
|
};
|
|
}
|
|
|
|
inspectTargetSupport(target: BinaryTarget): ProviderTargetSupport {
|
|
const hostPlatform = this.installationHost?.platform ?? process.platform;
|
|
const diagnostics = {
|
|
host_platform: hostPlatform,
|
|
target_kind: target.kind,
|
|
target_format: target.format,
|
|
architecture: target.architecture ?? null,
|
|
executable_role: target.executableRole ?? null,
|
|
managed: target.managed ?? null,
|
|
};
|
|
if (target.kind !== "executable")
|
|
return {
|
|
status: "unsupported",
|
|
code: "target_kind_unsupported",
|
|
reason: `Ghidra v1 imports executable targets, not ${target.kind} targets.`,
|
|
diagnostics,
|
|
};
|
|
if (hostPlatform === "win32")
|
|
return inspectWindowsP0TargetSupport(target, diagnostics);
|
|
if (!SUPPORTED_ARCHITECTURES.has(target.architecture))
|
|
return {
|
|
status: "unsupported",
|
|
code: "architecture_unsupported",
|
|
reason:
|
|
"Ghidra v1 requires a concrete x86, x86_64, arm, or arm64 target architecture.",
|
|
diagnostics,
|
|
};
|
|
return {
|
|
status: "supported",
|
|
code: null,
|
|
reason: null,
|
|
diagnostics,
|
|
};
|
|
}
|
|
|
|
resolveAnalysisProfile(
|
|
target: BinaryTarget,
|
|
options?: AnalysisProfileResolutionOptions,
|
|
) {
|
|
return resolveGhidraAnalysisProfile(
|
|
target,
|
|
GHIDRA_PROVIDER_IDENTITY,
|
|
this.#inspectInstallation(),
|
|
options?.signal,
|
|
);
|
|
}
|
|
|
|
createClient(
|
|
target: BinaryTarget,
|
|
profile?: AnalysisProfileCommitment,
|
|
context?: AnalysisClientContext,
|
|
): AnalysisClient {
|
|
const installation = this.#inspectInstallation();
|
|
const prerequisites = ghidraClientPrerequisites(
|
|
target,
|
|
profile,
|
|
installation,
|
|
);
|
|
if (!prerequisites.ok) return unavailableClient(prerequisites.error);
|
|
const committedProfile = prerequisites.value.profile;
|
|
const targetLimitations =
|
|
target.format === "dos-mz"
|
|
? [
|
|
"DOS MZ uses 16-bit x86 real mode with the Ghidra load segment 0x1000. Returned addresses are linear byte coordinates; they do not identify a unique segment:offset alias.",
|
|
"Static DOS analysis does not emulate BIOS, DOS interrupts, device ports, or self-modifying unpacking code. Packed targets require a separately identified unpacked artifact for original-program analysis; appended overlays are not the initialized load module.",
|
|
]
|
|
: [];
|
|
const providerLimitations =
|
|
installation.platform === "win32"
|
|
? windowsP0Limitations
|
|
: installation.platform === "darwin"
|
|
? [
|
|
"macOS sessions require a matching executable Ghidra native decompiler; REA checks for it but does not build native components or change Gatekeeper quarantine state.",
|
|
]
|
|
: [];
|
|
const client = this.clientFactory({
|
|
launcher: new GhidraHeadlessLauncher({
|
|
analyzeHeadlessPath: prerequisites.value.analyzeHeadlessPath,
|
|
...(this.config.ghidraJavaHome === undefined
|
|
? {}
|
|
: { javaHome: this.config.ghidraJavaHome }),
|
|
bridgeScriptPath: fileURLToPath(
|
|
new URL("../../bridge/ghidra/ReaGhidraBridge.java", import.meta.url),
|
|
),
|
|
...(target.format === "dos-mz" ? { dosMz: true } : {}),
|
|
platform: installation.platform,
|
|
}),
|
|
targetPath: target.path,
|
|
targetSha256: target.sha256,
|
|
transport:
|
|
installation.platform === "win32"
|
|
? "authenticated-loopback-tcp"
|
|
: "unix-socket",
|
|
providerVersion: prerequisites.value.providerVersion,
|
|
profileDigest: committedProfile.digest,
|
|
...(target.format === "dos-mz"
|
|
? {
|
|
expectedLanguageId: "x86:LE:16:Real Mode",
|
|
expectedCompilerSpecId: "default",
|
|
}
|
|
: {}),
|
|
...(context === undefined ? {} : { runId: context.runId }),
|
|
logger: this.logger.child({ layer: "ghidra-bridge" }),
|
|
});
|
|
return {
|
|
execute: async (operation, parameters, options) => {
|
|
if (
|
|
operation !== "health" &&
|
|
!isGhidraInventoryOperation(operation) &&
|
|
!isGhidraFunctionOperation(operation)
|
|
)
|
|
return err(
|
|
new AnalysisCapabilityUnavailableError(
|
|
GHIDRA_PROVIDER_IDENTITY.id,
|
|
operation,
|
|
"The Ghidra adapter does not declare this operation.",
|
|
),
|
|
);
|
|
if (operation === "health") {
|
|
const started = await client.start(options?.signal);
|
|
if (!started.ok)
|
|
return err(projectSessionError(operation, started.error));
|
|
return ok(
|
|
createAnalysisExecution(started.value, committedProfile.provider, {
|
|
analysisProfile: committedProfile,
|
|
limitations: [
|
|
...healthLimitations,
|
|
...providerLimitations,
|
|
...targetLimitations,
|
|
],
|
|
}),
|
|
);
|
|
}
|
|
const input = isGhidraFunctionOperation(operation)
|
|
? parseGhidraFunctionInput(operation, parameters)
|
|
: parseGhidraInventoryInput(operation, parameters);
|
|
if (!input.ok) return input;
|
|
const called = await client.callTool(
|
|
operation,
|
|
input.value,
|
|
options?.signal === undefined ? {} : { signal: options.signal },
|
|
);
|
|
if (!called.ok)
|
|
return err(projectSessionError(operation, called.error));
|
|
const result = isGhidraFunctionOperation(operation)
|
|
? parseGhidraFunctionResult(operation, called.value)
|
|
: parseGhidraInventoryResult(operation, called.value);
|
|
if (!result.ok) return result;
|
|
return ok(
|
|
createAnalysisExecution(result.value, committedProfile.provider, {
|
|
rawResult: called.value,
|
|
analysisProfile: committedProfile,
|
|
limitations: [
|
|
...limitationsFor(operation),
|
|
...providerLimitations,
|
|
...targetLimitations,
|
|
],
|
|
}),
|
|
);
|
|
},
|
|
runtimeLineageSnapshots: () => {
|
|
const observation = client.runtimeLineage?.() ?? null;
|
|
return observation === null
|
|
? []
|
|
: [{ provider: committedProfile.provider, observation }];
|
|
},
|
|
close: () => client.close(),
|
|
};
|
|
}
|
|
|
|
#inspectInstallation(): GhidraInstallationInspection {
|
|
const options = {
|
|
...(this.config.ghidraInstallDir === undefined
|
|
? {}
|
|
: { installDir: this.config.ghidraInstallDir }),
|
|
...(this.config.ghidraJavaHome === undefined
|
|
? {}
|
|
: { javaHome: this.config.ghidraJavaHome }),
|
|
...(this.installationHost?.platform === undefined
|
|
? {}
|
|
: { platform: this.installationHost.platform }),
|
|
...(this.installationHost?.architecture === undefined
|
|
? {}
|
|
: { architecture: this.installationHost.architecture }),
|
|
};
|
|
this.#installation ??=
|
|
this.installationHost === undefined
|
|
? inspectGhidraInstallation(options)
|
|
: inspectGhidraInstallation(options, this.installationHost);
|
|
return this.#installation;
|
|
}
|
|
}
|
|
|
|
const inspectWindowsP0TargetSupport = (
|
|
target: BinaryTarget,
|
|
diagnostics: Readonly<Record<string, string | boolean | null>>,
|
|
): ProviderTargetSupport => {
|
|
if (target.format !== "pe")
|
|
return {
|
|
status: "unsupported",
|
|
code: "target_format_unsupported",
|
|
reason: "Windows Ghidra P0 accepts PE targets only.",
|
|
diagnostics,
|
|
};
|
|
if (target.architecture !== "x86_64")
|
|
return {
|
|
status: "unsupported",
|
|
code: "architecture_unsupported",
|
|
reason: "Windows Ghidra P0 accepts x86-64 PE targets only.",
|
|
diagnostics,
|
|
};
|
|
if (target.executableRole !== "application")
|
|
return {
|
|
status: "unsupported",
|
|
code: "target_role_unsupported",
|
|
reason:
|
|
"Windows Ghidra P0 accepts PE applications, not DLL or non-executable images.",
|
|
diagnostics,
|
|
};
|
|
if (target.managed !== false)
|
|
return {
|
|
status: "unsupported",
|
|
code: "managed_target_unsupported",
|
|
reason:
|
|
"Windows Ghidra P0 accepts native PE applications; managed or unclassified PE targets are unsupported.",
|
|
diagnostics,
|
|
};
|
|
return {
|
|
status: "supported",
|
|
code: null,
|
|
reason: null,
|
|
diagnostics,
|
|
};
|
|
};
|
|
|
|
interface GhidraClientCoordinates {
|
|
readonly analyzeHeadlessPath: string;
|
|
readonly providerVersion: string;
|
|
readonly profile: AnalysisProfileCommitment;
|
|
}
|
|
|
|
const ghidraClientPrerequisites = (
|
|
target: BinaryTarget,
|
|
profile: AnalysisProfileCommitment | undefined,
|
|
installation: GhidraInstallationInspection,
|
|
): Result<GhidraClientCoordinates, AnalysisError> => {
|
|
if (target.kind !== "executable")
|
|
return err(
|
|
new AnalysisCapabilityUnavailableError(
|
|
"ghidra",
|
|
"health",
|
|
`Ghidra cannot import ${target.kind} targets through this adapter.`,
|
|
),
|
|
);
|
|
if (installation.status === "unavailable")
|
|
return err(
|
|
new ProviderAdapterError("ghidra", "health", {
|
|
diagnostics: ghidraInstallationDiagnostics(installation),
|
|
}),
|
|
);
|
|
if (
|
|
installation.platform === "win32" &&
|
|
!hasWindowsNativeAuthority(installation.platform)
|
|
)
|
|
return err(
|
|
new AnalysisCapabilityUnavailableError(
|
|
"ghidra",
|
|
"health",
|
|
WINDOWS_NATIVE_AUTHORITY_UNAVAILABLE_REASON,
|
|
),
|
|
);
|
|
if (profile === undefined || profile.provider.id !== "ghidra")
|
|
return err(new ProviderAdapterError("ghidra", "health"));
|
|
return ok({
|
|
analyzeHeadlessPath: installation.analyzeHeadlessPath,
|
|
providerVersion: installation.providerVersion,
|
|
profile,
|
|
});
|
|
};
|
|
|
|
const unavailableClient = (failure: AnalysisError): AnalysisClient => ({
|
|
execute: () => Promise.resolve(err(failure)),
|
|
close: () => Promise.resolve(),
|
|
});
|
|
|
|
const projectSessionError = (
|
|
operation: AnalysisOperation,
|
|
failure: GhidraSessionError,
|
|
): AnalysisError => {
|
|
if (failure.kind === "cancelled")
|
|
return new AnalysisCancelledError(operation);
|
|
if (failure.kind === "timeout" || failure.kind === "analysis_timeout")
|
|
return new AnalysisTimeoutError(
|
|
operation,
|
|
failure.timeoutMs ?? GHIDRA_STARTUP_TIMEOUT_MS,
|
|
);
|
|
if (failure.kind === "remote" && failure.remoteCode === "decompile_cancelled")
|
|
return new AnalysisCancelledError(operation);
|
|
if (
|
|
failure.kind === "remote" &&
|
|
["invalid_request", "not_found", "ambiguous"].includes(
|
|
failure.remoteCode ?? "",
|
|
)
|
|
)
|
|
return new AnalysisInputError(operation, { cause: failure });
|
|
if (failure.kind === "remote" && failure.remoteCode === "method_unavailable")
|
|
return new AnalysisCapabilityUnavailableError(
|
|
"ghidra",
|
|
operation,
|
|
failure.message,
|
|
);
|
|
return new ProviderAdapterError("ghidra", operation, {
|
|
cause: failure,
|
|
diagnostics: failure.diagnostics,
|
|
});
|
|
};
|