-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
197 lines (148 loc) · 6.54 KB
/
__init__.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
import sys
import copy
from binaryninja import LowLevelILOperation, PluginCommand, log_debug, log_info, log_error, log_alert
from PySide2.QtCore import Qt, QModelIndex, QAbstractTableModel
from PySide2.QtWidgets import (QDialog, QApplication, QTableView, QVBoxLayout, QHBoxLayout,
QPushButton, QLabel, QLineEdit)
class IndirectBranchModel(QAbstractTableModel):
COL_ARCH = 0
COL_ADDRESS = 1
def __init__(self, initial_data, default_branch, parent=None):
QAbstractTableModel.__init__(self, parent)
self.default_branch = default_branch
self.headers = ['arch', 'address']
self.branches = initial_data
def append_row(self, addr_str):
row = self.rowCount(None)
addr = self.parse_int(addr_str)
if addr is None:
return
# Avoid duplicate entries
if any([branch[self.COL_ADDRESS] == addr for branch in self.branches]):
return
self.insertRows(row)
self.setData(self.index(row, self.COL_ARCH), self.default_branch[self.COL_ARCH])
self.setData(self.index(row, self.COL_ADDRESS), hex(addr))
def parse_int(self, val):
try:
if val.startswith("0x"):
res = int(val, 16)
else:
res = int(val)
except ValueError:
log_error("Couldn't cast '%s' to int" % (val))
res = None
return res
# -----
def rowCount(self, _):
return len(self.branches)
def columnCount(self, _):
return len(self.headers)
def headerData(self, col, orientation, role):
if role == Qt.DisplayRole and orientation == Qt.Orientation.Horizontal:
return self.headers[col]
def data(self, index, role):
row = index.row()
col = index.column()
if role == Qt.DisplayRole:
branch = self.branches[row]
if col == self.COL_ARCH:
return branch[col].name
elif col == self.COL_ADDRESS:
return hex(int(branch[col]))
def flags(self, index):
col = index.column()
original_flags = super(IndirectBranchModel, self).flags(index)
if col == self.COL_ADDRESS:
return original_flags | Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable
else:
return original_flags
def setData(self, index, value, role=Qt.EditRole):
row = index.row()
col = index.column()
branch = self.branches[row]
if value != "" and col == self.COL_ADDRESS:
addr = self.parse_int(value)
if addr is None:
return False
branch[col] = addr
return True
return False
def insertRows(self, row, rows=1, index=QModelIndex()):
self.beginInsertRows(QModelIndex(), row, row + rows - 1)
for i in range(rows):
self.branches.insert(row + i, copy.copy(self.default_branch))
self.endInsertRows()
return True
def removeRows(self, row, rows=1, index=QModelIndex()):
self.beginRemoveRows(QModelIndex(), row, row + rows - 1)
del self.branches[row:row + rows]
self.endRemoveRows()
return True
class IndirectBranchSetterWidget(QDialog):
def __init__(self, bv, addr, parent=None):
super(IndirectBranchSetterWidget, self).__init__(parent)
self.bv = bv
self.indirect_jmp_addr = addr
# @@FIXME: What happens when there is more than one function for the same addr
self.func = self.bv.get_functions_containing(self.indirect_jmp_addr)[0]
# Setup widget properties
self.setWindowModality(Qt.NonModal)
self.showNormal() # Makes 'Qt.NonModal' work. Why?
title = QLabel(self.tr("Indirect Branch Editor (for 0x%x)" % (self.indirect_jmp_addr)))
self.setWindowTitle(title.text())
# Table
initial_data = []
for i in self.func.get_indirect_branches_at(self.indirect_jmp_addr):
initial_data.append([i.dest_arch, int(i.dest_addr)])
self.table_model = IndirectBranchModel(initial_data, [self.bv.arch, 0], parent=self)
self.table_view = QTableView()
self.table_view.setModel(self.table_model)
# Buttons and line edit
self.line_edit_new_branch = QLineEdit()
button_insert_row = QPushButton("Insert branch")
button_insert_row.clicked.connect(self.insert_row_clicked)
button_remove_row = QPushButton("Remove selected")
button_remove_row.clicked.connect(self.remove_row_clicked)
button_set_indirect_branches = QPushButton("Done")
button_set_indirect_branches.clicked.connect(self.set_indirect_branches_clicked)
# Buttons layout
buttons_layout = QHBoxLayout()
buttons_layout.addWidget(self.line_edit_new_branch)
buttons_layout.addWidget(button_insert_row)
buttons_layout.addWidget(button_remove_row)
buttons_layout.addWidget(button_set_indirect_branches)
# Main layout
layout = QVBoxLayout(self)
layout.addWidget(self.table_view)
layout.addLayout(buttons_layout)
self.setLayout(layout)
def insert_row_clicked(self):
new_addrs_str = self.line_edit_new_branch.text().strip()
new_addrs_str = new_addrs_str.split(",")
for addr_str in new_addrs_str:
self.table_model.append_row(addr_str.strip())
def remove_row_clicked(self):
select = self.table_view.selectionModel()
if select.hasSelection():
rows_selected = set()
for i in select.selectedIndexes():
rows_selected.add(i.row())
assert len(rows_selected) >= 1
# Hopefully this sort is enough to delete the correct rows.
sorted_rows_selected = sorted(list(rows_selected), reverse=True)
for row in sorted_rows_selected:
self.table_model.removeRows(row)
else:
log_error("No selection to remove")
def set_indirect_branches_clicked(self):
branches = self.table_model.branches
log_debug("Setting 0x%x's indirect branches to: %s" % (self.indirect_jmp_addr, branches))
self.func.set_user_indirect_branches(self.indirect_jmp_addr, branches)
self.accept()
def launch_plugin(bv, addr):
widget = IndirectBranchSetterWidget(bv, addr)
widget.exec_()
_name = "Indirect branches setter"
_description = "A plugin for setting indirect branches more easily, when binja fails to recognized a jump table"
PluginCommand.register_for_address(_name, _description, launch_plugin)