Ghidra Bundles

Dynamic modules

Scripting brings a poweful form of dynamic extensibilty to Ghidra, where Java source code is (re)compiled, loaded, and run without exiting Ghidra. When a script grows large or requires external dependencies, it might be worth the effort think of how to split up code into sensible pieces, or modules.

To support modularity while preserving the dynamic nature of scripts, Ghidra uses OSGi. Without delving too much into terminology, the key things to know are

  1. The unit of modularity in OSGi is the bundle. Bundles are mostly independent components with well-defined collaborations.
  2. Concretely, a bundle is a Jar file with extra metadata in its manifest file that tells the framework what it uses and what it provides.
  3. Bundles can export Java packages for use by other bundles, and packages can have version numbers assigned.
  4. Bundles can import packages from other bundles, and each import can be constrained by version number or range.
  5. Bundles are Jars, but Ghidra will automatically compile source directories to bundles. We refer to source directories intended to be used in OSGi as source bundles. In particular Ghidra script directories are source bundles.
  6. The entire Ghidra API is part of the "system bundle", so hosted bundles have complete access.

Source bundles

When a directory is added to the Bundle Manager, it is treated as a source bundle. When enabled, its Java contents are compiled to
    <user home>/.ghidra/.ghidra-<version>/osgi/compiled-bundles/<hash>,
where <hash> is a hash of the source bundle location.

exploded bundles

Each subdirectory of compiled-bundles/ is an exploded jar -- by compressing it, we get a standard Jar bundle:

            jar cMf mybundle.jar -C $HOME/.ghidra/.ghidra_<version>/osgi/compiled-bundles/<hash>  .
      

mybundle.jar could then be shared in binary form.

generated files

If there is no manifest in the source directory, Ghidra generates one using bndlib so that:

If no bundle activator is present, a stub is created and referenced in the generated manifest.

Dependency

Two types of code dependency are available when developing with OSGi, intra-bundle and inter-bundle.

intra-bundle (compile time) dependency

Classes within a bundle, e.g. source files in a source bundle, can rely one another with Java's usual package import.

This kind of dependency is resolved at compile time -- if a class isn't imported or present, compilation will fail!

inter-bundle (run time) dependency

To make use of code from other bundles, a bundle must declare its requirements. When a bundle is activated, the framework attempts to resolve its declared dependencies against active bundles. The first match, in the order those bundles were activated, will be "wired" to the dependent.

Note: OSGi bundle dependency is very similar to Java 9 modules, except that Java 9 modules provide load time resolution.

Via the Jar manifest, there are multiple ways to configure the requirements of a bundle. We'll describe only one, the Import-Package attribute, because Ghidra provides a shortcut which we'll describe afterwards.

Import-Package in the manifest

You can find a high level description of OSGi at https://osgi.org and details here .

Examples of the syntax for common cases are below:

          Import-Package:com.example.stuff
          Import-Package:com.example.stuff;version=1.2.3
          Import-Package:com.example.stuff;version="[1.2.0,1.3)"
          Import-Package:com.example.stuff,org.example.otherstuff
          Import-Package:com.example.stuff;version="[1.2.0,1.3)",org.example.otherstuff
          Import-Package:com.example.stuff;version="[1.2.0,1.3)",org.example.otherstuff;version=2.3.4
    

Note: the file META-INF/MANIFEST.MF should have only one Import-Package entry.

@importpackage in source

If a source bundle includes a file META-INF/MANIFEST.MF, it will be used as the bundle's manifest. If not, a manifest will be generated, see above.

Ghidra provides a shortcut to populate Import-Package, the @importpackage metadata tag. The @importpackage values from all source files in the root of a source bundle are appended to the generated manifest's Import-Package attribute.

Enabling and disabling bundles

For a bundle's contents to be available (e.g. for loading classes), its OSGi state must be "ACTIVE". Ghidra generally takes care of this, but the following provides more details about the underlying OSGi for debugging.

Ghidra activates a bundle when added, enabled, or when a script contained within is run.

When enabled, the root directory of a source bundle is also scanned for Ghidra scripts and any found are added to the script manager.

When disabled, any dependents of a bundle are stopped/deactivated first, then the bundle itself is stopped. Its scripts are then removed from the script manager.

The color of each bundle path reflects state as follows:

The normally hidden column "OSGi State" is also available. In addition to the Bundle state, this column will report

Adding and removing bundles from the manager

Adding a directory to the bundle manager will also enable it, so scripts within become available in the script manager.

Removing a bundle disables it, so its scripts will be removed from the script manager and its dependents will become inactive.

Cleaning bundles

When Ghidra builds a source bundle, the results are written to the directory
    <user home>/.ghidra/.ghidra-<version>/osgi/compiled-bundles/<hash>.
These files can then be loaded by the OSGi framework.

A clean deactivates then wipes this subdirectory for each selected bundle and clears its build summary.

If a source bundle's imports aren't available during build, some source files can produce errors. In order to force Ghidra to recompile, one must either modify the files with errors or clean the bundle then re-enable.