-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
How do you access the flat API in ghidra_scripts? #1919
Comments
When you start the Ghidra python interpreter or run a Ghidra python script, all of the methods and instance variables from GhidraScript and FlatProgram get injected into the environment. If you called toAddr right from the interpreter, it would work. The problem seems to be that when you import an outside module, that module doesn't know about the injected methods and variables. This surprises me, but it's possible that it's a limitation of Jython. |
I just remembered how to do this. Add the following import to
This will bring in everything the interpreter knows about. |
I wonder if a custom ClasspathPyImporter could be used to make this occur automagically. |
My 2c as a frequent Python user:
To be honest, this doesn't surprise me too much, as it is characteristic of Python that even global variables are still always scoped to a single module (in this case, (I see now that those scripts have special annotations like To me, the
My main concerns would be:
|
These comments are used to provide the Ghidra GUI with some metadata about the script. They don't affect the script's functionality at all. |
Not sure if its documented elsewhere, but at least this is shown in example scripts: https://github.com/NationalSecurityAgency/ghidra/blob/master/Ghidra/Features/Python/ghidra_scripts/external_module_callee.py and https://github.com/NationalSecurityAgency/ghidra/blob/master/Ghidra/Features/Python/ghidra_scripts/external_module_caller.py |
Thanks, I wrote those and totally forgot they existed. |
While explaining my initial idea I realized the method of detecting ghidra_scripts wouldn't work as GhidraScript isn't explicitly extended. 😂 Basically it didn't work the way I thought it did. |
Actually, I want to backtrack on this slightly: The It would be nice if there were a module that existed exclusively for the purpose of holding all of ghidra's injected names, similar to how all of Python's builtin names are available from the
and keep things a bit more portable. |
I tried to move a helper function into a script in
ghidra_scripts
, and am being defeated by what would seem to be the simplest possible thing. Following examples I see elsewhere online, I tried just writing the same code I would write in ghidra's python interpreter:ghidra_scripts/better.py
ghidra's interpreter
Okay, so the API funcs are not available to me for whatever reason. Maybe the examples I found were old? I locate the functions on
FlatProgramAPI
, however, they are instance methods, so if I want to use them in my module I need to create an instance first.ghidra's interpreter
Okay, so in order for my python module to create a
FlatProgramAPI
, it will need access tocurrentProgram
andmonitor
, which are... properties ofFlatProgramAPI
, making this a total bird-or-the-egg situation! What am I missing here?The text was updated successfully, but these errors were encountered: