Skip to content

Commit

Permalink
Initial prototype release
Browse files Browse the repository at this point in the history
  • Loading branch information
fmagin committed Dec 6, 2022
1 parent 2c11c50 commit fe45f13
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 263 deletions.
11 changes: 8 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.11
- name: Set up JDK 1.17
uses: actions/setup-java@v1
with:
java-version: 1.11
java-version: 1.17
- uses: er28-0652/setup-ghidra@master
with:
version: "10.1"
version: "10.2.2"

- name: Install Kotlin Plugin
run: >
wget https://github.com/GhidraJupyter/ghidra-jupyter-kotlin/releases/download/v1.6.0/ghidra_10.2.2_PUBLIC_20221203_GhidraJupyterKotlin.zip
&& unzip ghidra_10.2.2_PUBLIC_20221203_GhidraJupyterKotlin.zip -d $GHIDRA_INSTALL_DIR/Ghidra/Extensions
- name: Build with Gradle
run: gradle buildExtension
Expand Down
11 changes: 8 additions & 3 deletions .github/workflows/test_extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.11
- name: Set up JDK 1.17
uses: actions/setup-java@v1
with:
java-version: 1.11
java-version: 1.17
- uses: er28-0652/setup-ghidra@master
with:
version: "10.1"
version: "10.2.2"

- name: Install Kotlin Plugin
run: >
wget https://github.com/GhidraJupyter/ghidra-jupyter-kotlin/releases/download/v1.6.0/ghidra_10.2.2_PUBLIC_20221203_GhidraJupyterKotlin.zip
&& unzip ghidra_10.2.2_PUBLIC_20221203_GhidraJupyterKotlin.zip -d $GHIDRA_INSTALL_DIR/Ghidra/Extensions
- name: Build Extension
run: gradle buildExtension
Expand Down
6 changes: 3 additions & 3 deletions .idea/runConfigurations/Run_Ghidra.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 27 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,71 +1,49 @@
# Ghidra Extension in Kotlin using IntelliJ IDEA
# OpenAI Integration for Ghidra

> Write a Ghidra Extension without using Java or Eclipse!
> Get Function Summaries from ChatGTP
## Setup
## Installation

