Skip to content

Commit

Permalink
parser refactor + bytes input fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Max-p-otential committed Jul 9, 2023
1 parent 006c4a2 commit 602bf72
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
6 changes: 6 additions & 0 deletions gui/custom_widgets/main_context_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def submit_input(self):
user_line = self.input_widget.text()
logger.debug("Sending input '%s' to inferior", user_line)
user_input = b""
user_echo = b""
# Check if the user wants to input a byte string literal, i.e. the input is in the form: 'b"MyInput \x12\x34"'
if re.match(r'^b["\'].*["\']$', user_line):
# Parse the str as if it were a bytes object
Expand All @@ -214,11 +215,16 @@ def submit_input(self):
logger.debug("Parsed input as bytes string, final input: %s", byte_string)
# Don't pass a newline here, the user needs to specify this himself by writing '\n' at the end of his input
user_input = byte_string
# We want the user to get the evaluated string echoed back
# However without leading b' and ending ' to avoid confusion with regular string insert
user_echo = repr(byte_string)[2:][:-1].encode()
else:
user_echo = user_line.encode() + b"\n"
user_input = user_line.encode() + b"\n"
if self.inferior_attached:
self.gdb_write_input.emit(user_input)
else:
self.update_gui.emit("main", user_echo)
self.inferior_write.emit(user_input)
self.input_widget.clear()

Expand Down
36 changes: 21 additions & 15 deletions gui/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,28 @@ def parse_ascii_control(self, token: bytes):
# Colors
if start == b"30m":
self.parser.setTextColor(Qt.GlobalColor.black)
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
self.insert_token(token.replace(start, b"", 1))
elif start == b"31m":
self.parser.setTextColor(PwndbgGuiConstants.RED)
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
self.insert_token(token.replace(start, b"", 1))
elif start == b"32m":
self.parser.setTextColor(PwndbgGuiConstants.GREEN)
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
self.insert_token(token.replace(start, b"", 1))
elif start == b"33m":
self.parser.setTextColor(PwndbgGuiConstants.YELLOW)
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
self.insert_token(token.replace(start, b"", 1))
elif start == b"34m":
self.parser.setTextColor(PwndbgGuiConstants.LIGHT_BLUE)
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
self.insert_token(token.replace(start, b"", 1))
elif start == b"35m":
self.parser.setTextColor(PwndbgGuiConstants.PURPLE)
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
self.insert_token(token.replace(start, b"", 1))
elif start == b"36m":
self.parser.setTextColor(PwndbgGuiConstants.CYAN)
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
self.insert_token(token.replace(start, b"", 1))
elif start == b"37m":
self.parser.setTextColor(Qt.GlobalColor.white)
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
self.insert_token(token.replace(start, b"", 1))
elif start.startswith(b"38"):
# 256-bit color format: 38;5;<FG COLOR>m
# RGB format: 38;2;<r>;<g>;<b>m
Expand All @@ -116,26 +116,32 @@ def parse_ascii_control(self, token: bytes):
self.parser.setTextColor(QColor.fromRgb(r, g, b, 255))
# Add the "m" into the start for stripping
start = token[:token.index(b"m") + 1]
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
self.insert_token(token.replace(start, b"", 1))
elif start == b"39m":
self.parser.setTextColor(Qt.GlobalColor.white)
self.parser.insertPlainText(token.replace(start, b"").decode())
self.insert_token(token.replace(start, b""))
elif start == b"91m":
# Bright red
self.parser.setTextColor(Qt.GlobalColor.red)
self.parser.insertPlainText(token.replace(start, b"").decode())
self.insert_token(token.replace(start, b""))
# Font
elif start.startswith(b"0m"):
self.reset_font()
self.parser.insertPlainText(token[2:].decode())
self.insert_token(token[2:])
elif start.startswith(b"1m"):
self.parser.setFontWeight(QFont.Weight.Bold)
self.parser.insertPlainText(token[2:].decode())
self.insert_token(token[2:])
elif start.startswith(b"3m"):
self.parser.setFontItalic(not self.parser.fontItalic())
self.parser.insertPlainText(token[2:].decode())
self.insert_token(token[2:])
elif start.startswith(b"4m"):
self.parser.setFontUnderline(not self.parser.fontUnderline())
self.parser.insertPlainText(token[2:].decode())
self.insert_token(token[2:])
else:
self.insert_token(token)

def insert_token(self, token: bytes):
try:
self.parser.insertPlainText(token.decode())
except UnicodeDecodeError:
self.parser.insertPlainText(repr(token))

0 comments on commit 602bf72

Please sign in to comment.