This repository has been archived by the owner on May 6, 2024. It is now read-only.
forked from wagga40/Zircolite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zircolite.py
executable file
·494 lines (446 loc) · 21.3 KB
/
zircolite.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!python3
# -*- coding: utf-8 -*-
# Std library
import json
import sqlite3
import logging
from sqlite3 import Error
import os
import socket
import subprocess
import argparse
import sys
import time
import random
import string
import signal
import base64
from pathlib import Path
import shutil
from sys import platform as _platform
# Optional imports
# TQDM - Progress bar
try:
from tqdm import tqdm
hasTqdm = True
except ImportError: # If the module is not available creating a fake function that return the first argument value
def tqdm(arg, colour):
return arg
hasTqdm = False
# Requests
try:
import requests
hasRequests = True
except ImportError:
hasRequests = False
# Coloroma - Living in colors
try:
import colorama
from colorama import Fore
colorama.init(autoreset=True)
except ImportError: # If the module is not available creating a fake object
class ColorFake:
def __init__(self):
self.CYAN = ""
self.RED = ""
Fore = ColorFake()
# Jinja2 templating
try:
from jinja2 import Template
hasJinja2 = True
except ImportError: # If the module is not available
hasJinja2 = False
# Trap signal for less ugly exit
def signal_handler(sig, frame):
logging.info("[-] Execution interrupted !")
sys.exit(0)
# SQlite related functions
def createConnection(db):
""" create a database connection to a SQLite database """
conn = None
logging.debug(f"CONNECTING TO : {db}")
try:
conn = sqlite3.connect(db)
conn.row_factory = sqlite3.Row # Allow to get a dict
except Error as e:
logging.error(f"{Fore.RED} [-] {e}")
return conn
def executeQuery(dbConnection, query):
""" Perform a SQL Query with the provided connection """
if dbConnection is not None:
dbHandle = dbConnection.cursor()
logging.debug(f"EXECUTING : {query}")
try:
dbHandle.execute(query)
dbConnection.commit()
return True
except Error as e:
logging.debug(f" [-] {e}")
return False
else:
logging.error(f"{Fore.RED} [-] No connection to Db")
return False
def executeSelectQuery(dbConnection, query):
""" Perform a SQL Query with the provided connection """
if dbConnection is not None:
dbHandle = dbConnection.cursor()
logging.debug(f"EXECUTING : {query}")
try:
data = dbHandle.execute(query)
return data
except Error as e:
logging.debug(f" [-] {e}")
return {}
else:
logging.error(f"{Fore.RED} [-] No connection to Db")
return {}
# Zircolite core functions
def extractEvtx(file, tmpDir, evtx_dumpBinary):
"""
Convert EVTX to JSON using evtx_dump : https://github.com/omerbenamram/evtx.
Drop resulting JSON files in a tmp folder.
@params:
file - Required : EVTX file to convert
tmpDir - Required : directory where the JSON files will be saved
evtx_dumpBinary - Required : evtx_dump binary location
"""
logging.debug(f"EXTRACTING : {file}")
try:
filepath = Path(file)
filename = filepath.name
randString = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(8))
cmd = [evtx_dumpBinary, "--no-confirm-overwrite", "-o", "jsonl", str(file), "-f", tmpDir + "/" + str(filename) + randString + ".json"]
subprocess.call(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except Exception as e:
logging.error(f"{Fore.RED} [-] {e}")
def flattenJSON(file):
"""
Flatten json object with nested keys into a single level.
Returns the flattened json object
@params:
file - Required : A JSON converted EVTX file
"""
logging.debug(f"FLATTENING : {file}")
JSONLine = {}
JSONOutput = []
fieldStmt = ""
def flatten(x, name=''):
nonlocal fieldStmt
# If it is a Dict go deeper
if type(x) is dict:
for a in x:
flatten(x[a], name + a + '.')
else:
# Applying exclusions. Be carefull, the key/value pair is discarded if there is a partial match
if not any(exclusion in name[:-1] for exclusion in fieldExclusions):
# Arrays are not expanded
if type(x) is list:
value = ''.join(str(x))
else:
value = x
# Excluding useless values (e.g. "null"). The value must be an exact match.
if not value in uselessValues:
# Applying field mappings
if name[:-1] in fieldMappings:
key = fieldMappings[name[:-1]]
else:
# Removing all annoying character from field name
key = ''.join(e for e in name[:-1].split(".")[-1] if e.isalnum())
key = key.lower()
JSONLine[key] = value
if name[:-1] not in fullFieldNames:
fullFieldNames[name[:-1]] = key
# Creating the CREATE TABLE SQL statement
if key not in keyDict:
if type(value) is int:
keyDict[key] = ""
fieldStmt += "'" + key + "' INTEGER,\n"
else:
keyDict[key] = ""
fieldStmt += "'" + key + "' TEXT COLLATE NOCASE,\n"
with open(str(file), 'r', encoding='utf-8') as JSONFile:
for line in JSONFile:
flatten(json.loads(line))
JSONOutput.append(JSONLine)
JSONLine = {}
return {"dbFields": fieldStmt, "dbValues": JSONOutput}
def insertData2Db(JSONLine):
columnsStr = ""
valuesStr = ""
for key in sorted(JSONLine.keys()):
columnsStr += "'" + key + "',"
if type(JSONLine[key]) is int:
valuesStr += str(JSONLine[key]) + ", "
else:
valuesStr += "'" + str(JSONLine[key]).replace("'", "''") + "', "
insertStrmt = "INSERT INTO logs (" + columnsStr[:-1] + ") VALUES (" + valuesStr[:-2] + ");"
return executeQuery(dbConnection, insertStrmt)
def executeRule(rule, forwardTo = None):
results = {}
filteredRows = []
counter = 0
if "rule" in rule:
# for each SQL Query in the SIGMA rule
for SQLQuery in rule["rule"]:
data = executeSelectQuery(dbConnection, SQLQuery)
if data != {}:
# Convert to array of dict
rows = [dict(row) for row in data.fetchall()]
if len(rows) > 0:
counter += len(rows)
# Cleaning null/None fields
for row in rows:
match = {k: v for k, v in row.items() if v is not None}
filteredRows.append(match)
if "level" not in rule:
rule["level"] = "unknown"
if "tags" not in rule:
rule["tags"] = []
results = ({"title": rule["title"], "description": rule["description"], "sigma": rule["rule"], "rule_level": rule["level"], "tags": rule["tags"], "count": counter, "matches": filteredRows})
if counter > 0:
logging.debug(f'DETECTED : {rule["title"]} - Matchs : {counter} events')
else:
logging.debug("RULE FORMAT ERROR : rule key Missing")
if filteredRows == []:
return {}
# Forward results to HTTP server
if forwardTo is not None:
sendLogsHTTP(forwardTo, base64.b64encode(json.dumps({"host": socket.gethostname(), "title": rule["title"], "description": rule["description"], "sigma": rule["rule"], "rule_level": rule["level"], "tags": rule["tags"], "count": counter, "matches": filteredRows}).encode('utf-8')).decode('ascii'))
return results
def getOSExternalTools():
""" Determine wich binaries to run depending on host OS : 32Bits is NOT supported for now since evtx_dump is 64bits only"""
if _platform == "linux" or _platform == "linux2":
return "bin/evtx_dump_lin"
elif _platform == "darwin":
return "bin/evtx_dump_mac"
elif _platform == "win32":
return "bin\\evtx_dump_win.exe"
def generateFromTemplate(templateFile, outpoutFilename, data):
""" Use Jinja2 to output data in a specific format """
try:
with open(templateFile, 'r', encoding='utf-8') as tmpl:
template = Template(tmpl.read())
with open(outpoutFilename, 'a', encoding='utf-8') as tpl:
tpl.write(template.render(data=data))
except Exception as e:
logging.error(f"{Fore.RED} [-] Template error, activate debug mode to check for errors")
logging.debug(f" [-] {e}")
def quitOnError(message):
logging.error(message)
sys.exit(1)
def checkIfExists(path, errorMessage):
""" Test if path provided is a file """
if not (Path(path).is_file()):
quitOnError(errorMessage)
def initLogger(debugMode, logFile):
logLevel = logging.INFO
logFormat = "%(asctime)s %(levelname)-8s %(message)s"
if debugMode:
logLevel = logging.DEBUG
logFormat = "%(asctime)s %(levelname)-8s %(module)s:%(lineno)s %(funcName)s %(message)s"
logging.basicConfig(format=logFormat, filename=logFile, level=logLevel, datefmt='%Y-%m-%d %H:%M:%S')
logger = logging.StreamHandler()
logger.setLevel(logging.INFO)
logging.getLogger().addHandler(logger)
return logger
def sendLogsHTTP(host, payload = ""):
""" Just send provided payload to provided web server. Not very clean. Non-async code for now """
try:
r = requests.post(host, headers={'user-agent': 'zircolite/1.1.x'}, data={"data": payload})
logging.debug(f"{Fore.RED} [-] {r}")
return True
except Exception as e:
logging.debug(f"{Fore.RED} [-] {e}")
return False
################################################################
# MAIN()
################################################################
if __name__ == '__main__':
print("""
███████╗██╗██████╗ ██████╗ ██████╗ ██╗ ██╗████████╗███████╗
╚══███╔╝██║██╔══██╗██╔════╝██╔═══██╗██║ ██║╚══██╔══╝██╔════╝
███╔╝ ██║██████╔╝██║ ██║ ██║██║ ██║ ██║ █████╗
███╔╝ ██║██╔══██╗██║ ██║ ██║██║ ██║ ██║ ██╔══╝
███████╗██║██║ ██║╚██████╗╚██████╔╝███████╗██║ ██║ ███████╗
╚══════╝╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚══════╝
""")
# Init Args handling
tmpDir = "tmp-" + ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(8))
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--evtx", help="EVTX log file or directory where EVTX log files are stored in JSON or EVTX format", type=str, required=True)
parser.add_argument("-r", "--ruleset", help="JSON File containing SIGMA rules", type=str, required=True)
parser.add_argument("-c", "--config", help="JSON File containing field mappings and exclusions", type=str, default="config/fieldMappings.json")
parser.add_argument("-o", "--outfile", help="JSON file that will contains all detected events", type=str, default="detected_events.json")
parser.add_argument("-f", "--fileext", help="EVTX file extension", type=str, default="evtx")
parser.add_argument("-t", "--tmpdir", help="Temp directory that will contains EVTX converted as JSON", type=str, default=tmpDir)
parser.add_argument("-k", "--keeptmp", help="Do not remove the Temp directory", action='store_true')
parser.add_argument("-d", "--dbfile", help="Save data as a SQLite Db to the specified file on disk", type=str)
parser.add_argument("-l", "--logfile", help="Log file name", default="zircolite.log", type=str)
parser.add_argument("-j", "--jsononly", help="If logs files are already in JSON lines format ('jsonl' in evtx_dump) ", action='store_true')
parser.add_argument("--remote", help="Forward results to a HTTP server, arg must be the full address e.g http://address:port/uri", type=str)
parser.add_argument("--template", help="If a Jinja2 template is specified it will be used to generated output", type=str, action='append', nargs='+')
parser.add_argument("--templateOutput", help="If a Jinja2 template is specified it will be used to generate a crafted output", type=str, action='append', nargs='+')
parser.add_argument("--fields", help="Show all fields in full format", action='store_true')
parser.add_argument("--debug", help="Activate debug logging", action='store_true')
args = parser.parse_args()
signal.signal(signal.SIGINT, signal_handler)
# Init logging
consoleLogger = initLogger(args.debug, args.logfile)
logging.info("[+] Checking prerequisites")
# Init network
if (args.remote is not None):
if hasRequests:
if not sendLogsHTTP(args.remote):
quitOnError(f"{Fore.RED} [-] Remote host cannot be reached : {args.remote}")
else:
quitOnError(f"{Fore.RED} [-] Requests lib missing, you cannot use '--remote'")
# Cheking for evtx_dump binaries
evtx_dumpBinary = getOSExternalTools()
checkIfExists(evtx_dumpBinary, f"{Fore.RED} [-] Cannot find Evtx_dump")
# Checking ruleset arg
checkIfExists(args.ruleset, f"{Fore.RED} [-] Cannot find ruleset : {args.ruleset}")
# Checking if tmpdir is empty
if (Path(args.tmpdir).is_dir()):
quitOnError(f"{Fore.RED} [-] The Temp working directory exists: {args.tmpdir}. Please remove it or rename it")
# Checking templates args
readyForTemplating = False
if (args.template is not None):
if not hasJinja2:
quitOnError(f"{Fore.RED} [-] You provided a template but Jinja2 module is not installed : {args.template}")
if (args.templateOutput is None) or (len(args.template) != len(args.templateOutput)):
quitOnError(f"{Fore.RED} [-] Number of template ouput must match number of template ")
for template in args.template:
checkIfExists(template[0], f"{Fore.RED} [-] Cannot find template : {template[0]}")
readyForTemplating = True
# Start time counting
start_time = time.time()
# Initialize all configuration dict
fieldExclusions = {} # Will contain fields to discard
fieldMappings = {} # Will contain fields to rename during flattening
uselessValues = {} # Will contain values to discard during flattening
checkIfExists(args.config, f"{Fore.RED} [-] Cannot find mapping file")
with open(args.config, 'r') as fieldMappingsFile:
fieldMappingsDict = json.load(fieldMappingsFile)
fieldExclusions = fieldMappingsDict["exclusions"]
fieldMappings = fieldMappingsDict["mappings"]
uselessValues = fieldMappingsDict["useless"]
# Skipping extracting if jsononly parameter is set
if not args.jsononly:
logging.info(f"[+] Extracting EVTX Using '{args.tmpdir}' directory ")
EVTXPath = Path(args.evtx)
if EVTXPath.is_dir():
# EVTX recursive search in given directory with given file extension
EVTXList = list(EVTXPath.rglob(f"*.{args.fileext}"))
elif EVTXPath.is_file():
EVTXList = [EVTXPath]
else:
quitOnError(f"{Fore.RED} [-] Unable to extract EVTX from submitted path")
if len(EVTXList) > 0:
for evtx in tqdm(EVTXList, colour="yellow"):
extractEvtx(evtx, args.tmpdir, evtx_dumpBinary)
# Set the path for the next step
EVTXJSONList = list(Path(args.tmpdir).rglob("*.json"))
else:
quitOnError(f"{Fore.RED} [-] No EVTX files found. Please verify the directory or the extension with '--fileext'")
else:
EVTXJSONList = list(Path(args.evtx).rglob("*.json"))
logging.info("[+] Processing EVTX")
fieldStmt = ""
valuesStmt = []
fullFieldNames = {}
results = {}
keyDict = {}
if EVTXJSONList == []:
quitOnError(f"{Fore.RED} [-] No JSON files found.")
for evtxJSON in tqdm(EVTXJSONList, colour="yellow"):
if os.stat(evtxJSON).st_size != 0:
results = flattenJSON(evtxJSON)
fieldStmt += results["dbFields"]
valuesStmt += results["dbValues"]
if args.fields:
logging.info("[+] Saving fields to fields.json")
with open("fields.json", 'w', encoding='utf-8') as f:
json.dump(fullFieldNames, f, indent=4)
sys.exit(0)
logging.info("[+] Creating model")
dbConnection = createConnection(":memory:")
createTableStmt = "CREATE TABLE logs ( row_id INTEGER, " + fieldStmt + " PRIMARY KEY(row_id AUTOINCREMENT) );"
logging.debug(" CREATE : " + createTableStmt.replace('\n', ' ').replace('\r', ''))
if not executeQuery(dbConnection, createTableStmt):
quitOnError(f"{Fore.RED} [-] Unable to create table")
del createTableStmt
logging.info("[+] Inserting data")
for JSONLine in tqdm(valuesStmt, colour="yellow"):
insertData2Db(JSONLine)
# Creating index to speed up queries
executeQuery(dbConnection, 'CREATE INDEX "idx_eventid" ON "logs" ("eventid");')
logging.info("[+] Cleaning unused objects")
del valuesStmt
# Unload In memory DB to disk. Done here to permit debug in case of ruleset execution error
if args.dbfile is not None:
logging.info("[+] Saving working data to disk as a SQLite DB")
onDiskDb = sqlite3.connect(args.dbfile)
dbConnection.backup(onDiskDb)
onDiskDb.close()
logging.info(f"[+] Loading ruleset from : {args.ruleset}")
with open(args.ruleset) as f:
ruleset = json.load(f)
rulesetSize = len(ruleset)
logging.info(f"[+] Executing ruleset - {str(rulesetSize)} rules")
# Results are writen upon detection to allow analysis during execution and to avoid loosing results in case of error.
fullResults = []
with open(args.outfile, 'w', encoding='utf-8') as f:
if hasTqdm: # If tqdm is installed
with tqdm(ruleset, colour="yellow") as ruleBar:
f.write('[')
for rule in ruleBar: # for each rule in ruleset
ruleResults = executeRule(rule, args.remote)
if ruleResults != {}:
ruleBar.write(f'{Fore.CYAN} - {ruleResults["title"]} - Matchs : {ruleResults["count"]} events')
# To avoid printing this one on stdout but in the logs...
consoleLogger.setLevel(logging.ERROR)
logging.info(f'{Fore.CYAN} - {ruleResults["title"]} - Matchs : {ruleResults["count"]} events')
consoleLogger.setLevel(logging.INFO)
# Store results for templating
if readyForTemplating:
fullResults.append(ruleResults)
# Output to default json file
try:
json.dump(ruleResults, f, indent=4, ensure_ascii=False)
f.write(',\n')
except Exception as e:
logging.error(f"{Fore.RED} [-] Error saving some results : {e}")
logging.debug(f" [-] {e}")
f.write('{}]')
else:
f.write('[')
for rule in ruleset:
ruleResults = executeRule(rule)
if ruleResults != {}:
logging.info(f'{Fore.CYAN} - {ruleResults["title"]} - Matchs : {ruleResults["count"]} events')
# Store results for templating
if readyForTemplating:
fullResults.append(ruleResults)
# Output to default json file
try:
json.dump(ruleResults, f, indent=4, ensure_ascii=False)
f.write(',\n')
except Exception as e:
logging.error(f"{Fore.RED} [-] Error saving some results : {e}")
logging.debug(f" [-] {e}")
f.write('{}]')
logging.info(f"[+] Results written in : {args.outfile}")
# Templating
if readyForTemplating and fullResults != []:
for template, templateOutput in zip(args.template, args.templateOutput):
logging.info(f'[+] Applying template "{template[0]}", outputting to : {templateOutput[0]}')
generateFromTemplate(template[0], templateOutput[0], fullResults)
# Removing Working directory containing logs as json
if not args.keeptmp:
logging.info("[+] Cleaning")
try:
shutil.rmtree(args.tmpdir)
except OSError as e:
logging.error(f"{Fore.RED} [-] Error during cleanup {e}")
dbConnection.close()
logging.info(f"\nFinished in {int((time.time() - start_time))} seconds")