def setup_class(cls): cls.chain = [] super(ChainProxyTest, cls).setup_class() for _ in range(cls.n): config = ProxyConfig(**cls.get_proxy_config()) tmaster = cls.masterclass(config) proxy = ProxyThread(tmaster) proxy.start() cls.chain.insert(0, proxy) # Patch the orginal proxy to upstream mode cls.config = cls.proxy.tmaster.config = cls.proxy.tmaster.server.config = ProxyConfig( **cls.get_proxy_config())
def setup_class(cls): cls.chain = [] super().setup_class() for _ in range(cls.n): opts = cls.get_options() config = ProxyConfig(opts) tmaster = cls.masterclass(opts, config) proxy = ProxyThread(tmaster) proxy.start() cls.chain.insert(0, proxy) # Patch the orginal proxy to upstream mode opts = cls.get_options() cls.config = cls.proxy.tmaster.config = cls.proxy.tmaster.server.config = ProxyConfig(opts)
def setup_class(self): self.config = ProxyConfig(**self.get_proxy_config()) tmaster = tservers.TestMaster(self.config) tmaster.start_app(APP_HOST, APP_PORT) self.proxy = tservers.ProxyThread(tmaster) self.proxy.start()
def capture(): from mitmproxy.options import Options from mitmproxy.proxy.config import ProxyConfig from mitmproxy.proxy.server import ProxyServer from mitmproxy.tools.dump import DumpMaster import asyncio import signal try: options = Options(listen_host="0.0.0.0", listen_port=8889, http2=True) config = ProxyConfig(options) global m m = DumpMaster(options, with_termlog=False, with_dumper=False) m.server = ProxyServer(config) m.addons.add(Addon()) loop = asyncio.get_event_loop() try: loop.add_signal_handler(signal.SIGINT, getattr(m, "prompt_for_exit", m.shutdown)) loop.add_signal_handler(signal.SIGTERM, m.shutdown) except NotImplementedError: pass async def wakeup(): while True: await asyncio.sleep(0.2) asyncio.ensure_future(wakeup()) m.run() except KeyboardInterrupt: pressAnyKeyExitWithDisableProxy() except TypeError: pass except Exception as e: print("抓包模块出错:", e)
def setup_class(cls): opts = cls.get_options() cls.config = ProxyConfig(opts) tmaster = tservers.TestMaster(opts, cls.config) cls.proxy = tservers.ProxyThread(tmaster) cls.proxy.start()
def start_proxy(): opts = options.Options(listen_host=PROXY_LISTEN_HOST, listen_port=PROXY_LISTEN_PORT, confdir=os.path.join(BASE_DIR, "ssl"), ssl_insecure=True) opts.add_option("body_size_limit", int, 0, "") pconf = ProxyConfig(opts) #There is no current event loop in thread 'Thread-1'错误 #解决方法:到mitmproxy的master.py修改Master类的init函数, # loop = asyncio.new_event_loop() # asyncio.set_event_loop(loop) # self.should_exit = threading.Event() # self.channel = controller.Channel( # self, # loop, # self.should_exit, # ) try: m = DumpMaster(opts, with_termlog=False, with_dumper=False) m.server = ProxyServer(pconf) m.addons.add(Capture()) print("监听地址:{},监听端口:{}".format(PROXY_LISTEN_HOST, PROXY_LISTEN_PORT)) m.run() except: pass
def _run_proxy(event, namespace, service, listen_host, listen_port, verbose): # Run mitmproxy as a reverse-proxy to the Kubernetes ApiServer under the # subpath /api/v1/namespaces/{namespace}/services/{service}/proxy. See # https://k8s.io/docs/tasks/administer-cluster/access-cluster-services. client = kubernetes.config.new_client_from_config() prefix = f"/api/v1/namespaces/{namespace}/services/{service}/proxy" options = Options(listen_host=listen_host, listen_port=listen_port, mode=f"reverse:{client.configuration.host}") master = DumpMaster(options, with_termlog=verbose, with_dumper=verbose) options.keep_host_header = True options.ssl_insecure = True with NamedTemporaryFile() as cert_file: # If Kubernetes client has a client-certificate set up, then configure # mitmproxy to use it. Kubernetes stores the cert and key in separate # files, while mitmproxy expects a single file. So append the two file # contents into a new file and pass it to mitmproxy. if client.configuration.cert_file and client.configuration.key_file: with open(client.configuration.cert_file, "rb") as f: cert_file.write(f.read()) with open(client.configuration.key_file, "rb") as f: cert_file.write(f.read()) cert_file.flush() options.client_certs = cert_file.name master.server = ProxyServer(ProxyConfig(options)) master.addons.add(_Addon(client, prefix)) event.set() # Port is bound by this time, unblock parent process. master.run()
def test_cannot_set_both_allow_and_filter_options(self): opts = options.Options() opts.ignore_hosts = ["foo"] opts.allow_hosts = ["bar"] with pytest.raises(exceptions.OptionsError, match="--ignore-hosts and --allow-hosts are " "mutually exclusive; please choose " "one."): ProxyConfig(opts)
def setup_class(cls): cls.server = pathod.test.Daemon(ssl=cls.ssl, ssloptions=cls.ssloptions) cls.server2 = pathod.test.Daemon(ssl=cls.ssl, ssloptions=cls.ssloptions) opts = cls.get_options() cls.config = ProxyConfig(opts) tmaster = cls.masterclass(opts, cls.config) cls.proxy = ProxyThread(tmaster) cls.proxy.start()
def __init__(self, options): self.logger = logger self.logger.info("Initializing MalwareProxyX") self.pre_run_checks() self.proxy_options = self.generate_proxy_options(options) self.master = ProxyMaster(self.proxy_options, with_termlog=False, with_dumper=False) self.master.server = ProxyServer(ProxyConfig(self.proxy_options)) self.master.addons.add(TrafficHandler(options)) self.master.run()
def setup_class(cls): cls.server = pathod.test.Daemon(ssl=cls.ssl, ssloptions=cls.ssloptions) cls.server2 = pathod.test.Daemon(ssl=cls.ssl, ssloptions=cls.ssloptions) cls.config = ProxyConfig(**cls.get_proxy_config()) tmaster = cls.masterclass(cls.config) tmaster.start_app(APP_HOST, APP_PORT) cls.proxy = ProxyThread(tmaster) cls.proxy.start()
def __init__(self, host, port, options): self.options = options # Used to stored captured requests self.storage = RequestStorage( base_dir=options.pop('request_storage_base_dir', None)) # Used to modify requests/responses passing through the server # Deprecated. Will be superceded by request/response interceptors. self.modifier = RequestModifier() # The scope of requests we're interested in capturing. self.scopes = [] self.request_interceptor = None self.response_interceptor = None try: self._event_loop = asyncio.get_event_loop() if self._event_loop.is_closed(): # The event loop may be closed if the server had previously # been shutdown and then spun up again self._event_loop = asyncio.new_event_loop() asyncio.set_event_loop(self._event_loop) except: self._event_loop = asyncio.new_event_loop() asyncio.set_event_loop(self._event_loop) # mitmproxy specific options mitmproxy_opts = Options( listen_host=host, listen_port=port, ) # Create an instance of the mitmproxy server self._master = Master(mitmproxy_opts) self._master.server = ProxyServer(ProxyConfig(mitmproxy_opts)) self._master.addons.add(*addons.default_addons()) self._master.addons.add(SendToLogger()) self._master.addons.add(MitmProxyRequestHandler(self)) # Update the options now all addons have been added mitmproxy_opts.update( confdir=DEFAULT_CONFDIR, ssl_insecure=options.get('verify_ssl', True), upstream_cert=DEFAULT_UPSTREAM_CERT, stream_websockets=DEFAULT_STREAM_WEBSOCKETS, **self._get_upstream_proxy_args(), ) # Options that are prefixed mitm_ are passed through to mitmproxy mitmproxy_opts.update( **{k[5:]: v for k, v in options.items() if k.startswith('mitm_')})
def shutdown_proxy(): opts = options.Options(listen_host=PROXY_LISTEN_HOST, listen_port=PROXY_LISTEN_PORT, confdir=BASE_DIR + "/ssl/", ssl_insecure=True) opts.add_option("body_size_limit", int, 0, "") pconf = ProxyConfig(opts) m = DumpMaster(opts, with_termlog=False, with_dumper=False) m.server = ProxyServer(pconf) m.addons.add(Capture()) m.shutdown()
def main(): option = options.Options(listen_host='0.0.0.0', listen_port=18088, http2=True, mode="regular") #代理地址 m = DumpMaster(option, with_termlog=False, with_dumper=False) config = ProxyConfig(option) m.server = ProxyServer(config) m.addons.add(Addon()) loop = asyncio.get_event_loop() t = threading.Thread(target=loop_in_thread, args=(loop,m) ) try: t.start() except: m.shutdown() sys.exit(0)
def start_mitm(self): asyncio.set_event_loop(asyncio.new_event_loop()) print('[*] Starting mitmproxy on 127.0.0.1:8080') options = Options(listen_host='127.0.0.1', listen_port=8080, http2=True) m = DumpMaster(options, with_termlog=False, with_dumper=False) config = ProxyConfig(options) m.server = ProxyServer(config) addon = MitmAddon(self.ext_id) m.addons.add(addon) # run mitmproxy in backgroud loop = asyncio.get_event_loop() t = threading.Thread( target=loop_in_thread, args=(loop,m) ) t.start() return m
def __init__(self, mode: ProxyMode, port: int): mode = (f'{mode.value}:https://dummy-upstream' if mode == ProxyMode.REVERSE else mode.value) opts = Options(mode=mode, listen_port=port) super().__init__(opts) self.view = View() self.addons.add( *default_addons(), VaultFlows(), self.view, ProxyEventsAddon(), ) self.server = ProxyServer(ProxyConfig(opts))
def start_proxy(self, port, mode, flow_file_name): """ Start Proxy Capture / Intercept """ self.mitm_proxy_opts.keepserving = True self.mitm_proxy_opts.listen_port = port self.mitm_proxy_opts.cadir = settings.CA_DIR if settings.UPSTREAM_PROXY: self.mitm_proxy_opts.mode = settings.UPSTREAM_PROXY_CONF self.mitm_proxy_opts.ssl_insecure = settings.UPSTREAM_PROXY_SSL_INSECURE self.app_server = ProxyHandler(self.mitm_proxy_opts, mode, flow_file_name) self.app_server.server = ProxyServer(ProxyConfig(self.mitm_proxy_opts)) self.app_server.run()
def _run_proxy(event, namespace, service, listen_host, listen_port, verbose): # Run mitmproxy as a reverse-proxy to the Kubernetes ApiServer under the # subpath /api/v1/namespaces/{namespace}/services/{service}/proxy. See # https://k8s.io/docs/tasks/administer-cluster/access-cluster-services. client = kubernetes.config.new_client_from_config() prefix = f"/api/v1/namespaces/{namespace}/services/{service}/proxy" options = Options(listen_host=listen_host, listen_port=listen_port, mode=f"reverse:{client.configuration.host}") master = DumpMaster(options, with_termlog=verbose, with_dumper=verbose) options.keep_host_header = True options.ssl_insecure = True master.server = ProxyServer(ProxyConfig(options)) master.addons.add(_Addon(client, prefix)) event.set() # Port is bound by this time, unblock parent process. master.run()
def handle_client_connection(self, conn, client_address): config = self.config if get_proxy_context().mode == ProxyMode.REVERSE: upstream = self._get_upstream() if upstream: options = copy(self.config.options) options.mode = f'reverse:{upstream}' config = ProxyConfig(options) handler = ConnectionHandler( conn, client_address, config, self.channel, ) handler.handle()
GREEN = "\033[35m" # print(flow.response.headers) RESET = "\033[0m" # mode 0 = reset # see source mitmproxy/master.py for details def loop_in_thread(loop, m): asyncio.set_event_loop(loop) # This is the key. m.run_loop(loop.run_forever) if __name__ == "__main__": # try: options = Options(listen_host="0.0.0.0", listen_port=8080, http2=True) m = DumpMaster(options, with_termlog=False, with_dumper=False) config = ProxyConfig(options) m.server = ProxyServer(config) colorama.init() m.addons.add(Addon()) loop = asyncio.get_event_loop() t = threading.Thread(target=loop_in_thread, args=(loop, m)) t.start() print("hello") # Other servers might be started too. time.sleep(3000) print("going to shutdown mitmproxy")
def test_upstream_cert_insecure(self): opts = options.Options() opts.add_upstream_certs_to_client_chain = True with pytest.raises(exceptions.OptionsError, match="verify-upstream-cert"): ProxyConfig(opts)
def run_web(options): webserver = WebMaster(options) webserver.server = ProxyServer(ProxyConfig(options)) return webserver # type: master.Master
def test_invalid_cadir(self): opts = options.Options() opts.cadir = "foo" with pytest.raises(exceptions.OptionsError, match="parent directory does not exist"): ProxyConfig(opts)
if str(select) == '1': print('此程序仅设置Internet局域网代理 可能会被拦截,请允许.') print('不要启动加速器或者其它代理程序.') qq = config.QQ if not qq: qq = input_num('输入一个QQ号进行绑定:') print('正在设置代理..') enable_proxy() options = Options(listen_host="0.0.0.0", listen_port=config.proxy_port, http2=True) m = DumpMaster(options, with_termlog=False, with_dumper=False) m.server = ProxyServer(ProxyConfig(options)) m.addons.add(Addon()) try: loop = asyncio.get_event_loop() try: loop.add_signal_handler( signal.SIGINT, getattr(m, "prompt_for_exit", m.shutdown)) loop.add_signal_handler(signal.SIGTERM, m.shutdown) except NotImplementedError: pass async def wakeup(): while True: await asyncio.sleep(0.2)
def run_dump(options): server = DumpMaster(options, with_termlog=False, with_dumper=False) server.server = ProxyServer(ProxyConfig(options)) return server # type: master.Master
def config_server(self): config = ProxyConfig(self.options) self.mitm.server = ProxyServer(config) self.mitm.addons.add(self.addons)
def mkmaster(self, **opts): o = options.Options(**opts) c = ProxyConfig(o) return master.WebMaster(o, proxy.DummyServer(c))
def __init__(self, opts): super().__init__(opts) config = ProxyConfig(opts) self.server = ProxyServer(config)
def test_invalid_certificate(self): opts = options.Options() opts.certs = [tutils.test_data.path("mitmproxy/data/dumpfile-011")] with pytest.raises(exceptions.OptionsError, match="Invalid certificate format"): ProxyConfig(opts)
def run_console(options): server = ConsoleMaster(options) server.server = ProxyServer(ProxyConfig(options)) return server # type: master.Master