def _make_remote_connection(self, req): """ Creates an endpoint to the target server using the given configuration options then connects to the endpoint using self._client_factory """ self._request_obj = req # If we have a socks proxy, wrap the endpoint in it if context.in_scope(self._request_obj): # Modify the request connection settings to match settings in the factory if self.factory.force_ssl: self._request_obj.is_ssl = True if self.factory.forward_host: self._request_obj.host = self.factory.forward_host # Get connection from the request endpoint = get_endpoint(self._request_obj.host, self._request_obj.port, self._request_obj.is_ssl, socks_config=config.SOCKS_PROXY) else: endpoint = get_endpoint(self._request_obj.host, self._request_obj.port, self._request_obj.is_ssl) # Connect via the endpoint self.log("Accessing using endpoint") yield endpoint.connect(self._client_factory) self.log("Connected")
def prepare_request(self): """ Prepares request for submitting Saves the associated request with a temporary start time, mangles it, then saves the mangled version with an update start time. """ sendreq = self.request if context.in_scope(sendreq): mangle_macros = copy.copy(self.intercepting_macros) self.request.time_start = datetime.datetime.utcnow() if self.save_all: if self.stream_response and not mangle_macros: self.request.async_deep_save() else: yield self.request.async_deep_save() (sendreq, mangled) = yield macros.mangle_request(sendreq, mangle_macros) if sendreq and mangled and self.save_all: self.start_time = datetime.datetime.utcnow() sendreq.time_start = self.start_time yield sendreq.async_deep_save() else: self.log("Request out of scope, passing along unmangled") self.request = sendreq defer.returnValue(self.request)
def return_request_pair(self, request): """ If the request is in scope, it saves the completed request, sets the start/end time, mangles the response, saves the mangled version, then writes the response back through the transport. """ self.end_time = datetime.datetime.utcnow() if config.DEBUG_TO_FILE or config.DEBUG_VERBOSITY > 0: log_request(printable_data(request.response.full_response), id=self.connection_id, symbol='<m', verbosity_level=3) request.time_start = self.start_time request.time_end = self.end_time if context.in_scope(request): mangle_macros = copy.copy(self.intercepting_macros) if self.save_all: if self.stream_response and not mangle_macros: request.async_deep_save() else: yield request.async_deep_save() mangled = yield macros.mangle_response(request, mangle_macros) if mangled and self.save_all: yield request.async_deep_save() if request.response and (config.DEBUG_TO_FILE or config.DEBUG_VERBOSITY > 0): log_request(printable_data(request.response.full_response), id=self.connection_id, symbol='<', verbosity_level=3) else: self.log("Response out of scope, passing along unmangled") self.data_defer.callback(request) defer.returnValue(None)
def return_request_pair(self, request): self.end_time = datetime.datetime.now() log_request(console.printable_data(request.response.full_response), id=self.connection_id, symbol='<m', verbosity_level=3) request.time_start = self.start_time request.time_end = self.end_time if context.in_scope(request): if self.save_all: yield request.async_deep_save() # if we don't copy it, when we delete a macro from the console, # we get a crash. We do a shallow copy to keep the macro # instances the same. to_mangle = copy.copy(self.intercepting_macros).iteritems() old_rsp = request.response for k, macro in to_mangle: if macro.do_rsp: if macro.async_rsp: mangled_rsp = yield macro.async_mangle_response( request) else: mangled_rsp = macro.mangle_response(request) if mangled_rsp is None: request.response = None self.data_defer.callback(request) if self.save_all: yield request.async_deep_save() self.transport.loseConnection() defer.returnValue(None) request.response = mangled_rsp if request.response != old_rsp: request.response.unmangled = old_rsp if self.save_all: yield request.async_deep_save() # re-check after all the mangling context.filter_recheck() if request.response: log_request(console.printable_data( request.response.full_response), id=self.connection_id, symbol='<', verbosity_level=3) else: self.log("Response out of scope, passing along unmangled") self.data_defer.callback(request) defer.returnValue(None)
def _connection_made(self): self.log("Connection established, sending request...", verbosity_level=3) # Make sure to add errback lines = self.request.full_request.splitlines() for l in lines: self.log(l, symbol=">r", verbosity_level=3) sendreq = self.request if context.in_scope(sendreq): to_mangle = copy.copy(self.factory.intercepting_macros).iteritems() if self.factory.save_all: # It isn't the actual time, but this should work in case # we do an 'ls' before it gets a real time saved sendreq.time_start = datetime.datetime.now() if self.factory.stream_response and not to_mangle: self.request.async_deep_save() else: yield self.request.async_deep_save() ## Run intercepting macros # if we don't copy it, when we delete a macro from the console, # we get a crash. We do a shallow copy to keep the macro # instances the same. for k, macro in to_mangle: if macro.intercept_requests: if macro.async_req: sendreq = yield macro.async_mangle_request(sendreq) else: sendreq = macro.mangle_request(sendreq) if sendreq is None: self.log("Request dropped, losing connection") self.transport.loseConnection() self.request = None self.data_defer.callback(None) if self.factory.save_all: yield sendreq.async_deep_save() defer.returnValue(None) if sendreq != self.request: sendreq.unmangled = self.request if self.factory.save_all: yield sendreq.async_deep_save() else: self.log("Request out of scope, passing along unmangled") if not self._sent: self.factory.start_time = datetime.datetime.now() self.transport.write(sendreq.full_request) self.request = sendreq self.request.submitted = True self._sent = True self.data_defer.callback(sendreq) defer.returnValue(None)
def return_response(self, response): self.end_time = datetime.datetime.now() log_request(console.printable_data(response.full_response), id=self.connection_id, symbol='<m', verbosity_level=3) mangled_reqrsp_pair = yield mangle.mangle_response(response, self.connection_id) if mangled_reqrsp_pair: log_request(console.printable_data(mangled_reqrsp_pair.response.full_response), id=self.connection_id, symbol='<', verbosity_level=3) mangled_reqrsp_pair.time_start = self.start_time mangled_reqrsp_pair.time_end = self.end_time if context.in_scope(mangled_reqrsp_pair): yield mangled_reqrsp_pair.deep_save() self.data_defer.callback(mangled_reqrsp_pair)
def return_request_pair(self, request): self.end_time = datetime.datetime.now() log_request( printable_data(request.response.full_response), id=self.connection_id, symbol="<m", verbosity_level=3 ) request.time_start = self.start_time request.time_end = self.end_time if context.in_scope(request): to_mangle = copy.copy(self.intercepting_macros).iteritems() if self.save_all: if self.stream_response and not to_mangle: request.async_deep_save() else: yield request.async_deep_save() # if we don't copy it, when we delete a macro from the console, # we get a crash. We do a shallow copy to keep the macro # instances the same. old_rsp = request.response for k, macro in to_mangle: if macro.intercept_responses: if macro.async_rsp: mangled_rsp = yield macro.async_mangle_response(request) else: mangled_rsp = macro.mangle_response(request) if mangled_rsp is None: request.response = None self.data_defer.callback(request) if self.save_all: yield request.async_deep_save() self.log("Response dropped, losing connection") self.transport.loseConnection() defer.returnValue(None) request.response = mangled_rsp if request.response != old_rsp: request.response.unmangled = old_rsp if self.save_all: yield request.async_deep_save() if request.response: log_request( printable_data(request.response.full_response), id=self.connection_id, symbol="<", verbosity_level=3 ) else: self.log("Response out of scope, passing along unmangled") self.data_defer.callback(request) defer.returnValue(None)
def _connection_made(self): self.log('Connection established, sending request...', verbosity_level=3) # Make sure to add errback lines = self.request.full_request.splitlines() for l in lines: self.log(l, symbol='>r', verbosity_level=3) sendreq = self.request if context.in_scope(sendreq): if self.factory.save_all: yield sendreq.async_deep_save() ## Run intercepting macros # if we don't copy it, when we delete a macro from the console, # we get a crash. We do a shallow copy to keep the macro # instances the same. to_mangle = copy.copy(self.factory.intercepting_macros).iteritems() for k, macro in to_mangle: if macro.do_req: if macro.async_req: sendreq = yield macro.async_mangle_request(sendreq) else: sendreq = macro.mangle_request(sendreq) if sendreq is None: self.transport.loseConnection() self.request = None self.data_defer.callback(None) if self.factory.save_all: yield sendreq.async_deep_save() defer.returnValue(None) if sendreq != self.request: sendreq.unmangled = self.request if self.factory.save_all: yield sendreq.async_deep_save() else: self.log("Request out of scope, passing along unmangled") if not self._sent: self.factory.start_time = datetime.datetime.now() self.transport.write(sendreq.full_request) self.request = sendreq self.request.submitted = True self._sent = True self.data_defer.callback(sendreq) defer.returnValue(None)
def buildProtocol(self, addr): from pappyproxy.pappy import session if self.use_as_proxy and context.in_scope(self.request): p = UpstreamHTTPProxyClient(self.request) if 'username' in session.config.http_proxy and 'password' in session.config.http_proxy: username = session.config.http_proxy['username'] password = session.config.http_proxy['password'] p.creds = (username, password) else: p = ProxyClient(self.request) p.factory = self self.log("Building protocol", verbosity_level=3) p.data_defer.addCallback(self.return_request_pair) return p
def buildProtocol(self, addr): from pappyproxy.pappy import session if self.use_as_proxy and context.in_scope(self.request): p = UpstreamHTTPProxyClient(self.request) if 'username' in session.config.http_proxy and 'password' in session.config.http_proxy: username = session.config.http_proxy['username'] password = session.config.http_proxy['password'] p.creds = (username, password) else: p = ProxyClient(self.request) p.factory = self self.log("Building protocol", verbosity_level=3) p.data_defer.addCallback(self.return_request_pair) p.data_defer.addErrback(self._data_defer_errback) return p
def _connection_made(self): self.log('Connection established, sending request...', verbosity_level=3) # Make sure to add errback lines = self.request.full_request.splitlines() for l in lines: self.log(l, symbol='>r', verbosity_level=3) mangled_request = yield mangle.mangle_request(self.request, self.factory.connection_id) if mangled_request is None: self.transport.loseConnection() return if context.in_scope(mangled_request): yield mangled_request.deep_save() if not self._sent: self.transport.write(mangled_request.full_request) self._sent = True self.data_defer.callback(mangled_request.full_request)
def prepare_request(self): """ Prepares request for submitting Saves the associated request with a temporary start time, mangles it, then saves the mangled version with an update start time. Also updates flags and values needed for submitting the request. """ from pappyproxy.pappy import session sendreq = self.request if context.in_scope(sendreq): mangle_macros = copy.copy(self.intercepting_macros) self.request.time_start = datetime.datetime.utcnow() if self.save_all: if self.stream_response and not mangle_macros: self.request.async_deep_save() else: yield self.request.async_deep_save() (mangreq, mangled) = yield macros.mangle_request(sendreq, mangle_macros) if mangreq is None: self.log("Request dropped. Closing connections.") self.request.tags.add('dropped') self.request.response = None self.dropped_request = True defer.returnValue(None) else: sendreq = mangreq if sendreq and mangled and self.save_all: self.start_time = datetime.datetime.utcnow() sendreq.time_start = self.start_time yield sendreq.async_deep_save() if session.config.http_proxy: self.use_as_proxy = True if (not self.stream_response) and self.sendback_function: self.data_defer.addCallback(self.sendback_function) else: self.log("Request out of scope, passing along unmangled") self.request = sendreq defer.returnValue(self.request)
def return_request_pair(self, request): """ If the request is in scope, it saves the completed request, sets the start/end time, mangles the response, saves the mangled version, then calls back data_defer with the mangled request """ from pappyproxy.pappy import session self.end_time = datetime.datetime.utcnow() if session.config.debug_to_file or session.config.debug_verbosity > 0 and request.response: log_request(printable_data(request.response.full_response), id=self.connection_id, symbol='<m', verbosity_level=3) request.time_start = self.start_time request.time_end = self.end_time if context.in_scope(request): mangle_macros = copy.copy(self.intercepting_macros) if self.save_all: if self.stream_response and not mangle_macros: request.async_deep_save() else: yield request.async_deep_save() if request.response: mangled = yield macros.mangle_response(request, mangle_macros) if mangled and self.save_all: yield request.async_deep_save() if request.response and (session.config.debug_to_file or session.config.debug_verbosity > 0): log_request(printable_data(request.response.full_response), id=self.connection_id, symbol='<', verbosity_level=3) else: self.log("Response out of scope, passing along unmangled") self.data_defer.callback(request) defer.returnValue(None)
def connect(self): from pappyproxy.pappy import session yield self.prepare_request() if self.dropped_request: self.data_defer.callback(self.request) defer.returnValue(None) if context.in_scope(self.request): # Get connection using config endpoint = get_endpoint(self.request.host, self.request.port, self.request.is_ssl, socks_config=session.config.socks_proxy, use_http_proxy=True) else: # Just forward it normally endpoint = get_endpoint(self.request.host, self.request.port, self.request.is_ssl, socks_config=None, use_http_proxy=False) if self._use_string_transport: from pappyproxy.tests.testutil import TLSStringTransport # "Connect" via string transport protocol = self.buildProtocol(('127.0.0.1', 0)) # Pass the protocol back to the test if self._conn_info: self._conn_info['protocol'] = protocol tr = TLSStringTransport() protocol.makeConnection(tr) else: # Connect via the endpoint self.log("Accessing using endpoint") yield endpoint.connect(self) self.log("Connected")