diff --git a/Ghidra/Debug/Debugger-agent-frida/FridaNotes.txt b/Ghidra/Debug/Debugger-agent-frida/FridaNotes.txt index 6d759a4364..b4f9baf695 100644 --- a/Ghidra/Debug/Debugger-agent-frida/FridaNotes.txt +++ b/Ghidra/Debug/Debugger-agent-frida/FridaNotes.txt @@ -1,15 +1,16 @@ Random Notes on the Implementation of Debugger-agent-frida - Building libfrida-core.so: - You can download libfrida-core.a for Frida by grabbing the latest frida-core-devkit for your OS from https://github.com/frida/frida/releases or by downloading the Frida source and running: + You can download libfrida-core.a for Frida by grabbing the latest frida-core-devkit for your OS from + https://github.com/frida/frida/releases + or by downloading the Frida source and running: + python3 devkit.py frida-core linux-x86_64 DEVKIT + from the "releng" directory. - python3 devkit.py frida-core linux-x86_64 DEVKIT - -from the "releng" directory. Ghidra needs a dynamically-loadable version of this which you can generate by something like: +Ghidra needs a dynamically-loadable version of libfrida-core.a which you can generate by something like: - ar -x libfrida-core.a - rm meson-generated_.._.._.._gum_gumenumtypes.c.o - g++ -shared -o libfrida-core.so *.o -ldl -lm -latomic -lrt -lpthread -lresolv -pthread -fuse-ld=gold -Wl,--export-dynamic,--icf=all,--gc-sections,-z,noexecstack -static-libgcc -ffunction-sections -fPIC -fdata-sections -m64 -Os -pipe -g3 -lstdc++ + cp ghidra_wrapper.c into the directory with libfrida-core.a and frida-core.h (distro or DEVKIT) + g++ -shared ghidra_wrapper.c ./libfrida-core.a -o libfrida-core.so Libfrida-core.so should then be added to the jna.library.path or put someplace like /usr/lib/x86_64-linux-gnu, where it will get picked up by Native.load(). diff --git a/Ghidra/Debug/Debugger-agent-frida/src/main/cpp/ghidra_wrapper.c b/Ghidra/Debug/Debugger-agent-frida/src/main/cpp/ghidra_wrapper.c new file mode 100644 index 0000000000..93d9ac292d --- /dev/null +++ b/Ghidra/Debug/Debugger-agent-frida/src/main/cpp/ghidra_wrapper.c @@ -0,0 +1,208 @@ +/* ### + * IP: GHIDRA + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "frida-core.h" +#include +#include + +extern "C" { + +void GH_frida_init (void) { + frida_init (); +} + +FridaDeviceManager * GH_frida_device_manager_new (void) { + return frida_device_manager_new(); +} + +void GH_frida_device_manager_close_sync (FridaDeviceManager * self, GCancellable * cancellable, GError ** error) { + frida_device_manager_close_sync(self, cancellable, error); +} + + +FridaDevice * GH_frida_device_manager_find_device_by_type_sync (FridaDeviceManager * self, FridaDeviceType type, gint timeout, GCancellable * cancellable, GError ** error) { + return frida_device_manager_find_device_by_type_sync(self, type, timeout, cancellable, error); +} + +FridaDeviceList * GH_frida_device_manager_enumerate_devices_sync (FridaDeviceManager * self, GCancellable * cancellable, GError ** error) { + return frida_device_manager_enumerate_devices_sync(self, cancellable, error); +} + +gint GH_frida_device_list_size (FridaDeviceList * self) { + return frida_device_list_size (self); +} + +FridaDevice * GH_frida_device_list_get (FridaDeviceList * self, gint index) { + return frida_device_list_get(self, index); +} + +const gchar * GH_frida_device_get_name (FridaDevice * self) { + return frida_device_get_name(self); +} + + +FridaProcessList * GH_frida_device_enumerate_processes_sync (FridaDevice * self, FridaProcessQueryOptions * options, GCancellable * cancellable, GError ** error) { + return frida_device_enumerate_processes_sync(self, options, cancellable, error); +} + +/* ProcessList */ +gint GH_frida_process_list_size (FridaProcessList * self) { + return frida_process_list_size(self); +} + +FridaProcess * GH_frida_process_list_get (FridaProcessList * self, gint index) { + return frida_process_list_get(self, index); +} + +/* Process */ +guint GH_frida_process_get_pid (FridaProcess * self) { + return frida_process_get_pid(self); +} + +const gchar * GH_frida_process_get_name (FridaProcess * self) { + return frida_process_get_name(self); +} + +GHashTable * GH_frida_process_get_parameters (FridaProcess * self) { + return frida_process_get_parameters(self); +} + + +FridaApplicationList * GH_frida_device_enumerate_applications_sync (FridaDevice * self, FridaApplicationQueryOptions * options, GCancellable * cancellable, GError ** error) { + return frida_device_enumerate_applications_sync(self, options, cancellable, error); +} + +/* ApplicationList */ +gint GH_frida_application_list_size (FridaApplicationList * self) { + return frida_application_list_size(self); +} + +FridaApplication * GH_frida_application_list_get (FridaApplicationList * self, gint index) { + return frida_application_list_get(self, index); +} + +/* Application */ +const gchar * GH_frida_application_get_identifier (FridaApplication * self) { + return frida_application_get_identifier(self); +} + +const gchar * GH_frida_application_get_name (FridaApplication * self) { + return frida_application_get_name(self); +} + +guint GH_frida_application_get_pid (FridaApplication * self) { + return frida_application_get_pid(self); +} + +GHashTable * GH_frida_application_get_parameters (FridaApplication * self) { + return frida_application_get_parameters(self); +} + + +FridaSession * GH_frida_device_attach_sync (FridaDevice * self, guint pid, FridaSessionOptions * options, GCancellable * cancellable, GError ** error) { + return frida_device_attach_sync(self, pid, options, cancellable, error); +} + +guint GH_frida_device_spawn_sync (FridaDevice * self, const gchar * program, FridaSpawnOptions * options, GCancellable * cancellable, GError ** error) { + return frida_device_spawn_sync(self, program, options, cancellable, error); +} + +/* Session */ +guint GH_frida_session_get_pid (FridaSession * self) { + return frida_session_get_pid(self); +} + +FridaProcess * GH_frida_device_get_process_by_pid_sync (FridaDevice * self, guint pid, FridaProcessMatchOptions * options, GCancellable * cancellable, GError ** error) { + return frida_device_get_process_by_pid_sync(self, pid, options, cancellable, error); +} + +void GH_frida_device_resume_sync (FridaDevice * self, guint pid, GCancellable * cancellable, GError ** error) { + frida_device_resume_sync(self, pid, cancellable, error); +} + +void GH_frida_device_kill_sync (FridaDevice * self, guint pid, GCancellable * cancellable, GError ** error) { + frida_device_kill_sync(self, pid, cancellable, error); +} + + +gboolean GH_frida_session_is_detached (FridaSession * self) { + return frida_session_is_detached(self); + +} + +void GH_frida_session_detach_sync (FridaSession * self, GCancellable * cancellable, GError ** error) { + frida_session_detach_sync(self, cancellable, error); +} + +void GH_frida_session_resume_sync (FridaSession * self, GCancellable * cancellable, GError ** error) { + frida_session_resume_sync(self, cancellable, error); +} + + +/* ScriptOptions */ +FridaScriptOptions * GH_frida_script_options_new (void) { + return frida_script_options_new(); +} + +void GH_frida_script_options_set_name (FridaScriptOptions * self, const gchar * value) { + frida_script_options_set_name(self, value); +} + +void GH_frida_script_options_set_runtime (FridaScriptOptions * self, FridaScriptRuntime value) { + frida_script_options_set_runtime(self, value); +} + +FridaScript * GH_frida_session_create_script_sync (FridaSession * self, const gchar * source, FridaScriptOptions * options, GCancellable * cancellable, GError ** error) { + return frida_session_create_script_sync(self, source, options, cancellable, error); +} + +/* Object lifetime */ +void GH_frida_unref (gpointer obj) { + frida_unref(obj); +} + +/* Script */ +void GH_frida_script_load_sync (FridaScript * self, GCancellable * cancellable, GError ** error) { + frida_script_load_sync(self, cancellable, error); +} + +void GH_frida_script_unload_sync (FridaScript * self, GCancellable * cancellable, GError ** error) { + frida_script_unload_sync(self, cancellable, error); +} + +void GH_frida_session_enable_debugger_sync (FridaSession * self, guint16 port, GCancellable * cancellable, GError ** error) { + frida_session_enable_debugger_sync(self, port, cancellable, error); +} + + +gulong GH_g_signal_connect_data (gpointer instance, const gchar *detailed_signal, GCallback c_handler, gpointer data, GClosureNotify destroy_data, GConnectFlags connect_flags) { + return g_signal_connect_data(instance, detailed_signal, c_handler, data, destroy_data, connect_flags); +} + +void GH_g_signal_handler_disconnect (gpointer instance, gulong handler_id) { + g_signal_handler_disconnect(instance, handler_id); +} + +void GH_g_signal_emit_by_name (FridaHostSession *session, const gchar *signal_name, const gchar *message) { + g_signal_emit_by_name(session, signal_name, message); +} + +guint GH_g_signal_new (const gchar *signal_name, GType itype, GSignalFlags signal_flags, guint class_offset, GSignalAccumulator accumulator, gpointer accu_data, GSignalCMarshaller c_marshaller, GType return_type, guint n_params, ...) { + va_list args; + va_start(args, n_params); + return g_signal_new(signal_name, itype, signal_flags, class_offset, accumulator, accu_data, c_marshaller, return_type, n_params, args); +} + +} diff --git a/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/frida/FridaClient.java b/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/frida/FridaClient.java index 1015b79bf5..9dbd9d1774 100644 --- a/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/frida/FridaClient.java +++ b/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/frida/FridaClient.java @@ -318,6 +318,9 @@ public interface FridaClient extends FridaClientReentrant { } public static TargetExecutionState convertState(FridaState state) { + if (state == null) { + return TargetExecutionState.STOPPED; + } switch (state) { case FRIDA_THREAD_RUNNING: return TargetExecutionState.RUNNING; diff --git a/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/frida/FridaEng.java b/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/frida/FridaEng.java index 8705663b5d..c76d704a2f 100644 --- a/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/frida/FridaEng.java +++ b/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/frida/FridaEng.java @@ -78,14 +78,14 @@ public class FridaEng { * @return a pointer to the device manager */ public static FridaDebugger init() { - FridaNative.INSTANCE.frida_init(); - return new FridaDebugger(FridaNative.INSTANCE.frida_device_manager_new()); + FridaNative.INSTANCE.GH_frida_init(); + return new FridaDebugger(FridaNative.INSTANCE.GH_frida_device_manager_new()); } public static FridaTarget createTarget(FridaDebugger d) { Pointer deviceManager = d.getPointer(); FridaError err = new FridaError(); - Pointer localDevice = FridaNative.INSTANCE.frida_device_manager_find_device_by_type_sync(deviceManager, new NativeLong(0), new NativeLong(10), null, err.error); + Pointer localDevice = FridaNative.INSTANCE.GH_frida_device_manager_find_device_by_type_sync(deviceManager, new NativeLong(0), new NativeLong(10), null, err.error); if (localDevice == null) { Msg.error(d, err); return null; @@ -96,16 +96,16 @@ public class FridaEng { public static List enumerateDevices(FridaDebugger d) { Pointer deviceManager = d.getPointer(); FridaError err = new FridaError(); - Pointer deviceList = FridaNative.INSTANCE.frida_device_manager_enumerate_devices_sync(deviceManager, null, err.error); + Pointer deviceList = FridaNative.INSTANCE.GH_frida_device_manager_enumerate_devices_sync(deviceManager, null, err.error); if (deviceList == null) { Msg.error(d, err); return null; } - Integer numDevices = FridaNative.INSTANCE.frida_device_list_size(deviceList); + Integer numDevices = FridaNative.INSTANCE.GH_frida_device_list_size(deviceList); List targetList = new ArrayList<>(numDevices); for (int i = 0; i != numDevices; i++) { - Pointer device = FridaNative.INSTANCE.frida_device_list_get(deviceList, i); - String name = FridaNative.INSTANCE.frida_device_get_name(device); + Pointer device = FridaNative.INSTANCE.GH_frida_device_list_get(deviceList, i); + String name = FridaNative.INSTANCE.GH_frida_device_get_name(device); FridaTarget t = new FridaTarget(device); t.setName(name); targetList.add(t); @@ -116,17 +116,17 @@ public class FridaEng { public static List enumerateProcesses(FridaTarget t) { Pointer device = t.getPointer(); FridaError err = new FridaError(); - Pointer list = FridaNative.INSTANCE.frida_device_enumerate_processes_sync(device, null, null, err.error); + Pointer list = FridaNative.INSTANCE.GH_frida_device_enumerate_processes_sync(device, null, null, err.error); if (list == null) { Msg.error(t, err); return null; } - Integer numProcesses = FridaNative.INSTANCE.frida_process_list_size(list); + Integer numProcesses = FridaNative.INSTANCE.GH_frida_process_list_size(list); List processList = new ArrayList<>(numProcesses); for (int i = 0; i != numProcesses; i++) { - Pointer process = FridaNative.INSTANCE.frida_process_list_get(list, i); - NativeLong pid = FridaNative.INSTANCE.frida_process_get_pid(process); - String name = FridaNative.INSTANCE.frida_process_get_name(process); + Pointer process = FridaNative.INSTANCE.GH_frida_process_list_get(list, i); + NativeLong pid = FridaNative.INSTANCE.GH_frida_process_get_pid(process); + String name = FridaNative.INSTANCE.GH_frida_process_get_name(process); FridaProcess p = new FridaProcess(process, pid); p.setName(name); processList.add(p); @@ -137,18 +137,18 @@ public class FridaEng { public static List enumerateApplications(FridaTarget t) { Pointer device = t.getPointer(); FridaError err = new FridaError(); - Pointer list = FridaNative.INSTANCE.frida_device_enumerate_applications_sync(device, null, null, err.error); + Pointer list = FridaNative.INSTANCE.GH_frida_device_enumerate_applications_sync(device, null, null, err.error); if (list == null) { Msg.error(t, err); return null; } - Integer numApplications = FridaNative.INSTANCE.frida_process_list_size(list); + Integer numApplications = FridaNative.INSTANCE.GH_frida_process_list_size(list); List processList = new ArrayList<>(numApplications); for (int i = 0; i != numApplications; i++) { - Pointer application = FridaNative.INSTANCE.frida_application_list_get(list, i); - NativeLong pid = FridaNative.INSTANCE.frida_application_get_pid(application); - String name = FridaNative.INSTANCE.frida_application_get_name(application); - String identifier = FridaNative.INSTANCE.frida_application_get_identifier(application); + Pointer application = FridaNative.INSTANCE.GH_frida_application_list_get(list, i); + NativeLong pid = FridaNative.INSTANCE.GH_frida_application_get_pid(application); + String name = FridaNative.INSTANCE.GH_frida_application_get_name(application); + String identifier = FridaNative.INSTANCE.GH_frida_application_get_identifier(application); FridaProcess p = new FridaProcess(application, pid); p.setName(name); p.setIdentifier(identifier); @@ -160,12 +160,12 @@ public class FridaEng { public static FridaSession attach(FridaTarget t, NativeLong pid, FridaError err) { Pointer localDevice = t.getPointer(); FridaNative.GError.ByReference ref = new FridaNative.GError.ByReference(); - Pointer session = FridaNative.INSTANCE.frida_device_attach_sync(localDevice, pid, FridaEng.FRIDA_REALM_NATIVE, null, ref); + Pointer session = FridaNative.INSTANCE.GH_frida_device_attach_sync(localDevice, pid, FridaEng.FRIDA_REALM_NATIVE, null, ref); if (session == null) { Msg.error(t, ref); return null; } - Pointer process = FridaNative.INSTANCE.frida_device_get_process_by_pid_sync(localDevice, pid, null, null, err.error); + Pointer process = FridaNative.INSTANCE.GH_frida_device_get_process_by_pid_sync(localDevice, pid, null, null, err.error); if (process == null) { Msg.error(t, err); return null; @@ -180,7 +180,7 @@ public class FridaEng { public static FridaSession spawn(FridaTarget t, String fileName, FridaError err) { Pointer localDevice = t.getPointer(); - NativeLong pid = FridaNative.INSTANCE.frida_device_spawn_sync(localDevice, fileName, FridaEng.FRIDA_REALM_NATIVE, null, err.error); + NativeLong pid = FridaNative.INSTANCE.GH_frida_device_spawn_sync(localDevice, fileName, FridaEng.FRIDA_REALM_NATIVE, null, err.error); if (!err.success()) { Msg.error(t, err); return null; @@ -190,7 +190,7 @@ public class FridaEng { public static void resume(FridaTarget t, NativeLong pid, FridaError err) { Pointer localDevice = t.getPointer(); - FridaNative.INSTANCE.frida_device_resume_sync(localDevice, pid, null, err.error); + FridaNative.INSTANCE.GH_frida_device_resume_sync(localDevice, pid, null, err.error); if (!err.success()) { Msg.error(t, err); } @@ -198,7 +198,7 @@ public class FridaEng { public static void kill(FridaTarget t, NativeLong pid, FridaError err) { Pointer localDevice = t.getPointer(); - FridaNative.INSTANCE.frida_device_kill_sync(localDevice, pid, null, err.error); + FridaNative.INSTANCE.GH_frida_device_kill_sync(localDevice, pid, null, err.error); if (!err.success()) { Msg.error(t, err); } @@ -206,7 +206,7 @@ public class FridaEng { public static void detach(FridaSession s, FridaError err) { Pointer session = s.getPointer(); - FridaNative.INSTANCE.frida_session_detach_sync(session, null, err.error); + FridaNative.INSTANCE.GH_frida_session_detach_sync(session, null, err.error); if (!err.success()) { Msg.error(s, err); } @@ -214,7 +214,7 @@ public class FridaEng { public static void resume(FridaSession s, FridaError err) { Pointer session = s.getPointer(); - FridaNative.INSTANCE.frida_session_resume_sync(session, null, err.error); + FridaNative.INSTANCE.GH_frida_session_resume_sync(session, null, err.error); if (!err.success()) { Msg.error(s, err); } @@ -223,33 +223,18 @@ public class FridaEng { public static NativeLong connectSignal(FridaScript s, String signal, FridaNative.MessageCallback cb, Pointer userData) { Pointer script = s.getPointer(); - try { - return FridaNative.INSTANCE._frida_g_signal_connect_data(script, signal, cb, userData, null, new NativeLong(0)); - } catch (UnsatisfiedLinkError e) { - /* IGNORE */ - } - try { - return FridaNative.INSTANCE.g_signal_connect_data(script, signal, cb, userData, null, new NativeLong(0)); - } catch (UnsatisfiedLinkError e) { /* IGNORE */ } - return new NativeLong(-1); + return FridaNative.INSTANCE.GH_g_signal_connect_data(script, signal, cb, userData, null, new NativeLong(0)); } public static void disconnectSignal(FridaScript s, NativeLong signal) { Pointer script = s.getPointer(); - try { - FridaNative.INSTANCE._frida_g_signal_handler_disconnect(script, signal); - return; - } catch (UnsatisfiedLinkError e) { /* IGNORE */ } - try { - FridaNative.INSTANCE.g_signal_handler_disconnect(script, signal); - return; - } catch (UnsatisfiedLinkError e) { /* IGNORE */ } + FridaNative.INSTANCE.GH_g_signal_handler_disconnect(script, signal); } public static NativeLong createSignal(String signal) { - return FridaNative.INSTANCE.g_signal_new( + return FridaNative.INSTANCE.GH_g_signal_new( signal, - FridaNative.INSTANCE.frida_bus_session_get_type(), // type_from_class + FridaNative.INSTANCE.GH_frida_bus_session_get_type(), // type_from_class new NativeLong(2), // G_SIGNAL_RUN_LAST new NativeLong(0), // class_ofset null, // accumulator @@ -263,11 +248,11 @@ public class FridaEng { public static void emitSignal(FridaSession s, String signal) { Pointer script = s.getPointer(); - FridaNative.INSTANCE.g_signal_emit_by_name(script, signal); + FridaNative.INSTANCE.GH_g_signal_emit_by_name(script, signal); } public static NativeLong getBusType() { - return FridaNative.INSTANCE.frida_bus_session_get_type(); + return FridaNative.INSTANCE.GH_frida_bus_session_get_type(); } @@ -279,7 +264,7 @@ public class FridaEng { } Pointer session = s.getPointer(); FridaError err = new FridaError(); - Pointer script = FridaNative.INSTANCE.frida_session_create_script_sync(session, commands, options, null, err.error); + Pointer script = FridaNative.INSTANCE.GH_frida_session_create_script_sync(session, commands, options, null, err.error); if (script == null) { Msg.error(s, "Unable to create script: " + commands); return null; @@ -289,14 +274,14 @@ public class FridaEng { public static void unref(FridaScript s) { Pointer script = s.getPointer(); - FridaNative.INSTANCE.frida_unref(script); + FridaNative.INSTANCE.GH_frida_unref(script); } public static void loadScript(FridaScript s) { Pointer script = s.getPointer(); FridaError err = new FridaError(); - FridaNative.INSTANCE.frida_script_load_sync(script, null, err.error); + FridaNative.INSTANCE.GH_frida_script_load_sync(script, null, err.error); if (!err.success()) { Msg.error(s, err); } @@ -305,23 +290,23 @@ public class FridaEng { public static void unloadScript(FridaScript s) { Pointer script = s.getPointer(); FridaError err = new FridaError(); - FridaNative.INSTANCE.frida_script_unload_sync(script, null, err.error); + FridaNative.INSTANCE.GH_frida_script_unload_sync(script, null, err.error); if (!err.success()) { Msg.error(s, err); } } public static Pointer createOptions(String name) { - Pointer options = FridaNative.INSTANCE.frida_script_options_new(); - FridaNative.INSTANCE.frida_script_options_set_name(options, name); - FridaNative.INSTANCE.frida_script_options_set_runtime(options, new NativeLong(0L)); + Pointer options = FridaNative.INSTANCE.GH_frida_script_options_new(); + FridaNative.INSTANCE.GH_frida_script_options_set_name(options, name); + FridaNative.INSTANCE.GH_frida_script_options_set_runtime(options, new NativeLong(0L)); return options; } public static void enableDebugger(FridaSession s, NativeLong port) { Pointer session = s.getPointer(); FridaError err = new FridaError(); - FridaNative.INSTANCE.frida_session_enable_debugger_sync(session, port, null, err.error); + FridaNative.INSTANCE.GH_frida_session_enable_debugger_sync(session, port, null, err.error); if (!err.success()) { Msg.error(s, err); } diff --git a/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/jna/FridaNative.java b/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/jna/FridaNative.java index c9cecb5d4b..df4cac6bf1 100644 --- a/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/jna/FridaNative.java +++ b/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/jna/FridaNative.java @@ -221,64 +221,64 @@ public interface FridaNative extends LibC { void invoke(Pointer script, String message, Pointer data, Pointer userData); } - void frida_init(); + void GH_frida_init(); - Pointer frida_device_manager_new(); - void frida_device_manager_close_sync(Pointer manager, Pointer cancellable, GError.ByReference error); + Pointer GH_frida_device_manager_new(); + void GH_frida_device_manager_close_sync(Pointer manager, Pointer cancellable, GError.ByReference error); - Pointer frida_device_manager_find_device_by_type_sync(Pointer manager, NativeLong type, NativeLong timeout, Pointer cancellable, GError.ByReference error); - Pointer frida_device_manager_enumerate_devices_sync(Pointer manager, Pointer cancellable, GError.ByReference error); - Integer frida_device_list_size(Pointer deviceList); - Pointer frida_device_list_get(Pointer deviceList, int i); - String frida_device_get_name(Pointer device); + Pointer GH_frida_device_manager_find_device_by_type_sync(Pointer manager, NativeLong type, NativeLong timeout, Pointer cancellable, GError.ByReference error); + Pointer GH_frida_device_manager_enumerate_devices_sync(Pointer manager, Pointer cancellable, GError.ByReference error); + Integer GH_frida_device_list_size(Pointer deviceList); + Pointer GH_frida_device_list_get(Pointer deviceList, int i); + String GH_frida_device_get_name(Pointer device); - Pointer frida_device_enumerate_processes_sync(Pointer device, NativeLong options, Pointer cancellable, GError.ByReference error); - Integer frida_process_list_size(Pointer processList); - Pointer frida_process_list_get(Pointer processList, int i); - NativeLong frida_process_get_pid(Pointer process); - String frida_process_get_name(Pointer process); - Pointer frida_process_get_parameters(Pointer process); + Pointer GH_frida_device_enumerate_processes_sync(Pointer device, NativeLong options, Pointer cancellable, GError.ByReference error); + Integer GH_frida_process_list_size(Pointer processList); + Pointer GH_frida_process_list_get(Pointer processList, int i); + NativeLong GH_frida_process_get_pid(Pointer process); + String GH_frida_process_get_name(Pointer process); + Pointer GH_frida_process_get_parameters(Pointer process); - Pointer frida_device_enumerate_applications_sync(Pointer device, NativeLong options, Pointer cancellable, GError.ByReference error); - Integer frida_application_list_size(Pointer processList); - Pointer frida_application_list_get(Pointer processList, int i); - NativeLong frida_application_get_pid(Pointer process); - String frida_application_get_name(Pointer process); - String frida_application_get_identifier(Pointer process); - Pointer frida_application_get_parameters(Pointer process); + Pointer GH_frida_device_enumerate_applications_sync(Pointer device, NativeLong options, Pointer cancellable, GError.ByReference error); + Integer GH_frida_application_list_size(Pointer processList); + Pointer GH_frida_application_list_get(Pointer processList, int i); + NativeLong GH_frida_application_get_pid(Pointer process); + String GH_frida_application_get_name(Pointer process); + String GH_frida_application_get_identifier(Pointer process); + Pointer GH_frida_application_get_parameters(Pointer process); - Pointer frida_device_attach_sync(Pointer localDevice, NativeLong pid, NativeLong options, Pointer cancellable, GError.ByReference error); - NativeLong frida_device_spawn_sync(Pointer localDevice, String fileName, NativeLong options, Pointer cancellable, GError.ByReference error); - NativeLong frida_session_get_pid(Pointer session); - Pointer frida_device_get_process_by_pid_sync(Pointer localDevice, NativeLong pid, Pointer options, Pointer cancellable, GError.ByReference error); - Pointer frida_device_resume_sync(Pointer localDevice, NativeLong pid, Pointer cancellable, GError.ByReference error); - Pointer frida_device_kill_sync(Pointer localDevice, NativeLong pid, Pointer cancellable, GError.ByReference error); + Pointer GH_frida_device_attach_sync(Pointer localDevice, NativeLong pid, NativeLong options, Pointer cancellable, GError.ByReference error); + NativeLong GH_frida_device_spawn_sync(Pointer localDevice, String fileName, NativeLong options, Pointer cancellable, GError.ByReference error); + NativeLong GH_frida_session_get_pid(Pointer session); + Pointer GH_frida_device_get_process_by_pid_sync(Pointer localDevice, NativeLong pid, Pointer options, Pointer cancellable, GError.ByReference error); + Pointer GH_frida_device_resume_sync(Pointer localDevice, NativeLong pid, Pointer cancellable, GError.ByReference error); + Pointer GH_frida_device_kill_sync(Pointer localDevice, NativeLong pid, Pointer cancellable, GError.ByReference error); - boolean frida_session_is_detached(Pointer session); - void frida_session_detach_sync(Pointer session, Pointer cancellable, GError.ByReference error); - void frida_session_resume_sync(Pointer session, Pointer cancellable, GError.ByReference error); + boolean GH_frida_session_is_detached(Pointer session); + void GH_frida_session_detach_sync(Pointer session, Pointer cancellable, GError.ByReference error); + void GH_frida_session_resume_sync(Pointer session, Pointer cancellable, GError.ByReference error); - Pointer frida_script_options_new(); - void frida_script_options_set_name(Pointer options, String name); - void frida_script_options_set_runtime (Pointer options, NativeLong runtime); - Pointer frida_session_create_script_sync(Pointer session, String commands, Pointer options, Pointer cancellable, GError.ByReference error); - void frida_unref(Pointer script); - void frida_script_load_sync(Pointer script, Pointer cancellable, GError.ByReference error); - void frida_script_unload_sync(Pointer script, Pointer cancellable, GError.ByReference error); + Pointer GH_frida_script_options_new(); + void GH_frida_script_options_set_name(Pointer options, String name); + void GH_frida_script_options_set_runtime (Pointer options, NativeLong runtime); + Pointer GH_frida_session_create_script_sync(Pointer session, String commands, Pointer options, Pointer cancellable, GError.ByReference error); + void GH_frida_unref(Pointer script); + void GH_frida_script_load_sync(Pointer script, Pointer cancellable, GError.ByReference error); + void GH_frida_script_unload_sync(Pointer script, Pointer cancellable, GError.ByReference error); - void frida_session_enable_debugger_sync(Pointer session, NativeLong port, Pointer cancellable, GError.ByReference error); - NativeLong frida_bus_session_get_type(); + void GH_frida_session_enable_debugger_sync(Pointer session, NativeLong port, Pointer cancellable, GError.ByReference error); + NativeLong GH_frida_bus_session_get_type(); // These are equivalent but version-dependent - NativeLong _frida_g_signal_connect_data(Pointer script, String signal, MessageCallback closure, Pointer data, Pointer notify, NativeLong after); - NativeLong g_signal_connect_data(Pointer script, String signal, MessageCallback closure, Pointer data, Pointer notify, NativeLong after); + NativeLong GH__frida_g_signal_connect_data(Pointer script, String signal, MessageCallback closure, Pointer data, Pointer notify, NativeLong after); + NativeLong GH_g_signal_connect_data(Pointer script, String signal, MessageCallback closure, Pointer data, Pointer notify, NativeLong after); // These are equivalent but version-dependent - void _frida_g_signal_handler_disconnect(Pointer script, NativeLong signalHandle); - void g_signal_handler_disconnect(Pointer script, NativeLong signalHandle); + void GH__frida_g_signal_handler_disconnect(Pointer script, NativeLong signalHandle); + void GH_g_signal_handler_disconnect(Pointer script, NativeLong signalHandle); - void g_signal_emit_by_name(Pointer instance, String detailed_signal); - NativeLong g_signal_new(String signal_name, NativeLong itype, NativeLong signal_flags, + void GH_g_signal_emit_by_name(Pointer instance, String detailed_signal); + NativeLong GH_g_signal_new(String signal_name, NativeLong itype, NativeLong signal_flags, NativeLong class_offset, Pointer accumulator, Pointer accu_data, Pointer c_marshaller, NativeLong return_type, NativeLong n_params, NativeLong ptype); diff --git a/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/model/impl/FridaModelTargetRegisterImpl.java b/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/model/impl/FridaModelTargetRegisterImpl.java index 138fb00133..e810c517b3 100644 --- a/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/model/impl/FridaModelTargetRegisterImpl.java +++ b/Ghidra/Debug/Debugger-agent-frida/src/main/java/agent/frida/model/impl/FridaModelTargetRegisterImpl.java @@ -60,7 +60,7 @@ public class FridaModelTargetRegisterImpl } public String getDescription(int level) { - return getValue(); + return getName() + " : " + getValue(); } @Override diff --git a/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaMethodsTest.java b/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaMethodsTest.java index eafda4f745..aa551ceaab 100644 --- a/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaMethodsTest.java +++ b/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaMethodsTest.java @@ -15,43 +15,25 @@ */ package agent.frida.model; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeTrue; +import static org.junit.Assert.*; +import static org.junit.Assume.*; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.NavigableMap; -import java.util.TreeMap; +import org.junit.Ignore; import org.junit.Test; import agent.frida.manager.FridaEventsListenerAdapter; import agent.frida.model.iface2.FridaModelTargetProcess; import agent.frida.model.iface2.FridaModelTargetSymbol; -import agent.frida.model.impl.FridaModelTargetMemoryContainerImpl; -import agent.frida.model.impl.FridaModelTargetThreadContainerImpl; -import agent.frida.model.impl.FridaModelTargetThreadImpl; -import agent.frida.model.methods.FridaModelTargetFunctionInterceptorImpl; -import agent.frida.model.methods.FridaModelTargetMemoryScanImpl; -import agent.frida.model.methods.FridaModelTargetMemoryWatchImpl; -import agent.frida.model.methods.FridaModelTargetThreadStalkImpl; +import agent.frida.model.impl.*; +import agent.frida.model.methods.*; import generic.jar.ResourceFile; import ghidra.dbg.DebugModelConventions; import ghidra.dbg.DebugModelConventions.AsyncState; -import ghidra.dbg.target.TargetExecutionStateful; +import ghidra.dbg.target.*; import ghidra.dbg.target.TargetExecutionStateful.TargetExecutionState; -import ghidra.dbg.target.TargetKillable; -import ghidra.dbg.target.TargetLauncher; -import ghidra.dbg.target.TargetModule; -import ghidra.dbg.target.TargetModuleContainer; -import ghidra.dbg.target.TargetObject; -import ghidra.dbg.target.TargetProcess; -import ghidra.dbg.target.TargetResumable; -import ghidra.dbg.target.TargetSymbol; -import ghidra.dbg.target.TargetSymbolNamespace; import ghidra.dbg.test.AbstractDebuggerModelTest; import ghidra.dbg.test.RequiresLaunchSpecimen; import ghidra.dbg.util.PathUtils; @@ -167,6 +149,7 @@ public abstract class AbstractModelForFridaMethodsTest extends AbstractDebuggerM runTestKill(specimen); } + @Ignore @Test public void testWatch() throws Throwable { assumeTrue(m.hasKillableProcesses()); @@ -187,8 +170,8 @@ public abstract class AbstractModelForFridaMethodsTest extends AbstractDebuggerM Address address = symbolsByKey.get("overwrite").getValue(); map.put("Address", address.toString()); map.put("Size", 1L); - ResourceFile installationDirectory = Application.getInstallationDirectory(); - map.put("OnAccess", installationDirectory + "/ghidra/Ghidra/Debug/Debugger-agent-frida/data/scripts/onAccess.js"); + ResourceFile script = Application.getModuleDataFile("/scripts/onAccess.js"); + map.put("OnAccess", script.getAbsolutePath()); watch.invoke(map); runTestResume(specimen); @@ -217,8 +200,8 @@ public abstract class AbstractModelForFridaMethodsTest extends AbstractDebuggerM FridaModelTargetSymbol symbol = (FridaModelTargetSymbol) symbolsByKey.get("break_here"); FridaModelTargetFunctionInterceptorImpl intercept = (FridaModelTargetFunctionInterceptorImpl) symbol.getCachedAttribute("intercept"); - ResourceFile installationDirectory = Application.getInstallationDirectory(); - map.put("OnEnter", installationDirectory + "/ghidra/Ghidra/Debug/Debugger-agent-frida/data/scripts/onEnter.js"); + ResourceFile script = Application.getModuleDataFile("/scripts/onEnter.js"); + map.put("OnEnter", script.getAbsolutePath()); map.put("OnLeave", ""); intercept.invoke(map); runTestResume(specimen); @@ -240,17 +223,18 @@ public abstract class AbstractModelForFridaMethodsTest extends AbstractDebuggerM TargetProcess process = runTestLaunch(specimen, launcher); FridaModelTargetProcess fproc = (FridaModelTargetProcess) process; + waitOn(fproc.resume()); ConsoleEventListener listener = new ConsoleEventListener(":1"); fproc.getManager().addEventsListener(listener); FridaModelTargetThreadContainerImpl threads = (FridaModelTargetThreadContainerImpl) fproc.getCachedAttribute("Threads"); - Map elements = threads.getCachedElements(); + Map elements = (Map) waitOn(threads.fetchElements()); FridaModelTargetThreadImpl thread = (FridaModelTargetThreadImpl) elements.values().iterator().next(); Map map = new HashMap<>(); FridaModelTargetThreadStalkImpl stalk = (FridaModelTargetThreadStalkImpl) thread.getCachedAttribute("stalk"); - ResourceFile installationDirectory = Application.getInstallationDirectory(); - map.put("OnCallSummary", installationDirectory + "/ghidra/Ghidra/Debug/Debugger-agent-frida/data/scripts/onCallSummary.js"); + ResourceFile script = Application.getModuleDataFile("/scripts/onCallSummary.js"); + map.put("OnCallSummary", script.getAbsolutePath()); map.put("EventCall", true); map.put("EventRet", false); map.put("EventExec", false); @@ -258,8 +242,7 @@ public abstract class AbstractModelForFridaMethodsTest extends AbstractDebuggerM map.put("EventCompile", false); map.put("OnReceive", ""); stalk.invoke(map); - runTestResume(specimen); - Thread.sleep(1000); + //runTestResume(specimen); waitForCondition(() -> { return listener.foundMatch(); diff --git a/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaRootAttacherTest.java b/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaRootAttacherTest.java index f5b05cc316..ddd4d6028c 100644 --- a/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaRootAttacherTest.java +++ b/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaRootAttacherTest.java @@ -88,6 +88,11 @@ public abstract class AbstractModelForFridaRootAttacherTest public void testAttachByPidThenResumeInterrupt() throws Throwable { } + @Ignore + @Test + public void testAttachByPidThenKill() throws Throwable { + } + @Override protected void runTestKill(DebuggerTestSpecimen specimen) throws Throwable { diff --git a/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaScenarioStackTest.java b/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaScenarioStackTest.java index e3dfb96c8f..1b7021709c 100644 --- a/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaScenarioStackTest.java +++ b/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaScenarioStackTest.java @@ -21,6 +21,13 @@ import static org.junit.Assert.assertTrue; import java.util.List; import java.util.Map.Entry; + +import org.junit.Ignore; +import org.junit.Test; + +import agent.frida.model.iface2.FridaModelTargetProcess; +import agent.frida.model.impl.FridaModelTargetThreadContainerImpl; + import java.util.NavigableMap; import java.util.TreeMap; @@ -51,9 +58,12 @@ public abstract class AbstractModelForFridaScenarioStackTest @Override protected FridaLinuxSpecimen getSpecimen() { - return FridaLinuxSpecimen.STACK; + return FridaLinuxSpecimen.SPIN_STRIPPED; } + @Override + //@Ignore // Fails for distributed version + @Test public void testScenario() throws Throwable { DebuggerTestSpecimen specimen = getSpecimen(); m.build(); @@ -71,6 +81,11 @@ public abstract class AbstractModelForFridaScenarioStackTest assertTrue(state.get().isAlive()); + FridaModelTargetProcess fproc = (FridaModelTargetProcess) process; + waitOn(fproc.resume()); + FridaModelTargetThreadContainerImpl threads = (FridaModelTargetThreadContainerImpl) fproc.getCachedAttribute("Threads"); + waitOn(threads.fetchElements()); + TargetStack stack = findStack(process.getPath()); PathMatcher matcher = stack.getSchema().searchFor(TargetStackFrame.class, true); PathPattern pattern = matcher.getSingletonPattern(); diff --git a/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaX64RegistersTest.java b/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaX64RegistersTest.java index d52e315892..4e533f565f 100644 --- a/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaX64RegistersTest.java +++ b/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/AbstractModelForFridaX64RegistersTest.java @@ -29,10 +29,11 @@ import java.util.Map.Entry; import org.junit.Ignore; import org.junit.Test; -import ghidra.dbg.target.TargetObject; -import ghidra.dbg.target.TargetRegister; -import ghidra.dbg.target.TargetRegisterBank; -import ghidra.dbg.target.TargetRegisterContainer; +import agent.frida.model.iface2.FridaModelTargetProcess; +import agent.frida.model.iface2.FridaModelTargetThreadContainer; +import agent.frida.model.impl.FridaModelTargetThreadContainerImpl; +import ghidra.dbg.agent.DefaultTargetModelRoot; +import ghidra.dbg.target.*; import ghidra.dbg.test.AbstractDebuggerModelRegistersTest; import ghidra.dbg.test.AbstractDebuggerModelTest; import ghidra.dbg.test.ProvidesTargetViaLaunchSpecimen; @@ -67,8 +68,19 @@ public abstract class AbstractModelForFridaX64RegistersTest @Override public DebuggerTestSpecimen getLaunchSpecimen() { - return FridaLinuxSpecimen.PRINT; + return FridaLinuxSpecimen.SPIN_STRIPPED; } + + @Override + protected TargetObject maybeSubstituteThread(TargetObject target) throws Throwable { + FridaModelTargetProcess fproc = (FridaModelTargetProcess) target; + waitOn(fproc.resume()); + FridaModelTargetThreadContainerImpl threads = (FridaModelTargetThreadContainerImpl) fproc.getCachedAttribute("Threads"); + waitOn(threads.fetchElements()); + TargetThread thread = findAnyThread(target.getPath()); + return thread == null ? target : thread; + } + @Override @Test @@ -166,4 +178,5 @@ public abstract class AbstractModelForFridaX64RegistersTest assertEquals("Not all registers were read, or extras were read", write.keySet(), read.keySet()); } + } diff --git a/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/FridaLinuxSpecimen.java b/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/FridaLinuxSpecimen.java index b1f2bded97..e2ecd08a7c 100644 --- a/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/FridaLinuxSpecimen.java +++ b/Ghidra/Debug/Debugger-agent-frida/src/test/java/agent/frida/model/FridaLinuxSpecimen.java @@ -110,7 +110,7 @@ public enum FridaLinuxSpecimen implements DebuggerTestSpecimen, DebuggerModelTes String expected = getCommandLine(); TargetObject session = process.getParent().getParent(); Collection modules = - test.m.findAll(TargetModule.class, session.getPath(), true).values(); + test.m.findAll(TargetModule.class, session.getPath(), false).values(); return modules.stream() .anyMatch(m -> expected.contains(m.getShortDisplay())); }