You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

321 lines
10 KiB

#include <vmemu_t.hpp>
namespace vm {
emu_t::emu_t(vm::vmctx_t* vm_ctx)
: m_vm(vm_ctx), vip(vm_ctx->get_vip()), vsp(vm_ctx->get_vsp()) {}
emu_t::~emu_t() {
if (uc)
uc_close(uc);
}
bool emu_t::init() {
uc_err err;
if ((err = uc_open(UC_ARCH_X86, UC_MODE_64, &uc))) {
std::printf("> uc_open err = %d\n", err);
return false;
}
if ((err = uc_mem_map(uc, STACK_BASE, STACK_SIZE, UC_PROT_ALL))) {
std::printf("> uc_mem_map stack err, reason = %d\n", err);
return false;
}
if ((err = uc_mem_map(uc, m_vm->m_module_base, m_vm->m_image_size,
UC_PROT_ALL))) {
std::printf("> map memory failed, reason = %d\n", err);
return false;
}
if ((err = uc_mem_write(uc, m_vm->m_module_base,
reinterpret_cast<void*>(m_vm->m_module_base),
m_vm->m_image_size))) {
std::printf("> failed to write memory... reason = %d\n", err);
return false;
}
if ((err = uc_hook_add(uc, &code_exec_hook, UC_HOOK_CODE,
(void*)&vm::emu_t::code_exec_callback, this,
m_vm->m_module_base,
m_vm->m_module_base + m_vm->m_image_size))) {
std::printf("> uc_hook_add error, reason = %d\n", err);
return false;
}
if ((err = uc_hook_add(uc, &int_hook, UC_HOOK_INTR,
(void*)&vm::emu_t::int_callback, this, 0ull, 0ull))) {
std::printf("> uc_hook_add error, reason = %d\n", err);
return false;
}
if ((err =
uc_hook_add(uc, &invalid_mem_hook,
UC_HOOK_MEM_READ_UNMAPPED | UC_HOOK_MEM_WRITE_UNMAPPED |
UC_HOOK_MEM_FETCH_UNMAPPED,
(void*)&vm::emu_t::invalid_mem, this, true, false))) {
std::printf("> uc_hook_add error, reason = %d\n", err);
return false;
}
return true;
}
vm::instrs::vinstr_t emu_t::step() {
m_single_step.m_toggle = true;
uc_err err;
std::uintptr_t rip = m_vm->m_vm_entry_rva + m_vm->m_module_base,
rsp = STACK_BASE + STACK_SIZE - PAGE_4KB;
if ((err = uc_reg_write(uc, UC_X86_REG_RSP, &rsp))) {
std::printf("> uc_reg_write error, reason = %d\n", err);
return;
}
if ((err = uc_reg_write(uc, UC_X86_REG_RIP, &rip))) {
std::printf("> uc_reg_write error, reason = %d\n", err);
return;
}
cc_trace.m_uc = uc;
cc_trace.m_vip = vip;
cc_trace.m_vsp = vsp;
// -- if there is already exists a cpu context back up then restore from it...
if (m_single_step.cpu_context) {
uc_context_restore(uc, m_single_step.cpu_context);
uc_mem_write(uc, STACK_BASE, m_single_step.stack, STACK_SIZE);
}
// -- single step emulate...
std::printf("> beginning execution at = %p\n", rip);
if ((err = uc_emu_start(uc, rip, 0ull, 0ull, 0ull))) {
std::printf("> error starting emu... reason = %d\n", err);
return;
}
// -- allocate new memory context...
if (!m_single_step.cpu_context) {
uc_context* ctx;
uc_context_alloc(uc, &ctx);
m_single_step.cpu_context = ctx;
}
// -- save cpu and stack...
uc_context_save(uc, m_single_step.cpu_context);
uc_mem_read(uc, STACK_BASE, m_single_step.stack, STACK_SIZE);
m_single_step.m_toggle = false;
return vinstrs.back();
}
void emu_t::emulate() {
uc_err err;
std::uintptr_t rip = m_vm->m_vm_entry_rva + m_vm->m_module_base,
rsp = STACK_BASE + STACK_SIZE - PAGE_4KB;
if ((err = uc_reg_write(uc, UC_X86_REG_RSP, &rsp))) {
std::printf("> uc_reg_write error, reason = %d\n", err);
return;
}
if ((err = uc_reg_write(uc, UC_X86_REG_RIP, &rip))) {
std::printf("> uc_reg_write error, reason = %d\n", err);
return;
}
cc_trace.m_uc = uc;
cc_trace.m_vip = vip;
cc_trace.m_vsp = vsp;
std::printf("> beginning execution at = %p\n", rip);
if ((err = uc_emu_start(uc, rip, 0ull, 0ull, 0ull))) {
std::printf("> error starting emu... reason = %d\n", err);
return;
}
const auto jcc_result = has_jcc(vinstrs);
std::printf("> jcc result = %d\n", jcc_result.has_value());
}
void emu_t::int_callback(uc_engine* uc, std::uint32_t intno, emu_t* obj) {
uc_err err;
std::uintptr_t rip = 0ull;
static thread_local zydis_decoded_instr_t instr;
if ((err = uc_reg_read(uc, UC_X86_REG_RIP, &rip))) {
std::printf("> failed to read rip... reason = %d\n", err);
return;
}
if (!ZYAN_SUCCESS(ZydisDecoderDecodeBuffer(vm::utils::g_decoder.get(),
reinterpret_cast<void*>(rip),
PAGE_4KB, &instr))) {
std::printf("> failed to decode instruction at = 0x%p\n", rip);
if ((err = uc_emu_stop(uc))) {
std::printf("> failed to stop emulation, exiting... reason = %d\n", err);
exit(0);
}
return;
}
// advance rip over the instruction that caused the exception... this is
// usually a division by 0...
rip += instr.length;
if ((err = uc_reg_write(uc, UC_X86_REG_RIP, &rip))) {
std::printf("> failed to write rip... reason = %d\n", err);
return;
}
}
bool emu_t::code_exec_callback(uc_engine* uc,
uint64_t address,
uint32_t size,
emu_t* obj) {
uc_err err;
static thread_local zydis_decoded_instr_t instr;
if (!ZYAN_SUCCESS(ZydisDecoderDecodeBuffer(vm::utils::g_decoder.get(),
reinterpret_cast<void*>(address),
PAGE_4KB, &instr))) {
std::printf("> failed to decode instruction at = 0x%p\n", address);
if ((err = uc_emu_stop(uc))) {
std::printf("> failed to stop emulation, exiting... reason = %d\n", err);
exit(0);
}
return false;
}
if (instr.mnemonic == ZYDIS_MNEMONIC_INVALID)
return false;
// save the current cpu's context (all register values and such)...
// create a new emu_instr_t with this information... this info will be used by
// profiles to grab decrypted values and such...
uc_context* cpu_ctx;
uc_context_alloc(obj->uc, &cpu_ctx);
uc_context_save(obj->uc, cpu_ctx);
vm::instrs::emu_instr_t emu_instr{instr, cpu_ctx};
obj->cc_trace.m_instrs.push_back(emu_instr);
// RET or JMP REG means the end of a vm handler...
if (instr.mnemonic == ZYDIS_MNEMONIC_RET ||
(instr.mnemonic == ZYDIS_MNEMONIC_JMP &&
instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER)) {
// deobfuscate the instruction stream before profiling...
// 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
// 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(),
[&vip = obj->vip](const vm::instrs::emu_instr_t& instr) -> bool {
const auto& i = instr.m_instr;
return i.mnemonic == ZYDIS_MNEMONIC_MOV &&
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())
obj->cc_trace.m_instrs.erase((rva_fetch + 1).base(),
obj->cc_trace.m_instrs.end());
const auto vinstr =
vm::instrs::determine(obj->vip, obj->vsp, obj->cc_trace);
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});
});
if (vinstr.mnemonic != vm::instrs::mnemonic_t::unknown) {
if (vinstr.imm.has_imm)
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 {
vm::utils::print(inst_stream);
std::getchar();
}
obj->cc_trace.m_vip = obj->vip;
obj->cc_trace.m_vsp = obj->vsp;
obj->vinstrs.push_back(vinstr);
// -- 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) {
uc_context_free(instr.m_cpu);
});
obj->cc_trace.m_instrs.clear();
if (vinstr.mnemonic == vm::instrs::mnemonic_t::jmp ||
vinstr.mnemonic == vm::instrs::mnemonic_t::vmexit ||
obj->m_single_step.m_toggle)
uc_emu_stop(obj->uc);
}
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) {
switch (type) {
case UC_MEM_READ_UNMAPPED: {
uc_mem_map(uc, address & ~0xFFFull, PAGE_4KB, UC_PROT_ALL);
std::printf(">>> reading invalid memory at address = %p, size = 0x%x\n",
address, size);
break;
}
case UC_MEM_WRITE_UNMAPPED: {
uc_mem_map(uc, address & ~0xFFFull, PAGE_4KB, UC_PROT_ALL);
std::printf(
">>> writing invalid memory at address = %p, size = 0x%x, val = "
"0x%x\n",
address, size, value);
break;
}
case UC_MEM_FETCH_UNMAPPED: {
std::printf(">>> fetching invalid instructions at address = %p\n",
address);
std::uintptr_t rip, rsp;
uc_reg_read(uc, UC_X86_REG_RSP, &rsp);
uc_mem_read(uc, rsp, &rip, sizeof rip);
rsp += 8;
uc_reg_write(uc, UC_X86_REG_RSP, &rsp);
uc_reg_write(uc, UC_X86_REG_RIP, &rip);
std::printf(">>> injecting return to try and recover... rip = %p\n", rip);
break;
}
default:
break;
}
}
std::optional<std::pair<std::uintptr_t, std::uintptr_t>> emu_t::has_jcc(
std::vector<vm::instrs::vinstr_t>& vinstrs) {
if (vinstrs.back().mnemonic == vm::instrs::mnemonic_t::vmexit)
return {};
// number of LCONST virtual instructions which load 64bit imm's...
const std::uint32_t lconst_num = std::accumulate(
vinstrs.begin(), vinstrs.end(), 0,
[&](std::uint32_t val, vm::instrs::vinstr_t& v) -> std::uint32_t {
return v.mnemonic == vm::instrs::mnemonic_t::lconst && v.imm.size == 64
? ++val
: val;
});
std::printf("> number of LCONST = %d\n", lconst_num);
return {};
}
} // namespace vm