* Hit `Use this template` at the top of the repo on GitHub
* Clone the new repo to your development machine
* Add the line `GHIDRA_INSTALL_DIR=/path/to/your/ghidra_10.1_PUBLIC/` to `$HOME/.gradle/gradle.properties`
* Open IntelliJ, create a new `Project from Existing Sources...` and select the `build.gradle`
* If you are using the [Kotlin Jupyter Plugin](https://github.com/GhidraJupyter/ghidra-jupyter-kotlin) uncomment the line in the `dependencies` block in the `build.gradle`
* Wait for IntelliJ to finish indexing and fetching dependencies, hit the build button, and then run Ghidra
1. This plugin requires Kotlin language support in Ghidra, so make sure that you have it installed:
https://github.com/GhidraJupyter/ghidra-jupyter-kotlin/releases

2. Grab the latest release of this Plugin from the [releases page](https://github.com/fmagin/ghidra-openai/releases)
and install it in Ghidra.

## Features

* Gradle Config that works out of the box with IntelliJ
* IntelliJ IDEA Run Configuration for debugging of the extension
* If you have are using the [Kotlin Jupyter Plugin](https://github.com/GhidraJupyter/ghidra-jupyter-kotlin) you can also set breakpoints in the script file!
* GitHub CI files that
* make sure the extension at least builds for each PR
* will automatically build a release and publish it on GitHub if a commit is tagged with a version matching `vX.X.X`, e.g. `v1.2.3`/`v1.2.0` (`v1.2` doesn't work!)

## Usage

## Additional Development Tips
### Initial Setup

These aspects can not be included in the repo files itself, but make development smoother.
Configure your API Key via `Edit -> Tool Options -> OpenAI`

### Thread Breakpoints
![img.png](imgs/API_key.png)

Make sure that you use breakpoints that only suspend the thread, and not everything.
This means that the breakpoint will only suspend the thread that is currently running the analysis or the script,
and the GUI will keep working.
* Set a breakpoint, right-click the icon, and in the `Suspend` line select `Thread` instead of `All`
* IntelliJ IDEA will suggest making this the default, click this too

### GUI Integration

### Use Scripts and the Jupyter Kernel to prototype ideas
Right Click in the Decompiler, and select `Get and apply Function Summary via OpenAI`

With the [Kotlin Jupyter Plugin](https://github.com/GhidraJupyter/ghidra-jupyter-kotlin) you can test your new ideas first.
IntelliJ IDEA can do hot reloading of classes, but this has limits and then still requires a Ghidra restart,
which takes an annoying amount of time. The QT Console is fairly basic, but the Jupyter Notebook uses nearly the same
code analysis engine as IntelliJ itself.
Before:
![img.png](imgs/before.png)

### Automatic conversion to Kotlin
After:
![img.png](imgs/after.png)

* pasting Java code into a Kotlin file you will get the suggestion for this to be converted and then pasted
* right-click `.java` file in the Project Tree there is an action at the very bottom to convert the entire file

### Configuration

## Issues
You can easily change the model used for the completions and its parameters via
the `Edit -> Tool Options -> OpenAI -> Model` menu.

If any step in this process doesn't work as described in the README, please open an issue on GitHub.
I have only tested this on Linux so there might be some aspects that work differently on macOS or Windows, though these
should be minor.
![img.png](imgs/parameters.png)

The decompiled text will be appended to the prompt and will be sent as a completion request to the OpenAI API.

### Known issues

#### Ghidra looks even worse than usual when run via IDE
## Potential Future Work

The run configuration only includes the class loader VM option, and none of the others that are usually set by the
Ghidra launch script, which include OpenGL settings and Font Anti Aliasing, because this depends on the OS.

Generate the VM options for your system:
```sh
cd $GHIDRA_INSTALL_DIR
java -cp ./support/LaunchSupport.jar LaunchSupport ./support/.. -vmargs
```

and then [edit the run configuration](https://www.jetbrains.com/help/idea/run-debug-configuration.html) and add them.
No promises that I will ever implement them, but I'll gladly provide advice if you want to try:
* Dedicated Dialog that previews the function summary and allows retrying before applying
* Allow a custom prompt to be specified for a specific _program_ via the `Options for $program` menu
* Request better variable or function names like https://github.com/JusticeRage/Gepetto does
15 changes: 11 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,27 @@ else {
}
//----------------------END "DO NOT MODIFY" SECTION-------------------------------

// Set the JVM target to 11, as described in https://stackoverflow.com/a/44297713/13220684
// Ghidra requires 11 and the buildExtension.gradle sets this for Java
// Set the JVM target to 17, as described in https://stackoverflow.com/a/44297713/13220684
// Ghidra requires 17 and the buildExtension.gradle sets this for Java
// IntelliJ will complain about the discrepancy between the Java and the Kotlin target otherwise
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
jvmTarget = "11"
jvmTarget = "17"
}
}

dependencies {
// If you are using the Ghidra Jupyter Plugin for Kotlin, add the following line to declare it as a dependency
// This allows using the extension methods and makes sure that the required Kotlin libraries are present and not
// conflicting
// api fileTree(dir: ghidraInstallDir + '/Ghidra/Extensions/GhidraJupyterKotlin/', include: "**/*.jar")
api fileTree(dir: ghidraInstallDir + '/Ghidra/Extensions/GhidraJupyterKotlin/', include: "**/*.jar")

// OpenAI API
implementation "com.aallam.openai:openai-client:2.0.0"
// implementation platform('com.aallam.openai:openai-client-bom:2.0.0')
// implementation 'io.ktor:ktor-client:1.5.4'
// implementation 'io.ktor:ktor-client-jvm:1.5.4'
implementation 'io.ktor:ktor-client-java:2.1.0'
}

// Make it explicit that the compilation depends on some libraries in the `lib` folder,
Expand Down
4 changes: 2 additions & 2 deletions extension.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=@extname@
description=Kotlin Example Extension.
description=OpenAI Integration Proof of Concept.
author=Florian Magin
createdOn=2021-12-15
createdOn=2022-12-06
version=@extversion@
Binary file added imgs/API_key.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/parameters.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 0 additions & 24 deletions src/main/kotlin/ghidra/examples/HelloWorldService.kt

This file was deleted.

Loading

0 comments on commit fe45f13

Please sign in to comment.