-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshellcode_hash_search.py
More file actions
307 lines (260 loc) · 9.93 KB
/
Copy pathshellcode_hash_search.py
File metadata and controls
307 lines (260 loc) · 9.93 KB
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
####### NOTICE #######
# This plugin is an adaptation from the original mandiant ShellcodeHashes plugin for IDA found at:
# https://github.com/mandiant/flare-ida/blob/master/python/flare/shellcode_hash_search.py
# This adaptation converts the IDA plugin to a Binary Ninja plugin
# Python comments have been added to bound the modified sections of code and highlight any changes.
# These comments begin with the following string: "# CHANGE: "
####### END NOTICE #######
# CHANGE: Original Mandiant code from here
########################################################################
# Copyright 2012 Mandiant
# Copyright 2014,2018 FireEye
#
# Mandiant licenses this file to you under the Apache License, Version
# 2.0 (the "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
########################################################################
import sys
import ctypes
import logging
import os.path
import sqlite3
# CHANGE: Omitted unnecessary import statements and python code from original script here
############################################################
# SQL queries
############################################################
sql_lookup_hash_value='''
select
h.hash_val,
h.symbol_name,
l.lib_name,
t.hash_name,
t.hash_size
from
symbol_hashes h,
source_libs l,
hash_types t
where
h.hash_val=? and
h.lib_key=l.lib_key and
h.hash_type=t.hash_type;
'''
sql_lookup_hash_type_value='''
select
h.hash_val,
h.symbol_name,
l.lib_name,
t.hash_name,
t.hash_size
from
symbol_hashes h,
source_libs l,
hash_types t
where
h.hash_val=? and
h.lib_key=l.lib_key and
h.hash_type=t.hash_type and
h.hash_type=?;
'''
sql_get_all_hash_types='''
select
hash_type,
hash_size,
hash_name,
hash_code
from hash_types;
'''
sql_find_source_lib_by_name='''
select
lib_key
from
source_libs
where
lib_name=?;
'''
sql_adjust_cache_size='''
PRAGMA cache_size=200000;
'''
############################################################
# Row wrappers
############################################################
class SymbolHash(object):
def __init__(self, hashVal, symbolName, libName, hashName, hashSize):
self.hashVal = hashVal
self.symbolName = symbolName
self.libName = libName
self.hashName = hashName
self.hashSize = hashSize
def __str__(self):
return '%s:0x%08x %s!%s' % (self.hashName, self.hashVal, self.libName, self.symbolName )
class HashType(object):
def __init__(self, hashType, hashSize, hashName, hashCode):
self.hashType = hashType
self.hashSize = hashSize
self.hashName = hashName
self.hashCode = hashCode
class HashHit(object):
def __init__(self, ea, symHash):
self.ea = ea
self.symHash = symHash
############################################################
#
############################################################
class DbStore(object):
'''
Used to access the hash db.
'''
def __init__(self, dbPath):
self.dbPath = dbPath
self.conn = sqlite3.connect(dbPath)
self.conn.execute(sql_adjust_cache_size)
def close(self):
self.conn.close()
self.conn = None
def getSymbolByHash(self, hashVal):
'''
Returns list of SymbolHash objects for requested hashvalue.
List is empty for no hits
'''
retList = []
cur = self.conn.execute(sql_lookup_hash_value, (ctypes.c_int64(hashVal).value,))
for row in cur:
#logger.debug("Found hits for value: %08x", hashVal)
sym = SymbolHash(*row)
retList.append(sym)
return retList
def getAllHashTypes(self):
'''
Returns a list of HashType objects stored in the DB.
'''
retArr = []
cur = self.conn.execute(sql_get_all_hash_types)
for row in cur:
retArr.append(HashType(*row))
return retArr
def getSymbolByTypeHash(self, hashType, hashVal):
'''
Returns list of SymbolHash objects for requested hashvalue.
List is empty for no hits
'''
retList = []
cur = self.conn.execute(sql_lookup_hash_type_value, (ctypes.c_int64(hashVal).value, hashType))
for row in cur:
#logger.debug("Found hits for value: %08x", hashVal)
sym = SymbolHash(*row)
retList.append(sym)
return retList
############################################################
#
############################################################
# CHANGE: End original Mandiant code here.
# CHANGE: The following code is based on, but modified from, the original Mandiant shellcodehashes plugin
# CHANGE: The core of the changes are to convert the code from the IDA API to the Binary Ninja API
# CHANGE: Known limitations:
# CHANGE: Ability to choose options in a GUI has not been implemented
# CHANGE: Ability to search through non-code data has not been implemented
from binaryninja import *
class SearchParams(object):
'''
Just used to track the user provided search parameters.
'''
def __init__(self):
self.useXORSeed = False
self.XORSeed = 0
#hashTypes: list of HashTypes user confirmed to process
self.hashTypes = []
############################################################
# SearchParams
############################################################
def find_constants(instr):
if isinstance(instr, Constant):
return [instr.constant, instr.address]
class ShellcodeHashSearcher(object):
def __init__(self, dbstore, params, bv):
self.dbstore = dbstore
self.params = params
self.bv = bv
def processCode(self):
# TODO - add in option to use current selection
# TODO - add in option to search for .data/.rdata constants rather than just instruction args
self.lookForOpArgs()
def run(self):
log.log_info('Starting up')
self.processCode()
self.dbstore.close()
log.log_info('Done')
def lookForOpArgs(self):
for func in self.bv.functions:
try:
for opval, addr in func.mlil.traverse(find_constants):
if self.params.useXORSeed:
opval = opval ^ self.params.XORSeed
for h in self.params.hashTypes:
hits = self.dbstore.getSymbolByTypeHash(h.hashType, opval)
for sym in hits:
log.log_info("0x%08x: %s" % (addr, str(sym)))
self.markupLine(addr, sym)
except Exception as err:
log.warn("Exception: %s", str(err))
def markupLine(self, loc, sym):
comm = '%s!%s' % (sym.libName, sym.symbolName)
log.log_info("Making comment @ 0x%08x: %s" % (loc, comm))
self.bv.set_comment_at(loc, str(comm))
###################################################################
#
###################################################################
class SearchLauncher(object):
def __init__(self, bv):
self.params = SearchParams()
self.bv = bv
def run(self):
try:
log.log_info("Starting up")
dbFile = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'shellcode_hashes', 'sc_hashes.db'))
log.log_info('Trying default db path: %s' % dbFile)
if not os.path.exists(dbFile):
dbFile = get_open_filename_input("Select shellcode hash database", "*.db")
if (dbFile is None) or (not os.path.isfile(dbFile)):
log.log_info("No file select. Stopping now")
return
self.dbstore = DbStore(dbFile)
log.log_info("Loaded db file: %s" % dbFile)
self.getParams()
searcher = ShellcodeHashSearcher(self.dbstore, self.params, self.bv)
log.log_info('Starting to run the searcher now')
searcher.run()
log.log_info("Done")
except RejectionException:
log.log_info('User canceled action')
except Exception as err:
log.log_info("Exception caught: %s" % str(err))
def getParams(self):
log.log_info('Getting options')
self.promptForHashTypes()
self.params.useXORSeed = get_choice_input("Use XORSeed?", "", ["No", "Yes"])
if self.params.useXORSeed:
self.params.XORSeed = get_int_input("Enter hex XORSeed (either hex or decimal format)", "")
def promptForHashTypes(self):
'''
Currently defaults to searching all algorithms in the database.
TODO - display a chooser to allow users to limit the selection to particular algorithms
'''
# grab all by default
hashTypes = self.dbstore.getAllHashTypes()
# we used to prompt y/n for each one. too obnoxious, just force all hashes
self.params.hashTypes = hashTypes
###################################################################
#
###################################################################
def shellcode_hash_search(bv):
launcher = SearchLauncher(bv)
launcher.run()
PluginCommand.register("ShellcodeHashes", "Find and annotate shellcode hash values", shellcode_hash_search)