From 2b4bb0ebed6ac57ffc3fa8549872b5fb4878f2ea Mon Sep 17 00:00:00 2001 From: John Doe Date: Mon, 27 Dec 2021 21:12:56 -0800 Subject: [PATCH] added imul and nor handlers... --- CMakeLists.txt | 1 + include/vminstrs.hpp | 8 +++-- src/vmprofiles/imul.cpp | 67 ++++++++++++++++++++++++++++++++++ src/vmprofiles/nor.cpp | 79 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 src/vmprofiles/imul.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b9ee349..02857c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,7 @@ list(APPEND vmprofiler_SOURCES "src/vminstrs.cpp" "src/vmlocate.cpp" "src/vmprofiles/add.cpp" + "src/vmprofiles/imul.cpp" "src/vmprofiles/jmp.cpp" "src/vmprofiles/lconst.cpp" "src/vmprofiles/lreg.cpp" diff --git a/include/vminstrs.hpp b/include/vminstrs.hpp index 726c5ae..d55eda1 100644 --- a/include/vminstrs.hpp +++ b/include/vminstrs.hpp @@ -17,6 +17,7 @@ enum class mnemonic_t { mul, imul, nand, + nor, read, write, shl, @@ -144,14 +145,17 @@ extern profiler_t add; extern profiler_t lvsp; extern profiler_t svsp; extern profiler_t nand; +extern profiler_t nor; extern profiler_t read; extern profiler_t write; +extern profiler_t imul; /// /// unsorted vector of profiles... they get sorted once at runtime... /// -inline std::vector profiles = { - &write, &svsp, &read, &nand, &lvsp, &add, &jmp, &sreg, &lreg, &lconst}; +inline std::vector profiles = {&imul, &nor, &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/imul.cpp b/src/vmprofiles/imul.cpp new file mode 100644 index 0000000..acf28e4 --- /dev/null +++ b/src/vmprofiles/imul.cpp @@ -0,0 +1,67 @@ +#include + +namespace vm::instrs { +profiler_t imul = { + "IMUL", + mnemonic_t::imul, + {{// 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; + }, + // IMUL REG + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_IMUL && + instr.operands[0].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::imul}; + res.imm.has_imm = false; + + const auto imul_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_IMUL && + i.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER; + }); + + res.stack_size = imul_reg->m_instr.operands[0].size; + return res; + }}; +} \ No newline at end of file diff --git a/src/vmprofiles/nor.cpp b/src/vmprofiles/nor.cpp index e69de29..b9db467 100644 --- a/src/vmprofiles/nor.cpp +++ b/src/vmprofiles/nor.cpp @@ -0,0 +1,79 @@ +#include + +namespace vm::instrs { +profiler_t nor = { + "NOR", + mnemonic_t::nor, + {{// 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; + }, + // OR REG, REG + [&](const zydis_reg_t vip, + const zydis_reg_t vsp, + const zydis_decoded_instr_t& instr) -> bool { + return instr.mnemonic == ZYDIS_MNEMONIC_OR && + 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