From 36c9ae28b117f701dc0287dbedbe5cbc53c6d796 Mon Sep 17 00:00:00 2001 From: androids-electric-sheep Date: Tue, 20 Feb 2024 20:32:59 +0000 Subject: [PATCH 1/4] adding support to edit binary in a hex editor --- mitmproxy/tools/console/master.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/mitmproxy/tools/console/master.py b/mitmproxy/tools/console/master.py index ee303ff5d2..31f5024eaa 100644 --- a/mitmproxy/tools/console/master.py +++ b/mitmproxy/tools/console/master.py @@ -9,7 +9,7 @@ import sys import tempfile import threading -from typing import TypeVar +from typing import TypeVar, cast import urwid from tornado.platform.asyncio import AddThreadSelectorEventLoop @@ -29,6 +29,7 @@ from mitmproxy.tools.console import palettes from mitmproxy.tools.console import signals from mitmproxy.tools.console import window +from mitmproxy.utils import strutils T = TypeVar("T", str, bytes) @@ -120,12 +121,27 @@ def get_editor(self) -> str: else: return "vi" + def get_binary_editor(self) -> str: + if m := os.environ.get("MITMPROXY_BINARY_EDITOR"): + return m + for editor in "hexedit", "hexer": + if shutil.which(editor): + return editor + if os.name == "nt": + return "notepad" + return "vi" + def spawn_editor(self, data: T) -> T: text = not isinstance(data, bytes) fd, name = tempfile.mkstemp("", "mitmproxy", text=text) with open(fd, "w" if text else "wb") as f: f.write(data) - c = self.get_editor() + + if not text and strutils.is_mostly_bin(cast(bytes, data)): + c = self.get_binary_editor() + else: + c = self.get_editor() + cmd = shlex.split(c) cmd.append(name) with self.uistopped(): From c685d05a901a88eaf4306375e0d80c292bea9831 Mon Sep 17 00:00:00 2001 From: androids-electric-sheep Date: Tue, 20 Feb 2024 20:42:09 +0000 Subject: [PATCH 2/4] adding changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 051202a58b..f049a80a9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ ([#6659](https://github.com/mitmproxy/mitmproxy/pull/6659), @basedBaba) * Fix a regression when leaf cert creation would fail with intermediate CAs in `ca_file`. ([#6666](https://github.com/mitmproxy/mitmproxy/pull/6666), @manselmi) +* Adding support to edit binary files in a hex editor if one is installed ## 21 January 2024: mitmproxy 10.2.2 From e11217abe8cc1a1f0362881bad9d0ba40af52c4e Mon Sep 17 00:00:00 2001 From: androids-electric-sheep Date: Tue, 20 Feb 2024 20:56:15 +0000 Subject: [PATCH 3/4] linting fix --- mitmproxy/tools/console/master.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mitmproxy/tools/console/master.py b/mitmproxy/tools/console/master.py index 31f5024eaa..101ed8f538 100644 --- a/mitmproxy/tools/console/master.py +++ b/mitmproxy/tools/console/master.py @@ -9,7 +9,8 @@ import sys import tempfile import threading -from typing import TypeVar, cast +from typing import cast +from typing import TypeVar import urwid from tornado.platform.asyncio import AddThreadSelectorEventLoop From 776adf2ad757b428eb00801c78665208a0be10bd Mon Sep 17 00:00:00 2001 From: androids-electric-sheep Date: Tue, 20 Feb 2024 21:56:40 +0000 Subject: [PATCH 4/4] making binary editor method static --- mitmproxy/tools/console/master.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mitmproxy/tools/console/master.py b/mitmproxy/tools/console/master.py index 101ed8f538..b1203a8289 100644 --- a/mitmproxy/tools/console/master.py +++ b/mitmproxy/tools/console/master.py @@ -122,7 +122,8 @@ def get_editor(self) -> str: else: return "vi" - def get_binary_editor(self) -> str: + @staticmethod + def get_binary_editor() -> str: if m := os.environ.get("MITMPROXY_BINARY_EDITOR"): return m for editor in "hexedit", "hexer":