more prepared to write virtual branch detection code now...

main
John Doe 3 years ago
parent c35a646f64
commit 29a40b4421

2
deps/vmprofiler vendored

@ -1 +1 @@
Subproject commit b018ca7999b7a5bd372f56fad4d11f1f9033f255 Subproject commit c5df1338acfb22485387e4b96d26051640b3a931

@ -24,13 +24,22 @@ class emu_t {
uc_engine* uc; uc_engine* uc;
const vm::vmctx_t* m_vm; const vm::vmctx_t* m_vm;
zydis_reg_t vip, vsp; zydis_reg_t vip, vsp;
/// <summary>
/// current code trace...
/// </summary>
vm::instrs::hndlr_trace_t cc_trace; vm::instrs::hndlr_trace_t cc_trace;
/// <summary>
/// current virtual code block...
/// block...
/// </summary>
vm::instrs::vblk_t* cc_blk; vm::instrs::vblk_t* cc_blk;
/// <summary> /// <summary>
/// set to true when emulating the vm enter... /// current code virtual routine...
/// </summary> /// </summary>
bool m_vm_enter; vm::instrs::vrtn_t* cc_vrtn;
/// <summary> /// <summary>
/// unicorn engine hook /// unicorn engine hook
@ -80,13 +89,21 @@ class emu_t {
static void int_callback(uc_engine* uc, std::uint32_t intno, emu_t* obj); static void int_callback(uc_engine* uc, std::uint32_t intno, emu_t* obj);
/// <summary> /// <summary>
/// determines if there is a JCC in the virtual instruction stream, if there /// determines if its possible that the virtual instruction stream contains a
/// is returns a pair of image based addresses for both of the branches... /// virtual JCC...
///
/// this simply checks to see if there are at least 3 LCONST that load 64bit
/// constant values...
///
/// it also checks if the last 2 LCONST's load image based addresses which
/// land inside of executable sections...
///
/// this function cannot be used to determine if there is a virtual branch or
/// not... it is only a useful/preliminary function...
/// </summary> /// </summary>
/// <param name="vinstrs">vector of virtual instructions...</param> /// <param name="vinstrs">vector of virtual instructions...</param>
/// <returns>returns a pair of imaged based addresses, one for each branch /// <returns>returns true if there is at least 3 LCONST in the virtual
/// address... if there is no jcc then it returns nothing...</returns> /// instruction stream that load 64bit values...</returns>
std::optional<std::pair<std::uintptr_t, std::uintptr_t>> has_jcc( bool could_have_jcc(std::vector<vm::instrs::vinstr_t>& vinstrs);
std::vector<vm::instrs::vinstr_t>& vinstrs);
}; };
} // namespace vm } // namespace vm

