VMProtect虚拟机保护分析入门(上)

本文主要向您介绍VMProtect虚拟机保护分析入门,欢迎查阅!

开始

以前在逆向分析的时候,遇见VMP的代码就束手无策,只能跳过。最近在分析的时候又遇见vmp,准备研究一下。我这次遇见的VMP用查壳工具看是VMProtect(1.60-2.05)[-]。所以本次选用的壳版本是VMP1.8,也可前往下载最新版试用!

VMP介绍

VMP全称VMProtect,号称目前软件保护最扣一道防线。为了防止逆向分析人员对软件的逆向分析,VMP最主要的是对指定关键代码进行虚拟化,同时再加一些乱序跳转和大量的废指令,反调试,内存保护,导入表保护,使逆向分析人员无法分析执行的代码,经过VMP虚拟机的代码被膨胀好多倍。本次学习只研究VMP最关键和最难的部分:虚拟化

初步对比

我在visual stdio里写了下面代码,并对加壳时TestVmpFunc函数选择虚拟化。本都得使用的调试器是x64dbg

#include <iostream>_declspec(naked) void TestVmpFunc(){__asm{mov eax,0x100mov ebx,0x1000add eax,ebxretn}}int main(){//下面这是特征码,用于在调试器里定位自己的这段代码__asm {mov eax,eaxmov eax,eax}while (true) {__asm {pushadmov eax, TestVmpFunccall eaxpopad}system("pause");}std::cout << "完成了" << std::endl;return 0;}

用调试器附加观察原来只有四条汇编指令:

VMProtect虚拟机保护分析入门

被虚拟化后成这样:

VMProtect虚拟机保护分析入门

代码被虚拟化之后,假如在调试器中单步执行会跳来跳去,一条汇编会变成成百上千条指令,无法判断他在干什么。

基本原理

经过一番查资料,知道本质来讲VMP是一个基于堆栈机的intel指令模拟器,对过编译把原来的intel指令编译成精心设计的一组虚拟指令,然后用自己的一套引擎来解释执行。VMP加壳后,他会将原来的代码进行删除,导致基本完全无法进行还原。

VMP是防止别人逆向分析自己的代码,逆向分析的目的是分析代码,了解代码逻辑和代码的目的,然后加以利用。看样子,目前只能通过对虚拟机引擎的分析,来搞懂虚拟机引擎,然后理清代码流程,达到逆向分析的目的。

自己实现一个简单的虚拟机加深了解

定义寄存器和内存

这里第8个寄存器为指令指针寄存器类似x86的eip

uint32_t g_regs[8];//8个寄存器uint32_t g_mem[1000];//1000个内存空间

这里为了简单,规定每条指令都有三个操作数(哪怕某一条指令用不到三个参数)

指令格式为:OPCODE r,s,t

//指令操作数struct Instruct {    uint32_t opcode;    uint32_t r;    uint32_t s;    uint32_t t;};

声明OPCode

enum OP_CODE {opSTOP,/*停止执行 忽略r,s,t参数*/opIN,/*读入一个值放到reg[r]里*/opOUT,/*将reg[r]的值输入*/opADD,/*regs[r] = regs[s] + regs[t]*/opLD,//regs[r]=dmem[regs[s] + t]opST,//dmem[regs[s] + t] = regs[r]opLDA,//regs[r]= regs[s]+topLDC,//regs[r]=t};
std::vector<instruct> g_instruct_list;//指令列表

初始化

void Init(){memset(g_regs, 0, sizeof(g_regs));g_instruct_list.clear();}

加载代码

void LoadCode(const std::string & file_name){//代码文件为txt文件//每行模式为opcode,r,s,t//例如:1,0,0,0std::ifstream file(file_name);if (!file.is_open()) {return;}auto GetOneInstruct = [&file](Instruct & instruct) {char elem;uint32_t values[4] = { 0 };bool success = true;for (int i = 0; i < 4 ; i++) {file >> values[i];if (file.fail()) {success = false;break;}if (i < 4 - 1) {file >> elem;}}if (!success) {return false;}instruct = { values[0],values[1],values[2],values[3] };return true;};Instruct instruct;while (GetOneInstruct(instruct)) {g_instruct_list.push_back(instruct);}}

运行指令

bool RunInstruct(const Instruct& instruct){switch (instruct.opcode) {case opSTOP:return false;case opIN:Handle_opIN(instruct);break;case opOUT:Handle_opOUT(instruct);break;case opADD:Handle_opADD(instruct);break;default:throw std::logic_error("Invalid Op Code:" + std::to_string(instruct.opcode));break;}return true;}void RunCode() {while (true) {uint32_t eip = g_regs[7];if (eip > g_instruct_list.size() - 1) {break;}const Instruct& instruct = g_instruct_list.at(eip);if (!RunInstruct(instruct)) {break;}g_regs[7]++;}}// handle处理void Handle_opIN(const Instruct& instruct);void Handle_opOUT(const Instruct& instruct);void Handle_opADD(const Instruct& instruct);void Handle_opLD(const Instruct& instruct);void Handle_opST(const Instruct& instruct);void Handle_opLDA(const Instruct& instruct);void Handle_opLDC(const Instruct& instruct);

测试

int main(){Init();LoadCode("asm.txt");RunCode();return -1;}

注:本文转自博客园/ 作者:张东升 / 如涉及侵权,请联系删除!

查阅→VMProtect虚拟机保护分析入门(下)

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年3月9日
下一篇 2022年3月9日

相关推荐

发表回复

登录后才能评论