modules.auxiliary.services

Package modules :: Package auxiliary :: Module services |
[hide private]
[frames] | no frames]

# Source Code for Module modules.auxiliary.services

     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 logging
     7  import time
     8
     9  from lib.cuckoo.common.abstracts import Auxiliary
    10  from lib.cuckoo.common.config import Config
    11  from lib.cuckoo.core.database import Database
    12
    13  log = logging.getLogger(__name__)
    14  cfg = Config()
    15  db = Database()
    16
    17 -class Services(Auxiliary):
    18      """Allows one or more additional VMs to be run next to an analysis. Either
    19      as global services (which are generally never rebooted) or on a
    20      per-analysis basis."""
    21
    22 -    def start_service(self, service):
    23          """Start a VM containing one or more services."""
    24          # We give all services a total of 5 minutes to boot up before
    25          # starting the actual analysis.
    26          timeout = self.task.timeout or cfg.timeouts.default
    27          timeout += 300
    28          tags = "service,%s" % service
    29
    30          return db.add_service(timeout, self.task.owner, tags)
    31
    32 -    def stop_service(self, task_id):
    33          """Stop a VM containing one or more services."""
    34          db.guest_set_status(task_id, "stop")
    35
    36 -    def start(self):
    37          self.tasks = []
    38
    39          if self.task.category == "service":
    40              return
    41
    42          # Have to explicitly enable services.
    43          if not self.task.options.get("services"):
    44              return
    45
    46          for service in self.options.get("services", "").split(","):
    47              service = service.strip()
    48              if not service:
    49                  continue
    50
    51              task_id = self.start_service(service)
    52              self.tasks.append((task_id, service))
    53
    54              log.info("Started service %s #%d for task #%d",
    55                       service, task_id, self.task.id)
    56
    57          # Wait until each service is either starting to run, running, or for
    58          # some reason stopped.
    59          wait_states = "starting", "running", "stopping"
    60          for task_id, service in self.tasks:
    61              while db.guest_get_status(task_id) not in wait_states:
    62                  time.sleep(1)
    63
    64          # Wait an additional timeout before starting the actual analysis.
    65          timeout = self.options.get("timeout")
    66          if isinstance(timeout, int):
    67              time.sleep(timeout)
    68
    69 -    def stop(self):
    70          if self.task.category == "service":
    71              return
    72
    73          for task_id, service in self.tasks:
    74              log.info("Stopping service %s #%d for task #%d",
    75                       service, task_id, self.task.id)
    76              self.stop_service(task_id)
    77