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

Source Code for Module modules.auxiliary.screenshots

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