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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| package com.tenet;
import com.github.unidbg.Emulator; import com.github.unidbg.Module; import com.github.unidbg.arm.backend.*; import com.github.unidbg.arm.context.RegisterContext; import unicorn.Arm64Const;
import java.io.PrintStream; import java.util.Collections; import java.util.HashMap; import java.util.Map;
public class TenetTrace{ private final Emulator<?> emulator; private final Module module; private final RegisterContext registerContext; private PrintStream out = System.err; private String mRead = ""; private String mWrite = "";
private static Map<String, Integer> arm64Regs;
static { Map<String,Integer> aMap = new HashMap<>(); aMap.put("X0", Arm64Const.UC_ARM64_REG_X0); aMap.put("X1", Arm64Const.UC_ARM64_REG_X1); aMap.put("X2", Arm64Const.UC_ARM64_REG_X2); aMap.put("X3", Arm64Const.UC_ARM64_REG_X3); aMap.put("X4", Arm64Const.UC_ARM64_REG_X4); aMap.put("X5", Arm64Const.UC_ARM64_REG_X5); aMap.put("X6", Arm64Const.UC_ARM64_REG_X6); aMap.put("X7", Arm64Const.UC_ARM64_REG_X7); aMap.put("X8", Arm64Const.UC_ARM64_REG_X8); aMap.put("X9", Arm64Const.UC_ARM64_REG_X9); aMap.put("X10", Arm64Const.UC_ARM64_REG_X10); aMap.put("X11", Arm64Const.UC_ARM64_REG_X11); aMap.put("X12", Arm64Const.UC_ARM64_REG_X12); aMap.put("X13", Arm64Const.UC_ARM64_REG_X13); aMap.put("X14", Arm64Const.UC_ARM64_REG_X14); aMap.put("X15", Arm64Const.UC_ARM64_REG_X15); aMap.put("X16", Arm64Const.UC_ARM64_REG_X16); aMap.put("X17", Arm64Const.UC_ARM64_REG_X17); aMap.put("X18", Arm64Const.UC_ARM64_REG_X18); aMap.put("X19", Arm64Const.UC_ARM64_REG_X19); aMap.put("X20", Arm64Const.UC_ARM64_REG_X20); aMap.put("X21", Arm64Const.UC_ARM64_REG_X21); aMap.put("X22", Arm64Const.UC_ARM64_REG_X22); aMap.put("X23", Arm64Const.UC_ARM64_REG_X23); aMap.put("X24", Arm64Const.UC_ARM64_REG_X24); aMap.put("X25", Arm64Const.UC_ARM64_REG_X25); aMap.put("X26", Arm64Const.UC_ARM64_REG_X26); aMap.put("X27", Arm64Const.UC_ARM64_REG_X27); aMap.put("X28", Arm64Const.UC_ARM64_REG_X28); aMap.put("X29", Arm64Const.UC_ARM64_REG_X29); aMap.put("X30", Arm64Const.UC_ARM64_REG_X30); aMap.put("SP", Arm64Const.UC_ARM64_REG_SP); arm64Regs = Collections.unmodifiableMap(aMap); } public Map<String, Long> preRegsValue = null;
public TenetTrace(Emulator<?> emulator, Module module, PrintStream redirect){ this.emulator = emulator; this.module = module; this.registerContext = emulator.getContext(); if (redirect != null) { this.out = redirect; } start(); }
public Map<String, Long> getAllRegister(){ Map<String, Long> regs = new HashMap<>(); for (Map.Entry<String, Integer> entry : arm64Regs.entrySet()) { regs.put(entry.getKey(), registerContext.getLongByReg(entry.getValue())); } return regs; }
public Map<String, Long> getDiff(Map<String, Long> nowRegs, Map<String, Long> preRegs){ if(preRegs == null){ return nowRegs; } Map<String, Long> diffRegs = new HashMap<>(); for (Map.Entry<String, Integer> entry : arm64Regs.entrySet()) { String regName = entry.getKey(); long now = nowRegs.get(regName); long pre = preRegs.get(regName); if(now != pre){ diffRegs.put(regName, now); } } return diffRegs; }
public void showRegs(Map<String, Long> regs){ for (Map.Entry<String, Long> entry : regs.entrySet()) { out.printf("%s=%#X,", entry.getKey(), entry.getValue()); } out.printf("PC=%#X", registerContext.getPCPointer().peer); }
public void start(){ emulator.getBackend().hook_add_new(new CodeHook() { @Override public void hook(Backend backend, long address, int size, Object user) { if(preRegsValue!=null){ out.println(""); } Map<String, Long> regs = getAllRegister(); Map<String, Long> diffRegs = getDiff(regs, preRegsValue); showRegs(diffRegs); preRegsValue = regs; out.print(mRead); out.print(mWrite); mRead = ""; mWrite= ""; }
@Override public void onAttach(UnHook unHook) {
}
@Override public void detach() {
} }, module.base, module.base+module.size, null);
emulator.getBackend().hook_add_new(new ReadHook() { @Override public void hook(Backend backend, long address, int size, Object user) { byte[] reads = emulator.getBackend().mem_read(address, size); String content = String.format(",mr=%#X:%s", address, bytesToHex(reads)); mRead += content; }
@Override public void onAttach(UnHook unHook) {
}
@Override public void detach() {
} }, 1, 0, null);
emulator.getBackend().hook_add_new(new WriteHook() { @Override public void hook(Backend backend, long address, int size, long value, Object user) { byte[] writes = emulator.getBackend().mem_read(address, size); String content = String.format(",mw=%#X:%s", address, bytesToHex(writes)); mWrite += content; }
@Override public void onAttach(UnHook unHook) {
}
@Override public void detach() {
} }, 1, 0, null);
}
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); public static String bytesToHex(byte[] bytes) { char[] hexChars = new char[bytes.length * 2]; for (int j = 0; j < bytes.length; j++) { int v = bytes[j] & 0xFF; hexChars[j * 2] = HEX_ARRAY[v >>> 4]; hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; } return new String(hexChars); }
}
|