modules.auxiliary.screenshots

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

# Source Code for Module modules.auxiliary.screenshots

     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 time
     7  import logging
     8  import StringIO
     9  from threading import Thread
    10
    11  from lib.common.abstracts import Auxiliary
    12  from lib.common.results import NetlogFile
    13  from lib.api.screenshot import Screenshot
    14
    15  log = logging.getLogger(__name__)
    16  SHOT_DELAY = 1
    17  # Skip the following area when comparing screen shots.
    18  # Example for 800x600 screen resolution.
    19  # SKIP_AREA = ((735, 575), (790, 595))
    20  SKIP_AREA = None
    21
    22 -class Screenshots(Auxiliary, Thread):
    23      """Take screenshots."""
    24
    25 -    def __init__(self, options={}, analyzer=None):
    26          Thread.__init__(self)
    27          Auxiliary.__init__(self, options, analyzer)
    28          self.do_run = True
    29
    30 -    def stop(self):
    31          """Stop screenshotting."""
    32          self.do_run = False
    33
    34 -    def run(self):
    35          """Run screenshotting.
    36          @return: operation status.
    37          """
    38          if "screenshots" in self.options:
    39              self.do_run = int(self.options["screenshots"])
    40
    41          if not Screenshot().have_pil():
    42              log.warning("Python Image Library is not installed, "
    43                          "screenshots are disabled")
    44              return False
    45
    46          img_counter = 0
    47          img_last = None
    48
    49          while self.do_run:
    50              time.sleep(SHOT_DELAY)
    51
    52              try:
    53                  img_current = Screenshot().take()
    54              except IOError as e:
    55                  log.error("Cannot take screenshot: %s", e)
    56                  continue
    57
    58              if img_last:
    59                  if Screenshot().equal(img_last, img_current, SKIP_AREA):
    60                      continue
    61
    62              img_counter += 1
    63
    64              # workaround as PIL can't write to the socket file object :(
    65              tmpio = StringIO.StringIO()
    66              img_current.save(tmpio, format="JPEG")
    67              tmpio.seek(0)
    68
    69              # now upload to host from the StringIO
    70              nf = NetlogFile()
    71              nf.init("shots/%s.jpg" % str(img_counter).rjust(4, "0"))
    72
    73              for chunk in tmpio:
    74                  nf.sock.sendall(chunk)
    75
    76              nf.close()
    77
    78              img_last = img_current
    79
    80          return True
    81