master #1

Open
Manifold wants to merge 32 commits from Manifold/vmemu:master into main

2
.gitmodules vendored

@ -1,3 +1,3 @@
[submodule "deps/vmprofiler"] [submodule "deps/vmprofiler"]
path = deps/vmprofiler path = deps/vmprofiler
url = https://githacks.org/vmp3/vmprofiler url = https://git.back.engineering/Manifold/vmprofiler

@ -2,7 +2,6 @@
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 b01ba0ef77198161b14c666f99c5c0cd073bda0f Subproject commit fa64967bd77983b8fd3affc1de196a0d525adb52

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <unicorn/unicorn.h> #include <unicorn/unicorn.h>
#include <array>
#include <atomic> #include <atomic>
#include <functional> #include <functional>
#include <linuxpe> #include <linuxpe>
@ -14,12 +15,18 @@
#define STACK_BASE 0xFFFF000000000000 #define STACK_BASE 0xFFFF000000000000
namespace vm { namespace vm {
namespace reg_names
{
const std::array<std::string, 8> prefixes = {{"Red", "Blue", "Pink", "Green", "Orange", "Yellow", "Black", "White"}};
const std::array<std::string, 4> suffixes = {{"Crewmate", "Engineer", "Imposter", "Shapeshifter"}};
}
class emu_t { class emu_t {
public: public:
explicit emu_t(vm::vmctx_t* vm_ctx); explicit emu_t(vm::vmctx_t* vm_ctx);
~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 = false;
private: private:
uc_engine* uc; uc_engine* uc;
@ -61,7 +68,9 @@ class emu_t {
/// <param name="size"></param> /// <param name="size"></param>
/// <param name="obj"></param> /// <param name="obj"></param>
/// <returns></returns> /// <returns></returns>
static bool code_exec_callback(uc_engine* uc, uint64_t address, uint32_t size, static bool code_exec_callback(uc_engine* uc,
uint64_t address,
uint32_t size,
emu_t* obj); emu_t* obj);
/// <summary> /// <summary>
@ -74,8 +83,10 @@ class emu_t {
/// <param name="size"></param> /// <param name="size"></param>
/// <param name="obj"></param> /// <param name="obj"></param>
/// <returns></returns> /// <returns></returns>
static bool branch_pred_spec_exec(uc_engine* uc, uint64_t address, static bool branch_pred_spec_exec(uc_engine* uc,
uint32_t size, emu_t* obj); uint64_t address,
uint32_t size,
emu_t* obj);
/// <summary> /// <summary>
/// invalid memory access handler. no runtime values can possibly effect the /// invalid memory access handler. no runtime values can possibly effect the
@ -88,8 +99,12 @@ class emu_t {
/// <param name="size">size of the memory access...</param> /// <param name="size">size of the memory access...</param>
/// <param name="value">value being read...</param> /// <param name="value">value being read...</param>
/// <param name="obj">emu_t object pointer...</param> /// <param name="obj">emu_t object pointer...</param>
static void invalid_mem(uc_engine* uc, uc_mem_type type, uint64_t address, static void invalid_mem(uc_engine* uc,
int size, int64_t value, emu_t* obj); uc_mem_type type,
uint64_t address,
int size,
int64_t value,
emu_t* obj);
/// <summary> /// <summary>
/// interrupt callback for unicorn engine. this is used to advance rip over /// interrupt callback for unicorn engine. this is used to advance rip over

@ -1,8 +1,14 @@
#include <string> #include <string>
#include <vmemu_t.hpp> #include <vmemu_t.hpp>
#include <unordered_set>
#ifdef _WIN32
#pragma warning(disable : 4477)
#endif()
int g_new_delete_tracker;
namespace vm { namespace vm {
emu_t::emu_t(vm::vmctx_t* vm_ctx) : m_vm(vm_ctx) {} emu_t::emu_t(vm::vmctx_t* vm_ctx) : m_vm(vm_ctx) {};
emu_t::~emu_t() { emu_t::~emu_t() {
if (uc) uc_close(uc); if (uc) uc_close(uc);
@ -83,21 +89,20 @@ bool emu_t::emulate(std::uint32_t vmenter_rva, vm::instrs::vrtn_t& vrtn) {
return false; return false;
} }
cc_trace.m_vip = cc_blk->m_vm.vip; std::printf("> beginning execution at = %p (%p)\n", rip, rip - m_vm->m_module_base + m_vm->m_image_base);
cc_trace.m_vsp = cc_blk->m_vm.vsp; std::printf("vsp: %s, vip: %s\n", ZydisRegisterGetString(cc_blk->m_vm.vsp),
ZydisRegisterGetString(cc_blk->m_vm.vip));
std::printf("> beginning execution at = %p\n", rip);
if ((err = uc_emu_start(uc, rip, 0ull, 0ull, 0ull))) { if ((err = uc_emu_start(uc, rip, 0ull, 0ull, 0ull))) {
std::printf("> error starting emu... reason = %d\n", err); std::printf("> error starting emu... reason = %d\n", err);
return false; return false;
} }
extract_branch_data(); extract_branch_data();
std::printf("> emulated blk_%p\n\n", cc_blk->m_vip.img_base); std::printf("> emulated blk_%p\n\n", cc_blk->m_vip.img_based);
// keep track of the emulated blocks... by their addresses... // keep track of the emulated blocks... by their addresses...
std::vector<std::uintptr_t> blk_addrs; std::unordered_set<std::uintptr_t> blk_addrs;
blk_addrs.push_back(blk.m_vip.rva + m_vm->m_module_base); blk_addrs.insert(blk.m_vip.rva + m_vm->m_module_base);
// the vector containing the vblk's grows inside of this for loop // the vector containing the vblk's grows inside of this for loop
// thus we cannot use an advanced for loop (which uses itr's)... // thus we cannot use an advanced for loop (which uses itr's)...
@ -107,8 +112,7 @@ bool emu_t::emulate(std::uint32_t vmenter_rva, vm::instrs::vrtn_t& vrtn) {
// force the emulation of all branches... // force the emulation of all branches...
for (const auto br : blk.branches) { for (const auto br : blk.branches) {
// only emulate blocks that havent been emulated before... // only emulate blocks that havent been emulated before...
if (std::find(blk_addrs.begin(), blk_addrs.end(), br) != if (blk_addrs.find(br) != blk_addrs.end())
blk_addrs.end())
continue; continue;
std::uintptr_t vsp = 0ull; std::uintptr_t vsp = 0ull;
@ -120,18 +124,25 @@ 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...
// The stack is now set up with vsp pointing to the next branch to execute, therefore the jmp will go to that branch.
uc_mem_write(uc, vsp, &br, sizeof br); uc_mem_write(uc, vsp, &br, sizeof br);
std::printf("> beginning execution at = %p\n", blk.m_jmp.rip); std::printf("> beginning execution at = %p (%p)\n", blk.m_jmp.rip,
blk.m_jmp.rip - m_vm->m_module_base + m_vm->m_image_base);
std::printf("vsp: %s, vip: %s\n", ZydisRegisterGetString(cc_blk->m_vm.vsp),
ZydisRegisterGetString(cc_blk->m_vm.vip));
if ((err = uc_emu_start(uc, blk.m_jmp.rip, 0ull, 0ull, 0ull))) { if ((err = uc_emu_start(uc, blk.m_jmp.rip, 0ull, 0ull, 0ull))) {
std::printf("> error starting emu... reason = %d\n", err); std::printf("> error starting emu... reason = %d\n", err);
return false; return false;
} }
extract_branch_data(); extract_branch_data();
std::printf("> emulated blk_%p\n", cc_blk->m_vip.img_base); std::printf("> emulated blk_%p\n", cc_blk->m_vip.img_based);
// We needn't execute a block more than once for a single vrtn.
blk_addrs.insert(br);
} }
} }
} }
@ -139,71 +150,77 @@ bool emu_t::emulate(std::uint32_t vmenter_rva, vm::instrs::vrtn_t& vrtn) {
// free all virtual code block virtual jmp information... // free all virtual code block virtual jmp information...
std::for_each(vrtn.m_blks.begin(), vrtn.m_blks.end(), std::for_each(vrtn.m_blks.begin(), vrtn.m_blks.end(),
[&](vm::instrs::vblk_t& blk) { [&](vm::instrs::vblk_t& blk) {
if (blk.m_jmp.ctx) uc_context_free(blk.m_jmp.ctx); if (blk.m_jmp.ctx) uct_context_free(blk.m_jmp.ctx);
if (blk.m_jmp.stack) delete[] blk.m_jmp.stack; if (blk.m_jmp.stack) delete[] blk.m_jmp.stack; if (blk.m_jmp.stack) g_new_delete_tracker--;
}); });
return true; return true;
} }
void emu_t::extract_branch_data() { void emu_t::extract_branch_data() {
auto br_info = could_have_jcc(cc_blk->m_vinstrs); if (cc_blk->m_vinstrs.empty())
if (br_info.has_value()) { {
auto [br1, br2] = br_info.value(); cc_blk->branch_type = vm::instrs::vbranch_type::none;
// convert to absolute addresses... return;
br1 -= m_vm->m_image_base;
br2 -= m_vm->m_image_base;
br1 += m_vm->m_module_base;
br2 += m_vm->m_module_base;
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);
if (br1_legit && br2_legit) {
std::printf("> virtual jcc uncovered... br1 = %p, br2 = %p\n", br1, br2);
cc_blk->branch_type = vm::instrs::vbranch_type::jcc;
cc_blk->branches.push_back(br1);
cc_blk->branches.push_back(br2);
} else if (br1_legit || br2_legit) {
std::printf("> absolute virtual jmp uncovered... branch = %p\n",
br1_legit ? br1 : br2);
cc_blk->branch_type = vm::instrs::vbranch_type::absolute;
cc_blk->branches.push_back(br1_legit ? br1 : br2);
} else {
std::printf("> unknown branch type...\n");
} }
} else if (cc_blk->m_vinstrs.back().mnemonic == auto br_info = could_have_jcc(cc_blk->m_vinstrs);
vm::instrs::mnemonic_t::vmexit) { if (br_info.has_value()) {
cc_blk->branch_type = vm::instrs::vbranch_type::none; auto [br1, br2] = br_info.value();
} else if (cc_blk->m_vinstrs.back().mnemonic == vm::instrs::mnemonic_t::jmp) { // convert to absolute addresses...
// see if there is 1 lconst... br1 -= m_vm->m_image_base;
if (auto last_lconst = std::find_if( br2 -= m_vm->m_image_base;
cc_blk->m_vinstrs.rbegin(), cc_blk->m_vinstrs.rend(), br1 += m_vm->m_module_base;
[&](vm::instrs::vinstr_t& vinstr) -> bool { br2 += m_vm->m_module_base;
return vinstr.mnemonic == vm::instrs::mnemonic_t::lconst &&
vinstr.imm.size == 64; auto br1_legit = legit_branch(*cc_blk, br1);
}); auto br2_legit = legit_branch(*cc_blk, br2);
last_lconst != cc_blk->m_vinstrs.rend()) { std::printf("> br1 legit: %d, br2 legit: %d\n", br1_legit, br2_legit);
const auto imm_img_based = last_lconst->imm.val;
const auto imm_mod_based = if (br1_legit && br2_legit) {
(imm_img_based - m_vm->m_image_base) + m_vm->m_module_base; std::printf("> virtual jcc uncovered... br1 = %p, br2 = %p\n", br1, br2);
cc_blk->branch_type = vm::instrs::vbranch_type::jcc;
// check to see if the imm is inside of the module... and if the ptr lands cc_blk->branches.push_back(br1);
// inside of an executable section... then lastly check to see if its a cc_blk->branches.push_back(br2);
// legit branch or not... } else if (br1_legit || br2_legit) {
if (imm_img_based >= m_vm->m_image_base && std::printf("> absolute virtual jmp uncovered... branch = %p\n",
imm_img_based < m_vm->m_image_base + m_vm->m_image_size && br1_legit ? br1 : br2);
vm::utils::scn::executable(m_vm->m_module_base, imm_mod_based)) {
cc_blk->branches.push_back(imm_mod_based);
cc_blk->branch_type = vm::instrs::vbranch_type::absolute; cc_blk->branch_type = vm::instrs::vbranch_type::absolute;
} cc_blk->branches.push_back(br1_legit ? br1 : br2);
} else { } else {
std::printf("> jump table detected... review instruction stream...\n"); std::printf("> unknown branch type...\n");
uc_emu_stop(uc); }
} else if (cc_blk->m_vinstrs.back().mnemonic ==
vm::instrs::mnemonic_t::vmexit) {
cc_blk->branch_type = vm::instrs::vbranch_type::none;
} else if (cc_blk->m_vinstrs.back().mnemonic == vm::instrs::mnemonic_t::jmp) {
// see if there is 1 lconst...
if (auto last_lconst = std::find_if(
cc_blk->m_vinstrs.rbegin(), cc_blk->m_vinstrs.rend(),
[&](vm::instrs::vinstr_t& vinstr) -> bool {
return vinstr.mnemonic == vm::instrs::mnemonic_t::lconst &&
vinstr.imm.size == 64;
});
last_lconst != cc_blk->m_vinstrs.rend()) {
const auto imm_img_based = last_lconst->imm.val;
const auto imm_mod_based =
(imm_img_based - m_vm->m_image_base) + m_vm->m_module_base;
// check to see if the imm is inside of the module... and if the ptr lands
// inside of an executable section... then lastly check to see if its a
// legit branch or not...
if (imm_img_based >= m_vm->m_image_base &&
imm_img_based < m_vm->m_image_base + m_vm->m_image_size &&
vm::utils::scn::executable(m_vm->m_module_base, imm_mod_based)) {
cc_blk->branches.push_back(imm_mod_based);
cc_blk->branch_type = vm::instrs::vbranch_type::absolute;
std::printf("> absolute virtual jmp uncovered... branch = %p\n", imm_mod_based);
}
} else {
std::printf("> jump table detected... review instruction stream...\n");
uc_emu_stop(uc);
}
} }
}
} }
void emu_t::int_callback(uc_engine* uc, std::uint32_t intno, emu_t* obj) { void emu_t::int_callback(uc_engine* uc, std::uint32_t intno, emu_t* obj) {
@ -252,18 +269,13 @@ bool emu_t::branch_pred_spec_exec(uc_engine* uc, uint64_t address,
return false; return false;
} }
if (instr.mnemonic == ZYDIS_MNEMONIC_INVALID) return false; if (instr.mnemonic == ZYDIS_MNEMONIC_INVALID) {std::printf("> bad instruction\n"); return false;}
uc_context* ctx; uc_context* ctx;
uc_context_alloc(uc, &ctx); uct_context_alloc(uc, &ctx);
uc_context_save(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.empty())
if (!obj->cc_trace.m_instrs.size()) { obj->cc_trace.m_begin = address;
obj->cc_trace.m_stack = new std::uint8_t[STACK_SIZE];
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...
@ -274,7 +286,7 @@ bool emu_t::branch_pred_spec_exec(uc_engine* uc, uint64_t address,
// 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);
// 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... // 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(),
@ -285,21 +297,26 @@ bool emu_t::branch_pred_spec_exec(uc_engine* uc, uint64_t address,
i.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER && i.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER &&
i.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY && i.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY &&
i.operands[1].mem.base == vip && i.operands[1].size == 32; i.operands[1].mem.base == vip && i.operands[1].size == 32;
}); });
if (rva_fetch != obj->cc_trace.m_instrs.rend()) if (rva_fetch != obj->cc_trace.m_instrs.rend())
{
for (auto itr = (rva_fetch + 1).base(); itr < obj->cc_trace.m_instrs.end(); itr++)
{
uct_context_free(itr->m_cpu);
}
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);
// -- 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) {
uc_context_free(instr.m_cpu); uct_context_free(instr.m_cpu);
}); });
delete[] obj->cc_trace.m_stack;
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) {
@ -310,6 +327,9 @@ bool emu_t::branch_pred_spec_exec(uc_engine* uc, uint64_t address,
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); 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... // -- 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);
} }
@ -335,12 +355,14 @@ bool emu_t::code_exec_callback(uc_engine* uc, uint64_t address, uint32_t size,
if (instr.mnemonic == ZYDIS_MNEMONIC_INVALID) return false; if (instr.mnemonic == ZYDIS_MNEMONIC_INVALID) return false;
uc_context* ctx; uc_context* ctx;
uc_context_alloc(uc, &ctx); uct_context_alloc(uc, &ctx);
uc_context_save(uc, ctx); uc_context_save(uc, ctx);
// if this is the first instruction of this handler then save the stack... // if this is the first instruction of this handler then save the stack...
if (!obj->cc_trace.m_instrs.size()) { if (!obj->cc_trace.m_instrs.size()) {
obj->cc_trace.m_stack = new std::uint8_t[STACK_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++;
obj->cc_trace.m_begin = address; obj->cc_trace.m_begin = address;
uc_mem_read(uc, STACK_BASE, obj->cc_trace.m_stack, STACK_SIZE); uc_mem_read(uc, STACK_BASE, obj->cc_trace.m_stack, STACK_SIZE);
} }
@ -357,7 +379,9 @@ bool emu_t::code_exec_callback(uc_engine* uc, uint64_t address, uint32_t size,
// 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... // remove any instructions from this instruction to the JMP/RET...
const auto rva_fetch = std::find_if( // This may be more performant and elegant, but it fucks my new jmp profiler.
// I will keep it in as a comment for now in case I can make a jmp profiler compatible with this.
/* 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(),
[& vip = obj->cc_trace.m_vip]( [& vip = obj->cc_trace.m_vip](
const vm::instrs::emu_instr_t& instr) -> bool { const vm::instrs::emu_instr_t& instr) -> bool {
@ -369,11 +393,17 @@ bool emu_t::code_exec_callback(uc_engine* uc, uint64_t address, uint32_t size,
}); });
if (rva_fetch != obj->cc_trace.m_instrs.rend()) if (rva_fetch != obj->cc_trace.m_instrs.rend())
{
for (auto itr = (rva_fetch + 1).base(); itr < --obj->cc_trace.m_instrs.end(); ++itr)
{
uct_context_free(itr->m_cpu);
}
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());
} */
// set the virtual code block vip address information... // set the virtual code block vip address information...
if (!obj->cc_blk->m_vip.rva || !obj->cc_blk->m_vip.img_base) { if (!obj->cc_blk->m_vip.rva || !obj->cc_blk->m_vip.img_based) {
// find the last write done to VIP... // find the last write done to VIP...
auto vip_write = std::find_if( auto vip_write = 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(),
@ -385,28 +415,37 @@ bool emu_t::code_exec_callback(uc_engine* uc, uint64_t address, uint32_t size,
}); });
uc_context* backup; uc_context* backup;
uc_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);
obj->cc_blk->m_vip.rva = vip_addr -= obj->m_vm->m_module_base; obj->cc_blk->m_vip.rva = vip_addr -= obj->m_vm->m_module_base;
obj->cc_blk->m_vip.img_base = vip_addr += obj->m_vm->m_image_base; obj->cc_blk->m_vip.img_based = vip_addr += obj->m_vm->m_image_base;
uc_context_restore(uc, backup); uc_context_restore(uc, backup);
uc_context_free(backup); uct_context_free(backup);
} else { } else {
const auto vinstr = vm::instrs::determine(obj->cc_trace); const auto vinstr = vm::instrs::determine(obj->cc_trace);
if (vinstr.mnemonic != vm::instrs::mnemonic_t::unknown) { if (vinstr.mnemonic != vm::instrs::mnemonic_t::unknown) {
if (vinstr.imm.has_imm) if (obj->m_log_instructions) {
std::printf("> %s %p\n", std::printf("%p: ", obj->cc_trace.m_begin + obj->m_vm->m_image_base - obj->m_vm->m_module_base);
vm::instrs::get_profile(vinstr.mnemonic)->name.c_str(), if (vinstr.imm.has_imm)
vinstr.imm.val); if (vinstr.mnemonic == instrs::mnemonic_t::lreg || vinstr.mnemonic == instrs::mnemonic_t::sreg)
else std::printf("> %s %s_%s\n",
std::printf("> %s\n", vm::instrs::get_profile(vinstr.mnemonic)->name.c_str(),
vm::instrs::get_profile(vinstr.mnemonic)->name.c_str()); vm::reg_names::prefixes[(vinstr.imm.val / 8) % 8].c_str(),
vm::reg_names::suffixes[(vinstr.imm.val / 8) / 8].c_str());
else
std::printf("> %s %p\n",
vm::instrs::get_profile(vinstr.mnemonic)->name.c_str(),
vinstr.imm.val);
else
std::printf("> %s\n",
vm::instrs::get_profile(vinstr.mnemonic)->name.c_str());
}
} else { } else {
zydis_rtn_t inst_stream; zydis_rtn_t inst_stream;
std::for_each(obj->cc_trace.m_instrs.begin(), std::for_each(obj->cc_trace.m_instrs.begin(),
@ -419,19 +458,18 @@ bool emu_t::code_exec_callback(uc_engine* uc, uint64_t address, uint32_t size,
"> err: please define the following vm handler (at = %p):\n", "> err: please define the following vm handler (at = %p):\n",
(obj->cc_trace.m_begin - obj->m_vm->m_module_base) + (obj->cc_trace.m_begin - obj->m_vm->m_module_base) +
obj->m_vm->m_image_base); obj->m_vm->m_image_base);
vm::utils::print(inst_stream); vm::utils::print(inst_stream);
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;
// backup current unicorn-engine context... // backup current unicorn-engine context...
uc_context_alloc(uc, &backup); uct_context_alloc(uc, &backup);
uc_context_alloc(uc, &copy); uct_context_alloc(uc, &copy);
uc_context_save(uc, backup); uc_context_save(uc, backup);
// make a copy of the first cpu context of the jmp handler... // make a copy of the first cpu context of the jmp handler...
@ -440,36 +478,64 @@ bool emu_t::code_exec_callback(uc_engine* uc, uint64_t address, uint32_t size,
// restore the unicorn-engine context... also free the backup... // restore the unicorn-engine context... also free the backup...
uc_context_restore(uc, backup); uc_context_restore(uc, backup);
uc_context_free(backup); uct_context_free(backup);
// set current code block virtual jmp instruction information... // set current code block virtual jmp instruction information...
obj->cc_blk->m_jmp.ctx = copy; obj->cc_blk->m_jmp.ctx = copy;
obj->cc_blk->m_jmp.rip = obj->cc_trace.m_begin; obj->cc_blk->m_jmp.rip = obj->cc_trace.m_begin;
obj->cc_blk->m_jmp.stack = new std::uint8_t[STACK_SIZE]; obj->cc_blk->m_jmp.stack = new std::uint8_t[STACK_SIZE]; g_new_delete_tracker++;
obj->cc_blk->m_jmp.m_vm = {obj->cc_trace.m_vip, obj->cc_trace.m_vsp}; obj->cc_blk->m_jmp.m_vm = {obj->cc_trace.m_vip, obj->cc_trace.m_vsp};
std::memcpy(obj->cc_blk->m_jmp.stack, obj->cc_trace.m_stack, std::memcpy(obj->cc_blk->m_jmp.stack, obj->cc_trace.m_stack,
STACK_SIZE); STACK_SIZE);
} }
if (vinstr.mnemonic == vm::instrs::mnemonic_t::vmexit)
{
int i = 0;
for (const auto& instr_it : obj->cc_trace.m_instrs)
{
if ((instr_it.m_instr.mnemonic == ZYDIS_MNEMONIC_POP ||
instr_it.m_instr.mnemonic == ZYDIS_MNEMONIC_POPFQ) &&
instr_it.m_instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER)
{
if (i >= 16)
{
std::printf("[!] failed to parse vmexit pops\n");
return false;
}
auto pushed_reg = instr_it.m_instr.operands[instr_it.m_instr.mnemonic == ZYDIS_MNEMONIC_POPFQ ? 2 : 0].reg.value;
if (std::find(obj->cc_blk->vmexit_pop_order.begin(), obj->cc_blk->vmexit_pop_order.begin() + i, pushed_reg)
!= obj->cc_blk->vmexit_pop_order.begin() + i)
{
std::printf("[!] failed to parse vmexit pops\n");
return false;
}
obj->cc_blk->vmexit_pop_order[i++] = pushed_reg;
}
}
if (i != 16)
{
std::printf("[!] failed to parse vmexit pops\n");
return false;
}
}
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);
} }
// -- 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) {
uc_context_free(instr.m_cpu); uct_context_free(instr.m_cpu);
}); });
delete[] obj->cc_trace.m_stack; 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();
} }
return true; return true;
} }
void emu_t::invalid_mem(uc_engine* uc, uc_mem_type type, uint64_t address, void emu_t::invalid_mem(uc_engine* uc, uc_mem_type type, uint64_t address,
int size, int64_t value, emu_t* obj) { int size, int64_t value, emu_t* obj) {
@ -509,9 +575,9 @@ bool emu_t::legit_branch(vm::instrs::vblk_t& vblk, std::uintptr_t branch_addr) {
// make a backup of the current emulation state... // make a backup of the current emulation state...
uc_context* backup; uc_context* backup;
uc_context_alloc(uc, &backup); uct_context_alloc(uc, &backup);
uc_context_save(uc, backup); uc_context_save(uc, backup);
std::uint8_t* stack = new std::uint8_t[STACK_SIZE]; std::uint8_t* stack = new std::uint8_t[STACK_SIZE]; g_new_delete_tracker++;
uc_mem_read(uc, STACK_BASE, stack, STACK_SIZE); uc_mem_read(uc, STACK_BASE, stack, STACK_SIZE);
// restore cpu and stack back to the virtual jump handler... // restore cpu and stack back to the virtual jump handler...
@ -523,6 +589,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, UC_X86_REG_RIP, &rip);
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);
@ -530,8 +600,8 @@ bool emu_t::legit_branch(vm::instrs::vblk_t& vblk, std::uintptr_t branch_addr) {
// restore original cpu and stack... // restore original cpu and stack...
uc_mem_write(uc, STACK_BASE, stack, STACK_SIZE); uc_mem_write(uc, STACK_BASE, stack, STACK_SIZE);
uc_context_restore(uc, backup); uc_context_restore(uc, backup);
uc_context_free(backup); uct_context_free(backup);
delete[] stack; delete[] stack; if (stack) g_new_delete_tracker--;
// add normal execution callback back... // add normal execution callback back...
uc_hook_del(uc, branch_pred_hook); uc_hook_del(uc, branch_pred_hook);
@ -564,31 +634,32 @@ 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;
}; };
const auto lconst1 = static const auto valid_mem = [=](uintptr_t loc)
std::find_if(vinstrs.rbegin(), vinstrs.rend(), lconst64_chk); {
// check to see if the imm val is inside of the image...
if (lconst1 == vinstrs.rend()) return {}; if (loc > m_vm->m_image_base + m_vm->m_image_size ||
loc < m_vm->m_image_base ||
const auto lconst2 = std::find_if(lconst1 + 1, vinstrs.rend(), lconst64_chk); 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 {};
} 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 {}; if (lconst2 == vinstrs.rend()) return {};
} while (!valid_mem(lconst1->imm.val));
// check to see if the imm val is inside of the image... std::printf("possible jcc: %p, %p\n", lconst1->imm.val, lconst2->imm.val);
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