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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add console-based hex editors for binary formats #5252

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions mitmproxy/tools/console/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import threading

Copy link
Contributor

@rinsuki rinsuki May 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is need to use platform.system()

Suggested change
import platform

from tornado.platform.asyncio import AddThreadSelectorEventLoop

from mitmproxy.utils import strutils
import urwid

from mitmproxy import addons
Expand Down Expand Up @@ -123,12 +123,28 @@ def get_editor(self) -> str:
else:
return "vi"

def get_hex_editor(self) -> str:
if m := os.environ.get("MITMPROXY_HEXEDITOR"):
return m
mhils marked this conversation as resolved.
Show resolved Hide resolved
for editor in "ghex", "hexedit", "bless", "wxhexeditor", "nano":
if shutil.which(editor):
return editor
if os.name == "nt":
for editor in "HxD", "WinVi", "Cygnus", "Frhed", "notepad":
if shutil.which(editor):
return editor
else:
return "vi"
Comment on lines +129 to +137
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bless command is completely different tool on macOS (in macOS, it is a tool to modify startup disk configuration / set volumes bootability), so it might be good to check Darwin or not.

also I think nano is not suitable for binary editor.

Suggested change
for editor in "ghex", "hexedit", "bless", "wxhexeditor", "nano":
if shutil.which(editor):
return editor
if os.name == "nt":
for editor in "HxD", "WinVi", "Cygnus", "Frhed", "notepad":
if shutil.which(editor):
return editor
else:
return "vi"
editors = ["ghex", "hexedit", "wxhexeditor"]
if platform.system() == "Darwin":
editors = ["hexf"] + editors
else:
editors += ["bless"]
if os.name == "nt":
editors += ["HxD", "WinVi", "Cygnus", "Frhed", "notepad"]
for editor in editors:
if shutil.which(editor):
return editor
return "vi"

(note: hexf command is shortcut to Hex Fiend.app https://github.com/HexFiend/HexFiend )


def spawn_editor(self, data):
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 strutils.is_mostly_bin(data):
c = self.get_hex_editor()
else:
c = self.get_editor()
cmd = shlex.split(c)
cmd.append(name)
with self.uistopped():
Expand Down