Advertisement
cephurs

analyzeHeadlessREADME

Mar 6th, 2019
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 43.92 KB | None | 0 0
  1. Headless Analyzer README
  2. Table of Contents
  3. Introduction
  4. Usage
  5. Examples
  6. Writing Scripts for the Headless Analyzer
  7. Passing Parameters using arguments
  8. Passing Parameters using askXxx() methods
  9. Headless Scripts
  10. Enabling/Disabling Analysis
  11. Setting the Import Directory
  12. Checking for Analysis Timeout
  13. Headless Scripts: Passing Values Between Scripts
  14. Using Scripts to Control Program Disposition
  15. Using Multiple Scripts to Control Program Disposition
  16. Wildcards: Specifying Files to Import or Process
  17. Headless Analyzer Introduction and Guidelines
  18. The Headless Analyzer is a command-line-based (non-GUI) version of Ghidra that allows users to:
  19.  
  20. Create and populate projects
  21. Perform analysis on imported or existing binaries
  22. Run non-GUI scripts in a project (scripts may be program-dependent or program-independent)
  23. The Headless Analyzer can be useful when performing repetitive tasks on a project (i.e., importing and analyzing a directory of files or running a script over all the binaries in a project).
  24.  
  25. Users initiate Headless operation using the analyzeHeadless shell script. The shell script takes, at a minimum, the path and name of an existing project (or one to be created). When other parameters are specified, the following types of actions may be performed:
  26. Import a single file or directory of executable(s) (recursively or non-recursively).
  27. Process a single file or directory of executable(s) already present in an existing project (recursively or non-recursively).
  28. Run any number of non-GUI Ghidra pre-processing scripts on each executable.
  29. Turn analysis on or off for each executable.
  30. Run any number of non-GUI Ghidra post-processing scripts on each executable.
  31. Write to a log with information about each file processed; separated logging is available for scripts.
  32. Keep or delete a created project.
  33. Save any changes made to the project/file, or operate in a read-only manner in -import or -process modes.
  34. Use pre- and/or post-processing scripts to dictate program disposition. For example, scripts can dictate whether further processing (i.e., analysis or other scripts) should be aborted and whether the current file should be deleted after all processing is complete.
  35. While running, be aware that:
  36. The Headless Analyzer may not run if the specified project is already open in Ghidra.
  37. In bulk import mode (i.e., specifying a directory, -import dirOfExes, or wildcard string, -import dir1/*), any file beginning with the character "." is assumed to be a hidden file and ignored by default. However, when a file beginning with "." is named during import (for example, import /Users/user/.hidden.exe), the Headless Analyzer will attempt to import it.
  38. Log files can only be redirected if Log4J is used.
  39. (Back to Top)
  40. Headless Analyzer Usage
  41. The Headless Analyzer uses the command-line parameters discussed below. See Examples for common use cases.
  42.  
  43. analyzeHeadless <project_location> <project_name>[/<folder_path>] | ghidra://<server>[:<port>]/<repository_name>[/<folder_path>]
  44. [[-import [<directory>|<file>]+] | [-process [<project_file>]]]
  45. [-preScript <ScriptName> [<arg>]*]
  46. [-postScript <ScriptName> [<arg>]*]
  47. [-scriptPath "<path1>[;<path2>...]"]
  48. [-propertiesPath "<path1>[;<path2>...]"]
  49. [-scriptlog <path to script log file>]
  50. [-log <path to log file>]
  51. [-overwrite]
  52. [-recursive]
  53. [-readOnly]
  54. [-deleteProject]
  55. [-noanalysis]
  56. [-processor <languageID>]
  57. [-cspec <compilerSpecID>]
  58. [-analysisTimeoutPerFile <timeout in seconds>]
  59. [-keystore <KeystorePath>]
  60. [-connect [<userID>]]
  61. [-p]
  62. [-commit ["<comment>"]]
  63. [-okToDelete]
  64. [-max-cpu <max cpu cores to use>]
  65. [-loader <desired loader name>]
  66.  
  67. <project_location>
  68. The directory that either contains an existing Ghidra project (in -import or -process mode) or will contain a newly created project (in -import mode for a local project).
  69. You must specify either a project location and project name, or a Ghidra Server repository URL.
  70.  
  71. Some parameters will have no effect, depending on which project_location is specified. The following table shows parameters that are specific to project_location:
  72.  
  73. Parameter Local Project Server Repository
  74. -p X
  75. - connect X
  76. - keystore X
  77. - commit X
  78. - delete X
  79.  
  80.  
  81. <project_name>[/<folder_path>]
  82. The name of either an existing project (in -import or -process mode) or new project (in -import mode) to be created in the above directory. If the optional folder path is included, imports will be rooted under this project folder. In -import mode with -recursive enabled, any folders in the folder path that do not already exist in the project will be created (even if nested).
  83. You must specify either a project location and project name, or a Ghidra Server repository URL.
  84.  
  85.  
  86. ghidra://<server>[:<port>]/<repository_name>[/<folder_path>]
  87. A Ghidra Server repository URL (shared Ghidra Server project) and folder path. Using the repository URL eliminates the need for a local shared Ghidra project; however, the named repository must already exist on the Ghidra Server. If the specified repository does not already exist, it will not be created (see the GhidraProject class for a simple API that allows shared project creation from within a script).
  88.  
  89. If the optional folder path is included, imports will be rooted under this folder (in -import mode, folders will be created if they don't already exist).
  90.  
  91.  
  92. -import [<directory>|<file>]+
  93. Note: -import and -process can not both be present in the parameters list.
  94.  
  95. Specifies one or more executables (or directories of executables) to import. When importing a directory, a folder with the same name will be created in the Ghidra project. When using the -recursive parameter, each executable that is found in a recursive search through the given directory will be stored in the project in the same relative location (i.e., any directories found under the import directory will also be created in the project).
  96.  
  97. Operating system-specific wildcard characters can be used when importing files and/or directories. Please see the Wildcards section for more details.
  98.  
  99. When importing multiple executables/directories in the same session, use one of the following methods:
  100. List multiple directories and/or executables after the -import option, separated by a space.
  101. -import /Users/myDir/peFiles /Users/myDir/otherFiles/test.exe
  102.  
  103. Repeat the -import option multiple times (each use of -import may be separated by other parameters) to import from more than one directory or file source.
  104. -import /Users/myDir/peFiles -recursive -import /Users/myDir/otherFiles/test.exe
  105.  
  106. -process [<project_file>]
  107. Note: -import and -process can not both be present in the parameters list.
  108.  
  109. Performs processing (running pre/post-scripts and/or analysis) on one or more program files that already exist in the project or repository. Use the optional project_file argument to specify an existing file by name. Searching will be performed within the specified project folder (specified by folder_path, which was included with the project_name or repository URL specification). Omit the project_file argument to allow processing over all files within the project folder.
  110.  
  111. You can also use the wildcard characters '*' and '?' in the project_file parameter to specify all files within a folder which match the pattern. To prevent premature expansion (by the shell) of any wildcard characters, use single quotes around the project_file. For example:
  112. -process '*.exe'
  113.  
  114. For further details on wildcard usage, please see the Wildcards section below.
  115.  
  116. Omitting the optional project_file argument will cause all files to be processed within the project folder (equivelent to '*').
  117.  
  118. Including the -recursive parameter will cause the same project file name/pattern search to be performed recursively within all sub-folders.
  119.  
  120. Unlike the -import option, -process may only be specified once.
  121.  
  122.  
  123. -preScript <ScriptName.ext> [<arg>]*
  124. Identifies the name of a script that will execute before analysis, and an optional list of arguments to pass to the script. The script name must include its file extension (i.e., MyScript.java).
  125.  
  126. This parameter expects the script name only; do not include the path to the script. The Headless Analyzer searches specific default locations for the named script, but additional script director(ies) may also be specified (see the -scriptPath argument for more information).
  127.  
  128. This option must be repeated to specify additional scripts. See the Scripting section for a description of advanced scripting capabilities.
  129.  
  130.  
  131. -postScript <ScriptName.ext> [<arg>]*
  132. Identifies the name of a script that will execute after analysis, and an optional list of arguments to pass to the script. The script name must include its file extension (i.e., MyScript.java).
  133.  
  134. This parameter expects the script name only; do not include the path to the script. The Headless Analyzer searches specific default locations for the named script, but additional script director(ies) may also be specified (see the -scriptPath argument for more information).
  135.  
  136. This option must be repeated to specify additional scripts. See the Scripting section for a description of advanced scripting capabilities.
  137.  
  138.  
  139. -scriptPath "<path1>[;<path2>...]"
  140. Specifies the search path(s) for scripts, including secondary scripts (a script invoked from another script). A path may start with $GHIDRA_SCRIPT, which corresponds to the Ghidra installation directory, or $USER_HOME, which corresponds to the user's home directory. On Unix systems, these home variables must be escaped using a '\' (backslash) character.
  141.  
  142. Examples:
  143. Windows:
  144. -scriptPath "$GHIDRA_HOME/Ghidra/Features/Base/ghidra_scripts;/myscripts"
  145. Unix:
  146. -scriptPath "\$GHIDRA_HOME/Ghidra/Features/Base/ghidra_scripts;/myscripts"
  147. The scriptPath parameter is optional. If it is not present, the Headless Analyzer will search the following paths for the specified script(s):
  148. $USER_HOME/ghidra_scripts
  149. All ghidra_script subdirectories that exist in the Ghidra distribution
  150.  
  151.  
  152. -propertiesPath "<path1>[;<path2>…]"
  153. Specifies path(s) that contain .properties files used by scripts or secondary/subscripts. A path may start with $GHIDRA_SCRIPT, which corresponds to the Ghidra installation directory, or $USER_HOME, which corresponds to the user's home directory. On Unix systems these home variables must be escaped with a '\' character.
  154.  
  155. More information on the use of .properties files to pass parameters during Headless Analysis can be found here.
  156.  
  157.  
  158. -scriptlog <path to script log file>
  159. Sets the location of the file that stores logging information from pre- and post-scripts. If a path to a script log file is not set, script logs are written to script.log in the user directory, by default.
  160.  
  161. NOTE: Only the built-in scripting print methods will print to the the script log file (print, println, printf, printerr).
  162.  
  163.  
  164. -log <path to log file>
  165. Sets the location of the file that stores logging information from analysis or other non-script processing of the files. If a path to a log file is not set, logging information is written to application.log in the user directory, by default.
  166.  
  167.  
  168. -overwrite
  169. Applies to -import mode only and is ignored if the -readOnly option is present. If present, an existing project file that conflicts with an import file is overwritten. If this parameter is not included, import files that conflict with existing project files will be skipped (if not operating with the -readOnly option). If a conflicting file is contained within a version repository, and the -commit option has not been specified, the overwrite will fail. Removing a versioned file is also subject to other permission and in-use restrictions which could also cause an overwrite failure.
  170.  
  171.  
  172. -recursive
  173. If present, enables recursive descent into directories and project sub-folders when a directory/ folder has been specified in -import or -process modes.
  174.  
  175.  
  176. -readOnly
  177. If present in -import mode, imported files will NOT be saved to the project. If present in -process mode, any changes made to existing files by scripts or analysis are discarded. The -overwrite option will be ignored if this option is specified during import operations.
  178.  
  179.  
  180. -deleteProject
  181. If present, the Ghidra project will be deleted after scripts and/or analysis have completed (only applies if the project has been created in the current session with -import; existing projects are never deleted). This project delete option is assumed when the -readOnly option is specified for import operations which create a new project.
  182.  
  183.  
  184. -noanalysis
  185. If present, executables will not be analyzed (auto-analysis occurs by default).
  186.  
  187.  
  188. -processor <languageID>
  189. Specifies the processor information to be used in -import mode (and subsequent analysis, if analysis is enabled). Be sure to use quotes around the languageId if it contains spaces. If this parameter is not present, Ghidra uses header info (if available) to identify the processor.
  190.  
  191. The possible languageIDs can be found in the processor-specific .ldefs files (found here: ghidra_x.x\Ghidra\Processors\proc_name\data\languages\*.ldefs) in the id attribute of the language element. The specified <languageID> should match exactly, including case, as it appears in the .ldefs file.
  192.  
  193. For example:
  194. <language processor="x86"
  195. endian="little"
  196. size="32"
  197. variant="default"
  198. version="2.6"
  199. slafile="x86.sla"
  200. processorspec="x86.pspec"
  201. manualindexfile="../manuals/x86.idx"
  202. id="x86:LE:32:default">
  203.  
  204. Note: The -processor parameter may be used without specifying the -cspec parameter (if the given processor is valid, the Headless Analyzer chooses the default compiler specification for that processor).
  205.  
  206.  
  207. -cspec <compilerSpecID>
  208. Specifies the compiler specification to be used in -import mode (and subsequent analysis, if analysis is enabled).
  209.  
  210. The possible compilerSpecIDs can be found in the processor-specific .ldefs files (found here: ghidra_x.x\Ghidra\Processors\proc_name\data\languages\*.ldefs) in the id attribute of the appropriate compiler element. The specified <compilerSpecID> should match exactly, including case, as it appears in the .ldefs file.
  211.  
  212. For example:
  213. <compiler name="Visual Studio" spec="x86win.cspec" id="windows"/>
  214. <compiler name="gcc" spec="x86gcc.cspec" id="gcc"/>
  215. <compiler name="Borland C++" spec="x86borland.cspec" id="borlandcpp"/>
  216.  
  217. Note: The -cspec parameter may not be used without specifying the -processor parameter.
  218.  
  219.  
  220. -analysisTimeoutPerFile <timeout in seconds>
  221. Sets a timeout value (in seconds) for analysis. If analysis on a file exceeds the specified time, analysis is interrupted and processing continues as scheduled (i.e., to the -postScript stage, if specified). Results from individual analyzers that have completed processing prior to timeout will still be saved with the program. Post-scripts can be used to detect that analysis has timed out (in Headless processing ONLY) by calling the getHeadlessAnalysisTimeoutStatus() method.
  222.  
  223.  
  224. -keystore <KeystorePath>
  225. When connecting to a Ghidra Server using PKI or SSH authentication, this option allows specification of a suitable private keystore file. The file should rely on filesystem protection only to avoid prompting for a password.
  226.  
  227.  
  228. -connect <userID>
  229. If used, allows the process owner's default userID to be overridden with the given userID when connecting to a Ghidra Server (provided the server has been configured to allow this).
  230.  
  231.  
  232. -p
  233. When connecting to a server, allows interactive prompting for a password via the console. Although this method of authentication is normally discouraged, the server connection will likely fail authentication if a password is required and this parameter is not enabled.
  234. NOTE: In some cases, password entry will be echoed to the console (a warning will show at password prompt).
  235.  
  236.  
  237. -commit ["<comment>"]
  238. When connected to a shared project, enables a commit of changes to the project's underlying repository (residing on the Ghidra Server). Commits are enabled by default for shared projects; however, the optional quoted comment may be specified and will be saved with all commits. Commits do not apply when the -readOnly parameter is present.
  239.  
  240.  
  241. -okToDelete
  242. When using Headless Scripts to control program disposition in -process mode, it is possible to delete existing programs in a project. These deletions are permanent and can not be undone (in a versioned project, all versions of a program are deleted). To ensure that programs are not deleted irretrievably without the user's knowledge, Headless operation requires the -okToDelete parameter to be set if a program is to be deleted in -process mode. If a program is scheduled to be deleted and -okToDelete has not been set, Headless will print a warning and the program will not be deleted.
  243.  
  244. The -okToDelete parameter is not necessary when running in -import mode. If a HeadlessScripts schedules deletion of one of the programs being imported, the program will simply not be saved to the project.
  245.  
  246.  
  247. -max-cpu <max cpu cores to use>
  248. Sets the maximum number of CPU cores to use during headless processing (must be an integer). Setting max-cpu to 0 or a negative integer is equivalent to setting the maximum number of cores to 1.
  249.  
  250.  
  251. -loader <desired loader name>
  252. Forces the file to be imported using a specific loader.
  253.  
  254. Loaders can take additional arguments that they apply during the import process. Below is a list of the most commonly used loaders and their arguments.
  255. Note: Full java package loader paths are no longer recognized.
  256.  
  257. -loader BinaryLoader
  258. -loader-blockName <block name>
  259. -loader-baseAddr <base address1>
  260. -loader-fileOffset <file offset2>
  261. -loader-length <length in bytes2>
  262. -loader-applyLabels <true|false>
  263. -loader-anchorLabels <true|false>
  264. -loader ElfLoader
  265. -loader-applyLabels <true|false>
  266. -loader-anchorLabels <true|false>
  267. -loader-createExportSymbolFiles <true|false>
  268. -loader-loadExternalLibs <true|false>
  269. -loader-applyRelocations <true|false>
  270. -loader-imagebase <imagebase3>
  271. -loader-includeOtherBlocks <true|false>
  272. -loader-resolveExternalSymbols <true|false>
  273. -loader PeLoader
  274. -loader-applyLabels <true|false>
  275. -loader-anchorLabels <true|false>
  276. -loader-createExportSymbolFiles <true|false>
  277. -loader-loadExternalLibs <true|false>
  278. -loader-parseCliHeaders <true|false>
  279. -loader MachoLoader
  280. -loader-applyLabels <true|false>
  281. -loader-anchorLabels <true|false>
  282. -loader-createExportSymbolFiles <true|false>
  283. -loader-loadExternalLibs <true|false>
  284.  
  285. 1. Address must be in the form [space:]offset. Space is optional, and offset is a hex value with no leading 0x.
  286. 2. To specify hexadecimal, use a leading 0x.
  287. 3. Address is in the default space, and must be specified as a hexadecimal value without the leading 0x.
  288. (Back to Top)
  289. Headless Analyzer Examples
  290. Import a binary /binaries/binary1.exe to a local Ghidra Project named Project1. Analysis is on by default.
  291. analyzeHeadless /Users/user/ghidra/projects Project1 -import /binaries/binary1.exe
  292.  
  293.  
  294. Import all *.exe binaries from a local folder to a local Ghidra project named Project1, suppressing analysis.
  295. analyzeHeadless /Users/user/ghidra/projects Project1 -import /Users/user/sourceFiles/*.exe -noanalysis
  296.  
  297.  
  298. Import the binary /usr/local/binaries/binaryA.exe to a subfolder of a local Ghidra Project, running a prescript, but suppressing analysis.
  299. analyzeHeadless /Users/user/ghidra/projects Project1/folderOne -scriptPath /usr/scripts -preScript RunThisScriptFirst.java -import /usr/local/binaries/binaryA.exe -noanalysis
  300.  
  301.  
  302. Import the binary /usr/local/binaries/binaryB.exe to a local Ghidra Project, running a prescript that depends on a .properties file in the location /propertiesLocation. Analysis is on by default.
  303. analyzeHeadless /Users/user/ghidra/projects Project1 -scriptPath /usr/scripts -preScript RunThisScriptFirst.java -propertiesPath /propertiesLocation -import /usr/local/binaries/binaryB.exe
  304.  
  305.  
  306. Specify more than one import to a local project, running more than one script and performing analysis.
  307. analyzeHeadless /Users/user/ghidra/Projects Project1/folderOne -scriptPath /usr/scripts -preScript RunThisScriptFirst.java -preScript RunThisScriptSecond.java -import /usr/local/binaries/binaryA.exe /user/local/morebinaries -postScript RunThisScriptLast.java
  308.  
  309. OR
  310.  
  311. analyzeHeadless /Users/user/ghidra/Projects Project1/folderOne -scriptPath /usr/scripts -preScript RunThisScriptFirst.java -preScript RunThisScriptSecond.java -import /usr/local/binaries/binaryA.exe -postScript RunThisScriptLast.java -import /user/local/morebinaries
  312.  
  313.  
  314. Run a script on an existing project binary importedBinA.exe in the folder folderOne of the existing project named Project1.
  315. analyzeHeadless /Users/user/ghidra/Projects Project1/folderOne -scriptPath /user/scripts -postScript FixupScript.java -process importedBinA.exe -noanalysis
  316.  
  317.  
  318. Recursively run scripts and analysis over all the binaries in the folder folderTwo of the existing project named Project2.
  319. analyzeHeadless /Users/user/ghidra/Projects Project2/folderTwo -scriptPath /user/scripts -preScript FixupPreScript.java -process -recursive
  320.  
  321.  
  322. Run a script and analysis on binaries starting with the letter 'a' in the folder aFolder (and any of its subfolders) in the existing projected named Project1.
  323. analyzeHeadless /Users/user/ghidra/Projects Project1/aFolder -scriptPath /user/scripts -preScript ProcessAScript.java -process 'a*' -recursive
  324.  
  325.  
  326. Recursively import the directory/usr/local/binaries to a Ghidra Server, running a prescript and analysis. Commit changes with the specified comment. Server prompts for a password for the user named userID.
  327. analyzeHeadless ghidra://example.server.org:13100/RepositoryName/RootFolder -scriptPath /usr/scripts/ -preScript RunThisScriptFirst.java -import /usr/local/binaries -recursive -connect userID -p -commit "Testing server imports."
  328.  
  329.  
  330. Change the default log location when importing and analyzing a file.
  331. analyzeHeadless /Users/user/ghidra/projects Project1 -import /binaries/binary1.exe -log /new/log_location.txt
  332.  
  333.  
  334. Re-import and overwrite a file that already exists in the project.
  335. analyzeHeadless /Users/user/ghidra/projects Project1 -import /binaries/IAlreadyExist.exe -overwrite
  336.  
  337.  
  338. Create a new project, import and analyze a file, then delete the project when done.
  339. analyzeHeadless /Users/user/ghidra/projects ANewProject -import /binaries/binary2.exe -deleteProject
  340.  
  341.  
  342. Set a timeout value, in seconds, for analysis (analysis will abort if it takes longer than the set timeout value).
  343. analyzeHeadless /Users/user/ghidra/projects MyProject -import /binaries/binary2.exe -analysisTimeoutPerFile 100
  344.  
  345.  
  346. Run a script without using -import or -process modes (Script must not be program-dependent!).
  347. analyzeHeadless /Users/user/ghidra/projects MyProject -preScript HelloWorldScript.java -scriptPath /my/ghidra_scripts
  348.  
  349.  
  350. Specify a language and compiler to be used when importing with analysis.
  351. analyzeHeadless /Users/user/ghidra/projects MyProject -import hello.exe -processor "x86:LE:32:System Management Mode" -cspec default
  352.  
  353.  
  354. Import, run a script, and analyze a file, but don't allow the file to be saved to the project.
  355. analyzeHeadless /Users/user/ghidra/projects MyProject -import hello.exe -preScript GetInfoScript.java -readOnly
  356.  
  357.  
  358. Import and run scripts that take their own own arguments.
  359. analyzeHeadless /Users/user/ghidra/projects MyProject -import hello.exe -preScript Script.java arg1 arg2 arg3 -preScript AnotherScript.java "arg1 with spaces" arg2
  360.  
  361.  
  362. Import a PE file as a raw binary image with a specified base address and block name.
  363. analyzeHeadless /Users/user/ghidra/projects MyProject -import hello.exe -loader BinaryLoader -loader-baseAddr 0x1000 -loader-blockName MyBlock -processor x86:LE:32:default
  364.  
  365. (Back to Top)
  366. Writing Scripts for the Headless Analyzer
  367. Many scripts that extend the GhidraScript class, and written for use with the headed (GUI) version of Ghidra, can also be used during Headless operation. However, there are certain GUI-specific methods that do not make sense when called during Headless operation. When a GhidraScript containing one or more GUI-specific methods is run headlessly, the script will throw an ImproperUseException.
  368.  
  369. A script that extends the HeadlessScript class may be used to write scripts that refer to Headless-only methods. See the HeadlessScripts section for more detail.
  370.  
  371. Here are some general guidelines for running scripts headlessly.
  372.  
  373. If neither -import mode nor -process mode is specified in the Headless Analyzer command line arguments, only the specified pre/post-script(s) will be executed. In this case, all scripts must execute in a program-independent manner, or errors will occur. If you intend for scripts to be run against programs, please run in -process mode.
  374. For each pre-/post-script group, scripts are executed in the order specified on the command line.
  375. Any pre- or post-script may invoke the setTemporary method on currentProgram to prevent changes from being saved. In -import mode, the method prevents the specific import from being saved. In -process mode, the method prevents changes to the program from being saved.
  376. Avoid using the script API method setServerCredentials for shared projects.
  377. (Back to Top)
  378. Passing Parameters using arguments
  379. As of Ghidra 7.2, it is possible to pass script-specific arguments directly to scripts. The arguments are stored in a String array and can be accessed with the following method:
  380. String[] args = getScriptArgs();
  381. If running in headless mode, this array will contain the ordered list of arguments passed to the script on the command line (specified with -preScript or -postScript).
  382. For example, if a script was run with the following command:
  383. analyzeHeadless /Users/user/ghidra/projects MyProject -import hello.exe -preScript Script.java arg1 arg2 arg3 -preScript AnotherScript.java "arg1 with spaces" arg2
  384. Then the elements of the argument array for Script.java would look like this:
  385. args = {"arg1", "arg2", "arg3"}
  386. and the argument array for AnotherScript.java would look like this:
  387. args = {"arg1 with spaces", "arg2"}
  388. Passing Parameters using askXxx() methods
  389. Many of the GhidraScript askXxx() methods can be run in both headless and headed (GUI) modes, allowing seamless script usage between headed and headless modes. As of Ghidra 6.1, the following methods can be run in both modes:
  390.  
  391. askFile
  392. askDirectory
  393. askLanguage
  394. askProjectFolder
  395. askInt
  396. askLong
  397. askAddress
  398. askBytes
  399. askProgram
  400. askDomainFile
  401. askDouble
  402. askString
  403. askChoice
  404. askChoices
  405. askYesNo
  406. Further details for each specific askXxx() method can be found in the method's JavaDoc.
  407.  
  408. When running headlessly, the askXxx() methods allow users to to "pre-set" or "pass in" one or more values for use in scripts. Use the appropriate method to pass in values of certain types (i.e., file, directory, int, long).
  409.  
  410. To pass a value to a script, create a .properties file corresponding to each GhidraScript that uses an askXxx() method. For example, the .properties file that corresponds to a script named MyScript.java would share the script's basename and be called MyScript.properties. By default, the Headless Analyzer assumes that the script and its .properties file are both located in the same folder. If you would like the .properties file to be in a different location from the script, you can use the propertiesPath parameter to specify the location of the .properties file. Below is an example of a GhidraScript and its .properties file. Use it for reference to determine how the .properties file should be structured to communicate the necessary information to the GhidraScript:
  411.  
  412. Script1.java
  413. public class Script1 extends GhidraScript {
  414.  
  415. @Override
  416. public void run() throws Exception {
  417.  
  418. File userFile = askFile("Choose a file ", "Please choose a file: ");
  419. println("Chosen file: " + userFile.toString());
  420.  
  421. double userDouble = askDouble("Double dialog", "Please enter a double: ");
  422. println("Entered double: " + userDouble);
  423.  
  424. double userDouble2 = askDouble("Double dialog", "Please enter another double: ");
  425. println("Second entered double: " + userDouble2);
  426.  
  427. Address userAddress = askAddress("Address", "Enter an address!");
  428. println("Entered address: " + userAddress.toString());
  429.  
  430. byte[] userBytes = askBytes("Asking for bytes", "Put some bytes here --");
  431. StringBuilder byteStr = new StringBuilder();
  432. for (byte aByte : askedBytes) {
  433. byteStr.append(String.format("%02X ", aByte));
  434. }
  435. println("Bytes: " + byteStr.toString().trim());
  436.  
  437. String userString = askString("Asking for a string", "Please type a string: ", "my default String");
  438. println("Entered String: " + userString);
  439.  
  440. }
  441. }
  442. Script1.properties
  443.  
  444. # A comment line is indicated if the '#' or '!' character is the first non-whitespace character of that line.
  445. #
  446. # Use a space-separated concatenation of the parameters to communicate which variable gets what value:
  447. # Format: <space-separated concatenation of parameters> = <value>
  448. #
  449. # Notice that spaces at the beginning and end of parameters are removed prior to concatenation.
  450. #
  451. # Note that if the askXxx() method contains a "defaultValue" parameter, that parameter should not be included
  452. # in the concatenation of parameters.
  453.  
  454. Choose a file Please choose a file: = /Users/username/help.exe
  455. Double dialog Please enter a double: = 32.2
  456. Address Enter an address! = 0x10AB34D
  457. Double dialog Please enter another double: = 3.14159
  458. Asking for bytes Put some bytes here -- = AA BB CC 11 02 24
  459. Asking for a string Please type a string: = STRING ABC
  460.  
  461. NOTE: If script-specific arguments have been passed into the script, the askXxx() methods will consume values found in the argument array rather than a .properties file. The first askXxx() method will use the first value in the array, the second askXxx() method will use the second value in the array, and so on. If all of the arguments in the array have been consumed, the next askXxx() will throw an IndexOutOfBoundsException exception.
  462.  
  463. (Back to Top)
  464. HeadlessScripts
  465. A script of type HeadlessScript (which extends GhidraScript) can be used by any user looking for more control over the Headless Analysis process than is offered using the more generic GhidraScript class. Using HeadlessScripts, users are able to store variables for use by later scripts, change the location of where an import will be saved, and change the disposition of a program depending on script-specific conditions (i.e., save it in a different folder, delete it, turn off analysis, abort further processing, etc.).
  466.  
  467. HeadlessScripts allow the user to access certain methods that are specific to the HeadlessAnalyzer. Otherwise, these types of scripts operate exactly like GhidraScripts. Users should only use HeadlessScript for headless operation. While HeadlessScripts could possibly run successfully in the Ghidra GUI, an exception will be thrown if a HeadlessScript-only method is called during GUI operation.
  468.  
  469.  
  470. (Back to Top)
  471. Headless Scripts: Enabling/Disabling Analysis
  472. In order to enable or disable analysis using a HeadlessScript, simply include the following line in your script:
  473. enableHeadlessAnalysis(true); // turn on analysis
  474.  
  475. OR
  476.  
  477. enableHeadlessAnalysis(false); // turn off analysis
  478. Note that a script that includes this line should be run as a preScript, since preScripts execute before analysis would typically run. Running the script as a postScript is ineffective, since the stage at which analysis would have happened has already passed.
  479.  
  480. This change will persist throughout the current HeadlessAnalyzer session, unless changed again (in other words, once analysis is enabled via script for one program, it will also be enabled for future programs in the current session, unless changed).
  481.  
  482. NOTE: To check whether analysis is currently enabled, use the following method:
  483.  
  484. boolean analysisEnabled = isHeadlessAnalysisEnabled();
  485. (Back to Top)
  486. Headless Scripts: Setting the Import Directory
  487. When using -import mode, a user can change the path in the Ghidra project where imported files are saved. This is done by using the following script method:
  488. setHeadlessImportDirectory("path/to/new/dir");
  489. The new path does not have to exist (it will be created if it doesn't already exist). The path is also assumed to be relative to the project's root folder.
  490. Here are some examples assuming the Ghidra project structure looks like this:
  491.  
  492.  
  493. MyGhidraProject:
  494. /dir1
  495. /innerDir1
  496. /innerDir2
  497.  
  498. The following usage ensures that any files imported after the call to this method are saved in the existing MyGhidraProject:dir1/innerDir2 folder:
  499. setHeadlessImportDirectory("dir1/innerDir2");
  500.  
  501. In contrast, the following usage adds new folders to the Ghidra project and saves the imported files into the newly-created path.
  502. setHeadlessImportDirectory("dir1/innerDir2/my/folder");
  503.  
  504. changes the directory structure to:
  505. MyGhidraProject:
  506. /dir1
  507. /innerDir1
  508. /innerDir2
  509. /my
  510. /folder
  511.  
  512. Another usage example where new folders are added to the Ghidra project.
  513. setHeadlessImportDirectory("dir1/newDir/saveHere");
  514.  
  515. This changes the directory structure to:
  516. MyGhidraProject:
  517. /dir1
  518. /innerDir1
  519. /innerDir2
  520. /newDir
  521. /saveHere
  522.  
  523. When using this method to set the save directory for imports, whether the save succeeds may depend on the state of the -overwrite parameter. For example, if the new import location already exists and contains a file of the same name as the current program, the current program will only be successfully saved if -overwrite is enabled.
  524.  
  525. This change in import directory will persist throughout the current HeadlessAnalyzer session, unless changed again (in other words, once the import location has been changed, it will continue to be the import save location for future imported programs in the current session, unless changed again).
  526.  
  527. To revert back to the default import location (that which was specified via command line), pass the null object as the argument to this method:
  528.  
  529. setHeadlessImportDirectory(null); // Sets import save directory to default
  530. The setHeadlessImportDirectory method is ineffective in -process mode (the program will not be saved to a different location if this method is called when running in -process mode).
  531. (Back to Top)
  532. Headless Scripts: Checking for Analysis Timeout
  533. In the case where all of the following apply:
  534.  
  535. the user set an analysis timeout period using the -analysisTimeoutPerFile parameter
  536. analysis is enabled and has completed
  537. the current script is being run as a postScript
  538. the user can check whether analysis timed out, using the following query method:
  539. boolean didTimeout = analysisTimeoutOccurred();
  540. (Back to Top)
  541. Headless Scripts: Passing Values Between Scripts
  542. If you are running multiple scripts in headless operation and would like to store a value in one script that is accessible by another script, use the HeadlessScript methods below. They facilitate the storage and retrieval of key-value pairs to/from a data structure that is available to any script of type HeadlessScript:
  543.  
  544. storeHeadlessValue(String key, Object value);
  545. Object myObject = getStoredHeadlessValue(String key);
  546. boolean containsKey = headlessStorageContainsKey(String key);
  547. (Back to Top)
  548. Headless Scripts: Using Scripts to Control Program Disposition
  549. HeadlessScripts can be used to control disposition of the program currently being imported/processed (note: if running in -process mode with -readOnly enabled, programs can not be deleted, even if directed by a script).
  550.  
  551. The available options to control program disposition are as follows:
  552.  
  553. HeadlessContinuationOption.ABORT
  554. in import mode, does not run any follow-on scripts/analysis; program is imported.
  555. in process mode, does not run any follow-on scripts/analysis; changes to the current (existing) program are saved.
  556.  
  557. HeadlessContinuationOption.ABORT_AND_DELETE
  558. in import mode, does not run any follow-on scripts/analysis; program is not imported.
  559. in process mode, does not run any follow-on scripts/analysis; the current (existing) program is deleted.
  560.  
  561. HeadlessContinuationOption.CONTINUE_THEN_DELETE
  562. in import mode, continues to run any follow-on scripts/analysis; program is not imported.
  563. in process mode, continues to run any follow-on scripts/analysis; the current (existing) program is deleted after processing is complete.
  564.  
  565. HeadlessContinuationOption.CONTINUE (default setting)
  566. in import mode, continues to run any follow-on scripts/analysis; program is imported.
  567. in process mode, continues to run any follow-on scripts/analysis; changes to the current (existing) program are saved.
  568. To set the program disposition, use the setHeadlessContinuationOption method. For example, to dictate that further processing be aborted and the program deleted, the script should use the following method with the ABORT_AND_DELETE option:
  569. setHeadlessContinuationOption(HeadlessContinuationOption.ABORT_AND_DELETE);
  570. At the start of processing for each program (immediately before the first script runs), the script's continuation option is set to CONTINUE by default. If the setHeadlessContinationOption method is not used, then operation continues as normal.
  571.  
  572. Note that when an option is set, it takes effect AFTER the current script completes. For example, setting the continuation option to ABORT does not immediately abort the current script; instead, it aborts any processing (analysis, other scripts) that immediately follow the current script.
  573.  
  574. In the case where a subscript or secondary script sets an ABORT or ABORT_AND_DELETE option, that option will go into effect once the primary (or outermost) script has completed execution.
  575.  
  576. For a very basic example script, see SetHeadlessContinuationOptionScript.java, which is included in the Ghidra distribution.
  577.  
  578. When multiple scripts set program disposition, they are combined. Continue on to Using Multiple Scripts to Control Program Disposition to understand how this works.
  579.  
  580. (Back to Top)
  581. Headless Scripts: Using Multiple Scripts to Control Program Disposition
  582. While running scripts that change the program disposition, there may be instances when the program disposition is changed more than once for the same program. Some cases where this could happen are:
  583. when the user runs multiple pre-scripts and/or post-scripts that use setHeadlessContinuationOption
  584. when the user runs scripts that call sub-scripts (or secondary scripts) that use setHeadlessContinuationOption
  585. when the user runs a script that makes multiple calls to the setHeadlessContinuationOption method
  586. If there are multiple calls to setHeadlessContinuationOption within a single script, the last method call is used as the setting dictated by that script.
  587.  
  588. However, if multiple scripts make calls to setHeadlessContinuationOption, the options from each script are combined in a rational way (in the order the options were set) to potentially result in a new continuation option.
  589.  
  590. For example, if Script1.java sets the continuation option, then is followed by Script2.java which also sets the continuation option, the resulting continuation status is shown in the following diagram:
  591.  
  592. Script1 Continuation Option Script2 Continuation Option
  593. ABORT ABORT_AND_DELETE CONTINUE_THEN_DELETE CONTINUE
  594. ABORT ABORT* ABORT* ABORT* ABORT*
  595. ABORT_AND_DELETE ABORT_AND_DELETE* ABORT_AND_DELETE* ABORT_AND_DELETE* ABORT_AND_DELETE*
  596. CONTINUE_THEN_DELETE ABORT_AND_DELETE ABORT_AND_DELETE CONTINUE_THEN_DELETE CONTINUE_THEN_DELETE
  597. CONTINUE ABORT ABORT_AND_DELETE CONTINUE_THEN_DELETE CONTINUE
  598.  
  599. * In cases where Script1 specifies ABORT or ABORT_AND_DELETE, Script2 will not run unless Script2 is a subscript or secondary script called by Script1.
  600.  
  601. Keep in mind:
  602. If Script2 does not change the continuation option, then the status from Script1 will carry over.
  603. An ABORT at the postScript stage is still meaningful in stopping further processing, since follow-on analysis may occur as a result of changes made by the postScript.
  604. You can check the current continuation option by using the getHeadlessContinuationOption method. For example:
  605. HeadlessContinuationOption currentOption = getHeadlessContinuationOption();
  606.  
  607. When specifying deletion options such as ABORT_AND_DELETE or CONTINUE_THEN_DELETE in -process mode, be sure to include -okToDelete in the command line parameters to verify that deletions are allowed. This is an extra safety step to ensure programs aren't deleted when the user didn't mean to delete them.
  608. (Back to Top)
  609. Wildcards: Specifying Files to Import or Process
  610. Wildcards can be used when specifying files and/or directories for -import mode, or when specifying one or more files for -process mode. Wildcards in -import mode are expanded by the underlying system shell before being passed on to headless Ghidra (consequently, any wildcard limitations will be dictated by the specific operating system you are using). Wildcards in -process mode are expanded by headless Ghidra and are limited to the use of '*' and '?' only.
  611.  
  612. Note that wildcarding is NOT supported for specifying the Ghidra project/repository location or folder path.
  613.  
  614. Below are some general guidelines for wildcard usage:
  615.  
  616. -import mode
  617. During import, the rules for wildcard use depend on the operating system on which the Headless Analyzer is being run. The operating system will expand the wildcards to a list of matching files and pass the list to the Headless Analyzer.
  618.  
  619. Unix-based Operating Systems allow the following wildcards:
  620. Use the '*' character to substitute for zero or more characters
  621. Use the '?' character to substitute for exactly one character
  622. Use ranges of characters enclosed in square brackets (for example, [a-z]) to substitute for any one of the characters in that range. Negation of the characters is also allowed by using a leading "!" within the brackets (i.e., [!a-z]).
  623. Wildcards can expand to either directories or files
  624.  
  625. Windows allows the following wildcards:
  626. Use the '*' character to substitute for zero or more characters
  627. Use the '?' to substitute for one character or less
  628. Wildcards can only expand to files (directories whose names conform to the wildcard string will not be returned)
  629.  
  630. When using a wildcard to specify files, be sure to use as specific of a string as possible. There may be unintended consequences to using more generalized wildcard strings, such as:
  631. ./analyzeHeadless /home/usr/ghidra/projects TestProj -import /home/files/n*
  632.  
  633. When using a Unix-based operating system, this import specification results in not only all files in /home/files starting with n to be imported, but also the contents of the all directories starting with n to be imported (contents of those directories' subdirectories would also be imported, if the -recursive option was specified -- note that the contents of the directories starting with n are not also subject to the restriction that they start with n).
  634.  
  635. -process mode:
  636. In process mode, the wildcard string applies only to files, not directories. The only accepted wildcard characters are '*' and '?'.
  637.  
  638. There are some cases where the wildcard string may be prematurely expanded by the operating system. For example, in order to run in -process mode over all existing project files that start with the letter a, one might use the following command:
  639. ./analyzeHeadless /home/usr/ghidra/projects TestProj -process a* -recursive
  640.  
  641. Instead of the "a*" string being passed along to be used within the Ghidra project, the shell will prematurely expand the wildcard string to match all a-prefixed files in the current directory. The command ends up looking like this:
  642. ./analyzeHeadless /home/usr/ghidra/projects TestProj -process analyzeHeadless -recursive
  643.  
  644. In order to prevent the system shell from doing this premature wildcard expansion, simply surround the wildcard string with single-quote characters:
  645. ./analyzeHeadless /home/usr/ghidra/projects TestProj -process 'a*' -recursive
  646.  
  647. (Back to Top)
  648. Last modified: Aug 31 2017
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement