Advertisement
Thunder-Menu

RDR2_IDA_SCRIPTFILE_LeaSubPatternMovCallAsm.py

Aug 4th, 2023
1,236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import idautils
  2. import idc
  3. import idaapi
  4.  
  5. output_file = "result.txt"
  6.  
  7. def find_mov_instructions():
  8.     with open(output_file, "w") as file:
  9.         imagebase = idaapi.get_imagebase()
  10.         lea_address = None
  11.         call_address = None
  12.         for seg_ea in idautils.Segments():
  13.             seg_name = idc.get_segm_name(seg_ea)
  14.             if seg_name == ".text":
  15.                 for head in idautils.Heads(seg_ea, idc.get_segm_end(seg_ea)):
  16.                     mnem = idc.print_insn_mnem(head)
  17.                     if mnem == "lea":
  18.                         opnd_type = idc.get_operand_type(head, 1)
  19.                         if opnd_type == idc.o_mem:
  20.                             opnd = idc.get_operand_value(head, 1)
  21.                             if idc.get_segm_name(opnd) == ".text" and idc.print_insn_mnem(opnd) == "sub":
  22.                                 lea_address = head
  23.                                 lea_hex_view = get_hex_view(lea_address)
  24.                                 sub_address = opnd
  25.                                 sub_hex_view = get_hex_view(sub_address)
  26.                                 mov_address = None
  27.                                 mov_value_hex = None
  28.                                 asm = None
  29.                     elif mnem == "call":
  30.                         call_address = head
  31.                     elif mnem == "mov":
  32.                         if lea_address and call_address and not mov_address:
  33.                             mov_address = head
  34.                             mov_value_hex = get_mov_value_hex(mov_address)
  35.                             if is_valid_mov_value(mov_value_hex):
  36.                                 asm = idc.GetDisasm(mov_address)
  37.                                 if "[" not in asm and "00000" not in mov_value_hex:
  38.                                     file.write("lea Address: {}\n".format(hex(lea_address)))
  39.                                     file.write("lea Address - Imagebase: {}\n".format(hex(lea_address - imagebase)))
  40.                                     file.write("lea sub Hex View: {}\n".format(sub_hex_view))
  41.                                     file.write("mov Address: {}\n".format(hex(mov_address)))
  42.                                     file.write("mov value: {}\n".format(mov_value_hex))
  43.                                     file.write("call Address: {}\n".format(hex(call_address)))
  44.                                     file.write("mov ASM: {}\n".format(asm))
  45.                                     file.write("\n")
  46.                                     file.write("\n")
  47.                     else:
  48.                         lea_address = None
  49.                         call_address = None
  50.  
  51. def get_hex_view(address):
  52.     bytes_str = ""
  53.     bytes = idc.get_bytes(address, 16)
  54.     for byte in bytes:
  55.         bytes_str += "{:02X} ".format(byte)
  56.     return bytes_str.strip()
  57.  
  58. def get_mov_value_hex(address):
  59.     mov_value = idc.get_operand_value(address, 1)
  60.     mov_value_hex = "{:016X}".format(mov_value)
  61.     return mov_value_hex
  62.  
  63. def is_valid_mov_value(mov_value_hex):
  64.     return len(mov_value_hex) == 16 and "00000" not in mov_value_hex
  65.  
  66. find_mov_instructions()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement