-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathPCode.cpp
1026 lines (945 loc) · 31.1 KB
/
PCode.cpp
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) Sun Jun 9 22:03:40 2013 XiaoWei
*
* handsomexiaowei@gmail.com
*/
#include "PCode.hpp"
#include <string.h>
#include "algorithms.hpp"
#define _STDINT_H
#include "Libudis86/udis86.h"
#pragma warning(disable:4244)
#pragma warning(disable:4996)
void PCode::out_log_file(char * file_name)
{
#ifdef _DEBUG
v_log = NULL;//fopen(file_name, "wb");
#endif
}
#ifdef _DEBUG
void PCode::out_info(char * _sz)
{
fprintf(v_log, "%s", _sz);
}
#endif
PCode::PCode()
: _key_(rand()),
key(_key_),
r_pc_size(0),
is_begin_vmcode(false),
current_instruction(NULL),
vmcode_size(0)
#ifdef _DEBUG
,v_log(stdout)
#endif
{
pcode_info.size = DEFAULT_ALLOC_SIZE;
pcode_info.buf = new unsigned char [pcode_info.size];
pcode_info.offset = 0;
for (int i = 0; i < REG_NUMBER; i++)
{
v_reg_context.vr[i].reg_id = T_INVALID;
v_reg_context.vr[i].reg_offset = i*4;
}
}
PCode::PCode(vm_handle table,bool sign)
: _key_(rand()),key(_key_),r_pc_size(0),is_begin_vmcode(false)
#ifdef _DEBUG
,v_log(stdout)
#endif
{
pcode_info.size = DEFAULT_ALLOC_SIZE;
pcode_info.buf = new unsigned char [pcode_info.size];
pcode_info.offset = 0;
init_handle_table(table);
init_sign(sign);
for (int i = 0; i < REG_NUMBER; i++)
{
v_reg_context.vr[i].reg_id = T_INVALID;
v_reg_context.vr[i].reg_offset = i*4;
}
}
PCode::~PCode()
{
if (pcode_info.buf)
{
delete [] pcode_info.buf;
pcode_info.buf = NULL;
}
#ifdef _DEBUG
if (v_log != stdout)
{
fclose(v_log);
}
#endif
}
#ifdef PROTECT_X64
const unsigned long
#else
const unsigned int
#endif
PCode::get_original_key()
{
return _key_;
}
void PCode::set_key( const
#ifdef PROTECT_X64
unsigned long _key
#else
unsigned int _key
#endif
)
{
if ( _key_ != key )
{
cout << "程序已经开始运行,无法改变Key\r\n" << endl;
throw ;
return;
}
_key_ = _key;
key = _key;
}
#ifdef PROTECT_X64
const unsigned long
#else
const unsigned int
#endif
PCode::get_current_key()
{
return key;
}
typedef unsigned char (*b_MyFn)(unsigned char *);
typedef unsigned short (*w_MyFn)(unsigned short *);
typedef unsigned int (*d_MyFn)(unsigned int *);
typedef unsigned long (*q_MyFn)(unsigned long *);
#ifdef PROTECT_X64
typedef q_MyFn KeyFn;
#else
typedef d_MyFn KeyFn;
#endif
size_t PCode::get_code_size()
{
return pcode_info.offset;
}
void PCode::call_encode_pcode_fn(vcode_encryption::MyFn fn,void *data,long *ikey)
{
using namespace AsmJit;
AsmJit::Assembler a;
if (*ikey)
a.mov(nax,(long)ikey);
else
a.mov(nax,(long)&key);
a.push(nax);
a.mov(nax,(long)data);
a.push(nax);
#ifdef PROTECT_X64
a.call((void*)fn);
a.add(nsp,16);
a.ret();
#else
a.call((void*)fn);
a.add(nsp,8);
a.ret();
#endif
typedef void (*f)();
f mycall = function_cast<f>(a.make());
mycall();
}
void PCode::count_vmcode_begin()
{
vmcode_size = 0;
is_begin_vmcode = true;
}
size_t PCode::count_vmcode_end()
{
is_begin_vmcode = false;
return vmcode_size;
}
void PCode::db(unsigned char b)
{
if (is_begin_vmcode)
{
vmcode_size++;
return;
}
if (!r_pc_size)
{
pVMHandleInfo instruction = (pVMHandleInfo)&pcode_info.handle_table; //这里拦截VM指令
int count = sizeof(vm_handle)/sizeof(VMHandleInfo);
while (count != 0)
{
if (instruction->handle_i == b) //获取到handle了
{
v_log = stdout;
char *f = "VM_Handle: ";
char *l = "";
if (pcode_info.handle_table.b_read_stack.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_read_stack",l);
else if (pcode_info.handle_table.w_read_stack.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_read_stack",l);
else if (pcode_info.handle_table.d_read_stack.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_read_stack",l);
else if (pcode_info.handle_table.b_write_stack.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_write_stack",l);
else if (pcode_info.handle_table.w_write_stack.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_write_stack",l);
else if (pcode_info.handle_table.d_write_stack.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_write_stack",l);
#ifdef PROTECT_X64
else if (pcode_info.handle_table.q_nand.handle_i == b)
fprintf(v_log,"%s%s%s",f,"q_nand",l);
else if (pcode_info.handle_table.q_push_imm.handle_i == b)
fprintf(v_log,"%s%s%s",f,"q_push_imm",l);
else if (pcode_info.handle_table.q_shl.handle_i == b)
fprintf(v_log,"%s%s%s",f,"q_shl",l);
else if (pcode_info.handle_table.q_push_imm.handle_i == b)
fprintf(v_log,"%s%s%s",f,"q_push_imm",l);
else if (pcode_info.handle_table.q_pop_reg.handle_i == b)
fprintf(v_log,"%s%s%s",f,"q_pop_reg",l);
else if (pcode_info.handle_table.q_push_reg.handle_i == b)
fprintf(v_log,"%s%s%s",f,"q_push_reg",l);
else if (pcode_info.handle_table.q_read_stack.handle_i == b)
fprintf(v_log,"%s%s%s",f,"q_read_stack",l);
else if (pcode_info.handle_table.q_write_stack.handle_i == b)
fprintf(v_log,"%s%s%s",f,"q_write_stack",l);
else if (pcode_info.handle_table.q_shl.handle_i == b)
fprintf(v_log,"%s%s%s",f,"q_shl",l);
else if (pcode_info.handle_table.q_shr.handle_i == b)
fprintf(v_log,"%s%s%s",f,"q_shr",l);
else if (pcode_info.handle_table.q_ror.handle_i == b)
fprintf(v_log,"%s%s%s",f,"q_ror",l);
else if (pcode_info.handle_table.q_add.handle_i == b)
fprintf(v_log,"%s%s%s",f,"q_add",l);
else if (pcode_info.handle_table.q_rol.handle_i == b)
fprintf(v_log,"%s%s%s",f,"q_rol",l);
else if (pcode_info.handle_table.q_read_mem.handle_i == b)
fprintf(v_log,"%s%s%s",f,"q_read_mem",l);
else if (pcode_info.handle_table.q_write_mem.handle_i == b)
fprintf(v_log,"%s%s%s",f,"q_write_mem",l);
else if (pcode_info.handle_table.d_push_imm_sx.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_push_imm_sx",l);
else if (pcode_info.handle_table.d_push_imm_zx.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_push_imm_zx",l);
#endif
else if (pcode_info.handle_table.b_push_reg.handle_i == b)
{
fprintf(v_log,"%s%s%s",f,"b_push_reg",l);
}
else if (pcode_info.handle_table.w_push_reg.handle_i == b)
{
fprintf(v_log,"%s%s%s",f,"w_push_reg",l);
}
else if (pcode_info.handle_table.d_push_reg.handle_i == b)
{
fprintf(v_log,"%s%s%s",f,"d_push_reg",l);
}
else if (pcode_info.handle_table.b_pop_reg.handle_i == b)
{
fprintf(v_log,"%s%s%s",f,"b_pop_reg",l);
}
else if (pcode_info.handle_table.w_pop_reg.handle_i == b)
{
fprintf(v_log,"%s%s%s",f,"w_pop_reg",l);
}
else if (pcode_info.handle_table.d_pop_reg.handle_i == b)
{
fprintf(v_log,"%s%s%s",f,"d_pop_reg",l);
}
else if (pcode_info.handle_table.b_push_imm.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_push_imm",l);
else if (pcode_info.handle_table.w_push_imm.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_push_imm",l);
else if (pcode_info.handle_table.d_push_imm.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_push_imm",l);
else if (pcode_info.handle_table.b_shl.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_shl",l);
else if (pcode_info.handle_table.w_shl.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_shl",l);
else if (pcode_info.handle_table.d_shl.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_shl",l);
else if (pcode_info.handle_table.b_shr.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_shr",l);
else if (pcode_info.handle_table.w_shr.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_shr",l);
else if (pcode_info.handle_table.d_shr.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_shr",l);
else if (pcode_info.handle_table.b_push_imm.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_push_imm",l);
else if (pcode_info.handle_table.w_push_imm.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_push_imm",l);
else if (pcode_info.handle_table.d_push_imm.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_push_imm",l);
else if (pcode_info.handle_table.b_shl.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_shl",l);
else if (pcode_info.handle_table.w_shl.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_shl",l);
else if (pcode_info.handle_table.d_shl.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_shl",l);
else if (pcode_info.handle_table.b_shr.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_shr",l);
else if (pcode_info.handle_table.w_shr.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_shr",l);
else if (pcode_info.handle_table.d_shr.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_shr",l);
else if (pcode_info.handle_table.shld.handle_i == b)
fprintf(v_log,"%s%s%s",f,"shld",l);
else if (pcode_info.handle_table.shrd.handle_i == b)
fprintf(v_log,"%s%s%s",f,"shrd",l);
else if (pcode_info.handle_table.b_nand.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_nand",l);
else if (pcode_info.handle_table.w_nand.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_nand",l);
else if (pcode_info.handle_table.d_nand.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_nand",l);
else if (pcode_info.handle_table.set_pc.handle_i == b)
fprintf(v_log,"%s%s%s",f,"set_pc",l);
else if (pcode_info.handle_table.ret.handle_i == b)
fprintf(v_log,"%s%s%s",f,"ret",l);
else if (pcode_info.handle_table.in.handle_i == b)
fprintf(v_log,"%s%s%s",f,"in",l);
else if (pcode_info.handle_table.rdtsc.handle_i == b)
fprintf(v_log,"%s%s%s",f,"rdtsc",l);
else if (pcode_info.handle_table.cpuid.handle_i == b)
fprintf(v_log,"%s%s%s",f,"cpuid",l);
else if (pcode_info.handle_table.check_stack.handle_i == b)
fprintf(v_log,"%s%s%s",f,"check_stack",l);
else if (pcode_info.handle_table.dispatch.handle_i == b)
fprintf(v_log,"%s%s%s",f,"dispatch",l);
else if (pcode_info.handle_table.push_stack_top_base.handle_i == b)
fprintf(v_log,"%s%s%s",f,"push_vsp",l);
else if (pcode_info.handle_table.b_read_mem.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_read_mem",l);
else if (pcode_info.handle_table.w_read_mem.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_read_mem",l);
else if (pcode_info.handle_table.d_read_mem.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_read_mem",l);
else if (pcode_info.handle_table.b_write_mem.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_write_mem",l);
else if (pcode_info.handle_table.w_write_mem.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_write_mem",l);
else if (pcode_info.handle_table.d_write_mem.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_write_mem",l);
else if (pcode_info.handle_table.pop_stack_top_base.handle_i == b)
fprintf(v_log,"%s%s%s",f,"pop_stack_top_base",l);
else if (pcode_info.handle_table.b_push_imm_sx.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_push_imm_sx",l);
else if (pcode_info.handle_table.w_push_imm_sx.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_push_imm_sx",l);
else if (pcode_info.handle_table.b_push_imm_zx.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_push_imm_zx",l);
else if (pcode_info.handle_table.w_push_imm_zx.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_push_imm_zx",l);
else if (pcode_info.handle_table.b_add.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_add",l);
else if (pcode_info.handle_table.w_add.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_add",l);
else if (pcode_info.handle_table.d_add.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_add",l);
else if (pcode_info.handle_table.b_rol.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_rol",l);
else if (pcode_info.handle_table.w_rol.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_rol",l);
else if (pcode_info.handle_table.d_rol.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_rol",l);
else if (pcode_info.handle_table.b_ror.handle_i == b)
fprintf(v_log,"%s%s%s",f,"b_ror",l);
else if (pcode_info.handle_table.w_ror.handle_i == b)
fprintf(v_log,"%s%s%s",f,"w_ror",l);
else if (pcode_info.handle_table.d_ror.handle_i == b)
fprintf(v_log,"%s%s%s",f,"d_ror",l);
else if (pcode_info.handle_table.set_key.handle_i == b )
fprintf( v_log,"%s%s%s",f,"set_key",l );
else if (pcode_info.handle_table.run_stack.handle_i == b)
fprintf( v_log,"%s%s%s",f,"run_stack",l);
else if (pcode_info.handle_table.int3.handle_i == b)
fprintf( v_log,"%s%s%s",f,"int3",l);
else if (pcode_info.handle_table.fstsw.handle_i == b)
fprintf( v_log,"%s%s%s",f,"fstsw",l);
r_pc_size = instruction->type;
fprintf(v_log,"\n");
if (r_pc_size == 0)
{
fprintf(v_log,"()\n");
}
pVMHandleInfo dispatch_en_key = &pcode_info.handle_table.dispatch;
for (vector<encryption>::iterator iter = dispatch_en_key->encode_key->begin();
iter != dispatch_en_key->encode_key->end();
++iter)
{
iter->en_fn(&key); // 对Key进行变换
// ud_t ud_obj;
// ud_init(&ud_obj);
// ud_set_mode(&ud_obj, 32);
// ud_set_input_buffer(&ud_obj, (uint8_t*)iter->en_fn, 26);
// ud_set_syntax(&ud_obj, UD_SYN_INTEL);
// printf("\n");
// while (ud_disassemble(&ud_obj)) {
// printf("%s\n",ud_insn_asm(&ud_obj));
// }
}
for (list<vcode_encryption>::reverse_iterator iter =
dispatch_en_key->encode_pcode->rbegin();
iter != dispatch_en_key->encode_pcode->rend();
++iter)
{
call_encode_pcode_fn(iter->fn,&b,&iter->key);
}
////先动态加密KEY////
for (vector<encryption>::iterator iter2 = instruction->encode_key->begin();
iter2 != instruction->encode_key->end();
++iter2) //可能出现q_read_mem错误
{
iter2->en_fn(&key);
}
current_instruction = instruction;
break;
}
instruction++; //这个不能放在前面否则会导致读取到q_read_mem
count--;
}
}
else
{
#ifdef _DEBUG
if (strcmp(reg_name,"") == 0)
{
fprintf(v_log,"(0%x)\n",b);
}
else
{
if (b != 0)
{
if (b % sizeof(long) == 0)
{
fprintf(v_log,"(%s) index:(%x)\n",reg_name,b);
}
else
{
fprintf(v_log,"(%s) index:(%x) offset:(%d)\n",reg_name,b / sizeof(long),b % sizeof(long));
}
}
else
fprintf(v_log,"(%s) index:(%x)\n",reg_name,b);
if (b >= REG_NUMBER * 4)
{
printf("虚拟寄存器引索超过边界\n");
__asm int 3;
}
strcpy(reg_name,"");
}
#endif
for (list<vcode_encryption>::reverse_iterator iter = current_instruction->encode_pcode->rbegin();
iter != current_instruction->encode_pcode->rend();
++iter) //可能出现q_read_mem错误
{
call_encode_pcode_fn(iter->fn,&b,&iter->key); //兼容64程序
}
current_instruction = NULL;
r_pc_size = 0; //不是虚拟pcode而是读取数据
}
check_pcode_buf();
if (pcode_info.sign == false)
pcode_info.buf[pcode_info.size - 1 - pcode_info.offset] = b;
else
pcode_info.buf[pcode_info.offset] = b;
pcode_info.offset++;
}
void PCode::dw(unsigned short w)
{
if (is_begin_vmcode)
{
vmcode_size+=2;
return;
}
if (r_pc_size)
{
#ifdef _DEBUG
fprintf(v_log,"(0%x)\r\n",w);
#endif
for (list<vcode_encryption>::reverse_iterator iter = current_instruction->encode_pcode->rbegin();
iter != current_instruction->encode_pcode->rend();
++iter) //可能出现q_read_mem错误
{
call_encode_pcode_fn(iter->fn,&w,&iter->key);
}
current_instruction = NULL;
r_pc_size = 0;
}
check_pcode_buf();
if (pcode_info.sign == false)
{
unsigned short * buf = (unsigned short *)&pcode_info.buf[pcode_info.size - 2 - pcode_info.offset];
*buf = w;
}
else
{
unsigned short * buf = (unsigned short *)&pcode_info.buf[pcode_info.offset];
*buf = w;
}
pcode_info.offset += 2;
}
void PCode::dd(unsigned int d)
{
if (is_begin_vmcode)
{
vmcode_size += 4;
return;
}
if (r_pc_size)
{
for (list<vcode_encryption>::reverse_iterator iter = current_instruction->encode_pcode->rbegin();
iter != current_instruction->encode_pcode->rend();
++iter) //可能出现q_read_mem错误
{
call_encode_pcode_fn(iter->fn,&d,&iter->key);
}
current_instruction = NULL;
r_pc_size = 0;
}
check_pcode_buf();
unsigned int * buf;
if (pcode_info.sign == false)
buf = (unsigned int *)&pcode_info.buf[pcode_info.size - 4 - pcode_info.offset];
else
buf = (unsigned int *)&pcode_info.buf[pcode_info.size - 4 - pcode_info.offset];
*buf = d;
pcode_info.offset += 4;
}
void PCode::dq(unsigned long long q)
{
if (is_begin_vmcode)
{
vmcode_size += 8;
return;
}
if (r_pc_size)
{
#ifdef _DEBUG
fprintf(v_log,"(0%x)\r\n",q);
#endif
for (list<vcode_encryption>::reverse_iterator iter = current_instruction->encode_pcode->rbegin();
iter != current_instruction->encode_pcode->rend();
++iter) //可能出现q_read_mem错误
{
call_encode_pcode_fn(iter->fn,&q,&iter->key);
}
current_instruction = NULL;
r_pc_size = 0;
}
check_pcode_buf();
if (pcode_info.sign == false)
{
unsigned long long * buf = (unsigned long long *)pcode_info.buf[pcode_info.size - 8 - pcode_info.offset];
*buf = q;
}
else
{
unsigned long long * buf = (unsigned long long *)pcode_info.buf[pcode_info.offset];
*buf = q;
}
pcode_info.offset += 8;
}
void PCode::da(long a) // address 说明是一个地址
{
if (is_begin_vmcode)
{
vmcode_size += sizeof(long);
return;
}
if (r_pc_size)
{
#ifdef _DEBUG
fprintf(v_log,"(0%x)\r\n",a);
#endif
for (list<vcode_encryption>::reverse_iterator iter = current_instruction->encode_pcode->rbegin();
iter != current_instruction->encode_pcode->rend(); ++iter) //可能出现q_read_mem错误
{
call_encode_pcode_fn(iter->fn,&a,&iter->key);
}
current_instruction = NULL;
r_pc_size = 0;
}
check_pcode_buf();
if (pcode_info.sign == false)
{
unsigned long long * buf = (unsigned long long *)pcode_info.buf[pcode_info.size - sizeof(long) - pcode_info.offset];
*buf = a;
}
else
{
unsigned long long * buf = (unsigned long long *)pcode_info.buf[pcode_info.offset];
*buf = a;
}
pcode_info.offset += sizeof(long);
}
void PCode::init_handle_table(vm_handle table)
{
memcpy(&pcode_info.handle_table,&table,sizeof(table));
}
void PCode::init_sign(bool b)
{
pcode_info.sign = b;
}
void PCode::check_pcode_buf()
{
if (pcode_info.offset + 8 > pcode_info.size)
{
if (pcode_info.sign == false)
{
unsigned char *newbuf = new unsigned char [pcode_info.size + DEFAULT_ALLOC_SIZE];
memset(newbuf,0,pcode_info.size + DEFAULT_ALLOC_SIZE);
memcpy(&newbuf[DEFAULT_ALLOC_SIZE],pcode_info.buf,pcode_info.size);
pcode_info.size += DEFAULT_ALLOC_SIZE;
delete [] pcode_info.buf;
pcode_info.buf = newbuf;
}
else
{
unsigned char *newbuf = new unsigned char [pcode_info.size + DEFAULT_ALLOC_SIZE];
memset(newbuf,0,pcode_info.size + DEFAULT_ALLOC_SIZE);
memcpy(newbuf,pcode_info.buf,pcode_info.size);
pcode_info.size += DEFAULT_ALLOC_SIZE;
delete [] pcode_info.buf;
pcode_info.buf = newbuf;
}
}
}
#ifdef _DEBUG
void PCode::set_register_name(long _register)
{
switch (_register & T_ALL_REGISTER)
{
case T_RAX:
strcpy(reg_name,"rax");
break;
case T_RBX:
strcpy(reg_name,"rbx");
break;
case T_RCX:
strcpy(reg_name,"rcx");
break;
case T_RDX:
strcpy(reg_name,"rdx");
break;
case T_RSP:
strcpy(reg_name,"rsp");
break;
case T_RBP:
strcpy(reg_name,"rbp");
break;
case T_RSI:
strcpy(reg_name,"rsi");
break;
case T_RDI:
strcpy(reg_name,"rdi");
break;
case T_KEY64:
strcpy(reg_name,"key");
break;
case T_EFLAG_:
strcpy(reg_name,"flag");
break;
case T_TMP_R64_1:
strcpy(reg_name,"tr1");
break;
case T_TMP_R64_2:
strcpy(reg_name,"tr2");
break;
case T_TMP_R64_3:
strcpy(reg_name,"tr3");
break;
case T_TMP_R64_4:
strcpy(reg_name,"tr4");
break;
case T_TMP_R64_5:
strcpy(reg_name,"tr5");
break;
case T_TMP_R64_6:
strcpy(reg_name,"tr6");
break;
case T_TMP_R64_7:
strcpy(reg_name,"tr7");
break;
case T_TMP_R64_8:
strcpy(reg_name,"tr8");
break;
case T_JMPINSADDR64:
strcpy(reg_name,"jcc");
break;
case T_NEXTINSADDR64:
strcpy(reg_name,"pc");
break;
default:
strcpy(reg_name,"invalid" );
break;
}
if (_register & T_E32X)
{
if (reg_name[0] == 'r')
reg_name[0] = 'e';
if (_register & T_16X)
{
if (_register & T_8H)
{
strcat(reg_name,"|8h");
}
else if (_register & T_8L)
{
strcat(reg_name,"|8l");
} else
strcat(reg_name,"|16l");
}
else
{
strcat(reg_name,"|32l");
}
}
}
#endif
void PCode::v_push_register(long _register,bool _disable)
{
#ifdef _DEBUG
set_register_name(_register);
#endif
if (_register & T_INVALID)
{
//没找到寄存器
pvm_reg invalid_r = find_invalid_register();
//#ifdef PROTECT_X64
if (_register & T_8H)
{
db(pcode_info.handle_table.b_push_reg.handle_i);
db(invalid_r->reg_offset + 1);
return;
} else if (_register & T_8L)
{
db(pcode_info.handle_table.b_push_reg.handle_i);
} else if (_register & T_16X)
{
db(pcode_info.handle_table.w_push_reg.handle_i);
} else if (_register & T_E32X)
{
db(pcode_info.handle_table.d_push_reg.handle_i);
} else
{
#ifdef PROTECT_X64
db(pcode_info.handle_table.q_push_reg.handle_i);
#else
db(pcode_info.handle_table.d_push_reg.handle_i);
#endif
}
db(invalid_r->reg_offset);
return;
}
size_t size = get_code_size();
for (int i = 0; i < REG_NUMBER; i++)
{
long regid = T_ALL_REGISTER & _register;
long vregid = v_reg_context.vr[i].reg_id;
if ( regid && ((vregid & T_ALL_REGISTER) == regid) )
{
long reg_offset = v_reg_context.vr[i].reg_offset;
long reg_status = v_reg_context.vr[i].status;
//表示有寄存器类型
if (_register & T_E32X )
{
//表示是32位寄存器
if (_register & T_16X)
{
//表示是16位寄存器
if (_register & T_8H)
{
//8位寄存器高位
db(pcode_info.handle_table.b_push_reg.handle_i);
db(reg_offset + 1);
return ;
}
else if (_register & T_8L)
{
//8位寄存器低位
db(pcode_info.handle_table.b_push_reg.handle_i);
db(reg_offset);
return;
}
else
{
//16位寄存器
db(pcode_info.handle_table.w_push_reg.handle_i);
db(reg_offset);
return;
}
}
else
{
//32位寄存器
db(pcode_info.handle_table.d_push_reg.handle_i);
db(reg_offset);
#ifndef PROTECT_X64
if (_disable)
v_reg_context.vr[i].reg_id = T_INVALID;
return;
#endif
}
}
else
{
#ifndef PROTECT_X64
#ifdef _DEBUG
printf("保护32位程序却要push 64位寄存器\n");
__asm int 3;
#endif
#endif
#ifdef PROTECT_X64
//64位寄存器
if (_register & T_RN)
{
//R8-R15
db(pcode_info.handle_table.q_push_reg.handle_i);
db(reg_offset);
} else
{
//RAX-RDI
db(pcode_info.handle_table.q_push_reg.handle_i);
db(reg_offset);
}
if (_disable)
v_reg_context.vr[i].reg_id = T_INVALID; //设置为无效
return;
#endif
}
v_reg_context.vr[i].status = _register & disable;
}
}
if (size == get_code_size( ))
{
#ifdef _DEBUG
set_register_name(_register);
printf("没有找到寄存器%s\n", reg_name);
__asm int 3;
#endif
}
}
pvm_reg PCode::find_invalid_register()
{
int invalid_register_array[REG_NUMBER];
int invalid_register_count = 0;
for (int i = 0; i < REG_NUMBER; i++)
{
if (v_reg_context.vr[i].reg_id & T_INVALID)
{
invalid_register_array[invalid_register_count++] = i;
}
}
if (invalid_register_count)
{
int r = invalid_register_array[rand()%invalid_register_count];
return &v_reg_context.vr[r];
}
return NULL;
}
void PCode::v_pop_register(long _register)
{
#ifdef _DEBUG
set_register_name(_register);
printf("register : %s\n", reg_name);
#endif
for (int i = 0; i < REG_NUMBER; i++)
{
long regid = T_ALL_REGISTER & _register;
long vregid = v_reg_context.vr[i].reg_id;
if ( regid && ((vregid & T_ALL_REGISTER) == regid) ) //表示有寄存器类型
{
long reg_offset = v_reg_context.vr[i].reg_offset;
if (_register & T_E32X) //表示是32位寄存器
{
if (_register & T_16X) //表示是16位寄存器
{
if (_register & T_8H) //8位寄存器高位
{
db(pcode_info.handle_table.b_pop_reg.handle_i);
db(reg_offset + 1);
v_reg_context.vr[i].status = _register & enable;
return;
}
else if (_register & T_8L) //8位寄存器低位
{
db(pcode_info.handle_table.b_pop_reg.handle_i);
db(reg_offset);
v_reg_context.vr[i].status = _register & enable;
return;
}
else //16位寄存器
{
db(pcode_info.handle_table.w_pop_reg.handle_i);
db(reg_offset);
v_reg_context.vr[i].status = _register & enable;
return;
}
}
else //32位寄存器
{
db(pcode_info.handle_table.d_pop_reg.handle_i);
db(reg_offset);
v_reg_context.vr[i].status = _register & enable;
return;
}
}
else
{
#ifndef PROTECT_X64
#ifdef _DEBUG
printf("保护32位程序却要pop 64位寄存器\n");
__asm int 3;
#endif
#else
//64位寄存器
if (_register & T_RN)
{
//R8-R15
db(pcode_info.handle_table.q_pop_reg.handle_i);
db(reg_offset);
v_reg_context.vr[i].status = _register & enable;
return;
}
else
{
//RAX-RDI
db(pcode_info.handle_table.q_pop_reg.handle_i);
db(reg_offset);
v_reg_context.vr[i].status = _register & enable;
return;
}
#endif
}
}
}
//没找到寄存器
pvm_reg invalid_r = find_invalid_register();
if (_register & T_8H)
{
db(pcode_info.handle_table.b_pop_reg.handle_i);