Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop into interactive mode from analyzeHeadless #37

Closed
0ddc0de opened this issue Mar 27, 2023 · 2 comments
Closed

Drop into interactive mode from analyzeHeadless #37

0ddc0de opened this issue Mar 27, 2023 · 2 comments

Comments

@0ddc0de
Copy link

0ddc0de commented Mar 27, 2023

Hi,

First of all, thanks for this project!

I was wondering if dropping into pdb/ipdb, or a REPL from Ghidra's headless mode would be possible using Ghidrathon.

Just naively trying ipdb results in:

[...snip...]
INFO  SCRIPT: /src/checker/ghidra.py (HeadlessAnalyzer)
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/jep/java_import_hook.py", line 45, in __getattr__
    return super(module, self).__getattribute__(name)
AttributeError: module 'pdb' has no attribute 'Pdb'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/.ghidra/.ghidra_10.2.3_PUBLIC/Extensions/ghidrathon/data/python/jeprunscript.py", line 31, in jep_runscript
    exec(compile(source, path, "exec"), {**globals(), **additional_globals})
  File "/src/checker/ghidra.py", line 8, in <module>
    from IPython import embed
  File "/usr/lib/python3/dist-packages/IPython/__init__.py", line 55, in <module>
    from .core.application import Application
  File "/usr/lib/python3/dist-packages/IPython/core/application.py", line 25, in <module>
    from IPython.core import release, crashhandler
  File "/usr/lib/python3/dist-packages/IPython/core/crashhandler.py", line 27, in <module>
    from IPython.core import ultratb
  File "/usr/lib/python3/dist-packages/IPython/core/ultratb.py", line 112, in <module>
    from IPython.core import debugger
  File "/usr/lib/python3/dist-packages/IPython/core/debugger.py", line 123, in <module>
    from pdb import Pdb as OldPdb
  File "/usr/local/lib/python3.10/dist-packages/jep/java_import_hook.py", line 57, in __getattr__
    clazz = forName('{0}.{1}'.format(self.__name__, name))
ImportError: java.lang.ClassNotFoundException: pdb.Pdb
[...snip...]

Using

import code
code.interact(local={**globals(), **locals()})

results in a prompt, but the code is only interpreted when I leave the interactive session (e.g., CTRL+D):

[...snip...]
INFO  SCRIPT: /src/checker/ghidra.py (HeadlessAnalyzer)
>>> print("foobar")
>>> Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

now exiting InteractiveConsole...
foobar
[...snip...]

Is there any way to do this?

@mike-hunhoff
Copy link
Collaborator

Hi @0ddc0de - thank you for reaching out. TLDR; this unfortunately appears to be a bug in Ghidrathon. In theory, after fixing this bug what you are trying to do should work, although, I am uncertain without testing how dropping into a REPL may mess with headless execution flow.

Long winded explanation and notes:

Based on ImportError: java.lang.ClassNotFoundException: pdb.Pdb it appears Ghidra's ghidra.app.util.bin.format.pdb package is overshadowing Python's pdb module. This is likely due to Jep's import hook giving preference to Java over Python when resolving imports.

We should give preference to Python when executing Python code but still allow access to the overshadowed Java package. This likely requires wrapping Jep's ClassEnquirer as discussed here or modifying Jep's java_import_hook.py.

@0ddc0de
Copy link
Author

0ddc0de commented Apr 25, 2023

@mike-hunhoff, just tested with 47e343f, and dropped successfully into an ipdb REPL. Thank you very much for addressing this. I will close this issue.

@0ddc0de 0ddc0de closed this as completed Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants