From 9170ac0a8d0c271a0524f6216a9ae5c222746227 Mon Sep 17 00:00:00 2001 From: Grazfather Date: Fri, 21 Jul 2023 16:31:33 -0400 Subject: [PATCH] nop: Add force req when not already --f (#970) --- docs/commands/nop.md | 3 ++- gef.py | 16 +++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/docs/commands/nop.md b/docs/commands/nop.md index fd4be2476..7c3ab011b 100644 --- a/docs/commands/nop.md +++ b/docs/commands/nop.md @@ -12,7 +12,8 @@ nop [LOCATION] [--i ITEMS] [--f] [--n] [--b] `--f` Force patch even when the selected settings could overwrite partial instructions -`--n` Instead of replacing whole instructions, insert ITEMS nop instructions, no matter how many instructions it overwrites +`--n` Instead of replacing whole instructions, insert ITEMS nop instructions, no matter how many +instructions it overwrites `--b` Instead of replacing whole instructions, fill ITEMS bytes with nops diff --git a/gef.py b/gef.py index cfdd42fd6..3834248e1 100644 --- a/gef.py +++ b/gef.py @@ -6009,7 +6009,7 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: args : argparse.Namespace = kwargs["arguments"] address = parse_address(args.address) num_instructions = args.n - + last_addr = gdb_get_nth_next_instruction_address(address, num_instructions) total_bytes = (last_addr - address) + gef_get_instruction_at(last_addr).size() target_addr = address + total_bytes @@ -6017,7 +6017,7 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: info(f"skipping {num_instructions} instructions ({total_bytes} bytes) from {address:#x} to {target_addr:#x}") gdb.execute(f"set $pc = {target_addr:#x}") return - + @register class NopCommand(GenericCommand): @@ -6050,10 +6050,10 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: address = parse_address(args.address) nop = gef.arch.nop_insn num_items = args.i or 1 - fill_bytes = args.b + fill_bytes = args.b fill_nops = args.n force_flag = args.f or False - + if fill_nops and fill_bytes: err("only is possible specify --b or --n at same time") return @@ -6074,8 +6074,9 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: if len(nop) > total_bytes or total_bytes % len(nop): warn(f"Patching {total_bytes} bytes at {address:#x} will result in LAST-NOP " f"(byte nr {total_bytes % len(nop):#x}) broken and may cause a crash or " - f"break disassembly. Use --f (force) to ignore this warning") + "break disassembly.") if not force_flag: + warn("Use --f (force) to ignore this warning.") return target_end_address = address + total_bytes @@ -6087,12 +6088,13 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: curr_ins = gef_next_instruction(curr_ins.address) final_ins_end_addr = curr_ins.address + curr_ins.size() - + if final_ins_end_addr != target_end_address: warn(f"Patching {total_bytes} bytes at {address:#x} will result in LAST-INSTRUCTION " f"({curr_ins.address:#x}) being partial overwritten and may cause a crash or " - f"break disassembly. You must use --f to allow misaligned patching.") + "break disassembly.") if not force_flag: + warn("Use --f (force) to ignore this warning.") return nops = bytearray(nop * total_bytes)