From dd230c240769d0889cf6d32e8d78a98536c8f5d3 Mon Sep 17 00:00:00 2001 From: Nex Date: Fri, 17 Jun 2022 14:56:39 +0200 Subject: [PATCH] Added optional file logging --- mvt/common/command.py | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/mvt/common/command.py b/mvt/common/command.py index 3334700..0ecef21 100644 --- a/mvt/common/command.py +++ b/mvt/common/command.py @@ -16,6 +16,8 @@ class Command(object): def __init__(self, target_path=None, results_path=None, ioc_files=[], module_name=None, serial=None, fast_mode=False, log=logging.getLogger(__name__)): + self.name = "" + self.target_path = target_path self.results_path = results_path self.ioc_files = ioc_files @@ -46,13 +48,25 @@ class Command(object): def _store_timeline(self): if self.results_path: - if len(self.timeline) > 0: - save_timeline(self.timeline, - os.path.join(self.results_path, "timeline.csv")) + return - if len(self.timeline_detected) > 0: - save_timeline(self.timeline_detected, - os.path.join(self.results_path, "timeline_detected.csv")) + if len(self.timeline) > 0: + save_timeline(self.timeline, + os.path.join(self.results_path, "timeline.csv")) + + if len(self.timeline_detected) > 0: + save_timeline(self.timeline_detected, + os.path.join(self.results_path, "timeline_detected.csv")) + + def _add_log_file_handler(self, logger): + if not self.results_path: + return + + fh = logging.FileHandler(os.path.join(self.results_path, "command.log")) + formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") + fh.setLevel(logging.DEBUG) + fh.setFormatter(formatter) + logger.addHandler(fh) def init(self): raise NotImplementedError @@ -62,6 +76,7 @@ class Command(object): def run(self): self._create_storage() + self._add_log_file_handler(self.log) try: self.init() @@ -72,8 +87,13 @@ class Command(object): if self.module_name and module.__name__ != self.module_name: continue - m = module(target_path=self.target_path, results_path=self.results_path, fast_mode=self.fast_mode, - log=logging.getLogger(module.__module__)) + module_logger = logging.getLogger(module.__module__) + self._add_log_file_handler(module_logger) + + m = module(target_path=self.target_path, + results_path=self.results_path, + fast_mode=self.fast_mode, + log=module_logger) if self.iocs.total_ioc_count: m.indicators = self.iocs @@ -93,3 +113,4 @@ class Command(object): self.timeline_detected.extend(m.timeline_detected) self._store_timeline() +