@ -78,12 +78,12 @@ bool emu_t::emulate(std::uint32_t vmenter_rva, vm::instrs::vrtn_t& vrtn) {
cc_trace.m_vip = vip; cc_trace.m_vip = vip;
cc_trace.m_vsp = vsp; cc_trace.m_vsp = vsp;
vrtn.m_rva = vmenter_rva; vrtn.m_rva = vmenter_rva;
m_vm_enter = true;
vm::instrs::vblk_t blk; vm::instrs::vblk_t blk;
blk.m_vip = {0ull, 0ull}; blk.m_vip = {0ull, 0ull};
blk.m_cpu = {nullptr, nullptr};
cc_blk = &blk; cc_blk = &blk;
cc_vrtn = &vrtn;
std::printf("> beginning execution at = %p\n", rip); 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))) {
@ -91,9 +91,9 @@ bool emu_t::emulate(std::uint32_t vmenter_rva, vm::instrs::vrtn_t& vrtn) {
return false; return false;
} }
std::printf("> blk address = %p\n", blk.m_vip.img_base); std::printf(
const auto jcc_result = has_jcc(blk.m_vinstrs); "> blk_%p, number of virtual instructions = %d, could have a jcc = %d\n",
std::printf("> jcc result = %d\n", jcc_result.has_value()); blk.m_vip.img_base, blk.m_vinstrs.size(), could_have_jcc(blk.m_vinstrs));
return true; return true;
} }
@ -148,18 +148,17 @@ bool emu_t::code_exec_callback(uc_engine* uc,
if (instr.mnemonic == ZYDIS_MNEMONIC_INVALID) if (instr.mnemonic == ZYDIS_MNEMONIC_INVALID)
return false; return false;
// save the current cpu's context (all register values and such)... uc_context* ctx;
// create a new emu_instr_t with this information... this info will be used by uc_context_alloc(uc, &ctx);
// profiles to grab decrypted values and such... uc_context_save(uc, ctx);
uc_context* cpu_ctx;
uc_context_alloc(obj->uc, &cpu_ctx);
uc_context_save(obj->uc, cpu_ctx);
std::uint8_t* stack = reinterpret_cast<std::uint8_t*>(malloc(STACK_SIZE)); // if this is the first instruction of this handler then save the stack...
uc_mem_read(uc, STACK_BASE, stack, STACK_SIZE); if (!obj->cc_trace.m_instrs.size()) {
obj->cc_trace.m_stack = reinterpret_cast<std::uint8_t*>(malloc(STACK_SIZE));
uc_mem_read(uc, STACK_BASE, obj->cc_trace.m_stack, STACK_SIZE);
}
vm::instrs::emu_instr_t emu_instr{instr, cpu_ctx, stack}; obj->cc_trace.m_instrs.push_back({instr, ctx});
obj->cc_trace.m_instrs.push_back(emu_instr);
// RET or JMP REG means the end of a vm handler... // RET or JMP REG means the end of a vm handler...
if (instr.mnemonic == ZYDIS_MNEMONIC_RET || if (instr.mnemonic == ZYDIS_MNEMONIC_RET ||
@ -185,33 +184,32 @@ bool emu_t::code_exec_callback(uc_engine* uc,
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());
// extract vip address out of the vm enter trace... // set the virtual code block vip address information...
if (obj->m_vm_enter) { if (!obj->cc_blk->m_vip.rva || !obj->cc_blk->m_vip.img_base) {
auto vip_addr_set = 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(),
[&vip = obj->vip](vm::instrs::emu_instr_t& emu_instr) -> bool { [&vip = obj->vip](vm::instrs::emu_instr_t& instr) -> bool {
const auto& i = emu_instr.m_instr; const auto& i = instr.m_instr;
return i.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER && return i.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER &&
i.operands[0].reg.value == vip; i.operands[0].reg.value == vip;
}); });
// get the cpu context from the instruction after the instruction that
// writes to vip...
--vip_addr_set;
uc_context* backup; uc_context* backup;
uc_context_alloc(uc, &backup); uc_context_alloc(uc, &backup);
uc_context_save(uc, backup); uc_context_save(uc, backup);
uc_context_restore(uc, vip_addr_set->m_cpu); uc_context_restore(uc, (vip_write - 1)->m_cpu);
auto uc_reg =
vm::instrs::reg_map[vip_write->m_instr.operands[0].reg.value];
std::uintptr_t vip_addr = 0ull; std::uintptr_t vip_addr = 0ull;
uc_reg_read(uc, vm::instrs::reg_map[obj->vip], &vip_addr); uc_reg_read(uc, uc_reg, &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_base = vip_addr += obj->m_vm->m_image_base;
uc_context_restore(uc, backup); uc_context_restore(uc, backup);
uc_context_free(backup); uc_context_free(backup);
obj->m_vm_enter = false;
} else { } else {
const auto vinstr = const auto vinstr =
vm::instrs::determine(obj->vip, obj->vsp, obj->cc_trace); vm::instrs::determine(obj->vip, obj->vsp, obj->cc_trace);
@ -241,21 +239,25 @@ bool emu_t::code_exec_callback(uc_engine* uc,
obj->cc_blk->m_vinstrs.push_back(vinstr); obj->cc_blk->m_vinstrs.push_back(vinstr);
if (vinstr.mnemonic == vm::instrs::mnemonic_t::jmp) { if (vinstr.mnemonic == vm::instrs::mnemonic_t::jmp) {
uc_context *b1, *b2; uc_context *backup, *copy;
uc_context_alloc(uc, &b1);
uc_context_alloc(uc, &b2);
uc_context_save(uc, b1);
uc_context_restore(uc, obj->cc_trace.m_instrs.begin()->m_cpu);
uc_context_save(uc, b2);
uc_context_restore(uc, b1);
std::uint8_t* stack = // backup current unicorn-engine context...
reinterpret_cast<std::uint8_t*>(malloc(STACK_SIZE)); uc_context_alloc(uc, &backup);
uc_context_alloc(uc, &copy);
uc_context_save(uc, backup);
std::memcpy(stack, obj->cc_trace.m_instrs.begin()->stack, STACK_SIZE); // make a copy of the first cpu context of the jmp handler...
uc_context_restore(uc, obj->cc_trace.m_instrs.begin()->m_cpu);
uc_context_save(uc, copy);
// restore the unicorn-engine context... also free the backup...
uc_context_restore(uc, backup);
uc_context_free(backup);
obj->cc_blk->m_cpu.ctx = b2; // set current code block virtual jmp instruction information...
obj->cc_blk->m_cpu.stack = stack; obj->cc_blk->m_jmp.ctx = copy;
obj->cc_blk->m_jmp.stack = new std::uint8_t[STACK_SIZE];
uc_mem_read(uc, STACK_BASE, obj->cc_blk->m_jmp.stack, STACK_SIZE);
} }
if (vinstr.mnemonic == vm::instrs::mnemonic_t::jmp || if (vinstr.mnemonic == vm::instrs::mnemonic_t::jmp ||
@ -267,9 +269,9 @@ bool emu_t::code_exec_callback(uc_engine* uc,
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); uc_context_free(instr.m_cpu);
free(instr.stack);
}); });
free(obj->cc_trace.m_stack);
obj->cc_trace.m_instrs.clear(); obj->cc_trace.m_instrs.clear();
} }
return true; return true;
@ -314,49 +316,51 @@ void emu_t::invalid_mem(uc_engine* uc,
} }
} }
std::optional<std::pair<std::uintptr_t, std::uintptr_t>> emu_t::has_jcc( bool emu_t::could_have_jcc(std::vector<vm::instrs::vinstr_t>& vinstrs) {
std::vector<vm::instrs::vinstr_t>& vinstrs) { // check to see if there is at least 3 LCONST %i64's
if (vinstrs.back().mnemonic == vm::instrs::mnemonic_t::vmexit) if (std::accumulate(
return {}; vinstrs.begin(), vinstrs.end(), 0u,
[&](std::uint32_t val, vm::instrs::vinstr_t& v) -> std::uint32_t {
// number of LCONST virtual instructions which load 64bit imm's... return v.mnemonic == vm::instrs::mnemonic_t::lconst &&
const std::uint32_t lconst_num = std::accumulate( v.imm.size == 64
vinstrs.begin(), vinstrs.end(), 0, ? ++val
[&](std::uint32_t val, vm::instrs::vinstr_t& v) -> std::uint32_t { : val;
return v.mnemonic == vm::instrs::mnemonic_t::lconst && v.imm.size == 64 }) < 3)
? ++val return false;
: val;
}); // extract the lconst64's out of the virtual instruction stream...
static const auto lconst64_chk = [&](vm::instrs::vinstr_t& v) -> bool {
if (lconst_num < 3) return v.mnemonic == vm::instrs::mnemonic_t::lconst && v.imm.size == 64;
return {}; };
const auto lconst1 = std::find_if( const auto lconst1 =
vinstrs.rbegin(), vinstrs.rend(), [&](vm::instrs::vinstr_t& v) -> bool { std::find_if(vinstrs.rbegin(), vinstrs.rend(), lconst64_chk);
return v.mnemonic == vm::instrs::mnemonic_t::lconst && v.imm.size == 64;
}); if (lconst1 == vinstrs.rend())
return false;
const auto lconst2 = std::find_if(
lconst1 + 1, vinstrs.rend(), [&](vm::instrs::vinstr_t& v) -> bool { const auto lconst2 = std::find_if(lconst1 + 1, vinstrs.rend(), lconst64_chk);
return v.mnemonic == vm::instrs::mnemonic_t::lconst && v.imm.size == 64;
}); if (lconst2 == vinstrs.rend())
return false;
static const auto exec_callbk = [&](uc_engine* uc, uint64_t address,
uint32_t size, emu_t* obj) {}; // 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 ||
uc_context *backup, *br1, *br2; lconst1->imm.val < m_vm->m_image_base ||
uc_context_alloc(uc, &backup); lconst2->imm.val > m_vm->m_image_base + m_vm->m_image_size ||
uc_context_alloc(uc, &br1); lconst2->imm.val < m_vm->m_image_base)
uc_context_alloc(uc, &br2); return false;
uc_context_save(uc, backup);
// check to see if the imm's points to something inside of an executable
uc_context_restore(uc, cc_blk->m_cpu.ctx); // section...
uc_mem_write(uc, STACK_BASE, cc_blk->m_cpu.stack, STACK_SIZE); if (!vm::utils::scn::executable(
m_vm->m_module_base,
uc_context_restore(uc, backup); (lconst1->imm.val - m_vm->m_image_base) + m_vm->m_module_base) ||
uc_context_free(backup); !vm::utils::scn::executable(
uc_context_free(br1); m_vm->m_module_base,
uc_context_free(br2); (lconst2->imm.val - m_vm->m_image_base) + m_vm->m_module_base))
return {}; return false;
return true;
} }
} // namespace vm } // namespace vm
Loading…
Cancel
Save