Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve and patch sigma rules analyzer #1

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions timesketch/lib/analyzers/sigma_tagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def run(self):
for rule in sigma_rules:
tags_applied[rule.get('file_name')] = 0
try:
if not rule.get('es_query'):
continue
sigma_rule_counter += 1
tagged_events_counter = self.run_sigma_rule(
rule.get('es_query'), rule.get('file_name'),
Expand Down
4 changes: 4 additions & 0 deletions timesketch/lib/datastores/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ def __init__(self, host='127.0.0.1', port=9200):
if self.user and self.password:
parameters['http_auth'] = (self.user, self.password)

parameters['timeout'] = 30

self.client = Elasticsearch(
[{'host': host, 'port': port}], **parameters)

Expand Down Expand Up @@ -655,6 +657,8 @@ def search_stream(self, sketch_id=None, query_string=None,
for event in result['hits']['hits']:
yield event

self.client.clear_scroll(scroll_id=scroll_id)

def get_filter_labels(self, sketch_id, indices):
"""Aggregate labels for a sketch.

Expand Down
16 changes: 14 additions & 2 deletions timesketch/lib/sigma_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,36 @@ def get_sigma_rule(filepath, sigma_config=None):
abs_path, 'r', encoding='utf-8', errors='replace') as file:
try:
rule_return = {}
parsed_sigma_rules = {}
logsource = dict()
rule_yaml_data = yaml.safe_load_all(file.read())
for doc in rule_yaml_data:
if 'logsource' not in doc:
logsource = next(rule_yaml_data)
if 'detection' not in logsource:
logsource['detection'] = dict()
if 'detection' in doc:
for k, v in doc['detection'].items():
logsource['detection'][k] = v
doc.update(logsource)
rule_return.update(doc)
parser = sigma_collection.SigmaCollectionParser(
yaml.safe_dump(doc), sigma_conf_obj, None)
parsed_sigma_rules = parser.generate(sigma_backend)
if logsource:
break

except NotImplementedError as exception:
logger.error(
'Error generating rule in file {0:s}: {1!s}'
.format(abs_path, exception))
raise
# raise

except sigma_exceptions.SigmaParseError as exception:
logger.error(
'Sigma parsing error generating rule in file {0:s}: {1!s}'
.format(abs_path, exception))
raise
# raise

except yaml.parser.ParserError as exception:
logger.error(
Expand Down