Skip to content

Python Scripting Cheat Sheet

Thomas Roth edited this page Mar 9, 2019 · 3 revisions

Ghidra Python Scripting Cheat-sheet

The Python API is identical to the Java API, for which a basic documentation can be found here: Ghidra API - Hosted by GHIDRA_RE

Pre-imported globals

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.

Important misc.

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:

from ghidra.program.model.listing import CodeUnit

cu = currentProgram.getListing().getCodeUnitAt(addr)

# Get a comment

# Set the comment
cu.setComment(CodeUnit.EOL_COMMENT, "Comment text")

Adding bookmarks

createBookmark(addr, "Category", "Description")

Working with functions

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)

Working with addresses

# 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
Clone this wiki locally
You can’t perform that action at this time.