forked from OALabs/hashdb-bn
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ui.py
216 lines (176 loc) · 7.41 KB
/
ui.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
from typing import List, Optional, Tuple
import binaryninjaui
if "qt_major_version" in binaryninjaui.__dict__ and binaryninjaui.qt_major_version == 6:
from PySide6.QtCore import Qt
from PySide6.QtWidgets import (
QAbstractItemView,
QAbstractScrollArea,
QDialog,
QDialogButtonBox,
QLabel,
QSizePolicy,
QTableWidget,
QTableWidgetItem,
QVBoxLayout,
)
else:
from PySide2.QtCore import Qt
from PySide2.QtWidgets import (
QAbstractItemView,
QAbstractScrollArea,
QDialog,
QDialogButtonBox,
QLabel,
QSizePolicy,
QTableWidget,
QTableWidgetItem,
QVBoxLayout,
)
from . import hashdb_api as api
class _HashAlgorithmInfoTable(QTableWidget):
def __init__(self, parent=None):
super(_HashAlgorithmInfoTable, self).__init__(parent)
self.verticalHeader().hide()
self.setShowGrid(False)
self.setSelectionBehavior(
QAbstractItemView.SelectionBehavior.SelectRows
) # always select the entire row
self.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
self.setSizePolicy(
QSizePolicy(
QSizePolicy.Policy.Expanding, # horizontal
QSizePolicy.Policy.Expanding, # vertical
)
)
self.setSizeAdjustPolicy(
QAbstractScrollArea.SizeAdjustPolicy.AdjustToContents
) # perform the actual auto-expansion according to the size policy
self.setWordWrap(True)
self.setTextElideMode(Qt.TextElideMode.ElideNone)
self.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
self.setSortingEnabled(False)
def populate(self, hash_algorithms: List[api.Algorithm]) -> None:
self.setRowCount(len(hash_algorithms))
self.setColumnCount(3)
self.setHorizontalHeaderLabels(["Algorithm", "Type", "Description"])
for row_idx, algorithm in enumerate(hash_algorithms):
algorithm_table_items = [
QTableWidgetItem(algorithm.algorithm),
QTableWidgetItem(f"{algorithm.type}"),
QTableWidgetItem(algorithm.description),
]
for column_idx, algorithm_table_item in enumerate(algorithm_table_items):
self.setItem(row_idx, column_idx, algorithm_table_item)
# trigger a resize
self.resizeColumnsToContents()
self.resizeRowsToContents()
class _HashAlgorithmInfoDialog(QDialog):
def __init__(self, title: str, prompt: str, parent=None):
super(_HashAlgorithmInfoDialog, self).__init__(parent)
self.setWindowTitle(title)
self.infoTextLabel = QLabel(prompt)
self.algorithmInfoTable = _HashAlgorithmInfoTable()
self.buttonBox = QDialogButtonBox(
QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel
)
layout = QVBoxLayout()
layout.addWidget(self.infoTextLabel)
layout.addWidget(self.algorithmInfoTable)
layout.addWidget(self.buttonBox)
self.setLayout(layout)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
def exec_and_get_selected_choice_idx(self) -> Optional[int]:
dialog_return_code: QDialog.DialogCode = (
self.exec()
) # block until dialog closes
if dialog_return_code == QDialog.DialogCode.Accepted:
return self.algorithmInfoTable.currentRow()
else:
return None
class _HuntAlgorithmInfoTable(QTableWidget):
def __init__(self, parent=None):
super(_HuntAlgorithmInfoTable, self).__init__(parent)
self.verticalHeader().hide()
self.setShowGrid(False)
self.setSelectionBehavior(
QAbstractItemView.SelectionBehavior.SelectRows
) # always select the entire row
self.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
self.setSizePolicy(
QSizePolicy(
QSizePolicy.Policy.Expanding, # horizontal
QSizePolicy.Policy.Expanding, # vertical
)
)
self.setSizeAdjustPolicy(
QAbstractScrollArea.SizeAdjustPolicy.AdjustToContents
) # perform the actual auto-expansion according to the size policy
self.setWordWrap(True)
self.setTextElideMode(Qt.TextElideMode.ElideNone)
self.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
self.setSortingEnabled(False)
def populate(
self, match_results: List[Tuple[api.HuntMatch, api.Algorithm]]
) -> None:
self.setRowCount(len(match_results))
self.setColumnCount(5)
self.setHorizontalHeaderLabels(
["Count", "Hit Rate", "Algorithm", "Type", "Description"]
)
for row_idx, (hunt_match, algorithm) in enumerate(match_results):
algorithm_table_items = [
QTableWidgetItem(f"{hunt_match.count}"),
QTableWidgetItem(f"{hunt_match.hitrate:.0%}"),
QTableWidgetItem(algorithm.algorithm),
QTableWidgetItem(f"{algorithm.type}"),
QTableWidgetItem(algorithm.description),
]
for column_idx, algorithm_table_item in enumerate(algorithm_table_items):
self.setItem(row_idx, column_idx, algorithm_table_item)
# trigger a resize
self.resizeColumnsToContents()
self.resizeRowsToContents()
class _HuntAlgorithmInfoDialog(QDialog):
def __init__(self, title: str, prompt: str, parent=None):
super(_HuntAlgorithmInfoDialog, self).__init__(parent)
self.setWindowTitle(title)
self.infoTextLabel = QLabel(prompt)
self.algorithmInfoTable = _HuntAlgorithmInfoTable()
self.buttonBox = QDialogButtonBox(
QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel
)
layout = QVBoxLayout()
layout.addWidget(self.infoTextLabel)
layout.addWidget(self.algorithmInfoTable)
layout.addWidget(self.buttonBox)
self.setLayout(layout)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
def exec_and_get_selected_choice_idx(self) -> Optional[int]:
dialog_return_code: QDialog.DialogCode = (
self.exec()
) # block until dialog closes
if dialog_return_code == QDialog.DialogCode.Accepted:
return self.algorithmInfoTable.currentRow()
else:
return None
def get_algorithm_choice(
context, title: str, prompt_text: str, algorithm_choices: List[api.Algorithm]
) -> Optional[int]:
hash_algorithm_info_dialog = _HashAlgorithmInfoDialog(
title=title, prompt=prompt_text, parent=context.widget
)
hash_algorithm_info_dialog.algorithmInfoTable.populate(algorithm_choices)
return hash_algorithm_info_dialog.exec_and_get_selected_choice_idx()
def get_hunt_algorithm_match_result_choice(
context,
title: str,
prompt_text: str,
match_results: List[Tuple[api.HuntMatch, api.Algorithm]],
) -> Optional[int]:
hunt_algorithm_match_result_dialog = _HuntAlgorithmInfoDialog(
title=title, prompt=prompt_text, parent=context.widget
)
hunt_algorithm_match_result_dialog.algorithmInfoTable.populate(match_results)
return hunt_algorithm_match_result_dialog.exec_and_get_selected_choice_idx()