-
Notifications
You must be signed in to change notification settings - Fork 100
Python Scripting Cheat Sheet
Thomas Roth edited this page Mar 9, 2019
·
3 revisions
The Python API is identical to the Java API, for which a basic documentation can be found here: Ghidra API - Hosted by GHIDRA_RE
These are available in the shell and in scripts, no need to import anything:
Name | Type | Description |
---|---|---|
currentAddress |
ghidra.program.model.address.GenericAddress |
The currently selected address. |
currentHighlight |
TODO | |
currentProgram |
ghidra.program.database.ProgramDB |
The current program. |
currentSelection |
ghidra.program.util.ProgramSelection |
The currently selected instructions. |
currentProgram.defaultPointerSize
- gets the pointer-size of the program (for example 32 or 64 bit)
A code unit is an interface to access both data & instructions
Comment types:
EOL_COMMENT
PLATE_COMMENT
PRE_COMMENT
POST_COMMENT
REPEATABLE_COMMENT
from ghidra.program.model.listing import CodeUnit
cu = currentProgram.getListing().getCodeUnitAt(addr)
# Get a comment
cu.getComment(CodeUnit.EOL_COMMENT)
# Set the comment
cu.setComment(CodeUnit.EOL_COMMENT, "Comment text")
createBookmark(addr, "Category", "Description")
from ghidra.program.model.symbol import SourceType
# Get the FunctionManager
fm = currentProgram.getFunctionManager()
# Get a function at a certain address
f = fm.getFunctionAt(currentAddress)
# Get a function which contains the currentAddress
f = fm.getFunctionContaining(currentAddress)
# Change the function name
f.setName("test", SourceType.USER_DEFINED)
# Get address from String
address = currentProgram.getAddressFactory().getAddress("0x123")
# Create new address from earlier one
new_address = address.add(5)
# Example: Get an address relative to the program base
currentProgram.minAddress.add(10)