Skip to main content

Overview

The GhidraScript class is the foundation for writing custom scripts in Ghidra. It extends FlatProgramAPI and provides access to program analysis, manipulation, and user interaction capabilities.

Creating a Script

All Ghidra scripts must:
  1. Be written in Java
  2. Extend ghidra.app.script.GhidraScript
  3. Implement the run() method
  4. Include a description comment at the top (lines starting with //)

Basic Template

Script State Variables

Ghidra automatically provides these instance variables when a script runs:

Core Methods

Abstract Methods

The main entry point for your script. This is where you implement your script logic.

Output Methods

Prints a message to the console and Ghidra’s log.
Prints a message without a newline.
Prints a formatted message using C-style printf formatting.
Prints an error message to stderr and Ghidra’s error log.

User Input Methods

GhidraScript provides numerous ask* methods for user input:
Prompts the user to enter an integer value.
Prompts the user to enter an address.
Prompts the user to enter a string.
Prompts the user with a yes/no question.
Prompts the user to select from a list of choices.
Prompts the user to select a file.
Prompts the user to select a directory.

Running Other Scripts

Runs another script by name. The called script shares the same GhidraState.
Runs a script without allowing it to modify this script’s state.

Analysis Control

Returns the analysis mode for this script. Override to control auto-analysis behavior. Analysis Modes:
  • AnalysisMode.ENABLED - Script runs normally with auto-analysis responding to changes
  • AnalysisMode.DISABLED - Auto-analysis is disabled during script execution
  • AnalysisMode.SUSPENDED - Analysis is suspended and will run after script completes

Utility Methods

Returns true if the script is running in headless (non-GUI) mode.
Returns the name of the current script.
Returns script-specific arguments passed to the script.
Sets script-specific arguments.

Complete Example

Properties Files

Scripts can use .properties files to pre-populate values for ask* methods:
  1. Create a file named MyScript.properties in the same directory as MyScript.java
  2. Add key-value pairs for default values
  3. In GUI mode, these values pre-populate input fields
  4. In headless mode, these values are used automatically
Example: AskScript.properties

Script Arguments

Scripts can accept command-line arguments when run in headless mode:
Access arguments in your script:

Best Practices

  1. Always check for null - State variables like currentProgram may be null
  2. Use monitor.checkCancelled() - Allow users to cancel long-running operations
  3. Handle exceptions - Wrap risky operations in try-catch blocks
  4. Clean up resources - Override cleanup(boolean success) to release resources
  5. Provide meaningful output - Use println() to keep users informed of progress
  6. Use transactions - When modifying the program, changes are automatically wrapped in transactions

See Also