Ejemplo n.º 1
0
    def __init__(self, ip, port, uri_opener, handler_klass=ProxyHandler,
                 ca_certs=CA_CERT_DIR, name='ProxyThread'):
        """
        :param ip: IP address to bind
        :param port: Port to bind
        :param uri_opener: The uri_opener that will be used to open
                           the requests that arrive from the browser
        :param handler_klass: A class that will know how to handle
                              requests from the browser
        """
        Process.__init__(self)
        self.daemon = True
        self.name = name
        
        # Internal vars
        self._server = None
        self._running = False
        self._uri_opener = uri_opener
        self._ca_certs = ca_certs

        # Stats
        self.total_handled_requests = 0

        # User configured parameters
        try:
            self._config = ProxyConfig(cadir=self._ca_certs,
                                       ssl_version_client='SSLv23',
                                       ssl_version_server='SSLv23',
                                       host=ip,
                                       port=port)
        except AttributeError as ae:
            if str(ae) == "'module' object has no attribute '_lib'":
                # This is a rare issue with the OpenSSL setup that some users
                # (mostly in mac os) find. Not related with w3af/mitmproxy but
                # with some broken stuff they have
                #
                # https://github.com/mitmproxy/mitmproxy/issues/281
                # https://github.com/andresriancho/w3af/issues/10716
                #
                # AttributeError: 'module' object has no attribute '_lib'
                raise ProxyException(self.INCORRECT_SETUP % ae)

            else:
                # Something unexpected, raise
                raise

        # Setting these options together with ssl_version_client and
        # ssl_version_server set to SSLv23 means that the proxy will allow all
        # types (including insecure) of SSL connections
        self._config.openssl_options_client = None
        self._config.openssl_options_server = None

        # Start the proxy server
        try:
            self._server = ProxyServer(self._config)
        except socket.error, se:
            raise ProxyException('Socket error while starting proxy: "%s"'
                                 % se.strerror)
Ejemplo n.º 2
0
    def __init__(self, ip, port, uri_opener, proxy_handler=w3afProxyHandler,
                 proxy_cert=SSL_CERT, name='ProxyThread'):
        """
        :param ip: IP address to bind
        :param port: Port to bind
        :param uri_opener: The uri_opener that will be used to open
                           the requests that arrive from the browser
        :param proxy_handler: A class that will know how to handle
                              requests from the browser
        :param proxy_cert: Proxy certificate to use, this is needed
                           for proxying SSL connections.
        """
        Process.__init__(self)
        self.daemon = True
        self.name = name
        
        # Internal vars
        self._server = None
        self._proxy_handler = proxy_handler
        self._running = False
        self._uri_opener = uri_opener

        # User configured parameters
        self._ip = ip
        self._port = port
        self._proxy_cert = proxy_cert

        # Start the proxy server
        try:
            self._server = ProxyServer((self._ip, self._port),
                                       self._proxy_handler)
        except socket.error, se:
            raise ProxyException('Socket error while starting proxy: "%s"'
                                 % se.strerror)
Ejemplo n.º 3
0
 def set_what_not_to_trap(self, regex):
     """
     Set regular expression that indicates what URLs TO trap.
     """
     try:
         self.what_not_to_trap = re.compile(regex)
     except re.error:
         error = 'The regular expression you configured is invalid.'
         raise ProxyException(error)
Ejemplo n.º 4
0
    def __init__(self,
                 ip,
                 port,
                 uri_opener,
                 handler_klass=ProxyHandler,
                 ca_certs=CA_CERT_DIR,
                 name='ProxyThread'):
        """
        :param ip: IP address to bind
        :param port: Port to bind
        :param uri_opener: The uri_opener that will be used to open
                           the requests that arrive from the browser
        :param handler_klass: A class that will know how to handle
                              requests from the browser
        """
        Process.__init__(self)
        self.daemon = True
        self.name = name

        # Internal vars
        self._server = None
        self._running = False
        self._uri_opener = uri_opener
        self._ca_certs = ca_certs

        # Stats
        self.total_handled_requests = 0

        # User configured parameters
        self._config = ProxyConfig(cadir=self._ca_certs,
                                   ssl_version_client='all',
                                   ssl_version_server='all',
                                   host=ip,
                                   port=port)

        # Start the proxy server
        try:
            self._server = ProxyServer(self._config)
        except socket.error, se:
            raise ProxyException('Socket error while starting proxy: "%s"' %
                                 se.strerror)
Ejemplo n.º 5
0
class Proxy(Process):
    """
    This class defines a simple HTTP proxy, it is mainly used for "complex"
    plugins.

    You should create a proxy instance like this:
        ws = Proxy('127.0.0.1', 8080, url_opener)

    Or like this, if you want to override the proxy handler (most times you
    want to do it!):
        ws = Proxy('127.0.0.1', 8080, url_opener, proxy_handler=ph)

    If the IP:Port is already in use, an exception will be raised while
    creating the ws instance.

    To start the proxy, and given that this is a Process class, you can do this:
        ws.start()

    Or if you don't want a different thread, you can simply call the run method:
        ws.run()

    The proxy handler class is the place where you'll perform all the magic
    stuff, like intercepting requests, modifying them, etc. A good idea if you
    want to code your own proxy handler is to inherit from the proxy handler
    that is already defined in this file (see: ProxyHandler).

    What you basically have to do is to inherit from it:
        class MyProxyHandler(ProxyHandler):

    And redefine the following methods:
        def do_ALL(self)
            Which originally receives a request from the browser, sends it to
            the remote site, receives the response and returns the response to
            the browser. This method is called every time the browser sends a
            new request.

    Things that work:
        - http requests like GET, HEAD, POST, CONNECT
        - https with certs and all (mitmproxy)

    :author: Andres Riancho ([email protected])
    """

    CA_CERT_DIR = os.path.join(ROOT_PATH, 'core/controllers/daemons/proxy/ca/')

    INCORRECT_SETUP = ('Your OpenSSL setup seems to be broken. The mitmproxy'
                       ' library failed to create the default configuration'
                       ' required to run.\n'
                       '\n'
                       'The original exception is: "%s"\n'
                       '\n'
                       'Please see this [0] github issue for potential'
                       ' workarounds and help.\n'
                       '\n'
                       '[0] https://github.com/mitmproxy/mitmproxy/issues/281')

    def __init__(self,
                 ip,
                 port,
                 uri_opener,
                 handler_klass=ProxyHandler,
                 ca_certs=CA_CERT_DIR,
                 name='ProxyThread'):
        """
        :param ip: IP address to bind
        :param port: Port to bind
        :param uri_opener: The uri_opener that will be used to open
                           the requests that arrive from the browser
        :param handler_klass: A class that will know how to handle
                              requests from the browser
        """
        Process.__init__(self)
        self.daemon = True
        self.name = name

        # Internal vars
        self._server = None
        self._running = False
        self._uri_opener = uri_opener
        self._ca_certs = ca_certs

        # Stats
        self.total_handled_requests = 0

        # User configured parameters
        try:
            self._config = ProxyConfig(cadir=self._ca_certs,
                                       ssl_version_client='SSLv23',
                                       ssl_version_server='SSLv23',
                                       host=ip,
                                       port=port)
        except AttributeError as ae:
            if str(ae) == "'module' object has no attribute '_lib'":
                # This is a rare issue with the OpenSSL setup that some users
                # (mostly in mac os) find. Not related with w3af/mitmproxy but
                # with some broken stuff they have
                #
                # https://github.com/mitmproxy/mitmproxy/issues/281
                # https://github.com/andresriancho/w3af/issues/10716
                #
                # AttributeError: 'module' object has no attribute '_lib'
                raise ProxyException(self.INCORRECT_SETUP % ae)

            else:
                # Something unexpected, raise
                raise

        # Setting these options together with ssl_version_client and
        # ssl_version_server set to SSLv23 means that the proxy will allow all
        # types (including insecure) of SSL connections
        self._config.openssl_options_client = None
        self._config.openssl_options_server = None

        # Start the proxy server
        try:
            self._server = ProxyServer(self._config)
        except socket.error, se:
            raise ProxyException('Socket error while starting proxy: "%s"' %
                                 se.strerror)
        except ProxyServerError, pse:
            raise ProxyException('%s' % pse)