From 907bbb32aa337ef1ae09bb157cf3de573ad85daa Mon Sep 17 00:00:00 2001 From: xtremegamer1 Date: Thu, 3 Nov 2022 21:42:37 -0600 Subject: [PATCH 1/3] theres no more tools tbh --- cmake.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/cmake.toml b/cmake.toml index 9f6f6c6..d58f890 100644 --- a/cmake.toml +++ b/cmake.toml @@ -2,7 +2,6 @@ name = "vmemu" [subdir.deps] -[subdir.tools] [target.vmemu] type = "static" From 4767c79d42cc03c24fb48ffb059b9a102f47ee86 Mon Sep 17 00:00:00 2001 From: xtremegamer1 Date: Wed, 9 Nov 2022 05:19:37 -0700 Subject: [PATCH 2/3] fixed speculative execution which was broken --- src/vmemu_t.cpp | 75 +++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/src/vmemu_t.cpp b/src/vmemu_t.cpp index fb675a1..9e09422 100644 --- a/src/vmemu_t.cpp +++ b/src/vmemu_t.cpp @@ -172,6 +172,7 @@ void emu_t::extract_branch_data() { br1 += m_vm->m_module_base; br2 += m_vm->m_module_base; + // We need to handle the case that the jump points to another jump auto br1_legit = legit_branch(*cc_blk, br1); auto br2_legit = legit_branch(*cc_blk, br2); std::printf("> br1 legit: %d, br2 legit: %d\n", br1_legit, br2_legit); @@ -269,19 +270,10 @@ bool emu_t::branch_pred_spec_exec(uc_engine* uc, uint64_t address, } if (instr.mnemonic == ZYDIS_MNEMONIC_INVALID) return false; - uc_context* ctx; uct_context_alloc(uc, &ctx); uc_context_save(uc, ctx); - // 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... @@ -292,7 +284,7 @@ bool emu_t::branch_pred_spec_exec(uc_engine* uc, uint64_t address, // makes it easier for profiles to be correct... vm::instrs::deobfuscate(obj->cc_trace); - // 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(), @@ -303,7 +295,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()) { @@ -313,17 +305,16 @@ 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); - + // -- 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) { @@ -565,6 +556,10 @@ 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); @@ -606,31 +601,37 @@ std::optional> emu_t::could_have_jcc( return v.mnemonic == vm::instrs::mnemonic_t::lconst && v.imm.size == 64; }; - 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); + 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; + + // 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, + (loc - m_vm->m_image_base) + m_vm->m_module_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 {}; + } 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 {}; - - // 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 {}; + } while (!valid_mem(lconst1->imm.val)); return {{lconst1->imm.val, lconst2->imm.val}}; } From 768a2dc6d0eebab45fc34544047a2301f12a41af Mon Sep 17 00:00:00 2001 From: xtremegamer1 Date: Wed, 9 Nov 2022 05:19:45 -0700 Subject: [PATCH 3/3] update deps --- deps/vmprofiler | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/vmprofiler b/deps/vmprofiler index 4de96fb..e3abc95 160000 --- a/deps/vmprofiler +++ b/deps/vmprofiler @@ -1 +1 @@ -Subproject commit 4de96fbaef64bbae7e85c37e32d287e6f29fe5f1 +Subproject commit e3abc955e14349a1a01d2ea1f998feafa7486de0