Skip to content

Commit

Permalink
Fix #328 - Add Package Name support when attaching to a process
Browse files Browse the repository at this point in the history
  • Loading branch information
as0ler authored Aug 20, 2021
1 parent f694329 commit 3f51f16
Showing 1 changed file with 61 additions and 11 deletions.
72 changes: 61 additions & 11 deletions src/io_frida.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1308,17 +1308,25 @@ 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);
}
g_error_free (error);
return false;
}

lo->pid = frida_process_get_pid (process);
g_object_unref (process);

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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;
Expand Down

0 comments on commit 3f51f16

Please sign in to comment.