1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include <unicorn/unicorn.h> #include <string.h>
#define THUMB_CODE "\x10\xb5\x4f\xf6\x00\x74\xe8\xbf\x10\xbd"
int STACK_MEMORY_BASE = 0x00000000; int STACK_MEMORY_SIZE = 0x00800000;
int HOOK_MEMORY_BASE = 0x1000000; int HOOK_MEMORY_SIZE = 0x0800000;
static void hook_code(uc_engine *uc, uint64_t address, uint32_t size, void *user_data) { unsigned char bytes[8]; char instruction[32] = {0}; int i = 0; uc_mem_read(uc,address,bytes,size); for(i = 0; i < size; i++){ sprintf(instruction + i * 3,"%02x ", bytes[i]); } printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x, instruction = %s\n", address, size, instruction); }
unsigned char bytes[32]; char instruction[64] = {0}; static void test_thumb(void) { uc_engine *uc; uc_err err; uc_hook trace2;
int sp = 1024; int code_length;
printf("Emulate THUMB code\n");
err = uc_open(UC_ARCH_ARM, UC_MODE_THUMB, &uc); if (err) { printf("Failed on uc_open() with error returned: %u (%s)\n", err, uc_strerror(err)); return; }
uc_mem_map(uc, HOOK_MEMORY_BASE, HOOK_MEMORY_SIZE, UC_PROT_ALL); uc_mem_map(uc, STACK_MEMORY_BASE, STACK_MEMORY_SIZE, UC_PROT_ALL);
code_length = sizeof(THUMB_CODE) - 1; uc_mem_write(uc, HOOK_MEMORY_BASE, THUMB_CODE, code_length);
int lr = HOOK_MEMORY_BASE + 0x80; sp = STACK_MEMORY_BASE + STACK_MEMORY_SIZE; uc_reg_write(uc, UC_ARM_REG_SP, &sp); printf(">>> SP = 0x%x\n", sp); uc_reg_write(uc, UC_ARM_REG_LR, &lr);
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, HOOK_MEMORY_BASE, HOOK_MEMORY_BASE + HOOK_MEMORY_SIZE);
err = uc_emu_start(uc, (HOOK_MEMORY_BASE) | 1, lr, 0, 0); if (err) { printf("Failed on uc_emu_start() with error returned: %u\n", err); }
printf(">>> Emulation done. Below is the CPU context\n");
uc_reg_read(uc, UC_ARM_REG_SP, &sp); printf(">>> SP = 0x%x\n", sp);
uc_close(uc); }
int main(int argc, char **argv, char **envp) { #ifdef DYNLOAD if (!uc_dyn_load(NULL, 0)) { printf("Error dynamically loading shared library.\n"); printf("Please check that unicorn.dll/unicorn.so is available as well as\n"); printf("any other dependent dll/so files.\n"); printf("The easiest way is to place them in the same directory as this app.\n"); return 1; } #endif printf("==========================\n"); test_thumb();
#ifdef DYNLOAD uc_dyn_free(); #endif return 0; }
|