Newer
Older
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
import idaapi
import idc
import os
import sys
import json
import jsonpickle
class Function:
def __init__(self, name, startAddr, endAddr, instruction_number):
self.name = name
self.startAddr = startAddr
self.endAddr = endAddr
self.basicBlocks = []
self.arguments = []
self.instruction_number = instruction_number
def addBB(self, BB):
self.basicBlocks.append(BB)
def addArg(self, arg):
self.arguments.append(arg)
class BasicBlock:
def __init__(self, startAddr, endAddr):
self.startAddr = startAddr
self.endAddr = endAddr
class Argument:
def __init__(self, location=None):
self.location = location
functions = []
start = SegStart(BeginEA())
for function_start in Functions(SegStart(start), SegEnd(start)):
# Count the number of instructions
instruction_counter = 0
for i in FuncItems(function_start):
instruction_counter += 1
f = idaapi.FlowChart(idaapi.get_func(function_start))
curFunc = Function(GetFunctionName(function_start), "0x%08x"%f[0].startEA, "0x%08x"%FindFuncEnd(function_start), instruction_counter)
visited = set()
visited.add(f[0].startEA)
wl = [f[0]]
while len(wl) > 0:
bb = wl.pop()
start = "0x%08x"%bb.startEA
end = "0x%08x"%bb.endEA
block = BasicBlock(start, end)
curFunc.basicBlocks.append(block)
for successor in bb.succs():
if successor.startEA not in visited:
visited.add(successor.startEA)
wl.append(successor)
functions.append(curFunc)
with open(idc.ARGV[1], "w") as output:
json.dump(json.loads(jsonpickle.encode(functions, unpicklable=False)), output, indent=4)
idc.Exit(0)