Ghidra: How to import third-party .jar files used by my script

Created on 21 Apr 2019  路  7Comments  路  Source: NationalSecurityAgency/ghidra

Hi, I write a script, in scripts manager, for doing some de-obfuscation operations. This script needs some external utility classes defined in a third-party .jar file, but I don't know how to import these third-party .jar files used by my script. Does GHIDRA support it? If it is, please tell me how to do it, thanks.

Question

Most helpful comment

The approach I described assumed you are using our GhidraDev Eclipse plugin to create the Ghidra Module project, and the GhidraDev plugin to export that project as an extension.

All 7 comments

If you are writing and sharing a script that requires a 3rd party jar, the supported way to do this is to create a Ghidra module with your script in the module's ghidra_scripts directory and your jar in the module's lib diretory. You can then export your Ghidra module as a Ghidra extension (zip file) and deliver it that way. Just be mindful of the licensing of your jar file to make sure it's compatible.

If you are writing and sharing a script that requires a 3rd party jar, the supported way to do this is to create a Ghidra module with your script in the module's ghidra_scripts directory and your jar in the module's lib diretory. You can then export your Ghidra module as a Ghidra extension (zip file) and deliver it that way. Just be mindful of the licensing of your jar file to make sure it's compatible.

I also met the same problem, sorry I didn't understand your approach.
I loaded the third-party library through eclipse, and then it can be used normally, but it must be started from eclipse every time.
It seems that your method is better, I want to ask if there are any steps.....

The approach I described assumed you are using our GhidraDev Eclipse plugin to create the Ghidra Module project, and the GhidraDev plugin to export that project as an extension.

This is an old thread but hopefully you find this info useful.

I found that Ghidra checks adds the --classpath here https://github.com/NationalSecurityAgency/ghidra/blob/7b76afee937f2309ac1f4ebf17ab91980a6d6874/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java#L230-L231

If you trace that a bit, you'll see that getClassPath() adds everything in System.getProperty("java.class.path") . If you check that, you'll find that Ghidra adds all the .jars in the ~/Ghidra/* folder, but it also adds things from ~/.ghidra/.ghidra_9.1.2_PUBLIC/plugins (a local hidden Ghidra directory, not where Ghidra is installed).

Solution

Add all the external dependencies to ~/.ghidra/.ghidra_9.1.2_PUBLIC/plugins/. If you do that, all your scripts will have access to them.

Example

$ cd ghidra_9.X.X_PUBLIC

# You might or might not need to create this folder
$ mkdir -p ~/.ghidra/.ghidra_9.1.2_PUBLIC/plugins

$ ls ~/.ghidra/.ghidra_9.1.2_PUBLIC/plugins
(empty)

# Fails because it cannot find external library
$ ./support/analyzeHeadless ghidra://sample.ghidra.server.com:/PATH -postScript SCRIPT.java -process FILE -connect USER -p 
...
SCRIPT.java:1: error: package javax.json does not exist
import javax.json.*;
^
...

# Copy your external libraries to be in Ghidra's classpath
$ mv ~/javax.json.jar ~/.ghidra/.ghidra_9.1.2_PUBLIC/plugins
$ ls ~/.ghidra/.ghidra_9.1.2_PUBLIC/plugins
javax.json.jar

# This time it success
$ ./support/analyzeHeadless ghidra://sample.ghidra.server.com:/PATH -postScript SCRIPT.java -process FILE -connect USER -p 

This is an old thread but hopefully you find this info useful.

I found that Ghidra checks adds the --classpath here

https://github.com/NationalSecurityAgency/ghidra/blob/7b76afee937f2309ac1f4ebf17ab91980a6d6874/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java#L230-L231

If you trace that a bit, you'll see that getClassPath() adds everything in System.getProperty("java.class.path") . If you check that, you'll find that Ghidra adds all the .jars in the ~/Ghidra/* folder, but it also adds things from ~/.ghidra/.ghidra_9.1.2_PUBLIC/plugins.

Solution

My fix was to add all the external dependencies to ~/.ghidra/.ghidra_9.1.2_PUBLIC/plugins/. If you do that, all your scripts will have access to them.

Example

$ cd ghidra_9.X.X_PUBLIC

# You might or might not need to create this folder
$ mkdir -p ~/.ghidra/.ghidra_9.1.2_PUBLIC/plugins

$ ls ~/.ghidra/.ghidra_9.1.2_PUBLIC/plugins
(empty)

# Fails because it cannot find external library
$ ./support/analyzeHeadless ghidra://sample.ghidra.server.com:/PATH -postScript SCRIPT.java -process FILE -connect USER -p 
...
SCRIPT.java:1: error: package javax.json does not exist
import javax.json.*;
^
...

# Copy your external libraries to be in Ghidra's classpath
$ mv ~/javax.json.jar ~/.ghidra/.ghidra_9.1.2_PUBLIC/plugins
$ ls ~/.ghidra/.ghidra_9.1.2_PUBLIC/plugins
javax.json.jar

# This time it success
$ ./support/analyzeHeadless ghidra://sample.ghidra.server.com:/PATH -postScript SCRIPT.java -process FILE -connect USER -p 

Thanks. This is real a useful approach, if some one did not want to create a Ghidra Module project.

@dlmgary
thanks for the ~/.ghidra/.ghidra_9.1.2_PUBLIC/plugins TIPS saved me time digging into lunch script logic .

May i complement the first question ?
now that java.class.path is problems is solved

is there a similar folder path for java.library.path ?

printing on the console gives me those value java.library.path=/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib

i kinda looking for a portable user based solution since i think java >8 kinda don't like lib being added to jdk's lib cant remeber well... what was the story

and modding the lunch script would lead me to forget what i did on the next install and rerun of my script ... since JNI ERROS are not verbose => a pain to trouble shoots

@dlmgary
thanks for the ~/.ghidra/.ghidra_9.1.2_PUBLIC/plugins TIPS saved me time digging into lunch script logic .

May i complement the first question ?
now that java.class.path is problems is solved

is there a similar folder path for java.library.path ?

printing on the console gives me those value java.library.path=/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib

i kinda looking for a portable user based solution since i think java >8 kinda don't like lib being added to jdk's lib cant remeber well... what was the story

and modding the lunch script would lead me to forget what i did on the next install and rerun of my script ... since JNI ERROS are not verbose => a pain to trouble shoots

that was a silly question my JNI libs are already in /usr/lib . so no setup required ... ghidra lunch script relay good.
thanks NSA

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mumbel picture mumbel  路  29Comments

niedabao1 picture niedabao1  路  23Comments

woachk picture woachk  路  33Comments

0x6d696368 picture 0x6d696368  路  18Comments

astrelsky picture astrelsky  路  21Comments