/
BinDiffFilter.py
341 lines (282 loc) · 11.5 KB
/
BinDiffFilter.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
from PySide import QtGui, QtCore
import idc
import pickle
class AbstractFilter(object):
class_counter = 0
def __init__(self,name = ""):
print "AbstractFilter __init__ : {0}".format(AbstractFilter.class_counter)
self.__name = name
if self.__name == "":
self.__name = str(AbstractFilter.class_counter)
AbstractFilter.class_counter += 1
pass
def filter(self,row):
raise NotImplementedError()
def getName(self):
return self.__name
"""
Sample filter
"""
class MatchedFunctions(AbstractFilter):
def filter(self,row):
print "filter test"
class CustomSortFilterProxyModel(QtGui.QSortFilterProxyModel):
def __init__(self, parent=None):
super(CustomSortFilterProxyModel, self).__init__(parent)
self.__lambdasToPickle = {}
self.__filterFunctions = {}
self.__hiddenFunctions = set()
self.__filtersCounter = 0
self.__colorRow = {} # list of {"color" : Qt.magenta,"func": set()}
self.__colorRow["intSafeFunctions"] = {
"color" : QtCore.Qt.magenta,
"indexes" : set()
}
self.__buildinFilters = {
"hideFunction": self.__filterHideFunction ,
"matched" : self.__filterHideMatchedFunctions
}
def addFilterFunction(self, func, name = ""):
"""
ex:
model.addFilterFunction(
lambda row: int(row["primary instructions"] > 3),
"more_than_3"
)
"""
if name == "":
name = str(self.__filtersCounter)
#detect whether it's lambda function cos we need to handle it in different way
#lambda functions should be passed as strings cos in that way they can be pickled
if isinstance(func,str):
self.__lambdasToPickle[name] = func
func = eval(func)
self.__filterFunctions[name] = func
self.invalidateFilter()
self.__filtersCounter += 1
def removeFilterFunction(self, name):
"""
name : hashable object
Removes the filter function associated with name,
if it exists.
"""
if name in self.__filterFunctions.keys():
del self.__filterFunctions[name]
if name in self.__lambdasToPickle.keys():
del self.__lambdasToPickle[name]
self.invalidateFilter()
def __filterExists(self,name):
return name in self.__filterFunctions.keys()
def filterAcceptsRow(self, row_num, parent):
try:
functions = self.__filterFunctions.values()
if functions == []:
return True
model = self.sourceModel()
row = self.__getRow(model,row_num)
#simple lambda filters
flags = [func(row) for func in functions]
return not False in flags
except Exception as e:
print e.message
return True
def data(self,index,role = QtCore.Qt.DisplayRole):
try:
if role == QtCore.Qt.BackgroundColorRole and index.isValid():
for item in self.__colorRow.values():
if index.row() in item["indexes"]:
#return item["color"]
return QtGui.QBrush(QtCore.Qt.yellow)
except:
pass
return super(QtGui.QSortFilterProxyModel,self).data(index,role)
"""
Built-in filters
"""
def __filterHideFunction(self,row):
try:
ea_primary = row["EA primary"]
except:
return True
return ea_primary not in self.__hiddenFunctions
def __filterHideMatchedFunctions(self,row):
try:
return float(row["similarity"]) < 1.0
except:
return True
"""
Public methods
"""
def hideFunction(self,ea_primary,refresh = True):
if not self.__filterExists("hideFunction"):
self.addFilterFunction(self.__filterHideFunction,"hideFunction")
self.__hiddenFunctions.add(ea_primary)
if refresh:
self.invalidateFilter()
def showFunction(self,ea_primary):
if ea_primary in self.__hiddenFunctions:
self.__hiddenFunctions.remove(ea_primary)
self.invalidateFilter()
else:
"Function : {0} is not hidden".format(ea_primary)
def hideMatchedFunctions(self):
self.addFilterFunction(self.__filterHideMatchedFunctions,"matched")
def showMatchedFunctions(self):
self.removeFilterFunction("matched")
"""
Helpers
"""
def __getColumnIdByName(self,name):
model = self.sourceModel()
for i in range(0,model.columnCount()):
if model.headerData(i,QtCore.Qt.Horizontal) == name:
return i
return -1
def __getRow(self,model,index):
row = {}
for colIndex in range(0,model.columnCount()):
#need column name + data
name = model.headerData(colIndex,QtCore.Qt.Horizontal)
data = model.data( model.index(index,colIndex) )
row[name] = data
return row
"""
Getters & Setters [necessary during pickling]
"""
def getFilterFunctions(self):
return self.__filterFunctions
def setFilterFunctions(self,ff):
self.__filterFunctions = ff
def getHiddenFunctions(self):
return self.__hiddenFunctions
def setHiddenFunctions(self,hf):
self.__hiddenFunctions = hf
def addInfSafeRowIndex(self,index):
self.__colorRow["intSafeFunctions"]["indexes"].add(index)
def getLambdaFilters(self):
return self.__lambdasToPickle
def getColoredRows(self):
return self.__colorRow
class CBinDiffFilter(object):
def __init__(self):
self.__columns = {}
self.__hiddenColumns = set()
self.__uselessStandardColumns = [
"confidence",
"change",
"comments ported",
"algorithm",
"matched basicblocks",
"basicblocks primary",
"basicblocks secondary",
"matched edges",
"edges primary",
"edges secondary"
]
def findBinDiffWindow(self):
try:
#collect necessary info
self.window = self.findWindow("Matched Functions")
self.table = self.findTableView()
self.orig_model = self.table.model()
#setup our new proxy model
self.proxy_model = CustomSortFilterProxyModel()
self.proxy_model.setSourceModel(self.orig_model)
self.proxy_model.setDynamicSortFilter(True)
#inject our new proxy model into the tableview
self.table.setModel(self.proxy_model)
self.__initColumnNamesWithId()
return True
except Exception as e:
print e.message
return False
def findWindow(self, windowName):
"""
Locate the IDA window we are looking for
"""
ins = QtCore.QCoreApplication.instance()
widgets = ins.allWidgets()
for x in widgets:
if x.objectName() == windowName:
return x
raise Exception('Could not locate window, make sure that it is open.')
def findTableView(self):
"""
Locate the TableView inside the window
"""
for x in self.window.children()[-1].children(): #last QWidget seems to hold all the children
if type(x) == QtGui.QTableView:
return x
raise Exception('Could not locate QTableView of the window.')
def findWindowLayout(self):
"""
Locate the the QVBoxLayout of the window
"""
for x in self.window.children(): #should always be [1] but we will search for it anyways
if type(x) == QtGui.QVBoxLayout:
return x
raise Exception('Could not locate QVBoxLayout of the window.')
def menuHandler(self,pos):
menu = QtGui.QMenu()
messageAction = menu.addAction("About")
print repr(pos)
action = menu.exec_(self.table.mapToGlobal(pos))
print "after menu.exec"
print repr(action)
print repr(messageAction)
if action == messageAction:
print "Message action clicked"
def saveFilters(self,filePath):
"""
TODO: maybe refactor this code to do it more general and automatic ????
"""
#hiddenFunctions
filterFunctions = self.proxy_model.getFilterFunctions()
hiddenFunctionsStatus = filterFunctions.has_key("hiddenFunctions")
hiddenFunctions = self.proxy_model.getHiddenFunctions()
#lambdas
lambdaFilters = self.proxy_model.getLambdaFilters()
filterObjects = {}
if hiddenFunctionsStatus:
filterObjects["hiddenFunctions"] = {}
filterObjects["hiddenFunctions"]["functions"] = hiddenFunctions
if len(lambdaFilters):
filterObjects["lambdaFilters"] = {}
filterObjects["lambdaFilters"] = lambdaFilters
if filterFunctions.has_key("matched"):
filterObjects["matched"] = {} #just to have that info
filterObjects["coloredRows"] = self.proxy_model.getColoredRows()
pickle.dump(filtersObjects,open(filePath,'wb'))
def loadFilters(self,filePath):
filtersObjects = pickle.load(open(filePath,'rb'))
self.proxy_model.setFilterFunctions(filtersObjects[0])
self.proxy_model.setHiddenFunctions(filtersObjects[1])
self.proxy_model.invalidateFilter()
def hideSomeStandardColumns(self):
for column in self.__uselessStandardColumns:
self.table.hideColumn( self.__columns[column] )
def showSomeStandardColumns(self):
for column in self.__uselessStandardColumns:
self.table.showColumn( self.__columns[column] )
def hideColumn(self,name):
self.table.hideColumn( self.__columns[name] )
def showColumn(self,name):
self.table.showColumn( self.__columns[name] )
def addIntSafeFunction(self,ea):
index = self.__getIndexFromValue(ea,"EA primary")
self.proxy_model.addInfSafeRowIndex(index)
"""
Helpers
"""
def __initColumnNamesWithId(self):
for colIndex in range(0,self.orig_model.columnCount()):
name = self.orig_model.headerData(colIndex,QtCore.Qt.Horizontal)
self.__columns[name] = colIndex
def __getIndexFromValue(self,value,columnName):
colIndex = self.__columns[columnName]
for rowIndex in range(0,self.proxy_model.rowCount()):
index = self.proxy_model.index(rowIndex,colIndex)
if self.proxy_model.data( index ) == value:
return rowIndex
#BinDiffFilter = CBinDiffFilter()
print "[+]BinDiffFilter plugin loaded"