def test_replay(self): o = dump.Options(server_replay=["nonexistent"], replay_kill_extra=True) tutils.raises(exceptions.OptionsError, dump.DumpMaster, o, proxy.DummyServer()) with tutils.tmpdir() as t: p = os.path.join(t, "rep") self.flowfile(p) o = dump.Options(server_replay=[p], replay_kill_extra=True) o.verbosity = 0 o.flow_detail = 0 m = dump.DumpMaster(o, proxy.DummyServer()) self.cycle(m, b"content") self.cycle(m, b"content") o = dump.Options(server_replay=[p], replay_kill_extra=False) o.verbosity = 0 o.flow_detail = 0 m = dump.DumpMaster(o, proxy.DummyServer()) self.cycle(m, b"nonexistent") o = dump.Options(client_replay=[p], replay_kill_extra=False) o.verbosity = 0 o.flow_detail = 0 m = dump.DumpMaster(o, proxy.DummyServer())
def setup_proxy(self, args): parser = cmdline.mitmdump() args = parser.parse_args(args) if args.quiet: args.flow_detail = 0 dump_options = options.Options() dump_options.load_paths(args.conf) dump_options.merge(cmdline.get_common_options(args)) dump_options.merge( dict( flow_detail = args.flow_detail, keepserving = args.keepserving, filtstr = " ".join(args.filter) if args.filter else None, ) ) dump_options.listen_host = self.proxy_host dump_options.listen_port = self.proxy_port pconf = config.ProxyConfig(dump_options) proxy_server = server.ProxyServer(pconf) master = dump.DumpMaster(dump_options, proxy_server) self.proxy_master = master
def mitmdump(args=None): # pragma: no cover from mitmproxy.tools import dump version_check.check_pyopenssl_version() parser = cmdline.mitmdump() args = parser.parse_args(args) if args.quiet: args.flow_detail = 0 master = None try: dump_options = dump.Options(**cmdline.get_common_options(args)) dump_options.flow_detail = args.flow_detail dump_options.keepserving = args.keepserving dump_options.filtstr = " ".join(args.args) if args.args else None server = process_options(parser, dump_options, args) master = dump.DumpMaster(dump_options, server) def cleankill(*args, **kwargs): master.shutdown() signal.signal(signal.SIGTERM, cleankill) master.run() except (dump.DumpError, exceptions.OptionsError) as e: print("mitmdump: %s" % e, file=sys.stderr) sys.exit(1) except (KeyboardInterrupt, RuntimeError): pass if master is None or master.has_errored: print("mitmdump: errors occurred during run", file=sys.stderr) sys.exit(1)
def mkmaster(self, flt, **options): if "verbosity" not in options: options["verbosity"] = 0 if "flow_detail" not in options: options["flow_detail"] = 0 o = dump.Options(filtstr=flt, tfile=io.StringIO(), **options) return dump.DumpMaster(o, proxy.DummyServer())
def mkmaster(self, flt, **opts): o = options.Options(view_filter=flt, verbosity='error', flow_detail=0, **opts) m = dump.DumpMaster(o, with_termlog=False, with_dumper=False) return m
def mkmaster(self, flt, **options): o = dump.Options(filtstr=flt, verbosity=-1, flow_detail=0, **options) m = dump.DumpMaster(o, proxy.DummyServer(), with_termlog=False, with_dumper=False) return m
def test_setheader(self): o = dump.Options(setheaders=[(".*", "one", "two")], tfile=io.StringIO()) o.verbosity = 0 o.flow_detail = 0 m = dump.DumpMaster(o, proxy.DummyServer()) f = self.cycle(m, b"content") assert f.request.headers["one"] == "two"
def test_replacements(self): o = dump.Options( replacements=[(".*", "content", "foo")], tfile=io.StringIO(), ) o.verbosity = 0 o.flow_detail = 0 m = dump.DumpMaster(o, proxy.DummyServer()) f = self.cycle(m, b"content") assert f.request.content == b"foo"
def start_mitm(): global mitm_master l = logging.getLogger("mono_mitm") if sys.version_info < (3, 5): l.critical("MITM NEEDS PYTHON > 3.5 TO START") print("MITM NEEDS PYTHON > 3.5 TO START") return l.info("start mitm proxy") #iptables thingy add_rule() #delete rule if it already exist os.system( "iptables -D PREROUTING -t nat -s 10.8.0.0/24 -p tcp -j REDIRECT --to-ports 8182" ) #add rule os.system( "iptables -A PREROUTING -t nat -s 10.8.0.0/24 -p tcp -j REDIRECT --to-ports 8182" ) from mitmproxy.tools import dump version_check.check_pyopenssl_version() #args = parser.parse_args(args) This might cause a problem #dump_options.load_paths(args.conf) #dump_options.merge(cmdline.get_common_options(args)) dump_options = options.Options() dump_options.merge( dict( flow_detail=1, web_port=8081, cadir='~/.mitmproxy', listen_port=8182, mode='transparent', no_server=False, rawtcp=False, scripts=['mitm.py'], ssl_insecure=True, ssl_version_client='all', ssl_version_server='all', tcp_hosts=['.*'], )) server = process_options(dump_options) mitm_master = dump.DumpMaster(dump_options, server) l.info("start_mitm --> run master") mitm_master.run()
def test_addons_dumper(self, dumper): with mock.patch('sys.stdout'): o = options.Options() m = dump.DumpMaster(o, with_dumper=dumper) assert (m.addons.get('dumper') is not None) == dumper
def test_addons_termlog(self, termlog): with mock.patch('sys.stdout'): o = options.Options() m = dump.DumpMaster(o, with_termlog=termlog) assert (m.addons.get('termlog') is not None) == termlog
def mkmaster(self, **opts): o = options.Options(**opts) m = dump.DumpMaster(o, with_termlog=False, with_dumper=False) return m
def test_mitmdump(): opts = options.Options() dump.DumpMaster(opts) ap = cmdline.mitmdump(opts) assert ap
def mkmaster(self, flt, **options): o = dump.Options(filtstr=flt, verbosity=-1, flow_detail=0, **options) return dump.DumpMaster(o, proxy.DummyServer())
def test_error(self): o = dump.Options(tfile=io.StringIO(), flow_detail=1) m = dump.DumpMaster(o, proxy.DummyServer()) f = tutils.tflow(err=True) m.error(f) assert "error" in o.tfile.getvalue()
from unittest import mock from mitmproxy import proxy, options from mitmproxy.tools import dump, console, web def print_typehints(opts): for name, option in sorted(opts.items()): print( # For Python 3.6, we can just use "{}: {}". "{} = None # type: {}".format( name, { int: "int", str: "str", bool: "bool", typing.Optional[str]: "Optional[str]", typing.Sequence[str]: "Sequence[str]" }[option.typespec])) if __name__ == "__main__": opts = options.Options() server = proxy.server.DummyServer(None) # initialize with all three tools here to capture tool-specific options defined in addons. dump.DumpMaster(opts, server) with mock.patch("sys.stdout.isatty", lambda: True): console.master.ConsoleMaster(opts, server) web.master.WebMaster(opts, server) print_typehints(opts)