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

Source Code for Module lib.common.results

 1  # Copyright (C) 2010-2014 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 logging 
 6  import socket 
 7   
 8  from lib.core.config import Config 
 9   
10  log = logging.getLogger(__name__) 
11   
12  BUFSIZE = 16 * 1024 
13   
14 -def upload_to_host(file_path, dump_path):
15 nc = infd = None 16 try: 17 nc = NetlogFile(dump_path) 18 19 infd = open(file_path, "rb") 20 tmp = infd.read(BUFSIZE) 21 while tmp: 22 nc.send(tmp) 23 tmp = infd.read(BUFSIZE) 24 except Exception as e: 25 log.error("Exception uploading file to host: %s", e) 26 finally: 27 if infd: 28 infd.close() 29 if nc: 30 nc.close()
31
32 -class NetlogConnection(object):
33 - def __init__(self, proto=""):
34 config = Config(cfg="analysis.conf") 35 self.hostip, self.hostport = config.ip, config.port 36 self.sock, self.file = None, None 37 self.proto = proto
38
39 - def connect(self):
40 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 41 try: 42 s.connect((self.hostip, self.hostport)) 43 s.sendall(self.proto) 44 except: 45 pass 46 else: 47 self.sock = s 48 self.file = s.makefile()
49
50 - def send(self, data, retry=True):
51 try: 52 self.sock.sendall(data) 53 except socket.error: 54 self.connect() 55 if retry: 56 self.send(data, retry=False) 57 except: 58 # We really have nowhere to log this, if the netlog connection 59 # does not work, we can assume that any logging won't work either. 60 # So we just fail silently. 61 self.close()
62
63 - def close(self):
64 try: 65 self.file.close() 66 self.sock.close() 67 except Exception: 68 pass
69
70 -class NetlogFile(NetlogConnection):
71 - def __init__(self, filepath):
72 self.filepath = filepath 73 NetlogConnection.__init__(self, proto="FILE\n{0}\n".format(self.filepath)) 74 self.connect()
75
76 -class NetlogHandler(logging.Handler, NetlogConnection):
77 - def __init__(self):
78 logging.Handler.__init__(self) 79 NetlogConnection.__init__(self, proto="LOG\n") 80 self.connect()
81
82 - def emit(self, record):
83 msg = self.format(record) 84 self.send("{0}\n".format(msg))
85