-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathVirtualMachine.cpp
More file actions
715 lines (654 loc) · 18.4 KB
/
VirtualMachine.cpp
File metadata and controls
715 lines (654 loc) · 18.4 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
* Copyrigth (C) Mon May 20 10:39:21 2013 XiaoWei
*
* handsomexiaowei@gmail.com
*/
#include "VirtualMachine.h"
#include "PCode.hpp"
#define _STDINT_H
#include "Libudis86/udis86.h"
#include "algorithms.hpp"
VirtualMachine::VirtualMachine(long base)
{
build_vm_handle(base);
}
VirtualMachine::VirtualMachine(long base, bool sign)
{
handle.sign = sign;
build_vm_handle(base);
}
VirtualMachine::~VirtualMachine()
{
for (list <ppcode_block_info>::iterator iter = pcode_list.begin(); iter != pcode_list.end(); ++iter)
{
ppcode_block_info info = *iter;
delete info;
}
for (list <handle_info>::iterator iter = handle_info_list.begin(); iter != handle_info_list.end(); ++iter)
{
handle_info hi = *iter;
for (vector <encryption>::iterator eiter = hi.encode.begin(); eiter != hi.encode.end();++eiter) //删除encode对象
{
if (eiter->enfuc)
{
delete eiter->enfuc;
eiter->enfuc = NULL;
}
}
for (vector <decryption>::iterator diter = hi.decode_key.begin(); diter != hi.decode_key.end();++diter) //删除decode对象
{
if (diter->defuc)
{
delete diter->defuc;
diter->defuc = NULL;
}
}
for (list <vcode_encryption>::iterator eiter = hi.encode_pcode.begin(); eiter != hi.encode_pcode.end();++eiter)
{
if (eiter->enfuc)
{
delete eiter->enfuc;
eiter->enfuc = NULL;
}
}
for (list <vcode_decryption>::iterator diter = hi.decode_pcode.begin(); diter != hi.decode_pcode.end();++diter)
{
if (diter->defuc)
{
delete diter->defuc;
diter->defuc = NULL;
}
}
}
}
typedef handle_info (VMHandle::*v_handle)();
void VirtualMachine::build_vm_handle(long base)
{
v_handle handle_array[] =
{
&VMHandle::b_read_stack,
&VMHandle::w_read_stack,
&VMHandle::d_read_stack,
&VMHandle::b_write_stack,
&VMHandle::w_write_stack,
&VMHandle::d_write_stack,
&VMHandle::b_push_reg,
&VMHandle::w_puah_reg,
&VMHandle::d_push_reg,
&VMHandle::b_pop_reg,
&VMHandle::w_pop_reg,
&VMHandle::d_pop_reg,
&VMHandle::b_push_imm,
&VMHandle::w_push_imm,
&VMHandle::d_push_imm,
&VMHandle::b_shl,
&VMHandle::w_shl,
&VMHandle::d_shl,
&VMHandle::b_shr,
&VMHandle::w_shr,
&VMHandle::d_shr,
&VMHandle::shld,
&VMHandle::shrd,
&VMHandle::b_nand,
&VMHandle::w_nand,
&VMHandle::d_nand,
&VMHandle::set_pc,
&VMHandle::ret,
&VMHandle::in,
&VMHandle::rdtsc,
&VMHandle::cpuid,
&VMHandle::check_stack,
&VMHandle::push_stack_top_base,
&VMHandle::b_read_mem,
&VMHandle::w_read_mem,
&VMHandle::d_read_mem,
&VMHandle::b_write_mem,
&VMHandle::w_write_mem,
&VMHandle::d_write_mem,
&VMHandle::pop_stack_top_base,
&VMHandle::b_push_imm_sx,
&VMHandle::w_push_imm_sx,
&VMHandle::b_push_imm_zx,
&VMHandle::w_push_imm_zx,
&VMHandle::b_add,
&VMHandle::w_add,
&VMHandle::d_add,
&VMHandle::b_rol,
&VMHandle::w_rol,
&VMHandle::d_rol,
&VMHandle::b_ror,
&VMHandle::w_ror,
&VMHandle::d_ror,
&VMHandle::set_key,
&VMHandle::run_stack_code,
&VMHandle::fstsw
#ifdef _DEBUG
,&VMHandle::int3
#endif
#ifdef PROTECT_X64
,&VMHandle::q_read_stack,
&VMHandle::q_write_stack,
&VMHandle::q_push_reg,
&VMHandle::q_pop_reg,
&VMHandle::q_push_imm,
&VMHandle::q_shl,
&VMHandle::q_shr,
&VMHandle::q_nand,
&VMHandle::q_read_mem,
&VMHandle::q_write_mem,
&VMHandle::d_push_imm_sx,
&VMHandle::d_push_imm_zx,
&VMHandle::q_add,
&VMHandle::q_rol,
&VMHandle::q_ror
#endif
};
unsigned long handle_count = sizeof(handle_array) / sizeof (v_handle);
printf("共有0x%x个VM_handle\n", handle_count);
RandList<v_handle>(handle_array, handle_count);
handle_info info;
for (unsigned int i = 0; i < handle_count; i++)
{
v_handle r_fuc = handle_array[i];
info = (handle.*r_fuc)();
#ifdef _DEBUG
printf("0x%2x %s()\n", i, info.handle_name);
#endif
handle_info_list.push_back(info);
}
long size = handle.a.getCodeSize();
for (unsigned int i = 0; i < 0xFF; i++) // 占位置,供full_handle_table用
{
#ifdef PROTECT_X64
handle.a.dq(rand());
#else
handle.a.dd(rand());
#endif
}
dispatch_base = handle.a.getCodeSize() + base; // VM_table后的地址
info = handle.dispatch(base + size);
full_handle_table(base,size);
handle_info_list.push_back(info);
handle_pcode.dispatch.encode_key = &handle_info_list.back().encode;
handle_pcode.dispatch.encode_pcode = &handle_info_list.back().encode_pcode;
handle.a.relocCode(handle.a.getCode(), base);
vm_info.base = base;
vm_info.buf = handle.a.getCode();
vm_info.size = handle.a.getCodeSize();
printf("vm_info %p, size:0x%x\n", vm_info.buf, vm_info.size);
}
void VirtualMachine::add_pcode(AsmJit::Assembler &a,PCode *code,long base,long ret_address,long v_key,long decryption_key) //true表示正
{
using namespace AsmJit;
int register_count = code->register_store_in.register_count;
for (int i = 0; i < register_count; ++i)
{
switch (code->register_store_in.register_array[i])
{
case T_NAX:
a.push(nax);
break;
case T_NBX:
a.push(nbx);
break;
case T_NCX:
a.push(ncx);
break;
case T_NDX:
a.push(ndx);
break;
case T_NSP:
a.push(nsp);
break;
case T_NBP:
a.push(nbp);
break;
case T_NSI:
a.push(nsi);
break;
case T_NDI:
a.push(ndi);
break;
case T_EFLAG:
#ifndef PROTECT_X64
a.pushfd();
#else
a.pushfq();
#endif
break;
case T_KEY:
a.push(v_key);
break;
case T_RET:
a.push(ret_address);
break;
#ifdef PROTECT_X64
case T_R8:
a.push(r8);
break;
case T_R9:
a.push(r9);
break;
case T_R10:
a.push(r10);
break;
case T_R11:
a.push(r11);
break;
case T_R12:
a.push(r12);
break;
case T_R13:
a.push(r13);
break;
case T_R14:
a.push(r14);
break;
case T_R15:
a.push(r15);
break;
#endif
}
}
a.mov(nbx,decryption_key);
a.mov(nbp,nsp);
#ifndef PROTECT_X64
a.sub(nsp,REG_NUMBER * 4 + VMSTACK_MIN_COUNT * 4 );
#else
a.sub(nsp,REG_NUMBER * 8 + VMSTACK_MIN_COUNT * 8);
#endif
a.mov(ndi,nsp);
if (code->pcode_info.sign)
a.mov(nsi,base + 50);
else
a.mov(esi,base + 50 + code->pcode_info.offset);
a.jmp(dispatch_base);
}
// DeDf : 1
ppcode_block_info VirtualMachine::create_function_head(long reloc_base,
long pcode_base,
PCode *pcode,
long v_key,
long Handle_i_key)
{
printf("create_function_head():\n"
" reloc_base : %x\n"
" pcode_base : %x\n",
reloc_base, pcode_base);
using namespace AsmJit;
ppcode_block_info info = new pcode_block_info;
info->entry = pcode_base;
int register_count = pcode->register_store_in.register_count;
for (int i = 0; i < register_count; ++i)
{
switch (pcode->register_store_in.register_array[i])
{
case T_NAX:
info->a.push(nax);
break;
case T_NBX:
info->a.push(nbx);
break;
case T_NCX:
info->a.push(ncx);
break;
case T_NDX:
info->a.push(ndx);
break;
case T_NSP:
info->a.push(nsp);
break;
case T_NBP:
info->a.push(nbp);
break;
case T_NSI:
info->a.push(nsi);
break;
case T_NDI:
info->a.push(ndi);
break;
case T_EFLAG:
#ifndef PROTECT_X64
info->a.pushfd();
#else
info->a.pushfq();
#endif
break;
case T_KEY:
info->a.push(v_key);
break;
#ifdef PROTECT_X64
case T_R8:
info->a.push(r8);
break;
case T_R9:
info->a.push(r9);
break;
case T_R10:
info->a.push(r10);
break;
case T_R11:
info->a.push(r11);
break;
case T_R12:
info->a.push(r12);
break;
case T_R13:
info->a.push(r13);
break;
case T_R14:
info->a.push(r14);
break;
case T_R15:
info->a.push(r15);
break;
#endif
}
}
info->a.mov(nbx,Handle_i_key);
info->a.mov(nbp,nsp);
#ifndef PROTECT_X64
info->a.sub(nsp,REG_NUMBER * 4 + VMSTACK_MIN_COUNT * 4);
#else
info->a.sub(nsp,REG_NUMBER * 8 + VMSTACK_MIN_COUNT * 8);
#endif
info->a.mov(ndi,nsp);
info->a.mov(nsi,pcode_base);
info->a.jmp(dispatch_base);
info->a.relocCode(info->a.getCode(),reloc_base);
info->buf = info->a.getCode();
info->size = info->a.getCodeSize();
pcode_list.push_back(info);
return info;
}
//俩个key 一个是vm_context使用的 一个是ebx使用的解密Pcode key
ppcode_block_info VirtualMachine::add_new_function(long base,PCode *code,long ret_address,long v_key,long decryption_key)
{
using namespace AsmJit;
pcode_block_info *info = new pcode_block_info;
info->entry = base;
add_pcode(info->a,code,base,ret_address,v_key,decryption_key);
if (code->pcode_info.sign)
{
info->pcode_base = base + 50;
long init_handle_size = 50 - info->a.getCodeSize();
for (int i = 0; i < init_handle_size; i++)
{
info->a.db(0xff & rand());
}
for (unsigned int i = 0; i < code->pcode_info.offset; i++)
{
info->a.db(code->pcode_info.buf[i]);
}
}
else
{
info->pcode_base = base + 50 + code->pcode_info.offset;
long init_handle_size = 50 - info->a.getCodeSize();
for (int i = 0; i < init_handle_size; i++)
{
info->a.db(0xff & rand());
}
for (unsigned int i = code->pcode_info.size - code->pcode_info.offset;
i < code->pcode_info.size;
i++)
{
info->a.db(code->pcode_info.buf[i]);
}
}
info->a.relocCode(info->a.getCode(),base);
info->buf = info->a.getCode();
info->size = info->a.getCodeSize();
pcode_list.push_back(info);
return info;
}
void VirtualMachine::full_handle_table(long base,long table_offset)
{
unsigned char * asmbuf = handle.a.getCode();
unsigned long * buf = (unsigned long *)(asmbuf + table_offset);
unsigned long count = 1;
FILE *file;
fopen_s(&file, "VMHandleAddress.txt", "wb");
for (list <handle_info>::iterator iter = handle_info_list.begin(); iter != handle_info_list.end(); ++iter)
{
pVMHandleInfo instruction = NULL;
handle_info info = *iter;
buf[count] = info.offset + base;
fprintf(file, "%20s : %x\r\n", info.handle_name, buf[count]);
if ( info.label == &handle.l_b_read_stack )
{
instruction = &handle_pcode.b_read_stack;
} else if ( info.label == &handle.l_w_read_stack )
{
instruction = &handle_pcode.w_read_stack;
} else if ( info.label == &handle.l_d_read_stack )
{
instruction = &handle_pcode.d_read_stack;
} else if ( info.label == &handle.l_b_write_stack )
{
instruction = &handle_pcode.b_write_stack;
} else if ( info.label == &handle.l_w_write_stack )
{
instruction = &handle_pcode.w_write_stack;
} else if ( info.label == &handle.l_d_write_stack )
{
instruction = &handle_pcode.d_write_stack;
} else if ( info.label == &handle.l_b_push_reg )
{
instruction = &handle_pcode.b_push_reg;
} else if ( info.label == &handle.l_w_push_reg )
{
instruction = &handle_pcode.w_push_reg;
} else if ( info.label == &handle.l_d_push_reg )
{
instruction = &handle_pcode.d_push_reg;
} else if ( info.label == &handle.l_b_pop_reg )
{
instruction = &handle_pcode.b_pop_reg;
} else if ( info.label == &handle.l_w_pop_reg )
{
instruction = &handle_pcode.w_pop_reg;
} else if ( info.label == &handle.l_d_pop_reg )
{
instruction = &handle_pcode.d_pop_reg;
} else if ( info.label == &handle.l_b_push_imm )
{
instruction = &handle_pcode.b_push_imm;
} else if ( info.label == &handle.l_w_push_imm )
{
instruction = &handle_pcode.w_push_imm;
} else if ( info.label == &handle.l_d_push_imm )
{
instruction = &handle_pcode.d_push_imm;
} else if ( info.label == &handle.l_b_shl )
{
instruction = &handle_pcode.b_shl;
} else if ( info.label == &handle.l_w_shl )
{
instruction = &handle_pcode.w_shl;
} else if ( info.label == &handle.l_d_shl )
{
instruction = &handle_pcode.d_shl;
} else if ( info.label == &handle.l_b_shr )
{
instruction = &handle_pcode.b_shr;
} else if ( info.label == &handle.l_w_shr )
{
instruction = &handle_pcode.w_shr;
} else if ( info.label == &handle.l_d_shr )
{
instruction = &handle_pcode.d_shr;
} else if ( info.label == &handle.l_shld )
{
instruction = &handle_pcode.shld;
} else if ( info.label == &handle.l_shrd )
{
instruction = &handle_pcode.shrd;
} else if ( info.label == &handle.l_b_nand )
{
instruction = &handle_pcode.b_nand;
} else if ( info.label == &handle.l_w_nand )
{
instruction = &handle_pcode.w_nand;
} else if ( info.label == &handle.l_d_nand )
{
instruction = &handle_pcode.d_nand;
} else if ( info.label == &handle.l_set_pc )
{
instruction = &handle_pcode.set_pc;
} else if ( info.label == &handle.l_ret )
{
instruction = &handle_pcode.ret;
} else if ( info.label == &handle.l_in )
{
instruction = &handle_pcode.in;
} else if ( info.label == &handle.l_rdtsc )
{
instruction = &handle_pcode.rdtsc;
} else if ( info.label == &handle.l_cpuid )
{
instruction = &handle_pcode.cpuid;
} else if ( info.label == &handle.l_check_stack )
{
instruction = &handle_pcode.check_stack;
} else if ( info.label == &handle.l_dispatch )
{
instruction = &handle_pcode.dispatch;
} else if ( info.label == &handle.l_push_stack_top_base )
{
instruction = &handle_pcode.push_stack_top_base;
} else if ( info.label == &handle.l_b_read_mem)
{
instruction = &handle_pcode.b_read_mem;
} else if ( info.label == &handle.l_w_read_mem)
{
instruction = &handle_pcode.w_read_mem;
} else if ( info.label == &handle.l_d_read_mem)
{
instruction = &handle_pcode.d_read_mem;
} else if ( info.label == &handle.l_b_write_mem)
{
instruction = &handle_pcode.b_write_mem;
} else if ( info.label == &handle.l_w_write_mem)
{
instruction = &handle_pcode.w_write_mem;
} else if ( info.label == &handle.l_d_write_mem)
{
instruction = &handle_pcode.d_write_mem;
} else if ( info.label == &handle.l_b_push_imm_sx )
{
instruction = &handle_pcode.b_push_imm_sx;
} else if ( info.label == &handle.l_w_push_imm_sx )
{
instruction = &handle_pcode.w_push_imm_sx;
} else if ( info.label == &handle.l_b_push_imm_zx )
{
instruction = &handle_pcode.b_push_imm_zx;
} else if ( info.label == &handle.l_w_push_imm_zx )
{
instruction = &handle_pcode.w_push_imm_zx;
} else if ( info.label == &handle.l_pop_stack_top_base)
{
instruction = &handle_pcode.pop_stack_top_base;
} else if ( info.label == &handle.l_b_add)
{
instruction = &handle_pcode.b_add;
} else if ( info.label == &handle.l_w_add)
{
instruction = &handle_pcode.w_add;
} else if ( info.label == &handle.l_d_add)
{
instruction = &handle_pcode.d_add;
} else if ( info.label == &handle.l_b_rol)
{
instruction = &handle_pcode.b_rol;
} else if ( info.label == &handle.l_w_rol)
{
instruction = &handle_pcode.w_rol;
} else if ( info.label == &handle.l_d_rol)
{
instruction = &handle_pcode.d_rol;
} else if ( info.label == &handle.l_b_ror)
{
instruction = &handle_pcode.b_ror;
} else if ( info.label == &handle.l_w_ror)
{
instruction = &handle_pcode.w_ror;
} else if ( info.label == &handle.l_d_ror)
{
instruction = &handle_pcode.d_ror;
} else if ( info.label == &handle.l_set_key )
{
instruction = &handle_pcode.set_key;
} else if ( info.label == &handle.l_run_stack_code)
{
instruction = &handle_pcode.run_stack;
} else if ( info.label == &handle.l_fstsw)
{
instruction = &handle_pcode.fstsw;
}
#ifdef _DEBUG
else if ( info.label == &handle.l_int3)
{
instruction = &handle_pcode.int3;
}
#endif
#ifdef PROTECT_X64
else if ( info.label == &handle.l_q_ror)
{
instruction = &handle_pcode.q_ror;
}else if ( info.label == &handle.l_q_read_stack )
{
instruction = &handle_pcode.q_read_stack;
} else if ( info.label == &handle.l_q_write_stack )
{
instruction = &handle_pcode.q_write_stack;
}else if ( info.label == &handle.l_q_push_reg )
{
instruction = &handle_pcode.q_push_reg;
}else if ( info.label == &handle.l_q_pop_reg )
{
instruction = &handle_pcode.q_pop_reg;
}else if ( info.label == &handle.l_q_push_imm )
{
instruction = &handle_pcode.q_push_imm;
}else if ( info.label == &handle.l_q_shl )
{
instruction = &handle_pcode.q_shl;
}else if ( info.label == &handle.l_q_shr )
{
instruction = &handle_pcode.q_shr;
}else if ( info.label == &handle.l_q_nand )
{
instruction = &handle_pcode.q_nand;
}else if ( info.label == &handle.l_q_read_mem)
{
instruction = &handle_pcode.q_read_mem;
}else if ( info.label == &handle.l_q_write_mem)
{
instruction = &handle_pcode.q_write_mem;
}else if ( info.label == &handle.l_d_push_imm_sx )
{
instruction = &handle_pcode.d_push_imm_sx;
}else if ( info.label == &handle.l_d_push_imm_zx )
{
instruction = &handle_pcode.d_push_imm_zx;
}else if ( info.label == &handle.l_q_add)
{
instruction = &handle_pcode.q_add;
}else if ( info.label == &handle.l_q_rol)
{
instruction = &handle_pcode.q_rol;
}
#endif
if (instruction == NULL)
{
printf("!!ERROR %s:%d:%s\r\n", __FILE__, __LINE__, info.handle_name);
}
instruction->handle_i = (unsigned char)count++;
instruction->encode_key = &iter->encode;
instruction->type = info.type;
instruction->encode_pcode = &iter->encode_pcode;
}
fclose(file);
}