dod-cyber-crime-center / pyhidra

Pyhidra is a Python library that provides direct access to the Ghidra API within a native CPython interpreter using jpype.
Other
184 stars 16 forks source link

Using pyhidra with Ghidra non-headless #22

Closed Rwkeith closed 10 months ago

Rwkeith commented 1 year ago

Hello pyhidra developers and contributors!

I'd like to control the listing view of a binary I have open in ghidra and see it update in real time.

So I found that using the DeferredPyhidraLauncher is what I need, but displaying the actual binary is the current issue.

import pyhidra
from pyhidra.launcher import DeferredPyhidraLauncher 
launcher = DeferredPyhidraLauncher (verbose=True)
launcher.start()
from ghidra.program.flatapi import FlatProgramAPI
from ghidra.base.project import GhidraProject
import ghidra
launcher.initialize_ghidra(headless=False)

project = GhidraProject.openProject('/projectlocation/', 'projectName', True)
program = project.openProgram('/', 'my_program.bin', False)

This all works fine, but I see only Ghidra's project window, with no active project selected, even though the output indicates that the project and program was opened. I'd like to see the binary open also and was seeing how that could be done? Thank you.

Edit: Now I've been trying to create a CodeBrowser Tool doing something like

localToolChest = project.getProjectManager().activeProject.getLocalToolChest()
ltt = localToolChest.getToolTemplates()
ltt[0].createTool(project.project)

but any time I try to create a tool, I get a java exception, Calls to MenuManager must be in the Swing Thread!

Rwkeith commented 1 year ago

updated original post

goatshriek commented 1 year ago

I haven't had a chance to test this myself, but I stumbled across the CreateEmptyProgramScript that comes with Ghidra, which uses SwingUtilities.invokeAndWait to do some UI stuff. Perhaps it would allow you to get around the Swing errors? I believe you should be able to call it from a Pyhidra environment with an import?

Rwkeith commented 1 year ago

That looks like a promising lead, but I can't find a way to get a reference to state. state, currentAddress, goTo(), and friends seem to be from GhidraState and GhidraScript which I haven't found a way to access through the flatapi from pyhidra. If you know a way to do this please share! Thanks again for looking.

Edit:

So I was able to get past the swing thread error and successfully run this after running the above

from javax.swing import SwingUtilities
def myRunnable():
    ltt[0].createTool(project.project)

SwingUtilities.invokeAndWait(myRunnable)

While there was some output indicating success, INFO Packed database cache: /tmp/user-Ghidra/packed-db-cache (PackedDatabaseCache) , I didn't get a gui window to spawn, and I don't see a way of getting a returned object.

Edit2:

I was able to spawn an empty tool window through a reference to the workspace. Now just need to see a way for a CodeBrowser

wsArray = project.getProjectManager().activeProject.getToolManager().getWorkspaces()

from javax.swing import SwingUtilities

def myRunnable():
    wsArray[0].createTool()

SwingUtilities.invokeAndWait(myRunnable)

Edit3:

Was able to get a CodeBrowser window by

from javax.swing import SwingUtilities
def myRunnable():
    ws[0].runTool(ltt[0])
SwingUtilities.invokeAndWait(myRunnable)

Now need to find a way to set the CodeBrowser to be the program desired

dc3-tsd commented 1 year ago

Could you provide some more details about what the intent of your code is? We want to make sure that we have a good understanding of your use case to ensure that we can provide a meaningful response.