This repository was archived by the owner on Jun 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVirtualTableDump.py
More file actions
828 lines (684 loc) · 27 KB
/
VirtualTableDump.py
File metadata and controls
828 lines (684 loc) · 27 KB
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
# Name: VirtualTableDump.py
# Version: 3.8.2
# Author: RenardDev (zeze839@gmail.com)
# IDA
import ida_ida
import ida_idaapi
import ida_name
import ida_bytes
import ida_pro
import ida_auto
import ida_search
import ida_hexrays
import idautils
import idaapi
import ida_nalt
import ida_funcs
import ida_typeinf
import idc
# Python
from pathlib import Path
import re
import os
DATABASE_FILE = Path(idc.get_idb_path())
DATABASE_DIRECTORY = DATABASE_FILE.parent
DATABASE_INFO = ida_idaapi.get_inf_structure()
MIN_EA = DATABASE_INFO.min_ea
MAX_EA = DATABASE_INFO.max_ea
FILE_TYPE = DATABASE_INFO.filetype
IS_64 = DATABASE_INFO.is_64bit()
if FILE_TYPE == idc.FT_PE:
IMAGEBASE = ida_nalt.get_imagebase()
elif (FILE_TYPE == idc.FT_ELF) | (FILE_TYPE == idc.FT_MACHO):
TYPE_INFO_ADDRESSES = [
ida_name.get_name_ea(ida_idaapi.BADADDR, '__ZTVN10__cxxabiv117__class_type_infoE'), # Single
ida_name.get_name_ea(ida_idaapi.BADADDR, '__ZTVN10__cxxabiv120__si_class_type_infoE'), # Single parent
ida_name.get_name_ea(ida_idaapi.BADADDR, '__ZTVN10__cxxabiv121__vmi_class_type_infoE') # Multiple parents
]
def IsNotValidAddress(address:int) -> bool:
if (address == 0) | (address == ida_idaapi.BADADDR) | (address >= MAX_EA) | (address < MIN_EA):
return True
return False
def FormatTypeName(type_name:str) -> str:
type_name = re.sub('`[^`\']*\'', '', type_name)
type_name = type_name.replace('::', '_')
type_name = type_name.replace('(', '_')
type_name = type_name.replace('<', '_')
type_name = type_name.replace(',', '_')
type_name = type_name.replace('>', '_')
type_name = type_name.replace('&', '_')
type_name = type_name.replace('?', '_')
type_name = type_name.replace('@', '_')
type_name = type_name.replace(')', '_')
type_name = re.sub('\\s+', '', type_name)
type_name = re.sub('\\(.*\\*\\)\\(.*\\)', '', type_name)
type_name = type_name.replace('*', '')
type_name = re.sub('\\_+', '_', type_name)
type_name = type_name.removeprefix('__')
type_name = type_name.removeprefix('_')
type_name = type_name.removesuffix('__')
type_name = type_name.removesuffix('_')
return type_name
def FixDubTypes(types, known_types = None):
if known_types == None:
known_types = []
new_types = []
for type, name, bases in types:
if bases:
FixDubTypes(bases, known_types)
is_dup = False
for known_type, known_name, known_bases in known_types:
if known_type == type:
is_dup = True
break
if is_dup != True:
new_types.append((type, name, bases))
known_types.append((type, name, bases))
return new_types
def SearchBaseTypes(search_type:int, main_type:int = 0):
types = []
if main_type != 0:
if main_type == search_type:
return types
if FILE_TYPE == idc.FT_PE:
for search_type_ref in idautils.XrefsTo(search_type):
search_type_ref_address = search_type_ref.frm
# TODO: Checking for valid record
signature = ida_bytes.get_dword(search_type_ref_address - 12)
attributes = ida_bytes.get_dword(search_type_ref_address + 20)
if ((signature == 0) | (signature == 1)) & (attributes > 0x40):
continue
number_of_bases = ida_bytes.get_dword(search_type_ref_address + 4)
if number_of_bases == 0:
continue
if IS_64:
chd = IMAGEBASE + ida_bytes.get_dword(search_type_ref_address + 24)
else:
chd = ida_bytes.get_dword(search_type_ref_address + 24)
if IsNotValidAddress(chd):
continue
number_of_items = ida_bytes.get_dword(chd + 8)
if (number_of_items == 0) | (number_of_items == 1):
continue
if IS_64:
array_of_bases = IMAGEBASE + ida_bytes.get_dword(chd + 12)
else:
array_of_bases = ida_bytes.get_dword(chd + 12)
if IsNotValidAddress(array_of_bases):
continue
for i in range(number_of_items - number_of_bases, number_of_items):
if IS_64:
base_bcd = IMAGEBASE + ida_bytes.get_dword(array_of_bases + 4 * i)
else:
base_bcd = ida_bytes.get_dword(array_of_bases + 4 * i)
if IsNotValidAddress(base_bcd):
continue
if IS_64:
base_type = IMAGEBASE + ida_bytes.get_dword(base_bcd)
else:
base_type = ida_bytes.get_dword(base_bcd)
if IsNotValidAddress(base_type):
continue
if base_type == search_type:
continue
if main_type != 0:
if base_type == main_type:
continue
type_name = FormatTypeName(ida_name.get_demangled_name(base_type, ida_name.M_PRCMSK | ida_name.MT_LOCALNAME, 0, ida_name.GN_DEMANGLED | ida_name.GN_STRICT | ida_name.GN_LOCAL))
types.append((base_type, type_name, SearchBaseTypes(base_type, search_type)))
elif (FILE_TYPE == idc.FT_ELF) | (FILE_TYPE == idc.FT_MACHO):
# if IS_64:
# type_base = ida_bytes.get_qword(search_type)
# else:
# type_base = ida_bytes.get_dword(search_type)
type_base = ida_name.get_name_base_ea(search_type, 0)
if type_base == TYPE_INFO_ADDRESSES[0]: # Single
if main_type != 0:
type_name = FormatTypeName(ida_name.get_demangled_name(search_type, ida_name.M_PRCMSK | ida_name.MT_LOCALNAME, 0, ida_name.GN_DEMANGLED | ida_name.GN_STRICT | ida_name.GN_LOCAL))
types.append((search_type, type_name, []))
elif type_base == TYPE_INFO_ADDRESSES[1]: # Parent-Child
if IS_64:
type_parent = ida_bytes.get_qword(search_type + 16)
else:
type_parent = ida_bytes.get_dword(search_type + 8)
type_of_parent = ida_name.get_name_base_ea(type_parent, 0)
if type_of_parent == TYPE_INFO_ADDRESSES[0]: # Single
type_name = FormatTypeName(ida_name.get_demangled_name(type_parent, ida_name.M_PRCMSK | ida_name.MT_LOCALNAME, 0, ida_name.GN_DEMANGLED | ida_name.GN_STRICT | ida_name.GN_LOCAL))
types.append((type_parent, type_name, []))
else:
type_name = FormatTypeName(ida_name.get_demangled_name(type_parent, ida_name.M_PRCMSK | ida_name.MT_LOCALNAME, 0, ida_name.GN_DEMANGLED | ida_name.GN_STRICT | ida_name.GN_LOCAL))
types.append((type_parent, type_name, SearchBaseTypes(type_parent, search_type)))
elif type_base == TYPE_INFO_ADDRESSES[2]: # Multiple
if IS_64:
count_of_base_types = ida_bytes.get_qword(search_type + 24)
else:
count_of_base_types = ida_bytes.get_dword(search_type + 12)
for i in range(count_of_base_types):
if IS_64:
base_type = ida_bytes.get_qword(search_type + 32 + 16 * i)
else:
base_type = ida_bytes.get_dword(search_type + 16 + 8 * i)
if base_type == search_type:
continue
if main_type != 0:
if base_type == main_type:
continue
type_of_base = ida_name.get_name_base_ea(base_type, 0)
if type_of_base == TYPE_INFO_ADDRESSES[0]: # Single
type_name = FormatTypeName(ida_name.get_demangled_name(base_type, ida_name.M_PRCMSK | ida_name.MT_LOCALNAME, 0, ida_name.GN_DEMANGLED | ida_name.GN_STRICT | ida_name.GN_LOCAL))
types.append((base_type, type_name, []))
else:
type_name = FormatTypeName(ida_name.get_demangled_name(base_type, ida_name.M_PRCMSK | ida_name.MT_LOCALNAME, 0, ida_name.GN_DEMANGLED | ida_name.GN_STRICT | ida_name.GN_LOCAL))
types.append((base_type, type_name, SearchBaseTypes(base_type, search_type)))
if FILE_TYPE == idc.FT_PE:
return FixDubTypes(types)
elif (FILE_TYPE == idc.FT_ELF) | (FILE_TYPE == idc.FT_MACHO):
return types
def ReConstructTypes(types, known_types = None):
if known_types == None:
known_types = []
new_types = []
for type, name, bases in types:
if bases:
new_types.extend(ReConstructTypes(bases, known_types))
is_dup = False
for known_type, known_name, known_bases in known_types:
if known_type == type:
is_dup = True
break
if is_dup != True:
new_types.append((type, name, bases))
known_types.append((type, name, bases))
return new_types
def SearchTypes():
types = []
if FILE_TYPE == idc.FT_PE:
type_addresses = []
begin_address = MIN_EA
while True:
address = ida_search.find_binary(begin_address, MAX_EA, '2E 3F 41 56 74 79 70 65 5F 69 6E 66 6F 40 40 00', 0, ida_search.SEARCH_DOWN)
if IsNotValidAddress(address):
break
begin_address = address + 16
if IS_64:
type_address = ida_bytes.get_qword(address - 16)
spare = ida_bytes.get_qword(address - 8)
else:
type_address = ida_bytes.get_dword(address - 8)
spare = ida_bytes.get_dword(address - 4)
if IsNotValidAddress(type_address) | (spare != 0):
continue
type_addresses.append(type_address)
for type_address in type_addresses:
for based_type in idautils.XrefsTo(type_address):
based_type_address = based_type.frm
if IS_64:
based_spare = ida_bytes.get_qword(based_type_address + 8)
else:
based_spare = ida_bytes.get_dword(based_type_address + 4)
if based_spare != 0:
continue
based_name = FormatTypeName(ida_name.get_demangled_name(based_type_address, ida_name.M_PRCMSK | ida_name.MT_LOCALNAME, 0, ida_name.GN_DEMANGLED | ida_name.GN_STRICT | ida_name.GN_LOCAL))
types.append((based_type_address, based_name, SearchBaseTypes(based_type_address)))
elif (FILE_TYPE == idc.FT_ELF) | (FILE_TYPE == idc.FT_MACHO):
for type_info_address in TYPE_INFO_ADDRESSES:
if IsNotValidAddress(type_info_address):
continue
for based_type_info in idautils.XrefsTo(type_info_address):
based_type_info_address = based_type_info.frm
if IS_64:
raw_based_type_name = ida_bytes.get_qword(based_type_info_address + 8)
else:
raw_based_type_name = ida_bytes.get_dword(based_type_info_address + 4)
if IsNotValidAddress(raw_based_type_name):
continue
based_type_name = FormatTypeName(ida_name.get_demangled_name(based_type_info_address, ida_name.M_PRCMSK | ida_name.MT_LOCALNAME, 0, ida_name.GN_DEMANGLED | ida_name.GN_STRICT | ida_name.GN_LOCAL))
types.append((based_type_info_address, based_type_name, SearchBaseTypes(based_type_info_address)))
return ReConstructTypes(types)
def FindVirtualTableFunctions(name, table):
functions = []
offset = 0
while True:
if IS_64:
address = ida_bytes.get_qword(table + offset)
offset += 8
else:
address = ida_bytes.get_dword(table + offset)
offset += 4
if IsNotValidAddress(address) | ida_bytes.is_data(ida_bytes.get_flags(address)):
break
func = ida_funcs.get_func(address)
if func:
if ida_funcs.is_func_entry(func) != True:
break
else:
break
try:
decompiled_function = ida_hexrays.decompile(address)
if decompiled_function:
functions.append(address)
except ida_hexrays.DecompilationFailure:
break
return functions
def SearchVirtualTables(types, result = None):
if result == None:
result = []
if FILE_TYPE == idc.FT_PE:
for type, name, bases in types:
if bases:
SearchVirtualTables(bases, result)
if IsNotValidAddress(type):
continue
exist = False
for result_type, result_name, result_table, result_functions in result:
if result_type == type:
exist = True
break
if exist:
continue
for search_type_ref in idautils.XrefsTo(type):
search_type_ref_address = search_type_ref.frm
complete_object = search_type_ref_address - 12
# TODO: Checking for valid record
signature = ida_bytes.get_dword(complete_object)
if (signature != 0) & (signature != 1):
continue
if IS_64:
if IMAGEBASE + ida_bytes.get_dword(complete_object + 20) != complete_object:
continue
for type_ref in idautils.XrefsTo(complete_object):
type_ref_address = type_ref.frm
if IS_64:
table = type_ref_address + 8
else:
table = type_ref_address + 4
functions = FindVirtualTableFunctions(name, table)
if functions:
result.append((type, name, table, functions))
elif (FILE_TYPE == idc.FT_ELF) | (FILE_TYPE == idc.FT_MACHO):
for type, name, bases in types:
if bases:
SearchVirtualTables(bases, result)
if IsNotValidAddress(type):
continue
exist = False
for result_type, result_name, result_table, result_functions in result:
if result_type == type:
exist = True
break
if exist:
continue
for type_ref in idautils.XrefsTo(type):
type_ref_address = type_ref.frm
if IS_64:
offset_to_this = ida_bytes.get_qword(type_ref_address - 8)
else:
offset_to_this = ida_bytes.get_dword(type_ref_address - 4)
if offset_to_this != 0:
continue
if IS_64:
table = type_ref_address + 8
else:
table = type_ref_address + 4
functions = FindVirtualTableFunctions(name, table)
if functions:
result.append((type, name, table, functions))
return result
def IsKnownType(string):
tif = ida_typeinf.tinfo_t()
if ida_typeinf.parse_decl(tif, None, string + ';', ida_typeinf.PT_SIL) is None:
return (False, '')
while tif.is_ptr():
tif = tif.get_pointed_object()
tif.clr_const_volatile()
if tif.is_arithmetic() | tif.is_sse_type() | tif.is_func() | tif.is_void():
return (False, '')
return (True, tif.dstr())
def GetTypeString(type):
tif = ida_typeinf.tinfo_t()
if ida_typeinf.parse_decl(tif, None, type.dstr() + ';', ida_typeinf.PT_SIL) is None:
return (False, '')
tif.clr_const_volatile()
return (True, tif.dstr())
def GetEndType(tif):
while tif.is_ptr():
tif = tif.get_pointed_object()
return tif
def DecompileVirtualTablesFunctions(tables, declare, include):
decompiled = []
known_functions = []
for table_type, table_name, table_address, table_functions in tables:
unk_functions = 1
sub_functions = 1
pure_functions = 1
functions = []
for function in table_functions:
try:
decompiled_function = ida_hexrays.decompile(function)
if decompiled_function:
#calling_convention = ''
#function_type_data = idaapi.func_type_data_t()
#if decompiled_function.type.get_func_details(function_type_data):
# if function_type_data.cc == idaapi.CM_CC_CDECL:
# calling_convention = '__cdecl'
# elif function_type_data.cc == idaapi.CM_CC_STDCALL:
# calling_convention = '__stdcall'
# elif function_type_data.cc == idaapi.CM_CC_THISCALL:
# calling_convention = '__fastcall'
# elif function_type_data.cc == idaapi.CM_CC_FASTCALL:
# calling_convention = '__fastcall'
return_type = decompiled_function.type.get_rettype()
status, string = GetTypeString(return_type)
if status & (string != '?'):
return_type = string
else:
return_type = return_type.dstr()
args = [ decompiled_function.type.get_nth_arg(x) for x in range(1, decompiled_function.type.get_nargs()) ]
end_return_type = GetEndType(decompiled_function.type.get_rettype())
if end_return_type.is_func() | (end_return_type.is_int128() & decompiled_function.type.get_rettype().is_ptr()):
return_type = 'void*'
elif end_return_type.is_sse_type():
return_type = 'void*'
return_type = '*'.join(return_type.rsplit(' *'))
if (return_type == '_BOOL4') | (return_type == 'BOOL'):
return_type = 'bool'
elif return_type == '_DWORD*':
return_type = 'void*'
elif return_type == '_BYTE*':
return_type = 'unsigned char*'
elif return_type == '_QWORD*':
return_type = 'unsigned long long*'
elif return_type == '_DWORD':
return_type = 'unsigned int'
return_type = return_type.replace('::', '_').replace('class ', '').replace('struct ', '')
return_type_str = return_type.replace(' ', '').rsplit('&')[0].rsplit('*')[0].strip()
is_known = False
for known_table_type, known_table_name, known_table_address, known_table_functions in tables:
if known_table_name == return_type_str:
is_known = True
break
if is_known != True:
status, string = IsKnownType(decompiled_function.type.get_rettype().dstr().replace('::', '_').replace('class ', '').replace('struct ', ''))
if status & (string != '?'):
if string not in declare:
declare.append(string)
else:
if return_type_str not in include:
include.append(return_type_str)
function_name = ida_name.get_demangled_name(function, ida_name.M_PRCMSK | ida_name.MT_LOCALNAME, 0, ida_name.GN_DEMANGLED | ida_name.GN_STRICT | ida_name.GN_LOCAL)
function_name_parts = function_name.rsplit('::')
if function_name_parts:
function_name = function_name_parts[-1]
if function_name == '':
function_name = f'NonNamed_{unk_functions}'
unk_functions += 1
if (function_name == '`scalar deleting destructor\'') | (function_name == '`vector deleting destructor\''):
function_string = f'virtual ~{table_name}() = 0;'
#function_string = f'virtual void deconstructor_{table_name}() = 0;'
found_dup = 0
for func, func_str, func_name, func_ret, func_args, real_funcname in functions:
if func_str == function_string:
found_dup += 1
if found_dup:
function_string = f'virtual ~{function_name}{found_dup + 1}() = 0;'
#function_string = f'virtual void deconstructor_{function_name}{found_dup + 1}() = 0;'
continue # Not exist 2nd dcotr for Windows
functions.append((function, function_string, function_name, '', [], function_name))
continue
elif (function_name[0] == '~') | (function_name_parts[0] == function_name_parts[-1][1:]):
function_string = f'virtual ~{table_name}() = 0;'
#function_string = f'virtual void deconstructor_{table_name}() = 0;'
found_dup = 0
for func, func_str, func_name, func_ret, func_args, real_funcname in functions:
if func_str == function_string:
found_dup += 1
if found_dup:
function_string = f'virtual ~{table_name}{found_dup + 1}() = 0;'
#function_string = f'virtual void deconstructor_{table_name}{found_dup + 1}() = 0;'
continue # Not exist 2nd dcotr for Windows
functions.append((function, function_string, function_name, '', [], function_name))
continue
elif function_name[:4] == 'sub_':
return_type = 'void'
function_name = f'NonAssociated_{sub_functions}'
function_type_args = ''
sub_functions += 1
elif function_name == '__purecall':
return_type = 'void'
function_name = f'PureCall_{pure_functions}'
function_type_args = ''
pure_functions += 1
function_type = ida_name.get_demangled_name(function, 0, 0, ida_name.GN_DEMANGLED | ida_name.GN_STRICT)
if (function_name[:13] != 'NonAssociated') & (function_name[:8] != 'PureCall'):
function_type_args = re.search(r'\((.*)\)', function_type)
if function_type_args:
function_type_args = function_type_args.group(1)
if function_type_args == 'void':
function_type_args = ''
else:
function_type_args = '**'.join(function_type_args.rsplit(' **'))
function_type_args = '*'.join(function_type_args.rsplit(' *'))
function_type_args = '&'.join(function_type_args.rsplit(' &'))
function_type_args = ', '.join(function_type_args.rsplit(','))
else:
function_type_args = ''
function_type_args = function_type_args.replace('::', '_')
found_dup = 0
for func, func_str, func_name, func_ret, func_args, real_funcname in functions:
if func_name == function_name:
found_dup += 1
#if calling_convention != '__fastcall':
# function_string = f'virtual {return_type} {calling_convention} {function_name}('
#else:
# function_string = f'virtual {return_type} {function_name}('
if found_dup:
function_name_new = f'{function_name}{found_dup + 1}'
else:
function_name_new = function_name
function_string = f'virtual {return_type} {function_name_new}('
func_args = []
#if function_type_args:
if FILE_TYPE == idc.FT_PE and function_type_args:
function_string += function_type_args + ') = 0;'
else:
for i, arg in enumerate(args):
status, string = GetTypeString(arg)
if status & (string != '?'):
arg_type = string
else:
arg_type = arg.dstr()
end_arg_type = GetEndType(arg)
if end_arg_type.is_func() | ((end_arg_type.is_int128() | end_arg_type.is_sse_type()) & arg.is_ptr()):
arg_type = 'void*'
elif end_arg_type.is_sse_type():
arg_type = 'void*'
arg_type = '*'.join(arg_type.rsplit(' *'))
if (arg_type == '_BOOL4') | (arg_type == 'BOOL'):
arg_type = 'bool'
elif arg_type == '_DWORD*':
arg_type = 'void*'
elif arg_type == '_BYTE*':
arg_type = 'unsigned char*'
elif arg_type == '_QWORD*':
arg_type = 'unsigned long long*'
elif arg_type == '_DWORD':
arg_type = 'unsigned int'
arg_type = arg_type.replace('::', '_').replace('class ', '').replace('struct ', '')
arg_type_str = arg_type.replace('const ', '').replace('volatile ', '').replace(' ', '').rsplit('&')[0].rsplit('*')[0].strip()
is_known = False
for known_table_type, known_table_name, known_table_address, known_table_functions in tables:
if known_table_name == arg_type_str:
is_known = True
break
if is_known != True:
status, string = IsKnownType(arg.dstr().replace('::', '_').replace('class ', '').replace('struct ', ''))
if status & (string != '?'):
if string not in declare:
declare.append(string)
else:
narg = arg
while narg.is_ptr():
narg = narg.get_pointed_object()
if (narg.is_arithmetic() | narg.is_sse_type() | narg.is_func() | narg.is_void()) != True:
narg = narg.dstr().replace('::', '_').replace('class ', '').replace('struct ', '').replace('const ', '').replace('volatile ', '').replace(' ', '').rsplit('&')[0].rsplit('*')[0].strip()
if narg not in declare:
declare.append(narg)
else:
if arg_type_str not in include:
include.append(arg_type_str)
if i >= len(args) - 1:
function_string += f'{arg_type}'
else:
function_string += f'{arg_type}, '
func_args.append(arg_type)
function_string += ') = 0;'
functions.append((function, function_string, function_name, return_type, func_args, function_name_new))
is_exist = False
for rettype, name, args in known_functions:
if name == function_name:
is_exist = True
break
if is_exist != True:
known_functions.append((return_type, function_name, func_args))
except ida_hexrays.DecompilationFailure:
functions.append((function, f'virtual void FailedToDecompile_{function_name}() = 0;', function_name, return_type, []))
if functions:
for index, (func, string, name, rettype, args, realname) in enumerate(functions):
for known_rettype, known_name, known_args in known_functions:
if name == known_name:
if known_rettype != rettype:
args_string = ', '.join(args)
functions[index] = (func, f'virtual {known_rettype} {realname}({args_string}) = 0; // Fixed return type', name, known_rettype, args, realname)
break
decompiled.append((table_type, table_name, table_address, functions))
return decompiled
def GetBasesNames(bases, names = None):
if names == None:
names = set()
for type, name, base_types in bases:
# if base_types:
# GetBasesNames(base_types, names)
names.add(name)
return names
def PrintBaseTypes(types, index = 0):
for type, name, base_types in types:
print(' ' * (index + 1) + f' > {name}')
if base_types:
PrintBaseTypes(base_types, index + 1)
class VirtualTableDump(ida_idaapi.plugin_t):
flags = ida_idaapi.PLUGIN_MOD
wanted_name = 'VirtualTableDump'
comment = 'VirtualTableDump - Dumper for all VTables.\n'
help = ''
def init(self):
if ida_pro.IDA_SDK_VERSION < 770:
idc.msg('[VirtualTableDump] Error: Optimal IDA version is 7.7.\n')
return ida_idaapi.PLUGIN_SKIP
return ida_idaapi.PLUGIN_KEEP
def term(self):
pass
def run(self, arg = None):
if ida_auto.auto_is_ok() != True:
idc.msg('[VirtualTableDump] Error: The analysis is not finished!\n')
return
if ida_hexrays.init_hexrays_plugin() != True:
idc.msg('[VirtualTableDump] Error: Failed to initialize hexrays plugin!\n')
return
if (FILE_TYPE != idc.FT_PE) & (FILE_TYPE != idc.FT_ELF) & (FILE_TYPE != idc.FT_MACHO):
idc.msg('[VirtualTableDump] Error: This file type is not supported!\n')
return
found_types = SearchTypes()
idc.msg(f'[VirtualTableDump] Info: Found {len(found_types)} types.\n')
if found_types:
tables = SearchVirtualTables(found_types)
idc.msg(f'[VirtualTableDump] Info: Found {len(tables)} virtual tables.\n')
if tables:
declare = []
include = []
tables = DecompileVirtualTablesFunctions(tables, declare, include)
idc.msg(f'[VirtualTableDump] Info: Decompiled {len(tables)} virtual tables.\n')
if tables:
known_bases = []
known_tables = []
code_bases = '\n'
code = ''
if include:
for inc in include:
code_bases += f'class {inc};\n'
code_bases += '\n'
if declare:
for decl in declare:
code_bases += f'class {decl} ' + '{};\n'
code_bases += '\n'
for type, name, base_types in found_types:
for table_type, table_name, table_address, table_functions in tables:
if table_name in known_tables:
continue
if type == table_type:
bases = GetBasesNames(base_types)
if bases:
bases_public = []
for base in bases:
base_is_known = False
for known_base in known_bases:
if base == known_base:
base_is_known = True
break
if base_is_known != True:
for known_base in include:
if base == known_base:
base_is_known = True
break
if base_is_known != True:
for known_base in declare:
if base == known_base:
base_is_known = True
break
known_bases.append(base)
found_base = False
for known_type, known_name, known_address, known_functions in tables:
if base == known_name:
if known_functions:
found_base = True
break
if base_is_known != True:
if found_base != True:
code_bases += f'class {base}' + ' {};\n'
else:
code_bases += f'class {base}; // Unexpected\n'
bases_public.append(f'public {base}')
bases_public = ', '.join(bases_public)
code += f'class {table_name} : {bases_public} ' + '{\n'
else:
code += f'class {table_name} ' + '{\n'
code += 'public:\n'
known_tables.append(table_name)
for function, function_name, real_function_name, func_ret, func_args, real_funcname in table_functions:
code += '\t' + function_name + '\n'
code += '};\n\n'
file_path = DATABASE_FILE.parent.joinpath(DATABASE_FILE.stem + '.h').__str__()
with open(file_path, 'w+') as f:
f.write(code_bases + '\n' + code)
f.close()
idc.msg(f'[VirtualTableDump] Info: Dumped in `{file_path}`.\n')
# if tables:
# for table_type, table_name, table_address, table_functions in tables:
# if len(table_functions) > 0:
# print(f'> {table_name}')
# for function in table_functions:
# print(f' > {function}')
_VirtualTableDump = None
bPluginMode = False
def PLUGIN_ENTRY():
global _VirtualTableDump
global bPluginMode
if _VirtualTableDump == None:
_VirtualTableDump = VirtualTableDump()
bPluginMode = True
return _VirtualTableDump
if __name__ == '__main__':
if bPluginMode != True:
if ida_pro.IDA_SDK_VERSION < 770:
idc.msg('[VirtualTableDump] Error: Optimal IDA version is 7.7\n')
else:
VirtualTableDump().run()