-
Notifications
You must be signed in to change notification settings - Fork 33
/
inhale.py
429 lines (388 loc) · 18.2 KB
/
inhale.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import argparse
import binwalk
import fnmatch
import hashlib
import json
import magic
import os
import re
import requests
import sys
import time
import yara
from datetime import datetime
from elasticsearch import Elasticsearch
from yara import CALLBACK_MATCHES
import time
import iModules
from iModules import *
CONFIG = helper.CONFIG # This imports our config file for use
### PARSER ARGUMENTS ###########################################################
parser = argparse.ArgumentParser(description='inhale')
parser.add_argument('-f', dest='infile', help='Analyze a single file')
parser.add_argument('-d', dest='directory', help='Analyze a directory of files')
parser.add_argument('-u', dest='urlFile', help='Analyze a remote file (url)')
parser.add_argument('-r', dest='rDirectory', help='Analyze a remote directory (url)')
parser.add_argument('-l', dest='urlList', help='Analyze a list of URLs in a text file')
parser.add_argument('-t', dest='tags', help='Add additional tags to the output.')
parser.add_argument('-b', dest='binWalkSigs', help="Turn off binwalk signatures", action="store_true")
parser.add_argument('-y', dest='yaraRules', action='store', help="Specify custom Yara Rules")
parser.add_argument('-o', dest='outdir', action='store',
help="Store scraped files in specific output dir (default: ./files/<date>/)")
parser.add_argument('-i', dest='noAdd', help="Just print info, don't add files to database", action="store_true")
parser.add_argument('--html', dest='htmlout', help="Save output as html to the webdir.", action="store_true")
### Global Vars ################################################################
ts = time.gmtime()
today = time.strftime("%Y-%m-%d", ts)
# ANSI Colors
cBLK = "\033[1;30m"
cRED = "\033[38;5;197m"
cGRN = "\033[1;32m"
cYEL = "\033[1;33m"
cBLUE = "\033[1;34m"
cMGNT = "\033[1;35m"
cCYAN = "\033[1;36m"
cWHT = "\033[1;37m"
cPNK = "\033[38;5;219m"
cPURP = "\033[38;5;141m"
e = "\033[0m"
# Text decorations
startline = cPNK+"╭"+"─"*79+e
divline = cPNK+"├"+"─"*79+e
endline = cPNK+"╰"+"─"*79+e
side = cPNK+"│"+cCYAN
banner = """░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ▒▒████████ ▒▒██ ░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ▒▒████ ██ ████ ████ ░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ▒▒██████ ██ ██████▒▒██ ░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ████████ ██ ██████▒▒▒▒ ░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░ ██████▒▒▒▒██████▒▒▒▒████▒▒ ░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░ ▒▒██████████\033[5m██\033[0m\033[38;5;219m \033[5m██\033[0m\033[38;5;219m██████████ ░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░ ████▒▒██████\033[5m██\033[0m\033[38;5;219m \033[5m██\033[0m\033[38;5;219m████████▒▒ ░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░ ████▒▒████████████████████ ░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░ ██████████████████▒▒ ░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░ ▒▒▒▒▒▒ ████████████████▒▒ ░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░ ▒▒▒▒▒▒▒▒ ████████████▒▒ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░ ▒▒▒▒▒▒▒▒ ██████████▒▒ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░ ▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒ ▒▒▒▒ ░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ▒▒▒▒ ▒▒▒▒▒▒▒▒▒▒ ░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░
\033[1;30;47m inhale.py - Malware Inhaler """
### Database Functions #########################################################
def elasticPost(fileinfo):
es = Elasticsearch()
es.indices.create(index='inhaled', ignore=400)
try:
es.index(index="inhaled", doc_type="file", body=fileinfo)
print("{}{} [+] Added {}{}{}!".format(side,e,cCYAN,fileinfo["filename"],e))
except:
print("{}{} {}[!] Could not add {}{} - Try passing -b to disable binwalk signatures!".format(side,e,cRED,fileinfo["filename"],e))
### File Operations ############################################################
# Recursively reads directory and creates a file list
def rGlob(treeroot, pattern):
results = []
for base, dirs, files in os.walk(treeroot):
goodfiles = fnmatch.filter(files, pattern)
results.extend(os.path.join(base, f) for f in goodfiles)
return results
### Analysis Functions #########################################################
def getHashes(afile):
BUF_SIZE = 65536
hashlist = {}
md5 = hashlib.md5()
sha1 = hashlib.sha1()
sha256 = hashlib.sha256()
with open(afile, 'rb') as f:
while True:
data = f.read(BUF_SIZE)
if not data:
break
md5.update(data)
sha1.update(data)
sha256.update(data)
hashlist["md5"] = md5.hexdigest()
hashlist["sha1"] = sha1.hexdigest()
hashlist["sha256"] = sha256.hexdigest()
return hashlist
# basically runs file command
def getMagic(afile):
with magic.Magic() as m:
filetype = m.id_filename(afile)
return filetype
def setMagicFlags(m):
# 0 Regular File
# 1 ELF
# 2 PE
# 3 PDF
# 4 Archive
if m[0:3] == "ELF":
return 1
elif m[0:2] == "PE":
return 2
elif m[0:3] == "PDF":
return 3
elif "archive" in m:
return 4
elif "bzip2" in m:
return 4
elif "gzip" in m:
return 4
else:
return 0
def getSize(afile):
statinfo = os.stat(afile)
return statinfo.st_size
# Find all URLS in a given file
def urlFind(string):
url = re.findall(b'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', string)
return url
def binWalkScan(filename):
sigz = {}
for module in binwalk.scan(filename, signature=True, quiet=True):
sigz[module.name] = {}
for result in module.results:
sigz[module.name]["0x%.8X" %result.offset] = result.description
return sigz
def yaraMatch(data):
return yara.CALLBACK_CONTINUE
### Parsing Logic ##############################################################
def parseFile(inputfile):
try:
finfo = {} # This holds all of the scan data
binInfo = r2hook.getBinInfo(inputfile) # The binary data from radare2
filename = inputfile
filetype = getMagic(filename) # Get filetype from magic value
fileFlag = setMagicFlags(filetype) # Get magic code, if any
baseName = filename.split("/")[-1:][0]
ext = baseName.split(".")[-1:][0]
if ext == baseName:
ext == "NONE"
bf = open(filename, "rb")
binFile = bf.read() # A buffer of the binary file
urls = set(urlFind(binFile)) # Finding urls in the file
fsize = getSize(filename) # Get file size
hashes = getHashes(filename) # Get MD5, SHA1, and SHA256
###- Building the data structure to post to the database. -###
finfo["filename"] = filename
finfo["file_ext"] = ext
finfo["filesize"] = fsize
finfo["filetype"] = filetype
finfo["md5"] = hashes["md5"]
finfo["sha1"] = hashes["sha1"]
finfo["sha256"] = hashes["sha256"]
finfo["added"] = datetime.now()
finfo["tags"] = tags # Tags from command line
if "bin" in binInfo:
# Fill the file info struct if it's a binary
finfo["r2_arch"] = binInfo["bin"]["arch"]
finfo["r2_baddr"] = binInfo["bin"]["baddr"]
finfo["r2_binsz"] = binInfo["bin"]["binsz"]
finfo["r2_bits"] = binInfo["bin"]["bits"]
finfo["r2_canary"] = binInfo["bin"]["canary"]
finfo["r2_class"] = binInfo["bin"]["class"]
finfo["r2_compiled"] = binInfo["bin"]["compiled"]
finfo["r2_dbg_file"] = binInfo["bin"]["dbg_file"]
finfo["r2_intrp"] = binInfo["bin"]["intrp"]
finfo["r2_lang"] = binInfo["bin"]["lang"]
finfo["r2_lsyms"] = binInfo["bin"]["lsyms"]
finfo["r2_machine"] = binInfo["bin"]["machine"]
finfo["r2_os"] = binInfo["bin"]["os"]
finfo["r2_pic"] = binInfo["bin"]["pic"]
finfo["r2_relocs"] = binInfo["bin"]["relocs"]
finfo["r2_rpath"] = binInfo["bin"]["rpath"]
finfo["r2_stripped"] = binInfo["bin"]["stripped"]
finfo["r2_subsys"] = binInfo["bin"]["subsys"]
if "core" in binInfo:
# These are extra sections that sometimes don't appear in r2 output
finfo["r2_format"] = binInfo["core"]["format"]
finfo["r2_iorw"] = binInfo["core"]["iorw"]
finfo["r2_type"] = binInfo["core"]["type"]
### Binwalk ###
if bwSigz:
bwSigs = binWalkScan(inputfile)
finfo["binwalk"] = bwSigs
### Yara ###
yaraTimeout = CONFIG["analyst_opts"]["yara_timeout"]
matches = rules.match(data=binFile, callback=yaraMatch, which_callbacks=CALLBACK_MATCHES,timeout=yaraTimeout)
yMatch = [] # A list of yara rule matches
if len(matches) > 0:
for match in matches:
yMatch.append(match.rule)
finfo["yara"] = yMatch
### URL Finder ###
if urls:
urlStr = []
for url in urls:
urlStr.append(url.decode("utf-8"))
finfo["urls"] = urlStr
### telfhash ###
if fileFlag == 1:
th = r2hook.telfhasher(filename)
if len(th) > 1:
finfo["telfhash"] = th
bf.close()
return finfo
except Exception as e:
print(e)
print("Error processing file!")
return
### Main logic #################################################################
if __name__ == '__main__':
args = parser.parse_args()
infile = args.infile
directory = args.directory
rDirectory = args.rDirectory
urlFile = args.urlFile
tags = args.tags
bwSigz = args.binWalkSigs
urlList = args.urlList
###- Command Line Switches -###
if args.yaraRules:
rules = yara.compile(args.yaraRules)
else:
rules = yara.compile('YaraRules/index.yar')
if args.outdir:
fpath = "./" + args.outdir + "/"
else:
fpath = "./files/" + today + "/"
if args.noAdd:
add2db = 0
else:
add2db = 1
if args.binWalkSigs:
bwSigz = 0
else:
bwSigz = 1
if tags:
tags = args.tags
else:
tags = ""
###- Config File Switches -###
if CONFIG["options"]["enable_database"]:
add2db = 1
else:
add2db = 0
### End Switches
ansiout = ""
ansiout += cPNK + banner + e + "\n"
ansiout += startline + "\n"
ansiout += "{} * T A S K S * {}\n".format(side,e)
###-- Processing single local file -----------------------------------------
if infile:
ansiout += "{}{} + Analyzing {}{}{}\n".format(side,e,cCYAN,infile,e)
try:
inn = parseFile(infile)
ansiout += outputs.printAnsi(inn) # Make an option
ansiout += endline
print(ansiout)
if args.htmlout:
outpath = outputs.generateHTML(ansiout,CONFIG["web"]["in_path"],CONFIG["web"]["fqdn"])
print("HTML output is here! {}".format(outpath))
except BrokenPipeError:
print("{}{} {}[!] File not found!{}".format(side,e,cRED,e))
print(endline)
sys.exit(1)
if add2db:
elasticPost(inn)
else:
z = 0
###-- Processing files from local directory --------------------------------
elif directory:
dirlist = rGlob(directory,"*")
print(dirlist)
if len(dirlist) > 0:
ansiout += "{}{} + Analyzing all files in {}{}{}\n".format(side,e,cCYAN,directory,e)
if add2db:
ansiout += "{}{} + Adding Directory {}{}{} to database\n".format(side,e,cCYAN,directory,e)
for ff in dirlist:
try:
inn = parseFile(ff)
ansiout += outputs.printAnsi(inn) # Make an option
if add2db:
elasticPost(inn)
except:
ansiout += "Couldn't process {}...continuing!".format(ff)
continue
ansiout += endline
print(ansiout)
if args.htmlout:
outpath = outputs.generateHTML(ansiout,CONFIG["web"]["in_path"],CONFIG["web"]["fqdn"])
print("HTML output is here! {}".format(outpath))
else:
print("{}{}{} [!] No files found!{}".format(side,e,cRED,e))
sys.exit(1)
###-- Processing single file from url --------------------------------------
elif urlFile:
singleFile, fHeaders, fOutput = rDownload.getSingleFile(urlFile,fpath)
if singleFile == 0:
ansiout += fOutput
print(ansiout)
print(endline)
exit()
ansiout += fOutput
headersList = []
for key, value in fHeaders.items():
headersList.append("{}: {}".format(key, value)) # Create a header list
inn = parseFile(singleFile)
inn["headers"] = headersList # Add the headers
ansiout += outputs.printAnsi(inn) # Make an option
ansiout += endline
print(ansiout)
if args.htmlout:
outpath = outputs.generateHTML(ansiout,CONFIG["web"]["in_path"],CONFIG["web"]["fqdn"])
print("HTML output is here! {}".format(outpath))
if add2db:
inn["url"] = urlFile
elasticPost(inn)
###-- Processing a list of urls with files ---------------------------------
elif urlList:
ansiout += "{}{} + Analyzing all files in {}{}{}".format(side,e,cCYAN,urlList,e)
with open(urlList,"r") as f:
lines = f.readlines()
for l in lines:
try:
l = l.split("\n")[0]
singleFile, fHeaders, fOutput = rDownload.getSingleFile(l,fpath)
ansiout += fOutput
headersList = []
for key, value in fHeaders.items(): # maybe abstract this to a utils.py
headersList.append("{}: {}".format(key, value)) # Create a header list
inn = parseFile(singleFile)
inn["headers"] = headersList # Add the headers
ansiout += outputs.printAnsi(inn) # make an option
if add2db:
inn["url"] = l
elasticPost(inn)
except:
#ansiout += "Error with {}\n".format(l)
continue
ansiout += endline
print(ansiout)
if args.htmlout:
outpath = outputs.generateHTML(ansiout,CONFIG["web"]["in_path"],CONFIG["web"]["fqdn"])
print("HTML output is here! {}".format(outpath))
###-- Processing files from remote directory -------------------------------
elif rDirectory:
fpath = rDownload.parseDir(rDirectory,fpath)
dirlist = rGlob(fpath,"*")
if len(dirlist) > 0:
ansiout += "{}{} + Analyzing all files scraped from {}{}{}\n".format(side,e,cCYAN,rDirectory,e)
for ff in dirlist:
try:
inn = parseFile(ff)
ansiout += outputs.printAnsi(inn)
if add2db:
inn["url"] = rDirectory
elasticPost(inn)
except:
ansiout += "Couldn't process {}...continuing!\n".format(ff)
continue
ansiout += endline
print(ansiout)
if args.htmlout:
outpath = outputs.generateHTML(ansiout,CONFIG["web"]["in_path"],CONFIG["web"]["fqdn"])
print("HTML output is here! {}".format(outpath))
else:
print("{}{}{} [!] No files downloaded!{}".format(side,e,cRED,e))
sys.exit(1)