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:
@@ -0,0 +1,118 @@
|
||||
import { readdir, readFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { DOMParser, onWarningStopParsing } from "@xmldom/xmldom";
|
||||
|
||||
import type { GhidraLanguageOverride } from "../config/ghidraLanguageOverride.js";
|
||||
import { ConfigurationError } from "../domain/configurationErrors.js";
|
||||
|
||||
/** One language definition and the compiler specs it declares. */
|
||||
export interface GhidraLanguageDefinition {
|
||||
readonly id: string;
|
||||
readonly compilerSpecIds: readonly string[];
|
||||
}
|
||||
|
||||
// Language definitions are small XML files; refuse anything implausibly large.
|
||||
const MAX_LDEFS_BYTES = 4 * 1024 * 1024;
|
||||
// Processor modules ship under Processors; installed extensions may add more.
|
||||
const LANGUAGE_ROOTS = ["Ghidra/Processors", "Ghidra/Extensions"] as const;
|
||||
|
||||
/** Parse the `<language>` elements of one `.ldefs` document. */
|
||||
export const parseGhidraLanguageDefinitions = (
|
||||
text: string,
|
||||
): readonly GhidraLanguageDefinition[] => {
|
||||
const definitions: GhidraLanguageDefinition[] = [];
|
||||
const document = new DOMParser({
|
||||
onError: onWarningStopParsing,
|
||||
}).parseFromString(text, "application/xml");
|
||||
const root = document.documentElement;
|
||||
if (root?.tagName !== "language_definitions")
|
||||
throw new SyntaxError("Expected Ghidra language_definitions XML root");
|
||||
for (const language of Array.from(root.getElementsByTagName("language"))) {
|
||||
if (language.parentNode !== root) continue;
|
||||
const id = language.getAttribute("id");
|
||||
if (id === null) continue;
|
||||
const compilerSpecIds: string[] = [];
|
||||
for (const compiler of Array.from(
|
||||
language.getElementsByTagName("compiler"),
|
||||
)) {
|
||||
if (compiler.parentNode !== language) continue;
|
||||
const compilerId = compiler.getAttribute("id");
|
||||
if (compilerId !== null) compilerSpecIds.push(compilerId);
|
||||
}
|
||||
definitions.push({ id, compilerSpecIds });
|
||||
}
|
||||
return definitions;
|
||||
};
|
||||
|
||||
const languageDirectories = async (installDir: string): Promise<string[]> => {
|
||||
const directories: string[] = [];
|
||||
for (const root of LANGUAGE_ROOTS) {
|
||||
let modules: string[];
|
||||
try {
|
||||
modules = await readdir(join(installDir, root));
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
for (const module of modules.sort())
|
||||
directories.push(join(installDir, root, module, "data", "languages"));
|
||||
}
|
||||
return directories;
|
||||
};
|
||||
|
||||
/** Read every language definition shipped by one Ghidra installation. */
|
||||
export const readGhidraLanguageCatalog = async (
|
||||
installDir: string,
|
||||
): Promise<readonly GhidraLanguageDefinition[]> => {
|
||||
const definitions: GhidraLanguageDefinition[] = [];
|
||||
for (const directory of await languageDirectories(installDir)) {
|
||||
let entries: string[];
|
||||
try {
|
||||
entries = await readdir(directory);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
for (const entry of entries.sort()) {
|
||||
if (!entry.endsWith(".ldefs")) continue;
|
||||
const bytes = await readFile(join(directory, entry));
|
||||
if (bytes.length > MAX_LDEFS_BYTES) continue;
|
||||
definitions.push(
|
||||
...parseGhidraLanguageDefinitions(bytes.toString("utf8")),
|
||||
);
|
||||
}
|
||||
}
|
||||
return definitions;
|
||||
};
|
||||
|
||||
/**
|
||||
* Check a configured import language against the installation before launch,
|
||||
* so an unknown ID fails as configuration instead of as a failed import.
|
||||
*/
|
||||
export const admitGhidraLanguageOverride = (
|
||||
override: GhidraLanguageOverride,
|
||||
catalog: readonly GhidraLanguageDefinition[],
|
||||
providerVersion: string,
|
||||
): ConfigurationError | null => {
|
||||
const language = catalog.find((value) => value.id === override.languageId);
|
||||
if (language === undefined)
|
||||
return new ConfigurationError("Invalid REA environment configuration", {
|
||||
settings: [
|
||||
{
|
||||
setting: "REA_GHIDRA_LANGUAGE_ID",
|
||||
constraint: `is not a language defined by Ghidra ${providerVersion}`,
|
||||
},
|
||||
],
|
||||
});
|
||||
if (
|
||||
override.compilerSpecId !== undefined &&
|
||||
!language.compilerSpecIds.includes(override.compilerSpecId)
|
||||
)
|
||||
return new ConfigurationError("Invalid REA environment configuration", {
|
||||
settings: [
|
||||
{
|
||||
setting: "REA_GHIDRA_COMPILER_SPEC_ID",
|
||||
constraint: `must be one of the selected language's compiler specs: ${language.compilerSpecIds.join(", ")}`,
|
||||
},
|
||||
],
|
||||
});
|
||||
return null;
|
||||
};
|
||||
Reference in New Issue
Block a user