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.
130 lines
4.0 KiB
130 lines
4.0 KiB
4 years ago
|
#pragma once
|
||
|
#include <Windows.h>
|
||
|
#include <algorithm>
|
||
4 years ago
|
#include <vmprofiler.hpp>
|
||
3 years ago
|
#include <xtils.hpp>
|
||
4 years ago
|
|
||
4 years ago
|
#include "parser.h"
|
||
|
|
||
|
namespace vm
|
||
|
{
|
||
3 years ago
|
/// <summary>
|
||
|
/// struct containing encoded data for a given virtual instruction...
|
||
|
/// </summary>
|
||
4 years ago
|
struct vinstr_data
|
||
|
{
|
||
3 years ago
|
/// <summary>
|
||
|
/// vm handler index also known as the opcode...
|
||
|
/// </summary>
|
||
4 years ago
|
std::uint8_t vm_handler;
|
||
3 years ago
|
|
||
|
/// <summary>
|
||
|
/// this field contains the second operand if any...
|
||
|
/// </summary>
|
||
4 years ago
|
std::uint64_t operand;
|
||
3 years ago
|
|
||
|
/// <summary>
|
||
|
/// size in bits of the second operand if any... zero if none...
|
||
|
/// </summary>
|
||
|
std::uint8_t imm_size;
|
||
4 years ago
|
};
|
||
4 years ago
|
|
||
3 years ago
|
/// <summary>
|
||
|
/// struct containing all information for a label...
|
||
|
/// </summary>
|
||
4 years ago
|
struct vlabel_data
|
||
|
{
|
||
3 years ago
|
/// <summary>
|
||
|
/// name of the label...
|
||
|
/// </summary>
|
||
4 years ago
|
std::string label_name;
|
||
3 years ago
|
|
||
|
/// <summary>
|
||
|
/// vector of encoded virtual instructions...
|
||
|
/// </summary>
|
||
4 years ago
|
std::vector< vinstr_data > vinstrs;
|
||
|
};
|
||
|
|
||
3 years ago
|
/// <summary>
|
||
|
/// struct containing compiled virtual instructions (encoded and encrypted) for a given label...
|
||
|
/// </summary>
|
||
4 years ago
|
struct compiled_label_data
|
||
|
{
|
||
3 years ago
|
/// <summary>
|
||
|
/// label name...
|
||
|
/// </summary>
|
||
4 years ago
|
std::string label_name;
|
||
3 years ago
|
|
||
|
/// <summary>
|
||
|
/// relative virtual address from vm_entry to the virtual instructions...
|
||
|
/// </summary>
|
||
|
std::uintptr_t alloc_rva;
|
||
|
|
||
|
/// <summary>
|
||
|
/// encrypted relative virtual address from vm_entry to virtual instructions...
|
||
|
/// </summary>
|
||
|
std::uintptr_t enc_alloc_rva;
|
||
|
|
||
|
/// <summary>
|
||
|
/// vector of bytes containing the raw, encrypted virtual instructions...
|
||
|
/// </summary>
|
||
4 years ago
|
std::vector< std::uint8_t > vinstrs;
|
||
|
};
|
||
|
|
||
3 years ago
|
/// <summary>
|
||
|
/// class containing member functions used to encode and encrypted virtual instructions...
|
||
|
/// </summary>
|
||
4 years ago
|
class compiler_t
|
||
|
{
|
||
|
public:
|
||
3 years ago
|
/// <summary>
|
||
|
/// default constructor
|
||
|
/// </summary>
|
||
|
/// <param name="vmctx">pointer to a vm context object which has already been init...</param>
|
||
4 years ago
|
explicit compiler_t( vm::ctx_t *vmctx );
|
||
3 years ago
|
|
||
|
/// <summary>
|
||
|
/// encode virtual instructions from parser::virt_labels
|
||
|
/// </summary>
|
||
|
/// <returns>returns a vector of labels containing encoded virtual instructions</returns>
|
||
4 years ago
|
std::vector< vlabel_data > *encode();
|
||
3 years ago
|
|
||
|
/// <summary>
|
||
|
/// encrypt virtual instructions from parser::virt_labels
|
||
|
/// </summary>
|
||
|
/// <returns>returns a vector of compiled labels containing encoded and encrypted virtual
|
||
|
/// instructions...</returns>
|
||
4 years ago
|
std::vector< compiled_label_data > encrypt();
|
||
4 years ago
|
|
||
4 years ago
|
private:
|
||
3 years ago
|
/// <summary>
|
||
|
/// encrypt virtual instructions rva... <a href="https://back.engineering/17/05/2021/#vm_entry">read more
|
||
|
/// here...</a>
|
||
|
/// </summary>
|
||
|
/// <param name="rva">relative virtual address to encrypted virtual instructions...</param>
|
||
|
/// <returns></returns>
|
||
4 years ago
|
std::uint64_t encrypt_rva( std::uint64_t rva );
|
||
|
|
||
3 years ago
|
/// <summary>
|
||
|
/// pointer to the vmctx passed in by the constructor...
|
||
|
/// </summary>
|
||
4 years ago
|
vm::ctx_t *vmctx;
|
||
3 years ago
|
|
||
|
/// <summary>
|
||
|
/// transformations used to decrypt the opcode operand extracted from calc_jmp...
|
||
|
/// you can read more <a href="https://back.engineering/17/05/2021/#calc_jmp">here...</a>
|
||
|
/// </summary>
|
||
4 years ago
|
transform::map_t calc_jmp_transforms;
|
||
3 years ago
|
|
||
|
/// <summary>
|
||
|
/// vector of encoded labels...
|
||
|
/// </summary>
|
||
4 years ago
|
std::vector< vlabel_data > virt_labels;
|
||
3 years ago
|
|
||
|
/// <summary>
|
||
|
/// vector of decoded zydis instructions containing the native instructions to encrypt the virtual instruction
|
||
|
/// rva which will be pushed onto the stack prior to jmping to vm entry...
|
||
|
/// </summary>
|
||
4 years ago
|
std::vector< zydis_decoded_instr_t > encrypt_vinstrs_rva;
|
||
|
};
|
||
|
} // namespace vm
|