Skip to content

Scripting in Miniature

Everything the previous chapters did by hand can be asked of a script: a small program that runs inside Ghidra, over the analysis, and prints or changes what it finds. This chapter walks one complete script — the one this guide's lab actually ran, shipped as examples/SurveyProgram.java — because a survey script is the right first shape: it reads everything, changes nothing, and its output is the first page of any analysis.

What a script is

A Ghidra script in Java is a class extending GhidraScript, with the work in its run() method. Inside run(), the script speaks of things already familiar:

  • currentProgram — the program under analysis: its functions, listing, and data, the same database the CodeBrowser shows.
  • monitor — the progress handle long work checks for cancellation.
  • println(...) — output, which the CodeBrowser shows in its console and headless writes to the log.
  • getScriptArgs() — the words that followed the script's name on the command line (chapter 10's separate-words rule).

SurveyProgram.java asks three questions of currentProgram: list every function with its entry address, list every defined string with its address, and decompile one function — named by the script's argument, or found at an address argument, which is the only handle a stripped binary leaves. The decompiler is driven through its programmatic face, DecompInterface, the same engine the Decompiler pane uses; that is why headless evidence could stand in for window reading throughout this guide. The file is about a hundred lines with its comments, and reading it top to bottom is a reasonable first exercise once the chapters before it make sense.

Two doors run it: the CodeBrowser's Script Manager window (Window menu), which lists every script it can find with a run button — Ghidra 12.2's own Base feature alone bundles 194 Java scripts, most of them small tools in this same shape (Script Manager itself documented) — and headless -postScript, which chapters 10 and 11 are built on.

The name collision, told honestly

The script began life as ListFunctions.java — an obvious name, which is exactly what was wrong with it. Ghidra bundles a script of the same name (in its Function ID feature). Headless resolves scripts by name across all its folders, and one lab run, with the script folders arranged slightly differently, quietly executed Ghidra's ListFunctions instead of the guide's — which rejected the argument scramble as an invalid choice, in an error message that made no sense until the log's SCRIPT: line named the file that had actually run. The fix was a rename to something nobody ships: SurveyProgram.

The lessons generalise beyond this guide:

  • Name your scripts like you name variables on someone else's machine — distinctively, because the search path is shared.
  • Read the SCRIPT: line when a script misbehaves. The log always says which file ran; "my script is broken" and "my script never ran" are different failures.
  • A collision can also fail the other way — "script not found" — when arguments or paths confuse the resolution (chapter 10's traps). Name, path, and argument quoting are the three suspects, in that order.

Beyond one script

Scripts can also write — rename functions, apply comments, create data — which makes them the way repetitive annotation is done honestly: a pass that names every function matching a known pattern applies chapter 7's discipline at scale, and it is checkable because the script is text that can be read before it runs. Write-capable scripts deserve the caution of any program run against your work: try them on a copy of the project first.

Python is the other language on offer: 12.2 ships PyGhidra, with its own launcher (support/pyghidraRun), which runs Python scripts against the same database. The guide's worked example is Java because Java is Ghidra's native tongue and its scripts run identically in the window and headless without extra machinery; PyGhidra is surveyed here, not exercised, and is labelled accordingly (documented).

Check: a first script has succeeded when it runs both ways — from the Script Manager and from -postScript — prints the survey you asked for, and its name is one Ghidra itself doesn't already own.