Ejemplo n.º 1
0
    def run(self):
        def process_options(_, _options, _args):
            from mitmproxy.proxy import server  # noqa

            if _args.version:
                print(debug.dump_system_info())
                sys.exit(0)

            # debug.register_info_dumpers()
            pconf = config.ProxyConfig(_options)
            if _options.no_server:
                return server.DummyServer(pconf)
            else:
                try:
                    return server.ProxyServer(pconf)
                except exceptions.ServerException as v:
                    print(str(v), file=sys.stderr)
                    sys.exit(1)

        from mitmproxy.tools import dump

        args = ['-b', self.address[0], '-p', str(self.address[1]), '-q']

        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 = 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,
                ))

            server = process_options(parser, dump_options, args)
            self._master = master = self._master_type(dump_options, server)

            # def clean_kill(*args, **kwargs):
            #    master.shutdown()

            # signal.signal(signal.SIGTERM, clean_kill)
            self.info('bind at tcp:{}'.format(self.server_address))
            master.run()
            self.info('stopped')
        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)
Ejemplo n.º 2
0
def mitmweb(args=None):  # pragma: no cover
    from mitmproxy.tools import web

    version_check.check_pyopenssl_version()

    parser = cmdline.mitmweb()

    args = parser.parse_args(args)

    try:
        web_options = options.Options()
        web_options.load_paths(args.conf)
        web_options.merge(cmdline.get_common_options(args))
        web_options.merge(
            dict(
                intercept = args.intercept,
                web_open_browser = args.web_open_browser,
                web_debug = args.web_debug,
                web_iface = args.web_iface,
                web_port = args.web_port,
            )
        )
        server = process_options(parser, web_options, args)
        m = web.master.WebMaster(web_options, server)
    except exceptions.OptionsError as e:
        print("mitmweb: %s" % e, file=sys.stderr)
        sys.exit(1)
    try:
        m.run()
    except (KeyboardInterrupt, RuntimeError):
        pass
Ejemplo n.º 3
0
def run(MasterKlass, args):  # pragma: no cover
    version_check.check_pyopenssl_version()
    debug.register_info_dumpers()

    opts = options.Options()
    parser = cmdline.mitmdump(opts)
    args = parser.parse_args(args)
    master = None
    try:
        opts.load_paths(args.conf)
        server = process_options(parser, opts, args)
        master = MasterKlass(opts, server)

        def cleankill(*args, **kwargs):
            master.shutdown()

        signal.signal(signal.SIGTERM, cleankill)
        master.run()
    except exceptions.OptionsError as e:
        print("%s: %s" % (sys.argv[0], e), file=sys.stderr)
        sys.exit(1)
    except (KeyboardInterrupt, RuntimeError):
        pass
    if master is None or getattr(master, "has_errored", None):
        print("%s: errors occurred during run" % sys.argv[0], file=sys.stderr)
        sys.exit(1)
Ejemplo n.º 4
0
def mitmweb(args=None):  # pragma: no cover
    from mitmproxy.tools import web

    version_check.check_pyopenssl_version()

    parser = cmdline.mitmweb()

    args = parser.parse_args(args)

    try:
        web_options = web.master.Options(**cmdline.get_common_options(args))
        web_options.intercept = args.intercept
        web_options.open_browser = args.open_browser
        web_options.wdebug = args.wdebug
        web_options.wiface = args.wiface
        web_options.wport = args.wport

        server = process_options(parser, web_options, args)
        m = web.master.WebMaster(web_options, server)
    except exceptions.OptionsError as e:
        print("mitmweb: %s" % e, file=sys.stderr)
        sys.exit(1)
    try:
        m.run()
    except (KeyboardInterrupt, RuntimeError):
        pass
Ejemplo n.º 5
0
def mitmproxy(args=None):  # pragma: no cover
    if os.name == "nt":
        print("Error: mitmproxy's console interface is not supported on Windows. "
              "You can run mitmdump or mitmweb instead.", file=sys.stderr)
        sys.exit(1)
    from mitmproxy.tools import console

    version_check.check_pyopenssl_version()
    assert_utf8_env()

    parser = cmdline.mitmproxy()
    args = parser.parse_args(args)

    try:
        console_options = console.master.Options(
            **cmdline.get_common_options(args)
        )
        console_options.palette = args.palette
        console_options.palette_transparent = args.palette_transparent
        console_options.eventlog = args.eventlog
        console_options.focus_follow = args.focus_follow
        console_options.intercept = args.intercept
        console_options.filter = args.filter
        console_options.no_mouse = args.no_mouse
        console_options.order = args.order

        server = process_options(parser, console_options, args)
        m = console.master.ConsoleMaster(console_options, server)
    except exceptions.OptionsError as e:
        print("mitmproxy: %s" % e, file=sys.stderr)
        sys.exit(1)
    try:
        m.run()
    except (KeyboardInterrupt, RuntimeError):
        pass
