#pragma once #include namespace vm { namespace handler { using instr_callback_t = bool(*)(const zydis_decoded_instr_t& instr); enum VM_MNEMONIC { INVALID, SREGQ, SREGDW, SREGW, LREGQ, LREGDW, LCONSTQ, LCONSTBZXW, LCONSTBSXDW, LCONSTDWSXQ, LCONSTWSXQ, LCONSTDW, PUSHVSP, ADDQ, ADDDW }; enum extention_t { none, sign_extend, zero_extend }; struct profile_t { const char* name; VM_MNEMONIC mnemonic; u8 imm_size; std::vector signature; extention_t extention; }; namespace profile { inline vm::handler::profile_t sregq = { // MOV RDX, [RBP] // ADD RBP, 8 // MOV [RAX+RDI], RDX "SREGQ", SREGQ, 8, { { // MOV RDX, [RBP] [](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 == ZYDIS_REGISTER_RDX && instr.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY && instr.operands[1].mem.base == ZYDIS_REGISTER_RBP; }, // ADD RBP, 8 [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE && instr.operands[1].imm.value.u == 8; }, // MOV [RAX+RDI], RDX or MOV [RDI+RAX], RDX [](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 == ZYDIS_REGISTER_RAX || instr.operands[0].mem.base == ZYDIS_REGISTER_RDI) && (instr.operands[0].mem.index == ZYDIS_REGISTER_RDI || instr.operands[0].mem.index == ZYDIS_REGISTER_RAX) && instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && instr.operands[1].reg.value == ZYDIS_REGISTER_RDX; } } } }; inline vm::handler::profile_t sregdw = { // MOV EDX, [RBP] // ADD RBP, 0x4 // MOV [RAX+RDI], EDX "SREGDW", SREGDW, 8, { { // MOV EDX, [RBP] [](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 == ZYDIS_REGISTER_EDX && instr.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY && instr.operands[1].mem.base == ZYDIS_REGISTER_RBP; }, // ADD RBP, 0x4 [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE && instr.operands[1].imm.value.u == 0x4; }, // MOV [RAX+RDI], EDX or MOV [RDI+RAX], EDX [](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 == ZYDIS_REGISTER_RAX || instr.operands[0].mem.base == ZYDIS_REGISTER_RDI) && (instr.operands[0].mem.index == ZYDIS_REGISTER_RAX || instr.operands[0].mem.index == ZYDIS_REGISTER_RDI) && instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && instr.operands[1].reg.value == ZYDIS_REGISTER_EDX; } } } }; inline vm::handler::profile_t sregw = { // MOV DX, [RBP] // ADD RBP, 0x2 // MOV [RAX+RDI], DX "SREGW", SREGW, 8, { { // MOV DX, [RBP] [](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 == ZYDIS_REGISTER_DX && instr.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY && instr.operands[1].mem.base == ZYDIS_REGISTER_RBP; }, // ADD RBP, 0x2 [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE && instr.operands[1].imm.value.u == 0x2; }, // MOV [RAX+RDI], DX or MOV [RDI+RAX], DX [](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 == ZYDIS_REGISTER_RAX || instr.operands[0].mem.base == ZYDIS_REGISTER_RDI) && (instr.operands[0].mem.index == ZYDIS_REGISTER_RDI || instr.operands[0].mem.index == ZYDIS_REGISTER_RAX) && instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && instr.operands[1].reg.value == ZYDIS_REGISTER_DX; } } } }; inline vm::handler::profile_t lregq = { // MOV RDX, [RAX+RDI] // SUB RBP, 8 // MOV [RBP], RDX "LREGQ", LREGQ, 8, { { // MOV RDX, [RAX+RDI] or MOV RDX, [RDI+RAX] [](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 == ZYDIS_REGISTER_RDX && instr.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY && (instr.operands[1].mem.base == ZYDIS_REGISTER_RAX || instr.operands[1].mem.base == ZYDIS_REGISTER_RDI) && (instr.operands[1].mem.index == ZYDIS_REGISTER_RDI || instr.operands[1].mem.index == ZYDIS_REGISTER_RAX); }, // SUB RBP, 8 [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE && instr.operands[1].imm.value.u == 0x8; }, // MOV [RBP], RDX [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && instr.operands[1].reg.value == ZYDIS_REGISTER_RDX; } } } }; inline vm::handler::profile_t lregdw = { // MOVZX AL, [RSI] // MOV RDX, [RAX + RDI] // SUB RBP, 0x4 // MOV [RBP], EDX "LREGDW", LREGDW, 8, { { // MOV RDX, [RAX + RDI] or MOV RDX, [RDI + RAX] [](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 == ZYDIS_REGISTER_EDX && instr.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY && (instr.operands[1].mem.base == ZYDIS_REGISTER_RAX || instr.operands[1].mem.base == ZYDIS_REGISTER_RDI) && (instr.operands[1].mem.index == ZYDIS_REGISTER_RAX || instr.operands[1].mem.index == ZYDIS_REGISTER_RDI); }, // SUB RBP, 0x4 [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE && instr.operands[1].imm.value.u == 0x4; }, // MOV [RBP], EDX [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && instr.operands[1].reg.value == ZYDIS_REGISTER_EDX; } } } }; inline vm::handler::profile_t lconstq = { // MOV RAX, [RSI] // SUB RBP, 8 // MOV [RBP], RAX "LCONSTQ", LCONSTQ, 64, { { // SUB RBP, 8 [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE && instr.operands[1].imm.value.u == 0x8; }, // MOV [RBP], RAX [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && instr.operands[1].reg.value == ZYDIS_REGISTER_RAX; } } } }; inline vm::handler::profile_t lconstbzxw = { // MOV AL, [RSI] // SUB RBP, 2 // MOV [RBP], AX "LCONSTBZXW", LCONSTBZXW, 8, { { // SUB RBP, 2 [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE && instr.operands[1].imm.value.u == 0x2; }, // MOV [RBP], AX [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && instr.operands[1].reg.value == ZYDIS_REGISTER_AX; } } } }; inline vm::handler::profile_t lconstbsxdw = { // CWDE // SUB RBP, 4 // MOV [RBP], EAX "LCONSTBSXDW", LCONSTBSXDW, 8, { { // CWDE [](const zydis_decoded_instr_t& instr) -> bool { return instr.mnemonic == ZYDIS_MNEMONIC_CWDE; }, // SUB RBP, 4 [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE && instr.operands[1].imm.value.u == 0x4; }, // MOV [RBP], EAX [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && instr.operands[1].reg.value == ZYDIS_REGISTER_EAX; } } }, vm::handler::extention_t::sign_extend }; inline vm::handler::profile_t lconstdwsxq = { // CDQE // SUB RBP, 8 // MOV [RBP], RAX "LCONSTDWSXQ", LCONSTDWSXQ, 32, { // CDQE [](const zydis_decoded_instr_t& instr) -> bool { return instr.mnemonic == ZYDIS_MNEMONIC_CDQE; }, // SUB RBP, 8 [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE && instr.operands[1].imm.value.u == 0x8; }, // MOV [RBP], RAX [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && instr.operands[1].reg.value == ZYDIS_REGISTER_RAX; } }, vm::handler::extention_t::sign_extend }; inline vm::handler::profile_t lconstwsxq = { // CDQE // SUB RBP, 8 // MOV [RBP], RAX "LCONSTWSXQ", LCONSTWSXQ, 16, { { // CDQE [](const zydis_decoded_instr_t& instr) -> bool { return instr.mnemonic == ZYDIS_MNEMONIC_CDQE; }, // SUB RBP, 8 [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE && instr.operands[1].imm.value.u == 0x8; }, // MOV [RBP], RAX [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && instr.operands[1].reg.value == ZYDIS_REGISTER_RAX; } } }, vm::handler::extention_t::sign_extend }; inline vm::handler::profile_t lconstdw = { // SUB RBP, 4 // MOV [RBP], EAX "LCONSTDW", LCONSTDW, 32, { { // SUB RBP, 4 [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE && instr.operands[1].imm.value.u == 0x4; }, // MOV [RBP], EAX [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && instr.operands[1].reg.value == ZYDIS_REGISTER_EAX; } } } }; inline vm::handler::profile_t pushvsp = { // MOV RAX, RBP // SUB RBP, 8 "PUSHVSP", PUSHVSP, NULL, { { // MOV RAX, RBP [](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 == ZYDIS_REGISTER_RAX && instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && instr.operands[1].reg.value == ZYDIS_REGISTER_RBP; }, // SUB RBP, 8 [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE && instr.operands[1].imm.value.u == 0x8; }, // MOV [RBP], RAX [](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 == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && instr.operands[1].reg.value == ZYDIS_REGISTER_RAX; } } } }; inline vm::handler::profile_t addq = { // ADD [RBP+8], RAX // PUSHFQ // POP [RBP] "ADDQ", ADDQ, NULL, { { // ADD [RBP+8], RAX [](const zydis_decoded_instr_t& instr) -> bool { return instr.mnemonic == ZYDIS_MNEMONIC_ADD && instr.operands[0].type == ZYDIS_OPERAND_TYPE_MEMORY && instr.operands[1].mem.base == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && instr.operands[1].reg.value == ZYDIS_REGISTER_RAX; }, // PUSHFQ [](const zydis_decoded_instr_t& instr) -> bool { return instr.mnemonic == ZYDIS_MNEMONIC_PUSHFQ; }, // POP [RBP] [](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 == ZYDIS_REGISTER_RBP; } } } }; inline vm::handler::profile_t adddw = { // ADD [RBP+8], EAX // PUSHFQ // POP [RBP] "ADDDW", ADDDW, NULL, { { // ADD [RBP+8], EAX [](const zydis_decoded_instr_t& instr) -> bool { return instr.mnemonic == ZYDIS_MNEMONIC_ADD && instr.operands[0].type == ZYDIS_OPERAND_TYPE_MEMORY && instr.operands[1].mem.base == ZYDIS_REGISTER_RBP && instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER && instr.operands[1].reg.value == ZYDIS_REGISTER_EAX; }, // PUSHFQ [](const zydis_decoded_instr_t& instr) -> bool { return instr.mnemonic == ZYDIS_MNEMONIC_PUSHFQ; }, // POP [RBP] [](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 == ZYDIS_REGISTER_RBP; } } } }; inline std::vector all = { &sregq, &sregdw, &sregw, &lregq, &lregdw, &lconstq, &lconstbzxw, &lconstbsxdw, &lconstdwsxq, &lconstwsxq, &lconstdw, &pushvsp, &addq, &adddw }; } } }