diff --git a/CMakeLists.txt b/CMakeLists.txt index 17fa165..b9ee349 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,7 +57,13 @@ list(APPEND vmprofiler_SOURCES "src/vmprofiles/jmp.cpp" "src/vmprofiles/lconst.cpp" "src/vmprofiles/lreg.cpp" + "src/vmprofiles/lvsp.cpp" + "src/vmprofiles/nand.cpp" + "src/vmprofiles/nor.cpp" + "src/vmprofiles/read.cpp" "src/vmprofiles/sreg.cpp" + "src/vmprofiles/svsp.cpp" + "src/vmprofiles/write.cpp" "src/vmutils.cpp" "include/vmctx.hpp" "include/vminstrs.hpp" diff --git a/include/vminstrs.hpp b/include/vminstrs.hpp index 6edb87f..726c5ae 100644 --- a/include/vminstrs.hpp +++ b/include/vminstrs.hpp @@ -141,11 +141,17 @@ extern profiler_t sreg; extern profiler_t lreg; extern profiler_t lconst; extern profiler_t add; +extern profiler_t lvsp; +extern profiler_t svsp; +extern profiler_t nand; +extern profiler_t read; +extern profiler_t write; /// /// unsorted vector of profiles... they get sorted once at runtime... /// -inline std::vector profiles = {&add, &jmp, &sreg, &lreg, &lconst}; +inline std::vector profiles = { + &write, &svsp, &read, &nand, &lvsp, &add, &jmp, &sreg, &lreg, &lconst}; /// /// no i did not make this by hand, you cannot clown upon me! diff --git a/src/vmprofiles/lconst.cpp b/src/vmprofiles/lconst.cpp index 7d654b5..e5532a5 100644 --- a/src/vmprofiles/lconst.cpp +++ b/src/vmprofiles/lconst.cpp @@ -58,6 +58,10 @@ profiler_t lconst = { vm::instrs::reg_map[mov_vsp_imm->m_instr.operands[1].reg.value]; uc_reg_read(hndlr.m_uc, imm_reg, &res.imm.val); + + res.imm.val <<= (64 - res.imm.size); + res.imm.val >>= (64 - res.imm.size); + uc_context_restore(hndlr.m_uc, backup); uc_context_free(backup); return res; diff --git a/src/vmprofiles/lreg.cpp b/src/vmprofiles/lreg.cpp index aca63ac..c86c50b 100644 --- a/src/vmprofiles/lreg.cpp +++ b/src/vmprofiles/lreg.cpp @@ -61,6 +61,10 @@ profiler_t lreg = { vm::instrs::reg_map[mov_reg_vreg->m_instr.operands[1].mem.index]; uc_reg_read(hndlr.m_uc, idx_reg, &res.imm.val); + + res.imm.val <<= (64 - res.imm.size); + res.imm.val >>= (64 - res.imm.size); + uc_context_restore(hndlr.m_uc, backup); uc_context_free(backup); return res; diff --git a/src/vmprofiles/lvsp.cpp b/src/vmprofiles/lvsp.cpp new file mode 100644 index 0000000..206227b --- /dev/null +++ b/src/vmprofiles/lvsp.cpp @@ -0,0 +1,37 @@ +#include + +namespace vm::instrs { +profiler_t lvsp = { + "LVSP", + mnemonic_t::lvsp, + {{// MOV VSP, [VSP] + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_MOV && + instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER && + instr.operands[0].reg.value == vsp && + instr.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY && + instr.operands[1].mem.base == vsp; + }}}, + [&](zydis_reg_t& vip, + zydis_reg_t& vsp, + hndlr_trace_t& hndlr) -> std::optional { + vinstr_t res{mnemonic_t::svsp}; + res.imm.has_imm = false; + + const auto load_vsp = std::find_if( + hndlr.m_instrs.begin(), hndlr.m_instrs.end(), + [&](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[0].reg.value == vsp && + i.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY && + i.operands[1].mem.base == vsp; + }); + + res.stack_size = load_vsp->m_instr.operands[0].size; + return res; + }}; +} \ No newline at end of file diff --git a/src/vmprofiles/nand.cpp b/src/vmprofiles/nand.cpp new file mode 100644 index 0000000..abe08cf --- /dev/null +++ b/src/vmprofiles/nand.cpp @@ -0,0 +1,79 @@ +#include + +namespace vm::instrs { +profiler_t nand = { + "NAND", + mnemonic_t::nand, + {{// MOV REG, [VSP] + LOAD_VALUE, + // MOV REG, [VSP+OFFSET] + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_MOV && + instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER && + instr.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY && + instr.operands[1].mem.base == vsp && + instr.operands[1].mem.disp.has_displacement; + }, + // NOT REG + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_NOT && + instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER; + }, + // AND REG, REG + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_AND && + instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER && + instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER; + }, + // MOV [VSP+OFFSET], REG + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_MOV && + instr.operands[0].type == ZYDIS_OPERAND_TYPE_MEMORY && + instr.operands[0].mem.base == vsp && + instr.operands[0].mem.disp.has_displacement && + instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER; + }, + // PUSHFQ + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_PUSHFQ; + }, + // POP [VSP] + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_POP && + instr.operands[0].type == ZYDIS_OPERAND_TYPE_MEMORY && + instr.operands[0].mem.base == vsp; + }}}, + [&](zydis_reg_t& vip, + zydis_reg_t& vsp, + hndlr_trace_t& hndlr) -> std::optional { + vinstr_t res{mnemonic_t::nand}; + res.imm.has_imm = false; + + // MOV [VSP+OFFSET], REG + const auto mov_vsp_reg = std::find_if( + hndlr.m_instrs.begin(), hndlr.m_instrs.end(), + [&](emu_instr_t& instr) -> bool { + const auto& i = instr.m_instr; + return i.mnemonic == ZYDIS_MNEMONIC_MOV && + i.operands[0].type == ZYDIS_OPERAND_TYPE_MEMORY && + i.operands[0].mem.base == vsp && + i.operands[0].mem.disp.has_displacement && + i.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER; + }); + + res.stack_size = mov_vsp_reg->m_instr.operands[1].size; + return res; + }}; +} \ No newline at end of file diff --git a/src/vmprofiles/nor.cpp b/src/vmprofiles/nor.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/vmprofiles/read.cpp b/src/vmprofiles/read.cpp new file mode 100644 index 0000000..5bbd100 --- /dev/null +++ b/src/vmprofiles/read.cpp @@ -0,0 +1,40 @@ +#include + +namespace vm::instrs { +profiler_t read = { + "READ", + mnemonic_t::read, + {{// MOV REG, [VSP] + LOAD_VALUE, + // MOV REG, [REG] + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_MOV && + instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER && + instr.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY && + instr.operands[1].mem.base != vsp; + }, + // MOV [VSP], REG + STR_VALUE}}, + [&](zydis_reg_t& vip, + zydis_reg_t& vsp, + hndlr_trace_t& hndlr) -> std::optional { + vinstr_t res{mnemonic_t::read}; + res.imm.has_imm = false; + + // MOV REG, [REG] + const auto mov_reg_reg = std::find_if( + hndlr.m_instrs.begin(), hndlr.m_instrs.end(), + [&](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 != vsp; + }); + + res.stack_size = mov_reg_reg->m_instr.operands[0].size; + return res; + }}; +} \ No newline at end of file diff --git a/src/vmprofiles/sreg.cpp b/src/vmprofiles/sreg.cpp index 8a6c3ae..5c3171f 100644 --- a/src/vmprofiles/sreg.cpp +++ b/src/vmprofiles/sreg.cpp @@ -67,6 +67,10 @@ profiler_t sreg = { vm::instrs::reg_map[mov_vreg_value->m_instr.operands[0].mem.index]; uc_reg_read(hndlr.m_uc, idx_reg, &res.imm.val); + + res.imm.val <<= (64 - res.imm.size); + res.imm.val >>= (64 - res.imm.size); + uc_context_restore(hndlr.m_uc, backup); uc_context_free(backup); return res; diff --git a/src/vmprofiles/svsp.cpp b/src/vmprofiles/svsp.cpp new file mode 100644 index 0000000..cc2d958 --- /dev/null +++ b/src/vmprofiles/svsp.cpp @@ -0,0 +1,53 @@ +#include + +namespace vm::instrs { +profiler_t svsp = { + "SVSP", + mnemonic_t::svsp, + {{// MOV REG, VSP + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_MOV && + instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER && + instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && + instr.operands[1].reg.value == vsp; + }, + // SUB VSP, OFFSET + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_SUB && + instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER && + instr.operands[0].reg.value == vsp && + instr.operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE; + }, + // MOV [VSP], REG + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_MOV && + instr.operands[0].type == ZYDIS_OPERAND_TYPE_MEMORY && + instr.operands[0].mem.base == vsp && + instr.operands[0].mem.disp.has_displacement == false && + instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER; + }}}, + [&](zydis_reg_t& vip, + zydis_reg_t& vsp, + hndlr_trace_t& hndlr) -> std::optional { + vinstr_t res{mnemonic_t::lvsp}; + const auto sub_vsp = std::find_if( + hndlr.m_instrs.begin(), hndlr.m_instrs.end(), + [&](emu_instr_t& instr) -> bool { + const auto& i = instr.m_instr; + return i.mnemonic == ZYDIS_MNEMONIC_SUB && + i.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER && + i.operands[0].reg.value == vsp && + i.operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE; + }); + + res.imm.has_imm = false; + res.stack_size = sub_vsp->m_instr.operands[1].imm.value.u; + return res; + }}; +} \ No newline at end of file diff --git a/src/vmprofiles/write.cpp b/src/vmprofiles/write.cpp new file mode 100644 index 0000000..64ab737 --- /dev/null +++ b/src/vmprofiles/write.cpp @@ -0,0 +1,58 @@ +#include + +namespace vm::instrs { +profiler_t write = { + "WRITE", + mnemonic_t::write, + {{// MOV REG, [VSP] + LOAD_VALUE, + // MOV REG, [VSP+OFFSET] + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_MOV && + instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER && + instr.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY && + instr.operands[1].mem.base == vsp && + instr.operands[1].mem.disp.has_displacement; + }, + // ADD VSP, OFFSET + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_ADD && + instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER && + instr.operands[0].reg.value == vsp && + instr.operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE; + }, + // MOV [REG], REG + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_MOV && + instr.operands[0].type == ZYDIS_OPERAND_TYPE_MEMORY && + instr.operands[0].mem.base != vsp && + instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && + instr.operands[1].reg.value != vsp; + }}}, + [&](zydis_reg_t& vip, + zydis_reg_t& vsp, + hndlr_trace_t& hndlr) -> std::optional { + vinstr_t res{mnemonic_t::write}; + res.imm.has_imm = false; + + const auto mov_reg_reg = std::find_if( + hndlr.m_instrs.begin(), hndlr.m_instrs.end(), + [&](emu_instr_t& instr) -> bool { + const auto& i = instr.m_instr; + return i.mnemonic == ZYDIS_MNEMONIC_MOV && + i.operands[0].type == ZYDIS_OPERAND_TYPE_MEMORY && + i.operands[0].mem.base != vsp && + i.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && + i.operands[1].reg.value != vsp; + }); + + res.stack_size = mov_reg_reg->m_instr.operands[1].size; + return res; + }}; +} \ No newline at end of file