Skip to content

Commit

Permalink
Port to the new Gum.Module API
Browse files Browse the repository at this point in the history
  • Loading branch information
oleavr committed Jan 6, 2025
1 parent a2ffb76 commit 9539493
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 80 deletions.
31 changes: 15 additions & 16 deletions lib/agent/agent.vala
Original file line number Diff line number Diff line change
Expand Up @@ -1509,13 +1509,14 @@ namespace Frida.Agent {
}

public static NativeBridgeApi open () throws Error {
string? nb_mod = null;
string? vm_mod = null;
Gum.Process.enumerate_modules ((details) => {
if (/\/lib(64)?\/libnativebridge.so$/.match (details.path))
nb_mod = details.path;
else if (/^lib(art|dvm).so$/.match (details.name) && !/\/system\/fake-libs/.match (details.path))
vm_mod = details.path;
Gum.Module? nb_mod = null;
Gum.Module? vm_mod = null;
Gum.Process.enumerate_modules (module => {
unowned string path = module.path;
if (/\/lib(64)?\/libnativebridge.so$/.match (path))
nb_mod = module;
else if (/^lib(art|dvm).so$/.match (module.name) && !/\/system\/fake-libs/.match (path))
vm_mod = module;
bool carry_on = nb_mod == null || vm_mod == null;
return carry_on;
});
Expand All @@ -1530,29 +1531,27 @@ namespace Frida.Agent {
NBUnloadLibraryFunc? unload;
NBGetTrampolineFunc get_trampoline;

load = (NBLoadLibraryFunc) Gum.Module.find_export_by_name (nb_mod, "NativeBridgeLoadLibrary");;
load = (NBLoadLibraryFunc) nb_mod.find_export_by_name ("NativeBridgeLoadLibrary");;
if (load != null) {
flavor = MODERN;
load_ext = (NBLoadLibraryExtFunc) Gum.Module.find_export_by_name (nb_mod, "NativeBridgeLoadLibraryExt");
load_ext = (NBLoadLibraryExtFunc) nb_mod.find_export_by_name ("NativeBridgeLoadLibraryExt");
// XXX: NativeBridgeUnloadLibrary() is only a stub as of Android 11 w/ libndk_translation.so
unload = null;
get_trampoline = (NBGetTrampolineFunc) Gum.Module.find_export_by_name (nb_mod,
"NativeBridgeGetTrampoline");
get_trampoline = (NBGetTrampolineFunc) nb_mod.find_export_by_name ("NativeBridgeGetTrampoline");
} else {
flavor = LEGACY;
load = (NBLoadLibraryFunc) Gum.Module.find_export_by_name (nb_mod,
"_ZN7android23NativeBridgeLoadLibraryEPKci");
load_ext = (NBLoadLibraryExtFunc) Gum.Module.find_export_by_name (nb_mod,
load = (NBLoadLibraryFunc) nb_mod.find_export_by_name ("_ZN7android23NativeBridgeLoadLibraryEPKci");
load_ext = (NBLoadLibraryExtFunc) nb_mod.find_export_by_name (
"_ZN7android26NativeBridgeLoadLibraryExtEPKciPNS_25native_bridge_namespace_tE");
// XXX: Unload implementation seems to be unreliable.
unload = null;
get_trampoline = (NBGetTrampolineFunc) Gum.Module.find_export_by_name (nb_mod,
get_trampoline = (NBGetTrampolineFunc) nb_mod.find_export_by_name (
"_ZN7android25NativeBridgeGetTrampolineEPvPKcS2_j");
}
if (load == null || get_trampoline == null)
throw new Error.NOT_SUPPORTED ("NativeBridge API is not available on this system");

var get_vms = (JNIGetCreatedJavaVMsFunc) Gum.Module.find_export_by_name (vm_mod, "JNI_GetCreatedJavaVMs");
var get_vms = (JNIGetCreatedJavaVMsFunc) vm_mod.find_export_by_name ("JNI_GetCreatedJavaVMs");
if (get_vms == null)
throw new Error.NOT_SUPPORTED ("Unable to locate Java VM");

Expand Down
35 changes: 19 additions & 16 deletions lib/payload/cloak.vala
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,18 @@ namespace Frida {
private delegate ssize_t ReadFunc (int fd, void * buf, size_t count);

construct {
Gum.Module.enumerate_imports ("libart.so", imp => {
if (imp.name == "read") {
read_slot = (ReadFunc *) imp.slot;
return false;
}
return true;
});
if (read_slot != null)
old_read_impl = update_read_slot (on_read);
var art = Gum.Process.find_module_by_name ("libart.so");
if (art != null) {
art.enumerate_imports (imp => {
if (imp.name == "read") {
read_slot = (ReadFunc *) imp.slot;
return false;
}
return true;
});
if (read_slot != null)
old_read_impl = update_read_slot (on_read);
}
}

~ThreadCountCloaker () {
Expand Down Expand Up @@ -212,34 +215,34 @@ namespace Frida {
construct {
var interceptor = Gum.Interceptor.obtain ();

unowned string libc = Gum.Process.query_libc_name ();
var libc = Gum.Process.get_libc_module ();

var open_listener = new OpenDirListener (this);
listeners.add (open_listener);
interceptor.attach ((void *) Gum.Module.find_export_by_name (libc, "opendir"), open_listener);
interceptor.attach ((void *) libc.find_export_by_name ("opendir"), open_listener);

var close_listener = new CloseDirListener (this);
listeners.add (close_listener);
interceptor.attach ((void *) Gum.Module.find_export_by_name (libc, "closedir"), close_listener);
interceptor.attach ((void *) libc.find_export_by_name ("closedir"), close_listener);

var readdir_impl = Gum.Module.find_export_by_name (libc, "readdir");
var readdir_impl = libc.find_export_by_name ("readdir");
var readdir_listener = new ReadDirListener (this, LEGACY);
listeners.add (readdir_listener);
interceptor.attach ((void *) readdir_impl, readdir_listener);

var readdir64_impl = Gum.Module.find_export_by_name (libc, "readdir64");
var readdir64_impl = libc.find_export_by_name ("readdir64");
if (readdir64_impl != 0 && readdir64_impl != readdir_impl) {
var listener = new ReadDirListener (this, MODERN);
listeners.add (listener);
interceptor.attach ((void *) readdir64_impl, listener);
}

var readdir_r_impl = Gum.Module.find_export_by_name (libc, "readdir_r");
var readdir_r_impl = libc.find_export_by_name ("readdir_r");
var readdir_r_listener = new ReadDirRListener (this, LEGACY);
listeners.add (readdir_r_listener);
interceptor.attach ((void *) readdir_r_impl, readdir_r_listener);

var readdir64_r_impl = Gum.Module.find_export_by_name (libc, "readdir64_r");
var readdir64_r_impl = libc.find_export_by_name ("readdir64_r");
if (readdir64_r_impl != 0 && readdir64_r_impl != readdir_r_impl) {
var listener = new ReadDirRListener (this, MODERN);
listeners.add (listener);
Expand Down
7 changes: 4 additions & 3 deletions lib/payload/exit-monitor.vala
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ namespace Frida {
unowned Gum.InvocationListener listener = this;

#if WINDOWS
interceptor.attach ((void *) Gum.Module.find_export_by_name ("kernel32.dll", "ExitProcess"), listener);
interceptor.attach ((void *) Gum.Process.find_module_by_name ("kernel32.dll").find_export_by_name ("ExitProcess"),
listener);
#else
unowned string libc = Gum.Process.query_libc_name ();
var libc = Gum.Process.get_libc_module ();
const string[] apis = {
"exit",
"_exit",
"abort",
};
foreach (var symbol in apis) {
interceptor.attach ((void *) Gum.Module.find_export_by_name (libc, symbol), listener);
interceptor.attach ((void *) libc.find_export_by_name (symbol), listener);
}
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion lib/payload/fd-guard.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Frida {
construct {
var interceptor = Gum.Interceptor.obtain ();

var close = Gum.Module.find_export_by_name (Gum.Process.query_libc_name (), "close");
var close = Gum.Process.get_libc_module ().find_export_by_name ("close");
close_listener = new CloseListener (this);
interceptor.attach ((void *) close, close_listener);
}
Expand Down
26 changes: 16 additions & 10 deletions lib/payload/fork-monitor.vala
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ namespace Frida {
}

static construct {
unowned string libc = Gum.Process.query_libc_name ();
fork_impl = (void *) Gum.Module.find_export_by_name (libc, "fork");
vfork_impl = (void *) Gum.Module.find_export_by_name (libc, "vfork");
var libc = Gum.Process.get_libc_module ();
fork_impl = (void *) libc.find_export_by_name ("fork");
vfork_impl = (void *) libc.find_export_by_name ("vfork");
}

construct {
Expand All @@ -61,15 +61,21 @@ namespace Frida {
string cmdline;
FileUtils.get_contents ("/proc/self/cmdline", out cmdline);
if (cmdline == "zygote" || cmdline == "zygote64" || cmdline == "usap32" || cmdline == "usap64") {
var set_argv0 = (void *) Gum.Module.find_export_by_name ("libandroid_runtime.so", "_Z27android_os_Process_setArgV0P7_JNIEnvP8_jobjectP8_jstring");
if (set_argv0 != null) {
interceptor.attach (set_argv0, listener, (void *) HookId.SET_ARGV0);
child_recovery_behavior = DEFERRED_UNTIL_SET_ARGV0;
var runtime = Gum.Process.find_module_by_name ("libandroid_runtime.so");
if (runtime != null) {
var set_argv0 = (void *) runtime.find_export_by_name ("_Z27android_os_Process_setArgV0P7_JNIEnvP8_jobjectP8_jstring");
if (set_argv0 != null) {
interceptor.attach (set_argv0, listener, (void *) HookId.SET_ARGV0);
child_recovery_behavior = DEFERRED_UNTIL_SET_ARGV0;
}
}

var setcontext = (void *) Gum.Module.find_export_by_name ("libselinux.so", "selinux_android_setcontext");
if (setcontext != null)
interceptor.attach (setcontext, listener, (void *) HookId.SET_CTX);
var selinux = Gum.Process.find_module_by_name ("libselinux.so");
if (selinux != null) {
var setcontext = (void *) selinux.find_export_by_name ("selinux_android_setcontext");
if (setcontext != null)
interceptor.attach (setcontext, listener, (void *) HookId.SET_CTX);
}
}
} catch (FileError e) {
}
Expand Down
27 changes: 15 additions & 12 deletions lib/payload/spawn-monitor.vala
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,35 @@ namespace Frida {
var interceptor = Gum.Interceptor.obtain ();

#if WINDOWS
var create_process_internal = Gum.Module.find_export_by_name ("kernelbase.dll", "CreateProcessInternalW");
if (create_process_internal == 0)
create_process_internal = Gum.Module.find_export_by_name ("kernel32.dll", "CreateProcessInternalW");
var kernelbase = Gum.Process.find_module_by_name ("kernelbase.dll");
var create_process_internal = (kernelbase != null) ? kernelbase.find_export_by_name ("CreateProcessInternalW") : 0;
if (create_process_internal == 0) {
create_process_internal = Gum.Process.find_module_by_name ("kernel32.dll")
.find_export_by_name ("CreateProcessInternalW");
}
assert (create_process_internal != 0);
interceptor.attach ((void *) create_process_internal, this);
#else
unowned string libc = Gum.Process.query_libc_name ();
var libc = Gum.Process.get_libc_module ();
#if DARWIN
posix_spawn = (PosixSpawnFunc) Gum.Module.find_export_by_name (libc, "posix_spawn");
posix_spawnattr_init = (PosixSpawnAttrInitFunc) Gum.Module.find_export_by_name (libc, "posix_spawnattr_init");
posix_spawnattr_destroy = (PosixSpawnAttrDestroyFunc) Gum.Module.find_export_by_name (libc, "posix_spawnattr_destroy");
posix_spawnattr_getflags = (PosixSpawnAttrSetFlagsFunc) Gum.Module.find_export_by_name (libc, "posix_spawnattr_getflags");
posix_spawnattr_setflags = (PosixSpawnAttrSetFlagsFunc) Gum.Module.find_export_by_name (libc, "posix_spawnattr_setflags");
posix_spawn = (PosixSpawnFunc) libc.find_export_by_name ("posix_spawn");
posix_spawnattr_init = (PosixSpawnAttrInitFunc) libc.find_export_by_name ("posix_spawnattr_init");
posix_spawnattr_destroy = (PosixSpawnAttrDestroyFunc) libc.find_export_by_name ("posix_spawnattr_destroy");
posix_spawnattr_getflags = (PosixSpawnAttrSetFlagsFunc) libc.find_export_by_name ("posix_spawnattr_getflags");
posix_spawnattr_setflags = (PosixSpawnAttrSetFlagsFunc) libc.find_export_by_name ("posix_spawnattr_setflags");

execve = (void *) Gum.Module.find_export_by_name (libc, "execve");
execve = (void *) libc.find_export_by_name ("execve");

interceptor.attach ((void *) posix_spawn, this);

interceptor.replace (execve, (void *) replacement_execve, this);
#else
Gum.Address execve = 0;
#if ANDROID
execve = Gum.Module.find_symbol_by_name (libc, "__execve");
execve = libc.find_symbol_by_name ("__execve");
#endif
if (execve == 0)
execve = Gum.Module.find_export_by_name (libc, "execve");
execve = libc.find_export_by_name ("execve");
interceptor.attach ((void *) execve, this);
#endif
#endif
Expand Down
7 changes: 4 additions & 3 deletions lib/payload/thread-suspend-monitor.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ namespace Frida {
construct {
var interceptor = Gum.Interceptor.obtain ();

task_threads = (TaskThreadsFunc) Gum.Module.find_export_by_name (LIBSYSTEM_KERNEL, "task_threads");
thread_suspend = (ThreadSuspendFunc) Gum.Module.find_export_by_name (LIBSYSTEM_KERNEL, "thread_suspend");
thread_resume = (ThreadResumeFunc) Gum.Module.find_export_by_name (LIBSYSTEM_KERNEL, "thread_resume");
var kernel = Gum.Process.find_module_by_name (LIBSYSTEM_KERNEL);
task_threads = (TaskThreadsFunc) kernel.find_export_by_name ("task_threads");
thread_suspend = (ThreadSuspendFunc) kernel.find_export_by_name ("thread_suspend");
thread_resume = (ThreadResumeFunc) kernel.find_export_by_name ("thread_resume");

interceptor.replace ((void *) task_threads, (void *) replacement_task_threads, this);
interceptor.replace ((void *) thread_suspend, (void *) replacement_thread_suspend, this);
Expand Down
16 changes: 12 additions & 4 deletions lib/payload/unwind-sitter-glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ static gpointer
frida_find_vtable (void)
{
GumAddress result = 0;
GumModule * libunwind;
GumAddress export;
uint64_t address;
G_GNUC_UNUSED cs_err err;
Expand All @@ -309,11 +310,15 @@ frida_find_vtable (void)
size_t size;
const size_t max_size = 2048;

export = gum_module_find_export_by_name (FRIDA_LIBUNWIND_PATH, "unw_init_local");
libunwind = gum_process_find_module_by_name (FRIDA_LIBUNWIND_PATH);
if (libunwind == NULL)
goto beach;

export = gum_module_find_export_by_name (libunwind, "unw_init_local");
if (export == 0)
export = gum_module_find_export_by_name (FRIDA_LIBUNWIND_PATH, "_Unwind_RaiseException");
export = gum_module_find_export_by_name (libunwind, "_Unwind_RaiseException");
if (export == 0)
return NULL;
goto beach;
export = gum_strip_code_address (export);
address = export;

Expand All @@ -340,7 +345,7 @@ frida_find_vtable (void)
GumMemoryRange bss_range;

bss_range.base_address = 0;
gum_module_enumerate_sections (FRIDA_LIBUNWIND_PATH, (GumFoundSectionFunc) frida_find_bss_range, &bss_range);
gum_module_enumerate_sections (libunwind, (GumFoundSectionFunc) frida_find_bss_range, &bss_range);

while (cs_disasm_iter (capstone, &code, &size, &address, insn))
{
Expand Down Expand Up @@ -404,6 +409,9 @@ frida_find_vtable (void)
cs_free (insn, 1);
cs_close (&capstone);

beach:
g_clear_object (&libunwind);

return GSIZE_TO_POINTER (result);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/payload/unwind-sitter.vala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Frida {
var interceptor = Gum.Interceptor.obtain ();

dyld_find_unwind_sections = (DyldFindUnwindSectionsFunc)
Gum.Module.find_export_by_name (LIBDYLD, "_dyld_find_unwind_sections");
Gum.Process.find_module_by_name (LIBDYLD).find_export_by_name ("_dyld_find_unwind_sections");

interceptor.replace ((void *) dyld_find_unwind_sections, (void *) replacement_dyld_find_unwind_sections, this);

Expand Down
14 changes: 7 additions & 7 deletions src/darwin/frida-helper-backend-glue.m
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@
static void frida_spawn_instance_unset_helpers (FridaSpawnInstance * self);
static void frida_spawn_instance_call_set_helpers (FridaSpawnInstance * self, GumDarwinUnifiedThreadState * state, mach_vm_address_t helpers);
static void frida_spawn_instance_call_dlopen (FridaSpawnInstance * self, GumDarwinUnifiedThreadState * state, mach_vm_address_t lib_name, int mode);
static gboolean frida_find_cf_initialize (const GumModuleDetails * details, gpointer user_data);
static gboolean frida_find_cf_initialize (GumModule * module, gpointer user_data);
static void frida_spawn_instance_call_cf_initialize (FridaSpawnInstance * self, GumDarwinUnifiedThreadState * state);
static void frida_spawn_instance_set_nth_breakpoint (FridaSpawnInstance * self, guint n, GumAddress break_at, FridaBreakpointRepeat repeat);
static void frida_spawn_instance_enable_nth_breakpoint (FridaSpawnInstance * self, guint n);
Expand Down Expand Up @@ -3824,15 +3824,15 @@ static void frida_darwin_helper_backend_launch_using_lsaw (NSString * identifier
}

static gboolean
frida_find_cf_initialize (const GumModuleDetails * details, gpointer user_data)
frida_find_cf_initialize (GumModule * module, gpointer user_data)
{
FridaSpawnInstance * self = user_data;
GumDarwinModule * core_foundation;

if (strcmp (details->path, CORE_FOUNDATION) != 0)
if (strcmp (gum_module_get_path (module), CORE_FOUNDATION) != 0)
return TRUE;

core_foundation = gum_darwin_module_new_from_memory (CORE_FOUNDATION, self->task, details->range->base_address,
core_foundation = gum_darwin_module_new_from_memory (CORE_FOUNDATION, self->task, gum_module_get_range (module)->base_address,
GUM_DARWIN_MODULE_FLAGS_NONE, NULL);

self->cf_initialize_address = gum_darwin_module_resolve_symbol_address (core_foundation, "___CFInitialize");
Expand Down Expand Up @@ -4291,7 +4291,7 @@ static void frida_darwin_helper_backend_launch_using_lsaw (NSString * identifier
{
GumDarwinModule * module;

module = gum_darwin_module_resolver_find_module (resolver, "/usr/lib/system/libsystem_kernel.dylib");
module = gum_darwin_module_resolver_find_module_by_name (resolver, "/usr/lib/system/libsystem_kernel.dylib");
if (module == NULL)
goto no_libc;
FRIDA_AGENT_CONTEXT_RESOLVE (mach_task_self);
Expand All @@ -4301,7 +4301,7 @@ static void frida_darwin_helper_backend_launch_using_lsaw (NSString * identifier
FRIDA_AGENT_CONTEXT_RESOLVE (mach_port_destroy);
FRIDA_AGENT_CONTEXT_RESOLVE (thread_terminate);

module = gum_darwin_module_resolver_find_module (resolver, "/usr/lib/system/libsystem_pthread.dylib");
module = gum_darwin_module_resolver_find_module_by_name (resolver, "/usr/lib/system/libsystem_pthread.dylib");
if (module == NULL)
goto no_libc;
FRIDA_AGENT_CONTEXT_TRY_RESOLVE (pthread_create_from_mach_thread);
Expand All @@ -4315,7 +4315,7 @@ static void frida_darwin_helper_backend_launch_using_lsaw (NSString * identifier

if (mapper == NULL)
{
module = gum_darwin_module_resolver_find_module (resolver, "/usr/lib/system/libdyld.dylib");
module = gum_darwin_module_resolver_find_module_by_name (resolver, "/usr/lib/system/libdyld.dylib");
if (module == NULL)
goto no_libc;
FRIDA_AGENT_CONTEXT_RESOLVE (dlopen);
Expand Down
Loading

0 comments on commit 9539493

Please sign in to comment.