Ghidra Advanced Development Class

Topics

What is Ghidra?

What is Ghidra?

What is Ghidra?

Programs

Plugins

Scripts

Tools

Project Manager


Server

Why Use Ghidra?

How to Install Ghidra

What is Eclipse?

Eclipse


Eclipse


Eclipse
Eclipse
Eclipse
Eclipse

Lab 1


Development

Development

Anatomy of an Extension Project

Extension Project

Extension Project


src/main/java/
src/main/resoures/
Extension Project


src/main/help/
Extension Project


ghidra_scripts/
Extension Project

data/
Extension Project

lib/
Extension Project

os/
Extension Project

Lab 2

Program API

High-Level Classes


Flat vs. Program API

Program API

Flat Program API


Flat Program API
public class FlatProgramAPI { FlatProgramAPI(Program) analyze() clear...() create...() find...() get...() remove...() save() set...() to...() }

Scripting

Scripting


Scripting

Scripting

public abstract class GhidraScript extends FlatProgramAPI { ask...() create...() get...() goto...() print...() run...() to...() }
Scripting
//TODO add description here //TODO add metadata here public class MyScript extends GhidraScript { public void run() throws Exception { //TODO add code here } }
Scripting

Scripting


Scripting

Scripting


Scripting

Scripting

Scripting

Scripting

while (!monitor.isCancelled()) { ... }
Console


Scripting Lab

Lab 3

Lab 3 Answer
//It prints "Hello class" to the console. //@category Category A.Category B //@keybinding alt shift 6 //@menupath Script.My Class Script //@toolbar Info.png import ghidra.app.script.GhidraScript; public class Lab3Script extends GhidraScript { @Override public void run() throws Exception { println("Hello class"); } }
Lab 4

Lab 4 Answer
//Ask for an integer and print the current //program’s name that many times to the console //@category GADC import ghidra.app.script.GhidraScript; public class Lab4Script extends GhidraScript { @Override public void run() throws Exception { int n = askInt("How Many Times?", "N"); for (int i = 0; i < n; ++i) { if (monitor.isCancelled()) { break; } println(i + ". " + currentProgram.getName()); Thread.sleep(1000); } } }
Lab 5

Lab 5 Answer
//This script searches through all instructions that are //moving a scalar into a register //and sets an EOL comment in the form "[register] = [value]" //@category GADC import ghidra.app.script.GhidraScript; import ghidra.program.model.lang.Register; import ghidra.program.model.listing.Instruction; import ghidra.program.model.scalar.Scalar; public class Lab5Script extends GhidraScript { @Override public void run() throws Exception { for (Instruction instruction = getFirstInstruction(); instruction != null; instruction = getInstructionAfter(instruction)) { if ( monitor.isCancelled() ) { break; } if (instruction.getNumOperands() != 2) { continue; } Object[] opObjects0 = instruction.getOpObjects(0); if (opObjects0.length != 1 || !(opObjects0[0] instanceof Register)) { continue; } Object[] opObjects1 = instruction.getOpObjects(1); if (opObjects1.length != 1 || !(opObjects1[0] instanceof Scalar)) { continue; } Register register = (Register) opObjects0[0]; Scalar scalar = (Scalar) opObjects1[0]; String comment = "[" + register.getName() + "]=[" + scalar.toString(16, false, false, "", "") + "]"; setEOLComment(instruction.getMinAddress(), comment); } } }

Headless Scripting

Headless Scripting

Plugins

Plugins

Plugins
ProgramPlugin

Lab 6

Docking Windows

GUIs
Docking Windows
Actions

Tables

Trees

Lab 7

Component Provider

ComponentProvider

Lab 8

Lab 9

Lab 10 (Optional)

Handling Binary Formats

Binary Formats

A Toy Format

The "Ghidra Format" (see ghidra.h): struct ghidra_header { char magic[6]; // magic number identifier unsigned byte cputype; // cpu specifier unsigned short nsections; // number of sections unsigned short nsymbols; // number of symbols unsigned int flags; // flags }; struct ghidra_section { // for 32-bit architectures char name[16]; // name of this section unsigned int addr; // memory address of this section unsigned int size; // size in bytes of this section unsigned int offset; // file offset of this section unsigned int flags; // flags (section type and attributes }; struct ghidra_symbol { // for 32-bit architectures char name[25]; // name of this symbol unsigned int addr; // memory address of this symbol unsigned short type; // type of this symbol };
Lab 11

Analyzers for Raw Binary Files

Analyzers

// Display name, input type, priority String getName(); AnalyzerType getAnalysisType(); AnalysisPriority getPriority(); // Called for changes to analyzer inputs boolean added(...); boolean removed(...); // Register and react to user options void registerOptions(...); void optionsChanged(...);
Lab 12

Loaders

Loaders
package ghidra.app.util.opinion; // By convention public class MyLoader implements Loader { Collection<LoadSpec> findSupportedLoadSpecs(...); List<DomainObject> load(...); ... }
Lab 13

Lab 14

File System Loaders

File System Viewer

File System Viewer

Lab 15

Lab 16

Sleigh

Sleigh
Lab 17

Making a Build of Your Extension

Making a Build

Making a Build (alt)
cd /path/to/extension export GHIDRA_INSTALL_DIR=/path/to/ghidra gradle extensionDistZip #Substitute extension name
Lab 18