Exemplo n.º 1
0
    def _handle_error_on_increment(self, error, last_errors):
        """
        Handle the error
        """
        #
        # Create a detailed exception message
        #
        msg = (
            'w3af found too many consecutive errors while performing'
            ' HTTP requests. In most cases this means that the remote web'
            ' server is not reachable anymore, the network is down, or'
            ' a WAF is blocking our tests. The last error message was "%s".')

        reason_msg = self.get_exception_reason(error)

        # If I got a reason, it means that it is a known exception.
        if reason_msg is not None:
            # Stop using ExtendedUrllib instance
            e = ScanMustStopByKnownReasonExc(msg % error, reason=reason_msg)

        else:
            e = ScanMustStopByUnknownReasonExc(msg % error, errs=last_errors)

        self._stop_exception = e
        # pylint: disable=E0702
        raise self._stop_exception
Exemplo n.º 2
0
    def _handle_error_count_exceeded(self, error):
        """
        Handle the case where we exceeded MAX_ERROR_COUNT
        """
        # Create a detailed exception message
        msg = ('w3af found too many consecutive errors while performing'
               ' HTTP requests. In most cases this means that the remote web'
               ' server is not reachable anymore, the network is down, or'
               ' a WAF is blocking our tests. The last exception message'
               ' was "%s" (%s.%s).')

        reason_msg = get_exception_reason(error)
        args = (error, error.__class__.__module__, error.__class__.__name__)

        # If I got a reason, it means that it is a known exception.
        if reason_msg is not None:
            # Stop using ExtendedUrllib instance
            e = ScanMustStopByKnownReasonExc(msg % args, reason=reason_msg)

        else:
            last_errors = []
            last_n_responses = list(self._last_responses)[-MAX_ERROR_COUNT:]

            for response_meta in last_n_responses:
                last_errors.append(response_meta.message)

            e = ScanMustStopByUnknownReasonExc(msg % args, errs=last_errors)

        self._stop_exception = e
        # pylint: disable=E0702
        raise self._stop_exception
Exemplo n.º 3
0
 def wrapper(self, *args, **kwargs):
     try:
         return meth(self, *args, **kwargs)
     except IOError as (errno, strerror):
         if errno == ENOSPC:
             msg = 'No space left on device'
             raise ScanMustStopByKnownReasonExc(msg)
Exemplo n.º 4
0
    def do_open(self, req):
        """
        Called by handler's url_open method.
        """
        host = req.get_host()
        if not host:
            raise urllib2.URLError('no host given')

        try:
            resp_statuses = self._hostresp.setdefault(host,
                                                      self._get_tail_filter())
            # Check if all our last 'resp_statuses' were timeouts and raise
            # a ScanMustStopException if this is the case.
            if len(resp_statuses) == self._curr_check_failures:

                # https://mail.python.org/pipermail/python-dev/2007-January/070515.html
                # https://github.com/andresriancho/w3af/search?q=deque&ref=cmdform&type=Issues
                # https://github.com/andresriancho/w3af/issues/1311
                #
                # I copy the deque to avoid issues when iterating over it in the
                # all() / for statement below
                resp_statuses_cp = copy.copy(resp_statuses)

                if all(st == RESP_TIMEOUT for st in resp_statuses_cp):
                    msg = ('w3af found too many consecutive timeouts. The'
                           ' remote webserver seems to be unresponsive; please'
                           ' verify manually.')
                    reason = 'Timeout while trying to reach target.'
                    raise ScanMustStopByKnownReasonExc(msg, reason=reason)

            conn_factory = self._get_connection
            conn = self._cm.get_available_connection(host, conn_factory)

            if conn.is_fresh:
                # First of all, call the request method. This is needed for
                # HTTPS Proxy
                if isinstance(conn, ProxyHTTPConnection):
                    conn.proxy_setup(req.get_full_url())

                conn.is_fresh = False
                self._start_transaction(conn, req)
                resp = conn.getresponse()
            else:
                # We'll try to use a previously created connection
                resp = self._reuse_connection(conn, req, host)
                # If the resp is None it means that connection is bad. It was
                # possibly closed by the server. Replace it with a new one.
                if resp is None:
                    conn.close()
                    conn = self._cm.replace_connection(conn, host,
                                                       conn_factory)
                    # First of all, call the request method. This is needed for
                    # HTTPS Proxy
                    if isinstance(conn, ProxyHTTPConnection):
                        conn.proxy_setup(req.get_full_url())

                    # Try again with the fresh one
                    conn.is_fresh = False
                    self._start_transaction(conn, req)
                    resp = conn.getresponse()

        except socket.timeout:
            # We better discard this connection
            self._cm.remove_connection(conn, host)
            resp_statuses.append(RESP_TIMEOUT)
            raise URLTimeoutError()

        except socket.error:
            # We better discard this connection
            self._cm.remove_connection(conn, host)
            resp_statuses.append(RESP_BAD)
            raise
        
        except httplib.HTTPException:
            # We better discard this connection
            self._cm.remove_connection(conn, host)
            resp_statuses.append(RESP_BAD)
            raise
        
        else:
            # This response seems to be fine
            resp_statuses.append(RESP_OK)
    
            # If not a persistent connection, don't try to reuse it
            if resp.will_close:
                self._cm.remove_connection(conn, host)
    
            if DEBUG:
                om.out.debug("STATUS: %s, %s" % (resp.status, resp.reason))
            
            resp._handler = self
            resp._host = host
            resp._url = req.get_full_url()
            resp._connection = conn
            resp.code = resp.status
            resp.headers = resp.msg
            resp.msg = resp.reason
            return resp
