Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support to edit binary files in a hex editor if one is installed #6675

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion mitmproxy/tools/console/master.py
Expand Up @@ -9,6 +9,7 @@
import sys
import tempfile
import threading
from typing import cast
from typing import TypeVar

import urwid
Expand All @@ -29,6 +30,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)

Expand Down Expand Up @@ -120,12 +122,28 @@ def get_editor(self) -> str:
else:
return "vi"

@staticmethod
def get_binary_editor() -> 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():
Expand Down