Overview
PyGhidra enables you to write Ghidra scripts in native CPython 3, providing seamless integration between Python and Ghidra’s Java API through JPype. This allows you to leverage Python’s ecosystem while accessing all of Ghidra’s powerful reverse engineering capabilities.Features
- Native CPython 3 support
- Full Java interoperability via JPype
- Pythonic interfaces to Java objects
- Virtual environment support
- Interactive console within Ghidra
- Script provider for running Python GhidraScripts
Installation
PyGhidra is included with Ghidra as a feature module. It handles:- Virtual environment creation and management
- Externally managed environment support
- Automatic dependency installation
Script Structure
PyGhidra scripts follow the same structure as Java scripts but use Python syntax:Script Metadata
@category:- Organizes scripts in the Script Manager@runtime PyGhidra- Declares this script requires PyGhidra runtime
Type Checking Support
PyGhidra provides type hints through theghidra_builtins module:
currentProgram, currentAddress, etc.
Java Interoperability
Importing Java Classes
Import Java classes as if they were Python modules:Using Java Objects
Java objects work like Python objects with added convenience features:Automatic Getter/Setter Access
Java bean properties can be accessed as Python attributes:Java Arrays
Many Ghidra methods require Java arrays. JPype provides helpers:Passing Python Bytes
For read-only operations, Python bytes objects work directly:Accessing Ghidra Script Variables
PyGhidra scripts automatically have access to the same state variables as Java scripts:| Variable | Type | Description |
|---|---|---|
currentProgram | Program | The active program |
currentAddress | Address | Current cursor location |
currentLocation | ProgramLocation | Current program location |
currentSelection | ProgramSelection | Current selection |
currentHighlight | ProgramSelection | Current highlight |
monitor | TaskMonitor | Task monitor |
Using FlatProgramAPI
AllFlatProgramAPI methods are available directly in PyGhidra scripts:
Working with Memory
Working with Functions
Working with Instructions
Working with Data
Working with Symbols
User Interaction
PyGhidra scripts support all the same user interaction methods:Output
Exception Handling
Complete Examples
Example 1: Basic PyGhidra Script
Example 2: Function Analysis
Example 3: String Analysis
Example 4: Binary Pattern Search
JPype Reference
PyGhidra uses JPype for Java interoperability. Key concepts:Type Conversions
| Python Type | Java Type | Notes |
|---|---|---|
int | int, long | Automatic |
float | double, float | Automatic |
str | String | Automatic |
bytes | byte[] | For read-only |
list | List, ArrayList | Auto-conversion |
dict | Map, HashMap | Auto-conversion |
Creating Java Arrays
Calling Methods
Best Practices
- Use type hints - Import
ghidra_builtinsfor better IDE support - Check for None - Java methods can return null
- Handle signed bytes - Java bytes are signed (-128 to 127)
- Use monitor.isCancelled() - Allow users to cancel long operations
- Prefer Python idioms - Use list comprehensions, slicing, etc.
- Leverage existing Python libraries - NumPy, regex, etc.
Troubleshooting
Common Issues
Problem: “Cannot find Java class”Resources
- JPype Documentation - Detailed Java-Python interop guide
- Ghidra JavaDoc - Complete API reference
- PyGhidra source:
Ghidra/Features/PyGhidra/
See Also
- GhidraScript API - Java scripting reference
- FlatProgramAPI - Simplified program API
