|
|
|
@ -65,6 +65,10 @@ bool emu_t::init() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool emu_t::emulate(std::uint32_t vmenter_rva, vm::instrs::vrtn_t& vrtn) {
|
|
|
|
|
static int vmenter_number = 0;
|
|
|
|
|
std::printf("\n[%.4d] STARTING FROM NEW VMENTER AT %p (%p)\n\n", vmenter_number++,
|
|
|
|
|
vmenter_rva + m_vm->m_module_base,
|
|
|
|
|
vmenter_rva + m_vm->m_image_base);
|
|
|
|
|
uc_err err;
|
|
|
|
|
vrtn.m_rva = vmenter_rva;
|
|
|
|
|
|
|
|
|
@ -124,7 +128,6 @@ bool emu_t::emulate(std::uint32_t vmenter_rva, vm::instrs::vrtn_t& vrtn) {
|
|
|
|
|
auto& new_blk = vrtn.m_blks.emplace_back();
|
|
|
|
|
new_blk.m_vip = {0ull, 0ull};
|
|
|
|
|
new_blk.m_vm = {blk.m_jmp.m_vm.vip, blk.m_jmp.m_vm.vsp};
|
|
|
|
|
new_blk.is_branch = true;
|
|
|
|
|
cc_blk = &new_blk;
|
|
|
|
|
|
|
|
|
|
// emulate the branch...
|
|
|
|
@ -269,13 +272,20 @@ bool emu_t::branch_pred_spec_exec(uc_engine* uc, uint64_t address,
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (instr.mnemonic == ZYDIS_MNEMONIC_INVALID) {std::printf("> bad instruction\n"); return false;}
|
|
|
|
|
if (instr.mnemonic == ZYDIS_MNEMONIC_INVALID) return false;
|
|
|
|
|
|
|
|
|
|
uc_context* ctx;
|
|
|
|
|
uct_context_alloc(uc, &ctx);
|
|
|
|
|
uc_context_save(uc, ctx);
|
|
|
|
|
|
|
|
|
|
if (obj->cc_trace.m_instrs.empty())
|
|
|
|
|
obj->cc_trace.m_begin = address;
|
|
|
|
|
// if this is the first instruction of this handler then save the stack...
|
|
|
|
|
if (!obj->cc_trace.m_instrs.size()) {
|
|
|
|
|
obj->cc_trace.m_vip = obj->cc_blk->m_vm.vip;
|
|
|
|
|
obj->cc_trace.m_vsp = obj->cc_blk->m_vm.vsp;
|
|
|
|
|
obj->cc_trace.m_stack = new std::uint8_t[STACK_SIZE]; g_new_delete_tracker++;
|
|
|
|
|
uc_mem_read(uc, STACK_BASE, obj->cc_trace.m_stack, STACK_SIZE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
obj->cc_trace.m_instrs.push_back({instr, ctx});
|
|
|
|
|
|
|
|
|
|
// RET or JMP REG means the end of a vm handler...
|
|
|
|
@ -285,16 +295,8 @@ bool emu_t::branch_pred_spec_exec(uc_engine* uc, uint64_t address,
|
|
|
|
|
// deobfuscate the instruction stream before profiling...
|
|
|
|
|
// makes it easier for profiles to be correct...
|
|
|
|
|
vm::instrs::deobfuscate(obj->cc_trace);
|
|
|
|
|
std::printf("> vsp = %s, vip = %s, address = %p (%p)\n", ZydisRegisterGetString(obj->cc_trace.m_vsp), ZydisRegisterGetString(obj->cc_trace.m_vip),
|
|
|
|
|
obj->cc_trace.m_begin, obj->cc_trace.m_begin - obj->m_vm->m_module_base + obj->m_vm->m_image_base);
|
|
|
|
|
zydis_rtn_t inst_stream;
|
|
|
|
|
std::for_each(obj->cc_trace.m_instrs.begin(),
|
|
|
|
|
obj->cc_trace.m_instrs.end(),
|
|
|
|
|
[&](vm::instrs::emu_instr_t& instr) {
|
|
|
|
|
inst_stream.push_back({instr.m_instr});
|
|
|
|
|
});
|
|
|
|
|
vm::utils::print(inst_stream);
|
|
|
|
|
/* // find the last MOV REG, DWORD PTR [VIP] in the instruction stream, then
|
|
|
|
|
|
|
|
|
|
// find the last MOV REG, DWORD PTR [VIP] in the instruction stream, then
|
|
|
|
|
// remove any instructions from this instruction to the JMP/RET...
|
|
|
|
|
const auto rva_fetch = std::find_if(
|
|
|
|
|
obj->cc_trace.m_instrs.rbegin(), obj->cc_trace.m_instrs.rend(),
|
|
|
|
@ -305,7 +307,7 @@ bool emu_t::branch_pred_spec_exec(uc_engine* uc, uint64_t address,
|
|
|
|
|
i.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER &&
|
|
|
|
|
i.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY &&
|
|
|
|
|
i.operands[1].mem.base == vip && i.operands[1].size == 32;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (rva_fetch != obj->cc_trace.m_instrs.rend())
|
|
|
|
|
{
|
|
|
|
@ -315,29 +317,27 @@ bool emu_t::branch_pred_spec_exec(uc_engine* uc, uint64_t address,
|
|
|
|
|
}
|
|
|
|
|
obj->cc_trace.m_instrs.erase((rva_fetch + 1).base(),
|
|
|
|
|
obj->cc_trace.m_instrs.end());
|
|
|
|
|
} */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto vinstr = vm::instrs::determine(obj->cc_trace);
|
|
|
|
|
std::printf("instruction is: %d\n", vinstr.mnemonic);
|
|
|
|
|
|
|
|
|
|
// -- free the trace since we will start a new one...
|
|
|
|
|
std::for_each(obj->cc_trace.m_instrs.begin(), obj->cc_trace.m_instrs.end(),
|
|
|
|
|
[&](const vm::instrs::emu_instr_t& instr) {
|
|
|
|
|
uct_context_free(instr.m_cpu);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
delete[] obj->cc_trace.m_stack; if (obj->cc_trace.m_stack) g_new_delete_tracker--;
|
|
|
|
|
obj->cc_trace.m_instrs.clear();
|
|
|
|
|
|
|
|
|
|
if (vinstr.mnemonic != vm::instrs::mnemonic_t::jmp) {
|
|
|
|
|
/* if (vinstr.mnemonic != vm::instrs::mnemonic_t::sreg) uc_emu_stop(uc);
|
|
|
|
|
if (vinstr.mnemonic != vm::instrs::mnemonic_t::sreg) uc_emu_stop(uc);
|
|
|
|
|
|
|
|
|
|
if (!vinstr.imm.has_imm) uc_emu_stop(uc);
|
|
|
|
|
|
|
|
|
|
if (vinstr.imm.size != 8 || vinstr.imm.val > 8 * VIRTUAL_REGISTER_COUNT)
|
|
|
|
|
uc_emu_stop(uc); */
|
|
|
|
|
|
|
|
|
|
// stop if done or if instruction is invalid
|
|
|
|
|
if (vinstr.mnemonic == instrs::mnemonic_t::unknown)
|
|
|
|
|
uc_emu_stop(uc);
|
|
|
|
|
|
|
|
|
|
// -- stop after 10 legit SREG's...
|
|
|
|
|
if (++obj->m_sreg_cnt == 10) uc_emu_stop(uc);
|
|
|
|
|
}
|
|
|
|
@ -425,7 +425,7 @@ bool emu_t::code_exec_callback(uc_engine* uc, uint64_t address, uint32_t size,
|
|
|
|
|
uc_context* backup;
|
|
|
|
|
uct_context_alloc(uc, &backup);
|
|
|
|
|
uc_context_save(uc, backup);
|
|
|
|
|
uc_context_restore(uc, vip_write->m_cpu);
|
|
|
|
|
uc_context_restore(uc, (--vip_write)->m_cpu);
|
|
|
|
|
|
|
|
|
|
std::uintptr_t vip_addr = 0ull;
|
|
|
|
|
uc_reg_read(uc, vm::instrs::reg_map[obj->cc_trace.m_vip], &vip_addr);
|
|
|
|
@ -470,8 +470,8 @@ bool emu_t::code_exec_callback(uc_engine* uc, uint64_t address, uint32_t size,
|
|
|
|
|
uc_emu_stop(uc);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
//I don't think this if statement is neccesary
|
|
|
|
|
//if (obj->cc_blk->m_vinstrs.size()) {
|
|
|
|
|
|
|
|
|
|
if (obj->cc_blk->m_vinstrs.size()) {
|
|
|
|
|
if (vinstr.mnemonic == vm::instrs::mnemonic_t::jmp) {
|
|
|
|
|
uc_context *backup, *copy;
|
|
|
|
|
|
|
|
|
@ -500,7 +500,8 @@ bool emu_t::code_exec_callback(uc_engine* uc, uint64_t address, uint32_t size,
|
|
|
|
|
if (vinstr.mnemonic == vm::instrs::mnemonic_t::jmp ||
|
|
|
|
|
vinstr.mnemonic == vm::instrs::mnemonic_t::vmexit)
|
|
|
|
|
uc_emu_stop(obj->uc);
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
obj->cc_blk->m_vinstrs.push_back(vinstr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -514,7 +515,7 @@ bool emu_t::code_exec_callback(uc_engine* uc, uint64_t address, uint32_t size,
|
|
|
|
|
obj->cc_trace.m_instrs.clear();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void emu_t::invalid_mem(uc_engine* uc, uc_mem_type type, uint64_t address,
|
|
|
|
|
int size, int64_t value, emu_t* obj) {
|
|
|
|
@ -568,10 +569,6 @@ bool emu_t::legit_branch(vm::instrs::vblk_t& vblk, std::uintptr_t branch_addr) {
|
|
|
|
|
uc_reg_read(uc, UC_X86_REG_RIP, &rip);
|
|
|
|
|
uc_reg_read(uc, vm::instrs::reg_map[vblk.m_vm.vsp], &vsp);
|
|
|
|
|
uc_mem_write(uc, vsp, &branch_addr, sizeof branch_addr);
|
|
|
|
|
|
|
|
|
|
// reset vsp and vip to their pre-jmp status in the current code trace
|
|
|
|
|
// this will be reset when the jmp instruction is emulated again
|
|
|
|
|
cc_trace.m_vsp = cc_blk->m_vm.vsp, cc_trace.m_vip = cc_blk->m_vm.vip;
|
|
|
|
|
|
|
|
|
|
m_sreg_cnt = 0u;
|
|
|
|
|
uc_emu_start(uc, rip, 0ull, 0ull, 0ull);
|
|
|
|
@ -613,32 +610,31 @@ std::optional<std::pair<std::uintptr_t, std::uintptr_t>> emu_t::could_have_jcc(
|
|
|
|
|
return v.mnemonic == vm::instrs::mnemonic_t::lconst && v.imm.size == 64;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const auto valid_mem = [=](uintptr_t loc)
|
|
|
|
|
{
|
|
|
|
|
// check to see if the imm val is inside of the image...
|
|
|
|
|
if (loc > m_vm->m_image_base + m_vm->m_image_size ||
|
|
|
|
|
loc < m_vm->m_image_base ||
|
|
|
|
|
loc > m_vm->m_image_base + m_vm->m_image_size ||
|
|
|
|
|
loc < m_vm->m_image_base)
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
const auto lconst1 =
|
|
|
|
|
std::find_if(vinstrs.rbegin(), vinstrs.rend(), lconst64_chk);
|
|
|
|
|
|
|
|
|
|
if (lconst1 == vinstrs.rend()) return {};
|
|
|
|
|
|
|
|
|
|
const auto lconst2 = std::find_if(lconst1 + 1, vinstrs.rend(), lconst64_chk);
|
|
|
|
|
|
|
|
|
|
auto lconst1 = vinstrs.rbegin();
|
|
|
|
|
do {
|
|
|
|
|
lconst1 =
|
|
|
|
|
std::find_if(lconst1 + 1, vinstrs.rend(), lconst64_chk);
|
|
|
|
|
if (lconst1 == vinstrs.rend()) return {};
|
|
|
|
|
} while (!valid_mem(lconst1->imm.val));
|
|
|
|
|
|
|
|
|
|
auto lconst2 = lconst1;
|
|
|
|
|
do {
|
|
|
|
|
lconst2 =
|
|
|
|
|
std::find_if(lconst2 + 1, vinstrs.rend(), lconst64_chk);
|
|
|
|
|
if (lconst2 == vinstrs.rend()) return {};
|
|
|
|
|
} while (!valid_mem(lconst1->imm.val));
|
|
|
|
|
|
|
|
|
|
std::printf("possible jcc: %p, %p\n", lconst1->imm.val, lconst2->imm.val);
|
|
|
|
|
// check to see if the imm val is inside of the image...
|
|
|
|
|
if (lconst1->imm.val > m_vm->m_image_base + m_vm->m_image_size ||
|
|
|
|
|
lconst1->imm.val < m_vm->m_image_base ||
|
|
|
|
|
lconst2->imm.val > m_vm->m_image_base + m_vm->m_image_size ||
|
|
|
|
|
lconst2->imm.val < m_vm->m_image_base)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
// check to see if the imm's points to something inside of an executable
|
|
|
|
|
// section...
|
|
|
|
|
if (!vm::utils::scn::executable(
|
|
|
|
|
m_vm->m_module_base,
|
|
|
|
|
(lconst1->imm.val - m_vm->m_image_base) + m_vm->m_module_base) ||
|
|
|
|
|
!vm::utils::scn::executable(
|
|
|
|
|
m_vm->m_module_base,
|
|
|
|
|
(lconst2->imm.val - m_vm->m_image_base) + m_vm->m_module_base))
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return {{lconst1->imm.val, lconst2->imm.val}};
|
|
|
|
|
}
|
|
|
|
|