Ghidra provides a robust scripting and extension framework designed to automate reverse engineering tasks. The system supports multiple languages and runtime environments, primarily centered around a Java-based OSGi infrastructure and a Python integration layer.
The scripting system operates through several key components that manage the lifecycle from script discovery through execution:
Sources:
BundleHost is the central manager for the embedded Apache Felix OSGi framework. It maintains a registry of GhidraBundle objects representing script directories and JAR files Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java59-67
Key Responsibilities:
felixFramework) Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java70bundleMap Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java67BundleHostListener objects Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java97-125Bundle Types:
| Type | Class | Description |
|---|---|---|
| Source Directory | GhidraSourceBundle | Java source compiled on-demand with manifest generation Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraSourceBundle.java59-132 |
| JAR Bundle | GhidraJarBundle | Pre-compiled OSGi bundles loaded directly Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraJarBundle.java1-32 |
| Placeholder | GhidraPlaceholderBundle | Represents missing/invalid bundles Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java171-172 |
For details, see OSGi Bundle Management.
Sources:
GhidraSourceBundle represents a Java source directory compiled into an OSGi bundle as needed Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraSourceBundle.java59-62 The compilation output is cached in a directory derived from the source directory path hash Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraSourceBundle.java150-168
Compilation Process:
newSources requiring compilation Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraSourceBundle.java99-103JavaCompiler to build the source directory Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraSourceBundle.java105BundleActivator classes Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraSourceBundle.java57-62Sources:
Ghidra discovers scripts by scanning configured source directories, including the user scripts directory (typically ~/ghidra_scripts) and module-specific directories Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.java152-185
Metadata Parsing:
The GhidraScriptInfoManager tracks metadata parsed from script headers, such as @category, @keybinding, and @importpackage Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java122-140
Sources:
The script execution pipeline varies between Java and Python but shares common GUI integration via GhidraScriptComponentProvider.
JavaScriptProvider Details:
For Java scripts, the JavaScriptProvider delegates to the OSGi bundle system:
GhidraSourceBundle containing the script Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java55-61bundleHost.activateAll() Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java158GhidraScript subclass Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java100For details, see Script Development and Execution.
Sources:
PyGhidra provides Python scripting capabilities through a JPype bridge that connects a native CPython 3 interpreter to the JVM.
PyGhidra can be used as a standalone library or within the Ghidra GUI. It utilizes PyGhidraLauncher to manage JVM startup and environment configuration.
API Highlights:
pyghidra.start(): Initializes the JVM and Ghidra in headless mode.pyghidra.open_project(): Opens or creates a Ghidra project.pyghidra.analyze(): Triggers auto-analysis on a program.For details, see PyGhidra - Python Integration.
Sources:
The HeadlessAnalyzer provides non-GUI script execution for automation Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessAnalyzer.java72-100 It manages its own application initialization and logging parameters Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessAnalyzer.java101-139
Sources:
The main script management UI consists of:
Sources:
The BundleStatusComponentProvider visualizes bundle status and exposes actions for managing the OSGi environment, such as adding, removing, enabling, and refreshing bundles Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusComponentProvider.java68-73
Sources:
Refresh this wiki