vm题算是逆向中比较难的一种题型了,在这里详细的记录一下。
一
原理
vm_init
typedef struct
{
unsigned long R0; //寄存器
unsigned long R1;
unsigned long R2;
unsigned long R4;
unsigned char *rip; //指向正在解释的opcode地址
vm_opcode op_list[OPCODE_N]; //opcode列表,存放了所有的opcode及其对应的处理函数
}vm_cpu;
typedef struct
{
unsigned long opcode;
void (*handle)(void*);
}vm_opcode;
vm_start
void vm_start(vm_cpu *cpu)
{
cpu->eip = (unsigned char*)opcodes; //这里不是在上面就初始化过了吗???
while((*cpu->eip) != 0xf4)//如果opcode不为RET,就调用vm_dispatcher来解释执行
{
vm_dispatcher(*cpu->eip)
}
}
vm_dispatcher
void vm_dispatcher(vm_cpu *cpu)
{
int i;
for(i = 0; i < OPCODE_N; i++)
{
if(*cpu->eip == cpu->op_list[i].opcode)
{
cpu->op_list[i].handle(cpu);
break;
}
}
}
二
实战1
分析函数
0xF1
v2 = (int *)(a1->_rip + 2);
a1->_rip += 6LL;
0xF1, 0xE1, 0x00, 0x00, 0x00, 0x00,
0xF2
0xF5
0xF7
0xF8
0xF6
翻译
#include<stdio.h>
void myswap(char*a,char*b);
int main()
{
unsigned char opcode[575] = {
0xF5, 0xF1, 0xE1, 0x00, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x20, 0x00, 0x00, 0x00, 0xF1, 0xE1,
0x01, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x21, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x02, 0x00, 0x00,
0x00, 0xF2, 0xF1, 0xE4, 0x22, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x03, 0x00, 0x00, 0x00, 0xF2, 0xF1,
0xE4, 0x23, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x04, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x24, 0x00,
0x00, 0x00, 0xF1, 0xE1, 0x05, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x25, 0x00, 0x00, 0x00, 0xF1,
0xE1, 0x06, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x26, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x07, 0x00,
0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x27, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x08, 0x00, 0x00, 0x00, 0xF2,
0xF1, 0xE4, 0x28, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x09, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x29,
0x00, 0x00, 0x00, 0xF1, 0xE1, 0x0A, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x2A, 0x00, 0x00, 0x00,
0xF1, 0xE1, 0x0B, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x2B, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x0C,
0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x2C, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x0D, 0x00, 0x00, 0x00,
0xF2, 0xF1, 0xE4, 0x2D, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x0E, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4,
0x2E, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x0F, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x2F, 0x00, 0x00,
0x00, 0xF1, 0xE1, 0x10, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x30, 0x00, 0x00, 0x00, 0xF1, 0xE1,
0x11, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x31, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x12, 0x00, 0x00,
0x00, 0xF2, 0xF1, 0xE4, 0x32, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x13, 0x00, 0x00, 0x00, 0xF2, 0xF1,
0xE4, 0x33, 0x00, 0x00, 0x00, 0xF4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xF5, 0xF1, 0xE1, 0x00, 0x00, 0x00, 0x00, 0xF1, 0xE2, 0x01, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4,
0x00, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x01, 0x00, 0x00, 0x00, 0xF1, 0xE2, 0x02, 0x00, 0x00, 0x00,
0xF2, 0xF1, 0xE4, 0x01, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x02, 0x00, 0x00, 0x00, 0xF1, 0xE2, 0x03,
0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x02, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x03, 0x00, 0x00, 0x00,
0xF1, 0xE2, 0x04, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x03, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x04,
0x00, 0x00, 0x00, 0xF1, 0xE2, 0x05, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x04, 0x00, 0x00, 0x00,
0xF1, 0xE1, 0x05, 0x00, 0x00, 0x00, 0xF1, 0xE2, 0x06, 0x00, 0x00, 0x00, 0xF2, 0xF1, 0xE4, 0x05,
0x00, 0x00, 0x00, 0xF1, 0xE1, 0x06, 0x00, 0x00, 0x00, 0xF1, 0xE2, 0x07, 0x00, 0x00, 0x00, 0xF1,
0xE3, 0x08, 0x00, 0x00, 0x00, 0xF1, 0xE5, 0x0C, 0x00, 0x00, 0x00, 0xF6, 0xF7, 0xF1, 0xE4, 0x06,
0x00, 0x00, 0x00, 0xF1, 0xE1, 0x07, 0x00, 0x00, 0x00, 0xF1, 0xE2, 0x08, 0x00, 0x00, 0x00, 0xF1,
0xE3, 0x09, 0x00, 0x00, 0x00, 0xF1, 0xE5, 0x0C, 0x00, 0x00, 0x00, 0xF6, 0xF7, 0xF1, 0xE4, 0x07,
0x00, 0x00, 0x00, 0xF1, 0xE1, 0x08, 0x00, 0x00, 0x00, 0xF1, 0xE2, 0x09, 0x00, 0x00, 0x00, 0xF1,
0xE3, 0x0A, 0x00, 0x00, 0x00, 0xF1, 0xE5, 0x0C, 0x00, 0x00, 0x00, 0xF6, 0xF7, 0xF1, 0xE4, 0x08,
0x00, 0x00, 0x00, 0xF1, 0xE1, 0x0D, 0x00, 0x00, 0x00, 0xF1, 0xE2, 0x13, 0x00, 0x00, 0x00, 0xF8,
0xF1, 0xE4, 0x0D, 0x00, 0x00, 0x00, 0xF1, 0xE7, 0x13, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x0E, 0x00,
0x00, 0x00, 0xF1, 0xE2, 0x12, 0x00, 0x00, 0x00, 0xF8, 0xF1, 0xE4, 0x0E, 0x00, 0x00, 0x00, 0xF1,
0xE7, 0x12, 0x00, 0x00, 0x00, 0xF1, 0xE1, 0x0F, 0x00, 0x00, 0x00, 0xF1, 0xE2, 0x11, 0x00, 0x00,
0x00, 0xF8, 0xF1, 0xE4, 0x0F, 0x00, 0x00, 0x00, 0xF1, 0xE7, 0x11, 0x00, 0x00, 0x00,0xf4};
//翻译
for (int i = 0; i < 575; )
{
if (opcode[i] == 0xf1) // mov
{
switch (opcode[i+1])
{
case 0xE1:
//a1->R0 = *((char *)input + *v2);
printf("R0=input[%d]n",*(int*)&opcode[i+2]);
break;
case 0xE2:
//a1->R1 = *((char *)input + *v2);
printf("R1=input[%d]n",*(int*)&opcode[i+2]);
break;
case 0xE3:
//a1->R2 = *((char *)input + *v2);
printf("R2=input[%d]n",*(int*)&opcode[i+2]);
break;
case 0xE4:
//*((_BYTE *)input + *v2) = a1->R0;
printf("input[%d]=R0n",*(int*)&opcode[i+2]);
break;
case 0xE5:
//a1->R3 = *((char *)input + *v2);
printf("R3=input[%d]n",*(int*)&opcode[i+2]);
break;
case 0xE7:
//*((_BYTE *)input + *v2) = a1->R1;
printf("input[%d]=R1n",*(int*)&opcode[i+2]);
break;
default:
printf("mov wrong!!!!!n");
break;
}
i+=6;
}
else if (opcode[i] == 0xf2) // xor
{
printf("R0=R0^R1n");
i+=1;
}
else if (opcode[i] == 0xf5) // scanf
{
printf("please input:n");
i+=1;
}
else if (opcode[i] == 0xf4) // nop
{
printf("0xF4 nopn");
printf("n");
i+=1;
}
else if (opcode[i] == 0xf7) //*
{
printf("R0*=R3n");
i+=1;
}
else if (opcode[i] == 0xf8) // change
{
printf("change(R0,R1)n");
i+=1;
}
else if (opcode[i] == 0xf6) //
{
printf("R0=R2+2*R1+3*R0n");
i+=1;
}
else if(opcode[i]==0)
{
printf("nopn");
i++;
}
}
printf("over!!");
return 0;
}
please input: R1=18
R0=input[0]
R0=R0^R1 //input0^18
input[32]=R0
R0=input[1]
R0=R0^R1
input[33]=R0
R0=input[2]
R0=R0^R1
input[34]=R0
R0=input[3]
R0=R0^R1
input[35]=R0
R0=input[4]
R0=R0^R1
input[36]=R0
R0=input[5]
R0=R0^R1
input[37]=R0
R0=input[6]
R0=R0^R1
input[38]=R0
R0=input[7]
R0=R0^R1
input[39]=R0
R0=input[8]
R0=R0^R1
input[40]=R0
R0=input[9]
R0=R0^R1
input[41]=R0
R0=input[10]
R0=R0^R1
input[42]=R0
R0=input[11]
R0=R0^R1
input[43]=R0
R0=input[12]
R0=R0^R1
input[44]=R0
R0=input[13]
R0=R0^R1
input[45]=R0
R0=input[14]
R0=R0^R1
input[46]=R0
R0=input[15]
R0=R0^R1
input[47]=R0
R0=input[16]
R0=R0^R1
input[48]=R0
R0=input[17]
R0=R0^R1
input[49]=R0
R0=input[18]
R0=R0^R1
input[50]=R0
R0=input[19]
R0=R0^R1
input[51]=R0
0xF4 nop
for(int i=0;i<21;i++)
{
printf("%c",cpdata[i]^18);
}
//This_is_not_flag_233
please input:
R0=input[0]
R1=input[1]
R0=R0^R1
input[0]=R0
R0=input[1]
R1=input[2]
R0=R0^R1
input[1]=R0
R0=input[2]
R1=input[3]
R0=R0^R1
input[2]=R0
R0=input[3]
R1=input[4]
R0=R0^R1
input[3]=R0
R0=input[4]
R1=input[5]
R0=R0^R1
input[4]=R0
R0=input[5]
R1=input[6]
R0=R0^R1
input[5]=R0
R0=input[6] //input[6]=
R1=input[7]
R2=input[8]
R3=input[12]
R0=R2+2*R1+3*R0
R0*=R3
input[6]=R0
R0=input[7] //input[7]=
R1=input[8]
R2=input[9]
R3=input[12]
R0=R2+2*R1+3*R0
R0*=R3
input[7]=R0
R0=input[8] //input[8]=(input[8]/R3-2*input[9]-input[10])/4
R1=input[9]
R2=input[10]
R3=input[12]
R0=R2+2*R1+3*R0
R0*=R3
input[8]=R0
R0=input[13] //置换 13 19
R1=input[19]
change(R0,R1)
input[13]=R0
input[19]=R1
R0=input[14] //置换14 18
R1=input[18]
change(R0,R1)
input[14]=R0
input[18]=R1
R0=input[15] //置换15 17
R1=input[17]
change(R0,R1)
input[15]=R0
input[17]=R1
0xF4 nop
over!!
#include<stdio.h>
void myswap(char*a,char*b);
int main()
{
for(int i=30;i<127;i++)
{
if(realdata[8]==(unsigned char)((realdata[10]+2*realdata[9]+3*i)*realdata[12]))
{
//printf("flag[8]==%cn",i);
realdata[8]=i;
}
}
for(int i=30;i<127;i++)
{
if(realdata[7]==(unsigned char)((realdata[9]+2*realdata[8]+3*i)*realdata[12]))
{
//printf("flag[7]==%cn",i);
realdata[7]=i;
}
}
for(int i=30;i<127;i++)
{
int a=(realdata[8]+2*realdata[7]+3*i)*realdata[12];
if(realdata[6]==(unsigned char)((realdata[8]+2*realdata[7]+3*i)*realdata[12]))
{
//printf("flag[6]==%cn",i);
realdata[6]=i;
}
}
myswap(&realdata[13],&realdata[19]);
myswap(&realdata[14],&realdata[18]);
myswap(&realdata[15],&realdata[17]);
for(int i=0;i<20;i++)
{
printf("%c",realdata[i]);
}
return 0;
}
void myswap(char* a,char* b)
{
char t=*a;
*a=*b;
*b=t;
}
//Y0u_hav3_r3v3rs3_1t!
三
实战2
分析结构体
typedef struct
{
unsigned int R[6];
unsigned int eip;
unsigned int esp;
char zf;
vm_opcode op_list[OPCODE_N];
}vm_cpu;
typedef struct
{
unsigned int opcode;
void (*handle)(void*);
}vm_opcode;
分析函数
fun0
fun1
fun2
fun3
fun4
fun5
fun6
fun7
翻译
#include<stdio.h>
int main()
{
unsigned int R[6]={0,0,0,0,0,0};
int zf;
int esp=0;
unsigned int mystack[80]={0};
unsigned char opcode[137] = {
0x00, 0x03, 0x02, 0x00, 0x03, 0x00, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x00,
0x00, 0x03, 0x02, 0x32, 0x03, 0x00, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00,
0x00, 0x03, 0x02, 0x64, 0x03, 0x00, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x01, 0x00,
0x00, 0x03, 0x00, 0x08, 0x00, 0x02, 0x02, 0x01, 0x03, 0x04, 0x01, 0x00, 0x03, 0x05, 0x02, 0x00,
0x03, 0x00, 0x01, 0x02, 0x00, 0x02, 0x00, 0x01, 0x01, 0x00, 0x00, 0x03, 0x00, 0x01, 0x03, 0x00,
0x03, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x03, 0x01, 0x28, 0x04, 0x06, 0x5F, 0x05, 0x00, 0x00,
0x03, 0x03, 0x00, 0x02, 0x01, 0x00, 0x03, 0x02, 0x96, 0x03, 0x00, 0x02, 0x03, 0x00, 0x00, 0x00,
0x00, 0x04, 0x07, 0x88, 0x00, 0x03, 0x00, 0x01, 0x03, 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x03,
0x00, 0x03, 0x01, 0x28, 0x04, 0x07, 0x63, 0xFF, 0xFF };
unsigned int input[200] = {
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x0000009B, 0x000000A8, 0x00000002, 0x000000BC, 0x000000AC, 0x0000009C,
0x000000CE, 0x000000FA, 0x00000002, 0x000000B9, 0x000000FF, 0x0000003A, 0x00000074, 0x00000048,
0x00000019, 0x00000069, 0x000000E8, 0x00000003, 0x000000CB, 0x000000C9, 0x000000FF, 0x000000FC,
0x00000080, 0x000000D6, 0x0000008D, 0x000000D7, 0x00000072, 0x00000000, 0x000000A7, 0x0000001D,
0x0000003D, 0x00000099, 0x00000088, 0x00000099, 0x000000BF, 0x000000E8, 0x00000096, 0x0000002E,
0x0000005D, 0x00000057, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000000C9, 0x000000A9, 0x000000BD, 0x0000008B,
0x00000017, 0x000000C2, 0x0000006E, 0x000000F8, 0x000000F5, 0x0000006E, 0x00000063, 0x00000063,
0x000000D5, 0x00000046, 0x0000005D, 0x00000016, 0x00000098, 0x00000038, 0x00000030, 0x00000073,
0x00000038, 0x000000C1, 0x0000005E, 0x000000ED, 0x000000B0, 0x00000029, 0x0000005A, 0x00000018,
0x00000040, 0x000000A7, 0x000000FD, 0x0000000A, 0x0000001E, 0x00000078, 0x0000008B, 0x00000062,
0x000000DB, 0x0000000F, 0x0000008F, 0x0000009C, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00004800, 0x0000F100,
0x00004000, 0x00002100, 0x00003501, 0x00006400, 0x00007801, 0x0000F900, 0x00001801, 0x00005200,
0x00002500, 0x00005D01, 0x00004700, 0x0000FD00, 0x00006901, 0x00005C00, 0x0000AF01, 0x0000B200,
0x0000EC01, 0x00005201, 0x00004F01, 0x00001A01, 0x00005000, 0x00008501, 0x0000CD00, 0x00002300,
0x0000F800, 0x00000C00, 0x0000CF00, 0x00003D01, 0x00004501, 0x00008200, 0x0000D201, 0x00002901,
0x0000D501, 0x00000601, 0x0000A201, 0x0000DE00, 0x0000A601, 0x0000CA01, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000};
for(int i=0;i<137;)
{
if(opcode[i]==0)
{
int v2=opcode[i+1];
if(v2)
{
switch (v2)
{
case 1:
input[R[2]]=R[0];
printf("input[%d]=R[0](%d)n",R[2],R[0]);
break;
case 2:
printf("R[%d]=R[%d](%d)n",opcode[i+2],opcode[i+3],R[opcode[i+3]]);
R[opcode[i+2]]=R[opcode[i+3]];
//printf("R[%d]=R[%d]n",opcode[i+2],opcode[i+3]);
break;
case 3:
printf("R[%d]=opcode[%d](%d)n",opcode[i+2],i+3,opcode[i+3]);
R[opcode[i+2]]=opcode[i+3];
//printf("R[%d]=opcode[%d]n",opcode[i+2],i+3);
break;
default:
break;
}
}
else
{
printf("R0=input[%d](%d)n",R[2],input[R[2]]);
R[0]=input[R[2]];
//printf("R0=input[R2]n");
}
i+=4;
}
else if(opcode[i]==1)
{
int v2=opcode[i+1];
if (v2)
{
switch (v2)
{
case 1u:
//stack[++a1->_esp] = a1->R0; // push R0
mystack[++esp]=R[0];
printf("push R0(%d)n",R[0]);
break;
case 2u:
//stack[++a1->_esp] = a1->R2; // push R2
mystack[++esp]=R[2];
printf("push R2(%d)n",R[2]);
break;
case 3u:
//stack[++a1->_esp] = a1->R3; // push R3
mystack[++esp]=R[3];
printf("push R3(%d)n",R[3]);
break;
}
}
else
{
//stack[++a1->_esp] = a1->R0; // push R0
mystack[++esp]=R[0];
printf("push R0(%d)n",R[0]);
}
i+=2;
}
else if(opcode[i]==2)
{
int v2 =opcode[i+1];
if (v2)
{
switch (v2)
{
case 1u:
//a1->R1 = stack[a1->_esp--]; // pop R1
R[1]=mystack[esp--];
printf("pop R1(%d)n",R[1]);
break;
case 2u:
//a1->R2 = stack[a1->_esp--]; // pop R2
R[2]=mystack[esp--];
printf("pop R2(%d)n",R[2]);
break;
case 3u:
//a1->R3 = stack[a1->_esp--]; // pop R3
R[3]=mystack[esp--];
printf("pop R3(%d)n",R[3]);
break;
}
}
else
{
//a1->R0 = stack[a1->_esp--]; // pop R0
R[0]=mystack[esp--];
printf("pop R0(%d)n",R[0]);
}
i+=2;
}
else if(opcode[i]==3)
{
switch (opcode[i+1])
{
case 0u:
//*(&a1->R0 + opcode[a1->_eip + 2]) += *(&a1->R0 + opcode[a1->_eip + 3]);
printf("R[%d]+=R[%d] %d+=%dn",opcode[i+2],opcode[i+3],R[opcode[i+2]],R[opcode[i+3]]);
R[opcode[i+2]]+=R[opcode[i+3]];
//printf("R[%d]+=R[%d] %d+=%dn",opcode[i+2],opcode[i+3],R[opcode[i+2]],R[opcode[i+3]]);
break;
case 1u:
//*(&a1->R0 + opcode[a1->_eip + 2]) -= *(&a1->R0 + opcode[a1->_eip + 3]);
printf("R[%d]-=R[%d] %d-=%dn",opcode[i+2],opcode[i+3],R[opcode[i+2]],R[opcode[i+3]]);
R[opcode[i+2]]-=R[opcode[i+3]];
//printf("R[%d]-=R[%d] %d-=%dn",opcode[i+2],opcode[i+3],R[opcode[i+2]],R[opcode[i+3]]);
break;
case 2u:
//*(&a1->R0 + opcode[a1->_eip + 2]) *= *(&a1->R0 + opcode[a1->_eip + 3]);
printf("R[%d]*=R[%d] %d*=%dn",opcode[i+2],opcode[i+3],R[opcode[i+2]],R[opcode[i+3]]);
R[opcode[i+2]]*=R[opcode[i+3]];
//printf("R[%d]*=R[%d]n %d*=%d",opcode[i+2],opcode[i+3],R[opcode[i+2]],R[opcode[i+3]]);
break;
case 3u:
//*(&a1->R0 + opcode[a1->_eip + 2]) ^= *(&a1->R0 + opcode[a1->_eip + 3]);
printf("R[%d]^=R[%d] %d^=%dn",opcode[i+2],opcode[i+3],R[opcode[i+2]],R[opcode[i+3]]);
R[opcode[i+2]]^=R[opcode[i+3]];
//printf("R[%d]^=R[%d]n",opcode[i+2],opcode[i+3]);
break;
case 4u:
//*(&a1->R0 + opcode[a1->_eip + 2]) <<= *(&a1->R0 + opcode[a1->_eip + 3]);
printf("R[%d]<<=R[%d] %d<<%dn",opcode[i+2],opcode[i+3],R[opcode[i+2]],R[opcode[i+3]]);
R[opcode[i+2]]<<=R[opcode[i+3]];
//printf("R[%d]<<=R[%d]n",opcode[i+2],opcode[i+3]);
//*(&a1->R0 + opcode[a1->_eip + 2]) &= 0xFF00u;
printf("R[%d]&=0xff00; %d&=0xff00n",opcode[i+2],R[opcode[i+2]]);
R[opcode[i+2]]&=0xff00;
//printf("R[%d]&=0xff00;n",opcode[i+2]);
break;
case 5u:
//*(&a1->R0 + opcode[a1->_eip + 2]) = (unsigned int)*(&a1->R0 + opcode[a1->_eip + 2]) >> *(&a1->R0 + opcode[a1->_eip + 3]);
printf("R[%d]=R[%d]>>R[%d] %d=%d>>%dn",opcode[i+2],opcode[i+2],opcode[i+3],R[opcode[i+2]],R[opcode[i+2]],R[opcode[i+3]]);
R[opcode[i+2]]=R[opcode[i+2]]>>R[opcode[i+3]];
//printf("R[%d]=R[%d]>>R[%d]n",opcode[i+2],opcode[i+2],opcode[i+3]);
break;
default:
break;
}
i+=4;
}
else if(opcode[i]==4)
{
if (R[0] == R[1])
{
zf=0;
printf("R0=R1-->zf=0n");
}
if (R[0] != R[1])
{
// a1->_ezf = 1;
zf = 1;
printf("R0!=R1-->zf=1n");
}
i+=1;
}
else if(opcode[i]==5)
{
i=opcode[i+1];
printf("jmp to fun%dn",i);
}
else if(opcode[i]==6)
{
if (zf==1)
{
//result = (unsigned int)(a1->_eip + 2); // 相当于不跳转
i+=2;
printf("zf=1,eip+2n");
} // 如果等于zf==1
else
{
i=opcode[i+1];
printf("zf!=1,jmp to fun%dn",i);
}
}
else if(opcode[i]==7)
{
if(zf==1)
{
i=opcode[i+1];
printf("zf=1,jmp to fun%d",i);
}
else
{
i+=2;
printf("zf!=1,eip+2n");
}
}
else if(opcode[i]==0xff)
{
printf("结束n");
return 0;
}
else
{
printf("maybe wrong:%dn",i);
}
}
return 0;
}
//part1
R[2]=opcode[3](0)
R[2]+=R[3] 0+=0
R0=input[0](0)
R[1]=R[0](0)
R[2]=opcode[19](50)
R[2]+=R[3] 50+=0
R0=input[50](155)
R[1]+=R[0] 0+=155
R[2]=opcode[35](100)
R[2]+=R[3] 100+=0
R0=input[100](201)
R[1]^=R[0] 155^=201
R[0]=opcode[51](8)
R[2]=R[1](82)
R[1]<<=R[0] 82<<8
R[1]&=0xff00; 20992&=0xff00
R[2]=R[2]>>R[0] 82=82>>8
R[1]+=R[2] 20992+=0
R[0]=R[1](20992)
push R0(20992)
R[0]=opcode[77](1)
R[3]+=R[0] 0+=1
R[0]=R[3](1)
R[1]=opcode[89](40)
R0!=R1-->zf=1
zf=1,eip+2
jmp to fun0
//part2
R[2]=opcode[3](0)
R[2]+=R[3] 0+=1
R0=input[1](0)
R[1]=R[0](0)
R[2]=opcode[19](50)
R[2]+=R[3] 50+=1
R0=input[51](168)
R[1]+=R[0] 0+=168
R[2]=opcode[35](100)
R[2]+=R[3] 100+=1
R0=input[101](169)
R[1]^=R[0] 168^=169
R[0]=opcode[51](8)
R[2]=R[1](1)
R[1]<<=R[0] 1<<8
R[1]&=0xff00; 256&=0xff00
R[2]=R[2]>>R[0] 1=1>>8
R[1]+=R[2] 256+=0
R[0]=R[1](256)
push R0(256)
R[0]=opcode[77](1)
R[3]+=R[0] 1+=1
R[0]=R[3](2)
R[1]=opcode[89](40)
R0!=R1-->zf=1
zf=1,eip+2
jmp to fun0
R[2]=opcode[3](0)
R[2]+=R[3] 0+=2
R0=input[2](0)
R[1]=R[0](0)
R[2]=opcode[19](50)
R[2]+=R[3] 50+=2
R0=input[52](2)
R[1]+=R[0] 0+=2
R[2]=opcode[35](100)
R[2]+=R[3] 100+=2
R0=input[102](189)
R[1]^=R[0] 2^=189
R[0]=opcode[51](8)
R[2]=R[1](191)
R[1]<<=R[0] 191<<8
R[1]&=0xff00; 48896&=0xff00
R[2]=R[2]>>R[0] 191=191>>8
R[1]+=R[2] 48896+=0
R[0]=R[1](48896)
push R0(48896)
R[0]=opcode[77](1)
R[3]+=R[0] 2+=1
R[0]=R[3](3)
R[1]=opcode[89](40)
R0!=R1-->zf=1
zf=1,eip+2
jmp to fun0
………………
//part40
R[2]=opcode[3](0)
R[2]+=R[3] 0+=39
R0=input[39](0)
R[1]=R[0](0)
R[2]=opcode[19](50)
R[2]+=R[3] 50+=39
R0=input[89](87)
R[1]+=R[0] 0+=87
R[2]=opcode[35](100)
R[2]+=R[3] 100+=39
R0=input[139](156)
R[1]^=R[0] 87^=156
R[0]=opcode[51](8)
R[2]=R[1](203)
R[1]<<=R[0] 203<<8
R[1]&=0xff00; 51968&=0xff00
R[2]=R[2]>>R[0] 203=203>>8
R[1]+=R[2] 51968+=0
R[0]=R[1](51968)
push R0(51968)
R[0]=opcode[77](1)
R[3]+=R[0] 39+=1
R[0]=R[3](40)
R[1]=opcode[89](40)
R0=R1-->zf=0
zf!=1,jmp to fun95
R[3]=opcode[98](0)
pop R1(51968)
R[2]=opcode[104](150)
R[2]+=R[3] 150+=0
R0=input[150](18432)
R0!=R1-->zf=1
zf=1,jmp to fun136
结束
int R[6]={0};
// R[2]=opcode[3]; //
// R[2]+=R[3]; //
R[0]=input[0];
R[1]=R[0]; //R[1]=input[0]
//R[2]=opcode[19]; //
//R[2]+=R[3]; //
R[0]=input[50];
R[1]+=R[0]; //R[1]+=input[50]
//R[2]=opcode[35]; //
//R[2]+=R[3]; //
R[0]=input[100];
R[1]^=R[0]; //R[1]^=input[100]
R[0]=opcode[51];
R[2]=R[1];
R[1]<<=R[0]; // ((input[0]+input[50])^input[100])<<opcode[51];
R[1]&=0xff00; // R1=(((input[0]+input[50])^input[100])<<opcode[51])&0xff
R[2]=R[2]>>R[0]; // R[2]=((input[0]+input[50])^input[100])>>opcpde[51]
R[1]+=R[2]; //R1+=((input[0]+input[50])^input[100])>>opcpde[51]
R[0]=R[1];
//push R0;
R[0]=opcode[77];
R[3]+=R[0]; //R[3]+=opcode[77]
R[0]=R[3]; //R[0]=
R[1]=opcode[89]; // R1=40 ,第一次R0=1 接着2、3、4…………39 、40
比较R0和R1
R[2]=(input[0]+input[50])^input[100];
R[1]=(((input[0]+input[50])^input[100])<<opcode[51])&0xff00;
R[2]=((input[0]+input[50])^input[100])>>opcode[51];
R[1]=(((input[0]+input[50])^input[100])<<opcode[51])&0xff00+(((input[0]+input[50])^input[100])>>opcode[51]);
#include<stdio.h>
int main()
{
unsigned int cpdata[40]={0x00004800, 0x0000F100,
0x00004000, 0x00002100, 0x00003501, 0x00006400, 0x00007801, 0x0000F900, 0x00001801, 0x00005200,
0x00002500, 0x00005D01, 0x00004700, 0x0000FD00, 0x00006901, 0x00005C00, 0x0000AF01, 0x0000B200,
0x0000EC01, 0x00005201, 0x00004F01, 0x00001A01, 0x00005000, 0x00008501, 0x0000CD00, 0x00002300,
0x0000F800, 0x00000C00, 0x0000CF00, 0x00003D01, 0x00004501, 0x00008200, 0x0000D201, 0x00002901,
0x0000D501, 0x00000601, 0x0000A201, 0x0000DE00, 0x0000A601, 0x0000CA01};
unsigned int input[144] = {
0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031,
0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031,
0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031,
0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031,
0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031, 0x00000031,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x0000009B, 0x000000A8, 0x00000002, 0x000000BC, 0x000000AC, 0x0000009C,
0x000000CE, 0x000000FA, 0x00000002, 0x000000B9, 0x000000FF, 0x0000003A, 0x00000074, 0x00000048,
0x00000019, 0x00000069, 0x000000E8, 0x00000003, 0x000000CB, 0x000000C9, 0x000000FF, 0x000000FC,
0x00000080, 0x000000D6, 0x0000008D, 0x000000D7, 0x00000072, 0x00000000, 0x000000A7, 0x0000001D,
0x0000003D, 0x00000099, 0x00000088, 0x00000099, 0x000000BF, 0x000000E8, 0x00000096, 0x0000002E,
0x0000005D, 0x00000057, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000000C9, 0x000000A9, 0x000000BD, 0x0000008B,
0x00000017, 0x000000C2, 0x0000006E, 0x000000F8, 0x000000F5, 0x0000006E, 0x00000063, 0x00000063,
0x000000D5, 0x00000046, 0x0000005D, 0x00000016, 0x00000098, 0x00000038, 0x00000030, 0x00000073,
0x00000038, 0x000000C1, 0x0000005E, 0x000000ED, 0x000000B0, 0x00000029, 0x0000005A, 0x00000018,
0x00000040, 0x000000A7, 0x000000FD, 0x0000000A, 0x0000001E, 0x00000078, 0x0000008B, 0x00000062,
0x000000DB, 0x0000000F, 0x0000008F, 0x0000009C, 0x00000000, 0x00000000, 0x00000000, 0x00000000};
for(int i=0;i<40;i++)
{
int data=cpdata[39-i];
data=((data<<8)&0xff00)+(data>>8);
data^=input[100+i];
data-=input[50+i];
printf("%c",data);
}
return 0;
}
//hgame{y0ur_rever5e_sk1ll_i5_very_g0od!!}
看雪ID:马先越
https://bbs.kanxue.com/user-home-984774.htm
# 往期推荐
3、阿里云CTF2024-暴力ENOTYOURWORLD题解
球分享
球点赞
球在看
点击阅读原文查看更多
原文始发于微信公众号(看雪学苑):VM逆向,一篇就够了