-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandAsm.h
938 lines (899 loc) · 16.8 KB
/
randAsm.h
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
#pragma once
#include <vector>
#include <cassert>
// ***** 定义各种花指令 ******
class OneByteAsm {
public:
static unsigned char get() {
std::vector<unsigned char> result;
for (int i = 0; i < 8; i++) {
// push reg
result.push_back(0x50 + i);
// pop reg
result.push_back(0x58 + i);
// inc reg
result.push_back(0x40 + i);
// dec reg
result.push_back(0x48 + i);
}
// int 3
result.push_back(0xCC);
// ret
result.push_back(0xC3);
// leave
result.push_back(0xC9);
// nop
result.push_back(0x90);
// pushad
result.push_back(0x60);
// popad
result.push_back(0x61);
// pushfd
result.push_back(0x9C);
// popfd
result.push_back(0x9D);
// outsb
result.push_back(0x6E);
// outsd
result.push_back(0x6F);
// insb
result.push_back(0x6C);
// insd
result.push_back(0x6D);
// 随机返回一个
return result[rand() % result.size()];
}
};
// 替换push reg花指令
class PushReg {
public:
/*
50 PUSH EAX
51 PUSH ECX
52 PUSH EDX
53 PUSH EBX
54 PUSH ESP
55 PUSH EBP
56 PUSH ESI
57 PUSH EDI
*/
static std::vector<unsigned char> get(unsigned char reg) {
std::vector<unsigned char> result;
// push esp 是特例
if (reg == 0x54) {
if (rand() % 3) {
result.push_back(reg);
}
else {
// push rand_reg
// push reg // not esp
// lea reg, [esp+8]
// mov [esp+4], reg
// pop reg
// push rand_reg
result.push_back(0x50 + rand() % 8);
// push reg
// 随机一个reg,避开esp
int reg = rand() % 8;
if (reg == 4) {
if (rand() % 2) {
reg += rand() % 2 + 1;
}
else {
reg -= rand() % 2 + 1;
}
}
// push reg
result.push_back(0x50 + reg);
// lea reg, [esp+8]
result.push_back(0x8D);
switch (reg)
{
case 0:
result.push_back(0x44);
break;
case 1:
result.push_back(0x4C);
break;
case 2:
result.push_back(0x54);
break;
case 3:
result.push_back(0x5C);
break;
case 4:
assert(false);
break;
case 5:
result.push_back(0x6C);
break;
case 6:
result.push_back(0x74);
break;
case 7:
result.push_back(0x7C);
break;
default:
assert(false);
break;
}
result.push_back(0x24);
result.push_back(0x08);
// mov [esp+4], reg
result.push_back(0x89);
switch (reg)
{
case 0:
result.push_back(0x44);
break;
case 1:
result.push_back(0x4C);
break;
case 2:
result.push_back(0x54);
break;
case 3:
result.push_back(0x5C);
break;
case 4:
assert(false);
break;
case 5:
result.push_back(0x6C);
break;
case 6:
result.push_back(0x74);
break;
case 7:
result.push_back(0x7C);
break;
default:
assert(false);
break;
}
result.push_back(0x24);
result.push_back(0x04);
// pop reg
result.push_back(0x58 + reg);
}
return result;
}
// push reg_rand
result.push_back(0x50 + rand() % 8);
// mov [esp], reg
result.push_back(0x89);
switch (reg) {
case 0x50:
result.push_back(0x04);
break;
case 0x51:
result.push_back(0x0C);
break;
case 0x52:
result.push_back(0x14);
break;
case 0x53:
result.push_back(0x1C);
break;
case 0x54:
assert(false);
break;
case 0x55:
result.push_back(0x2C);
break;
case 0x56:
result.push_back(0x34);
break;
case 0x57:
result.push_back(0x3C);
break;
default:
assert(false);
break;
}
result.push_back(0x24);
return result;
}
};
// 替换push idata花指令
class PushData {
public:
/*
6A PUSH BYTE
68 PUSH DWORD
*/
static std::vector<unsigned char> get(unsigned char* buf) {
std::vector<unsigned char> result;
bool is_byte = (buf[0] == 0x6A);
DWORD idata = 0;
if (is_byte) {
idata = buf[1];
}
else {
idata = *(DWORD*)(buf + 1);
}
unsigned char tmp[4] = { 0 };
tmp[0] = (unsigned char)rand();
tmp[1] = (unsigned char)rand();
tmp[2] = (unsigned char)rand();
tmp[3] = (unsigned char)rand();
// push reg_rand
// push reg1
// mov reg1, rand_data
// lea reg1, [reg1 + idata - rand_data]
// MOV [ESP+0x04], reg1
// pop reg1
// push reg
result.push_back(0x50 + rand() % 8);
// 随机一个reg,避开esp
int reg1 = rand() % 8;
if (reg1 == 4) {
if (rand() % 2) {
reg1++;
}
else {
reg1--;
}
}
// push reg1
result.push_back(0x50 + reg1);
// mov reg1, rand_data
result.push_back(0xB8 + reg1);
result.push_back(tmp[0]);
result.push_back(tmp[1]);
result.push_back(tmp[2]);
result.push_back(tmp[3]);
// lea reg1, [reg1 + xxx]
// 先计算 xxx
DWORD xxx = idata - *(DWORD*)(tmp);
result.push_back(0x8D);
switch (reg1)
{
case 0:
result.push_back(0x80);
break;
case 1:
result.push_back(0x89);
break;
case 2:
result.push_back(0x92);
break;
case 3:
result.push_back(0x9B);
break;
case 4:
assert(false);
break;
case 5:
result.push_back(0xAD);
break;
case 6:
result.push_back(0xB6);
break;
case 7:
result.push_back(0xBF);
break;
default:
assert(false);
break;
}
unsigned char* pxxx = (unsigned char*)&xxx;
result.push_back(*pxxx);
result.push_back(*(pxxx + 1));
result.push_back(*(pxxx + 2));
result.push_back(*(pxxx + 3));
// MOV [ESP+0x04], reg1
result.push_back(0x89);
switch (reg1)
{
case 0:
result.push_back(0x44);
break;
case 1:
result.push_back(0x4C);
break;
case 2:
result.push_back(0x54);
break;
case 3:
result.push_back(0x5C);
break;
case 4:
assert(false);
break;
case 5:
result.push_back(0x6C);
break;
case 6:
result.push_back(0x74);
break;
case 7:
result.push_back(0x7C);
break;
default:
assert(false);
break;
}
result.push_back(0x24);
result.push_back(0x04);
// pop reg1
result.push_back(0x58 + reg1);
return result;
}
};
// 替换pop reg花指令
/*
push randomreg1
mov reg, [esp+4]
mov [esp+4], randomreg2 ; randomreg2 不能是esp
pop randomreg2
pop randomreg2
*/
class PopReg {
public:
/*
58 POP EAX
59 POP ECX
5A POP EDX
5B POP EBX
5C POP ESP
5D POP EBP
5E POP ESI
5F POP EDI
*/
static std::vector<unsigned char> get(unsigned char reg) {
std::vector<unsigned char> result;
// pop esp是特例, 其实正常代码不会出现这个指令
if (reg == 0x5C) {
if (rand() % 2) {
result.push_back(reg);
return result;
}
else {
//mov esp, dword ptr ss : [esp]
result.push_back(0x8B);
result.push_back(0x24);
result.push_back(0x24);
return result;
}
}
// 四分之一的概率走这个
if (rand() % 4 == 0) {
// push random
// pop random
// pop reg
int tmp = rand() % 8;
result.push_back(0x50 + tmp);
result.push_back(0x58 + tmp);
result.push_back(reg);
return result;
}
/*
push rand_reg
mov reg, [esp+4]
mov [esp+4], rand_reg2
pop rand_reg2
pop rand_reg2
*/
// push randomreg1
result.push_back(0x50 + rand() % 8);
// mov reg, [esp+4]
result.push_back(0x8B);
switch (reg) {
case 0x58:
result.push_back(0x44);
break;
case 0x59:
result.push_back(0x4C);
break;
case 0x5A:
result.push_back(0x54);
break;
case 0x5B:
result.push_back(0x5C);
break;
case 0x5C:
assert(false);
break;
case 0x5D:
result.push_back(0x6C);
break;
case 0x5E:
result.push_back(0x74);
break;
case 0x5F:
result.push_back(0x7C);
break;
default:
assert(false);
break;
}
result.push_back(0x24);
result.push_back(0x04);
// 不能是esp
int reg2 = 0x58 + rand() % 8;
if (reg2 == 0x5C) {
if (rand() % 2) {
reg2++;
}
else {
reg2--;
}
}
// mov [esp+4], randomreg2
result.push_back(0x89);
switch (reg2) {
case 0x58:
result.push_back(0x44);
break;
case 0x59:
result.push_back(0x4C);
break;
case 0x5A:
result.push_back(0x54);
break;
case 0x5B:
result.push_back(0x5C);
break;
case 0x5C:
assert(false);
break;
case 0x5D:
result.push_back(0x6C);
break;
case 0x5E:
result.push_back(0x74);
break;
case 0x5F:
result.push_back(0x7C);
break;
default:
assert(false);
break;
}
result.push_back(0x24);
result.push_back(0x04);
//pop randomreg2
result.push_back(reg2);
//pop randomreg2
result.push_back(reg2);
return result;
}
};
// 纯垃圾指令,可以插入任意位置
class Rubbish {
public:
static std::vector<unsigned char> get() {
std::vector<unsigned char> result;
// 为了支持垃圾指令向前插入,所以不能是跳转指令开头
// lea reg, [reg]
/*
8D00 | lea eax,dword ptr ds:[eax] |
8D09 | lea ecx,dword ptr ds:[ecx] |
8D1B | lea ebx,dword ptr ds:[ebx] |
8D12 | lea edx,dword ptr ds:[edx] |
8D2424 | lea esp,dword ptr ss:[esp] |
8D6D 00 | lea ebp,dword ptr ss:[ebp] |
8D36 | lea esi,dword ptr ds:[esi] |
8D3F | lea edi,dword ptr ds:[edi] |
*/
if (rand() % 4 == 0) {
result.push_back(0x8D);
unsigned char nextIns[] = { 0x00, 0x09, 0x1B, 0x12, 0x36, 0x3F };
result.push_back(nextIns[rand() % sizeof(nextIns)]);
}
else {
result.push_back(0x90);
}
switch (rand() % 7) {
case 0:
{
/*mov reg, reg
8BC0 MOV EAX,EAX
8BDB MOV EBX,EBX
8BC9 MOV ECX,ECX
8BD2 MOV EDX,EDX
8BED MOV EBP,EBP
8BF6 MOV ESI,ESI
8BFF MOV EDI,EDI
8BE4 MOV ESP,ESP
*/
if (rand() % 2) {
result.push_back(0x8B);
}
else {
// xchg reg, reg
result.push_back(0x87);
}
switch (rand() % 8) {
case 0:
result.push_back(0xC0);
break;
case 1:
result.push_back(0xDB);
break;
case 2:
result.push_back(0xC9);
break;
case 3:
result.push_back(0xD2);
break;
case 4:
result.push_back(0xED);
break;
case 5:
result.push_back(0xF6);
break;
case 6:
result.push_back(0xFF);
break;
case 7:
result.push_back(0xE4);
break;
default:
assert(false);
break;
}
}
break;
case 1:
{
/*
jmp $ + len
*/
unsigned char len = rand() % 2 + 1;
if (rand() % 2) {
result.push_back(0xEB);
result.push_back(len);
}
else {
result.push_back(0xE9);
result.push_back(len);
result.push_back(0);
result.push_back(0);
result.push_back(0);
}
for (unsigned char i = 0; i < len; i++) {
result.push_back(OneByteAsm::get());
}
}
break;
case 2:
{
// push ecx
result.push_back(0x51);
/*mov ecx, reg
8BC8 MOV ECX,EAX
8BCB MOV ECX,EBX
8BC9 MOV ECX,ECX
8BCA MOV ECX,EDX
8BCD MOV ECX,EBP
8BCE MOV ECX,ESI
8BCF MOV ECX,EDI
8BCC MOV ECX,ESP
*/
result.push_back(0x8B);
result.push_back(0xC8 + rand() % 8);
// 这个太大了,所以给个1/6的概率
if (rand() % 6) {
// lea ecx, ds:[0x12345678] / mov ecx, 0x12345678
if (rand() % 2) {
result.push_back(0x8D);
result.push_back(0x0D);
}
else {
result.push_back(0xB9);
}
result.push_back(rand());
result.push_back(rand());
result.push_back(rand());
result.push_back(rand());
}
// jmp $ + xxx
unsigned char len = rand() % 2 + 1;
result.push_back(0xE9);
result.push_back(len);
result.push_back(0);
result.push_back(0);
result.push_back(0);
for (unsigned char i = 0; i < len; i++) {
result.push_back(OneByteAsm::get());
}
// pop ecx
result.push_back(0x59);
}
break;
case 3:
{
/*
70 71
72 73
74 75
76 77
78 79
7A 7B
7C 7D
7E 7F
*/
unsigned char len = rand() % 4 + 1;
unsigned char j1 = len + 2;
unsigned char x = 0x70 + (rand() % 8) * 2;
if (rand() % 2) {
result.push_back(x);
result.push_back(j1);
result.push_back(x + 1);
}
else {
result.push_back(x + 1);
result.push_back(j1);
result.push_back(x);
}
result.push_back(len);
for (unsigned char i = 0; i < len; i++)
result.push_back(OneByteAsm::get());
}
break;
case 4:
{
/* 前缀0F
80 81
82 83
84 85
86 87
88 89
8A 8B
8C 8D
8E 8F
*/
unsigned char len = rand() % 4 + 1;
unsigned char j1 = len + 6;
unsigned char x = 0x80 + (rand() % 8) * 2;
if (rand() % 2) {
result.push_back(0x0F);
result.push_back(x);
result.push_back(j1);
result.push_back(0);
result.push_back(0);
result.push_back(0);
result.push_back(0x0F);
result.push_back(x + 1);
}
else {
result.push_back(0x0F);
result.push_back(x + 1);
result.push_back(j1);
result.push_back(0);
result.push_back(0);
result.push_back(0);
result.push_back(0x0F);
result.push_back(x);
}
result.push_back(len);
result.push_back(0);
result.push_back(0);
result.push_back(0);
for (unsigned char i = 0; i < len; i++)
result.push_back(OneByteAsm::get());
}
break;
case 5:
{
// mov dword ptr ss:[esp-xxx], reg
result.push_back(0x89);
switch (rand() % 8)
{
case 0:
result.push_back(0x44);
break;
case 1:
result.push_back(0x4C);
break;
case 2:
result.push_back(0x54);
break;
case 3:
result.push_back(0x5C);
break;
case 4:
result.push_back(0x64);
break;
case 5:
result.push_back(0x6C);
break;
case 6:
result.push_back(0x74);
break;
case 7:
result.push_back(0x7C);
break;
default:
assert(false);
break;
}
result.push_back(0x24);
result.push_back(-(rand() % 4 + 1) * 4);
}
break;
case 6:
{
/*
push ebp;
mov ebp, esp
push reg
...
mov reg2, reg2 / xchg reg3,reg3
...
pop reg
pop ebp
*/
// push ebp
result.push_back(0x55);
// mov ebp, esp
result.push_back(0x8B);
result.push_back(0xEC);
// push reg
unsigned char reg = rand() % 8;
if (reg == 4) {
if (rand() % 2) {
reg++;
}
else {
reg--;
}
}
result.push_back(reg + 0x50);
if (rand() % 2) {
/*mov reg, reg
8BC0 MOV EAX,EAX
8BDB MOV EBX,EBX
8BC9 MOV ECX,ECX
8BD2 MOV EDX,EDX
8BED MOV EBP,EBP
8BF6 MOV ESI,ESI
8BFF MOV EDI,EDI
8BE4 MOV ESP,ESP
*/
result.push_back(0x8B);
}
else {
//xchg reg, reg
result.push_back(0x87);
}
switch (rand() % 8) {
case 0:
result.push_back(0xC0);
break;
case 1:
result.push_back(0xDB);
break;
case 2:
result.push_back(0xC9);
break;
case 3:
result.push_back(0xD2);
break;
case 4:
result.push_back(0xED);
break;
case 5:
result.push_back(0xF6);
break;
case 6:
result.push_back(0xFF);
break;
case 7:
result.push_back(0xE4);
break;
default:
assert(false);
break;
}
// pop reg
result.push_back(reg + 0x58);
// pop ebp
result.push_back(0x5D);
}
break;
default:
assert(false);
break;
}
return result;
}
};
// 替换RETN指令的花指令
class ReplaceRetn {
public:
/*
push reg
mov reg, [esp+4]
xchg reg, [esp]
ret 4
*/
static std::vector<unsigned char> get() {
std::vector<unsigned char> result;
// push reg
unsigned char reg = rand() % 8;
if (reg == 4) {
if (rand() % 2) {
reg++;
}
else {
reg--;
}
}
result.push_back(reg + 0x50);
// mov reg, [esp+4]
result.push_back(0x8B);
switch (reg) {
case 0:
result.push_back(0x44);
break;
case 1:
result.push_back(0x4C);
break;
case 2:
result.push_back(0x54);
break;
case 3:
result.push_back(0x5C);
break;
case 4:
assert(false);
result.push_back(0x64);
break;
case 5:
result.push_back(0x6C);
break;
case 6:
result.push_back(0x74);
break;
case 7:
result.push_back(0x7C);
break;
default:
assert(false);
break;
}
result.push_back(0x24);
result.push_back(0x04);
// xchg reg, [esp]
result.push_back(0x87);
switch (reg) {
case 0:
result.push_back(0x04);
break;
case 1:
result.push_back(0x0C);
break;
case 2:
result.push_back(0x14);
break;
case 3:
result.push_back(0x1C);
break;
case 4:
assert(false);
result.push_back(0x24);
break;
case 5:
result.push_back(0x2C);
break;
case 6:
result.push_back(0x34);
break;
case 7:
result.push_back(0x3C);
break;
default:
assert(false);
break;
}
result.push_back(0x24);
// ret 4
result.push_back(0xC2);
result.push_back(0x04);
result.push_back(0x00);
return result;
};
};