-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStartInspection.py
87 lines (72 loc) · 3.35 KB
/
StartInspection.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
import cProfile
import pprint
import binaryninja as bn
from .RttiInformation.ClassContext import GlobalClassContextManager
from .Common import Utils
from . import Config
from .RttiInformation import TypeCreation
from .ClassDataStructureDetection.Constructors import DetectConstructor
from .RttiInformation.VirtualTableInference import VirtualFunctionTable
def is_bv_valid_for_plugin(bv: bn.BinaryView) -> bool:
if bv.arch.name == "x86_64" or bv.arch.name == "x86":
return True
else:
print(f'ClassyPP: Executable CPU Arch is: {bv.arch.name}. This plugin supports only x86 32/64 bit executables.')
return False
def GetUserInputs() -> bool:
if Config.ENABLE_LOGGING or Config.ENABLE_DEBUG_LOGGING:
Utils.logging_file = Utils.GetLogfileHandle()
choice = bn.interaction.ChoiceField("",
["Add comment for function", "Change name of function",
"Do not detect constructors"])
bn.interaction.get_form_input([choice], "Constructor functions handling mode")
Config.CONSTRUCTOR_FUNCTION_HANDLING = choice.result
return True
def CleanupPlugin():
if Config.ENABLE_LOGGING or Config.ENABLE_DEBUG_LOGGING:
Utils.logging_file.close()
class InspectInBackground(bn.BackgroundTaskThread):
def __init__(self, bv: bn.BinaryView):
bn.BackgroundTaskThread.__init__(
self, "ClassyPP - Performing inspection and extraction...", True)
self.bv = bv
def run(self):
try:
if GetUserInputs():
self.bv.begin_undo_actions()
if Config.ENABLE_LOGGING or Config.ENABLE_DEBUG_LOGGING:
Utils.logging_file = Utils.GetLogfileHandle()
self.RTTI_inspection()
self.DetectAndVerifyConstructor()
self.bv.update_analysis_and_wait()
except KeyboardInterrupt:
Utils.LogToFile('Cancelled by user request')
print('Cancelled by user request')
self.bv.commit_undo_actions()
CleanupPlugin()
def DetectAndVerifyConstructor(self):
if Config.CONSTRUCTOR_FUNCTION_HANDLING != 2:
# Iterate over all found vfTables and detect their constructors
print(f'ClassyPP: Constructor Detection process started...')
Utils.LogToFile(str(VirtualFunctionTable.global_vfTables))
VirtualFunctionTable.DetectVTables(self.bv, self)
def RTTI_inspection(self) -> bool:
Utils.LogToFile(f'inspect: Starting Scan.')
if TypeCreation.CreateTypes(self.bv):
GCM: GlobalClassContextManager = GlobalClassContextManager(
self.bv, self)
if GCM.DetectAndDefineAllInformation():
Utils.LogToFile(f'ClassyPP: Successfully created types.')
print(f'ClassyPP: Successfully defined RTTI Information.')
return True
else:
Utils.LogToFile(f'ClassyPP: Failed to create RTTI classes.')
else:
Utils.LogToFile(f'ClassyPP: Failed to create types.')
return False
def inspect(bv: bn.BinaryView):
if bv.analysis_info.state != 2:
print(f'ClassyPP: Binja analysis still ongoing, please run this plugin only after analysis completes.')
else:
background_thread = InspectInBackground(bv)
background_thread.start()