Skip to content

Commit

Permalink
nop: Add force req when not already --f (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grazfather authored Jul 21, 2023
1 parent 99c59a9 commit 9170ac0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
3 changes: 2 additions & 1 deletion docs/commands/nop.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 9 additions & 7 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -6009,15 +6009,15 @@ 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

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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 9170ac0

Please sign in to comment.