Skip to content

Commit

Permalink
provide more info when padding
Browse files Browse the repository at this point in the history
  • Loading branch information
aquynh committed Sep 15, 2016
1 parent e50e1c6 commit dbf58fa
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions keypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,8 @@ def patch_code(self, address, assembly, syntax, padding, save_origcode, orig_asm
if orig_comment == None:
orig_comment = ''

nop_comment = ""
padding_len = 0
if not undo:
# we are patching via Patcher
(orig_encoding, orig_len) = self.ida_get_item(address)
Expand All @@ -697,17 +699,21 @@ def patch_code(self, address, assembly, syntax, padding, save_origcode, orig_asm
# for now, only support NOP padding on Intel CPU
if padding and self.arch == KS_ARCH_X86:
if patch_len < orig_len:
padding_len = orig_len - patch_len
patch_len = orig_len
patch_data = patch_data.ljust(patch_len, X86_NOP)
elif patch_len > orig_len:
patch_end = address + patch_len - 1
ins_end = ItemEnd(patch_end)
padding_len = ins_end - patch_end
padding_len = ins_end - patch_end - 1

if padding_len > 0:
patch_len = ins_end - address
patch_data = patch_data.ljust(patch_len, X86_NOP)

if padding_len > 0:
nop_comment = "\nKeypatch padded NOP to next boundary: {0} bytes".format(padding_len)

orig_asm = self.ida_get_disasm_range(address, address + patch_len)
else:
# we are reverting the change via "Undo" menu
Expand All @@ -723,15 +729,19 @@ def patch_code(self, address, assembly, syntax, padding, save_origcode, orig_asm
if save_origcode == True:
# append original instruction to comments
if orig_comment == '':
new_patch_comment = "Keypatch modified this from:\n {0}".format('\n '.join(orig_asm))
new_patch_comment = "Keypatch modified this from:\n {0}{1}".format('\n '.join(orig_asm), nop_comment)
else:
new_patch_comment = "\nKeypatch modified this from:\n {0}".format('\n '.join(orig_asm))
new_patch_comment = "\nKeypatch modified this from:\n {0}{1}".format('\n '.join(orig_asm), nop_comment)

new_comment = "{0}{1}".format(orig_comment, new_patch_comment)
idc.MakeComm(address, new_comment)

print("Keypatch: successfully patched {0:d} byte(s) at 0x{1:X} from [{2}] to [{3}]".format(plen,
if padding_len == 0:
print("Keypatch: successfully patched {0:d} byte(s) at 0x{1:X} from [{2}] to [{3}]".format(plen,
address, to_hexstr(p_orig_data), to_hexstr(patch_data)))
else:
print("Keypatch: successfully patched {0:d} byte(s) at 0x{1:X} from [{2}] to [{3}], with {4} byte(s) NOP padded".format(plen,
address, to_hexstr(p_orig_data), to_hexstr(patch_data), padding_len))
# save this patching for future "undo"
patch_info.append((address, assembly, p_orig_data, new_patch_comment))
else: # we are reverting
Expand Down

0 comments on commit dbf58fa

Please sign in to comment.