-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathtest-ragdiff2.py
executable file
·241 lines (218 loc) · 6.7 KB
/
test-ragdiff2.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python3
# ragdiff2 - Copyright 2010-2015
# pancake <nopcode.org>
# nibble <develsec.org>
import sys
import os
import gtk
import gtk.gdk
import xdot
import time
import tempfile
from r2.r_core import *
# workaround a bug in valabind or swig here..
try:
FcnType_FCN
except:
FcnType_FCN = 1
FcnType_SYM = 4
BlockDiff_NULL = 0
BlockDiff_MATCH = 'm'
BlockDiff_UNMATCH = 'u'
class ViewWidget(gtk.VBox):
ui = '''
<ui>
<toolbar name="ToolBar">
<toolitem action="ZoomIn"/>
<toolitem action="ZoomOut"/>
<toolitem action="ZoomFit"/>
<toolitem action="Zoom100"/>
</toolbar>
</ui>
'''
def __init__(self):
gtk.VBox.__init__(self)
# XDotWidget
xdotwidget = self.xdotwidget = xdot.DotWidget()
# Toolbar
uimanager = gtk.UIManager()
actiongroup = gtk.ActionGroup('Actions')
actiongroup.add_actions((
('ZoomIn', gtk.STOCK_ZOOM_IN, None, None, None, self.xdotwidget.on_zoom_in),
('ZoomOut', gtk.STOCK_ZOOM_OUT, None, None, None, self.xdotwidget.on_zoom_out),
('ZoomFit', gtk.STOCK_ZOOM_FIT, None, None, None, self.xdotwidget.on_zoom_fit),
('Zoom100', gtk.STOCK_ZOOM_100, None, None, None, self.xdotwidget.on_zoom_100),
))
uimanager.insert_action_group(actiongroup, 0)
uimanager.add_ui_from_string(self.ui)
toolbar = uimanager.get_widget('/ToolBar')
toolbar.set_icon_size(gtk.ICON_SIZE_SMALL_TOOLBAR)
toolbar.set_style(gtk.TOOLBAR_ICONS)
toolbar.set_show_arrow(False)
label = self.label = gtk.Label()
hbox = gtk.HBox(False, 5)
hbox.pack_start(toolbar, False)
hbox.pack_start(label, False)
self.pack_start(hbox, False)
self.pack_start(xdotwidget)
def set_filename(self, name):
self.label.set_text(name)
def set_code(self, code, fit):
self.xdotwidget.set_dotcode(code)
if (fit):
self.xdotwidget.zoom_to_fit()
class DiffWidget(gtk.VPaned):
def __init__(self):
gtk.VPaned.__init__(self)
self.set_position (500)
# Graph viewers
hbox = gtk.HBox(True, 5)
dw = self.dw = ViewWidget()
dw2 = self.dw2 = ViewWidget()
hbox.pack_start(dw)
hbox.pack_start(dw2)
self.add(hbox)
# Function list
scrolledwin = gtk.ScrolledWindow()
scrolledwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
liststore = self.liststore = gtk.ListStore(str, str, str, str, str)
treeview = gtk.TreeView(liststore)
# Column Address L
tvcolumn0 = gtk.TreeViewColumn('Function L')
treeview.append_column(tvcolumn0)
cell0 = gtk.CellRendererText()
tvcolumn0.pack_start(cell0, True)
tvcolumn0.add_attribute(cell0, 'text', 0)
tvcolumn0.set_sort_column_id(0)
# Column Function L
tvcolumn1 = gtk.TreeViewColumn('Address L')
treeview.append_column(tvcolumn1)
cell1 = gtk.CellRendererText()
tvcolumn1.pack_start(cell1, True)
tvcolumn1.add_attribute(cell1, 'text', 1)
tvcolumn1.set_sort_column_id(1)
# Column Address R
tvcolumn2 = gtk.TreeViewColumn('Function R')
treeview.append_column(tvcolumn2)
cell2 = gtk.CellRendererText()
tvcolumn2.pack_start(cell2, True)
tvcolumn2.add_attribute(cell2, 'text', 2)
tvcolumn2.set_sort_column_id(2)
# Column Function R
tvcolumn3 = gtk.TreeViewColumn('Address R')
treeview.append_column(tvcolumn3)
cell3 = gtk.CellRendererText()
tvcolumn3.pack_start(cell3, True)
tvcolumn3.add_attribute(cell3, 'text', 3)
tvcolumn3.set_sort_column_id(3)
# Column Diff
tvcolumn4 = gtk.TreeViewColumn('Diff')
treeview.append_column(tvcolumn4)
cell4 = gtk.CellRendererText()
tvcolumn4.pack_start(cell4, True)
tvcolumn4.add_attribute(cell4, 'text', 4)
tvcolumn4.set_sort_column_id(4)
# Set treeview options and add it to scrolledwin
treeview.set_reorderable(True)
scrolledwin.add(treeview)
self.add2(scrolledwin)
treeview.connect('row-activated', self.on_row_activated)
def on_row_activated(self, treeview, row, col):
model = treeview.get_model()
self.row_handler(self, model[row][1], model[row][3])
def set_row_handler(self, handler):
self.row_handler = handler
def set_filename_l(self, name):
self.dw.set_filename(name)
def set_filename_r(self, name):
self.dw2.set_filename(name)
def set_code_l(self, code, fit):
self.dw.set_code(code, fit)
def set_code_r(self, code, fit):
self.dw2.set_code(code, fit)
def add_function(self, row):
self.liststore.append(row)
def clear_functions(self):
self.liststore.clear()
class RagDiff2(gtk.Window):
def __init__(self):
gtk.Window.__init__(self)
self.c = RCore()
self.c2 = RCore()
# Main window
self.set_title('ragdiff2')
self.set_default_size(512, 512)
dw = self.dw = DiffWidget()
dw.set_row_handler(self.update_graphs)
self.add(dw)
self.connect('destroy', gtk.main_quit)
def update_graphs(self, dw, addr_l, addr_r):
if addr_l != '':
file_l = tempfile.gettempdir()+os.sep+'ragdiff2_l'
self.c.cmd0('agd %s > %s' % (addr_l, file_l))
fp = file(file_l)
dw.set_code_l (fp.read(), True)
fp.close()
os.unlink(file_l)
else:
dw.set_code_l ('digraph code { }', False)
if addr_r != '':
file_r = tempfile.gettempdir()+os.sep+'ragdiff2_r'
self.c2.cmd0('agd %s > %s' % (addr_r, file_r))
fp = file(file_r)
dw.set_code_r (fp.read(), True)
fp.close()
os.unlink(file_r)
else:
dw.set_code_r ('digraph code { }', False)
def set_files(self, file_l, file_r):
# Init cores
self.c.config.set_i("io.va", 1)
self.c.config.set_i("anal.split", 1)
self.c.file_open(file_l, 0, 0)
self.c.bin_load(None, 0)
self.c2.config.set_i("io.va", 1)
self.c2.config.set_i("anal.split", 1)
self.c2.file_open(file_r, 0, 0)
self.c2.bin_load(None, 0)
# Configure bindiffing
self.c.anal.diff_setup_i (False, -1, -1)
self.c2.anal.diff_setup_i (False, -1, -1)
# Clear treeview
self.dw.clear_functions()
self.dw.set_filename_l(file_l)
self.dw.set_filename_r(file_r)
def diff(self):
# Diff
self.c.gdiff(self.c2)
# Fill treeview
for fcn in self.c.anal.get_fcns():
if (fcn.type == FcnType_FCN or fcn.type == FcnType_SYM):
diffaddr = '0x%08x' % fcn.diff.addr
difftype = '%c' % fcn.diff.type
if (difftype == 'm'): #BlockDiff_MATCH):
difftype = "MATCH"
elif (difftype == BlockDiff_UNMATCH):
difftype = "UNMATCH"
else:
difftype = "NEW"
diffaddr = ''
self.dw.add_function([fcn.name, '0x%08x' % fcn.addr, fcn.diff.name, diffaddr, difftype])
# Add NEW function that appear in c2, but not in c1
for fcn in self.c2.anal.get_fcns():
if ((fcn.type == FcnType_FCN or fcn.type == FcnType_SYM) and fcn.diff.type == BlockDiff_NULL):
self.dw.add_function(['', '', fcn.name, '0x%08x' % fcn.addr, "NEW"])
def main():
# Check args
if (len (sys.argv) != 3):
sys.exit(' usage: %s <file> <file>' % sys.argv[0])
# Create ragdiff2 win
rd = RagDiff2()
rd.show_all()
# Set and Diff files
rd.set_files(sys.argv[1], sys.argv[2])
rd.diff()
# Gtk main
gtk.main()
if __name__ == '__main__':
main()