MCPcopy
hub / github.com/keystone-engine/keypatch / patcher

Method patcher

keypatch.py:1607–1662  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1605
1606 # handler for Patcher menu
1607 def patcher(self):
1608 # be sure that this arch is supported by Keystone
1609 if self.kp_asm.arch is None:
1610 idc.Warning("ERROR: Keypatch cannot handle this architecture (unsupported by Keystone), quit!")
1611 return
1612
1613 selection, addr_begin, addr_end = idaapi.read_selection()
1614 if selection:
1615 # call Fill Range function on this selected code
1616 return self.fill_range()
1617
1618 address = idc.ScreenEA()
1619
1620 if self.opts is None:
1621 self.load_configuration()
1622
1623 init_assembly = None
1624 while True:
1625 f = Keypatch_Patcher(self.kp_asm, address, assembly=init_assembly, opts=self.opts)
1626 ok = f.Execute()
1627 if ok == 1:
1628 try:
1629 syntax = None
1630 if f.kp_asm.arch == KS_ARCH_X86:
1631 syntax_id = f.c_syntax.value
1632 syntax = self.kp_asm.get_syntax_by_idx(syntax_id)
1633
1634 assembly = f.c_assembly.value
1635 self.opts = f.get_opts('c_opt_chk')
1636 padding = (self.opts.get("c_opt_padding", 0) != 0)
1637 comment = (self.opts.get("c_opt_comment", 0) != 0)
1638
1639 raw_assembly = self.kp_asm.ida_resolve(assembly, address)
1640
1641 print("Keypatch: attempting to modify \"{0}\" at 0x{1:X} to \"{2}\"".format(
1642 self.kp_asm.ida_get_disasm(address), address, assembly))
1643
1644 length = self.kp_asm.patch_code(address, raw_assembly, syntax, padding, comment, None)
1645 if length > 0:
1646 # update start address pointing to the next instruction
1647 init_assembly = None
1648 address += length
1649 else:
1650 init_assembly = f.c_assembly.value
1651 if length == 0:
1652 idc.Warning("ERROR: Keypatch found invalid assembly [{0}]".format(assembly))
1653 elif length == -1:
1654 idc.Warning("ERROR: Keypatch failed to patch binary at 0x{0:X}!".format(address))
1655 elif length == -2:
1656 idc.Warning("ERROR: Keypatch can't read original data at 0x{0:X}, try again".format(address))
1657
1658 except KsError as e:
1659 print("Keypatch Error: {0}".format(e))
1660 else: # Cancel
1661 break
1662 f.Free()
1663
1664 # handler for Fill Range menu

Callers 2

runMethod · 0.95
activateMethod · 0.80

Calls 8

fill_rangeMethod · 0.95
load_configurationMethod · 0.95
Keypatch_PatcherClass · 0.85
get_syntax_by_idxMethod · 0.80
get_optsMethod · 0.80
ida_resolveMethod · 0.80
ida_get_disasmMethod · 0.80
patch_codeMethod · 0.80

Tested by

no test coverage detected