def __connect(self): """ Connect to the PED-RPC server. """ # if we have a pre-existing server socket, ensure it's closed. self.__disconnect() # connect to the server, timeout on failure. self.__server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.__server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.__server_sock.settimeout(3.0) try: self.__server_sock.connect((self.__host, self.__port)) except socket.error as e: if self.__retry != 5: self.__retry += 1 time.sleep(5) self.__connect() else: raise exception.BoofuzzRpcError( 'PED-RPC> unable to connect to server {0}:{1}. Error message: "{2}"\n' .format(self.__host, self.__port, e)) # disable timeouts and lingering. self.__server_sock.settimeout(None) self.__server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, self.NOLINGER)
def __pickle_recv(self): """ This routine is used for marshaling arbitrary data from the PyDbg server. We can send pretty much anything here. For example a tuple containing integers, strings, arbitrary objects and structures. Our "protocol" is a simple length-value protocol where each datagram is prefixed by a 4-byte length of the data to be received. @raise pdx: An exception is raised if the connection was severed. @rtype: Mixed @return: Whatever is received over the socket. """ try: # TODO: this should NEVER fail, but alas, it does and for the time being i can't figure out why. # it gets worse. you would think that simply returning here would break things, but it doesn't. # gotta track this down at some point. recvd = self.__server_sock.recv(4) length = struct.unpack("<L", recvd)[0] except Exception: return try: received = b"" while length: chunk = self.__server_sock.recv(length) received += chunk length -= len(chunk) except socket.error as e: raise exception.BoofuzzRpcError( "PED-RPC> unable to connect to server " '{0}:{1}. Error message: "{2}"\n'.format( self.__host, self.__port, e)) return pickle.loads(received)
def __pickle_send(self, data): """ This routine is used for marshaling arbitrary data to the PyDbg server. We can send pretty much anything here. For example a tuple containing integers, strings, arbitrary objects and structures. Our "protocol" is a simple length-value protocol where each datagram is prefixed by a 4-byte length of the data to be received. @type data: Mixed @param data: Data to marshal and transmit. Data can *pretty much* contain anything you throw at it. @raise pdx: An exception is raised if the connection was severed. """ data = pickle.dumps(data, protocol=2) self.__debug("sending %d bytes" % len(data)) try: self.__server_sock.send(struct.pack("<L", len(data))) self.__server_sock.send(data) except socket.error as e: raise exception.BoofuzzRpcError( "PED-RPC> unable to connect to server " '{0}:{1}. Error message: "{2}"\n'.format( self.__host, self.__port, e))