Introduction
PyGhidra is a Python library that provides direct access to the Ghidra API within a native CPython 3 interpreter using JPype. Originally developed by the Department of Defense Cyber Crime Center (DC3) as “Pyhidra”, it enables modern Python workflows with full Ghidra functionality.Key Features
- Native CPython 3 - Use Python 3.x with modern syntax and libraries
- Standalone operation - Run Ghidra scripts outside the GUI
- Full API access - Complete access to Ghidra’s Java API
- Project management - Open, create, and manage Ghidra projects
- Type stubs - IDE autocomplete and type checking support
- Integration ready - Use Ghidra as part of larger Python workflows
Installation
Prerequisites
- Ghidra 12.0 or later installed
- Python 3.8 or later
- pip package manager
Install PyGhidra
Online installation:Install Type Stubs (Optional)
For better IDE support:Set Ghidra Installation Path
Option 1: Environment variableQuick Start
Basic Program Analysis
Legacy API (Simple)
Core API Reference
pyghidra.start()
Initialize Ghidra in headless mode:Project Management
Open or create project:Analysis Operations
Run analysis:Transactions
All program modifications require transactions:Running GhidraScripts
Advanced Usage
Walking Projects
Process all domain files:Working with Filesystems
Accessing the Decompiler
Memory Operations
Symbol Operations
Real-World Examples
Example 1: Batch Binary Analysis
Example 2: Function Signature Extraction
Example 3: Custom Analysis with Transactions
Custom Launchers
For advanced JVM configuration:Package Name Conflicts
When Python modules conflict with Java packages:Best Practices
- Use context managers - Ensures proper resource cleanup
- Handle transactions - Always wrap modifications in transactions
- Set timeouts - Use task monitors with timeouts for long operations
- Save work - Call
program.save()after modifications - Check started state - Use
pyghidra.started()before callingstart() - Release programs - Always release programs when done
Troubleshooting
Common Issues
ImportError: No module named pyghidraMigration from Jython
Key differences when migrating from Jython scripts:| Jython 2 | PyGhidra (Python 3) |
|---|---|
print "text" | print("text") |
xrange() | range() |
| Auto state variables | Must access via program |
| GUI context | Standalone context |
.properties files | Python configuration |
