def test_err(self): c = flow.State() f = tutils.tflow() c.add_flow(f) f.error = Error("message") assert c.update_flow(f) c = flow.State() f = tutils.tflow() c.add_flow(f) c.set_limit("~e") assert not c.view f.error = tutils.terr() assert c.update_flow(f) assert c.view
def test_server_playback_full(self): state = flow.State() s = serverplayback.ServerPlayback() o = options.Options(refresh_server_playback=True, keepserving=False) m = mastertest.RecordingMaster(o, None, state) m.addons.add(o, s) f = tutils.tflow() f.response = netlib.tutils.tresp(content=f.request.content) s.load([f, f]) tf = tutils.tflow() assert not tf.response m.request(tf) assert tf.response == f.response tf = tutils.tflow() tf.request.content = b"gibble" assert not tf.response m.request(tf) assert not tf.response assert not s.stop s.tick() assert not s.stop tf = tutils.tflow() m.request(tutils.tflow()) assert s.stop
def run_mitmproxy(options, event): mode = "regular" upstream = None if options.upstream: upstream = options.upstream mode = "upstream" opts = mitmproxy.options.Options( mode=mode, listen_port=int(options.proxyport), cadir=os.path.abspath(options.cadir), upstream_server=upstream ) config = ProxyConfig(opts) state = flow.State() server = ProxyServer(config) proxy = ReflashProxy(opts, server, state) proxy.options = options proxy.options.sid = 0 proxy.options.instrumented = {} if not proxy.options.input: proxy.options.mode = "pass" else: proxy.options.mode = "sandbox" proxy.stopEvent = event proxy.run()
def test_concurrent2(): s = flow.State() fm = flow.FlowMaster(None, s) s = script.Script(tutils.test_data.path("scripts/concurrent_decorator.py"), script.ScriptContext(fm)) s.load() m = mock.Mock() class Dummy: def __init__(self): self.response = self self.error = self self.reply = m t_start = time.time() for hook in ("clientconnect", "serverconnect", "response", "error", "clientconnect"): d = Dummy() s.run(hook, d) d.reply() while (time.time() - t_start) < 20 and m.call_count <= 5: if m.call_count == 5: return time.sleep(0.001) assert False
def __init__(self, server, options): flow.FlowMaster.__init__(self, options, server, flow.State()) self.has_errored = False self.addons.add(options, *builtins.default_addons()) self.addons.add(options, dumper.Dumper()) # This line is just for type hinting self.options = self.options # type: Options self.replay_ignore_params = options.replay_ignore_params self.replay_ignore_content = options.replay_ignore_content self.replay_ignore_host = options.replay_ignore_host self.refresh_server_playback = options.refresh_server_playback self.replay_ignore_payload_params = options.replay_ignore_payload_params self.set_stream_large_bodies(options.stream_large_bodies) if self.server and self.options.http2 and not tcp.HAS_ALPN: # pragma: no cover print( "ALPN support missing (OpenSSL 1.0.2+ required)!\n" "HTTP/2 is disabled. Use --no-http2 to silence this warning.", file=sys.stderr) if options.client_replay: self.start_client_playback(self._readflow(options.client_replay), not options.keepserving) if options.rfile: try: self.load_flows_file(options.rfile) except exceptions.FlowReadException as v: self.add_log("Flow file corrupted.", "error") raise DumpError(v) if self.options.app: self.start_app(self.options.app_host, self.options.app_port)
def test_flow(self): """ normal flow: connect -> request -> response """ c = flow.State() f = tutils.tflow() c.add_flow(f) assert f assert c.flow_count() == 1 assert c.active_flow_count() == 1 newf = tutils.tflow() assert c.add_flow(newf) assert c.active_flow_count() == 2 f.response = HTTPResponse.wrap(netlib.tutils.tresp()) assert c.update_flow(f) assert c.flow_count() == 2 assert c.active_flow_count() == 1 assert not c.update_flow(None) assert c.active_flow_count() == 1 newf.response = HTTPResponse.wrap(netlib.tutils.tresp()) assert c.update_flow(newf) assert c.active_flow_count() == 0
def start_server(proxy_port, proxy_mode): LOGO = r''' ____ _ ____ ____ ___ __ __ __ __ | __ )| |_ _ ___ | _ \ | _ \ / _ \ \ \/ / \ \ / / | _ \| | | | |/ _ \ | |_) | | |_) | | | | | \ / \ V / | |_) | | |_| | __/ | __/ | _ < | |_| | / \ | | |____/|_|\__,_|\___| |_| |_| \_\ \___/ /_/\_\ |_| ''' cprint(LOGO, 'cyan') cprint('[+] Starting Proxy On 0.0.0.0:{0}'.format(proxy_port), 'cyan') cprint('[+] Starting Proxy Mode: {0}'.format(proxy_mode), 'cyan') port = int(proxy_port) if proxy_mode == 'http': mode = 'regular' else: mode = proxy_mode opts = options.Options( listen_port=port, mode=mode, cadir="./ssl/", ssl_insecure=True, ) config = proxy.ProxyConfig(opts) server = ProxyServer(config) state = flow.State() m = BlueProxy(opts, server, state) m.run()
def test_getset_ignore(self): p = mock.Mock() p.config.check_ignore = HostMatcher() fm = flow.FlowMaster(p, flow.State()) assert not fm.get_ignore_filter() fm.set_ignore_filter(["^apple\.com:", ":443$"]) assert fm.get_ignore_filter()
def test_server_playback(self): s = flow.State() f = tutils.tflow() f.response = HTTPResponse.wrap(netlib.tutils.tresp(content=f.request)) pb = [f] fm = flow.FlowMaster(None, s) fm.refresh_server_playback = True assert not fm.do_server_playback(tutils.tflow()) fm.start_server_playback(pb, False, [], False, False, None, False, None, False) assert fm.do_server_playback(tutils.tflow()) fm.start_server_playback(pb, False, [], True, False, None, False, None, False) r = tutils.tflow() r.request.content = "gibble" assert not fm.do_server_playback(r) assert fm.do_server_playback(tutils.tflow()) q = queue.Queue() fm.tick(q, 0) assert fm.should_exit.is_set() fm.stop_server_playback() assert not fm.server_playback
def test_script(self): s = flow.State() fm = flow.FlowMaster(None, s) assert not fm.load_script(tutils.test_data.path("scripts/all.py")) f = tutils.tflow(resp=True) fm.handle_clientconnect(f.client_conn) assert fm.scripts[0].ns["log"][-1] == "clientconnect" fm.handle_serverconnect(f.server_conn) assert fm.scripts[0].ns["log"][-1] == "serverconnect" fm.handle_request(f) assert fm.scripts[0].ns["log"][-1] == "request" fm.handle_response(f) assert fm.scripts[0].ns["log"][-1] == "response" # load second script assert not fm.load_script(tutils.test_data.path("scripts/all.py")) assert len(fm.scripts) == 2 fm.handle_clientdisconnect(f.server_conn) assert fm.scripts[0].ns["log"][-1] == "clientdisconnect" assert fm.scripts[1].ns["log"][-1] == "clientdisconnect" # unload first script fm.unload_scripts() assert len(fm.scripts) == 0 assert not fm.load_script(tutils.test_data.path("scripts/all.py")) f.error = tutils.terr() fm.handle_error(f) assert fm.scripts[0].ns["log"][-1] == "error"
def test_clear(self): c = flow.State() f = self._add_request(c) f.intercepted = True c.clear() assert c.flow_count() == 0
def test_stream(self): with tutils.tmpdir() as tdir: p = os.path.join(tdir, "foo") def r(): r = flow.FlowReader(open(p, "rb")) return list(r.stream()) s = flow.State() fm = flow.FlowMaster(None, s) f = tutils.tflow(resp=True) fm.start_stream(file(p, "ab"), None) fm.handle_request(f) fm.handle_response(f) fm.stop_stream() assert r()[0].response f = tutils.tflow() fm.start_stream(file(p, "ab"), None) fm.handle_request(f) fm.shutdown() assert not r()[1].response
def test_set_limit(self): c = flow.State() f = tutils.tflow() assert len(c.view) == 0 c.add_flow(f) assert len(c.view) == 1 c.set_limit("~s") assert c.limit_txt == "~s" assert len(c.view) == 0 f.response = HTTPResponse.wrap(netlib.tutils.tresp()) c.update_flow(f) assert len(c.view) == 1 c.set_limit(None) assert len(c.view) == 1 f = tutils.tflow() c.add_flow(f) assert len(c.view) == 2 c.set_limit("~q") assert len(c.view) == 1 c.set_limit("~s") assert len(c.view) == 1 assert "Invalid" in c.set_limit("~")
def test_concurrent_err(): s = flow.State() fm = flow.FlowMaster(None, s) tutils.raises("Concurrent decorator not supported for 'start' method", script.Script, tutils.test_data.path("scripts/concurrent_decorator_err.py"), fm)
def __init__(self, server, options): flow.FlowMaster.__init__(self, options, server, flow.State()) self.has_errored = False self.addons.add(*builtins.default_addons()) self.addons.add(dumper.Dumper()) # This line is just for type hinting self.options = self.options # type: Options self.set_stream_large_bodies(options.stream_large_bodies) if not self.options.no_server and server: self.add_log( "Proxy server listening at http://{}".format(server.address), "info") if self.server and self.options.http2 and not tcp.HAS_ALPN: # pragma: no cover self.add_log( "ALPN support missing (OpenSSL 1.0.2+ required)!\n" "HTTP/2 is disabled. Use --no-http2 to silence this warning.", "error") if options.rfile: try: self.load_flows_file(options.rfile) except exceptions.FlowReadException as v: self.add_log("Flow file corrupted.", "error") raise DumpError(v) if self.options.app: self.start_app(self.options.app_host, self.options.app_port)
def test_tick(self): first = tutils.tflow() s = flow.State() fm = flow.FlowMaster(None, s) fm.start_client_playback([first, tutils.tflow()], True) c = fm.client_playback c.testing = True assert not c.done() assert not s.flow_count() assert c.count() == 2 c.tick(fm) assert s.flow_count() assert c.count() == 1 c.tick(fm) assert c.count() == 1 c.clear(c.current) c.tick(fm) assert c.count() == 0 c.clear(c.current) assert c.done() q = queue.Queue() fm.state.clear() fm.tick(q, timeout=0) fm.stop_client_playback() assert not fm.client_playback
def test_script_reqerr(self): s = flow.State() fm = flow.FlowMaster(None, s) assert not fm.load_script(tutils.test_data.path("scripts/reqerr.py")) f = tutils.tflow() fm.handle_clientconnect(f.client_conn) assert fm.handle_request(f)
def test_set_intercept(self): c = flow.State() assert not c.set_intercept("~q") assert c.intercept_txt == "~q" assert "Invalid" in c.set_intercept("~") assert not c.set_intercept(None) assert c.intercept_txt is None
def test_load_flows_reverse(self): r = self._treader() s = flow.State() conf = ProxyConfig(mode="reverse", upstream_server=("https", ("use-this-domain", 80))) fm = flow.FlowMaster(DummyServer(conf), s) fm.load_flows(r) assert s.flows[0].request.host == "use-this-domain"
def __init__(self, config): config.port = 0 s = ProxyServer(config) state = flow.State() flow.FlowMaster.__init__(self, s, state) self.apps.add(testapp, "testapp", 80) self.apps.add(errapp, "errapp", 80) self.clear_log()
def test_kill(self): s = flow.State() fm = flow.FlowMaster(None, s) f = tutils.tflow() f.intercept(mock.Mock()) assert not f.reply.acked f.kill(fm) assert f.reply.acked
def test_kill(self): s = flow.State() fm = flow.FlowMaster(None, s) f = tutils.tflow() f.intercept(mock.Mock()) f.kill(fm) for i in s.view: assert "killed" in str(i.error)
def __init__(self, opts, config): s = ProxyServer(config) state = flow.State() flow.FlowMaster.__init__(self, opts, s, state) self.addons.add(*builtins.default_addons()) self.apps.add(testapp, "testapp", 80) self.apps.add(errapp, "errapp", 80) self.clear_log()
def sniff_main(): opts = options.Options(upstream_server="http://localhost:8080", cadir="~/.mitmproxy/") config = ProxyConfig(opts) state = flow.State() server = ProxyServer(config) m = Sniff(opts, server, state) m.run()
def start(self): print "[*] Pumpkin-Proxy running on port:8080 \n" opts = options.Options(listen_port=8080, mode="transparent") config = proxy.ProxyConfig(opts) state = flow.State() server = ProxyServer(config) server.allow_reuse_address = True self.m = MasterHandler(opts, server, state, self.session) self.m.run(self.send)
def test_duplicate_flow(): s = flow.State() fm = flow.FlowMaster(None, s) fm.load_script(tutils.test_data.path("scripts/duplicate_flow.py")) f = tutils.tflow() fm.handle_request(f) assert fm.state.flow_count() == 2 assert not fm.state.view[0].request.is_replay assert fm.state.view[1].request.is_replay
def test_load_flows_reverse(self): r = self._treader() s = flow.State() opts = options.Options(mode="reverse", upstream_server="https://use-this-domain") conf = ProxyConfig(opts) fm = flow.FlowMaster(opts, DummyServer(conf), s) fm.load_flows(r) assert s.flows[0].request.host == "use-this-domain"
def test_duplicate_flow(self): s = flow.State() fm = flow.FlowMaster(None, s) f = tutils.tflow(resp=True) f = fm.load_flow(f) assert s.flow_count() == 1 f2 = fm.duplicate_flow(f) assert f2.response assert s.flow_count() == 2 assert s.index(f2) == 1
def test_load_script(self): s = flow.State() fm = flow.FlowMaster(None, s) assert not fm.load_script(tutils.test_data.path("scripts/a.py")) assert not fm.load_script(tutils.test_data.path("scripts/a.py")) assert not fm.unload_scripts() assert fm.load_script("nonexistent") assert "ValueError" in fm.load_script( tutils.test_data.path("scripts/starterr.py")) assert len(fm.scripts) == 0
def test_killall(self): s = flow.State() fm = flow.FlowMaster(None, None, s) f = tutils.tflow() f.reply.handle() f.intercept(fm) s.killall(fm) for i in s.view: assert "killed" in str(i.error)