This repository has been archived by the owner on May 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
idasploiter.py
2744 lines (2095 loc) · 106 KB
/
idasploiter.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 python
#
# IDA Sploiter is an exploit development and vulnerability research environment
# implemented as a plugin for Hex-Ray's IDA Pro disassembler.
IDASPLOITER_VERSION = "1.1"
# Copyright (C) 2014 Peter Kacherginsky
# 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 OWNER 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.
# Performance profiling
import cProfile
import pstats
# IDA libraries
import idaapi
import idautils
import idc
from idaapi import Form, Choose2, plugin_t
# Python libraries
import os
import binascii
import string
import textwrap
import copy
import csv
import itertools
import struct
from struct import pack, unpack
from ctypes import *
###############################################################################
# Data Tables and Structures
###############################################################################
# Initialize the list of supported processors.
SPLOITER_SUPPORTED_ARCHES = [
idaapi.PLFM_386,
idaapi.PLFM_PPC
]
###############################################################################
# 00-FF Single byte to unicode transforms
# NOTE: Some unicode characters can have two bytes
# http://www.phenoelit.org/stuff/Phenoelit20c3.pdf
# Ascii_to_Unicode_transforms
ASCII_TO_UNICODE_GENERAL = [
"\x00\x00", "\x00\x01", "\x00\x02", "\x00\x03", "\x00\x04", "\x00\x05",
"\x00\x06", "\x00\x07", "\x00\x08", "\x00\x09", "\x00\x0a", "\x00\x0b",
"\x00\x0c", "\x00\x0d", "\x00\x0e", "\x00\x0f", "\x00\x10", "\x00\x11",
"\x00\x12", "\x00\x13", "\x00\x14", "\x00\x15", "\x00\x16", "\x00\x17",
"\x00\x18", "\x00\x19", "\x00\x1a", "\x00\x1b", "\x00\x1c", "\x00\x1d",
"\x00\x1e", "\x00\x1f", "\x00\x20", "\x00\x21", "\x00\x22", "\x00\x23",
"\x00\x24", "\x00\x25", "\x00\x26", "\x00\x27", "\x00\x28", "\x00\x29",
"\x00\x2a", "\x00\x2b", "\x00\x2c", "\x00\x2d", "\x00\x2e", "\x00\x2f",
"\x00\x30", "\x00\x31", "\x00\x32", "\x00\x33", "\x00\x34", "\x00\x35",
"\x00\x36", "\x00\x37", "\x00\x38", "\x00\x39", "\x00\x3a", "\x00\x3b",
"\x00\x3c", "\x00\x3d", "\x00\x3e", "\x00\x3f", "\x00\x40", "\x00\x41",
"\x00\x42", "\x00\x43", "\x00\x44", "\x00\x45", "\x00\x46", "\x00\x47",
"\x00\x48", "\x00\x49", "\x00\x4a", "\x00\x4b", "\x00\x4c", "\x00\x4d",
"\x00\x4e", "\x00\x4f", "\x00\x50", "\x00\x51", "\x00\x52", "\x00\x53",
"\x00\x54", "\x00\x55", "\x00\x56", "\x00\x57", "\x00\x58", "\x00\x59",
"\x00\x5a", "\x00\x5b", "\x00\x5c", "\x00\x5d", "\x00\x5e", "\x00\x5f",
"\x00\x60", "\x00\x61", "\x00\x62", "\x00\x63", "\x00\x64", "\x00\x65",
"\x00\x66", "\x00\x67", "\x00\x68", "\x00\x69", "\x00\x6a", "\x00\x6b",
"\x00\x6c", "\x00\x6d", "\x00\x6e", "\x00\x6f", "\x00\x70", "\x00\x71",
"\x00\x72", "\x00\x73", "\x00\x74", "\x00\x75", "\x00\x76", "\x00\x77",
"\x00\x78", "\x00\x79", "\x00\x7a", "\x00\x7b", "\x00\x7c", "\x00\x7d",
"\x00\x7e", "\x00\x7f"
]
ASCII_TO_UNICODE_ANSI = ASCII_TO_UNICODE_GENERAL + [
"\x20\xac", "\x00\x81", "\x20\x1a", "\x01\x92", "\x20\x1e", "\x20\x26",
"\x20\x20", "\x20\x21", "\x02\xc6", "\x20\x30", "\x01\x60", "\x20\x39",
"\x01\x52", "\x00\x8d", "\x01\x7d", "\x00\x8f", "\x90\x00", "\x20\x18",
"\x20\x19", "\x20\x1c", "\x20\x1d", "\x20\x22", "\x20\x13", "\x20\x14",
"\x02\xdc", "\x21\x22", "\x01\x61", "\x3a\x20", "\x01\x53", "\x00\x9d",
"\x01\x7e", "\x01\x78", "\x00\xa0", "\x00\xa1", "\x00\xa2", "\x00\xa3",
"\x00\xa4", "\x00\xa5", "\x00\xa6", "\x00\xa7", "\x00\xa8", "\x00\xa9",
"\x00\xaa", "\x00\xab", "\x00\xac", "\x00\xad", "\x00\xae", "\x00\xaf",
"\x00\xb0", "\x00\xb1", "\x00\xb2", "\x00\xb3", "\x00\xb4", "\x00\xb5",
"\x00\xb6", "\x00\xb7", "\x00\xb8", "\x00\xb9", "\x00\xba", "\x00\xbb",
"\x00\xbc", "\x00\xbd", "\x00\xbe", "\x00\xbf", "\x00\xc0", "\x00\xc1",
"\x00\xc2", "\x00\xc3", "\x00\xc4", "\x00\xc5", "\x00\xc6", "\x00\xc7",
"\x00\xc8", "\x00\xc9", "\x00\xca", "\x00\xcb", "\x00\xcc", "\x00\xcd",
"\x00\xce", "\x00\xcf", "\x00\xd0", "\x00\xd1", "\x00\xd2", "\x00\xd3",
"\x00\xd4", "\x00\xd5", "\x00\xd6", "\x00\xd7", "\x00\xd8", "\x00\xd9",
"\x00\xda", "\x00\xdb", "\x00\xdc", "\x00\xdd", "\x00\xde", "\x00\xdf",
"\x00\xe0", "\x00\xe1", "\x00\xe2", "\x00\xe3", "\x00\xe4", "\x00\xe5",
"\x00\xe6", "\x00\xe7", "\x00\xe8", "\x00\xe9", "\x00\xea", "\x00\xeb",
"\x00\xec", "\x00\xed", "\x00\xee", "\x00\xef", "\x00\xf0", "\x00\xf1",
"\x00\xf2", "\x00\xf3", "\x00\xf4", "\x00\xf5", "\x00\xf6", "\x00\xf7",
"\x00\xf8", "\x00\xf9", "\x00\xfa", "\x00\xfb", "\x00\xfc", "\x00\xfd",
"\x00\xfe", "\x00\xff"
]
ASCII_TO_UNICODE_OEM = ASCII_TO_UNICODE_GENERAL + [
"\x00\xc7", "\x00\xfc", "\x00\xe9", "\x00\xe2", "\x00\xe4", "\x00\xe0",
"\x00\xe5", "\x00\xe7", "\x00\xea", "\x00\xeb", "\x00\xe8", "\x00\xef",
"\x00\xee", "\x00\xec", "\x00\xc4", "\x00\xc5", "\x00\xc9", "\x00\xe6",
"\x00\xc6", "\x00\xf4", "\x00\xf6", "\x00\xf2", "\x00\xfb", "\x00\xf9",
"\x00\xff", "\x00\xd6", "\x00\xdc", "\x00\xf8", "\x00\xa3", "\x00\xd8",
"\x00\xd7", "\x01\x92", "\x00\xe1", "\x00\xed", "\x00\xf3", "\x00\xfa",
"\x00\xf1", "\x00\xd1", "\x00\xaa", "\x00\xba", "\x00\xbf", "\x00\xae",
"\x00\xac", "\x00\xbd", "\x00\xbc", "\x00\xa1", "\x00\xab", "\x00\xbb",
"\x25\x91", "\x25\x92", "\x25\x93", "\x25\x02", "\x25\x24", "\x00\xc1",
"\x00\xc2", "\x00\xc0", "\x00\xa9", "\x25\x63", "\x25\x51", "\x25\x57",
"\x25\x5d", "\x00\xa2", "\x00\xa5", "\x25\x10", "\x25\x14", "\x25\x34",
"\x25\x2c", "\x25\x1c", "\x25\x00", "\x25\x3c", "\x00\xe3", "\x00\xc3",
"\x25\x5a", "\x25\x54", "\x25\x69", "\x25\x66", "\x25\x60", "\x25\x50",
"\x25\x6c", "\x00\xa4", "\x00\xf0", "\x00\xd0", "\x00\xca", "\x00\xcb",
"\x00\xc8", "\x01\x31", "\x00\xcd", "\x00\xce", "\x00\xcf", "\x25\x18",
"\x25\x0c", "\x25\x88", "\x25\x84", "\x00\xa6", "\x00\xcc", "\x25\x80",
"\x00\xd3", "\x00\xdf", "\x00\xd4", "\x00\xd2", "\x00\xf5", "\x00\xd5",
"\x00\xb5", "\x00\xfe", "\x00\xde", "\x00\xda", "\x00\xdb", "\x00\xd9",
"\x00\xfd", "\x00\xdd", "\x00\xaf", "\x00\xb4", "\x00\xad", "\x00\xb1",
"\x20\x17", "\x00\xbe", "\x00\xb6", "\x00\xa7", "\x00\xf7", "\x00\xb8",
"\x00\xb0", "\x00\xa8", "\x00\xb7", "\x00\xb9", "\x00\xb3", "\x00\xb2",
"\x25\xa0", "\x00\xa0"
]
ASCII_TO_UNICODE_UTF7 = ASCII_TO_UNICODE_GENERAL + [
"\xff\x80", "\xff\x81", "\xff\x82", "\xff\x83", "\xff\x84", "\xff\x85",
"\xff\x86", "\xff\x87", "\xff\x88", "\xff\x89", "\xff\x8a", "\xff\x8b",
"\xff\x8c", "\xff\x8d", "\xff\x8e", "\xff\x8f", "\xff\x90", "\xff\x91",
"\xff\x92", "\xff\x93", "\xff\x94", "\xff\x95", "\xff\x96", "\xff\x97",
"\xff\x98", "\xff\x99", "\xff\x9a", "\xff\x9b", "\xff\x9c", "\xff\x9d",
"\xff\x9e", "\xff\x9f", "\xff\xa0", "\xff\xa1", "\xff\xa2", "\xff\xa3",
"\xff\xa4", "\xff\xa5", "\xff\xa6", "\xff\xa7", "\xff\xa8", "\xff\xa9",
"\xff\xaa", "\xff\xab", "\xff\xac", "\xff\xad", "\xff\xae", "\xff\xaf",
"\xff\xb0", "\xff\xb1", "\xff\xb2", "\xff\xb3", "\xff\xb4", "\xff\xb5",
"\xff\xb6", "\xff\xb7", "\xff\xb8", "\xff\xb9", "\xff\xba", "\xff\xbb",
"\xff\xbc", "\xff\xbd", "\xff\xbe", "\xff\xbf", "\xff\xc0", "\xff\xc1",
"\xff\xc2", "\xff\xc3", "\xff\xc4", "\xff\xc5", "\xff\xc6", "\xff\xc7",
"\xff\xc8", "\xff\xc9", "\xff\xca", "\xff\xcb", "\xff\xcc", "\xff\xcd",
"\xff\xce", "\xff\xcf", "\xff\xd0", "\xff\xd1", "\xff\xd2", "\xff\xd3",
"\xff\xd4", "\xff\xd5", "\xff\xd6", "\xff\xd7", "\xff\xd8", "\xff\xd9",
"\xff\xda", "\xff\xdb", "\xff\xdc", "\xff\xdd", "\xff\xde", "\xff\xdf",
"\xff\xe0", "\xff\xe1", "\xff\xe2", "\xff\xe3", "\xff\xe4", "\xff\xe5",
"\xff\xe6", "\xff\xe7", "\xff\xe8", "\xff\xe9", "\xff\xea", "\xff\xeb",
"\xff\xec", "\xff\xed", "\xff\xee", "\xff\xef", "\xff\xf0", "\xff\xf1",
"\xff\xf2", "\xff\xf3", "\xff\xf4", "\xff\xf5", "\xff\xf6", "\xff\xf7",
"\xff\xf8", "\xff\xf9", "\xff\xfa", "\xff\xfb", "\xff\xfc", "\xff\xfd",
"\xff\xfe", "\xff\xff"
]
ASCII_TO_UNICODE_UTF8 = ASCII_TO_UNICODE_GENERAL + [
"\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00",
"\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00",
"\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00",
"\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00",
"\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00",
"\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00",
"\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00",
"\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00",
"\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00",
"\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00",
"\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x00", "\x00\x01",
"\x00\x02", "\x00\x03", "\x00\x04", "\x00\x05", "\x00\x06", "\x00\x07",
"\x00\x08", "\x00\x09", "\x00\x0a", "\x00\x0b", "\x00\x0c", "\x00\x0d",
"\x00\x0e", "\x00\x0f", "\x00\x10", "\x00\x11", "\x00\x12", "\x00\x13",
"\x00\x14", "\x00\x15", "\x00\x16", "\x00\x17", "\x00\x18", "\x00\x19",
"\x00\x1a", "\x00\x1b", "\x00\x1c", "\x00\x1d", "\x00\x1e", "\x00\x1f",
"\x00\x00", "\x00\x01", "\x00\x02", "\x00\x03", "\x00\x04", "\x00\x05",
"\x00\x06", "\x00\x07", "\x00\x08", "\x00\x09", "\x00\x0a", "\x00\x0b",
"\x00\x0c", "\x00\x0d", "\x00\x0e", "\x00\x0f", "\x00\x00", "\x00\x01",
"\x00\x02", "\x00\x03", "\x00\x04", "\x00\x05", "\x00\x06", "\x00\x07",
"\x00\x08", "\x00\x09", "\x00\x0a", "\x00\x0b", "\x00\x0c", "\x00\x0d",
"\x00\x0e", "\x00\x0f"
]
ASCII_TO_UNICODE = [ASCII_TO_UNICODE_ANSI,
ASCII_TO_UNICODE_OEM,
ASCII_TO_UNICODE_UTF7,
ASCII_TO_UNICODE_UTF8]
###############################################################################
# Microsoft Portable Executable and Common Object File Format Specification
# Revision 8.3 - February 6, 2013
class IMAGE_DOS_HEADER(Structure):
_fields_ = [
("signature" , c_char * 2),
("lastsize" , c_short),
("nblocks" , c_short),
("nreloc" , c_short),
("hdrsize" , c_short),
("minalloc" , c_short),
("maxalloc" , c_short),
("ss" , c_short),
("sp" , c_short),
("checksum" , c_short),
("ip" , c_short),
("cs" , c_short),
("relocpos" , c_short),
("noverlay" , c_short),
("reserved1" , c_short * 4),
("oem_id" , c_short),
("reserved2" , c_short * 10),
("e_lfanew" , c_long)
]
class IMAGE_FILE_HEADER(Structure):
_fields_ = [
("Machine" , c_short),
("NumberOfSections" , c_short),
("TimeDateStamp" , c_long),
("PointerToSymbolTable" , c_long),
("NumberOfSymbols" , c_long),
("SizeOfOptionalHeader" , c_short),
("Characteristics" , c_short)
]
class IMAGE_DATA_DIRECTORY(Structure):
_fields_ = [
("VirtualAddress", c_long),
("Size", c_long)
]
class IMAGE_OPTIONAL_HEADER(Structure):
_fields_ = [
("signature" , c_short),
("MajorLinkerVersion" , c_byte),
("MinorLinkerVersion" , c_byte),
("SizeOfCode" , c_long),
("SizeOfInitializedData" , c_long),
("SizeOfUninitializedData" , c_long),
("AddressOfEntryPoint" , c_long),
("BaseOfCode" , c_long),
("BaseOfData" , c_long),
("ImageBase" , c_long),
("SectionAlignment" , c_long),
("FileAlignment" , c_long),
("MajorOSVersion" , c_short),
("MinorOSVersion" , c_short),
("MajorImageVersion" , c_short),
("MinorImageVersion" , c_short),
("MajorSubsystemVersion" , c_short),
("MinorSubsystemVersion" , c_short),
("Reserved" , c_long),
("SizeOfImage" , c_long),
("SizeOfHeaders" , c_long),
("Checksum" , c_long),
("Subsystem" , c_short),
("DLLCharacteristics" , c_short),
("SizeOfStackReserve" , c_long),
("SizeOfStackCommit" , c_long),
("SizeOfHeapReserve" , c_long),
("SizeOfHeapCommit" , c_long),
("LoaderFlags" , c_long),
("NumberOfRvaAndSizes" , c_long),
("DataDirectory" , IMAGE_DATA_DIRECTORY * 16)
]
class IMAGE_OPTIONAL_HEADER64(Structure):
_fields_ = [
("signature" , c_short),
("MajorLinkerVersion" , c_byte),
("MinorLinkerVersion" , c_byte),
("SizeOfCode" , c_long),
("SizeOfInitializedData" , c_long),
("SizeOfUninitializedData" , c_long),
("AddressOfEntryPoint" , c_long),
("BaseOfCode" , c_long),
("ImageBase" , c_longlong),
("SectionAlignment" , c_long),
("FileAlignment" , c_long),
("MajorOSVersion" , c_short),
("MinorOSVersion" , c_short),
("MajorImageVersion" , c_short),
("MinorImageVersion" , c_short),
("MajorSubsystemVersion" , c_short),
("MinorSubsystemVersion" , c_short),
("Reserved" , c_long),
("SizeOfImage" , c_long),
("SizeOfHeaders" , c_long),
("Checksum" , c_long),
("Subsystem" , c_short),
("DLLCharacteristics" , c_short),
("SizeOfStackReserve" , c_longlong),
("SizeOfStackCommit" , c_longlong),
("SizeOfHeapReserve" , c_longlong),
("SizeOfHeapCommit" , c_longlong),
("LoaderFlags" , c_long),
("NumberOfRvaAndSizes" , c_long),
("DataDirectory" , IMAGE_DATA_DIRECTORY * 16)
]
class IMAGE_LOAD_CONFIG_DIRECTORY(Structure):
_fields_ = [
("Size" , c_long),
("TimeDateStamp" , c_long),
("MajorVersion" , c_short),
("MinorVersion" , c_short),
("GlobalFlagsClear" , c_long),
("GlobalFlagsSet" , c_long),
("CriticalSectionDefaultTimeout" , c_long),
("DeCommitFreeBlockThreshold" , c_long),
("DeCommitTotalFreeThreshold" , c_long),
("LockPrefixTable" , c_long),
("MaximumAllocationSize" , c_long),
("VirtualMemoryThreshold" , c_long),
("ProcessHeapFlags" , c_long),
("ProcessAffinityMask" , c_long),
("CSDVersion" , c_short),
("Reserved1" , c_short),
("EditList" , c_long),
("SecurityCookie" , c_long),
("SEHandlerTable" , c_long),
("SEHandlerCount" , c_long)
]
class IMAGE_LOAD_CONFIG_DIRECTORY64(Structure):
_fields_ = [
("Size" , c_long),
("TimeDateStamp" , c_long),
("MajorVersion" , c_short),
("MinorVersion" , c_short),
("GlobalFlagsClear" , c_long),
("GlobalFlagsSet" , c_long),
("CriticalSectionDefaultTimeout" , c_long),
("DeCommitFreeBlockThreshold" , c_longlong),
("DeCommitTotalFreeThreshold" , c_longlong),
("LockPrefixTable" , c_longlong),
("MaximumAllocationSize" , c_longlong),
("VirtualMemoryThreshold" , c_longlong),
("ProcessHeapFlags" , c_long),
("ProcessAffinityMask" , c_long),
("CSDVersion" , c_short),
("Reserved1" , c_short),
("EditList" , c_longlong),
("SecurityCookie" , c_longlong),
("SEHandlerTable" , c_longlong),
("SEHandlerCount" , c_longlong),
]
def is_processor_supported():
# Check if the current processor is supported.
if idaapi.ph.id not in SPLOITER_SUPPORTED_ARCHES:
return False
else:
return True
def read_module_memory(addr, size):
# Determine if the debugger is running and loaded.
if idaapi.dbg_can_query() and idaapi.get_process_state() < 0:
return idaapi.dbg_read_memory(addr, size)
else:
return idaapi.get_many_bytes(addr, size)
###############################################################################
# Module Class - Manages module characteristics
class Module():
def __init__(self, name, size, base, rebase_to):
self.addr = base
# BUG: IDA's API does not always zero out the SWIG buffer.
# BUG: ntdll does not have path information.
if "\x00" in name:
name,junk = name.split("\x00",1)
self.file = os.path.basename(name)
self.path = os.path.dirname(name)
self.size = size
# Parse the module as a PE file
if idaapi.dbg_can_query() and idaapi.get_process_state() < 0:
pe = PE(self.addr)
else:
pe = None
self.NXCompat = "No"
self.ASLR = "No"
self.SafeSEH = "N/A"
self.GS = "Null"
if pe:
self.NXCompat = pe.isNXCompat()
self.ASLR = pe.isDynamicBase()
self.SafeSEH = pe.isSafeSEH()
self.GS = pe.isGS()
###############################################################################
# PE Module Parser
class PE():
# PE flags
IMAGE_FILE_MACHINE_I386 = 0x14c
MAGIC_PE32 = 0x10b
MAGIC_PE32_PLUS = 0x20b
IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE = 0x0040
IMAGE_DLL_CHARACTERISTICS_NX_COMPAT = 0x0100
IMAGE_DLLCHARACTERISTICS_NO_SEH = 0x0400
def __init__(self, base):
# PE headers
self.dos_header = None
self.file_header = None
self.optional_header = None
self.load_config_directory = None
self.arch64 = False
# Verify DOS header magic
magic_dos = read_module_memory( base, 0x2)
if magic_dos != "MZ":
print "[idasploiter] Invalid DOS header magic."
return None
# Parse the DOS header
self.dos_header = IMAGE_DOS_HEADER.from_buffer_copy(
read_module_memory(base, 0x40) )
# Get offset to PE header
base_pe = base + self.dos_header.e_lfanew
# Verify PE header magic
magic_pe = read_module_memory( base_pe, 0x4 )
if magic_pe != "PE\x00\x00":
print "[idasploiter] Invalid PE header magic: %x" % unpack("I",magic_pe)[0]
return None
# Parse the FILE header
# NOTE: IMAGE_FILE_HEADER size is 0x14
self.file_header = IMAGE_FILE_HEADER.from_buffer_copy(
read_module_memory( base_pe + 0x4, sizeof(IMAGE_FILE_HEADER) ) )
# Parse the OPTIONAL header
base_pe_optional = base_pe + 0x4 + 0x14
magic_pe_opt = read_module_memory ( base_pe_optional, 0x2)
magic_pe_opt = unpack("H", magic_pe_opt)[0]
# PE32 Optional Header
if magic_pe_opt == self.MAGIC_PE32:
self.arch64 = False
self.optional_header = IMAGE_OPTIONAL_HEADER.from_buffer_copy(
read_module_memory( base_pe_optional, self.file_header.SizeOfOptionalHeader ))
# PE32+ Optional Header
elif magic_pe_opt == self.MAGIC_PE32_PLUS:
self.arch64 = True
self.optional_header = IMAGE_OPTIONAL_HEADER64.from_buffer_copy(
read_module_memory( base_pe_optional, self.file_header.SizeOfOptionalHeader ))
# Invalid PE Header
else:
print "[idasploiter] Invalid IMAGE_OPTIONAL_HEADER magic: %x" % unpack("H",magic_pe_opt)[0]
return None
# Load Configuration Table
load_config_directory_rva = self.optional_header.DataDirectory[10].VirtualAddress
# Parse Load Configuration Table if present
if load_config_directory_rva:
# Read LOAD CONFIG DIRECTORY size before parsing it
load_config_directory_size = read_module_memory( base + load_config_directory_rva, 0x4)
load_config_directory_size = unpack("I",load_config_directory_size)[0]
# Parse LOAD CONFIG DIRECTORY based on PE32 format
if self.optional_header.signature == self.MAGIC_PE32:
self.load_config_directory = IMAGE_LOAD_CONFIG_DIRECTORY.from_buffer_copy( read_module_memory( base + load_config_directory_rva, load_config_directory_size ))
# Parse LOAD CONFIG DIRECTORY based on PE32+ format
elif self.optional_header.signature == self.MAGIC_PE32_PLUS:
self.load_config_directory = IMAGE_LOAD_CONFIG_DIRECTORY64.from_buffer_copy( read_module_memory( base + load_config_directory_rva, load_config_directory_size ))
# Invalid PE header
else:
print "[idasploiter] Invalid IMAGE_OPTIONAL_HEADER magic: %x" % unpack("H",magic)[0]
return None
def isDynamicBase(self):
if self.optional_header:
if self.optional_header.DLLCharacteristics & self.IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE:
return "Yes"
else:
return "No"
else:
return "Unk"
def isNXCompat(self):
if self.optional_header:
if self.optional_header.DLLCharacteristics & self.IMAGE_DLL_CHARACTERISTICS_NX_COMPAT:
return "Yes"
else:
return "No"
else:
return "Unk"
def isSafeSEH(self):
if self.file_header and self.optional_header:
# SafeSEH is only applicable to 32-bit Windows.
if self.file_header.Machine != self.IMAGE_FILE_MACHINE_I386:
return "N/A"
# NO_SEH flag indicates that the module does not use SEH and no handlers can be called in this image.
elif self.optional_header.DLLCharacteristics & self.IMAGE_DLLCHARACTERISTICS_NO_SEH:
return "No SEH"
# SafeSEH is enabled when there is SEHandlerTable entry in the LOAD_CONFIG_DIRECTORY
elif self.load_config_directory and self.load_config_directory.SEHandlerTable != 0:
return "Yes"
# Otherwise SafeSEH is not used
else:
return "No"
else:
return "Unk"
def isGS(self):
if self.file_header and self.optional_header:
# NOTE: PE32+ does not necessarily provide a pointer to the Security Cookie in the
# LoadConfigDirectory. The address appears to be still present in the binary.
# TODO: Need to do additional research into reliably detecting /GS in PE32+ files.
# PE32+ binaries with missing LoadConfigDirectory may still used security cookies.
if self.arch64 and not self.load_config_directory:
return "Unk"
if self.load_config_directory and self.load_config_directory.SecurityCookie != 0:
# Attempt to read the security cookie
# NOTE: The extra exception handler was added to account for cookie being stored in memory locations
# that are not accessible by the debugger (e.g. manually loading win32k.sys, security cookie
# address will point to kernel space which we can't read)
try:
# Confirm the Security Cookie is not zero
if self.arch64:
cookie = read_module_memory(self.load_config_directory.SecurityCookie, 8)
cookie = struct.unpack("<Q", cookie)[0]
else:
cookie = read_module_memory(self.load_config_directory.SecurityCookie, 4)
cookie = struct.unpack("<I", cookie)[0]
if cookie != 0:
return "Yes"
else:
return "Null"
except Exception, e:
return "Inv"
else:
return "No"
else:
return "Unk"
###############################################################################
# Pointer Class - Manager writable pointer characteristics
class Ptr():
def __init__(self, module, ptr_ea, ptr_offset, ptr_charset, call_ea, insn_disas):
self.module = module
self.ptr_ea = ptr_ea
self.ptr_offset = ptr_offset
self.ptr_charset = ptr_charset
self.call_ea = call_ea
self.insn_disas = insn_disas
self.name = idaapi.get_name(self.call_ea + self.ptr_offset, self.ptr_ea) or ""
self.call_name = idaapi.get_func_name(self.call_ea) or ""
self.p2p_ea = None
self.p2p_offset = None
self.p2p_charset = None
###############################################################################
# Function Pointer Search Engine
class FuncPtr():
def __init__(self,sploiter):
self.sploiter = sploiter
self.ptr_calls = list()
self.ptrs = list()
def search_pointers(self):
# To be defined by parent classes.
pass
###############################################################################
# Gadget Class - manages ROP gadget characteristics
class Gadget():
def __init__(self, instructions, pivot, operations, chg_registers, use_registers):
self.address = 0x0
self.module = ""
self.instructions = instructions
self.size = len(instructions)
self.pivot = pivot
self.operations = operations
self.chg_registers = chg_registers
self.use_registers = use_registers
self.ptr_charset = []
###############################################################################
# ROP Search Engine
class Rop():
def __init__(self, sploiter):
self.maxRopOffset = 40 # Maximum offset from the return instruction to look for gadgets. default: 40
self.maxRopSize = 6 # Maximum number of instructions to look for gadgets. default: 6
self.maxRetnImm = 64 # Maximum imm16 value in retn. default: 64
self.maxJopImm = 255 # Maximum jop [reg + IMM] value. default: 64
self.maxRops = 0 # Maximum number of ROP chains to find. default: 0 (unlimited)
self.debug = False
self.regnames = idaapi.ph_get_regnames()
self.sploiter = sploiter
self.retns = list()
self.gadgets = list()
# Decoded instruction cache
self.insn_cache = dict()
def get_o_reg_name(self, insn, n):
# To be defined by parent classes.
return None
def search_retns(self):
# To be defined by parent classes.
pass
def search_gadgets(self):
# To be defined by parent classes.
pass
# Attempt to build a gadget at the provided start address
# by verifying it properly terminates at the expected RETN.
def build_gadget(self, ea, ea_end):
# To be defined by parent classes.
return None
###############################################################
# Decode instruction
def decode_instruction(self, insn, ea, ea_end):
# To be defined by parent classes.
pass
###############################################################################
# Sploiter Engine
class Sploiter():
def __init__(self):
# Process modules list
self.modules = list()
self.rop = None
self.funcptr = None
# Default patterns
self.c4_list = string.punctuation
self.c3_list = string.uppercase
self.c2_list = string.lowercase
self.c1_list = string.digits
# Check if processor supports 64-bit addressing
if idaapi.ph.flag & idaapi.PR_USE64:
self.addr64 = True
self.addr_format = "%016X"
self.pack_format_be = ">Q"
self.pack_format_le = "<Q"
else:
self.addr64 = False
self.addr_format = "%08X"
self.pack_format_be = ">I"
self.pack_format_le = "<I"
def get_func_ptr_instance(self):
# To be implemented by the plugin.
pass
def get_rop_instance(self):
# To be implemented by the plugin.
pass
def get_ptr_charset(self, ea):
ptr_charset = []
# Flags. True until proven otherwise.
nonull = True
unicode = True
ascii = True
asciiprint = True
alphanum = True
alpha = True
numeric = True
ptr_bytes = pack(self.pack_format_be, ea)
for i,b in enumerate( ptr_bytes ):
b_int = ord(b)
if b in self.ptrBadChars:
return None
# Locate any null bytes
if nonull and b == "\x00":
nonull = False
# Unicode compatible address must have
# null bytes at even points in the address
if unicode and not i % 2:
if not ptr_bytes[i:i+2] in ASCII_TO_UNICODE[ self.unicodeTable ]:
unicode = False
# Find any non-ascii characters
if ascii and not b_int > 127:
# Find any non-ascii printable characters
if asciiprint and not (b_int < 32 or b_int > 126):
# Find any non-alphanumeric characters
if alphanum and (b in string.ascii_letters or b in string.digits):
# Find any non-numeric characters
if numeric and not b in string.digits:
numeric = False
# Find any non-letter characters
if alpha and not b in string.ascii_letters:
alpha = False
else:
alphanum = False
numeric = False
alpha = False
else:
asciiprint = False
alphanum = False
numeric = False
alpha = False
else:
ascii = False
asciiprint = False
alphanum = False
numeric = False
alpha = False
# NOTE: You can continue to filter for upper/lower here if necessary
if nonull: ptr_charset.append("nonull")
if unicode: ptr_charset.append("unicode")
if ascii:
ptr_charset.append("ascii")
if asciiprint:
ptr_charset.append("asciiprint")
if alphanum:
ptr_charset.append("alphanum")
if numeric: ptr_charset.append("numeric")
if alpha: ptr_charset.append("alpha")
return ptr_charset
def process_modules(self):
# Reset modules list
self.modules = list()
# Check if the debugger is current active, if not add one module entry for the main executable.
if idaapi.dbg_can_query() and idaapi.get_process_state() < 0:
for m in idautils.Modules():
module = Module(m.name, m.size, m.base, m.rebase_to)
self.modules.append(module)
else:
# Get the IDA info struct.
info = idaapi.get_inf_structure()
# NOTE: There is no universal way in IDA SDK to get the base address/size of a loaded file because
# some of the loaders do not fill out the module information structure when loading the input file.
# To get around this I hacked in information that is both dynamically pulled from the database and
# "good enough", by using the maxEA and minEA of the loaded file. Since we are only searching the
# segments of the input file and they will all fall within this range this should be sufficient even
# though the size and base address fields are not accurate.
#
# I will buy beer for anyone who can come up with a universal way to get the base address and size
# of the loaded input file regardless of format or loader module used to load it. You also can't reparse
# the original file.
# Fake the module information.
module = Module(idaapi.get_root_filename(), info.maxEA - info.minEA, info.minEA, 0) # name, size, base, rebase_to
self.modules.append(module)
def show_modules_view(self):
mod = ModuleView(self)
mod.show()
def process_rop(self, select_list = None):
# Initialize ROP gadget search engine
self.rop = self.get_rop_instance()
if self.rop is None:
return
# Prompt user for ROP search settings
f = RopForm(self, select_list)
ok = f.Execute()
if ok == 1:
# Configure ROP gadget search engine
# Get selected modules
self.rop.modules = [self.modules[i] for i in f.mod.GetEmbSelection()]
if len(self.rop.modules) > 0:
# Pointer filters
self.rop.ptrNonull = f.cPtrNonull.checked
self.rop.ptrUnicode = f.cPtrUnicode.checked
self.rop.ptrAscii = f.cPtrAscii.checked
self.rop.ptrAsciiPrint = f.cPtrAsciiPrint.checked
self.rop.ptrAlphaNum = f.cPtrAlphaNum.checked
self.rop.ptrAlpha = f.cPtrAlpha.checked
self.rop.ptrNum = f.cPtrNum.checked
# Filter bad characters
buf = f.strBadChars.value
buf = buf.replace(' ','') # remove spaces
buf = buf.replace('\\x','') # remove '\x' prefixes
buf = buf.replace('0x','') # remove '0x' prefixes
try:
buf = binascii.unhexlify(buf) # convert to bytes
self.ptrBadChars = buf
except Exception, e:
idaapi.warning("Invalid input: %s" % e)
self.ptrBadChars = ""
# Ascii_to_Unicode_transformation table
# BUG: DropdownControl does not work on IDA 6.5
self.unicodeTable = f.radUnicode.value
# ROP instruction filters
self.rop.ropBadMnems = [mnem.strip().lower() for mnem in f.strBadMnems.value.split(',')]
self.rop.ropAllowJcc = f.cRopAllowJcc.checked
self.rop.ropNoBadBytes = f.cRopNoBadBytes.checked
# Get ROP engine settings
self.rop.maxRopSize = f.intMaxRopSize.value
self.rop.maxRopOffset = f.intMaxRopOffset.value
self.rop.maxRops = f.intMaxRops.value
self.rop.maxRetnImm = f.intMaxRetnImm.value
# Gadget search values
self.rop.searchRop = f.cRopSearch.checked
self.rop.searchJop = f.cJopSearch.checked
# Search for returns and ROP gadgets
self.rop.search_retns()
self.rop.search_gadgets()
# Show the ROP gadgets view
ropView = RopView(self)
ropView.show()
else:
idaapi.warning("No modules selected.")
f.Free()
def process_funcptr(self, select_list = None):
# Prompt user for function pointer search settings
f = PtrForm(self, select_list)
ok = f.Execute()
if ok == 1:
# Initialize Function Pointer search engine
self.funcptr = self.get_func_ptr_instance()
if self.funcptr is None:
return
# Configure Function Pointer search engine
# Get selected modules
self.funcptr.modules = [self.modules[i] for i in f.mod.GetEmbSelection()]
if len(self.funcptr.modules) > 0:
# Pointer filters
self.funcptr.ptrNonull = f.cPtrNonull.checked
self.funcptr.ptrUnicode = f.cPtrUnicode.checked
self.funcptr.ptrAscii = f.cPtrAscii.checked
self.funcptr.ptrAsciiPrint = f.cPtrAsciiPrint.checked
self.funcptr.ptrAlphaNum = f.cPtrAlphaNum.checked
self.funcptr.ptrAlpha = f.cPtrAlpha.checked
self.funcptr.ptrNum = f.cPtrNum.checked
# Apply pointer filters to the funcptr or ptr-to-ptr
if f.rFilterP2P.selected:
self.funcptr.filterP2P = True
else:
self.funcptr.filterP2P = False
# Ascii_to_Unicode_transformation table
# BUG: DropdownControl does not work on IDA 6.5
self.unicodeTable = f.radUnicode.value
# Filter bad characters
buf = f.strBadChars.value
buf = buf.replace(' ','') # remove spaces
buf = buf.replace('\\x','') # remove '\x' prefixes
buf = buf.replace('0x','') # remove '0x' prefixes
try:
buf = binascii.unhexlify(buf) # convert to bytes
self.ptrBadChars = buf
except Exception, e:
idaapi.warning("Invalid input: %s" % e)
self.ptrBadChars = ""
# Offsets
self.funcptr.ptrOffset = int(f.intPtrOffset.value, 16)
self.funcptr.p2pOffset = int(f.intP2POffset.value, 16)
# Pointer search engine settings
self.funcptr.searchP2P = not f.rSearchPtrOnly.checked
self.funcptr.maxPtrs = f.intMaxPtrs.value
# Search for writable pointers
self.funcptr.search_pointers()
# Show the writable pointers view
ptrView = PtrView(self)
ptrView.show()