lib/cuckoo/core/reporter.py
# Copyright (C) 2010-2012 Cuckoo Sandbox Developers.
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
# See the file 'docs/LICENSE' for copying permission.
import os
import inspect
import pkgutil
import logging
import copy
from lib.cuckoo.common.constants import CUCKOO_ROOT
from lib.cuckoo.common.config import Config
from lib.cuckoo.common.abstracts import Report
from lib.cuckoo.common.exceptions import CuckooDependencyError, CuckooReportError, CuckooOperationalError
import modules.reporting as plugins
log = logging.getLogger(__name__)
class Reporter:
"""Report generator."""
def __init__(self, analysis_path, custom=""):
"""@param analysis_path: analysis folder path.
@param custom: custom options.
"""
self.analysis_path = analysis_path
self.custom = custom
self.cfg = Config(cfg=os.path.join(CUCKOO_ROOT, "conf", "reporting.conf"))
self.__populate(plugins)
def __populate(self, modules):
"""Load modules.
@param modules: modules.
"""
prefix = modules.__name__ + "."
for loader, name, ispkg in pkgutil.iter_modules(modules.__path__):
if ispkg:
continue
try:
section = getattr(self.cfg, name)
except AttributeError:
continue
if not section.enabled:
continue
path = "%s.%s" % (plugins.__name__, name)
try:
__import__(path, globals(), locals(), ["dummy"], -1)
except CuckooDependencyError as e:
log.warning("Unable to import reporting module \"%s\": %s" % (name, e))
def run(self, data):
"""Generates all reports.
@param data: analysis results.
@raise CuckooReportError: if a report module fails.
"""
Report()
for plugin in Report.__subclasses__():
self._run_report(plugin, data)
def _run_report(self, plugin, data):
"""Run a single report plugin.
@param plugin: report plugin.
@param data: results data from analysis.
"""
current = plugin()
current.set_path(self.analysis_path)
current.cfg = Config(current.conf_path)
module = inspect.getmodule(current)
if "." in module.__name__:
module_name = module.__name__.rsplit(".", 1)[1]
else:
module_name = module.__name__
try:
current.set_options(self.cfg.get(module_name))
except CuckooOperationalError:
raise CuckooReportError("Reporting module %s not found in configuration file" % module_name)
try:
# Run report, for each report a brand new copy of results is
# created, to prevent a reporting module to edit global
# result set and affect other reporting modules.
current.run(copy.deepcopy(data))
log.debug("Executed reporting module \"%s\"" % current.__class__.__name__)
except NotImplementedError:
return
except CuckooReportError as e:
log.warning("Failed to execute reporting module \"%s\": %s" % (current.__class__.__name__, e))