Ejemplo n.º 6
0
def start() -> None:
    version_check.check_pyopenssl_version()

    AsyncIOMainLoop().install()
    loop = asyncio.get_event_loop()

    intercepts = Intercepts()
    proxy = Proxy(loop, UpdatesHandler.broadcast)

    web_options, server = configuration()
    master = Master(web_options, loop, proxy, server)

    web_application = Application(master, intercepts)
    http_server = tornado.httpserver.HTTPServer(web_application)
    http_server.listen(5000, "127.0.0.1")

    tornado_loop = tornado.ioloop.IOLoop.instance()
    tornado_loop.add_callback(master.start)
    tornado.ioloop.PeriodicCallback(lambda: master.tick(timeout=0), 5).start()

    try:
        print("Starting proxy server")
        tornado_loop.start()
    except (KeyboardInterrupt, RuntimeError):
        pass
Ejemplo n.º 7
0
def mitmweb(args=None):  # pragma: no cover
    from mitmproxy.tools import web

    version_check.check_pyopenssl_version()

    parser = cmdline.mitmweb()

    args = parser.parse_args(args)

    try:
        web_options = web.master.Options(**cmdline.get_common_options(args))
        web_options.intercept = args.intercept
        web_options.wdebug = args.wdebug
        web_options.wiface = args.wiface
        web_options.wport = args.wport
        web_options.wsingleuser = args.wsingleuser
        web_options.whtpasswd = args.whtpasswd
        web_options.process_web_options(parser)

        server = process_options(parser, web_options, args)
        m = web.master.WebMaster(web_options, server)
    except exceptions.OptionsError as e:
        print("mitmweb: %s" % e, file=sys.stderr)
        sys.exit(1)
    try:
        m.run()
    except (KeyboardInterrupt, RuntimeError):
        pass
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
def mitmproxy(args=None):  # pragma: no cover
    if os.name == "nt":
        print("Error: mitmproxy's console interface is not supported on Windows. "
              "You can run mitmdump or mitmweb instead.", file=sys.stderr)
        sys.exit(1)
    from mitmproxy.tools import console

    version_check.check_pyopenssl_version()
    assert_utf8_env()

    parser = cmdline.mitmproxy()
    args = parser.parse_args(args)

    try:
        console_options = console.master.Options(
            **cmdline.get_common_options(args)
        )
        console_options.palette = args.palette
        console_options.palette_transparent = args.palette_transparent
        console_options.eventlog = args.eventlog
        console_options.focus_follow = args.focus_follow
        console_options.intercept = args.intercept
        console_options.filter = args.filter
        console_options.no_mouse = args.no_mouse
        console_options.order = args.order

        server = process_options(parser, console_options, args)
        m = console.master.ConsoleMaster(console_options, server)
    except exceptions.OptionsError as e:
        print("mitmproxy: %s" % e, file=sys.stderr)
        sys.exit(1)
    try:
        m.run()
    except (KeyboardInterrupt, RuntimeError):
        pass
Ejemplo n.º 10
0
def run(MasterKlass, args):  # pragma: no cover
    version_check.check_pyopenssl_version()
    debug.register_info_dumpers()

    opts = options.Options()
    parser = cmdline.mitmdump(opts)
    args = parser.parse_args(args)
    master = None
    try:
        unknown = optmanager.load_paths(opts, args.conf)
        server = process_options(parser, opts, args)
        master = MasterKlass(opts, server)
        master.addons.configure_all(opts, opts.keys())
        remaining = opts.update_known(**unknown)
        if remaining and opts.verbosity > 1:
            print("Ignored options: %s" % remaining)
        if args.options:
            print(optmanager.dump_defaults(opts))
            sys.exit(0)
        opts.set(*args.setoptions)

        def cleankill(*args, **kwargs):
            master.shutdown()

        signal.signal(signal.SIGTERM, cleankill)
        master.run()
    except exceptions.OptionsError as e:
        print("%s: %s" % (sys.argv[0], e), file=sys.stderr)
        sys.exit(1)
    except (KeyboardInterrupt, RuntimeError):
        pass
    return master
Ejemplo n.º 11
0
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.filter) if args.filter 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)
Ejemplo n.º 12
0
def test_check_pyopenssl_version(sexit):
    fp = io.StringIO()
    version_check.check_pyopenssl_version(fp=fp)
    assert not fp.getvalue()
    assert not sexit.called

    version_check.check_pyopenssl_version((9999, ), fp=fp)
    assert "outdated" in fp.getvalue()
    assert sexit.called
Ejemplo n.º 13
0
def test_check_pyopenssl_version(sexit):
    fp = io.StringIO()
    version_check.check_pyopenssl_version(fp=fp)
    assert not fp.getvalue()
    assert not sexit.called

    version_check.check_pyopenssl_version((9999,), fp=fp)
    assert "outdated" in fp.getvalue()
    assert sexit.called
