-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab4_3.py
67 lines (53 loc) · 2.11 KB
/
Lab4_3.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
## Iterate through instructions
## Make a comment for all "cmp" instrructions
## Partially from http://www.openrce.org/articles/full_view/11
from sets import Set
ea = ScreenEA()
callers = dict()
callees = dict()
mnemonics = dict()
FunctionDict = dict()
def ParseFunction():
for seg in Segments():
for head in range(seg, SegEnd(seg)):
if isCode(GetFlags(head)):
mnem = GetMnem(head)
if mnem == 'call':
Name = GetFunctionName(head)
Opnd = GetOpnd(head, 0)
if (Opnd.find("strcpy") > -1 or Opnd.find("sprintf") > -1 or Opnd.find("strncpy") > -1 or Opnd.find("wcsncpy") > -1 or Opnd.find("swprintf") > -1 ):
print Name, ':', hex(head), ':', Opnd
def ParseFunctions(fun):
if fun in FunctionDict.keys():
return
f_name = GetFunctionName(fun)
callers[f_name] = Set(map(GetFunctionName, CodeRefsTo(fun, 0)))
for ref in CodeRefsTo(fun, 0):
caller_name = GetFunctionName(ref)
callees[caller_name] = callees.get(caller_name, Set())
callees[caller_name].add(f_name)
mnem = GetMnem(fun)
FunctionDict[fun] = 1
mnemonics[mnem] = mnemonics.get(mnem, 0)+1
MakeComm(fun, "call instruction")
SetColor(fun, CIC_ITEM, 0xF0F02F)
return
# For each of the segments
#for fun in Functions(SegStart(ea), SegEnd(ea)):
# ParseFunctions(fun)
#for seg in Segments():
# for head in Heads(seg, SegEnd(seg)):
# ParseFunctions(head)
ParseFunction()
# Get the list of all functions
#functions = Set(callees.keys()+callers.keys())
# For each of the functions, print the number of functions calling it and
# number of functions being called. In short, indegree and outdegree
#for f in functions:
#print 'function[ %s ]:called[%d]:calls[%d]' % (f, len(callers.get(f, [])), len(callees.get(f, [])))
# Sort the mnemonics by number of occurrences
#sorted = map(lambda x:(x[1], x[0]), mnemonics.items())
#sorted.sort()
# Print the sorted list
#for mnemonic, count in sorted:
#print mnemonic, count