From 3f51f16e5e7c460b5c744a24842994004b5a3b72 Mon Sep 17 00:00:00 2001 From: Murphy Date: Fri, 20 Aug 2021 18:10:39 +0200 Subject: [PATCH] Fix #328 - Add Package Name support when attaching to a process --- src/io_frida.c | 72 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 11 deletions(-) diff --git a/src/io_frida.c b/src/io_frida.c index 33813b05..7593764a 100644 --- a/src/io_frida.c +++ b/src/io_frida.c @@ -66,7 +66,8 @@ static void pending_cmd_free(RFPendingCmd * pending_cmd); static void perform_request_unlocked(RIOFrida *rf, JsonBuilder *builder, GBytes *data, GBytes **bytes); static void exec_pending_cmd_if_needed(RIOFrida * rf); static char *__system(RIO *io, RIODesc *fd, const char *command); -static char *resolve_bundleid(FridaDevice *device, GCancellable *cancellable, const char *appname); +static char *resolve_package_name_by_process_name(FridaDevice *device, GCancellable *cancellable, const char *appname); +static char *resolve_process_name_by_package_name(FridaDevice *device, GCancellable *cancellable, const char *bundleid); static int atopid(const char *maybe_pid, bool *valid); // event handlers @@ -308,12 +309,11 @@ static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) { // rf->device = get_device_manager (device_manager, "local", rf->cancellable, &error); goto error; } - if (lo->spawn) { - char *appid = resolve_bundleid (rf->device, rf->cancellable, lo->process_specifier); - if (appid) { + char *package_name = resolve_package_name_by_process_name (rf->device, rf->cancellable, lo->process_specifier); + if (package_name) { free (lo->process_specifier); - lo->process_specifier = appid; + lo->process_specifier = package_name; } // try to resolve it as an app name too char **argv = r_str_argv (lo->process_specifier, NULL); @@ -1308,9 +1308,18 @@ static bool resolve_process(FridaDevice *device, R2FridaLaunchOptions *lo, GCanc if (!lo->process_specifier) { return false; } - FridaProcess *process = frida_device_get_process_by_name_sync ( device, lo->process_specifier, 0, cancellable, &error); + if (error != NULL) { + error = NULL; + char *procname = resolve_process_name_by_package_name(device, cancellable, lo->process_specifier); + if (procname) { + free (lo->process_specifier); + lo->process_specifier = procname; + } + process = frida_device_get_process_by_name_sync ( + device, lo->process_specifier, 0, cancellable, &error); + } if (error != NULL) { if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { eprintf ("%s\n", error->message); @@ -1318,7 +1327,6 @@ static bool resolve_process(FridaDevice *device, R2FridaLaunchOptions *lo, GCanc g_error_free (error); return false; } - lo->pid = frida_process_get_pid (process); g_object_unref (process); @@ -1621,7 +1629,7 @@ static void dumpDevices(GCancellable *cancellable) { } -static char *resolve_bundleid(FridaDevice *device, GCancellable *cancellable, const char *appname) { +static char *resolve_package_name_by_process_name(FridaDevice *device, GCancellable *cancellable, const char *process_name) { char *res = NULL; int count = 0; FridaApplicationList *list; @@ -1630,7 +1638,7 @@ static char *resolve_bundleid(FridaDevice *device, GCancellable *cancellable, co GError *error; if (r2f_debug ()) { - printf ("resolve-bundleid\n"); + printf ("resolve_package_name_by_process_name\n"); return NULL; } @@ -1648,8 +1656,8 @@ static char *resolve_bundleid(FridaDevice *device, GCancellable *cancellable, co for (i = 0; i != num_applications; i++) { FridaApplication *application = frida_application_list_get (list, i); if (application) { - const char *an = frida_application_get_name (application); - if (!strcmp (appname, an)) { + const char *name = frida_application_get_name (application); + if (!strcmp (process_name, name)) { res = strdup (frida_application_get_identifier (application)); break; } @@ -1663,6 +1671,48 @@ static char *resolve_bundleid(FridaDevice *device, GCancellable *cancellable, co return res; } +static char *resolve_process_name_by_package_name(FridaDevice *device, GCancellable *cancellable, const char *bundleid) { + char *res = NULL; + int count = 0; + FridaApplicationList *list; + GArray *applications; + gint num_applications, i; + GError *error; + + if (r2f_debug ()) { + printf ("resolve_process_name_by_package_name\n"); + return NULL; + } + + error = NULL; + list = frida_device_enumerate_applications_sync (device, NULL, cancellable, &error); + if (error != NULL) { + if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { + eprintf ("error: %s\n", error->message); + } + goto beach; + } + num_applications = frida_application_list_size (list); + + applications = g_array_sized_new (FALSE, FALSE, sizeof (FridaApplication *), num_applications); + for (i = 0; i != num_applications; i++) { + FridaApplication *application = frida_application_list_get (list, i); + if (application) { + const char *bid = frida_application_get_identifier (application); + if (!strcmp (bundleid, bid)) { + res = strdup (frida_application_get_name (application)); + break; + } + g_object_unref (application); /* borrow it */ + } + } + +beach: + g_clear_error (&error); + g_clear_object (&list); + return res; +} + static int dumpApplications(FridaDevice *device, GCancellable *cancellable) { int count = 0; FridaApplicationList *list;