Ejemplo n.º 14
0
def run(MasterKlass, args, extra=None):  # pragma: no cover
    """
        extra: Extra argument processing callable which returns a dict of
        options.
    """
    version_check.check_pyopenssl_version()
    debug.register_info_dumpers()

    opts = options.Options()
    parser = cmdline.mitmdump(opts)
    args = parser.parse_args(args)
    master = None
    try:
        unknown = optmanager.load_paths(opts, args.conf)
        pconf = process_options(parser, opts, args)
        if pconf.options.server:
            try:
                server = proxy.server.ProxyServer(pconf)
            except exceptions.ServerException as v:
                print(str(v), file=sys.stderr)
                sys.exit(1)
        else:
            server = proxy.server.DummyServer(pconf)

        master = MasterKlass(opts, server)
        master.addons.trigger("configure", opts.keys())
        master.addons.trigger("tick")
        remaining = opts.update_known(**unknown)
        if remaining and opts.verbosity > 1:
            print("Ignored options: %s" % remaining)
        if args.options:
            print(optmanager.dump_defaults(opts))
            sys.exit(0)
        if args.commands:
            master.commands.dump()
            sys.exit(0)
        opts.set(*args.setoptions)
        if extra:
            opts.update(**extra(args))

        def cleankill(*args, **kwargs):
            master.shutdown()

        signal.signal(signal.SIGTERM, cleankill)
        master.run()
    except exceptions.OptionsError as e:
        print("%s: %s" % (sys.argv[0], e), file=sys.stderr)
        sys.exit(1)
    except (KeyboardInterrupt, RuntimeError) as e:
        pass
    return master
Ejemplo n.º 15
0
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()
Ejemplo n.º 16
0
def mitmmonitor(args=None):  # pragma: no cover
    import monitor

    version_check.check_pyopenssl_version()

    parser = cmdline.monitor()

    args = parser.parse_args(args)

    try:
        monitor_options = options.Options()
        monitor_options.load_paths(args.conf)
        monitor_options.merge(cmdline.get_common_options(args))
        server = process_options(parser, monitor_options, args)
        m = monitor.master.MonitorMaster(monitor_options, server)
    except exceptions.OptionsError as e:
        sys.exit(1)
    try:
        m.run()
    except (KeyboardInterrupt, RuntimeError):
        pass
Ejemplo n.º 17
0
from mitmproxy.utils import strutils

import certifi
import OpenSSL
from OpenSSL import SSL

from mitmproxy import certs
from mitmproxy.utils import version_check
from mitmproxy.types import serializable
from mitmproxy import exceptions
from mitmproxy.types import basethread

# This is a rather hackish way to make sure that
# the latest version of pyOpenSSL is actually installed.
version_check.check_pyopenssl_version()

socket_fileobject = socket.SocketIO

EINTR = 4
HAS_ALPN = SSL._lib.Cryptography_HAS_ALPN

# To enable all SSL methods use: SSLv23
# then add options to disable certain methods
# https://bugs.launchpad.net/pyopenssl/+bug/1020632/comments/3
SSL_BASIC_OPTIONS = (
    SSL.OP_CIPHER_SERVER_PREFERENCE
)
if hasattr(SSL, "OP_NO_COMPRESSION"):
    SSL_BASIC_OPTIONS |= SSL.OP_NO_COMPRESSION
Ejemplo n.º 18
0
def test_unparseable_pyopenssl_version(version, sexit):
    version.split.return_value = ["foo", "bar"]
    fp = io.StringIO()
    version_check.check_pyopenssl_version(fp=fp)
    assert "Cannot parse" in fp.getvalue()
    assert not sexit.called
Ejemplo n.º 19
0
def test_unparseable_pyopenssl_version(version, sexit):
    version.split.return_value = ["foo", "bar"]
    fp = io.StringIO()
    version_check.check_pyopenssl_version(fp=fp)
    assert "Cannot parse" in fp.getvalue()
    assert not sexit.called
Ejemplo n.º 20
0
from typing import Optional  # noqa

from mitmproxy.utils import strutils

import certifi
import OpenSSL
from OpenSSL import SSL

from mitmproxy import certs
from mitmproxy.utils import version_check
from mitmproxy import exceptions
from mitmproxy.types import basethread

# This is a rather hackish way to make sure that
# the latest version of pyOpenSSL is actually installed.
version_check.check_pyopenssl_version()

socket_fileobject = socket.SocketIO

# workaround for https://bugs.python.org/issue29515
# Python 3.5 and 3.6 for Windows is missing a constant
IPPROTO_IPV6 = getattr(socket, "IPPROTO_IPV6", 41)

EINTR = 4
HAS_ALPN = SSL._lib.Cryptography_HAS_ALPN

# To enable all SSL methods use: SSLv23
# then add options to disable certain methods
# https://bugs.launchpad.net/pyopenssl/+bug/1020632/comments/3
SSL_BASIC_OPTIONS = (SSL.OP_CIPHER_SERVER_PREFERENCE)
if hasattr(SSL, "OP_NO_COMPRESSION"):