-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
libgraph.py
66 lines (50 loc) · 1.22 KB
/
libgraph.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
import r2pipe
import sys
import os
r2 = r2pipe.open("/bin/ls")
libpath = ["", ".", "/lib", "/usr/lib"]
output = "aa"
# output = 'dot'
done = {}
def findlib(lib):
if os.path.isfile(lib):
return lib
for a in libpath:
if os.path.isfile("%s/%s" % (a, lib)):
return "%s/%s" % (a, lib)
return []
def getlibs(lib):
return r2.syscmdj("rabin2 -lj %s" % (lib))["libs"]
def filter(s):
return s.replace("-", "_").replace("+", "x")
def makeNode(name):
r2.cmd("agn %s" % (filter(name)))
def makeEdge(f, t):
r2.cmd("age %s %s" % (filter(f), filter(t)))
def graphlibs(src, root):
hs = src.replace("/", "_")
hs = hs.replace("-", "_")
hs = hs.replace("+", "x")
try:
if done[hs]:
return
except:
done[hs] = True
src = findlib(src)
makeNode(src)
for lib in getlibs(src):
lib = findlib(lib)
makeNode(lib)
makeEdge(src, lib)
graphlibs(lib, src)
if len(sys.argv) > 1:
path = sys.argv[1]
graphlibs(path, None)
if output == "dot":
print r2.cmd("aggd")
else:
print r2.cmd("e scr.color=true;agg")
r2.quit()
else:
print "Usage: libgraph.py [path-to-bin]"
sys.exit(1)