-
Notifications
You must be signed in to change notification settings - Fork 10
/
engine.cpp
4085 lines (3267 loc) · 85.8 KB
/
engine.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
/*
===============================
===============================
== smi²le Permutation Engine ==
=========== by b0b ============
===============================
===============================
+v1.0 (11/2008)
Pretty much just a recode of z0mbie's Real Permutation Engine since his shit fucks up a lot (A LOT), but its still an awesome idea so I tried to fix it.
*Added ETG engine (also by z0mbie) to add random junk code.
*Also ripped some of the mutatable instructions from RPME & Code Pervertor 2&3 for the Mutate() function & added a few new ones.
+v1.1 (01/2009)
*Added a shitload more instructions to Mutate() function. Results are more polymorphic now.
Stuff left to do:
-polymorphic loop generator.
-fix AddTrash() so that it sucks less.
== What do it does? ==
smi²le will disassemble a block of code into a list of opcode information.
Using that list the engine can:
+add trash/junk code
+add random jmps to code
+mutate instructions
When reassembling, the instructions are written to a buffer in random(ish) offsets
and linked together with jmps.
This means you can take a block of code and (per)mutate and/or add junk until it is unrecognizable,
then write to a buffer (maybe already filled with random data ;D) to make code appear 100% different
and obfuscated.
Can you dig it?
== How to make it does what it do? ==
void pseudo-example()
{
if (smi2le::Disasm() == ERR_SUCCESS)
{
for (as_long_as_you_feel_like_it)
{
if (smi2le::AddTrash())
break;
if (smi2le::Permutate())
break;
if (smi2le::Mutate())
break;
//any other shit you add to the engine.
}
if (smi2le::Asm() == ERR_SUCCES)
{
do_stuff();
}
}
}
*/
//#define DEBUG_DUMP_OPLIST
#ifdef DEBUG_DUMP_OPLIST
#include <stdio.h>
#endif
#include "disasm.cpp" //lde32
#include "etg.hpp" //executable trash generator
#include "etg.cpp"
#define MAXCMDLEN 16
#define ERR_SUCCESS 0
#define ERR_BAD_PARAM 1
#define ERR_NO_MEM 2
#define ERR_DISASM 3
#define ERR_MORE_MEM 4
#define ERR_TRY_AGAIN 5
#define M_NONE 0
#define M_DONE 1
#define M_TODO 2
#define MAX_ATTEMPTS 10000
#pragma pack(1)
struct oplist
{
unsigned char op[MAXCMDLEN]; //stored instruction
unsigned short len; //instruction length
unsigned long ofs; //offset of instruction in buffer (only used by Disasm and Asm)
unsigned char asmFlag; //flag is set if op has been assembled (only used by Asm)
oplist* ref; //if this instruction refers to another in the list, this will store a ptr to that oplist
oplist* flow; //next instruction.
oplist* next; //next list entry.
};
#pragma pack()
/*
oplist is a linked list of length disassembled opcodes.
op = raw bytes for opcode
len = length of op, in bytes
ofs = used by Disasm & Asm, holds relative offset of instruction
asmFlag = used by map_asm to mark instructions as assembled
ref = if instruction is a jmp/jcc/loop/call then ref is the oplist that is referred to by this instruction, otherwise ref is null
flow = if instruction is not a jmp/ret then flow will be the next oplist in execution, otherwise it will be null
next = next oplist entry in list. null if @ end of list
*/
const BYTE r2s[] = {
0/*eax*/,
1/*ecx*/,
2/*edx*/,
3/*ebx*/,
// 4/*esp*/,
// 5/*ebp*/,
6/*esi*/,
7/*edi*/ };
class smi2le
{
public:
smi2le(unsigned int seed) { map = NULL; op_root = NULL; srand(seed); }
~smi2le() { if (map) free(map); if (op_root) destroy_opcodes(); }
int Disasm(const unsigned char *lpInBuff, const unsigned long dwInSize, const unsigned long dwEntry); //disasm's into a list of opcodes.
int Permutate(const unsigned short jmpChance); //adds random jmps to opcode list (1 in jmpChance % chance of inserting a jmp @ any instruction currently in oplist).
int AddTrash(const unsigned short wCycles, const unsigned short wMinSize, const unsigned short wMaxSize, const bool bRemovenops);
int Mutate(unsigned short mutChance); //mutate instructions. 1 in mutChance % chance of mutating an instruction if it can be mutated.
int Asm(unsigned char *lpOutBuff, const unsigned long dwOutSize, unsigned long *dwOutEP); //assembles oplist into a buffer.
unsigned long MinBuffSize();
static unsigned long __cdecl my_rand(unsigned long fp, unsigned long range); //ETG requires an external rand function..
private:
static oplist *addsubOps(unsigned char r, unsigned char type, unsigned long origValue, oplist *next, unsigned short min, unsigned short max);
void destroy_opcodes();
long bound_distance(unsigned long n, unsigned long size);
int map_asm(unsigned long dwEP);
unsigned char *map;
unsigned long mapsize;
oplist *op_root;
};
unsigned long smi2le::my_rand(unsigned long fp, unsigned long range)
{
if (range)
return rand() % range;
else
return 0;
}
void smi2le::destroy_opcodes()
{
oplist *op_iter, *i;
for (op_iter = op_root; op_iter; op_iter = i)
{
i = op_iter->next;
free(op_iter);
}
op_root = NULL;
}
unsigned long smi2le::MinBuffSize()
{
oplist *op_iter;
unsigned long ret = 0;
for (op_iter = op_root; op_iter; op_iter = op_iter->next)
{
ret += op_iter->len;
}
return ret;
}
//basically just generates a bunch of add/sub ops.
//so that an instruction like "add eax, 1234" will turn into "add eax, 54321 -> sub eax, 6754 -> sub eax, 46333" or something
//it seemed like a cool idea.
oplist *smi2le::addsubOps(unsigned char r, unsigned char type, unsigned long origValue, oplist *next, unsigned short min, unsigned short max)
//r = destination register.(only last 3 bits should be set, higher 5 will be ignored anyway)
//type = false for sub, true for add
//origValue = original value that should be add/sub'd
//next = next in oplist
//min = min # of instructions
//max = max # of instructions
{
oplist *ret, *templist;
unsigned long v = 0, n;
bool addsub;
ret = (oplist*)malloc(sizeof(oplist));
if (!ret)
return (oplist*)ERR_NO_MEM;
r &= 7;
memset(ret, 0, sizeof(oplist));
templist = ret;
while (max--)
{
if (my_rand(0, 2))
addsub = true;
else
addsub = false;
templist->len = 6;
templist->op[0] = 0x81;
templist->op[1] = 0xC0 | (addsub ? 0 : 0x28) | r;
//random 32bit value in op[2]
n = my_rand(0, 0xFFFF0000);
*(DWORD*)&templist->op[2] = n;
if (addsub)
v += n;
else
v -= n;
templist->flow = (oplist*)malloc(sizeof(oplist));
if (!templist->flow)
return (oplist*)ERR_NO_MEM;
memset(templist->flow, 0, sizeof(oplist));
templist->next = templist->flow;
templist = templist->flow;
if (min)
min--;
else
{
if (!my_rand(0, 3))
break;
}
}
//fix v to = origValue.
templist->len = 6;
templist->op[0] = 0x81;
if (type)
{
//add
if (v > origValue)
{
//sub v - origValue
templist->op[1] = 0xE8 | r;
*(DWORD*)&templist->op[2] = v - origValue;
}
else
{
//add origValue - v
templist->op[1] = 0xC0 | r;
*(DWORD*)&templist->op[2] = origValue - v;
}
}
else
{
//sub
templist->op[1] = 0xE8 | r;
if (v + origValue < v)
{
//sub r, v
//sub origValue
*(DWORD*)&templist->op[2] = v;
templist->flow = (oplist*)malloc(sizeof(oplist));
if (!templist->flow)
return (oplist*)ERR_NO_MEM;
memset(templist->flow, 0, sizeof(oplist));
templist->next = templist->flow;
templist = templist->flow;
templist->len = 6;
templist->op[0] = 0x81;
templist->op[1] = 0xE8 | r;
*(DWORD*)&templist->op[2] = origValue;
}
else
//sub r, v + origValue
*(DWORD*)&templist->op[2] = v + origValue;
}
templist->flow = next;
templist->next = next;
return ret;
}
int smi2le::Mutate(unsigned short mutChance)
{
oplist *op_iter, *it2, *it3, *it4;
unsigned long i;
if (!mutChance)
return ERR_BAD_PARAM;
for (op_iter = op_root; op_iter; op_iter = op_iter->next)
{
if (op_iter->len == 1)
{
//-xchg eax, r
//+push eax
//+push r
//+pop eax
//+pop r
//or
//+push r2
//+mov r2, eax
//+mov eax, r
//+mov r, r2
//+pop r2
//or
//+xchg eax, r (2 byte instruction)
//or
//+xchg r, r2
//+xchg r2, eax
//+xchg r, r2
if (op_iter->op[0] >> 3 == 0x12 && !my_rand(0, mutChance))
{
oplist *it5;
BYTE d;
d = op_iter->op[0] & 7;
switch (my_rand(0, 4))
{
case 0:
it2 = (oplist*)malloc(sizeof(oplist));
if (!it2)
return ERR_NO_MEM;
it3 = (oplist*)malloc(sizeof(oplist));
if (!it3)
{
free(it2);
return ERR_NO_MEM;
}
it4 = (oplist*)malloc(sizeof(oplist));
if (!it4)
{
free(it2);
free(it3);
return ERR_NO_MEM;
}
memset(it3, 0, sizeof(oplist));
memset(it4, 0, sizeof(oplist));
memcpy(it2, op_iter, sizeof(oplist));
op_iter->len = 1;
op_iter->op[0] = 0x50;
op_iter->flow = it3;
op_iter->next = it3;
it3->len = 1;
it3->op[0] = 0x50 | d;
it3->next = it4;
it3->flow = it4;
it4->len = 1;
it4->op[0] = 0x58;
it4->flow = it2;
it4->next = it2;
it2->len = 1;
it2->op[0] = 0x58 | d;
op_iter = it2;
break;
case 1:
for (i = 0; i < sizeof(r2s) / sizeof(BYTE); i++)
{
if (r2s[i] != 0 && r2s[i] != d)
break;
}
if (i == sizeof(r2s) / sizeof(BYTE))
break;
it2 = (oplist*)malloc(sizeof(oplist));
if (!it2)
return ERR_NO_MEM;
it3 = (oplist*)malloc(sizeof(oplist));
if (!it3)
{
free(it2);
return ERR_NO_MEM;
}
it4 = (oplist*)malloc(sizeof(oplist));
if (!it4)
{
free(it2);
free(it3);
return ERR_NO_MEM;
}
it5 = (oplist*)malloc(sizeof(oplist));
if (!it5)
{
free(it2);
free(it3);
free(it4);
return ERR_NO_MEM;
}
memset(it3, 0, sizeof(oplist));
memset(it4, 0, sizeof(oplist));
memset(it5, 0, sizeof(oplist));
memcpy(it2, op_iter, sizeof(oplist));
op_iter->len = 1;
op_iter->op[0] = 0x50 | r2s[i];
op_iter->flow = it3;
op_iter->next = it3;
it3->len = 2;
it3->op[0] = 0x89;
it3->op[1] = 0xC0 | r2s[i];
it3->flow = it4;
it3->next = it4;
it4->len = 2;
it4->op[0] = 0x89;
it4->op[1] = 0xC0 | (d << 3);
it4->flow = it5;
it4->next = it5;
it5->len = 2;
it5->op[0] = 0x89;
it5->op[1] = 0xC0 | (r2s[i] << 3) | d;
it5->flow = it2;
it5->next = it2;
it2->len = 1;
it2->op[0] = 0x58 | r2s[i];
op_iter = it2;
break;
case 2:
op_iter->len = 2;
op_iter->op[0] = 0x87;
op_iter->op[1] = 0xC0 | (my_rand(0, 2) ? (d << 3) : d);
break;
case 3:
for (i = 0; i < sizeof(r2s) / sizeof(BYTE); i++)
{
if (r2s[i] != 0 && r2s[i] != d)
break;
}
if (i == sizeof(r2s) / sizeof(BYTE))
break;
it2 = (oplist*)malloc(sizeof(oplist));
if (!it2)
return ERR_NO_MEM;
it3 = (oplist*)malloc(sizeof(oplist));
if (!it3)
{
free(it2);
return ERR_NO_MEM;
}
memset(it3, 0, sizeof(oplist));
memcpy(it2, op_iter, sizeof(oplist));
op_iter->len = 2;
op_iter->op[0] = 0x87;
op_iter->op[1] = 0xC0 | (d << 3) | r2s[i];
op_iter->flow = it3;
op_iter->next = it3;
it3->len = 1;
it3->op[0] = 0x90 | r2s[i];
it3->flow = it2;
it3->next = it2;
it2->len = 2;
it2->op[0] = 0x87;
it2->op[1] = 0xC0 | (d << 3) | r2s[i];
op_iter = it2;
break;
}
continue;
}
}
if (op_iter->len == 2)
{
//- 10001011 11100101 ; mov esp,ebp
// 01011101 ; pop ebp
//+ 11001001 ; leave
if (op_iter->op[0] == 0x8B && op_iter->op[1] == 0xE5 && !my_rand(0, mutChance))
{
for (it2 = op_iter->flow; it2;)
{
if (it2->len == 1 && it2->op[0] == 0x5D)
{
it2->op[0] = 0xC9;
memcpy(op_iter, it2, sizeof(oplist));
free(it2);
break;
}
else
{
if (it2->ref && !it2->flow)
{
it2 = it2->ref;
}
else
it2 = NULL;
}
}
if (it2)
continue;
}
if ((op_iter->op[1] & 0xC0) == 0xC0)
{
if ((op_iter->op[0] & 0xFD) == 0x89 && !my_rand(0, mutChance))
{
// - 10001001 11xxxyyy ; mov r1,r2
// + 01010xxx ; push r2
// 01011yyy ; pop r1
//or
// - 10001011 11xxxyyy ; mov r1,r2
// + 01010yyy ; push r2
// 01011xxx ; pop r1
unsigned char t = op_iter->op[0];
it2 = (oplist*)malloc(sizeof(oplist));
if (!it2)
return ERR_NO_MEM;
memcpy(it2, op_iter, sizeof(oplist));
op_iter->len = 1;
op_iter->op[0] = 0x50 | ((op_iter->op[1] >> (t == 0x89 ? 3 : 0)) & 7);
op_iter->flow = it2;
op_iter->next = it2;
it2->len = 1;
it2->op[0] = 0x58 | ((op_iter->op[1] >> (t == 0x89 ? 0 : 3)) & 7);
op_iter = it2;
continue;
}
// - 00ttt001 11xxxyyy ; ttt r1,r2 (ADD,ADC,AND,OR,SUB,SBB,XOR,CMP)
// + 00ttt011 11yyyxxx
// - 10001001 11xxxyyy ; mov r1,r2
// + 10001011 11yyyxxx
if (((op_iter->op[0] == 0x89 || op_iter->op[0] == 0x8B) || (!(op_iter->op[0] & 0xC0) && (op_iter->op[0] & 7) == 1 && (op_iter->op[0] & 7) == 3)))
{
op_iter->op[0] ^= 0x02;
op_iter->op[1] = 0xC0 | ((op_iter->op[1] >> 3) & 7) | ((op_iter->op[1] & 7) << 3);
continue;
}
//if (xxx==yyy)
//- 001100xx 11xxxyyy ; xor r1,r1
//+ 001010xx 11xxxyyy ; sub r1,r1
if ((op_iter->op[0] & 0xFC) == 0x30 || (op_iter->op[0] & 0xFC) == 0x28)
{
if (((op_iter->op[1] >> 3 ) & 7) == (op_iter->op[1] & 7) && !my_rand(0, mutChance))
{
op_iter->op[0] ^= 0x18;
continue;
}
}
//if (xxx==yyy)
//- 0000100x 11xxxyyy ; or r1,r1
//+ 1000010x 11xxxyyy ; test r1,r1
if (((op_iter->op[0] & 0xFE) == 0x08 || (op_iter->op[0] & 0xFE) == 0x84) && ((op_iter->op[1] >> 3) & 7) == (op_iter->op[1] & 7) && !my_rand(0, mutChance))
{
op_iter->op[0] ^= 0x8C;
continue;
}
//sub r1, r2 -> 00101001 11abcxyz (abc = r2, xyz = r1)
if (op_iter->op[0] == 0x29 && !my_rand(0, mutChance))
{
BYTE r1, r2;
r1 = op_iter->op[1] & 7;
r2 = (op_iter->op[1] >> 3) & 7;
//sub r, r
if (r1 == r2)
{
if (my_rand(0, 2))
{
//mov r1, 0
op_iter->len = 5;
op_iter->op[0] = 0xB8 | r1;
*(DWORD*)&op_iter->op[1] = 0;
}
else
{
//or
//xor r1, r1
op_iter->len = 2;
op_iter->op[0] = 0x31;
op_iter->op[1] = 0xC0 | (r1 << 3) | r1;
}
}
else
{
//push r3
//mov r3, r2
//sub r1, r3
//pop r3
//or
//push r3
//mov r3, r1
//sub r3, r2
//mov r1, r3
//pop r3
for (i = 0; i < sizeof(r2s) / sizeof(BYTE); i++)
{
if (r2s[i] != r1 && r2s[i] != r2)
break;
}
if (i == sizeof(r2s) / sizeof(BYTE))
continue;
it2 = (oplist*)malloc(sizeof(oplist));
if (!it2)
return ERR_NO_MEM;
it3 = (oplist*)malloc(sizeof(oplist));
if (!it3)
{
free(it2);
return ERR_NO_MEM;
}
it4 = (oplist*)malloc(sizeof(oplist));
if (!it4)
{
free(it2);
free(it3);
return ERR_NO_MEM;
}
memset(it3, 0, sizeof(oplist));
memset(it4, 0, sizeof(oplist));
memcpy(it2, op_iter, sizeof(oplist));
if (my_rand(0, 2))
{
op_iter->len = 1;
op_iter->op[0] = 0x50 | r2s[i];
op_iter->flow = it3;
op_iter->next = it3;
it3->len = 2;
it3->op[0] = 0x89;
it3->op[1] = 0xC0 | (r2 << 3) | r2s[i];
it3->flow = it4;
it3->next = it4;
it4->len = 2;
it4->op[0] = 0x29;
it4->op[1] = 0xC0 | (r2s[i] << 3) | r1;
it4->flow = it2;
it4->next = it2;
it2->len = 1;
it2->op[0] = 0x58 | r2s[i];
}
else
{
oplist *it5;
it5 = (oplist*)malloc(sizeof(oplist));
if (!it5)
{
memcpy(op_iter, it2, sizeof(oplist));
free(it2);
free(it3);
free(it4);
return ERR_NO_MEM;
}
op_iter->len = 1;
op_iter->op[0] = 0x50 | r2s[i];
op_iter->flow = it3;
op_iter->next = it3;
it3->len = 2;
it3->op[0] = 0x89;
it3->op[1] = 0xC0 | (r1 << 3) | r2s[i];
it3->flow = it4;
it3->next = it4;
it4->len = 2;
it4->op[0] = 0x29;
it4->op[1] = 0xC0 | (r2 << 3) | r2s[i];
it4->flow = it5;
it4->next = it5;
it5->len = 2;
it5->op[0] = 0x89;
it5->op[1] = 0xC0 | (r2s[i] << 3) | r1;
it5->flow = it2;
it5->next = it2;
it2->len = 1;
it2->op[0] = 0x58 | r2s[i];
}
op_iter = it2;
}
continue;
}
//-xchg r1, r2
//+xchg r2, r1
//or
//+push r1
//+push r2
//+pop r1
//+pop r2
//or
//+push r3
//+mov r3, r1
//+mov r1, r2
//+mov r2, r3
//+pop r3
//or
//+xchg r3, r2
//+xchg r1, r3
//+xchg r2, r3
if (op_iter->op[0] == 0x87 && !my_rand(0, mutChance))
{
oplist *it5;
BYTE d1, d2;
d1 = op_iter->op[1] & 7;
d2 = (op_iter->op[1] >> 3) & 7;
switch (my_rand(0, 4))
{
case 0:
op_iter->op[1] = 0xC0 | (d1 << 3) | d2;
break;
case 1:
it2 = (oplist*)malloc(sizeof(oplist));
if (!it2)
return ERR_NO_MEM;
it3 = (oplist*)malloc(sizeof(oplist));
if (!it3)
{
free(it2);
return ERR_NO_MEM;
}
it4 = (oplist*)malloc(sizeof(oplist));
if (!it4)
{
free(it2);
free(it3);
return ERR_NO_MEM;
}
memset(it3, 0, sizeof(oplist));
memset(it4, 0, sizeof(oplist));
memcpy(it2, op_iter, sizeof(oplist));
op_iter->len = 1;
op_iter->op[0] = 0x50 | d1;
op_iter->flow = it3;
op_iter->next = it3;
it3->len = 1;
it3->op[0] = 0x50 | d2;
it3->flow = it4;
it3->next = it4;
it4->len = 1;
it4->op[0] = 0x58 | d1;
it4->flow = it2;
it4->next = it2;
it2->len = 1;
it2->op[0] = 0x58 | d2;
op_iter = it2;
break;
case 2:
for (i = 0; i < sizeof(r2s) / sizeof(BYTE); i++)
{
if (r2s[i] != d1 && r2s[i] != d2)
break;
}
if (i == sizeof(r2s) / sizeof(BYTE))
break;
it2 = (oplist*)malloc(sizeof(oplist));
if (!it2)
return ERR_NO_MEM;
it3 = (oplist*)malloc(sizeof(oplist));
if (!it3)
{
free(it2);
return ERR_NO_MEM;
}
it4 = (oplist*)malloc(sizeof(oplist));
if (!it4)
{
free(it2);
free(it3);
return ERR_NO_MEM;
}
it5 = (oplist*)malloc(sizeof(oplist));
if (!it5)
{
free(it2);
free(it3);
free(it4);
return ERR_NO_MEM;
}
memset(it3, 0, sizeof(oplist));
memset(it4, 0, sizeof(oplist));
memset(it5, 0, sizeof(oplist));
memcpy(it2, op_iter, sizeof(oplist));
op_iter->len = 1;
op_iter->op[0] = 0x50 | r2s[i];
op_iter->flow = it3;
op_iter->next = it3;
it3->len = 2;
it3->op[0] = 0x89;
it3->op[1] = 0xC0 | (d1 << 3) | r2s[i];
it3->flow = it4;
it3->next = it4;
it4->len = 2;
it4->op[0] = 0x89;
it4->op[1] = 0xC0 | (d2 << 3) | d1;
it4->flow = it5;
it4->next = it5;
it5->len = 2;
it5->op[0] = 0x89;
it5->op[1] = 0xC0 | (r2s[i] << 3) | d2;
it5->flow = it2;
it5->next = it2;
it2->len = 1;
it2->op[0] = 0x58 | r2s[i];
op_iter = it2;
break;
case 3:
for (i = 0; i < sizeof(r2s) / sizeof(BYTE); i++)
{
if (r2s[i] != d1 && r2s[i] != d2)
break;
}
if (i == sizeof(r2s) / sizeof(BYTE))
break;
it2 = (oplist*)malloc(sizeof(oplist));
if (!it2)
return ERR_NO_MEM;
it3 = (oplist*)malloc(sizeof(oplist));
if (!it3)
{
free(it2);
return ERR_NO_MEM;
}
memset(it3, 0, sizeof(oplist));
memcpy(it2, op_iter, sizeof(oplist));
op_iter->len = 2;
op_iter->op[0] = 0x87;
op_iter->op[1] = 0xC0 | (r2s[i] << 3) | d2;
op_iter->flow = it3;
op_iter->next = it3;
it3->len = 2;
it3->op[0] = 0x87;
it3->op[1] = 0xC0 | (d1 << 3) | r2s[i];
it3->flow = it2;
it3->next = it2;
it2->len = 2;
it2->op[0] = 0x87;
it2->op[1] = 0xC0 | (d2 << 3) | r2s[i];
op_iter = it2;
break;
}
continue;
}
}
//- mov [r1], r2 -> 10001001 00xxxyyy
//+ push r2 -> 01010xxx
//+ pop [r1] -> 10001111 000000yyy
//or
//- mov r1, [r2] -> 10001011 00xxxyyy
//+ push [r2] -> 11111111 00110yyy
//+ pop r1 -> 01011xxx
if ((op_iter->op[0] & 0xFD) == 0x89 && !my_rand(0, mutChance) && (op_iter->op[1] & 0xC0) == 0)
{
BYTE d, s;
it2 = (oplist*)malloc(sizeof(oplist));
if (!it2)
return ERR_NO_MEM;
memcpy(it2, op_iter, sizeof(oplist));
op_iter->next = it2;
op_iter->flow = it2;
if (op_iter->op[0] == 0x89)
{