about summary refs log tree commit diff
path: root/frida_mode/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'frida_mode/src/main.c')
-rw-r--r--frida_mode/src/main.c139
1 files changed, 25 insertions, 114 deletions
diff --git a/frida_mode/src/main.c b/frida_mode/src/main.c
index 91687046..1ab9993f 100644
--- a/frida_mode/src/main.c
+++ b/frida_mode/src/main.c
@@ -1,5 +1,4 @@
 #include <errno.h>
-#include <fcntl.h>
 #include <unistd.h>
 #include <sys/types.h>
 
@@ -11,15 +10,14 @@
   #include <sys/personality.h>
 #endif
 
-#include "frida-gumjs.h"
+#include "frida-gum.h"
 
 #include "config.h"
 #include "debug.h"
 
 #include "entry.h"
 #include "instrument.h"
-#include "intercept.h"
-#include "js.h"
+#include "interceptor.h"
 #include "lib.h"
 #include "output.h"
 #include "persistent.h"
@@ -29,8 +27,6 @@
 #include "stats.h"
 #include "util.h"
 
-#define PROC_MAX 65536
-
 #ifdef __APPLE__
 extern mach_port_t mach_task_self();
 extern GumAddress  gum_darwin_find_entrypoint(mach_port_t task);
@@ -45,6 +41,13 @@ typedef int *(*main_fn_t)(int argc, char **argv, char **envp);
 
 static main_fn_t main_fn = NULL;
 
+static int on_fork(void) {
+
+  prefetch_read();
+  return fork();
+
+}
+
 #ifdef __APPLE__
 static void on_main_os(int argc, char **argv, char **envp) {
 
@@ -75,7 +78,7 @@ static void on_main_os(int argc, char **argv, char **envp) {
 
 #endif
 
-static void embedded_init(void) {
+static void embedded_init() {
 
   static gboolean initialized = false;
   if (!initialized) {
@@ -87,117 +90,25 @@ static void embedded_init(void) {
 
 }
 
-static void afl_print_cmdline(void) {
-
-  char * buffer = g_malloc0(PROC_MAX);
-  gchar *fname = g_strdup_printf("/proc/%d/cmdline", getppid());
-  int    fd = open(fname, O_RDONLY);
-
-  if (fd < 0) {
-
-    WARNF("Failed to open /proc/self/cmdline, errno: (%d)", errno);
-    return;
-
-  }
-
-  ssize_t bytes_read = read(fd, buffer, PROC_MAX - 1);
-  if (bytes_read < 0) {
-
-    FATAL("Failed to read /proc/self/cmdline, errno: (%d)", errno);
-
-  }
-
-  int idx = 0;
-
-  for (ssize_t i = 0; i < bytes_read; i++) {
-
-    if (i == 0 || buffer[i - 1] == '\0') {
-
-      OKF("AFL - COMMANDLINE: argv[%d] = %s", idx++, &buffer[i]);
-
-    }
-
-  }
-
-  close(fd);
-  g_free(fname);
-  g_free(buffer);
-
-}
-
-static void afl_print_env(void) {
-
-  char * buffer = g_malloc0(PROC_MAX);
-  gchar *fname = g_strdup_printf("/proc/%d/environ", getppid());
-  int    fd = open(fname, O_RDONLY);
-
-  if (fd < 0) {
-
-    WARNF("Failed to open /proc/self/cmdline, errno: (%d)", errno);
-    return;
-
-  }
-
-  ssize_t bytes_read = read(fd, buffer, PROC_MAX - 1);
-  if (bytes_read < 0) {
-
-    FATAL("Failed to read /proc/self/cmdline, errno: (%d)", errno);
-
-  }
-
-  int idx = 0;
-
-  for (ssize_t i = 0; i < bytes_read; i++) {
-
-    if (i == 0 || buffer[i - 1] == '\0') {
-
-      OKF("AFL - ENVIRONMENT %3d: %s", idx++, &buffer[i]);
-
-    }
-
-  }
-
-  close(fd);
-  g_free(fname);
-  g_free(buffer);
-
-}
-
-__attribute__((visibility("default"))) void afl_frida_start(void) {
-
-  afl_print_cmdline();
-  afl_print_env();
-
-  /* Configure */
-  entry_config();
-  instrument_config();
-  js_config();
-  lib_config();
-  output_config();
-  persistent_config();
-  prefetch_config();
-  ranges_config();
-  stalker_config();
-  stats_config();
-
-  js_start();
-
-  /* Initialize */
-  output_init();
+void afl_frida_start() {
 
   embedded_init();
+  stalker_init();
+  lib_init();
   entry_init();
   instrument_init();
-  lib_init();
+  output_init();
   persistent_init();
   prefetch_init();
-  stalker_init();
   ranges_init();
   stats_init();
 
-  /* Start */
+  void *fork_addr =
+      GSIZE_TO_POINTER(gum_module_find_export_by_name(NULL, "fork"));
+  intercept(fork_addr, on_fork, NULL);
+
   stalker_start();
-  entry_start();
+  entry_run();
 
 }
 
@@ -205,7 +116,7 @@ static int *on_main(int argc, char **argv, char **envp) {
 
   on_main_os(argc, argv, envp);
 
-  intercept_unhook_self();
+  unintercept_self();
 
   afl_frida_start();
 
@@ -219,7 +130,7 @@ extern int *main(int argc, char **argv, char **envp);
 static void intercept_main(void) {
 
   main_fn = main;
-  intercept_hook(main, on_main, NULL);
+  intercept(main, on_main, NULL);
 
 }
 
@@ -232,7 +143,7 @@ static void intercept_main(void) {
   OKF("Entry Point: 0x%016" G_GINT64_MODIFIER "x", entry);
   void *main = GSIZE_TO_POINTER(entry);
   main_fn = main;
-  intercept_hook(main, on_main, NULL);
+  intercept(main, on_main, NULL);
 
 }
 
@@ -243,8 +154,8 @@ static int on_libc_start_main(int *(main)(int, char **, char **), int argc,
                               void(*stack_end)) {
 
   main_fn = main;
-  intercept_unhook_self();
-  intercept_hook(main, on_main, NULL);
+  unintercept_self();
+  intercept(main, on_main, NULL);
   return __libc_start_main(main, argc, ubp_av, init, fini, rtld_fini,
                            stack_end);
 
@@ -252,7 +163,7 @@ static int on_libc_start_main(int *(main)(int, char **, char **), int argc,
 
 static void intercept_main(void) {
 
-  intercept_hook(__libc_start_main, on_libc_start_main, NULL);
+  intercept(__libc_start_main, on_libc_start_main, NULL);
 
 }