-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathyarascan.py
122 lines (95 loc) · 4.5 KB
/
yarascan.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
"""
Defunct <defunct<at>defunct.io> - NOP Developments LLC. 2016
Yarascan - v0.1.0
Yarascan is a PoC UI plugin to demonstrate the usage of BinjaDock.
Signatures for crypto, malware, antivm and so on used in this PoC and more are located at:
(https://github.com/Yara-Rules/rules.git)
.. note::
In this example you will need to define SIG_FILE with the appropriate yara rules file.
MIT License
Copyright (c) <2016> <NOP Developments LLC>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
SIG_FILE = '/yara/crypto_signatures.yar'
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtCore import Qt
from binaryninja import *
from defunct.widgets import BinjaWidget
import defunct.widgets
import yara
class YaraWidget(BinjaWidget):
"""Binja Yara plugin
Identifies yara signatures and displays them in a BinjaDockWidget.
"""
def __init__(self):
super(YaraWidget, self).__init__('Yara')
self._rules = yara.compile(filepath=core.BNGetUserPluginDirectory() + SIG_FILE)
self._table = QtWidgets.QTableWidget()
self._table.setColumnCount(2)
self._table.setHorizontalHeaderLabels(['Offset', 'Signature'])
self._table.horizontalHeader().setStretchLastSection(True)
self._table.verticalHeader().setVisible(False)
self.setLayout(QtWidgets.QStackedLayout())
self.layout().addWidget(self._table)
self.setObjectName('BNPlugin_Yara')
# self.addToolMenuAction("&Yara about", lambda: log_info('Yara scan!'))
class YaraThread(QtCore.QThread):
finished = QtCore.pyqtSignal(list)
def __init__(self, rules, file):
super(YaraWidget.YaraThread, self).__init__()
self._rules = rules
self._file = file
def run(self):
self.finished.emit(self._rules.match(self._file))
def scan(self, bv):
""" Scans a binary view for with the compiled yara rules
:param bv: The BinaryView to use
:type bv: binaryninja.BinaryView
:return:
"""
self._view = bv
self._thread = YaraWidget.YaraThread(self._rules, bv.file.filename)
self._thread.finished.connect(self.scan_finished)
self._thread.start()
@QtCore.pyqtSlot(list)
def scan_finished(self, matches):
records = [[x.rule, a[0]] for x in matches for a in x.strings]
self._table.setRowCount(len(records))
for i in range(0, len(records)):
# TODO: Proper QTableView / model implementation instead of lazy use of QTableWidget
desc = QtWidgets.QTableWidgetItem(records[i][0])
desc.setFlags(Qt.ItemIsEnabled)
# TODO: meta tooltip
offset = QtWidgets.QTableWidgetItem('0x%.8x' % records[i][1])
offset.setFlags(Qt.ItemIsEnabled)
offset.setForeground(QtGui.QColor(162, 217, 175))
self._table.setItem(i, 0, offset)
self._table.setItem(i, 1, desc)
self._table.cellDoubleClicked.connect(self.cell_action)
self._thread.terminate()
self._core.show()
self._core.selectTab(self)
self.show()
def cell_action(self, row, column):
# TODO: view highlighting
self.navigate(self._view, int(self._table.item(row, 0).text(), 16))
def navigate(self, bv, offset):
bv.navigate('Hex:' + bv.view_type, offset)
d = YaraWidget()
def scan(bv, offset):
d.scan(bv)
PluginCommand.register_for_address('Yara', 'Scan for yara signatures', scan)