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

Source Code for Module lib.common.results

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