Skip to content

Commit

Permalink
add suggestions 1
Browse files Browse the repository at this point in the history
  • Loading branch information
therealdreg committed Jul 21, 2023
1 parent efa316c commit 911ce98
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
8 changes: 4 additions & 4 deletions docs/commands/nop.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ The `nop` command allows you to easily patch instructions with nops.
nop [LOCATION] [--i ITEMS] [--f] [--n] [--b]
```

`LOCATION` address/symbol to patch (by default this command replace whole instruction(s))
`LOCATION` address/symbol to patch (by default this command replaces whole instructions)

`--i ITEMS` number of items to insert (default 1)

`--f` Force patch when the final instruction/nop can be broken
`--f` Force patch even when the selected settings could overwrite partial instructions

`--n` Instead replace whole instruction(s), insert the number specified by ITEMS-value of nop(s)-instruction(s)
`--n` Instead of replacing whole instructions, insert ITEMS nop instructions, no matter how many instructions it overwrites

`--b` Instead replace whole instruction(s), fill with nop(s)-instruction(s) the number specified by ITEMS-value bytes
`--b` Instead of replacing whole instructions, fill ITEMS bytes with nops


```bash
Expand Down
26 changes: 15 additions & 11 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -6026,11 +6026,11 @@ class NopCommand(GenericCommand):

_cmdline_ = "nop"
_syntax_ = ("{_cmdline_} [LOCATION] [--i ITEMS] [--f] [--n] [--b]"
"\n\tLOCATION\taddress/symbol to patch (by default this command replace whole instruction(s))"
"\n\tLOCATION\taddress/symbol to patch (by default this command replaces whole instructions)"
"\t--i ITEMS\tnumber of items to insert (default 1)"
"\t--f\tForce patch when the final instruction/nop can be broken"
"\t--n\tInstead replace whole instruction(s), insert the number specified by ITEMS-value of nop(s)-instruction(s)"
"\t--b\tInstead replace whole instruction(s), fill with nop(s)-instruction(s) the number specified by ITEMS-value bytes")
"\t--f\tForce patch even when the selected settings could overwrite partial instructions"
"\t--n\tInstead of replacing whole instructions, insert ITEMS nop instructions, no matter how many instructions it overwrites"
"\t--b\tInstead of replacing whole instructions, fill ITEMS bytes with nops")
_example_ = [f"{_cmdline_}",
f"{_cmdline_} $pc+3",
f"{_cmdline_} --i 2 $pc+3",
Expand All @@ -6053,10 +6053,12 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None:
fill_bytes = args.b
fill_nops = args.n
force_flag = args.f or False
fill_instructions = False if fill_nops or fill_bytes else True

print(num_items)

if fill_nops + fill_bytes + fill_instructions != 1:
if fill_nops and fill_bytes:
err("only is possible specify --b or --n at same time")
return

total_bytes = 0
if fill_bytes:
Expand All @@ -6072,9 +6074,10 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None:
total_bytes = (last_addr - address) + gef_get_instruction_at(last_addr).size()

if len(nop) > total_bytes or total_bytes % len(nop):
warn(f"Patching {total_bytes} bytes at {address:#x} will result in LAST-NOP (byte nr {total_bytes % len(nop):#x}) broken and may cause a crash or break disassembly")
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 break disassembly. "
f"Use --f (force) to ignore this warning")
if not force_flag:
err("you must use --f to allow this kind of patch")
return

target_end_address = address + total_bytes
Expand All @@ -6088,12 +6091,13 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None:
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 ({curr_ins.address:#x}) broken and may cause a crash or break disassembly")
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 break disassembly. "
f"You must use --f to allow misaligned patching.")
if not force_flag:
err("you must use --f to allow this kind of patch")
return

nops = bytearray(nop * total_bytes) # this array will be bigger than needed when arch nop is > 1 but who cares
nops = bytearray(nop * total_bytes)
end_address = Address(value=address + total_bytes - 1)
if not end_address.valid:
err(f"Cannot patch instruction at {address:#x}: reaching unmapped area: {end_address:#x}")
Expand Down

0 comments on commit 911ce98

Please sign in to comment.