Instrument Ghidra GUI with Pyhidra : FrontEndTool.setActiveProject crash with Active Workspace cannot be null #7398
-
Hello, I try to instrument the Ghidra GUI in Python with Pyhidra: 1) I start Ghidra GUI with Pyhidra API, 2) I create a project in batch mode via Environment : INFO Using log config file: jar:file:/opt/third-party/ghidra_11.2.1_PUBLIC/Ghidra/Framework/Generic/lib/Generic.jar!/generic.log4j.xml (LoggingInitialization)
INFO Using log file: /home/cell/.config/ghidra/ghidra_11.2.1_PUBLIC/application.log (LoggingInitialization)
INFO Loading user preferences: /home/cell/.config/ghidra/ghidra_11.2.1_PUBLIC/preferences (Preferences)
INFO Searching for classes... (ClassSearcher)
INFO Class search complete (1756 ms) (ClassSearcher)
INFO Initializing SSL Context (SSLContextInitializer)
INFO Initializing Random Number Generator... (SecureRandomFactory)
INFO Random Number Generator initialization complete: NativePRNGNonBlocking (SecureRandomFactory)
INFO Trust manager disabled, cacerts have not been set (ApplicationTrustManagerFactory)
INFO User cell started Ghidra. (GhidraRun)
INFO User settings directory: /home/cell/.config/ghidra/ghidra_11.2.1_PUBLIC (GhidraRun)
INFO User temp directory: /tmp/cell-ghidra (GhidraRun)
INFO User cache directory: /var/tmp/cell-ghidra (GhidraRun)
INFO Ghidra startup complete (5822 ms) (GhidraRun)
INFO Creating project: /tmp/ghidra_frontend_test/ghidra_frontend_test (DefaultProject)
INFO Using Loader: Raw Binary (AutoImporter)
INFO Using Language/Compiler: ARM:LE:32:v4t:default (AutoImporter)
INFO Using Library Search Path: [., /bin, /lib, /lib64, /lib/x86_64-linux-gnu, /lib/aarch64-linux-gnu, /usr/bin, /usr/lib, /usr/X11R6/bin, /usr/X11R6/lib, /usr/java/packages/lib, /usr/lib64] (AutoImporter)
INFO Opening project: /tmp/ghidra_frontend_test/ghidra_frontend_test (DefaultProject)
Traceback (most recent call last):
File "EventDispatchThread.java", line 90, in java.awt.EventDispatchThread.run
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Active Workspace cannot be null
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "SwingUtilities.java", line 1480, in javax.swing.SwingUtilities.invokeAndWait
Exception: Java Exception
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/tmp/reproduce_bug.py", line 79, in <module>
SwingUtilities.invokeAndWait(set_active_project)
java.lang.reflect.InvocationTargetException: java.lang.reflect.InvocationTargetException My script: import threading
import time
import os
import shutil
import pathlib
import pyhidra
import jpype
def is_ghidra_gui_up() -> bool:
try:
if jpype.isJVMStarted():
from ghidra.framework import Application
from ghidra.framework.main import AppInfo
if Application.isInitialized() and AppInfo.getFrontEndTool():
return True
except:
pass
return False
################################################################################
# Setup ghidra GUI
################################################################################
threading.Thread(target=pyhidra.launcher.GuiPyhidraLauncher().start).start()
while not is_ghidra_gui_up():
time.sleep(0.02)
################################################################################
# Create project tree directory and binary file
################################################################################
work_dir = os.getcwd()
project_root_dir = f'{work_dir}{os.path.sep}ghidra_frontend_test'
dir_name = os.path.basename(project_root_dir)
bin_file = f'{project_root_dir}{os.path.sep}my.bin'
if os.path.exists(project_root_dir):
shutil.rmtree(project_root_dir)
os.makedirs(project_root_dir)
with open(bin_file, 'wb') as f:
f.write(b'\x00' * 8)
################################################################################
# Setup project in ghidra GUI
################################################################################
from ghidra.framework.main import AppInfo
from ghidra.base.project import GhidraProject
from ghidra.program.model.listing import Program
from ghidra.framework.model import ProjectLocator
from javax.swing import SwingUtilities
ghidra_project = GhidraProject.createProject(project_root_dir, dir_name, False)
lang = pyhidra.core._get_language('ARM:LE:32:v4t')
comp = pyhidra.core._get_compiler_spec(lang, None)
program = ghidra_project.importProgram(pathlib.Path(bin_file), lang, comp)
ghidra_project.saveAs(program, '/', program.getName(), True)
ghidra_project.save(program)
ghidra_project.close(program)
ghidra_project.getProject().close()
ghidra_project.close()
frontend_tool = AppInfo.getFrontEndTool()
project_loc = ProjectLocator(project_root_dir, os.path.basename(dir_name))
project = frontend_tool.getProjectManager().openProject(project_loc, False, False)
def set_active_project():
frontend_tool.setActiveProject(project)
SwingUtilities.invokeAndWait(set_active_project)
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Finally solved using only
|
Beta Was this translation helpful? Give feedback.
Finally solved using only
GhidraProject
and closing program to avoid warning message in the GUI.