modules.processing.debug

Package modules :: Package processing :: Module debug |
[hide private]
[frames] | no frames]

# Source Code for Module modules.processing.debug

     1  # Copyright (C) 2010-2013 Claudio Guarnieri.
     2  # Copyright (C) 2014-2016 Cuckoo Foundation.
     3  # This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
     4  # See the file 'docs/LICENSE' for copying permission.
     5
     6  import codecs
     7  import logging
     8  import os
     9
    10  from lib.cuckoo.common.abstracts import Processing
    11  from lib.cuckoo.common.exceptions import CuckooProcessingError
    12  from lib.cuckoo.core.database import Database
    13
    14  log = logging.getLogger(__name__)
    15
    16 -class Logfile(list):
    17 -    def __init__(self, filepath):
    18          list.__init__(self)
    19          self.filepath = filepath
    20
    21 -    def __iter__(self):
    22          try:
    23              for line in codecs.open(self.filepath, "rb", "utf-8"):
    24                  yield line
    25          except Exception as e:
    26              log.info("Error decoding %s: %s", self.filepath, e)
    27
    28 -    def __nonzero__(self):
    29          return bool(os.path.getsize(self.filepath))
    30
    31 -class Debug(Processing):
    32      """Analysis debug information."""
    33
    34 -    def run(self):
    35          """Run debug analysis.
    36          @return: debug information dict.
    37          """
    38          self.key = "debug"
    39          debug = {"log": [], "cuckoo": [], "errors": []}
    40
    41          if os.path.exists(self.log_path):
    42              try:
    43                  f = codecs.open(self.log_path, "rb", "utf-8")
    44                  debug["log"] = f.readlines()
    45              except ValueError as e:
    46                  raise CuckooProcessingError("Error decoding %s: %s" %
    47                                              (self.log_path, e))
    48              except (IOError, OSError) as e:
    49                  raise CuckooProcessingError("Error opening %s: %s" %
    50                                              (self.log_path, e))
    51
    52          if os.path.exists(self.cuckoolog_path):
    53              debug["cuckoo"] = Logfile(self.cuckoolog_path)
    54
    55          for error in Database().view_errors(int(self.task["id"])):
    56              debug["errors"].append(error.message)
    57
    58          if os.path.exists(self.mitmerr_path):
    59              mitmerr = open(self.mitmerr_path, "rb").read()
    60              if mitmerr:
    61                  debug["errors"].append(mitmerr)
    62
    63          return debug
    64