3

I read the documentation of headleass analyzer. It is used to perform analysis on existing binaries. I know that -postscript flag allows to enter the analysis script. I have a java script which works fine. But, I want to use python 3 for the analysis. I want to run that like:

./analyzeHeadless ghidra-project-directory -import binary-file -postscript yourpythonscript

Is it possible to run python script for the analysis? Also, is there any documentation available to do that?

Edit:

I made a following script and it does work fine (but it gives me not found errors for DecompInterface):

import ghidra.app.util.headless.HeadlessScript;
import ghidra.app.decompiler.ClangNode;
import ghidra.app.decompiler.ClangToken;
import ghidra.app.decompiler.ClangLine;
import ghidra.app.decompiler.ClangTokenGroup;
import ghidra.app.decompiler.DecompInterface;
import ghidra.app.decompiler.DecompileResults;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.CodeUnit;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.FunctionIterator;
import ghidra.program.model.listing.InstructionIterator;
import ghidra.program.model.listing.Program;
import ghidra.program.model.listing.Variable;
import ghidra.program.model.pcode.HighFunction;
import ghidra.program.model.pcode.HighSymbol;
import ghidra.program.model.pcode.HighVariable;
import ghidra.program.model.pcode.LocalSymbolMap;
import ghidra.program.model.pcode.PcodeOp;
import ghidra.program.model.pcode.Varnode;
import ghidra.program.model.symbol.Symbol;
import ghidra.program.model.symbol.Reference;
import ghidra.program.model.symbol.ReferenceIterator;
import ghidra.util.task.ConsoleTaskMonitor;

args = getScriptArgs()

print(args)

p = currentProgram
print(p)

filename = "/projects/zephyr/Ruturaj/ghidra_learning/" + p.getName() + ".txt"
print(filename)

#di = DecompInterface()
#print(di)
  • AFAIK Ghidra relies on Jython which does not support Python 3. You'll have to use Python 2. – Igor Skochinsky Jul 9 at 9:05
  • Thanks @IgorSkochinsky I'll note that down in the answer – Ruturaj Jul 9 at 15:12
3

I turns out that the python script can be used with headless analyzer script.

Using the following command I can run it, just like the java file:

./analyzeHeadless ghidra-project-directory -import binary-file -postscript yourpythonscript

Apparently all the classes defined for java can be used directly in the python script.

Particular class can be imported with from import statements, for example in java looks like:

import ghidra.program.model.listing.Variable;

And using python:

from ghidra.program.model.listing import Variable;

To import the variable class. The variable class can be imported just like java (see the import statement above). But, then you have to use the whole path name to access it. For example:

something = ghidra.program.model.listing.Variable()

So, in conclusion, the same script can be written using python by importing the classes without any issue. Hope this helps someone.

Edit:

As @igor said in the comment, it works only with python 2.7 (as ghidra relies on Jython). For python 3.x, something like ghidra_bridge can be used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.