|
|
@ -29,7 +29,8 @@ bool get_fetch_operand(const zydis_routine_t &routine,
|
|
|
|
instr_data.instr.operands[1].mem.base == ZYDIS_REGISTER_RSI;
|
|
|
|
instr_data.instr.operands[1].mem.base == ZYDIS_REGISTER_RSI;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (result == routine.end()) return false;
|
|
|
|
if (result == routine.end())
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
fetch_instr = *result;
|
|
|
|
fetch_instr = *result;
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
@ -53,7 +54,8 @@ std::optional<zydis_routine_t::iterator> get_fetch_operand(
|
|
|
|
instr_data.instr.operands[1].mem.base == ZYDIS_REGISTER_RSI;
|
|
|
|
instr_data.instr.operands[1].mem.base == ZYDIS_REGISTER_RSI;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (result == routine.end()) return {};
|
|
|
|
if (result == routine.end())
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -79,8 +81,10 @@ bool is_jmp(const zydis_decoded_instr_t &instr) {
|
|
|
|
instr.mnemonic <= ZYDIS_MNEMONIC_JZ;
|
|
|
|
instr.mnemonic <= ZYDIS_MNEMONIC_JZ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool flatten(zydis_routine_t &routine, std::uintptr_t routine_addr,
|
|
|
|
bool flatten(zydis_routine_t& routine,
|
|
|
|
bool keep_jmps, std::uint32_t max_instrs,
|
|
|
|
std::uintptr_t routine_addr,
|
|
|
|
|
|
|
|
bool keep_jmps,
|
|
|
|
|
|
|
|
std::uint32_t max_instrs,
|
|
|
|
std::uintptr_t module_base) {
|
|
|
|
std::uintptr_t module_base) {
|
|
|
|
zydis_decoded_instr_t instr;
|
|
|
|
zydis_decoded_instr_t instr;
|
|
|
|
std::uint32_t instr_cnt = 0u;
|
|
|
|
std::uint32_t instr_cnt = 0u;
|
|
|
@ -88,7 +92,8 @@ bool flatten(zydis_routine_t &routine, std::uintptr_t routine_addr,
|
|
|
|
while (ZYAN_SUCCESS(ZydisDecoderDecodeBuffer(
|
|
|
|
while (ZYAN_SUCCESS(ZydisDecoderDecodeBuffer(
|
|
|
|
vm::util::g_decoder.get(), reinterpret_cast<void*>(routine_addr), 0x1000,
|
|
|
|
vm::util::g_decoder.get(), reinterpret_cast<void*>(routine_addr), 0x1000,
|
|
|
|
&instr))) {
|
|
|
|
&instr))) {
|
|
|
|
if (++instr_cnt > max_instrs) return false;
|
|
|
|
if (++instr_cnt > max_instrs)
|
|
|
|
|
|
|
|
return false;
|
|
|
|
// detect if we have already been at this instruction... if so that means
|
|
|
|
// detect if we have already been at this instruction... if so that means
|
|
|
|
// there is a loop and we are going to just return...
|
|
|
|
// there is a loop and we are going to just return...
|
|
|
|
if (std::find_if(routine.begin(), routine.end(),
|
|
|
|
if (std::find_if(routine.begin(), routine.end(),
|
|
|
@ -101,16 +106,18 @@ bool flatten(zydis_routine_t &routine, std::uintptr_t routine_addr,
|
|
|
|
raw_instr.insert(raw_instr.begin(), (u8*)routine_addr,
|
|
|
|
raw_instr.insert(raw_instr.begin(), (u8*)routine_addr,
|
|
|
|
(u8*)routine_addr + instr.length);
|
|
|
|
(u8*)routine_addr + instr.length);
|
|
|
|
|
|
|
|
|
|
|
|
if (is_jmp(instr)) {
|
|
|
|
if (is_jmp(instr) ||
|
|
|
|
|
|
|
|
instr.mnemonic == ZYDIS_MNEMONIC_CALL &&
|
|
|
|
|
|
|
|
instr.operands[0].type != ZYDIS_OPERAND_TYPE_REGISTER) {
|
|
|
|
if (instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER) {
|
|
|
|
if (instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER) {
|
|
|
|
routine.push_back({instr, raw_instr, routine_addr});
|
|
|
|
routine.push_back({instr, raw_instr, routine_addr});
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (keep_jmps) routine.push_back({instr, raw_instr, routine_addr});
|
|
|
|
if (keep_jmps)
|
|
|
|
|
|
|
|
routine.push_back({instr, raw_instr, routine_addr});
|
|
|
|
ZydisCalcAbsoluteAddress(&instr, &instr.operands[0], routine_addr,
|
|
|
|
ZydisCalcAbsoluteAddress(&instr, &instr.operands[0], routine_addr,
|
|
|
|
&routine_addr);
|
|
|
|
&routine_addr);
|
|
|
|
|
|
|
|
|
|
|
|
} else if (instr.mnemonic == ZYDIS_MNEMONIC_RET) {
|
|
|
|
} else if (instr.mnemonic == ZYDIS_MNEMONIC_RET) {
|
|
|
|
routine.push_back({instr, raw_instr, routine_addr});
|
|
|
|
routine.push_back({instr, raw_instr, routine_addr});
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|