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

getProjectRootFolder() from flatAPI does not seem to be working in 9.1 #1250

Closed
james-tate opened this issue Nov 15, 2019 · 8 comments
Closed
Assignees
Labels
Feature: Python Type: Bug Something isn't working
Milestone

Comments

@james-tate
Copy link

Describe the bug
A clear and concise description of the bug.
This does not seem to be working in https://ghidra.re/ghidra_docs/api/ghidra/program/flatapi/FlatProgramAPI.html#getProjectRootFolder()

To Reproduce
Steps to reproduce the behavior:

  1. Open Python inside of GUI
  2. Execute getProjectRootFolder() inside of Python window

Expected behavior
Similar functionality to https://ghidra.re/ghidra_docs/api/ghidra/app/script/GhidraScript.html#askProjectFolder(java.lang.String)

Screenshots

getProjectRootFolder()
Traceback (most recent call last):
File "python", line 1, in
NameError: name 'getProjectRootFolder' is not defined

Attachments
If applicable, please attach any files that caused problems or log files generated by the software.

Environment (please complete the following information):

  • OS: [Linux]
  • Java Version: [12.0]
  • Ghidra Version: [9.1]
@astrelsky
Copy link
Contributor

It is accessible from a GhidraScript but not from Python. At first I thought it was because it is a protected method but this does not appear to be the case as both currentProgram and monitor are protected members yet are accessible from python.

@ryanmkurtz ryanmkurtz self-assigned this Nov 18, 2019
@ryanmkurtz ryanmkurtz added the Type: Bug Something isn't working label Nov 18, 2019
@ryanmkurtz
Copy link
Collaborator

Here is the code that explains the behavior:

private void injectScriptHierarchy(PythonScript script) {
if (script == null) {
return;
}
// Loop though the script class hierarchy
for (Class<?> scriptClass = script.getClass(); scriptClass != Object.class; scriptClass =
scriptClass.getSuperclass()) {
// Add public and protected fields
for (Field field : scriptClass.getDeclaredFields()) {
if (Modifier.isPublic(field.getModifiers()) ||
Modifier.isProtected(field.getModifiers())) {
try {
field.setAccessible(true);
setVariable(field.getName(), field.get(script));
}
catch (IllegalAccessException iae) {
throw new AssertException("Unexpected security manager being used!");
}
}
}
// Add public methods only once. Ignore inner classes.
if (!scriptMethodsInjected) {
for (Method method : scriptClass.getDeclaredMethods()) {
if (!method.getName().contains("$") &&
Modifier.isPublic(method.getModifiers())) {
method.setAccessible(true);
setMethod(script, method);
}
}
}
}
scriptMethodsInjected = true;
}

Only public methods are getting added to the python environment. I can't think of a good reason why this is being done, since it should mimic the java environment as closely as possibly.

@astrelsky
Copy link
Contributor

astrelsky commented Nov 18, 2019

@ryanmkurtz that is what I had thought. It is strange though because currentProgram is a protected member yet it is accessible. It doesn't make sense to add protected members but not protected methods.

@ryanmkurtz
Copy link
Collaborator

Im working on a fix.

@ryanmkurtz
Copy link
Collaborator

Doh, the obvious/simple fix of just adding protected methods didn't quite work:

>>> getProjectRootFolder()
Suppressing exception: exceptions.SystemErrorSystemError: Attempted to make
Java method visible, but it isn't callable[method=getProjectRootFolder, class=class
ghidra.app.script.GhidraScript]

I'll have to dig into this more.

@ryanmkurtz
Copy link
Collaborator

Looks like it's Jython blocking it:

    public void addMethod(Method m) {
        // Only add public methods unless we're overriding
        if (!Modifier.isPublic(m.getModifiers()) && Options.respectJavaAccessibility) {
            return;
        }

Just have to figure out how to set Options.respectJavaAccessibility programmatically.

@astrelsky
Copy link
Contributor

astrelsky commented Nov 18, 2019

Looks like it's Jython blocking it:

    public void addMethod(Method m) {
        // Only add public methods unless we're overriding
        if (!Modifier.isPublic(m.getModifiers()) && Options.respectJavaAccessibility) {
            return;
        }

Just have to figure out how to set Options.respectJavaAccessibility programmatically.

It's not programmatically, but try specifying -D python.security.respectJavaAccessibility=false when launching ghidra. source

The problem here I think would be that it would always allow python to access protected/private methods/members and not just the ones in GhidraScript.

@ryanmkurtz
Copy link
Collaborator

It seems to be working. I'll put the fix in.

@ryanmkurtz ryanmkurtz added this to the 9.2 milestone Nov 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature: Python Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants