aboutsummaryrefslogtreecommitdiff
path: root/frida_mode/src/persistent
diff options
context:
space:
mode:
authorWorksButNotTested <62701594+WorksButNotTested@users.noreply.github.com>2021-05-17 20:14:40 +0100
committerGitHub <noreply@github.com>2021-05-17 21:14:40 +0200
commitd0af55e78f85427983ddafd0af07dff654b3ea65 (patch)
treeae5dc5fc0164df2d02972322ca25fa1fca6d3893 /frida_mode/src/persistent
parente40c0c2da16f14dfddb5641f6f825903879534a9 (diff)
downloadafl++-d0af55e78f85427983ddafd0af07dff654b3ea65.tar.gz
Support for x86 (#920)
Co-authored-by: Your Name <you@example.com>
Diffstat (limited to 'frida_mode/src/persistent')
-rw-r--r--frida_mode/src/persistent/persistent_x86.c233
1 files changed, 228 insertions, 5 deletions
diff --git a/frida_mode/src/persistent/persistent_x86.c b/frida_mode/src/persistent/persistent_x86.c
index 9d39c4e9..bd7171b9 100644
--- a/frida_mode/src/persistent/persistent_x86.c
+++ b/frida_mode/src/persistent/persistent_x86.c
@@ -1,9 +1,9 @@
#include "frida-gum.h"
-#include "debug.h"
+#include "config.h"
+#include "instrument.h"
#include "persistent.h"
-#include "util.h"
#if defined(__i386__)
@@ -38,16 +38,239 @@ struct x86_regs {
typedef struct x86_regs arch_api_regs;
+static arch_api_regs saved_regs = {0};
+static void * saved_return = NULL;
+
gboolean persistent_is_supported(void) {
- return false;
+ return true;
+
+}
+
+static void instrument_persitent_save_regs(GumX86Writer * cw,
+ struct x86_regs *regs) {
+
+ GumAddress regs_address = GUM_ADDRESS(regs);
+
+ /* Should be pushing FPU here, but meh */
+ gum_x86_writer_put_pushfx(cw);
+ gum_x86_writer_put_push_reg(cw, GUM_REG_EAX);
+
+ gum_x86_writer_put_mov_reg_address(cw, GUM_REG_EAX, regs_address);
+
+ gum_x86_writer_put_mov_reg_offset_ptr_reg(cw, GUM_REG_EAX, (0x4 * 1),
+ GUM_REG_EBX);
+ gum_x86_writer_put_mov_reg_offset_ptr_reg(cw, GUM_REG_EAX, (0x4 * 2),
+ GUM_REG_ECX);
+ gum_x86_writer_put_mov_reg_offset_ptr_reg(cw, GUM_REG_EAX, (0x4 * 3),
+ GUM_REG_EDX);
+ gum_x86_writer_put_mov_reg_offset_ptr_reg(cw, GUM_REG_EAX, (0x4 * 4),
+ GUM_REG_EDI);
+ gum_x86_writer_put_mov_reg_offset_ptr_reg(cw, GUM_REG_EAX, (0x4 * 5),
+ GUM_REG_ESI);
+ gum_x86_writer_put_mov_reg_offset_ptr_reg(cw, GUM_REG_EAX, (0x4 * 6),
+ GUM_REG_EBP);
+
+ /* Store RIP */
+ gum_x86_writer_put_mov_reg_address(cw, GUM_REG_EBX,
+ GUM_ADDRESS(persistent_start));
+
+ gum_x86_writer_put_mov_reg_offset_ptr_reg(cw, GUM_REG_EAX, (0x4 * 7),
+ GUM_REG_EBX);
+
+ /* Store adjusted RSP */
+ gum_x86_writer_put_mov_reg_reg(cw, GUM_REG_EBX, GUM_REG_ESP);
+
+ /* RED_ZONE + Saved flags, RAX */
+ gum_x86_writer_put_add_reg_imm(cw, GUM_REG_EBX, (0x4 * 2));
+ gum_x86_writer_put_mov_reg_offset_ptr_reg(cw, GUM_REG_EAX, (0x4 * 8),
+ GUM_REG_EBX);
+
+ /* Save the flags */
+ gum_x86_writer_put_mov_reg_reg_offset_ptr(cw, GUM_REG_EBX, GUM_REG_ESP, 0x4);
+ gum_x86_writer_put_mov_reg_offset_ptr_reg(cw, GUM_REG_EAX, (0x4 * 9),
+ GUM_REG_EBX);
+
+ /* Save the RAX */
+ gum_x86_writer_put_mov_reg_reg_offset_ptr(cw, GUM_REG_EBX, GUM_REG_ESP, 0x0);
+ gum_x86_writer_put_mov_reg_offset_ptr_reg(cw, GUM_REG_EAX, (0x4 * 0),
+ GUM_REG_EBX);
+
+ /* Pop the saved values */
+ gum_x86_writer_put_lea_reg_reg_offset(cw, GUM_REG_ESP, GUM_REG_ESP, 0x8);
+
+}
+
+static void instrument_persitent_restore_regs(GumX86Writer * cw,
+ struct x86_regs *regs) {
+
+ GumAddress regs_address = GUM_ADDRESS(regs);
+ gum_x86_writer_put_mov_reg_address(cw, GUM_REG_EAX, regs_address);
+
+ gum_x86_writer_put_mov_reg_reg_offset_ptr(cw, GUM_REG_ECX, GUM_REG_EAX,
+ (0x4 * 2));
+ gum_x86_writer_put_mov_reg_reg_offset_ptr(cw, GUM_REG_EDX, GUM_REG_EAX,
+ (0x4 * 3));
+ gum_x86_writer_put_mov_reg_reg_offset_ptr(cw, GUM_REG_EDI, GUM_REG_EAX,
+ (0x4 * 4));
+ gum_x86_writer_put_mov_reg_reg_offset_ptr(cw, GUM_REG_ESI, GUM_REG_EAX,
+ (0x4 * 5));
+ gum_x86_writer_put_mov_reg_reg_offset_ptr(cw, GUM_REG_EBP, GUM_REG_EAX,
+ (0x4 * 6));
+
+ /* Don't restore RIP or RSP */
+
+ /* Restore RBX, RAX & Flags */
+ gum_x86_writer_put_mov_reg_reg_offset_ptr(cw, GUM_REG_EBX, GUM_REG_EAX,
+ (0x4 * 1));
+ gum_x86_writer_put_push_reg(cw, GUM_REG_EBX);
+
+ gum_x86_writer_put_mov_reg_reg_offset_ptr(cw, GUM_REG_EBX, GUM_REG_EAX,
+ (0x4 * 0));
+ gum_x86_writer_put_push_reg(cw, GUM_REG_EBX);
+ gum_x86_writer_put_mov_reg_reg_offset_ptr(cw, GUM_REG_EBX, GUM_REG_EAX,
+ (0x4 * 9));
+ gum_x86_writer_put_push_reg(cw, GUM_REG_EBX);
+
+ gum_x86_writer_put_popfx(cw);
+ gum_x86_writer_put_pop_reg(cw, GUM_REG_EAX);
+ gum_x86_writer_put_pop_reg(cw, GUM_REG_EBX);
+
+}
+
+static void instrument_save_ret(GumX86Writer *cw, void **saved_return_ptr) {
+
+ GumAddress saved_return_address = GUM_ADDRESS(saved_return_ptr);
+
+ gum_x86_writer_put_push_reg(cw, GUM_REG_EAX);
+ gum_x86_writer_put_push_reg(cw, GUM_REG_EBX);
+
+ gum_x86_writer_put_mov_reg_address(cw, GUM_REG_EAX, saved_return_address);
+ gum_x86_writer_put_mov_reg_reg_offset_ptr(cw, GUM_REG_EBX, GUM_REG_ESP, 0x8);
+ gum_x86_writer_put_mov_reg_offset_ptr_reg(cw, GUM_REG_EAX, 0, GUM_REG_EBX);
+
+ gum_x86_writer_put_pop_reg(cw, GUM_REG_EBX);
+ gum_x86_writer_put_pop_reg(cw, GUM_REG_EAX);
+
+}
+
+static void instrument_jump_ret(GumX86Writer *cw, void **saved_return_ptr) {
+
+ GumAddress saved_return_address = GUM_ADDRESS(saved_return_ptr);
+
+ /* Place holder for ret */
+ gum_x86_writer_put_push_reg(cw, GUM_REG_EAX);
+ gum_x86_writer_put_push_reg(cw, GUM_REG_EAX);
+
+ gum_x86_writer_put_mov_reg_address(cw, GUM_REG_EAX, saved_return_address);
+ gum_x86_writer_put_mov_reg_reg_offset_ptr(cw, GUM_REG_EAX, GUM_REG_EAX, 0);
+
+ gum_x86_writer_put_mov_reg_offset_ptr_reg(cw, GUM_REG_ESP, 0x4, GUM_REG_EAX);
+ gum_x86_writer_put_pop_reg(cw, GUM_REG_EAX);
+ gum_x86_writer_put_ret(cw);
+
+}
+
+static int instrument_afl_persistent_loop_func(void) {
+
+ int ret = __afl_persistent_loop(persistent_count);
+ previous_pc = 0;
+ return ret;
+
+}
+
+static void instrument_afl_persistent_loop(GumX86Writer *cw) {
+
+ gum_x86_writer_put_call_address_with_arguments(
+ cw, GUM_CALL_CAPI, GUM_ADDRESS(instrument_afl_persistent_loop_func), 0);
+ gum_x86_writer_put_test_reg_reg(cw, GUM_REG_EAX, GUM_REG_EAX);
+
+}
+
+static void persistent_prologue_hook(GumX86Writer *cw, struct x86_regs *regs) {
+
+ if (hook == NULL) return;
+
+ gum_x86_writer_put_mov_reg_address(cw, GUM_REG_ECX,
+ GUM_ADDRESS(&__afl_fuzz_len));
+ gum_x86_writer_put_mov_reg_reg_offset_ptr(cw, GUM_REG_ECX, GUM_REG_ECX, 0);
+ gum_x86_writer_put_mov_reg_reg_offset_ptr(cw, GUM_REG_ECX, GUM_REG_ECX, 0);
+
+ gum_x86_writer_put_mov_reg_address(cw, GUM_REG_EDX,
+ GUM_ADDRESS(&__afl_fuzz_ptr));
+ gum_x86_writer_put_mov_reg_reg_offset_ptr(cw, GUM_REG_EDX, GUM_REG_EDX, 0);
+
+ /* Base address is 64-bits (hence two zero arguments) */
+ gum_x86_writer_put_call_address_with_arguments(
+ cw, GUM_CALL_CAPI, GUM_ADDRESS(hook), 5, GUM_ARG_ADDRESS,
+ GUM_ADDRESS(regs), GUM_ARG_ADDRESS, GUM_ADDRESS(0), GUM_ARG_ADDRESS,
+ GUM_ADDRESS(0), GUM_ARG_REGISTER, GUM_REG_EDX, GUM_ARG_REGISTER,
+ GUM_REG_ECX);
}
void persistent_prologue(GumStalkerOutput *output) {
- UNUSED_PARAMETER(output);
- FATAL("Persistent mode not supported on this architecture");
+ /*
+ * SAVE REGS
+ * SAVE RET
+ * POP RET
+ * loop:
+ * CALL instrument_afl_persistent_loop
+ * TEST EAX, EAX
+ * JZ end:
+ * call hook (optionally)
+ * RESTORE REGS
+ * call original
+ * jmp loop:
+ *
+ * end:
+ * JMP SAVED RET
+ *
+ * original:
+ * INSTRUMENTED PERSISTENT FUNC
+ */
+
+ GumX86Writer *cw = output->writer.x86;
+
+ gconstpointer loop = cw->code + 1;
+
+ /* Stack must be 16-byte aligned per ABI */
+ instrument_persitent_save_regs(cw, &saved_regs);
+
+ /* Stash and pop the return value */
+ instrument_save_ret(cw, &saved_return);
+ gum_x86_writer_put_lea_reg_reg_offset(cw, GUM_REG_ESP, GUM_REG_ESP, (4));
+
+ /* loop: */
+ gum_x86_writer_put_label(cw, loop);
+
+ /* call instrument_prologue_func */
+ instrument_afl_persistent_loop(cw);
+
+ /* jz done */
+ gconstpointer done = cw->code + 1;
+ gum_x86_writer_put_jcc_near_label(cw, X86_INS_JE, done, GUM_UNLIKELY);
+
+ /* Optionally call the persistent hook */
+ persistent_prologue_hook(cw, &saved_regs);
+
+ instrument_persitent_restore_regs(cw, &saved_regs);
+ gconstpointer original = cw->code + 1;
+ /* call original */
+ gum_x86_writer_put_call_near_label(cw, original);
+ /* jmp loop */
+ gum_x86_writer_put_jmp_near_label(cw, loop);
+
+ /* done: */
+ gum_x86_writer_put_label(cw, done);
+
+ instrument_jump_ret(cw, &saved_return);
+
+ /* original: */
+ gum_x86_writer_put_label(cw, original);
+
+ gum_x86_writer_flush(cw);
}