Ejemplo n.º 1
0
    def run(self, dst_ip):
        """
        Connect a tcp socket 
        If we have a string to send, send it to the socket
        If have a regexp to match, read response and match the regexp

        args:
            dst_ip: string, IP address to connect to

        returns:
            None

        raises:
            MonitorFailed() on a socket operation timeout/error or if failed
            match the regexp.
        """
        tcp_sock = TCPSocket(ip=dst_ip, port=self.port, timeout=self.timeout)

        # connect socket, send string if required
        try:
            tcp_sock.connect()
            if self.send_string is not None:
                tcp_sock.sendall(self._send_bytes)
        except ProtocolError as e:
            raise MonitorFailed(e)

        # if we have nothing to match, close the socket and return
        if self.match_re is None:
            tcp_sock.close()
            return

        # we have a regexp to match
        # continuously read from the socket and perform matching until either
        # a match is found, a timeout occurred or remote end 
        # closed the conection
        response_string = ''
        while True:
            try:
                recv_bytes = tcp_sock.recv()
            except ProtocolError as e:
                log_msg = ('failed to match the regexp within the timeout, '
                           'got {error}, '
                           'response(up to 512 chars): {response_string}'
                           .format(error=e,
                                   response_string=response_string[:512]))
                raise MonitorFailed(log_msg)

            # remote side closed connection, no need to call sock.close()
            if recv_bytes == b'':
                raise MonitorFailed('remote closed the connection, '
                                    'failed to match the regexp in the '
                                    'response(up to 512 chars): {}'
                                    .format(response_string[:512]))
        
            # received data
            else:
                response_string += recv_bytes.decode(errors='ignore')
                if self._match_re_compiled.search(response_string):
                    tcp_sock.close()
                    return
Ejemplo n.º 2
0
    def run(self, dst_ip):
        """
        Connect a tcp socket 
        If we have a string to send, send it to the socket
        If have a regexp to match, read response and match the regexp

        args:
            dst_ip: string, IP address to connect to

        returns:
            None

        raises:
            MonitorFailed() on a socket operation timeout/error or if failed
            match the regexp.

        """
        tcp_sock = TCPSocket(ip=dst_ip, port=self.port, timeout=self.timeout)

        # connect socket, send string if required
        try:
            tcp_sock.connect()
            if self.send_string is not None:
                tcp_sock.sendall(self._send_bytes)
        except ProtocolError as e:
            raise MonitorFailed(e)

        # if we have nothing to match, close the socket and return
        if self.match_re is None:
            tcp_sock.close()
            return

        # we have a regexp to match, read response, perform matching
        try:
            response_bytes = tcp_sock.receive()
        except ProtocolError as e:
            raise MonitorFailed(e)
        else:
            # close the socket
            tcp_sock.close()

            # decode up to MAX_RESPONSE_BYTES
            response_text = response_bytes[:MAX_RESPONSE_BYTES].decode()
            
            # match regexp
            if not self._match_re_compiled.search(response_text):
                raise MonitorFailed('failed to match the reg exp')
Ejemplo n.º 3
0
    def run(self, dst_ip):
        """
        Connect a tcp socket 
        If we have a string to send, send it to the socket
        If have a regexp to match, read response and match the regexp

        args:
            dst_ip: string, IP address to connect to

        returns:
            None

        raises:
            MonitorFailed() on a socket operation timeout/error or if failed
            match the regexp.

        """
        tcp_sock = TCPSocket(ip=dst_ip, port=self.port, timeout=self.timeout)

        # connect socket, send string if required
        try:
            tcp_sock.connect()
            if self.send_string is not None:
                tcp_sock.sendall(self._send_bytes)
        except ProtocolError as e:
            raise MonitorFailed(e)

        # if we have nothing to match, close the socket and return
        if self.match_re is None:
            tcp_sock.close()
            return

        # we have a regexp to match, read response, perform matching
        try:
            response_bytes = tcp_sock.receive()
        except ProtocolError as e:
            raise MonitorFailed(e)
        else:
            # close the socket
            tcp_sock.close()

            # decode up to MAX_RESPONSE_BYTES
            response_text = response_bytes[:MAX_RESPONSE_BYTES].decode()

            # match regexp
            if not self._match_re_compiled.search(response_text):
                raise MonitorFailed('failed to match the reg exp')