Introduction
Ghidra provides powerful scripting capabilities that allow you to automate reverse engineering tasks, perform custom analysis, and extend Ghidra’s functionality. Scripts can be written in Java or Python and have full access to the Ghidra API.Scripting Languages
Ghidra supports multiple scripting environments:- Java: Full-featured scripting using GhidraScript class
- Python (Jython): Legacy Python 2.7 scripting within Ghidra GUI
- Python (PyGhidra): Native CPython 3 scripting with full Ghidra API access
Script Locations
Ghidra looks for scripts in several locations:- User Scripts Directory:
~/ghidra_scripts/(customizable) - System Scripts:
<GHIDRA_INSTALL>/Ghidra/Features/Base/ghidra_scripts/ - Custom Script Paths: Configurable via Script Manager
Script Manager
The Script Manager (Window → Script Manager) provides:- Script organization and categorization
- Quick script search and filtering
- Script editor integration
- Keybinding assignments
- Script execution and debugging
Execution Environments
GUI Mode
Run scripts interactively from within Ghidra:- Open Script Manager (Window → Script Manager)
- Browse or search for your script
- Double-click or press the Run button
- View output in the Console window
Headless Mode
Execute scripts in batch processing mode:PyGhidra Standalone
Run Python scripts outside Ghidra GUI:Script State Variables
All GhidraScripts have access to these state variables:currentProgram- The active programcurrentAddress- Current cursor locationcurrentLocation- Program location of cursorcurrentSelection- Current address selection (may be null)currentHighlight- Current highlight (may be null)monitor- TaskMonitor for progress tracking
Common Use Cases
Analysis Automation
Automate repetitive analysis tasks:- Apply custom signatures
- Identify code patterns
- Annotate binaries with metadata
Data Extraction
Extract information from programs:- Export function signatures
- Extract strings and constants
- Generate reports
Custom Analysis
Implement specialized analysis:- Protocol parsing
- Custom decompilation
- Vulnerability detection
Batch Processing
Process multiple binaries:- Compare programs
- Apply consistent annotations
- Generate aggregate reports
Script Categories
Scripts are organized by category using the@category annotation:
- Analysis - Custom analyzers
- Examples - Tutorial scripts
- Functions - Function-related operations
- Symbol - Symbol manipulation
- Data - Data type operations
Best Practices
Error Handling
Always handle exceptions appropriately:Transactions
Wrap program modifications in transactions:Progress Monitoring
Provide user feedback for long operations:Script Development Workflow
- Create - Use Script Manager to create new script from template
- Edit - Modify script in Eclipse or external editor
- Test - Run script on sample binary
- Debug - Use println() or Java debugger
- Refine - Optimize and add error handling
- Deploy - Share or use in production workflows
Resources
- Java Scripts - Java GhidraScript development
- Python Scripts - Jython scripting guide
- PyGhidra - Native Python 3 scripting
- Headless Analyzer - Batch processing
Example Scripts
Ghidra includes numerous example scripts demonstrating common tasks:AddCommentToProgramScript.java- Basic program modificationAskScript.java- User input dialogsExportFunctionInfoScript.java- Data extractionAutoRenameLabelsScript.java- Batch symbol operations
<GHIDRA_INSTALL>/Ghidra/Features/Base/ghidra_scripts/