参考链接(主要是备份向)
unidbg入门笔记
项目简介
项目地址
unidbg 是建立在Unicorn引擎之上的,Unicorn引擎是一个强大的开源CPU模拟器框架,支持多种架构,包括x86、ARM、MIPS等,因此unidbg也能够模拟这些不同的CPU架构。UniDGB的另一个核心组成部分是Capstone引擎,它用于反汇编和指令解码。
基本框架
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
| package com.kanxue.test2; import com.github.unidbg.AndroidEmulator; import com.github.unidbg.Module; import com.github.unidbg.arm.backend.DynarmicFactory; import com.github.unidbg.linux.android.AndroidEmulatorBuilder; import com.github.unidbg.linux.android.AndroidResolver; import com.github.unidbg.linux.android.dvm.AbstractJni; import com.github.unidbg.linux.android.dvm.DalvikModule; import com.github.unidbg.linux.android.dvm.VM; import com.github.unidbg.memory.Memory; import java.io.File; public class Test05 extends AbstractJni { private final AndroidEmulator emulator; private final VM vm; private final Module module; Test05(){ emulator = AndroidEmulatorBuilder .for32Bit().addBackendFactory(new DynarmicFactory(true)) .setProcessName("cc.ccc.cc") .build(); Memory memory = emulator.getMemory(); memory.setLibraryResolver(new AndroidResolver(23)); vm = emulator.createDalvikVM(new File("sssss.apk")); vm.setVerbose(true); vm.setJni(this); DalvikModule dm = vm.loadLibrary(new File("ssss.so"), true); module = dm.getModule(); vm.callJNI_OnLoad(emulator,module); } }
|
基本操作
emulator 操作
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
| Memory memory1 = emulator.getMemory();
int pid = emulator.getPid();
VM dalvikVM = emulator.createDalvikVM();
VM dalvikVM1 = emulator.createDalvikVM(new File("ss/ss/apk"));
VM dalvikVM2 = emulator.getDalvikVM();
emulator.showRegs();
Backend backend = emulator.getBackend();
String processName = emulator.getProcessName();
RegisterContext context = emulator.getContext();
emulator.traceRead(1,0);
emulator.traceWrite(1,0);
emulator.traceCode(1,0);
boolean running = emulator.isRunning();
|
memory 操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
memory.setLibraryResolver(new AndroidResolver(23));
UnidbgPointer pointer = memory.pointer(0x11111111);
Collection<MemoryMap> memoryMap = memory1.getMemoryMap();
Module sss = memory1.findModule("sss");
Module moduleByAddress = memory1.findModuleByAddress(0x111111);
|
VM 操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| VM vvm = emulator.createDalvikVM(new File("ssss.apk"));
vvm.setVerbose(true);
DalvikModule dalvikModule = vvm.loadLibrary(new File("ss.so"), true);
vvm.setJni(this);
Pointer jniEnv = vm.getJNIEnv();
Pointer javaVM = vm.getJavaVM();
dalvikModule.callJNI_OnLoad(emulator); vm.callJNI_OnLoad(emulator,dalvikModule.getModule());
|
符号调用
1 2 3 4 5 6 7 8 9 10 11
|
DvmObject<?> obj = vm.resolveClass("com/example/demo01/MainActivity").newObject(null); String signSting = "123456"; DvmObject dvmObject = obj.callJniMethodObject(emulator, "jniMd52([B)Ljava/lang/String;", signSting.getBytes(StandardCharsets.UTF_8)); String result = (String) dvmObject.getValue(); System.out.println("[symble] Call the so md5 function result is ==> " + result);
|
地址调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| ArrayList<Object> args = new ArrayList<>(); Pointer jniEnv = vm.getJNIEnv(); DvmObject object1 = ProxyDvmObject.createObject(vm, this);
args.add(jniEnv);
args.add(null); args.add(vm.addLocalObject(new StringObject(vm, "123456"))); Number number = module.callFunction(emulator, 0x11AE8 + 1, args.toArray()); System.out.println("[addr] number is ==> " + number.intValue()); DvmObject<?> object = vm.getObject(number.intValue()); System.out.println("[addr] Call the so md5 function result is ==> " + object.getValue());
|
参数 context
1 2
| DvmObject<?> context = vm.resolveClass("android/content/Context").newObject(null); list.add(vm.addLocalObject(context));
|
unidbg hook
hookZz
HookZz是inline hook方案,因此可以Hook Sub_xxx,缺点是短函数可能出bug,受限于inline Hook 原理。
文档参考:https://github.com/jmpews/HookZz
demo(1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| IHookZz hookZz = HookZz.getInstance(emulator); hookZz.enable_arm_arm64_b_branch(); hookZz.wrap(module.findSymbolByName("ss_encrypt"), new WrapCallback<RegisterContext>() { @Override public void preCall(Emulator<?> emulator, RegisterContext ctx, HookEntryInfo info) { Pointer pointer = ctx.getPointerArg(2); int length = ctx.getIntArg(3); byte[] key = pointer.getByteArray(0, length); Inspector.inspect(key, "ss_encrypt key"); } @Override public void postCall(Emulator<?> emulator, RegisterContext ctx, HookEntryInfo info) { System.out.println("ss_encrypt.postCall R0=" + ctx.getLongArg(0)); } }); hookZz.disable_arm_arm64_b_branch(); hookZz.instrument(module.base + 0x00000F5C + 1, new InstrumentCallback<Arm32RegisterContext>() { @Override public void dbiCall(Emulator<?> emulator, Arm32RegisterContext ctx, HookEntryInfo info) { System.out.println("R3=" + ctx.getLongArg(3) + ", R10=0x" + Long.toHexString(ctx.getR10Long())); } });
|
demo(2)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| IHookZz hookZz = HookZz.getInstance(emulator); hookZz.wrap(module.base + 0x1BD0 + 1, new WrapCallback<HookZzArm32RegisterContext>() { @Override public void preCall(Emulator<?> emulator, HookZzArm32RegisterContext ctx, HookEntryInfo info) { Pointer input = ctx.getPointerArg(0); System.out.println("input:" + input.getString(0)); }; @Override public void postCall(Emulator<?> emulator, HookZzArm32RegisterContext ctx, HookEntryInfo info) { Pointer result = ctx.getPointerArg(0); System.out.println("input:" + result.getString(0)); } });
|
Dobby
1 2 3 4 5 6 7 8 9 10 11 12
| Dobby dobby = Dobby.getInstance(emulator); dobby.replace(module.findSymbolByName("ss_encrypted_size"), new ReplaceCallback() { @Override public HookStatus onCall(Emulator<?> emulator, HookContext context, long originFunction) { System.out.println("ss_encrypted_size.onCall arg0=" + context.getIntArg(0) + ", originFunction=0x" + Long.toHexString(originFunction)); return HookStatus.RET(emulator, originFunction); } @Override public void postCall(Emulator<?> emulator, HookContext context) { System.out.println("ss_encrypted_size.postCall ret=" + context.getIntArg(0)); } }, true);
|
xHook
xHook 是爱奇艺开源的ndroid PLT hook框架,一个针对 Android 平台 ELF (可执行文件和动态库) 的 PLT (Procedure Linkage Table) hook 库。优点是挺稳定好用,缺点是不能Hook Sub_xxx 子函数。
参考文档:https://github.com/iqiyi/xHook
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
| IxHook xHook = XHookImpl.getInstance(emulator); xHook.register("libttEncrypt.so", "strlen", new ReplaceCallback() { @Override public HookStatus onCall(Emulator<?> emulator, HookContext context, long originFunction) { Pointer pointer = context.getPointerArg(0); String str = pointer.getString(0); System.out.println("strlen=" + str); context.push(str); return HookStatus.RET(emulator, originFunction); } @Override public void postCall(Emulator<?> emulator, HookContext context) { System.out.println("strlen=" + context.pop() + ", ret=" + context.getIntArg(0)); } }, true); xHook.register("libttEncrypt.so", "memmove", new ReplaceCallback() { @Override public HookStatus onCall(Emulator<?> emulator, long originFunction) { RegisterContext context = emulator.getContext(); Pointer dest = context.getPointerArg(0); Pointer src = context.getPointerArg(1); int length = context.getIntArg(2); Inspector.inspect(src.getByteArray(0, length), "memmove dest=" + dest); return HookStatus.RET(emulator, originFunction); } }); xHook.register("libttEncrypt.so", "memcpy", new ReplaceCallback() { @Override public HookStatus onCall(Emulator<?> emulator, long originFunction) { RegisterContext context = emulator.getContext(); Pointer dest = context.getPointerArg(0); Pointer src = context.getPointerArg(1); int length = context.getIntArg(2); Inspector.inspect(src.getByteArray(0, length), "memcpy dest=" + dest); return HookStatus.RET(emulator, originFunction); } }); xHook.refresh();
|
unicorn hook
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
| emulator.getBackend().hook_add_new(new CodeHook() { @Override public void hook(Backend backend, long address, int size, Object user) { RegisterContext context = emulator.getContext(); if (address == module.base + 0x1FF4){ Pointer md5Ctx = context.getPointerArg(0); Inspector.inspect(md5Ctx.getByteArray(0, 32), "md5Ctx"); Pointer plainText = context.getPointerArg(1); int length = context.getIntArg(2); Inspector.inspect(plainText.getByteArray(0, length), "plainText"); }else if (address == module.base + 0x2004){ Pointer cipherText = context.getPointerArg(1); Inspector.inspect(cipherText.getByteArray(0, 16), "cipherText"); } } @Override public void onAttach(UnHook unHook) { } @Override public void detach() { } }, module.base + 0x1FE8, module.base + 0x2004, "xxxxzzzz");
|
打印调用栈
1
| emulator.getUnwinder().unwind();
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public void callFunc() { emulator.getBackend().hook_add_new(new CodeHook() { @Override public void hook(Backend backend, long address, int size, Object user) { System.out.println("开始--------------------------"); System.out.println(user); System.out.println(size); emulator.getUnwinder().unwind(); System.out.println("==============================="); } @Override public void onAttach(UnHook unHook) { } @Override public void detach() { } },module.base+0xAD40,module.base+0xAD40,"xibei"); }
|
Console debugger
1 2
| Debugger attach = emulator.attach(); attach.addBreakPoint(module.base + 0xC365);
|
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
| c: 继续 n: 跨过 bt:回溯 st hex:搜索堆栈 shw hex:搜索可写堆 shr hex:搜索可读堆 shx-hex:搜索可执行堆 nb:在下一个街区破发 s|si:步入 s[decimal]:执行指定的金额指令 s(blx):执行util blx助记符,性能低 m(op)[size]:显示内存,默认大小为0x70,大小可以是十六进制或十进制 mr0-mr7,mfp,mip,msp[size]:显示指定寄存器的内存 m(address)[size]:显示指定地址的内存,地址必须以0x开头 wr0-wr7,wfp,wip,wsp<value>:写入指定寄存器 wb(address),ws(address),wi(address)<value>:写入指定地址的(字节、短、整数)内存,地址必须以0x开头 wx(address)<hex>:将字节写入指定地址的内存,地址必须以0x开头 b(address):添加临时断点,地址必须以0x开头,可以是模块偏移量 b: 添加寄存器PC的断点 r: 删除寄存器PC的断点 blr:添加寄存器LR的临时断点 p (assembly):位于PC地址的修补程序集 where: 显示java堆栈跟踪 trace[begin-end]:设置跟踪指令 traceRead[begin-end]:设置跟踪内存读取 traceWrite〔begin-end〕:设置跟踪内存写入 vm:查看加载的模块 vbs:查看断点 d|dis:显示反汇编 d(0x):在指定地址显示反汇编 stop: 停止模拟 run[arg]:运行测试 gc:运行System.gc() threads: 显示线程列表 cc size:将asm从0x4000c364-0x4000c364+size字节转换为c函数
|
监控内存读写
将信息输出到文件
1 2 3 4 5 6 7
| String traceFile = "myMonitorFile"; PrintStream traceStream = null; try { traceStream = new PrintStream(new FileOutputStream(traceFile), true); } catch (FileNotFoundException e) { e.printStackTrace(); }
|
监控内存读
1
| emulator.traceRead(module.base, module.base + module.size).setRedirect(traceStream);
|
监控内存写
1
| emulator.traceWrite(module.base, module.base + module.size).setRedirect(traceStream);
|
trace
1 2 3 4 5 6 7 8
| String traceFile = "myTraceCodeFile"; PrintStream traceStream = null; try { traceStream = new PrintStream(new FileOutputStream(traceFile), true); } catch (FileNotFoundException e) { e.printStackTrace(); } emulator.traceCode(module.base, module.base + module.size).setRedirect(traceStream);
|