1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
using namespace std; unsigned char target_func[] = {0xa0,0x10,0x00,0x00,0x00,0x00,0xa0,0x11,0x12,0x00,0x00,0x00,0xa4, 0xa0, 0x14, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x11, 0x29, 0x00,0x00, 0x00, 0xa1, 0xa2, 0x20, 0xa6, 0x5d, 0xa0, 0x14, 0x01, 0x00, 0x00, 0x00, 0xa1,0xa2, 0x21, 0xa6, 0x50, 0xa0, 0x14, 0x02, 0x00, 0x00, 0x00, 0xa1, 0xa2, 0x22, 0xa6,0x47, 0xa0, 0x14, 0x03, 0x00, 0x00, 0x00, 0xa1, 0xa2, 0x23, 0xa6, 0x3a, 0xa0, 0x14,0x04, 0x00, 0x00, 0x00, 0xa1, 0xa2, 0x24, 0xa6, 0x31, 0xa0, 0x14, 0x05, 0x00, 0x00,0x00, 0xa1, 0xa2, 0x25, 0xa6, 0x26, 0xa0, 0x14, 0x06, 0x00, 0x00, 0x00, 0xa1, 0xa2,0x26, 0xa6, 0x1b, 0xa0, 0x14, 0x07, 0x00, 0x00, 0x00, 0xa1, 0xa2, 0x27, 0xa6, 0x10,0xa0, 0x10, 0x30, 0x00, 0x00, 0x00, 0xa0, 0x11, 0x09, 0x00, 0x00, 0x00, 0xa5, 0xa3, 0xa0, 0x10, 0x40, 0x00, 0x00, 0x00, 0xa0, 0x11, 0x07, 0x00, 0x00, 0x00, 0xa5, 0xa3 }; void vm_xor(vm_processor *proc) { int arg1 = proc->r1; int arg2 = proc->r2; proc->r1 = arg1 ^ arg2; proc->eip += 1;//xor只占一个字节 } void vm_cmp(vm_processor *proc) { int arg1 = proc->r1; char *arg2 = *(proc->eip + 1) + heap_buf; if (arg1 == *arg2) { proc->flag = 1; } else { proc->flag = 0; } proc->eip += 2;//cmp占两个字节 } void vm_jnz(vm_processor *proc) { char arg1 = *(proc->eip + 1); if (proc->flag == 0) { proc->eip += arg1; } else proc->eip += 2;//jnz占两个字节 } void vm_ret(vm_processor *proc) { std::cout << "Finish"; } void vm_read(vm_processor *proc) { char *arg1 = heap_buf + proc->r1; int arg2 = proc->r2; cin.read(arg1, arg2); proc->eip += 1; } void vm_write(vm_processor *proc) { char *arg1 = heap_buf + proc->r1; int arg2 = proc->r2; cout.write(arg1, arg2); proc->eip += 1; } void vm_mov(vm_processor *proc) { unsigned char *dest = proc->eip + 1; int *src = (int*)(proc->eip + 2); switch (*dest) { case 0x10: proc->r1 = *src; break; case 0x11: proc->r2 = *src; break; case 0x12: proc->r3 = *src; break; case 0x13: proc->r4 = *src; break; case 0x14: proc->r1 = *(heap_buf + *src); default: break; } proc->eip += 6; } void exec_opcode(vm_processor *proc) { int flag = 0; int i = 0; while (!flag&&i < OPCODE_NUM) { if (*proc->eip == proc->op_table[i].opcode) { flag = 1; proc->op_table[i].func(proc); } else { i++; } } } void vm_interp(vm_processor *proc) { proc->eip = target_func; while (*proc->eip != RET) { exec_opcode(proc); } } //初始化处理器 void init_vm_processor(vm_processor *proc) { proc->r1 = 0; proc->r2 = 0; proc->r3 = 0; proc->r4 = 0; proc->flag = 0; proc->op_table[0].opcode = MOV; proc->op_table[1].opcode = XOR; proc->op_table[2].opcode = CMP; proc->op_table[3].opcode = RET; proc->op_table[4].opcode = SYS_READ; proc->op_table[5].opcode = SYS_WRITE; proc->op_table[6].opcode = JNZ; proc->op_table[0].func = (void(*)(void *))vm_mov; proc->op_table[1].func = (void(*)(void *))vm_xor; proc->op_table[2].func = (void(*)(void *))vm_cmp; proc->op_table[3].func = (void(*)(void *))vm_ret; proc->op_table[4].func = (void(*)(void *))vm_read; proc->op_table[5].func = (void(*)(void *))vm_write; proc->op_table[6].func = (void(*)(void *))vm_jnz; heap_buf = (char*)malloc(HEAP_SIZE_MAX); memcpy(heap_buf + 0x20, "syclover", 8); memcpy(heap_buf + 0x30, "success!\n", 9); memcpy(heap_buf + 0x40, "error!\n", 7); } int main() { vm_processor proc = { 0 }; //init vm processor init_vm_processor(&proc); vm_interp(&proc); return 0; }
|