From f26ea82b3c1b6eb994967ed62a152354a56cb8b6 Mon Sep 17 00:00:00 2001 From: Matt Weidner Date: Wed, 19 Jul 2017 22:17:16 -0500 Subject: [PATCH 1/5] Add intercetp toggle feature. --- mitmproxy/addons/intercept.py | 4 +++- mitmproxy/options.py | 5 +++++ mitmproxy/tools/console/consoleaddons.py | 7 +++++++ mitmproxy/tools/console/defaultkeys.py | 1 + mitmproxy/tools/console/statusbar.py | 2 ++ mitmproxy/tools/web/app.py | 1 + 6 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mitmproxy/addons/intercept.py b/mitmproxy/addons/intercept.py index ac8c4c88f..bac07a91c 100644 --- a/mitmproxy/addons/intercept.py +++ b/mitmproxy/addons/intercept.py @@ -11,12 +11,14 @@ class Intercept: if "intercept" in updated: if not ctx.options.intercept: self.filt = None + ctx.options.intercept_active = False return self.filt = flowfilter.parse(ctx.options.intercept) if not self.filt: raise exceptions.OptionsError( "Invalid interception filter: %s" % ctx.options.intercept ) + ctx.options.intercept_active = True def process_flow(self, f): if self.filt: @@ -24,7 +26,7 @@ class Intercept: self.filt(f), not f.request.is_replay, ]) - if should_intercept: + if should_intercept and ctx.options.intercept_active == True: f.intercept() # Handlers diff --git a/mitmproxy/options.py b/mitmproxy/options.py index 954db7e8c..8102efe73 100644 --- a/mitmproxy/options.py +++ b/mitmproxy/options.py @@ -376,6 +376,11 @@ class Options(optmanager.OptManager): """ ) + self.add_option( + "intercept_active", bool, False, + "Intercept toggle" + ) + self.add_option( "intercept", Optional[str], None, "Intercept filter expression." diff --git a/mitmproxy/tools/console/consoleaddons.py b/mitmproxy/tools/console/consoleaddons.py index a65f0afe0..0b0993c8a 100644 --- a/mitmproxy/tools/console/consoleaddons.py +++ b/mitmproxy/tools/console/consoleaddons.py @@ -68,6 +68,13 @@ class ConsoleAddon: """ return ["single", "vertical", "horizontal"] + @command.command("intercept_toggle") + def intercept_toggle(self) -> None: + """ + Toggles interception on/off leaving intercept filters intact. + """ + ctx.options.intercept_active = not ctx.options.intercept_active + @command.command("console.layout.cycle") def layout_cycle(self) -> None: """ diff --git a/mitmproxy/tools/console/defaultkeys.py b/mitmproxy/tools/console/defaultkeys.py index 105be2be2..4634b1e25 100644 --- a/mitmproxy/tools/console/defaultkeys.py +++ b/mitmproxy/tools/console/defaultkeys.py @@ -24,6 +24,7 @@ def map(km): km.add("ctrl f", "console.nav.pagedown", ["global"], "Page down") km.add("ctrl b", "console.nav.pageup", ["global"], "Page up") + km.add("I", "console.command intercept_toggle", ["global"], "Toggle intercept") km.add("i", "console.command set intercept=", ["global"], "Set intercept") km.add("W", "console.command set save_stream_file=", ["global"], "Stream to file") km.add("A", "flow.resume @all", ["flowlist", "flowview"], "Resume all intercepted flows") diff --git a/mitmproxy/tools/console/statusbar.py b/mitmproxy/tools/console/statusbar.py index a37ecbd83..f0d596393 100644 --- a/mitmproxy/tools/console/statusbar.py +++ b/mitmproxy/tools/console/statusbar.py @@ -178,6 +178,8 @@ class StatusBar(urwid.WidgetWrap): r.append(("heading_key", "T")) r.append("CP:%d]" % len(self.master.options.tcp_hosts)) if self.master.options.intercept: + if self.master.options.intercept_active: + r.append("I ") r.append("[") r.append(("heading_key", "i")) r.append(":%s]" % self.master.options.intercept) diff --git a/mitmproxy/tools/web/app.py b/mitmproxy/tools/web/app.py index 9a447fe7f..2a6f6c9e1 100644 --- a/mitmproxy/tools/web/app.py +++ b/mitmproxy/tools/web/app.py @@ -410,6 +410,7 @@ class Settings(RequestHandler): self.write(dict( version=version.VERSION, mode=str(self.master.options.mode), + intercept_active=self.master.options.intercept_active, intercept=self.master.options.intercept, showhost=self.master.options.showhost, upstream_cert=self.master.options.upstream_cert, From 7a28b1f15cc934af780591c30cdd4264dab17a33 Mon Sep 17 00:00:00 2001 From: Matt Weidner Date: Fri, 21 Jul 2017 13:31:04 -0500 Subject: [PATCH 2/5] Fixed indentation --- mitmproxy/options.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mitmproxy/options.py b/mitmproxy/options.py index 8102efe73..5e6602fb7 100644 --- a/mitmproxy/options.py +++ b/mitmproxy/options.py @@ -377,8 +377,8 @@ class Options(optmanager.OptManager): ) self.add_option( - "intercept_active", bool, False, - "Intercept toggle" + "intercept_active", bool, False, + "Intercept toggle" ) self.add_option( From ff9476eaaa083b5ba51c1c1619d4ef6ea57d1907 Mon Sep 17 00:00:00 2001 From: Matt Weidner Date: Fri, 21 Jul 2017 13:32:55 -0500 Subject: [PATCH 3/5] Fixed boolean evaluation syntax inconsistency --- mitmproxy/addons/intercept.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mitmproxy/addons/intercept.py b/mitmproxy/addons/intercept.py index bac07a91c..9e1a283eb 100644 --- a/mitmproxy/addons/intercept.py +++ b/mitmproxy/addons/intercept.py @@ -26,7 +26,7 @@ class Intercept: self.filt(f), not f.request.is_replay, ]) - if should_intercept and ctx.options.intercept_active == True: + if should_intercept and ctx.options.intercept_active: f.intercept() # Handlers From e754fe78cd34a380d013520ee915dc17cc3d6e0f Mon Sep 17 00:00:00 2001 From: Matt Weidner Date: Fri, 21 Jul 2017 13:34:26 -0500 Subject: [PATCH 4/5] Changed statusbar indicator to 'X' inside the intercept filter brackets --- mitmproxy/tools/console/statusbar.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mitmproxy/tools/console/statusbar.py b/mitmproxy/tools/console/statusbar.py index f0d596393..7628c475c 100644 --- a/mitmproxy/tools/console/statusbar.py +++ b/mitmproxy/tools/console/statusbar.py @@ -178,9 +178,9 @@ class StatusBar(urwid.WidgetWrap): r.append(("heading_key", "T")) r.append("CP:%d]" % len(self.master.options.tcp_hosts)) if self.master.options.intercept: - if self.master.options.intercept_active: - r.append("I ") r.append("[") + if not self.master.options.intercept_active: + r.append("X") r.append(("heading_key", "i")) r.append(":%s]" % self.master.options.intercept) if self.master.options.view_filter: From f8c5fb3d15e555f2c2b9b2bd934cb25f9dfb85a4 Mon Sep 17 00:00:00 2001 From: Matt Weidner Date: Fri, 21 Jul 2017 13:34:58 -0500 Subject: [PATCH 5/5] Extended intercept tests to include toggle feature --- test/mitmproxy/addons/test_intercept.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/mitmproxy/addons/test_intercept.py b/test/mitmproxy/addons/test_intercept.py index f436a8179..d4999eb5f 100644 --- a/test/mitmproxy/addons/test_intercept.py +++ b/test/mitmproxy/addons/test_intercept.py @@ -13,10 +13,12 @@ def test_simple(): assert not r.filt tctx.configure(r, intercept="~q") assert r.filt + assert tctx.options.intercept_active with pytest.raises(exceptions.OptionsError): tctx.configure(r, intercept="~~") tctx.configure(r, intercept=None) assert not r.filt + assert not tctx.options.intercept_active tctx.configure(r, intercept="~s") @@ -31,3 +33,13 @@ def test_simple(): f = tflow.tflow(resp=True) r.response(f) assert f.intercepted + + tctx.configure(r, intercept_active=False) + f = tflow.tflow(resp=True) + tctx.cycle(r, f) + assert not f.intercepted + + tctx.configure(r, intercept_active=True) + f = tflow.tflow(resp=True) + tctx.cycle(r, f) + assert f.intercepted