mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-03 09:49:23 +02:00
GP-5981: Giving Jython scripts invoked via runScript() a fresh set of
locals
This commit is contained in:
parent
6c1a1b4600
commit
03ee787325
2 changed files with 48 additions and 19 deletions
|
@ -50,8 +50,8 @@ public class GhidraJythonInterpreter extends InteractiveInterpreter {
|
||||||
private PyModule introspectModule;
|
private PyModule introspectModule;
|
||||||
private PyModule builtinModule;
|
private PyModule builtinModule;
|
||||||
private PyObject interrupt;
|
private PyObject interrupt;
|
||||||
private boolean scriptMethodsInjected;
|
|
||||||
private boolean cleanedUp;
|
private boolean cleanedUp;
|
||||||
|
private Stack<PyObject> localStack = new Stack<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a new GhidraPythonInterpreter instance.
|
* Gets a new GhidraPythonInterpreter instance.
|
||||||
|
@ -126,6 +126,7 @@ public class GhidraJythonInterpreter extends InteractiveInterpreter {
|
||||||
/**
|
/**
|
||||||
* Initializes/resets the python path to include all known Ghidra script paths.
|
* Initializes/resets the python path to include all known Ghidra script paths.
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
private void initializePythonPath() {
|
private void initializePythonPath() {
|
||||||
|
|
||||||
// Restore the python path back to default.
|
// Restore the python path back to default.
|
||||||
|
@ -359,24 +360,20 @@ public class GhidraJythonInterpreter extends InteractiveInterpreter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add public methods (only once). Ignore inner classes.
|
// Add public methods. Ignore inner classes.
|
||||||
//
|
//
|
||||||
// NOTE: We currently do not have a way to safely add protected methods. Disabling
|
// NOTE: We currently do not have a way to safely add protected methods. Disabling
|
||||||
// python.security.respectJavaAccessibility and adding in protected methods in the below
|
// python.security.respectJavaAccessibility and adding in protected methods in the below
|
||||||
// loop caused an InaccessibleObjectException for some users (relating to core Java
|
// loop caused an InaccessibleObjectException for some users (relating to core Java
|
||||||
// modules, not the GhidraScript class hierarchy).
|
// modules, not the GhidraScript class hierarchy).
|
||||||
if (!scriptMethodsInjected) {
|
for (Method method : scriptClass.getDeclaredMethods()) {
|
||||||
for (Method method : scriptClass.getDeclaredMethods()) {
|
if (!method.getName().contains("$") &&
|
||||||
if (!method.getName().contains("$") &&
|
Modifier.isPublic(method.getModifiers())) {
|
||||||
Modifier.isPublic(method.getModifiers())) {
|
method.setAccessible(true);
|
||||||
method.setAccessible(true);
|
setMethod(script, method);
|
||||||
setMethod(script, method);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scriptMethodsInjected = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -395,6 +392,29 @@ public class GhidraJythonInterpreter extends InteractiveInterpreter {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the interpreter's locals to a stack. These locals can be restored with the
|
||||||
|
* {@link #restoreLocals()} method.
|
||||||
|
*/
|
||||||
|
public void saveLocals() {
|
||||||
|
localStack.push(getLocals());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears the interpreter's locals
|
||||||
|
*/
|
||||||
|
public void clearLocals() {
|
||||||
|
setLocals(new PyStringMap());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restores the locals that were last saved with the {@link #saveLocals()} method, and removes
|
||||||
|
* them from the stack.
|
||||||
|
*/
|
||||||
|
public void restoreLocals() {
|
||||||
|
setLocals(localStack.pop());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a bound (callback/function pointer) method as a local variable in the interpreter.
|
* Sets a bound (callback/function pointer) method as a local variable in the interpreter.
|
||||||
*
|
*
|
||||||
|
@ -427,6 +447,8 @@ public class GhidraJythonInterpreter extends InteractiveInterpreter {
|
||||||
Msg.error(this,
|
Msg.error(this,
|
||||||
"Method " + methodName + " of " + obj + " attempting to shadow method " +
|
"Method " + methodName + " of " + obj + " attempting to shadow method " +
|
||||||
pyMethod.__func__ + " of " + pyMethod.__self__);
|
pyMethod.__func__ + " of " + pyMethod.__self__);
|
||||||
|
set(methodName, new PyMethod(new PyReflectedFunction(method), Py.java2py(obj),
|
||||||
|
Py.java2py(obj.getClass())));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!(pyMethod.__func__ instanceof PyReflectedFunction)) {
|
if (!(pyMethod.__func__ instanceof PyReflectedFunction)) {
|
||||||
|
|
|
@ -71,13 +71,20 @@ public class JythonScript extends GhidraScript {
|
||||||
updateStateFromVariables();
|
updateStateFromVariables();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ghidraScript instanceof JythonScript) {
|
try {
|
||||||
ghidraScript.set(scriptState);
|
interpreter.saveLocals();
|
||||||
JythonScript jythonScript = (JythonScript) ghidraScript;
|
interpreter.clearLocals();
|
||||||
interpreter.execFile(jythonScript.getSourceFile(), jythonScript);
|
if (ghidraScript instanceof JythonScript) {
|
||||||
|
ghidraScript.set(scriptState);
|
||||||
|
JythonScript jythonScript = (JythonScript) ghidraScript;
|
||||||
|
interpreter.execFile(jythonScript.getSourceFile(), jythonScript);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ghidraScript.execute(scriptState, getControls());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
finally {
|
||||||
ghidraScript.execute(scriptState, getControls());
|
interpreter.restoreLocals();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scriptState == state) {
|
if (scriptState == state) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue