def _setup_connection(self): client = netlib.tcp.TCPClient(("127.0.0.1", self.proxy.port)) client.connect() # send CONNECT request client.wfile.write( http1.assemble_request( netlib.http.Request( 'authority', b'CONNECT', b'', b'localhost', self.server.server.address.port, b'/', b'HTTP/1.1', [(b'host', b'localhost:%d' % self.server.server.address.port)], b'', ))) client.wfile.flush() # read CONNECT response while client.rfile.readline() != b"\r\n": pass client.convert_to_ssl(alpn_protos=[b'h2']) h2_conn = h2.connection.H2Connection(client_side=True, header_encoding=False) h2_conn.initiate_connection() client.wfile.write(h2_conn.data_to_send()) client.wfile.flush() return client, h2_conn
def _setup_connection(self): client = netlib.tcp.TCPClient(("127.0.0.1", self.proxy.port)) client.connect() # send CONNECT request client.wfile.write( http1.assemble_request( netlib.http.Request( "authority", b"CONNECT", b"", b"localhost", self.server.server.address.port, b"/", b"HTTP/1.1", [(b"host", b"localhost:%d" % self.server.server.address.port)], b"", ) ) ) client.wfile.flush() # read CONNECT response while client.rfile.readline() != b"\r\n": pass client.convert_to_ssl(alpn_protos=[b"h2"]) h2_conn = h2.connection.H2Connection(client_side=True, header_encoding=False) h2_conn.initiate_connection() client.wfile.write(h2_conn.data_to_send()) client.wfile.flush() return client, h2_conn
def write(self, flow): request = assemble_request(flow.request) response = assemble_response(flow.response) self._file.writestr("raw/{session}_c.txt".format(session=self._count), request) self._file.writestr("raw/{session}_s.txt".format(session=self._count), response) metadata = self._create_metadata(flow) if metadata: self._file.writestr("raw/{session}_m.xml".format(session=self._count), metadata) self._count += 1
def test_simple(self): sc = ServerConnection((self.d.IFACE, self.d.port)) sc.connect() f = tutils.tflow() f.server_conn = sc f.request.path = "/p/200:da" # use this protocol just to assemble - not for actual sending sc.wfile.write(http1.assemble_request(f.request)) sc.wfile.flush() assert http1.read_response(sc.rfile, f.request, 1000) assert self.d.last_log() sc.finish()
def test_simple(self): self.d = test.Daemon() sc = ServerConnection((self.d.IFACE, self.d.port)) sc.connect() f = tutils.tflow() f.server_conn = sc f.request.path = "/p/200:da" # use this protocol just to assemble - not for actual sending sc.wfile.write(http1.assemble_request(f.request)) sc.wfile.flush() assert http1.read_response(sc.rfile, f.request, 1000) assert self.d.last_log() sc.finish() self.d.shutdown()
def send_request(self, request): self.server_conn.wfile.write(http1.assemble_request(request)) self.server_conn.wfile.flush()
def run(self): r = self.flow.request first_line_format_backup = r.first_line_format try: self.flow.response = None # If we have a channel, run script hooks. if self.channel: request_reply = self.channel.ask("request", self.flow) if isinstance(request_reply, HTTPResponse): self.flow.response = request_reply if not self.flow.response: # In all modes, we directly connect to the server displayed if self.config.mode == "upstream": server_address = self.config.upstream_server.address server = ServerConnection(server_address, (self.config.host, 0)) server.connect() if r.scheme == "https": connect_request = make_connect_request((r.host, r.port)) server.wfile.write(http1.assemble_request(connect_request)) server.wfile.flush() resp = http1.read_response( server.rfile, connect_request, body_size_limit=self.config.body_size_limit ) if resp.status_code != 200: raise ReplayException("Upstream server refuses CONNECT request") server.establish_ssl( self.config.clientcerts, sni=self.flow.server_conn.sni ) r.first_line_format = "relative" else: r.first_line_format= "absolute" else: server_address = (r.host, r.port) server = ServerConnection(server_address, (self.config.host, 0)) server.connect() if r.scheme == "https": server.establish_ssl( self.config.clientcerts, sni=self.flow.server_conn.sni ) r.first_line_format = "relative" server.wfile.write(http1.assemble_request(r)) server.wfile.flush() self.flow.server_conn = server self.flow.response = HTTPResponse.wrap(http1.read_response( server.rfile, r, body_size_limit=self.config.body_size_limit )) if self.channel: response_reply = self.channel.ask("response", self.flow) if response_reply == Kill: raise Kill() except (ReplayException, HttpException, TcpException) as e: self.flow.error = Error(str(e)) if self.channel: self.channel.ask("error", self.flow) except Kill: # Kill should only be raised if there's a channel in the # first place. from ..proxy.root_context import Log self.channel.tell("log", Log("Connection killed", "info")) except Exception: from ..proxy.root_context import Log self.channel.tell("log", Log(traceback.format_exc(), "error")) finally: r.first_line_format = first_line_format_backup
def run(self): r = self.flow.request form_out_backup = r.form_out try: self.flow.response = None # If we have a channel, run script hooks. #if self.channel: # request_reply = self.channel.ask("request", self.flow) # if request_reply == Kill: # raise Kill() # elif isinstance(request_reply, HTTPResponse): # self.flow.response = request_reply if not self.flow.response: # In all modes, we directly connect to the server displayed if self.config.mode == "upstream": print("In upstream replay") print(r.scheme) print(r.method) print("host [%s] port [%d]" % (r.host, r.port)) print(self.flow.server_conn.address) server = ServerConnection(self.flow.server_conn.address, (self.config.host, 0)) server.connect() print("connected") #print(r) if (r.method == "CONNECT"): print('HTTPS replay') connect_request = make_connect_request( (r.host, r.port)) print(r) server.wfile.write(http1.assemble_request(r)) server.wfile.flush() resp = http1.read_response( server.rfile, connect_request, body_size_limit=self.config.body_size_limit) self.flow.response = HTTPResponse.wrap(resp) print('https resp: %s' % self.flow.response) if resp.status_code != 200: raise ReplayException( "Upstream server refuses CONNECT request") server.establish_ssl(self.config.clientcerts, sni=self.flow.server_conn.sni) r.form_out = "relative" return else: if (r.scheme == 'https'): #need to resend CONNECT request and establish before GET print('GET in HTTPS') r.form_out = "authority" connect_request = make_connect_request( (r.host, r.port)) oldmethod = r.method r.method = 'CONNECT' oldpath = r.path r.path = None server.wfile.write(http1.assemble_request(r)) server.wfile.flush() resp = http1.read_response( server.rfile, connect_request, body_size_limit=self.config.body_size_limit) self.flow.response = HTTPResponse.wrap(resp) print('https resp: %s' % self.flow.response) if resp.status_code != 200: raise ReplayException( "Upstream server refuses CONNECT request") #set the old request and change to relative to keep #it under the same host r.method = oldmethod r.path = oldpath r.form_out = 'relative' server.establish_ssl(self.config.clientcerts, sni=self.flow.server_conn.sni) print(r) else: r.form_out = 'absolute' else: print("in NOT upstream replay") server_address = (r.host, r.port) server = ServerConnection(server_address, (self.config.host, 0)) server.connect() if r.scheme == "https": server.establish_ssl(self.config.clientcerts, sni=self.flow.server_conn.sni) r.form_out = "relative" msg = http1.assemble_request(r) print('msg: %s' % msg) server.wfile.write(msg) #print("set current server to flush") server.wfile.flush() print("set current server to main server_conn") print(server) self.flow.server_conn = server myResponse = http1.read_response( server.rfile, r, body_size_limit=self.config.body_size_limit) self.flow.response = HTTPResponse.wrap(myResponse) print('myresp: %s' % self.flow.response) #self.flow.client_conn.send(http1.assemble_response(myResponse)) if self.channel: response_reply = self.channel.ask("response", self.flow) if response_reply == Kill: print("raise kill") raise Kill() except (ReplayException, HttpException, TcpException) as e: self.flow.error = Error(str(e)) if self.channel: self.channel.ask("error", self.flow) except Kill: # Kill should only be raised if there's a channel in the # first place. from ..proxy.root_context import Log self.channel.tell("log", Log("Connection killed", "info")) except Exception as e: from ..proxy.root_context import Log print(e) #self.channel.tell("log", Log(traceback.format_exc(), "error")) finally: r.form_out = form_out_backup print("End Replay Thread")
def run(self): r = self.flow.request form_out_backup = r.form_out try: self.flow.response = None # If we have a channel, run script hooks. #if self.channel: # request_reply = self.channel.ask("request", self.flow) # if request_reply == Kill: # raise Kill() # elif isinstance(request_reply, HTTPResponse): # self.flow.response = request_reply if not self.flow.response: # In all modes, we directly connect to the server displayed if self.config.mode == "upstream": print("In upstream replay") print(r.scheme) print(r.method) print("host [%s] port [%d]" % (r.host, r.port)) print(self.flow.server_conn.address) server = ServerConnection(self.flow.server_conn.address, (self.config.host, 0)) server.connect() print("connected") #print(r) if (r.method == "CONNECT"): print('HTTPS replay') connect_request = make_connect_request((r.host, r.port)) print(r) server.wfile.write(http1.assemble_request(r)) server.wfile.flush() resp = http1.read_response( server.rfile, connect_request, body_size_limit=self.config.body_size_limit ) self.flow.response = HTTPResponse.wrap(resp) print ('https resp: %s' % self.flow.response) if resp.status_code != 200: raise ReplayException("Upstream server refuses CONNECT request") server.establish_ssl( self.config.clientcerts, sni=self.flow.server_conn.sni ) r.form_out = "relative" return else: if (r.scheme == 'https'): #need to resend CONNECT request and establish before GET print('GET in HTTPS') r.form_out = "authority" connect_request = make_connect_request((r.host, r.port)) oldmethod = r.method r.method = 'CONNECT' oldpath = r.path r.path = None server.wfile.write(http1.assemble_request(r)) server.wfile.flush() resp = http1.read_response( server.rfile, connect_request, body_size_limit=self.config.body_size_limit ) self.flow.response = HTTPResponse.wrap(resp) print ('https resp: %s' % self.flow.response) if resp.status_code != 200: raise ReplayException("Upstream server refuses CONNECT request") #set the old request and change to relative to keep #it under the same host r.method = oldmethod r.path = oldpath r.form_out = 'relative' server.establish_ssl( self.config.clientcerts, sni=self.flow.server_conn.sni ) print(r) else: r.form_out = 'absolute' else: print("in NOT upstream replay") server_address = (r.host, r.port) server = ServerConnection(server_address, (self.config.host, 0)) server.connect() if r.scheme == "https": server.establish_ssl( self.config.clientcerts, sni=self.flow.server_conn.sni ) r.form_out = "relative" msg = http1.assemble_request(r) print('msg: %s' % msg) server.wfile.write(msg) #print("set current server to flush") server.wfile.flush() print("set current server to main server_conn") print(server) self.flow.server_conn = server myResponse = http1.read_response( server.rfile, r, body_size_limit=self.config.body_size_limit ) self.flow.response = HTTPResponse.wrap(myResponse) print('myresp: %s' % self.flow.response) #self.flow.client_conn.send(http1.assemble_response(myResponse)) if self.channel: response_reply = self.channel.ask("response", self.flow) if response_reply == Kill: print("raise kill") raise Kill() except (ReplayException, HttpException, TcpException) as e: self.flow.error = Error(str(e)) if self.channel: self.channel.ask("error", self.flow) except Kill: # Kill should only be raised if there's a channel in the # first place. from ..proxy.root_context import Log self.channel.tell("log", Log("Connection killed", "info")) except Exception as e: from ..proxy.root_context import Log print(e) #self.channel.tell("log", Log(traceback.format_exc(), "error")) finally: r.form_out = form_out_backup print("End Replay Thread")