Ghidra is a software reverse engineering (SRE) framework
parse $GHIDRA_HOME in analyzeHeadless -scriptPath, fixes GP-781
Jason P. Leasureb8f1b241ba41
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessAnalyzer.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessAnalyzer.javaindex e18ca98a78..f63589fdac 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessAnalyzer.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessAnalyzer.java@@ -293,9 +293,10 @@ public class HeadlessAnalyzer {}}- GhidraScriptUtil.initialize(new BundleHost(), options.scriptPaths);+ List<String> parsedScriptPaths = parseScriptPaths(options.scriptPaths);+ GhidraScriptUtil.initialize(new BundleHost(), parsedScriptPaths);try {- initializeScriptPaths();+ showConfiguredScriptPaths();compileScripts();Msg.info(HeadlessAnalyzer.class, "HEADLESS: execution starts");@@ -400,9 +401,10 @@ public class HeadlessAnalyzer {}}- GhidraScriptUtil.initialize(new BundleHost(), options.scriptPaths);+ List<String> parsedScriptPaths = parseScriptPaths(options.scriptPaths);+ GhidraScriptUtil.initialize(new BundleHost(), parsedScriptPaths);try {- initializeScriptPaths();+ showConfiguredScriptPaths();compileScripts();Msg.info(HeadlessAnalyzer.class, "HEADLESS: execution starts");@@ -668,10 +670,23 @@ public class HeadlessAnalyzer {}}- /**- * Gather paths where scripts may be found.- */- private void initializeScriptPaths() {+ private List<String> parseScriptPaths(List<String> scriptPaths) {+ List<String> parsedScriptPaths = new ArrayList<>();+ for (String path : scriptPaths) {+ ResourceFile pathFile = Path.fromPathString(path);+ String absPath = pathFile.getAbsolutePath();+ if (pathFile.exists()) {+ parsedScriptPaths.add(absPath);+ }+ else {++ Msg.warn(this, "REPORT: Could not find -scriptPath entry, skipping: " + absPath);+ }+ }+ return parsedScriptPaths;+ }++ private void showConfiguredScriptPaths() {StringBuffer buf = new StringBuffer("HEADLESS Script Paths:");for (ResourceFile dir : GhidraScriptUtil.getScriptSourceDirectories()) {buf.append("\n ");@@ -856,8 +871,8 @@ public class HeadlessAnalyzer {Class<?> c = Class.forName(className, true, classLoaderForDotClassScripts);// Get parent folder to pass to GhidraScript- File parentFile = new File(- c.getResource(c.getSimpleName() + ".class").toURI()).getParentFile();+ File parentFile = new File(c.getResource(c.getSimpleName() + ".class").toURI())+ .getParentFile();currScript = (GhidraScript) c.getConstructor().newInstance();currScript.setScriptArgs(scriptArgs);diff --git a/Ghidra/RuntimeScripts/Common/support/analyzeHeadlessREADME.html b/Ghidra/RuntimeScripts/Common/support/analyzeHeadlessREADME.htmlindex 00b7bd9cb9..dfbea0d5de 100644--- a/Ghidra/RuntimeScripts/Common/support/analyzeHeadlessREADME.html+++ b/Ghidra/RuntimeScripts/Common/support/analyzeHeadlessREADME.html@@ -313,7 +313,7 @@ The Headless Analyzer uses the command-line parameters discussed below. See <a h<LI><a name="scriptPath"><typewriter>-scriptPath "<path1>[;<path2>...]"</typewriter></a><br>Specifies the search path(s) for scripts, including secondary scripts (a script invoked from- another script). A path may start with <typewriter>$GHIDRA_SCRIPT</typewriter>, which corresponds+ another script). A path may start with <typewriter>$GHIDRA_HOME</typewriter>, which correspondsto the Ghidra installation directory, or <typewriter>$USER_HOME</typewriter>, which correspondsto the user's home directory. On Unix systems, these home variables must be escaped using a'<typewriter>\</typewriter>' (backslash) character.@@ -344,7 +344,7 @@ The Headless Analyzer uses the command-line parameters discussed below. See <a h<LI><a name="propertiesPath"><typewriter>-propertiesPath "<path1>[;<path2>…]"</typewriter></a><br>Specifies path(s) that contain <typewriter>.properties</typewriter> files used by scripts or- secondary/subscripts. A path may start with <typewriter>$GHIDRA_SCRIPT</typewriter>, which+ secondary/subscripts. A path may start with <typewriter>$GHIDRA_HOME</typewriter>, whichcorresponds to the Ghidra installation directory, or <typewriter>$USER_HOME</typewriter>, whichcorresponds to the user's home directory. On Unix systems these home variables must beescaped with a '\' character.