def test_gen_filter_by_submitted_cookies(): f1 = context.gen_filter_by_submitted_cookies(context.cmp_contains, 'Session') f2 = context.gen_filter_by_submitted_cookies(context.cmp_contains, 'Cookie', context.cmp_contains, 'CookieVal') r = Request(('GET / HTTP/1.1\r\n' 'Cookie: foo=bar\r\n' '\r\n')) assert not f1(r) assert not f2(r) r = Request(('GET / HTTP/1.1\r\n' 'Cookie: Session=bar\r\n' '\r\n')) assert f1(r) assert not f2(r) r = Request(('GET / HTTP/1.1\r\n' 'Cookie: Session=bar; CookieThing=NoMatch\r\n' '\r\n')) assert f1(r) assert not f2(r) r = Request(('GET / HTTP/1.1\r\n' 'Cookie: Session=bar; CookieThing=CookieValue\r\n' '\r\n')) assert f1(r) assert f2(r)
def test_filter_by_body(): f = context.gen_filter_by_body(['ct', 'sexy']) fn = context.gen_filter_by_body(['nct', 'sexy']) # Test request bodies r = Request() r.start_line = 'GET /sexy HTTP/1.1' r.headers['Header'] = 'sexy' r.body = 'foo' assert not f(r) assert fn(r) r.body = 'sexy' assert f(r) assert not fn(r) # Test response bodies r = Request() rsp = Response() rsp.start_line = 'HTTP/1.1 200 OK' rsp.headers['sexy'] = 'sexy' r.start_line = 'GET /sexy HTTP/1.1' r.headers['Header'] = 'sexy' r.response = rsp assert not f(r) assert fn(r) rsp.body = 'sexy' assert f(r) assert not fn(r)
def test_filter_by_params_post(): f1 = context.gen_filter_by_params(['ct', 'Session']) f2 = context.gen_filter_by_params(['ct', 'Cookie', 'ct', 'CookieVal']) r = Request(('GET / HTTP/1.1\r\n' 'Content-Type: application/x-www-form-urlencoded\r\n\r\n')) r.body = 'foo=bar' assert not f1(r) assert not f2(r) r = Request(('GET / HTTP/1.1\r\n' 'Content-Type: application/x-www-form-urlencoded\r\n\r\n')) r.body = 'Session=bar' assert f1(r) assert not f2(r) r = Request(('GET / HTTP/1.1\r\n' 'Content-Type: application/x-www-form-urlencoded\r\n\r\n')) r.body = 'Session=bar&Cookie=foo' assert f1(r) assert not f2(r) r = Request(('GET / HTTP/1.1\r\n' 'Content-Type: application/x-www-form-urlencoded\r\n\r\n')) r.body = 'Session=bar&CookieThing=CookieValue' assert f1(r) assert f2(r)
def test_filter_by_body(): f = context.gen_filter_by_body(context.cmp_contains, 'sexy') fn = context.gen_filter_by_body(context.cmp_contains, 'sexy', negate=True) # Test request bodies r = Request() r.status_line = 'GET /sexy HTTP/1.1' r.headers['Header'] = 'sexy' r.raw_data = 'foo' assert not f(r) assert fn(r) r.raw_data = 'sexy' assert f(r) assert not fn(r) # Test response bodies r = Request() rsp = Response() rsp.status_line = 'HTTP/1.1 200 OK' rsp.headers['sexy'] = 'sexy' r.status_line = 'GET /sexy HTTP/1.1' r.headers['Header'] = 'sexy' r.response = rsp assert not f(r) assert fn(r) rsp.raw_data = 'sexy' assert f(r) assert not fn(r)
def test_filter_by_raw_headers_response(): f1 = context.gen_filter_by_raw_headers(context.cmp_contains, 'Sexy:') fn1 = context.gen_filter_by_raw_headers(context.cmp_contains, 'Sexy:', negate=True) f2 = context.gen_filter_by_raw_headers(context.cmp_contains, 'sexy\r\nHeader') fn2 = context.gen_filter_by_raw_headers(context.cmp_contains, 'sexy\r\nHeader', negate=True) r = Request('GET / HTTP/1.1\r\n') rsp = Response('HTTP/1.1 200 OK\r\n') r.response = rsp rsp.headers['Header'] = 'Sexy' assert not f1(r) assert fn1(r) assert not f2(r) assert fn2(r) r = Request('GET / HTTP/1.1\r\n') rsp = Response('HTTP/1.1 200 OK\r\n') r.response = rsp rsp.headers['Sexy'] = 'sexy' assert f1(r) assert not fn1(r) assert not f2(r) assert fn2(r) rsp.headers['OtherHeader'] = 'sexy' rsp.headers['Header'] = 'foo' assert f1(r) assert not fn1(r) assert f2(r) assert not fn2(r)
def test_proxy_server_macro_multiple(mocker): proxy = TestProxyConnection() new_req_contents1 = 'GET / HTTP/1.1\r\nMangled: Very yes\r\n\r\n' new_rsp_contents1 = 'HTTP/1.1 200 OKILIE DOKILIE\r\nMangled: Very yes\r\n\r\n' new_req1 = Request(new_req_contents1) new_rsp1 = Response(new_rsp_contents1) new_req_contents2 = 'GET / HTTP/1.1\r\nMangled: Very very yes\r\n\r\n' new_rsp_contents2 = 'HTTP/1.1 200 OKILIE DOKILIE\r\nMangled: Very very yes\r\n\r\n' new_req2 = Request(new_req_contents2) new_rsp2 = Response(new_rsp_contents2) test_macro1 = InterceptMacroTest(new_req=new_req1, new_rsp=new_rsp1) test_macro2 = InterceptMacroTest(new_req=new_req2, new_rsp=new_rsp2) macros = collections.OrderedDict() macros['macro1'] = test_macro1 macros['macro2'] = test_macro2 proxy.setUp(mocker, int_macros=macros) proxy.write_as_browser('GET /serious.php HTTP/1.1\r\n\r\n') assert proxy.read_as_server() == new_req_contents2 proxy.write_as_server('HTTP/1.1 404 NOT FOUND\r\n\r\n') assert proxy.read_as_browser() == new_rsp_contents2
def test_gen_filter_by_all_request(): f = context.gen_filter_by_all(context.cmp_contains, 'hello') fn = context.gen_filter_by_all(context.cmp_contains, 'hello', negate=True) # Nowhere r = Request('GET / HTTP/1.1\r\n') assert not f(r) assert fn(r) # Verb r = Request('hello / HTTP/1.1\r\n') assert f(r) assert not fn(r) # Path r = Request('GET /hello HTTP/1.1\r\n') assert f(r) assert not fn(r) # Data r = Request('GET / HTTP/1.1\r\n') r.raw_data = 'hello' assert f(r) assert not fn(r) # Header key r = Request('GET / HTTP/1.1\r\n') r.headers['hello'] = 'goodbye' assert f(r) assert not fn(r) # Header value r = Request('GET / HTTP/1.1\r\n') r.headers['goodbye'] = 'hello' assert f(r) assert not fn(r) # Nowhere in headers r = Request('GET / HTTP/1.1\r\n') r.headers['goodbye'] = 'for real' assert not f(r) assert fn(r) # Cookie key r = Request('GET / HTTP/1.1\r\n') r.cookies['hello'] = 'world' assert f(r) assert not fn(r) # Cookie value r = Request('GET / HTTP/1.1\r\n') r.cookies['world'] = 'hello' assert f(r) assert not fn(r) # Nowhere in cookie r = Request('GET / HTTP/1.1\r\n') r.cookies['world'] = 'sucks' assert not f(r) assert fn(r)
def test_filter_by_raw_headers_response(): f1 = context.gen_filter_by_raw_headers(['ct', 'Sexy:']) fn1 = context.gen_filter_by_raw_headers(['nct', 'Sexy:']) f2 = context.gen_filter_by_raw_headers(['ct', 'sexy\r\nHeader']) fn2 = context.gen_filter_by_raw_headers(['nct', 'sexy\r\nHeader']) r = Request('GET / HTTP/1.1\r\n') rsp = Response('HTTP/1.1 200 OK\r\n') r.response = rsp rsp.headers['Header'] = 'Sexy' assert not f1(r) assert fn1(r) assert not f2(r) assert fn2(r) r = Request('GET / HTTP/1.1\r\n') rsp = Response('HTTP/1.1 200 OK\r\n') r.response = rsp rsp.headers['Sexy'] = 'sexy' assert f1(r) assert not fn1(r) assert not f2(r) assert fn2(r) rsp.headers['OtherHeader'] = 'sexy' rsp.headers['Header'] = 'foo' assert f1(r) assert not fn1(r) assert f2(r) assert not fn2(r)
def test_gen_filter_by_all_request(): f = context.gen_filter_by_all(['ct', 'hello']) fn = context.gen_filter_by_all(['nct', 'hello']) # Nowhere r = Request('GET / HTTP/1.1\r\n') assert not f(r) assert fn(r) # Verb r = Request('hello / HTTP/1.1\r\n') assert f(r) assert not fn(r) # Path r = Request('GET /hello HTTP/1.1\r\n') assert f(r) assert not fn(r) # Data r = Request('GET / HTTP/1.1\r\n') r.body = 'hello' assert f(r) assert not fn(r) # Header key r = Request('GET / HTTP/1.1\r\n') r.headers['hello'] = 'goodbye' assert f(r) assert not fn(r) # Header value r = Request('GET / HTTP/1.1\r\n') r.headers['goodbye'] = 'hello' assert f(r) assert not fn(r) # Nowhere in headers r = Request('GET / HTTP/1.1\r\n') r.headers['goodbye'] = 'for real' assert not f(r) assert fn(r) # Cookie key r = Request('GET / HTTP/1.1\r\n') r.cookies['hello'] = 'world' assert f(r) assert not fn(r) # Cookie value r = Request('GET / HTTP/1.1\r\n') r.cookies['world'] = 'hello' assert f(r) assert not fn(r) # Nowhere in cookie r = Request('GET / HTTP/1.1\r\n') r.cookies['world'] = 'sucks' assert not f(r) assert fn(r)
def async_mangle_request(self, request): # This function gets called to mangle/edit requests passed through the proxy retreq = request # Write original request to the temp file with tempfile.NamedTemporaryFile(delete=False) as tf: tfName = tf.name tf.write(request.full_request) # Have the console edit the file yield edit_file(tfName) # Create new mangled request from edited file with open(tfName, 'r') as f: text = f.read() os.remove(tfName) # Check if dropped if text == '': pappyproxy.proxy.log('Request dropped!') defer.returnValue(None) mangled_req = Request(text, update_content_length=True) mangled_req._host = request.host mangled_req.port = request.port mangled_req.is_ssl = request.is_ssl # Check if it changed if mangled_req.full_request != request.full_request: retreq = mangled_req defer.returnValue(retreq)
def test_filter_by_params_get(): f1 = context.gen_filter_by_params(['ct', 'Session']) f2 = context.gen_filter_by_params(['ct', 'Cookie', 'ct', 'CookieVal']) r = Request('GET / HTTP/1.1\r\n\r\n') assert not f1(r) assert not f2(r) r = Request('GET /?Session=foo HTTP/1.1\r\n\r\n') assert f1(r) assert not f2(r) r = Request('GET /?Session=foo&CookieThing=Fail HTTP/1.1\r\n\r\n') assert f1(r) assert not f2(r) r = Request('GET /?Session=foo&CookieThing=CookieValue HTTP/1.1\r\n\r\n') assert f1(r) assert f2(r)
def http_request(): req = Request('GET / HTTP/1.1\r\n\r\n') req.host = 'www.foo.faketld' req.port = '1337' req.is_ssl = True req.reqid = 123 rsp = Response('HTTP/1.1 200 OK\r\n\r\n') req.response = rsp return req
def test_mangle_request_single(httprequest): orig_req = httprequest.copy() # in case it gets mangled macro = mock_int_macro(modified_req=('GET /modified HTTP/1.1\r\n\r\n')) expected_req = Request('GET /modified HTTP/1.1\r\n\r\n') (new_req, mangled) = yield macros.mangle_request(orig_req, {'testmacro': macro}) assert new_req == expected_req assert httprequest == orig_req assert httprequest.unmangled is None assert new_req.unmangled == orig_req assert mangled
def test_proxy_server_macro_360_noscope(mocker): proxy = TestProxyConnection() new_req_contents = 'GET / HTTP/1.1\r\nMangled: Very yes\r\n\r\n' new_rsp_contents = 'HTTP/1.1 200 OKILIE DOKILIE\r\nMangled: Very yes\r\n\r\n' new_req = Request(new_req_contents) new_rsp = Response(new_rsp_contents) test_macro = InterceptMacroTest(new_req=new_req, new_rsp=new_rsp) proxy.setUp(mocker, int_macros={'test_macro': test_macro}, in_scope=False) proxy.write_as_browser('GET /serious.php HTTP/1.1\r\n\r\n') assert proxy.read_as_server() == 'GET /serious.php HTTP/1.1\r\n\r\n' proxy.write_as_server('HTTP/1.1 404 NOT FOUND\r\n\r\n') assert proxy.read_as_browser() == 'HTTP/1.1 404 NOT FOUND\r\n\r\n'
def test_gen_filter_by_set_cookies(): f1 = context.gen_filter_by_set_cookies(context.cmp_contains, 'Session') f2 = context.gen_filter_by_set_cookies(context.cmp_contains, 'Cookie', context.cmp_contains, 'CookieVal') r = Request('GET / HTTP/1.1\r\n\r\n') rsp = Response(('HTTP/1.1 200 OK\r\n' 'Set-Cookie: foo=bar\r\n' '\r\n')) r.response = rsp assert not f1(r) assert not f2(r) r = Request('GET / HTTP/1.1\r\n\r\n') rsp = Response(('HTTP/1.1 200 OK\r\n' 'Set-Cookie: foo=bar\r\n' 'Set-Cookie: Session=Banana\r\n' '\r\n')) r.response = rsp assert f1(r) assert not f2(r) r = Request('GET / HTTP/1.1\r\n\r\n') rsp = Response(('HTTP/1.1 200 OK\r\n' 'Set-Cookie: foo=bar\r\n' 'Set-Cookie: Session=Banana\r\n' 'Set-Cookie: CookieThing=NoMatch\r\n' '\r\n')) r.response = rsp assert f1(r) assert not f2(r) r = Request('GET / HTTP/1.1\r\n\r\n') rsp = Response(('HTTP/1.1 200 OK\r\n' 'Set-Cookie: foo=bar\r\n' 'Set-Cookie: Session=Banana\r\n' 'Set-Cookie: CookieThing=CookieValue\r\n' '\r\n')) r.response = rsp assert f1(r) assert f2(r)
def test_mangle_request_multiple(httprequest): orig_req = httprequest.copy() # in case it gets mangled macro = mock_int_macro(modified_req=('GET /cloud HTTP/1.1\r\n\r\n')) macro2 = CloudToButtMacro() intmacros = OrderedDict() intmacros['testmacro'] = macro intmacros['testmacro2'] = macro2 (new_req, mangled) = yield macros.mangle_request(orig_req, intmacros) expected_req = Request('GET /butt HTTP/1.1\r\n\r\n') assert new_req == expected_req assert httprequest == orig_req assert httprequest.unmangled is None assert new_req.unmangled == orig_req assert mangled
def httprequest(): return Request(('POST /test-request HTTP/1.1\r\n' 'Content-Length: 4\r\n' '\r\n' 'AAAA'))
def http_request(): return Request('GET / HTTP/1.1\r\n')
def req(): r = Request() r.start_line = 'GET / HTTP/1.1' return r
def mangle_request(self, request): return Request(string.replace(request.full_message, 'cloud', 'butt'))
def req(): r = Request() r.status_line = 'GET / HTTP/1.1' r.host = 'www.ffffff.eeeeee' r.raw_data = 'AAAA' return r
def req(): r = Request() r.start_line = 'GET / HTTP/1.1' r.host = 'www.ffffff.eeeeee' r.body = 'AAAA' return r