Package lib :: Package common :: Module results
[hide private]
[frames] | no frames]

Source Code for Module lib.common.results

  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 socket 
  8  import time 
  9  import sys 
 10   
 11  from lib.core.config import Config 
 12   
 13  log = logging.getLogger(__name__) 
 14   
 15  BUFSIZE = 1024*1024 
 16   
17 -def upload_to_host(file_path, dump_path, pids=[]):
18 nc = infd = None 19 try: 20 nc = NetlogFile() 21 nc.init(dump_path, file_path, pids) 22 23 infd = open(file_path, "rb") 24 buf = infd.read(BUFSIZE) 25 while buf: 26 nc.send(buf, retry=False) 27 buf = infd.read(BUFSIZE) 28 except Exception as e: 29 log.error("Exception uploading file %s to host: %s", file_path, e) 30 finally: 31 if infd: 32 infd.close() 33 if nc: 34 nc.close()
35
36 -class NetlogConnection(object):
37 - def __init__(self, proto=""):
38 config = Config(cfg="analysis.conf") 39 self.hostip, self.hostport = config.ip, config.port 40 self.sock = None 41 self.proto = proto
42
43 - def connect(self):
44 # Try to connect as quickly as possible. Just sort of force it to 45 # connect with a short timeout. 46 while not self.sock: 47 try: 48 s = socket.create_connection((self.hostip, self.hostport), 0.1) 49 except socket.error: 50 time.sleep(0.1) 51 continue 52 53 s.settimeout(None) 54 s.sendall(self.proto) 55 56 self.sock = s
57
58 - def send(self, data, retry=True):
59 if not self.sock: 60 self.connect() 61 62 try: 63 self.sock.sendall(data) 64 except socket.error as e: 65 if retry: 66 self.connect() 67 self.send(data, retry=False) 68 else: 69 print >>sys.stderr, "Unhandled exception in NetlogConnection:", str(e) 70 except Exception as e: 71 print >>sys.stderr, "Unhandled exception in NetlogConnection:", str(e) 72 # We really have nowhere to log this, if the netlog connection 73 # does not work, we can assume that any logging won't work either. 74 # So we just fail silently. 75 self.close()
76
77 - def close(self):
78 try: 79 self.sock.close() 80 except Exception: 81 pass
82
83 -class NetlogFile(NetlogConnection):
84 - def init(self, dump_path, filepath=None, pids=[]):
85 if filepath: 86 self.proto = "FILE 2\n{0}\n{1}\n{2}\n".format( 87 dump_path, filepath, " ".join(pids) 88 ) 89 else: 90 self.proto = "FILE\n{0}\n".format(dump_path) 91 92 self.connect()
93
94 -class NetlogHandler(logging.Handler, NetlogConnection):
95 - def __init__(self):
96 logging.Handler.__init__(self) 97 NetlogConnection.__init__(self, proto="LOG\n") 98 self.connect()
99
100 - def emit(self, record):
101 msg = self.format(record) 102 self.send("{0}\n".format(msg))
103