-
Notifications
You must be signed in to change notification settings - Fork 54
/
FunctionProfiler.py
75 lines (58 loc) · 2.39 KB
/
FunctionProfiler.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
# Find all cross references in the current function.
#@author fuzzywalls
#@category TNS
#@menupath TNS.Function Profiler
from utils import utils
from ghidra.program.model.symbol import RefType,SymbolType
class CrossRef(object):
def __init__(self, reference):
to_addr = reference.getToAddress()
symbol = getSymbolAt(to_addr)
self.from_addr = reference.getFromAddress()
self.symbol_type = str(symbol.getSymbolType())
symbol_object = symbol.getObject()
try:
if symbol_object.hasStringValue():
symbol_name = str(symbol.getObject())
if symbol_name.startswith('ds '):
self.symbol_name = symbol_name[3:]
self.symbol_type = 'String'
else:
self.symbol_name = symbol.getName()
except AttributeError:
self.symbol_name = symbol.getName()
def __str__(self):
return '{:12} {}'.format(self.from_addr, self.symbol_name)
class FunctionCrossReferences(object):
def __init__(self, function):
self.function = function
self.cross_references = []
def append(self, cross_ref):
self.cross_references.append(cross_ref)
def _loop_print(self, label):
print '\n{}\n{}'.format(label, '-' * len(label))
for cr in self.cross_references:
if cr.symbol_type == label:
print cr
def pretty_print(self):
print '\nCross References in {}\n{}'.format(function, '-' * 30)
self._loop_print('String')
self._loop_print('Function')
self._loop_print('Label')
print
code_manager = currentProgram.getCodeManager()
function_manager = currentProgram.getFunctionManager()
function = utils.get_function(function_manager, currentLocation.address)
if function is None:
print 'Current selection is not a function.'
exit()
cross_references = FunctionCrossReferences(function)
instructions = utils.get_instruction_list(code_manager, function)
for instruction in instructions:
for reference in instruction.getReferencesFrom():
ref_type = reference.getReferenceType()
if reference.isMemoryReference() and \
ref_type != RefType.CONDITIONAL_JUMP and \
ref_type != RefType.UNCONDITIONAL_JUMP:
cross_references.append(CrossRef(reference))
cross_references.pretty_print()