|
|
|
@ -60,21 +60,16 @@ std::optional<zydis_routine_t::iterator> get_fetch_operand(
|
|
|
|
|
|
|
|
|
|
void print(const zydis_decoded_instr_t &instr) {
|
|
|
|
|
char buffer[256];
|
|
|
|
|
ZydisFormatter formatter;
|
|
|
|
|
ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL);
|
|
|
|
|
ZydisFormatterFormatInstruction(&formatter, &instr, buffer, sizeof(buffer),
|
|
|
|
|
0u);
|
|
|
|
|
ZydisFormatterFormatInstruction(vm::util::g_formatter.get(), &instr, buffer,
|
|
|
|
|
sizeof(buffer), 0u);
|
|
|
|
|
std::puts(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void print(zydis_routine_t &routine) {
|
|
|
|
|
char buffer[256];
|
|
|
|
|
ZydisFormatter formatter;
|
|
|
|
|
ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL);
|
|
|
|
|
|
|
|
|
|
for (auto [instr, raw, addr] : routine) {
|
|
|
|
|
ZydisFormatterFormatInstruction(&formatter, &instr, buffer, sizeof(buffer),
|
|
|
|
|
addr);
|
|
|
|
|
ZydisFormatterFormatInstruction(vm::util::g_formatter.get(), &instr, buffer,
|
|
|
|
|
sizeof(buffer), addr);
|
|
|
|
|
std::printf("> %p %s\n", addr, buffer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -87,15 +82,12 @@ bool is_jmp(const zydis_decoded_instr_t &instr) {
|
|
|
|
|
bool flatten(zydis_routine_t &routine, std::uintptr_t routine_addr,
|
|
|
|
|
bool keep_jmps, std::uint32_t max_instrs,
|
|
|
|
|
std::uintptr_t module_base) {
|
|
|
|
|
ZydisDecoder decoder;
|
|
|
|
|
zydis_decoded_instr_t instr;
|
|
|
|
|
|
|
|
|
|
ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64,
|
|
|
|
|
ZYDIS_ADDRESS_WIDTH_64);
|
|
|
|
|
|
|
|
|
|
std::uint32_t instr_cnt = 0u;
|
|
|
|
|
|
|
|
|
|
while (ZYAN_SUCCESS(ZydisDecoderDecodeBuffer(
|
|
|
|
|
&decoder, reinterpret_cast<void *>(routine_addr), 0x1000, &instr))) {
|
|
|
|
|
vm::util::g_decoder.get(), reinterpret_cast<void *>(routine_addr), 0x1000,
|
|
|
|
|
&instr))) {
|
|
|
|
|
if (++instr_cnt > max_instrs) return false;
|
|
|
|
|
// detect if we have already been at this instruction... if so that means
|
|
|
|
|
// there is a loop and we are going to just return...
|
|
|
|
@ -116,9 +108,9 @@ bool flatten(zydis_routine_t &routine, std::uintptr_t routine_addr,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (keep_jmps) routine.push_back({instr, raw_instr, routine_addr});
|
|
|
|
|
|
|
|
|
|
ZydisCalcAbsoluteAddress(&instr, &instr.operands[0], routine_addr,
|
|
|
|
|
&routine_addr);
|
|
|
|
|
|
|
|
|
|
} else if (instr.mnemonic == ZYDIS_MNEMONIC_RET) {
|
|
|
|
|
routine.push_back({instr, raw_instr, routine_addr});
|
|
|
|
|
return true;
|
|
|
|
|