Compare commits

..

No commits in common. '9fed462a0a4b7907d90188ddd2ec8b3fe05ddc61' and '9ae253078db33ebf1bed807c5f9efbed8fafce8b' have entirely different histories.

@ -2,6 +2,7 @@
name = "vmemu" name = "vmemu"
[subdir.deps] [subdir.deps]
[subdir.tools]
[target.vmemu] [target.vmemu]
type = "static" type = "static"

2
deps/vmprofiler vendored

@ -1 +1 @@
Subproject commit f65c84050d39210c8057ab741b8cbaf9f1e2e17d Subproject commit 4de96fbaef64bbae7e85c37e32d287e6f29fe5f1

@ -26,7 +26,7 @@ class emu_t {
~emu_t(); ~emu_t();
bool init(); bool init();
bool emulate(std::uint32_t vmenter_rva, vm::instrs::vrtn_t& vrtn); bool emulate(std::uint32_t vmenter_rva, vm::instrs::vrtn_t& vrtn);
const bool m_log_instructions = true; const bool m_log_instructions = false;
private: private:
uc_engine* uc; uc_engine* uc;

@ -65,6 +65,10 @@ bool emu_t::init() {
} }
bool emu_t::emulate(std::uint32_t vmenter_rva, vm::instrs::vrtn_t& vrtn) { 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; uc_err err;
vrtn.m_rva = vmenter_rva; 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(); auto& new_blk = vrtn.m_blks.emplace_back();
new_blk.m_vip = {0ull, 0ull}; new_blk.m_vip = {0ull, 0ull};
new_blk.m_vm = {blk.m_jmp.m_vm.vip, blk.m_jmp.m_vm.vsp}; 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; cc_blk = &new_blk;
// emulate the branch... // emulate the branch...
@ -269,13 +272,20 @@ bool emu_t::branch_pred_spec_exec(uc_engine* uc, uint64_t address,
return false; 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; uc_context* ctx;
uct_context_alloc(uc, &ctx); uct_context_alloc(uc, &ctx);
uc_context_save(uc, ctx); uc_context_save(uc, ctx);
if (obj->cc_trace.m_instrs.empty()) // if this is the first instruction of this handler then save the stack...
obj->cc_trace.m_begin = address; 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}); obj->cc_trace.m_instrs.push_back({instr, ctx});
// RET or JMP REG means the end of a vm handler... // 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... // deobfuscate the instruction stream before profiling...
// makes it easier for profiles to be correct... // makes it easier for profiles to be correct...
vm::instrs::deobfuscate(obj->cc_trace); 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); // find the last MOV REG, DWORD PTR [VIP] in the instruction stream, then
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
// remove any instructions from this instruction to the JMP/RET... // remove any instructions from this instruction to the JMP/RET...
const auto rva_fetch = std::find_if( const auto rva_fetch = std::find_if(
obj->cc_trace.m_instrs.rbegin(), obj->cc_trace.m_instrs.rend(), obj->cc_trace.m_instrs.rbegin(), 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.erase((rva_fetch + 1).base(),
obj->cc_trace.m_instrs.end()); obj->cc_trace.m_instrs.end());
} */ }
const auto vinstr = vm::instrs::determine(obj->cc_trace); 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... // -- 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(), std::for_each(obj->cc_trace.m_instrs.begin(), obj->cc_trace.m_instrs.end(),
[&](const vm::instrs::emu_instr_t& instr) { [&](const vm::instrs::emu_instr_t& instr) {
uct_context_free(instr.m_cpu); 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(); obj->cc_trace.m_instrs.clear();
if (vinstr.mnemonic != vm::instrs::mnemonic_t::jmp) { 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.has_imm) uc_emu_stop(uc);
if (vinstr.imm.size != 8 || vinstr.imm.val > 8 * VIRTUAL_REGISTER_COUNT) 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); uc_emu_stop(uc);
// -- stop after 10 legit SREG's... // -- stop after 10 legit SREG's...
if (++obj->m_sreg_cnt == 10) uc_emu_stop(uc); 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; uc_context* backup;
uct_context_alloc(uc, &backup); uct_context_alloc(uc, &backup);
uc_context_save(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; std::uintptr_t vip_addr = 0ull;
uc_reg_read(uc, vm::instrs::reg_map[obj->cc_trace.m_vip], &vip_addr); 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); uc_emu_stop(uc);
return false; 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) { if (vinstr.mnemonic == vm::instrs::mnemonic_t::jmp) {
uc_context *backup, *copy; 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 || if (vinstr.mnemonic == vm::instrs::mnemonic_t::jmp ||
vinstr.mnemonic == vm::instrs::mnemonic_t::vmexit) vinstr.mnemonic == vm::instrs::mnemonic_t::vmexit)
uc_emu_stop(obj->uc); uc_emu_stop(obj->uc);
//} }
obj->cc_blk->m_vinstrs.push_back(vinstr); obj->cc_blk->m_vinstrs.push_back(vinstr);
} }
@ -569,10 +570,6 @@ bool emu_t::legit_branch(vm::instrs::vblk_t& vblk, std::uintptr_t branch_addr) {
uc_reg_read(uc, vm::instrs::reg_map[vblk.m_vm.vsp], &vsp); uc_reg_read(uc, vm::instrs::reg_map[vblk.m_vm.vsp], &vsp);
uc_mem_write(uc, vsp, &branch_addr, sizeof branch_addr); 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; m_sreg_cnt = 0u;
uc_emu_start(uc, rip, 0ull, 0ull, 0ull); 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; return v.mnemonic == vm::instrs::mnemonic_t::lconst && v.imm.size == 64;
}; };
static const auto valid_mem = [=](uintptr_t loc) const auto lconst1 =
{ std::find_if(vinstrs.rbegin(), vinstrs.rend(), lconst64_chk);
// 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;
};
auto lconst1 = vinstrs.rbegin();
do {
lconst1 =
std::find_if(lconst1 + 1, vinstrs.rend(), lconst64_chk);
if (lconst1 == vinstrs.rend()) return {}; if (lconst1 == vinstrs.rend()) return {};
} while (!valid_mem(lconst1->imm.val));
auto lconst2 = lconst1; const auto lconst2 = std::find_if(lconst1 + 1, vinstrs.rend(), lconst64_chk);
do {
lconst2 =
std::find_if(lconst2 + 1, vinstrs.rend(), lconst64_chk);
if (lconst2 == vinstrs.rend()) return {}; 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}}; return {{lconst1->imm.val, lconst2->imm.val}};
} }

Loading…
Cancel
Save