-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathzircolite.py
executable file
·1538 lines (1347 loc) · 73.8 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!python3
# Standard libs
import argparse
import base64
import chardet
import csv
import functools
import hashlib
import logging
import multiprocessing as mp
import os
import platform
import random
import re
import shutil
import signal
import sqlite3
import string
import subprocess
import sys
import time
from pathlib import Path
from sqlite3 import Error
# External libs (Mandatory)
import orjson as json
import xxhash
from colorama import Fore
from tqdm import tqdm
from RestrictedPython import compile_restricted
from RestrictedPython import safe_builtins
from RestrictedPython import limited_builtins
from RestrictedPython import utility_builtins
from RestrictedPython.Eval import default_guarded_getiter
from RestrictedPython.Guards import guarded_iter_unpack_sequence
# External libs (Optional)
updateDisabled = False
try:
import requests
except ImportError:
updateDisabled = True
sigmaConversionDisabled = False
try:
from sigma.collection import SigmaCollection
from sigma.backends.sqlite import sqlite
from sigma.processing.resolver import ProcessingPipelineResolver
from sigma.plugins import InstalledSigmaPlugins
import yaml
except ImportError:
sigmaConversionDisabled = True
pyevtxDisabled = False
try:
from evtx import PyEvtxParser
except ImportError:
pyevtxDisabled = True
jinja2Disabled = False
try:
from jinja2 import Template
except ImportError:
jinja2Disabled = True
xmlImportDisabled = False
try:
from lxml import etree
except ImportError:
xmlImportDisabled = True
def signal_handler(sig, frame):
print("[-] Execution interrupted !")
sys.exit(0)
def quitOnError(message, logger=None):
logger.error(message)
sys.exit(1)
def checkIfExists(path, errorMessage, logger=None):
"""Test if path provided is a file"""
if not (Path(path).is_file()):
quitOnError(errorMessage, logger)
def initLogger(debugMode, logFile=None):
fileLogLevel = logging.INFO
fileLogFormat = "%(asctime)s %(levelname)-8s %(message)s"
if debugMode:
fileLogLevel = logging.DEBUG
fileLogFormat = "%(asctime)s %(levelname)-8s %(module)s:%(lineno)s %(funcName)s %(message)s"
if logFile is not None:
logging.basicConfig(format=fileLogFormat, filename=logFile, level=fileLogLevel, datefmt='%Y-%m-%d %H:%M:%S')
logger = logging.StreamHandler()
formatter = logging.Formatter('%(message)s')
logger.setFormatter(formatter)
logger.setLevel(logging.INFO)
logging.getLogger().addHandler(logger)
else:
logging.basicConfig(format='%(message)s', level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S')
return logging.getLogger()
class templateEngine:
def __init__(self, logger=None, template=[], templateOutput=[], timeField=""):
self.logger = logger or logging.getLogger(__name__)
self.template = template
self.templateOutput = templateOutput
self.timeField = timeField
def generateFromTemplate(self, templateFile, outputFilename, data):
""" Use Jinja2 to output data in a specific format """
try:
tmpl = open(templateFile, 'r', encoding='utf-8')
template = Template(tmpl.read())
with open(outputFilename, 'a', encoding='utf-8') as tpl:
tpl.write(template.render(data=data, timeField=self.timeField))
except Exception as e:
self.logger.error(f"{Fore.RED} [-] Template error, activate debug mode to check for errors{Fore.RESET}")
self.logger.debug(f" [-] {e}")
def run(self, data):
for template, templateOutput in zip(self.template, self.templateOutput):
self.logger.info(f'[+] Applying template "{template[0]}", outputting to : {templateOutput[0]}')
self.generateFromTemplate(template[0], templateOutput[0], data)
class JSONFlattener:
""" Perform JSON Flattening """
def __init__(self, configFile, logger=None, timeAfter="1970-01-01T00:00:00", timeBefore="9999-12-12T23:59:59", timeField=None, hashes=False, args_config=None):
self.logger = logger or logging.getLogger(__name__)
self.keyDict = {}
self.fieldStmt = ""
self.valuesStmt = []
self.timeAfter = timeAfter
self.timeBefore = timeBefore
self.timeField = timeField
self.hashes = hashes
self.args_config = args_config
self.JSONArray = args_config.json_array_input
# Initialize the cache for compiled code
self.compiled_code_cache = {}
# Convert the argparse.Namespace to a dictionary
args_dict = vars(args_config)
# Find the chosen input format
self.chosen_input = next((key for key, value in args_dict.items() if "_input" in key and value), None)
if self.chosen_input is None:
self.chosen_input = "evtx_input" # Since evtx is the default input, we force it no chosen input has been found
with open(configFile, 'r', encoding='UTF-8') as fieldMappingsFile:
self.fieldMappingsDict = json.loads(fieldMappingsFile.read())
self.fieldExclusions = self.fieldMappingsDict["exclusions"]
self.fieldMappings = self.fieldMappingsDict["mappings"]
self.uselessValues = self.fieldMappingsDict["useless"]
self.aliases = self.fieldMappingsDict["alias"]
self.fieldSplitList = self.fieldMappingsDict["split"]
self.transforms = self.fieldMappingsDict["transforms"]
self.transforms_enabled = self.fieldMappingsDict["transforms_enabled"]
# Define the authorized BUILTINS for Resticted Python
def default_guarded_getitem(ob, index):
return ob[index]
default_guarded_getattr = getattr
self.RestrictedPython_BUILTINS = {
'__name__': 'script',
"_getiter_": default_guarded_getiter,
'_getattr_': default_guarded_getattr,
'_getitem_': default_guarded_getitem,
'base64': base64,
're': re,
'chardet': chardet,
'_iter_unpack_sequence_': guarded_iter_unpack_sequence
}
self.RestrictedPython_BUILTINS.update(safe_builtins)
self.RestrictedPython_BUILTINS.update(limited_builtins)
self.RestrictedPython_BUILTINS.update(utility_builtins)
def run(self, file):
"""
Flatten json object with nested keys into a single level.
Returns the flattened json object
"""
self.logger.debug(f"FLATTENING : {file}")
JSONLine = {}
JSONOutput = []
fieldStmt = ""
def transformValue(code, param):
try:
# Check if the code has already been compiled
if code in self.compiled_code_cache:
byte_code = self.compiled_code_cache[code]
else:
# Compile the code and store it in the cache
byte_code = compile_restricted(code, filename='<inline code>', mode='exec')
self.compiled_code_cache[code] = byte_code
# Prepare the execution environment
TransformFunction = {}
exec(byte_code, self.RestrictedPython_BUILTINS, TransformFunction)
return TransformFunction["transform"](param)
except Exception as e:
self.logger.debug(f"ERROR: Couldn't apply transform: {e}")
return param # Return the original parameter if transform fails
def flatten(x, name=''):
# Sample EVTX event : {"Event":{"#attributes":{"xmlns":"http://schemas.microsoft.com/win/2004/08/events/event"},"System":{"Provider":{"#attributes":{"Name":"Microsoft-Windows-Sysmon","Guid":"5770385F-C22A-43E0-BF4C-06F5698FFBD9"}},"EventID":16,"Version":3,"Level":4,"Task":16,"Opcode":0,"Keywords":"0x8000000000000000","TimeCreated":{"#attributes":{"SystemTime":"2021-03-03T12:47:27.371669Z"}},"EventRecordID":1,"Correlation":null,"Execution":{"#attributes":{"ProcessID":5132,"ThreadID":6404}},"Channel":"Microsoft-Windows-Sysmon/Operational","Computer":"DESKTOP-ET1DJSR","Security":{"#attributes":{"UserID":"S-1-5-21-1250304854-3630730510-2981668747-1001"}}},"EventData":{"UtcTime":"2021-03-03 12:47:27.369","Configuration":"C:\\Users\\user\\Downloads\\sysmonconfig-export.xml","ConfigurationFileHash":"SHA256=EA5133261F8C5D31A30DD852A05B90E804103837310C14A69747BA8367D6CDB5"}}}
nonlocal fieldStmt
# If it is a Dict go deeper
if isinstance(x, dict):
for a in x:
flatten(x[a], name + a + '.')
else:
# Applying exclusions. Be careful, the key/value pair is discarded if there is a partial match
if not any(exclusion in name[:-1] for exclusion in self.fieldExclusions):
# Arrays are not expanded
if isinstance(x, list):
value = ''.join(str(x))
else:
value = x
# Excluding useless values (e.g. "null"). The value must be an exact match.
if value not in self.uselessValues:
# Applying field mappings
rawFieldName = name[:-1]
key = self.fieldMappings.get(rawFieldName, ''.join(e for e in rawFieldName.split(".")[-1] if e.isalnum()))
# Preparing aliases (work on original field name and Mapped field name)
keys = [key]
if key in self.aliases:
keys.append(self.aliases[key])
if rawFieldName in self.aliases:
keys.append(self.aliases[rawFieldName])
# Applying field transforms
keysThatNeedTransformedValues = []
transformedValuesByKeys = {}
if self.transforms_enabled:
for fieldName in [key, rawFieldName]:
if fieldName in self.transforms:
for transform in self.transforms[fieldName]:
if transform["enabled"] and self.chosen_input in transform["source_condition"]:
transformCode = transform["code"]
# If the transform rule ask for a dedicated alias
if transform["alias"]:
keys.append(transform["alias_name"])
keysThatNeedTransformedValues.append(transform["alias_name"])
transformedValuesByKeys[transform["alias_name"]] = transformValue(transformCode, value)
else:
value = transformValue(transformCode, value)
# Applying field splitting
fieldsToSplit = []
if rawFieldName in self.fieldSplitList:
fieldsToSplit.append(rawFieldName)
if key in self.fieldSplitList:
fieldsToSplit.append(key)
if fieldsToSplit:
for field in fieldsToSplit:
try:
splittedFields = value.split(self.fieldSplitList[field]["separator"])
for splittedField in splittedFields:
k, v = splittedField.split(self.fieldSplitList[field]["equal"])
keyLower = k.lower()
JSONLine[k] = v
if keyLower not in self.keyDict:
self.keyDict[keyLower] = k
fieldStmt += f"'{k}' TEXT COLLATE NOCASE,\n"
except Exception as e:
self.logger.debug(f"ERROR : Couldn't apply field splitting, value(s) {str(splittedFields)} : {e}")
# Applying aliases
for key in keys:
if key in keysThatNeedTransformedValues:
JSONLine[key] = transformedValuesByKeys[key]
else:
JSONLine[key] = value
# Creating the CREATE TABLE SQL statement
keyLower = key.lower()
if keyLower not in self.keyDict:
self.keyDict[keyLower] = key
fieldStmt += f"'{key}' {'INTEGER' if isinstance(value, int) else 'TEXT COLLATE NOCASE'},\n"
# If filesize is not zero
if os.stat(file).st_size != 0:
with open(str(file), 'r', encoding='utf-8') as JSONFile:
filename = os.path.basename(file)
logs = JSONFile
# If the file is a json array
if self.JSONArray:
try:
logs = json.loads(JSONFile.read())
except Exception as e:
self.logger.debug(f'JSON ARRAY ERROR : {e}')
logs = []
for line in logs:
try:
if self.JSONArray:
dictToFlatten = line
else:
dictToFlatten = json.loads(line)
dictToFlatten["OriginalLogfile"] = filename
if self.hashes:
dictToFlatten["OriginalLogLinexxHash"] = xxhash.xxh64_hexdigest(line[:-1])
flatten(dictToFlatten)
except Exception as e:
self.logger.debug(f'JSON ERROR : {e}')
# Handle timestamp filters
if ((self.timeAfter != "1970-01-01T00:00:00" or self.timeBefore != "9999-12-12T23:59:59")
and (self.timeField in JSONLine)):
try:
timestamp = time.strptime(JSONLine[self.timeField].split(".")[0].replace("Z",""), '%Y-%m-%dT%H:%M:%S')
if timestamp > self.timeAfter and timestamp < self.timeBefore:
JSONOutput.append(JSONLine)
except Exception:
JSONOutput.append(JSONLine)
else:
JSONOutput.append(JSONLine)
JSONLine = {}
return {"dbFields": fieldStmt, "dbValues": JSONOutput}
def runAll(self, EVTXJSONList):
for evtxJSON in tqdm(EVTXJSONList, colour="yellow"):
if os.stat(evtxJSON).st_size != 0:
results = self.run(evtxJSON)
self.fieldStmt += results["dbFields"]
self.valuesStmt += results["dbValues"]
class zirCore:
""" Load data into database and apply detection rules """
def __init__(self, config, logger=None, noOutput=False, timeAfter="1970-01-01T00:00:00", timeBefore="9999-12-12T23:59:59", limit=-1, csvMode=False, timeField=None, hashes=False, dbLocation=":memory:", delimiter=";"):
self.logger = logger or logging.getLogger(__name__)
self.dbConnection = self.createConnection(dbLocation)
self.fullResults = []
self.ruleset = {}
self.noOutput = noOutput
self.timeAfter = timeAfter
self.timeBefore = timeBefore
self.config = config
self.limit = limit
self.csvMode = csvMode
self.timeField = timeField
self.hashes = hashes
self.delimiter = delimiter
self.first_json_output = True # To manage commas in JSON output
def close(self):
self.dbConnection.close()
def createConnection(self, db):
""" create a database connection to a SQLite database """
conn = None
self.logger.debug(f"CONNECTING TO : {db}")
try:
if db == ':memory:':
conn = sqlite3.connect(db, isolation_level=None)
conn.execute('PRAGMA journal_mode = MEMORY;')
conn.execute('PRAGMA synchronous = OFF;')
conn.execute('PRAGMA temp_store = MEMORY;')
else:
conn = sqlite3.connect(db)
conn.row_factory = sqlite3.Row # Allows to get a dict
def udf_regex(x, y):
if y is None:
return 0
if re.search(x, y):
return 1
else:
return 0
conn.create_function('regexp', 2, udf_regex) # Allows to use regex in SQlite
except Error as e:
self.logger.error(f"{Fore.RED} [-] {e}")
return conn
def createDb(self, fieldStmt):
createTableStmt = f"CREATE TABLE logs ( row_id INTEGER, {fieldStmt} PRIMARY KEY(row_id AUTOINCREMENT) );"
self.logger.debug(f" CREATE : {createTableStmt}")
if not self.executeQuery(createTableStmt):
self.logger.error(f"{Fore.RED} [-] Unable to create table{Fore.RESET}")
sys.exit(1)
def createIndex(self):
self.executeQuery('CREATE INDEX "idx_eventid" ON "logs" ("eventid");')
def executeQuery(self, query):
""" Perform a SQL Query with the provided connection """
if self.dbConnection is not None:
dbHandle = self.dbConnection.cursor()
self.logger.debug(f"EXECUTING : {query}")
try:
dbHandle.execute(query)
self.dbConnection.commit()
return True
except Error as e:
self.logger.debug(f" [-] {e}")
return False
else:
self.logger.error(f"{Fore.RED} [-] No connection to Db{Fore.RESET}")
return False
def executeSelectQuery(self, query):
"""
Execute a SELECT SQL query and return the results as a list of dictionaries.
"""
if self.dbConnection is None:
self.logger.error(f"{Fore.RED} [-] No connection to Db{Fore.RESET}")
return []
try:
cursor = self.dbConnection.cursor()
self.logger.debug(f"Executing SELECT query: {query}")
cursor.execute(query)
rows = cursor.fetchall()
# Convert rows to list of dictionaries
result = [dict(row) for row in rows]
return result
except sqlite3.Error as e:
self.logger.debug(f" [-] SQL query error: {e}")
return []
def loadDbInMemory(self, db):
""" In db only mode it is possible to restore an on disk Db to avoid EVTX extraction and flattening """
dbfileConnection = self.createConnection(db)
dbfileConnection.backup(self.dbConnection)
dbfileConnection.close()
def escape_identifier(self, identifier):
"""Escape SQL identifiers like table or column names."""
return identifier.replace("\"", "\"\"")
def insertData2Db(self, JSONLine):
"""Build a parameterized INSERT INTO query and insert data into the database."""
columns = JSONLine.keys()
columnsEscaped = ', '.join([self.escape_identifier(col) for col in columns])
placeholders = ', '.join(['?'] * len(columns))
values = []
for col in columns:
value = JSONLine[col]
if isinstance(value, int):
# Check if value exceeds SQLite INTEGER limits
if abs(value) > 9223372036854775807:
value = str(value) # Convert to string
values.append(value)
insertStmt = f'INSERT INTO logs ({columnsEscaped}) VALUES ({placeholders})'
try:
self.dbConnection.execute(insertStmt, values)
return True
except Exception as e:
self.logger.debug(f" [-] {e}")
return False
def insertFlattenedJSON2Db(self, flattenedJSON):
for JSONLine in tqdm(flattenedJSON, colour="yellow"):
self.insertData2Db(JSONLine)
self.createIndex()
def saveFlattenedJSON2File(self, flattenedJSON, outputFile):
with open(outputFile, 'w', encoding='utf-8') as file:
for JSONLine in tqdm(flattenedJSON, colour="yellow"):
file.write(f'{json.dumps(JSONLine).decode("utf-8")}\n')
def saveDbToDisk(self, dbFilename):
self.logger.info("[+] Saving working data to disk as a SQLite DB")
onDiskDb = sqlite3.connect(dbFilename)
self.dbConnection.backup(onDiskDb)
onDiskDb.close()
def executeRule(self, rule):
"""
Execute a single Sigma rule against the database and return the results.
"""
if "rule" not in rule:
self.logger.debug("RULE FORMAT ERROR: 'rule' key missing")
return {}
# Set default values for missing rule keys
rule_level = rule.get("level", "unknown")
tags = rule.get("tags", [])
filename = rule.get("filename", "")
description = rule.get("description", "")
title = rule.get("title", "Unnamed Rule")
rule_id = rule.get("id", "")
sigma_queries = rule["rule"]
filteredRows = []
# Process each SQL query in the rule
for SQLQuery in sigma_queries:
data = self.executeSelectQuery(SQLQuery)
if data:
if self.csvMode:
# Clean values for CSV output
cleaned_rows = [
{k: str(v).replace("\n", "").replace("\r", "").replace("None", "") for k, v in dict(row).items()}
for row in data
]
else:
# Remove None values
cleaned_rows = [
{k: v for k, v in dict(row).items() if v is not None}
for row in data
]
filteredRows.extend(cleaned_rows)
if filteredRows:
results = {
"title": title,
"id": rule_id,
"description": description.replace("\n", "").replace("\r", "") if self.csvMode else description,
"sigmafile": filename,
"sigma": sigma_queries,
"rule_level": rule_level,
"tags": tags,
"count": len(filteredRows),
"matches": filteredRows
}
self.logger.debug(f'DETECTED: {title} - Matches: {len(filteredRows)} events')
return results
else:
return {}
def loadRulesetFromFile(self, filename, ruleFilters):
try:
with open(filename, encoding='utf-8') as f:
self.ruleset = json.loads(f.read())
self.applyRulesetFilters(ruleFilters)
except Exception as e:
self.logger.error(f"{Fore.RED} [-] Loading JSON ruleset failed, are you sure it is a valid JSON file ? : {e}{Fore.RESET}")
def loadRulesetFromVar(self, ruleset, ruleFilters):
self.ruleset = ruleset
self.applyRulesetFilters(ruleFilters)
def applyRulesetFilters(self, ruleFilters=None):
# Remove empty rule and remove filtered rules
self.ruleset = list(filter(None, self.ruleset))
if ruleFilters is not None:
self.ruleset = [rule for rule in self.ruleset if not any(ruleFilter in rule["title"] for ruleFilter in ruleFilters)]
def ruleLevelPrintFormatter(self, level, orgFormat=Fore.RESET):
if level == "informational":
return f'{Fore.WHITE}{level}{orgFormat}'
if level == "low":
return f'{Fore.GREEN}{level}{orgFormat}'
if level == "medium":
return f'{Fore.YELLOW}{level}{orgFormat}'
if level == "high":
return f'{Fore.MAGENTA}{level}{orgFormat}'
if level == "critical":
return f'{Fore.RED}{level}{orgFormat}'
def executeRuleset(self, outFile, writeMode='w', showAll=False,
KeepResults=False, lastRuleset=False):
"""
Execute all rules in the ruleset and handle output.
"""
csvWriter = None
is_json_mode = not self.csvMode
# Prepare output file handle if needed
fileHandle = None
if not self.noOutput:
# Open file in text mode since we will write decoded strings
fileHandle = open(outFile, writeMode, encoding='utf-8', newline='')
if is_json_mode and writeMode != 'a':
fileHandle.write('[') # Start JSON array
# Iterate over rules in the ruleset
with tqdm(self.ruleset, colour="yellow") as ruleBar:
for rule in ruleBar:
# Show all rules if showAll is True
if showAll and "title" in rule:
rule_title = rule["title"]
rule_level = rule.get("level", "unknown")
formatted_level = self.ruleLevelPrintFormatter(rule_level, Fore.BLUE)
ruleBar.write(f'{Fore.BLUE} - {rule_title} [{formatted_level}]{Fore.RESET}')
# Execute the rule
ruleResults = self.executeRule(rule)
if not ruleResults:
continue # No matches, skip to next rule
# Apply limit if set
if self.limit != -1 and ruleResults["count"] > self.limit:
continue # Exceeds limit, skip this result
# Write progress message
rule_title = ruleResults["title"]
rule_level = ruleResults.get("rule_level", "unknown")
formatted_level = self.ruleLevelPrintFormatter(rule_level, Fore.CYAN)
rule_count = ruleResults["count"]
ruleBar.write(f'{Fore.CYAN} - {rule_title} [{formatted_level}] : {rule_count} events{Fore.RESET}')
# Store results if needed
if KeepResults:
self.fullResults.append(ruleResults)
# Handle output to file
if not self.noOutput:
if self.csvMode:
# Initialize CSV writer if not already done
if csvWriter is None:
fieldnames = ["rule_title", "rule_description", "rule_level", "rule_count"] + list(ruleResults["matches"][0].keys())
csvWriter = csv.DictWriter(fileHandle, delimiter=self.delimiter, fieldnames=fieldnames)
csvWriter.writeheader()
# Write matches to CSV
for data in ruleResults["matches"]:
dictCSV = {
"rule_title": ruleResults["title"],
"rule_description": ruleResults["description"],
"rule_level": ruleResults["rule_level"],
"rule_count": ruleResults["count"],
**data
}
csvWriter.writerow(dictCSV)
else:
# Write results as JSON using orjson
try:
# Handle commas between JSON objects
if not self.first_json_output:
fileHandle.write(',\n')
else:
self.first_json_output = False
# Serialize ruleResults to JSON bytes with indentation
json_bytes = json.dumps(ruleResults, option=json.OPT_INDENT_2)
# Write the decoded JSON string to the file
fileHandle.write(json_bytes.decode('utf-8'))
except Exception as e:
self.logger.error(f"Error saving some results: {e}")
# Close output file handle if needed
if not self.noOutput:
if is_json_mode and lastRuleset:
fileHandle.write(']') # Close JSON array
fileHandle.close()
def run(self, EVTXJSONList, Insert2Db=True, saveToFile=False, args_config=None):
self.logger.info("[+] Processing events")
flattener = JSONFlattener(configFile=self.config, timeAfter=self.timeAfter, timeBefore=self.timeBefore, timeField=self.timeField, hashes=self.hashes, args_config=args_config)
flattener.runAll(EVTXJSONList)
if saveToFile:
filename = f"flattened_events_{''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(4))}.json"
self.logger.info(f"[+] Saving flattened JSON to : {filename}")
self.saveFlattenedJSON2File(flattener.valuesStmt, filename)
if Insert2Db:
self.logger.info("[+] Creating model")
self.createDb(flattener.fieldStmt)
self.logger.info("[+] Inserting data")
self.insertFlattenedJSON2Db(flattener.valuesStmt)
self.logger.info("[+] Cleaning unused objects")
else:
return flattener.keyDict
del flattener
class evtxExtractor:
def __init__(self, logger=None, providedTmpDir=None, coreCount=None, useExternalBinaries=True, binPath = None, xmlLogs=False, sysmon4linux=False, auditdLogs=False, encoding=None, evtxtract=False, csvInput=False):
self.logger = logger or logging.getLogger(__name__)
if Path(str(providedTmpDir)).is_dir():
self.tmpDir = f"tmp-{self.randString()}"
self.logger.error(f"{Fore.RED} [-] Provided directory already exists using '{self.tmpDir}' instead{Fore.RESET}")
else:
self.tmpDir = providedTmpDir or f"tmp-{self.randString()}"
os.mkdir(self.tmpDir)
self.cores = coreCount or os.cpu_count()
self.useExternalBinaries = useExternalBinaries
self.sysmon4linux = sysmon4linux
self.xmlLogs = xmlLogs
self.auditdLogs = auditdLogs
self.evtxtract = evtxtract
self.csvInput = csvInput
# Sysmon 4 Linux default encoding is ISO-8859-1, Auditd is UTF-8
if not encoding and sysmon4linux:
self.encoding = "ISO-8859-1"
elif not encoding and (auditdLogs or evtxtract or xmlLogs):
self.encoding = "utf-8"
else:
self.encoding = encoding
self.evtxDumpCmd = self.getOSExternalTools(binPath)
def randString(self):
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(8))
def getOSExternalTools(self, binPath):
""" Determine which binaries to run depending on host OS and architecture: 32Bits is NOT supported for now since evtx_dump is 64bits only"""
if binPath is None:
if platform.system() == "Linux":
# Check for ARM architecture
if platform.machine().startswith('arm') or platform.machine().startswith('aarch'):
return "bin/evtx_dump_lin_arm"
else:
# Default to x64 architecture
return "bin/evtx_dump_lin"
elif platform.system() == "Darwin":
return "bin/evtx_dump_mac"
elif platform.system() == "Windows":
return "bin\\evtx_dump_win.exe"
else:
return binPath
def runUsingBindings(self, file):
"""
Convert EVTX to JSON using evtx_dump bindings (slower)
Drop resulting JSON files in a tmp folder.
"""
if not self.useExternalBinaries:
try:
filepath = Path(file)
filename = filepath.name
parser = PyEvtxParser(str(filepath))
with open(f"{self.tmpDir}/{str(filename)}-{self.randString()}.json", "w", encoding="utf-8") as f:
for record in parser.records_json():
f.write(f'{json.dumps(json.loads(record["data"])).decode("utf-8")}\n')
except Exception as e:
self.logger.error(f"{Fore.RED} [-] Cannot use PyEvtxParser : {e}{Fore.RESET}")
else:
self.logger.error(f"{Fore.RED} [-] Cannot use PyEvtxParser and evtx_dump is disabled or missing{Fore.RESET}")
def getTime(self, line):
timestamp = line.replace('msg=audit(','').replace('):','').split(':')
timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(float(timestamp[0])))
return timestamp
def auditdLine2JSON(self, auditdLine):
"""
Convert auditd logs to JSON : code from https://github.com/csark/audit2json
"""
event = {}
# According to auditd specs https://github.com/linux-audit/audit-documentation/wiki/SPEC-Audit-Event-Enrichment
# a GS ASCII character, 0x1D, will be inserted to separate original and translated fields
# Best way to deal with it is to remove it.
attributes = auditdLine.replace('\x1d',' ').split(' ')
for attribute in attributes:
if 'msg=audit' in attribute:
event['timestamp'] = self.getTime(attribute)
else:
try:
attribute = attribute.replace('msg=','').replace('\'','').replace('"','').split('=')
event[attribute[0]] = attribute[1].rstrip()
except Exception:
pass
if "host" not in event:
event['host'] = 'offline'
return event
def SysmonXMLLine2JSON(self, xmlLine):
"""
Remove syslog header and convert xml data to json : code from ZikyHD (https://github.com/ZikyHD)
"""
if 'Event' not in xmlLine:
return None
xmlLine = "<Event>" + xmlLine.split("<Event>")[1]
try: # isolate individual line parsing errors
root = etree.fromstring(xmlLine)
return self.xml2dict(root)
except Exception as ex:
self.logger.debug(f"Unable to parse line \"{xmlLine}\": {ex}")
return None
def XMLLine2JSON(self, xmlLine):
"""
Remove "Events" header and convert xml data to json : code from ZikyHD (https://github.com/ZikyHD)
"""
if '<Event ' not in xmlLine:
return None
try: # isolate individual line parsing errors
root = etree.fromstring(xmlLine)
return self.xml2dict(root, u'{http://schemas.microsoft.com/win/2004/08/events/event}')
except Exception as ex:
self.logger.debug(f"Unable to parse line \"{xmlLine}\": {ex}")
return None
def xml2dict(self, eventRoot, ns=u'http://schemas.microsoft.com/win/2004/08/events/event'):
def cleanTag(tag, ns):
if ns in tag:
return tag[len(ns):]
return tag
child = {"#attributes": {"xmlns": ns}}
for appt in eventRoot.getchildren():
nodename = cleanTag(appt.tag,ns)
nodevalue = {}
for elem in appt.getchildren():
cleanedTag = cleanTag(elem.tag,ns)
if not elem.text:
text = ""
else:
try:
text = int(elem.text)
except Exception:
text = elem.text
if cleanedTag == 'Data':
childnode = elem.get("Name")
elif cleanedTag == 'Qualifiers':
text = elem.text
else:
childnode = cleanedTag
if elem.attrib:
text = {"#attributes": dict(elem.attrib)}
obj={str(childnode):text}
nodevalue = {**nodevalue, **obj}
node = {str(nodename): nodevalue}
child = {**child, **node}
event = { "Event": child }
return event
def Logs2JSON(self, func, datasource, outfile, isFile=True):
"""
Use multiprocessing to convert supported log formats to JSON
"""
if isFile:
with open(datasource, "r", encoding=self.encoding) as fp:
data = fp.readlines()
else :
data = datasource.split("\n")
pool = mp.Pool(self.cores)
result = pool.map(func, data)
pool.close()
pool.join()
with open(outfile, "w", encoding="UTF-8") as fp:
for element in result:
if element is not None:
fp.write(json.dumps(element).decode("utf-8") + '\n')
def csv2JSON(self, CSVPath, JSONPath):
"""
Convert CSV Logs to JSON
"""
with open(CSVPath, encoding='utf-8') as CSVFile:
csvReader = csv.DictReader(CSVFile)
with open(JSONPath, 'w', encoding='utf-8') as JSONFile:
for row in csvReader:
JSONFile.write(json.dumps(row).decode("utf-8") + '\n')
def evtxtract2JSON(self, file, outfile):
"""
Convert EXVTXtract Logs to JSON using xml2dict and "dumps" it to a file
"""
# Load file as a string to add enclosing document since XML doesn't support multiple documents
with open(file, "r", encoding=self.encoding) as fp:
data = fp.read()
# Remove all non UTF-8 characters
data = bytes(data.replace('\x00','').replace('\x0B',''), 'utf-8').decode('utf-8', 'ignore')
data = f'<evtxtract>\n{data}\n</evtxtract>'
# Load the XML file
parser = etree.XMLParser(recover=True) # Recover=True allows the parser to ignore bad characters
root = etree.fromstring(data, parser=parser)
with open(outfile, "w", encoding="UTF-8") as fp:
for event in root.getchildren():
if "Event" in event.tag:
extractedEvent = self.xml2dict(event, u'{http://schemas.microsoft.com/win/2004/08/events/event}')
fp.write(json.dumps(extractedEvent).decode("utf-8") + '\n')
def run(self, file):
"""
Convert Logs to JSON
Drop resulting JSON files in a tmp folder.
"""
self.logger.debug(f"EXTRACTING : {file}")
filename = Path(file).name
outputJSONFilename = f"{self.tmpDir}/{str(filename)}-{self.randString()}.json"
try:
# Auditd or Sysmon4Linux logs
if self.sysmon4linux or self.auditdLogs:
func = self.SysmonXMLLine2JSON if self.sysmon4linux else self.auditdLine2JSON
self.Logs2JSON(func, str(file), outputJSONFilename)
# XML logs
elif self.xmlLogs:
with open(str(file), 'r', encoding="utf-8") as XMLFile:
data = XMLFile.read().replace("\n","").replace("</Event>","</Event>\n").replace("<Event ","\n<Event ")
self.Logs2JSON(self.XMLLine2JSON, data, outputJSONFilename, isFile=False)
# EVTXtract
elif self.evtxtract:
self.evtxtract2JSON(str(file), outputJSONFilename)
# CSV
elif self.csvInput:
self.csv2JSON(str(file), outputJSONFilename)
# EVTX
else:
if not self.useExternalBinaries or not Path(self.evtxDumpCmd).is_file():
self.logger.debug(" [-] No external binaries args or evtx_dump is missing")
self.runUsingBindings(file)
else:
cmd = [self.evtxDumpCmd, "--no-confirm-overwrite", "-o", "jsonl", str(file), "-f", outputJSONFilename, "-t", str(self.cores)]
subprocess.call(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except Exception as e:
self.logger.error(f"{Fore.RED} [-] {e}{Fore.RESET}")
def cleanup(self):
shutil.rmtree(self.tmpDir)
class zircoGuiGenerator:
"""
Generate the mini GUI
"""
def __init__(self, packageDir, templateFile, logger=None, timeField=""):
self.logger = logger or logging.getLogger(__name__)
self.templateFile = templateFile
self.tmpDir = f'tmp-zircogui-{self._randString()}'
self.tmpFile = f'data-{self._randString()}.js'
self.outputFile = f'zircogui-output-{self._randString()}'
self.packageDir = packageDir
self.timeField = timeField
def _randString(self):
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(4))
def generate(self, data, directory=""):
# Check if directory exists, fallback to current directory if not
final_directory = directory.rstrip("/") if os.path.exists(directory) else ""
if directory and not final_directory:
self.logger.error(f"{Fore.RED} [-] {directory} does not exist, fallback to current directory{Fore.RESET}")
try:
# Extract the GUI package
shutil.unpack_archive(self.packageDir, self.tmpDir, "zip")
# Generate data file
self.logger.info(f"[+] Generating ZircoGui package to: {final_directory}/{self.outputFile}.zip")
exportforzircoguiTmpl = templateEngine(self.logger, self.templateFile, self.tmpFile, self.timeField)
exportforzircoguiTmpl.generateFromTemplate(exportforzircoguiTmpl.template, exportforzircoguiTmpl.templateOutput, data)
# Move data file to package directory
shutil.move(self.tmpFile, f'{self.tmpDir}/zircogui/data.js')
# Create zip archive
shutil.make_archive(self.outputFile, 'zip', f"{self.tmpDir}/zircogui")
# Move to final destination if specified
if final_directory:
shutil.move(f"{self.outputFile}.zip", f"{final_directory}/{self.outputFile}.zip")
except Exception as e:
self.logger.error(f"{Fore.RED} [-] {e}{Fore.RESET}")
finally:
# Clean up temporary directory
if os.path.exists(self.tmpDir):
shutil.rmtree(self.tmpDir)
class rulesUpdater:
"""
Download rulesets from the https://github.com/wagga40/Zircolite-Rules repository and update if necessary.
"""
def __init__(self, logger=None):
self.url = "https://github.com/wagga40/Zircolite-Rules/archive/refs/heads/main.zip"
self.logger = logger or logging.getLogger(__name__)
self.tempFile = f'tmp-rules-{self._randString()}.zip'
self.tmpDir = f'tmp-rules-{self._randString()}'
self.updatedRulesets = []
def _randString(self):
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(4))
def download(self):
resp = requests.get(self.url, stream=True)
total = int(resp.headers.get('content-length', 0))
with open(self.tempFile, 'wb') as file, tqdm(
desc=self.tempFile,
total=total,
unit='iB',
unit_scale=True,
unit_divisor=1024,
colour="yellow"
) as bar:
for data in resp.iter_content(chunk_size=1024):
size = file.write(data)
bar.update(size)
def unzip(self):
shutil.unpack_archive(self.tempFile, self.tmpDir, "zip")
def checkIfNewerAndMove(self):
count = 0
rules_dir = Path('rules/')
if not rules_dir.exists():
rules_dir.mkdir()