Exemplo n.º 5
0
    def _handle_error_on_increment(self, error, parsed_traceback, last_errors):
        """
        Handle the error
        """
        # Stop using ExtendedUrllib instance
        self._error_stopped = True

        #
        # Create a detailed exception message
        #
        msg = (
            'w3af found too many consecutive errors while performing'
            ' HTTP requests. In most cases this means that the remote web'
            ' server is not reachable anymore, the network is down, or'
            ' a WAF is blocking our tests. The last error message was "%s".')

        if parsed_traceback:
            tback_str = ''
            for path, line, call in parsed_traceback[-3:]:
                tback_str += '    %s:%s at %s\n' % (path, line, call)

            msg += ' The last calls in the traceback are: \n%s' % tback_str

        reason_msg = None

        if isinstance(error, URLTimeoutError):
            # New exception type raised by keepalive handler
            reason_msg = error.message
            reason_err = error.message

        # Exceptions may be of type httplib.HTTPException or socket.error
        # We're interested on handling them in different ways
        elif isinstance(error, urllib2.URLError):
            reason_err = error.reason

            # Known reason errors. See errno module for more info on these
            # errors.
            EUNKNSERV = -2  # Name or service not known error
            EINVHOSTNAME = -5  # No address associated with hostname
            known_errors = (EUNKNSERV, ECONNREFUSED, EHOSTUNREACH, ECONNRESET,
                            ENETDOWN, ENETUNREACH, EINVHOSTNAME, ETIMEDOUT,
                            ENOSPC)

            if isinstance(reason_err, socket.error):
                if isinstance(reason_err, socket.sslerror):
                    reason_msg = 'SSL Error: %s' % error.reason
                elif reason_err[0] in known_errors:
                    reason_msg = str(reason_err)

        elif isinstance(error, ssl.SSLError):
            reason_msg = 'SSL Error: %s' % error.message

        elif isinstance(error, httplib.HTTPException):
            #
            #    Here we catch:
            #
            #    BadStatusLine, ResponseNotReady, CannotSendHeader,
            #    CannotSendRequest, ImproperConnectionState,
            #    IncompleteRead, UnimplementedFileMode, UnknownTransferEncoding,
            #    UnknownProtocol, InvalidURL, NotConnected.
            #
            #    TODO: Maybe we're being TOO generic in this isinstance?
            #
            reason_msg = '%s: %s' % (error.__class__.__name__, error.args)
            reason_err = error.message

        # If I got a reason, it means that it is a known exception.
        if reason_msg is not None:
            raise ScanMustStopByKnownReasonExc(msg % error, reason=reason_err)

        else:
            errors = [] if parsed_traceback else last_errors
            raise ScanMustStopByUnknownReasonExc(msg % error, errs=errors)