A blog on reverse engineering technology and reverse engineering technology.
Ghidra provides the really nice experience of setting up an Eclipse Project for you, with all the libraries setup and the ability to create a new script that has a basic skeleton. This is a great user experience and got me started with writing my own scripts pretty much immediately. After a few scripts Eclipse got fairly annoying though, because I am used to the JetBrains toolkit (mostly PyCharm, but they are similar enough). I wanted to have a full dev setup for Ghidra anyway so I started setting things up with IntelliJ. This series of blogposts will cover my entire setup and aims to include every Ghidra related development task running from IntelliJ and if possible as a fully IDE independent setup. This will cover both Java and Python, so writing and debugging Python scripts that run inside the Ghidra context is included.
This setup is for writing scripts for a non development Ghidra version, i.e. any of the zip files from either the website or Github releases.
If you already have a setup with Eclipse or just an Eclipse with GhidraDev migrating is fairly trivial.
GhidraDev -> New -> Ghidra Script Project
. The details are documented in Extensions/Eclipse/GhidraDev/GhidraDev_README.html
Import Project
to import the Eclipse Project. Most of the defaults work for me, the only thing I need to change is that the Project SDK should be 11 (/usr/lib/jvm/java-11-openjdk
on Arch)Project SDK (11)
and press ok.This still requires some files from the Eclipse Project, so you can’t just delete the Eclipse project Folder. I wouldn’t recommend this setup, but it is a quick way to get started.
11
, no libraries or frameworks, no template, whatever folder you wantsrc
directory to ghidra_scripts
for consistencysupport/buildGhidraJar
. This puts a ghidra.jar
in the root of your Ghidra installghidra_script
roots, just the /
File -> New -> Module from Existing Sources
ghidra_9.0.4
. This should contain the ghidra.jar
Create module from existing sources
ghidra_scripts
folders now, make sure they are all markedSpacebar
DownArrow
Spacebar
…) except the one named like your root e.g. ghidra_9.0.4
that contains the ghidra.jar
ghidra_scripts
folders, and you can inspect and edit them.Add a template named GhidraScript
(or whatever you want) with the following content:
//@category
//@keybinding
//@menupath
//@toolbar
import ghidra.app.script.GhidraScript;
public class ${NAME} extends GhidraScript {
@Override
public void run() throws Exception {
}
}
You can extend it with any template variables ( like ${NAME}
) you care about like author, creation date, copyright, etc.
There should be an New -> GhidraScript
Option now, when a source directory is selected.
This setup can still be improved in some aspects. With just the added JAR as a dependency the IDE has to resort to decompiling if inspecting or debugging Ghidra core code. This is still readable code, but the Javadoc annotations are missing which can be quite helpful. There is probably a nice way to get around this by providing the sources instead of the JAR (or both?) but because I have the Ghidra git locally I mostly use that. I will update this post when I find out a better way, either by getting annoyed by it enough or someone else telling me.
Writing Ghidra Scripts is a good first step to playing with the Ghidra API. Ghidra lacks some features which you might miss from IDA, but often they are a lot easier to implement yourself than they seem to be. One example for this is parsing structures into a datatype via a simple interface like IDA and not having to resort to the Ghidra header parsing process that is more tedious and involves the GUI. The actually logic to implement this is around 4 lines of as a Ghidra script. The details will be covered in a separate blogpost, if you just want the result: https://github.com/fmagin/ghidra_scripts/blob/master/ParseDataType.java