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
| from unicorn import * #导入包 from unicorn.arm_const import * #导入常量 import binascii
#设置hook def hook_code(uc,address,size,userdata): print(">>> Tracing instruction at 0x%x, instruction size = 0x%x" % (address,size)) #内存异常的 def hook_memory(uc, access, address, size,value,userdata): # 什么地方异常的 访问地址 pc = uc.reg_read(UC_ARM_REG_PC) print("memory error:pc:%x address:%x size:%x"%(pc,address,size))
a1 = b'123'
#定义一个虚拟机对象 架构 模式 +2就是THUMB指令集 mu = Uc(UC_ARCH_ARM, UC_MODE_THUMB) #把so加到进程里 #image image_base = 0x0 # 大小8m的镜像 image_size = 0x10000 * 8 #分配 mu.mem_map(image_base,image_size) #载入文件 rb模式载入 binary = open('libnative-lib.so','rb').read() #镜像写到虚拟机内存里面 mu.mem_write(image_base,binary)
# 运行函数需要个栈 stack stack_base = 0xa0000 #3m stack_size = 0x10000 * 3 # 栈的指针是向下增长的 减4个字节 stack_top = stack_base + stack_size - 0x4 #分配下stackbase mu.mem_map(stack_base,stack_size) #设置下寄存器 reg mu.reg_write(UC_ARM_REG_SP,stack_top)
# data segment #传参数需要数据段 data_base = 0xf0000 data_size = 0x10000 * 3 mu.mem_map(data_base, data_size) # data_base是a1的地址 mu.mem_write(data_base, a1) mu.reg_write(UC_ARM_REG_R0, data_base)
#fix got 修复 mu.mem_write(0x1EDB0, b'\xD9\x98\x00\x00')
# set hook mu.hook_add(UC_HOOK_CODE,hook_code,0) mu.hook_add(UC_HOOK_MEM_UNMAPPED,hook_code,0) #函数开始地址 target = image_base + 0x9B68 #函数while循环 v16地址 循环了16次 就16位 target_end = image_base + 0x9C2C
#start try: mu.emu_start(target + 1,target_end) r2 = mu.reg_read(UC_ARM_REG_R2) result = mu.mem_read(r2,16)
print(binascii.b2a_hex(result))
except UcError as e: print("tea111") print(e)
|