def test_stream_modify(tctx): """Test HTTP stream modification""" server = Placeholder(Server) flow = Placeholder(HTTPFlow) def enable_streaming(flow: HTTPFlow): if flow.response is None: flow.request.stream = lambda x: b"[" + x + b"]" else: flow.response.stream = lambda x: b"[" + x + b"]" assert (Playbook(http.HttpLayer(tctx, HTTPMode.regular)) >> DataReceived( tctx.client, b"POST http://example.com/ HTTP/1.1\r\n" b"Host: example.com\r\n" b"Transfer-Encoding: chunked\r\n\r\n" b"3\r\nabc\r\n" b"0\r\n\r\n") << http.HttpRequestHeadersHook(flow) >> reply(side_effect=enable_streaming) << OpenConnection(server) >> reply(None) << SendData( server, b"POST / HTTP/1.1\r\n" b"Host: example.com\r\n" b"Transfer-Encoding: chunked\r\n\r\n" b"5\r\n[abc]\r\n" b"2\r\n[]\r\n") << http.HttpRequestHook(flow) >> reply() << SendData(server, b"0\r\n\r\n") >> DataReceived( server, b"HTTP/1.1 200 OK\r\n" b"Transfer-Encoding: chunked\r\n\r\n" b"3\r\ndef\r\n" b"0\r\n\r\n") << http.HttpResponseHeadersHook(flow) >> reply(side_effect=enable_streaming) << SendData( tctx.client, b"HTTP/1.1 200 OK\r\n" b"Transfer-Encoding: chunked\r\n\r\n" b"5\r\n[def]\r\n" b"2\r\n[]\r\n") << http.HttpResponseHook(flow) >> reply() << SendData(tctx.client, b"0\r\n\r\n"))
def test_https_proxy(strategy, tctx): """Test a CONNECT request, followed by a HTTP GET /""" server = Placeholder(Server) flow = Placeholder(HTTPFlow) playbook = Playbook(http.HttpLayer(tctx, HTTPMode.regular)) tctx.options.connection_strategy = strategy (playbook >> DataReceived(tctx.client, b"CONNECT example.proxy:80 HTTP/1.1\r\n\r\n") << http.HttpConnectHook(Placeholder()) >> reply()) if strategy == "eager": (playbook << OpenConnection(server) >> reply(None)) (playbook << SendData(tctx.client, b'HTTP/1.1 200 Connection established\r\n\r\n') >> DataReceived(tctx.client, b"GET /foo?hello=1 HTTP/1.1\r\nHost: example.com\r\n\r\n") << layer.NextLayerHook(Placeholder()) >> reply_next_layer(lambda ctx: http.HttpLayer(ctx, HTTPMode.transparent)) << http.HttpRequestHeadersHook(flow) >> reply() << http.HttpRequestHook(flow) >> reply()) if strategy == "lazy": (playbook << OpenConnection(server) >> reply(None)) (playbook << SendData( server, b"GET /foo?hello=1 HTTP/1.1\r\nHost: example.com\r\n\r\n") >> DataReceived( server, b"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World!") << http.HttpResponseHeadersHook(flow) >> reply() << http.HttpResponseHook(flow) >> reply() << SendData( tctx.client, b"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World!")) assert playbook
def test_reverse_proxy(tctx, keep_host_header): """Test mitmproxy in reverse proxy mode. - make sure that we connect to the right host - make sure that we respect keep_host_header - make sure that we include non-standard ports in the host header (#4280) """ server = Placeholder(Server) tctx.options.mode = "reverse:http://localhost:8000" tctx.options.connection_strategy = "lazy" tctx.options.keep_host_header = keep_host_header assert ( Playbook(modes.ReverseProxy(tctx), hooks=False) >> DataReceived( tctx.client, b"GET /foo HTTP/1.1\r\n" b"Host: example.com\r\n\r\n") << NextLayerHook( Placeholder(NextLayer)) >> reply_next_layer(lambda ctx: http.HttpLayer(ctx, HTTPMode.transparent)) << OpenConnection(server) >> reply(None) << SendData( server, b"GET /foo HTTP/1.1\r\n" b"Host: " + (b"example.com" if keep_host_header else b"localhost:8000") + b"\r\n\r\n") >> DataReceived( server, b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n") << SendData(tctx.client, b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")) assert server().address == ("localhost", 8000)
def test_kill_stream(tctx): """Test that we can kill individual streams.""" playbook, cff = start_h2_client(tctx) flow1 = Placeholder(HTTPFlow) flow2 = Placeholder(HTTPFlow) req_headers_hook_1 = http.HttpRequestHeadersHook(flow1) def kill(flow: HTTPFlow): # Can't use flow.kill() here because that currently still depends on a reply object. flow.error = Error(Error.KILLED_MESSAGE) server = Placeholder(Server) data_req1 = Placeholder(bytes) assert (playbook >> DataReceived( tctx.client, cff.build_headers_frame( example_request_headers, flags=["END_STREAM"], stream_id=1).serialize() + cff.build_headers_frame( example_request_headers, flags=["END_STREAM"], stream_id=3).serialize()) << req_headers_hook_1 << http.HttpRequestHeadersHook(flow2) >> reply(side_effect=kill) << http.HttpErrorHook(flow2) >> reply() << SendData( tctx.client, cff.build_rst_stream_frame( 3, error_code=ErrorCodes.INTERNAL_ERROR).serialize()) >> reply(to=req_headers_hook_1) << http.HttpRequestHook(flow1) >> reply() << OpenConnection(server) >> reply( None, side_effect=make_h2) << SendData(server, data_req1)) frames = decode_frames(data_req1()) assert [type(x) for x in frames] == [ hyperframe.frame.SettingsFrame, hyperframe.frame.HeadersFrame, ]
def test_disconnect_while_intercept(tctx): """Test a server disconnect while a request is intercepted.""" tctx.options.connection_strategy = "eager" server1 = Placeholder(Server) server2 = Placeholder(Server) flow = Placeholder(HTTPFlow) assert ( Playbook(http.HttpLayer(tctx, HTTPMode.regular), hooks=False) >> DataReceived(tctx.client, b"CONNECT example.com:80 HTTP/1.1\r\n\r\n") << http.HttpConnectHook(Placeholder(HTTPFlow)) >> reply() << OpenConnection(server1) >> reply(None) << SendData(tctx.client, b'HTTP/1.1 200 Connection established\r\n\r\n') >> DataReceived(tctx.client, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") << layer.NextLayerHook(Placeholder()) >> reply_next_layer(lambda ctx: http.HttpLayer(ctx, HTTPMode.transparent)) << http.HttpRequestHook(flow) >> ConnectionClosed(server1) << CloseConnection(server1) >> reply(to=-3) << OpenConnection(server2) >> reply(None) << SendData(server2, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") >> DataReceived(server2, b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n") << SendData(tctx.client, b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n") ) assert server1() != server2() assert flow().server_conn == server2()
def test_proxy_chain(tctx, strategy): server = Placeholder(Server) tctx.options.connection_strategy = strategy playbook = Playbook(http.HttpLayer(tctx, HTTPMode.regular), hooks=False) playbook >> DataReceived(tctx.client, b"CONNECT proxy:8080 HTTP/1.1\r\n\r\n") if strategy == "eager": playbook << OpenConnection(server) playbook >> reply(None) playbook << SendData(tctx.client, b"HTTP/1.1 200 Connection established\r\n\r\n") playbook >> DataReceived(tctx.client, b"CONNECT second-proxy:8080 HTTP/1.1\r\n\r\n") playbook << layer.NextLayerHook(Placeholder()) playbook >> reply_next_layer( lambda ctx: http.HttpLayer(ctx, HTTPMode.transparent)) playbook << SendData( tctx.client, b"HTTP/1.1 502 Bad Gateway\r\n" b"content-length: 198\r\n" b"\r\n" b"mitmproxy received an HTTP CONNECT request even though it is not running in regular/upstream mode. " b"This usually indicates a misconfiguration, please see the mitmproxy mode documentation for details." ) assert playbook
def test_response_streaming(tctx): """Test HTTP response streaming""" server = Placeholder(Server) flow = Placeholder(HTTPFlow) def enable_streaming(flow: HTTPFlow): flow.response.stream = lambda x: x.upper() assert ( Playbook(http.HttpLayer(tctx, HTTPMode.regular)) >> DataReceived(tctx.client, b"GET http://example.com/largefile HTTP/1.1\r\nHost: example.com\r\n\r\n") << http.HttpRequestHeadersHook(flow) >> reply() << http.HttpRequestHook(flow) >> reply() << OpenConnection(server) >> reply(None) << SendData(server, b"GET /largefile HTTP/1.1\r\nHost: example.com\r\n\r\n") >> DataReceived(server, b"HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\nabc") << http.HttpResponseHeadersHook(flow) >> reply(side_effect=enable_streaming) << SendData(tctx.client, b"HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\nABC") >> DataReceived(server, b"def") << SendData(tctx.client, b"DEF") << http.HttpResponseHook(flow) >> reply() )
def test_simple(self, tctx, pipeline): req = http.Request.make("GET", "http://example.com/") resp = Placeholder(ResponseHeaders) playbook = Playbook(Http1Client(tctx)) ( playbook >> RequestHeaders(1, req, True) << SendData(tctx.server, b"GET / HTTP/1.1\r\ncontent-length: 0\r\n\r\n") >> RequestEndOfMessage(1) ) if pipeline: with pytest.raises(AssertionError, match="assert self.stream_id == event.stream_id"): assert (playbook >> RequestHeaders(3, req, True) ) return assert ( playbook >> DataReceived(tctx.server, b"HTTP/1.1 200 OK\r\ncontent-length: 0\r\n\r\n") << ReceiveHttp(resp) << ReceiveHttp(ResponseEndOfMessage(1)) # no we can send the next request >> RequestHeaders(3, req, True) << SendData(tctx.server, b"GET / HTTP/1.1\r\ncontent-length: 0\r\n\r\n") ) assert resp().response.status_code == 200
def test_request_trailers(tctx: Context, open_h2_server_conn: Server, stream): playbook, cff = start_h2_client(tctx) tctx.server = open_h2_server_conn def enable_streaming(flow: HTTPFlow): flow.request.stream = bool(stream) flow = Placeholder(HTTPFlow) server_data1 = Placeholder(bytes) server_data2 = Placeholder(bytes) (playbook >> DataReceived( tctx.client, cff.build_headers_frame(example_request_headers).serialize() + cff.build_data_frame(b"Hello, World!").serialize()) << http.HttpRequestHeadersHook(flow) >> reply(side_effect=enable_streaming)) if stream: playbook << SendData(tctx.server, server_data1) assert (playbook >> DataReceived( tctx.client, cff.build_headers_frame(example_request_trailers, flags=[ "END_STREAM" ]).serialize()) << http.HttpRequestHook(flow) >> reply() << SendData( tctx.server, server_data2)) frames = decode_frames(server_data1.setdefault(b"") + server_data2()) assert [type(x) for x in frames] == [ hyperframe.frame.SettingsFrame, hyperframe.frame.HeadersFrame, hyperframe.frame.DataFrame, hyperframe.frame.HeadersFrame, ]
def test_server_aborts(tctx, data): """Test the scenario where the server doesn't serve a response""" server = Placeholder(Server) flow = Placeholder(HTTPFlow) err = Placeholder(bytes) playbook = Playbook(http.HttpLayer(tctx, HTTPMode.regular), hooks=False) assert ( playbook >> DataReceived(tctx.client, b"GET http://example.com/ HTTP/1.1\r\nHost: example.com\r\n\r\n") << OpenConnection(server) >> reply(None) << SendData(server, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") ) if data: playbook >> DataReceived(server, data) assert ( playbook >> ConnectionClosed(server) << CloseConnection(server) << http.HttpErrorHook(flow) >> reply() << SendData(tctx.client, err) << CloseConnection(tctx.client) ) assert flow().error assert b"502 Bad Gateway" in err()
def test_simple(tctx): playbook, cff = start_h2_client(tctx) flow = Placeholder(HTTPFlow) server = Placeholder(Server) initial = Placeholder(bytes) assert (playbook >> DataReceived( tctx.client, cff.build_headers_frame(example_request_headers, flags=[ "END_STREAM" ]).serialize()) << http.HttpRequestHeadersHook(flow) >> reply() << http.HttpRequestHook(flow) >> reply() << OpenConnection(server) >> reply(None, side_effect=make_h2) << SendData(server, initial)) frames = decode_frames(initial()) assert [type(x) for x in frames] == [ hyperframe.frame.SettingsFrame, hyperframe.frame.HeadersFrame, ] sff = FrameFactory() assert ( playbook # a conforming h2 server would send settings first, we disregard this for now. >> DataReceived( server, sff.build_headers_frame(example_response_headers).serialize()) << http.HttpResponseHeadersHook(flow) >> reply() >> DataReceived( server, sff.build_data_frame(b"Hello, World!", flags=["END_STREAM" ]).serialize()) << http.HttpResponseHook(flow) >> reply() << SendData( tctx.client, cff.build_headers_frame(example_response_headers).serialize() + cff.build_data_frame(b"Hello, World!").serialize() + cff.build_data_frame(b"", flags=["END_STREAM"]).serialize())) assert flow().request.url == "http://example.com/" assert flow().response.text == "Hello, World!"
def test_simple(tctx): """open connection, receive data, send it to peer""" f = Placeholder(TCPFlow) assert ( Playbook(tcp.TCPLayer(tctx)) << tcp.TcpStartHook(f) >> reply() << OpenConnection(tctx.server) >> reply(None) >> DataReceived(tctx.client, b"hello!") << tcp.TcpMessageHook(f) >> reply() << SendData(tctx.server, b"hello!") >> DataReceived(tctx.server, b"hi") << tcp.TcpMessageHook(f) >> reply() << SendData(tctx.client, b"hi") >> ConnectionClosed(tctx.server) << CloseConnection(tctx.client, half_close=True) >> ConnectionClosed(tctx.client) << CloseConnection(tctx.server) << tcp.TcpEndHook(f) >> reply() >> ConnectionClosed(tctx.client) << None ) assert len(f().messages) == 2
def test_pipelining(tctx, transfer_encoding): """Test that multiple requests can be processed over the same connection""" tctx.server.address = ("example.com", 80) tctx.server.state = ConnectionState.OPEN req = b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n" if transfer_encoding == "identity": resp = (b"HTTP/1.1 200 OK\r\n" b"Content-Length: 12\r\n" b"\r\n" b"Hello World!") else: resp = (b"HTTP/1.1 200 OK\r\n" b"Transfer-Encoding: chunked\r\n" b"\r\n" b"c\r\n" b"Hello World!\r\n" b"0\r\n" b"\r\n") assert ( Playbook(http.HttpLayer(tctx, HTTPMode.transparent), hooks=False) # Roundtrip 1 >> DataReceived(tctx.client, req) << SendData(tctx.server, req) >> DataReceived(tctx.server, resp) << SendData(tctx.client, resp) # Roundtrip 2 >> DataReceived(tctx.client, req) << SendData(tctx.server, req) >> DataReceived(tctx.server, resp) << SendData(tctx.client, resp) )
def test_no_data_on_closed_stream(self, tctx): frame_factory = FrameFactory() req = Request.make("GET", "http://example.com/") resp = {":status": 200} assert ( Playbook(Http2Client(tctx)) << SendData( tctx.server, Placeholder(bytes)) # preamble + initial settings frame >> DataReceived( tctx.server, frame_factory.build_settings_frame({}, ack=True).serialize()) >> http.RequestHeaders(1, req, end_stream=True) << SendData( tctx.server, b"\x00\x00\x06\x01\x05\x00\x00\x00\x01\x82\x86\x84\\\x81\x07") >> http.RequestEndOfMessage(1) >> DataReceived( tctx.server, frame_factory.build_headers_frame(resp).serialize()) << http.ReceiveHttp(Placeholder( http.ResponseHeaders)) >> http.RequestProtocolError( 1, "cancelled", code=status_codes.CLIENT_CLOSED_REQUEST) << SendData( tctx.server, frame_factory.build_rst_stream_frame( 1, ErrorCodes.CANCEL).serialize()) >> DataReceived( tctx.server, frame_factory.build_data_frame(b"foo").serialize()) << SendData( tctx.server, frame_factory.build_rst_stream_frame( 1, ErrorCodes.STREAM_CLOSED).serialize()) ) # important: no ResponseData event here!
def test_tunnel_handshake_command(tctx: Context, success): server = Server(("proxy", 1234)) tl = TTunnelLayer(tctx, server, tctx.server) tl.child_layer = TChildLayer(tctx) playbook = Playbook(tl, logs=True) (playbook << Log("Got start. Server state: CLOSED") >> DataReceived( tctx.client, b"client-hello") << SendData( tctx.client, b"client-hello-reply") >> DataReceived( tctx.client, b"open") << OpenConnection(server) >> reply(None) << SendData(server, b"handshake-hello") >> DataReceived( server, b"handshake-" + success.encode()) << SendData( server, b"handshake-" + success.encode())) if success == "success": assert (playbook << Log(f"Opened: err=None. Server state: OPEN") >> DataReceived(server, b"tunneled-server-hello") << SendData( server, b"tunneled-server-hello-reply") >> ConnectionClosed(tctx.client) << Log("Got client close.") << CloseConnection(tctx.client)) assert tl.tunnel_state is tunnel.TunnelState.OPEN assert (playbook >> ConnectionClosed(server) << Log("Got server close.") << CloseConnection(server)) assert tl.tunnel_state is tunnel.TunnelState.CLOSED else: assert (playbook << CloseConnection(server) << Log("Opened: err='handshake error'. Server state: CLOSED")) assert tl.tunnel_state is tunnel.TunnelState.CLOSED
def test_early_server_data(tctx): playbook, cff = start_h2_client(tctx) sff = FrameFactory() tctx.server.address = ("example.com", 80) tctx.server.state = ConnectionState.OPEN tctx.server.alpn = b"h2" flow = Placeholder(HTTPFlow) server1 = Placeholder(bytes) server2 = Placeholder(bytes) assert ( playbook >> DataReceived(tctx.client, cff.build_headers_frame(example_request_headers, flags=["END_STREAM"]).serialize()) << http.HttpRequestHeadersHook(flow) >> reply() << (h := http.HttpRequestHook(flow)) # Surprise! We get data from the server before the request hook finishes. >> DataReceived(tctx.server, sff.build_settings_frame({}).serialize()) << SendData(tctx.server, server1) # Request hook finishes... >> reply(to=h) << SendData(tctx.server, server2) ) assert [type(x) for x in decode_frames(server1())] == [ hyperframe.frame.SettingsFrame, hyperframe.frame.SettingsFrame, ] assert [type(x) for x in decode_frames(server2())] == [ hyperframe.frame.HeadersFrame, ]
def test_tunnel_handshake_start(tctx: Context, success): server = Server(("proxy", 1234)) server.state = ConnectionState.OPEN tl = TTunnelLayer(tctx, server, tctx.server) tl.child_layer = TChildLayer(tctx) assert repr(tl) playbook = Playbook(tl, logs=True) (playbook << SendData(server, b"handshake-hello") >> DataReceived( tctx.client, b"client-hello") >> DataReceived( server, b"handshake-" + success.encode()) << SendData( server, b"handshake-" + success.encode())) if success == "success": playbook << Log("Got start. Server state: OPEN") else: playbook << CloseConnection(server) playbook << Log("Got start. Server state: CLOSED") playbook << SendData(tctx.client, b"client-hello-reply") if success == "success": playbook >> DataReceived(server, b"tunneled-server-hello") playbook << SendData(server, b"tunneled-server-hello-reply") assert playbook
def test_disconnect_during_handshake_command(tctx: Context, disconnect): server = Server(("proxy", 1234)) tl = TTunnelLayer(tctx, server, tctx.server) tl.child_layer = TChildLayer(tctx) playbook = Playbook(tl, logs=True) assert ( playbook << Log("Got start. Server state: CLOSED") >> DataReceived(tctx.client, b"client-hello") << SendData(tctx.client, b"client-hello-reply") >> DataReceived(tctx.client, b"open") << OpenConnection(server) >> reply(None) << SendData(server, b"handshake-hello") ) if disconnect == "client": assert ( playbook >> ConnectionClosed(tctx.client) >> ConnectionClosed(server) # proxyserver will cancel all other connections as well. << CloseConnection(server) << Log("Opened: err='connection closed without notice'. Server state: CLOSED") << Log("Got client close.") << CloseConnection(tctx.client) ) else: assert ( playbook >> ConnectionClosed(server) << CloseConnection(server) << Log("Opened: err='connection closed without notice'. Server state: CLOSED") )
def test_h1_to_h2(tctx): """Test HTTP/1 -> HTTP/2 request translation""" server = Placeholder(Server) flow = Placeholder(HTTPFlow) playbook = Playbook(http.HttpLayer(tctx, HTTPMode.regular)) conf = h2.config.H2Configuration(client_side=False) conn = h2.connection.H2Connection(conf) conn.initiate_connection() request = Placeholder(bytes) assert (playbook >> DataReceived( tctx.client, b"GET http://example.com/ HTTP/1.1\r\nHost: example.com\r\n\r\n") << http.HttpRequestHeadersHook(flow) >> reply() << http.HttpRequestHook(flow) >> reply() << OpenConnection(server) >> reply(None, side_effect=make_h2) << SendData(server, request)) events = conn.receive_data(request()) assert event_types(events) == [ h2.events.RemoteSettingsChanged, h2.events.RequestReceived, h2.events.StreamEnded ] conn.send_headers(1, example_response_headers) conn.send_data(1, b"Hello World!", end_stream=True) settings_ack = Placeholder(bytes) assert ( playbook >> DataReceived(server, conn.data_to_send()) << http.HttpResponseHeadersHook(flow) << SendData(server, settings_ack) >> reply(to=-2) << http.HttpResponseHook(flow) >> reply() << SendData( tctx.client, b"HTTP/1.1 200 OK\r\n\r\nHello World!") << CloseConnection(tctx.client)) assert settings_ack() == b'\x00\x00\x00\x04\x01\x00\x00\x00\x00'
def test_h2_to_h1(tctx): """Test HTTP/2 -> HTTP/1 request translation""" server = Placeholder(Server) flow = Placeholder(HTTPFlow) conn, playbook = h2_client(tctx) conn.send_headers(1, example_request_headers, end_stream=True) response = Placeholder(bytes) assert ( playbook >> DataReceived(tctx.client, conn.data_to_send()) << http.HttpRequestHeadersHook(flow) >> reply() << http.HttpRequestHook(flow) >> reply() << OpenConnection(server) >> reply(None) << SendData( server, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") >> DataReceived(server, b"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\n") << http.HttpResponseHeadersHook(flow) >> reply() >> DataReceived( server, b"Hello World!") << http.HttpResponseHook(flow) << CloseConnection(server) >> reply(to=-2) << SendData( tctx.client, response)) events = conn.receive_data(response()) assert event_types(events) == [ h2.events.ResponseReceived, h2.events.DataReceived, h2.events.DataReceived, h2.events.StreamEnded ] resp: h2.events.ResponseReceived = events[0] body: h2.events.DataReceived = events[1] assert resp.headers == [(b':status', b'200'), (b'content-length', b'12')] assert body.data == b"Hello World!"
def test_socks5_trickle(tctx: Context): tctx.options.connection_strategy = "lazy" playbook = Playbook(modes.Socks5Proxy(tctx)) for x in CLIENT_HELLO: playbook >> DataReceived(tctx.client, bytes([x])) playbook << SendData(tctx.client, b"\x05\x00") for x in b"\x05\x01\x00\x01\x7f\x00\x00\x01\x12\x34": playbook >> DataReceived(tctx.client, bytes([x])) assert playbook << SendData(tctx.client, b"\x05\x00\x00\x01\x00\x00\x00\x00\x00\x00")
def test_close_code(ws_testdata): tctx, playbook, flow = ws_testdata assert (playbook << websocket.WebsocketStartHook(flow) >> reply() >> DataReceived(tctx.server, b"\x88\x02\x0f\xa0") << SendData( tctx.server, masked(b"\x88\x02\x0f\xa0")) << CloseConnection( tctx.server) << SendData(tctx.client, b"\x88\x02\x0f\xa0") << CloseConnection( tctx.client) << websocket.WebsocketEndHook(flow) >> reply()) assert flow.websocket.close_code == 4000
def test_close_error(ws_testdata): tctx, playbook, flow = ws_testdata assert (playbook << websocket.WebsocketStartHook(flow) >> reply() >> DataReceived(tctx.server, b"\x88\x02\x0f\xa0") << SendData( tctx.server, masked(b"\x88\x02\x0f\xa0")) << CloseConnection( tctx.server) << SendData(tctx.client, b"\x88\x02\x0f\xa0") << CloseConnection( tctx.client) << websocket.WebsocketErrorHook(flow) >> reply()) assert "UNKNOWN_ERROR=4000" in flow.error.msg
def test_fragmented(ws_testdata): tctx, playbook, flow = ws_testdata assert (playbook << websocket.WebsocketStartHook(flow) >> reply() >> DataReceived(tctx.server, b"\x01\x03foo") >> DataReceived( tctx.server, b"\x80\x03bar") << websocket.WebsocketMessageHook(flow) >> reply() << SendData( tctx.client, b"\x01\x03foo") << SendData( tctx.client, b"\x80\x03bar")) assert flow.websocket.messages[-1].content == b"foobar"
def test_upgrade(tctx): """Test a HTTP -> WebSocket upgrade""" tctx.server.address = ("example.com", 80) tctx.server.state = ConnectionState.OPEN flow = Placeholder(HTTPFlow) assert ( Playbook(http.HttpLayer(tctx, HTTPMode.transparent)) >> DataReceived(tctx.client, b"GET / HTTP/1.1\r\n" b"Connection: upgrade\r\n" b"Upgrade: websocket\r\n" b"Sec-WebSocket-Version: 13\r\n" b"\r\n") << http.HttpRequestHeadersHook(flow) >> reply() << http.HttpRequestHook(flow) >> reply() << SendData(tctx.server, b"GET / HTTP/1.1\r\n" b"Connection: upgrade\r\n" b"Upgrade: websocket\r\n" b"Sec-WebSocket-Version: 13\r\n" b"\r\n") >> DataReceived(tctx.server, b"HTTP/1.1 101 Switching Protocols\r\n" b"Upgrade: websocket\r\n" b"Connection: Upgrade\r\n" b"\r\n") << http.HttpResponseHeadersHook(flow) >> reply() << http.HttpResponseHook(flow) >> reply() << SendData(tctx.client, b"HTTP/1.1 101 Switching Protocols\r\n" b"Upgrade: websocket\r\n" b"Connection: Upgrade\r\n" b"\r\n") << websocket.WebsocketStartHook(flow) >> reply() >> DataReceived(tctx.client, masked_bytes(b"\x81\x0bhello world")) << websocket.WebsocketMessageHook(flow) >> reply() << SendData(tctx.server, masked(b"\x81\x0bhello world")) >> DataReceived(tctx.server, b"\x82\nhello back") << websocket.WebsocketMessageHook(flow) >> reply() << SendData(tctx.client, b"\x82\nhello back") >> DataReceived(tctx.client, masked_bytes(b"\x81\x0bhello again")) << websocket.WebsocketMessageHook(flow) >> reply() << SendData(tctx.server, masked(b"\x81\x0bhello again")) ) assert len(flow().websocket.messages) == 3 assert flow().websocket.messages[0].content == b"hello world" assert flow().websocket.messages[0].from_client assert flow().websocket.messages[0].type == Opcode.TEXT assert flow().websocket.messages[1].content == b"hello back" assert flow().websocket.messages[1].from_client is False assert flow().websocket.messages[1].type == Opcode.BINARY
def test_socks5_eager_err(tctx: Context): tctx.options.connection_strategy = "eager" server = Placeholder(Server) assert (Playbook(modes.Socks5Proxy(tctx)) >> DataReceived( tctx.client, CLIENT_HELLO) << SendData( tctx.client, SERVER_HELLO) >> DataReceived( tctx.client, b"\x05\x01\x00\x01\x7f\x00\x00\x01\x12\x34") << OpenConnection(server) >> reply("out of socks") << SendData( tctx.client, b"\x05\x04\x00\x01\x00\x00\x00\x00\x00\x00") << CloseConnection(tctx.client))
def test_no_headers(tctx): """Test that we can correctly reassemble requests/responses with no headers.""" server = Placeholder(Server) assert ( Playbook(http.HttpLayer(tctx, HTTPMode.regular), hooks=False) >> DataReceived(tctx.client, b"GET http://example.com/ HTTP/1.1\r\n\r\n") << OpenConnection(server) >> reply(None) << SendData( server, b"GET / HTTP/1.1\r\n\r\n") >> DataReceived( server, b"HTTP/1.1 204 No Content\r\n\r\n") << SendData( tctx.client, b"HTTP/1.1 204 No Content\r\n\r\n")) assert server().address == ("example.com", 80)
def test_http_proxy_relative_request(tctx): """Test handling of a relative-form "GET /" in regular proxy mode.""" server = Placeholder(Server) assert (Playbook(http.HttpLayer( tctx, HTTPMode.regular), hooks=False) >> DataReceived( tctx.client, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") << OpenConnection(server) >> reply(None) << SendData( server, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") >> DataReceived(server, b"HTTP/1.1 204 No Content\r\n\r\n") << SendData(tctx.client, b"HTTP/1.1 204 No Content\r\n\r\n")) assert server().address == ("example.com", 80)
def test_receive_data_after_half_close(tctx): """ data received after the other connection has been half-closed should still be forwarded. """ assert (Playbook(tcp.TCPLayer(tctx), hooks=False) << OpenConnection(tctx.server) >> reply(None) >> DataReceived( tctx.client, b"eof-delimited-request") << SendData( tctx.server, b"eof-delimited-request") >> ConnectionClosed( tctx.client) << CloseConnection(tctx.server, half_close=True) >> DataReceived( tctx.server, b"i'm late") << SendData(tctx.client, b"i'm late") >> ConnectionClosed(tctx.server) << CloseConnection(tctx.client))
def test_upgrade_streamed(tctx): """If the HTTP response is streamed, we may get early data from the client.""" tctx.server.address = ("example.com", 80) tctx.server.state = ConnectionState.OPEN flow = Placeholder(HTTPFlow) def enable_streaming(flow: HTTPFlow): flow.response.stream = True assert ( Playbook(http.HttpLayer(tctx, HTTPMode.transparent)) >> DataReceived(tctx.client, b"GET / HTTP/1.1\r\n" b"Connection: upgrade\r\n" b"Upgrade: websocket\r\n" b"Sec-WebSocket-Version: 13\r\n" b"\r\n") << http.HttpRequestHeadersHook(flow) >> reply() << http.HttpRequestHook(flow) >> reply() << SendData(tctx.server, b"GET / HTTP/1.1\r\n" b"Connection: upgrade\r\n" b"Upgrade: websocket\r\n" b"Sec-WebSocket-Version: 13\r\n" b"\r\n") >> DataReceived(tctx.server, b"HTTP/1.1 101 Switching Protocols\r\n" b"Upgrade: websocket\r\n" b"Connection: Upgrade\r\n" b"\r\n") << http.HttpResponseHeadersHook(flow) >> reply(side_effect=enable_streaming) << SendData(tctx.client, b"HTTP/1.1 101 Switching Protocols\r\n" b"Upgrade: websocket\r\n" b"Connection: Upgrade\r\n" b"\r\n") << http.HttpResponseHook(flow) >> DataReceived(tctx.client, masked_bytes(b"\x81\x0bhello world")) # early !! >> reply(to=-2) << websocket.WebsocketStartHook(flow) >> reply() << websocket.WebsocketMessageHook(flow) >> reply() << SendData(tctx.server, masked(b"\x81\x0bhello world")) >> DataReceived(tctx.server, b"\x82\nhello back") << websocket.WebsocketMessageHook(flow) >> reply() << SendData(tctx.client, b"\x82\nhello back") >> DataReceived(tctx.client, masked_bytes(b"\x81\x0bhello again")) << websocket.WebsocketMessageHook(flow) >> reply() << SendData(tctx.server, masked(b"\x81\x0bhello again")) )