Skip to content

Commit

Permalink
Add 'get_current_interpreter()'
Browse files Browse the repository at this point in the history
  • Loading branch information
dc3-tsd committed Jun 7, 2022
1 parent 8922254 commit 7365f99
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ __pycache__
build/
dist/
*.egg-info
.pylintrc

# ghidra files from tests
*.rep
Expand Down
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Changelog

## Unreleased
- corrected server jvm lib locating for openjdk on macs
## [Unreleased]
- Corrected server JVM library locating for openjdk on MAC
- Ignore unmatched lines in application.properties
- Prevent parsing of application.properties on import.
- Added `get_current_interpreter()` function to detect and retrieve the interpreter within the Ghidra GUI.

## 0.1.3 - 2022-03-30
- Corrected server libjvm locating
Expand Down
29 changes: 29 additions & 0 deletions pyhidra/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,32 @@ def gui():
sys.exit()
import pyhidra
pyhidra.GuiPyhidraLauncher().start()


def get_current_interpreter():
"""
Gets the underlying GhidraScript for the focused Pyhidra InteractiveConsole.
This will always return None unless it is being access from a function
called from within the interactive console.
:return: The GhidraScript for the active interactive console.
"""

try:
from ghidra.framework.main import AppInfo
project = AppInfo.getActiveProject()
if project is None:
return None
ts = project.getToolServices()
tool = None
for t in ts.getRunningTools():
if t.getActiveWindow().isFocused():
tool = t
break
if tool is None:
return None
for plugin in tool.getManagedPlugins():
if plugin.name == 'PyhidraPlugin':
return plugin.script
except ImportError:
return None
4 changes: 2 additions & 2 deletions pyhidra/java/plugin/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from pyhidra.java.plugin.plugin import PyPhidraPlugin
from pyhidra.javac import java_compile
from pyhidra.version import CURRENT_APPLICATION, ExtensionDetails
from pyhidra.version import get_current_application, ExtensionDetails



Expand All @@ -27,7 +27,7 @@ def install():
"""
Install the plugin in Ghidra
"""
path = CURRENT_APPLICATION.extension_path / "pyhidra"
path = get_current_application().extension_path / "pyhidra"
ext = path / "extension.properties"
manifest = path / "Module.manifest"
if not manifest.exists():
Expand Down
10 changes: 5 additions & 5 deletions pyhidra/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from . import __version__
from .constants import LAUNCH_PROPERTIES, LAUNCHSUPPORT, GHIDRA_INSTALL_DIR, UTILITY_JAR
from .version import CURRENT_APPLICATION, CURRENT_GHIDRA_VERSION, MINIMUM_GHIDRA_VERSION, \
from .version import get_current_application, get_ghidra_version, MINIMUM_GHIDRA_VERSION, \
ExtensionDetails


Expand Down Expand Up @@ -97,7 +97,7 @@ def _report_fatal_error(cls, title: str, msg: str) -> NoReturn:

@classmethod
def _update(cls):
ext = CURRENT_APPLICATION.extension_path / "pyhidra" / "extension.properties"
ext = get_current_application().extension_path / "pyhidra" / "extension.properties"
if ext.exists():
details = ExtensionDetails(ext)
if details.pyhidra < __version__:
Expand All @@ -116,11 +116,11 @@ def check_ghidra_version(cls):
Checks if the currently installed Ghidra version is supported.
The launcher will report the problem and terminate if it is not supported.
"""
if CURRENT_GHIDRA_VERSION < MINIMUM_GHIDRA_VERSION:
if get_ghidra_version() < MINIMUM_GHIDRA_VERSION:
cls._report_fatal_error(
"Unsupported Version",
textwrap.dedent(f"""\
Ghidra version {CURRENT_GHIDRA_VERSION} is not supported
Ghidra version {get_ghidra_version()} is not supported
The minimum required version is {MINIMUM_GHIDRA_VERSION}
""").rstrip()
)
Expand Down Expand Up @@ -250,7 +250,7 @@ def _launch(self):
from ghidra import GhidraRun

if sys.platform == "win32":
appid = ctypes.c_wchar_p(CURRENT_APPLICATION.name)
appid = ctypes.c_wchar_p(get_current_application().name)
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(appid)
jpype.setupGuiEnvironment(lambda: GhidraRun().launch(self.layout, self.args))
try:
Expand Down
37 changes: 33 additions & 4 deletions pyhidra/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,40 @@ def extension_path(self) -> Path:
return root / f"{root.name}_{self.version}_{self.release_name}" / "Extensions"


CURRENT_APPLICATION = ApplicationInfo()
CURRENT_GHIDRA_VERSION = CURRENT_APPLICATION.version
_CURRENT_APPLICATION: ApplicationInfo = None
_CURRENT_GHIDRA_VERSION: str = None
MINIMUM_GHIDRA_VERSION = "10.1.1"


def get_current_application() -> ApplicationInfo:
global _CURRENT_APPLICATION
if _CURRENT_APPLICATION is None:
_CURRENT_APPLICATION = ApplicationInfo()
return _CURRENT_APPLICATION


def get_ghidra_version() -> str:
global _CURRENT_GHIDRA_VERSION
if _CURRENT_GHIDRA_VERSION is None:
_CURRENT_GHIDRA_VERSION = get_current_application().version
return _CURRENT_GHIDRA_VERSION


_EXTENSION_DEFAULTS: dict = None

def _get_extension_defaults() -> dict:
global _EXTENSION_DEFAULTS
if _EXTENSION_DEFAULTS is None:
_EXTENSION_DEFAULTS = {
"name": "pyhidra",
"description": "Native Python Plugin",
"author": "Department of Defense Cyber Crime Center (DC3)",
"createdOn": "",
"version": get_ghidra_version(),
"pyhidra": __version__
}
return _EXTENSION_DEFAULTS

def _properties_wrapper(cls):
@functools.wraps(cls)
def wrapper(ext: Union[Path, dict] = None):
Expand All @@ -67,7 +96,7 @@ def cast(key, value):
return cls.__annotations__[key](value)

if ext is None:
return cls()
return cls(_get_extension_defaults())
lines = ext.read_text().splitlines()
args = tuple(starmap(cast, map(lambda l: l.split('='), lines)))
return cls(*args)
Expand All @@ -84,7 +113,7 @@ class ExtensionDetails(NamedTuple):
description: str = "Native Python Plugin"
author: str = "Department of Defense Cyber Crime Center (DC3)"
createdOn: str = ""
version: str = CURRENT_GHIDRA_VERSION
version: str = None
pyhidra: str = __version__

def __repr__(self):
Expand Down
4 changes: 2 additions & 2 deletions pyhidra/win_shortcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sysconfig
from pathlib import Path
from pyhidra.constants import GHIDRA_INSTALL_DIR
from pyhidra.version import CURRENT_APPLICATION
from pyhidra.version import get_current_application


# creating a shortcut with the winapi to have a set app id is trivial right?
Expand Down Expand Up @@ -39,7 +39,7 @@ def __init__(self, key: str, pid: int) -> None:
_COINIT_APARTMENTTHREADED = 2
_COINIT_DISABLE_OLE1DDE = 4
_VT_LPWSTR = 31
_APP_ID = CURRENT_APPLICATION.name
_APP_ID = get_current_application().name

WINFUNCTYPE = ctypes.WINFUNCTYPE
_CoCreateInstance = ctypes.oledll.ole32.CoCreateInstance
Expand Down

0 comments on commit 7365f99

Please sign in to comment.