Skip to content

Instantly share code, notes, and snippets.

@jimmy947788
Created June 18, 2025 19:55
Show Gist options
  • Save jimmy947788/d4a04bfa3499abc82f4fc701c81636fb to your computer and use it in GitHub Desktop.
Save jimmy947788/d4a04bfa3499abc82f4fc701c81636fb to your computer and use it in GitHub Desktop.
# IDA Python Script:將剛才列出的 data item(DCD/DCQ/DCB 等)強制轉換為指令
# 🎯 用於修復誤標為資料的 code,特別是經過 VM Flatten 或 Anti-disasm 處理的 binary
import idaapi
import idautils
import idc
def is_exec_segment(seg):
return seg.perm & idaapi.SEGPERM_EXEC
def is_data_item(ea):
flags = idc.get_full_flags(ea)
return idaapi.is_data(flags)
def force_convert_data_to_code(ea):
"""強制刪除資料項目並建立指令"""
idc.del_items(ea, 0, idc.get_item_size(ea))
if idc.create_insn(ea):
print(f"[+] 已轉換為指令 @ {ea:#010x}: {idc.generate_disasm_line(ea, 0)}")
if idaapi.get_func(ea) is None:
idaapi.add_func(ea) # 可選:將其視為 function 起點
else:
print(f"[!] 無法建立指令 @ {ea:#010x},可能不是合法指令")
def convert_all_data_in_exec_load_segments():
print("=== 轉換所有可執行 LOAD 區段中誤標為 data 的項目為 code ===")
for seg_ea in idautils.Segments():
seg = idaapi.getseg(seg_ea)
seg_name = idc.get_segm_name(seg.start_ea)
if not seg or not is_exec_segment(seg):
continue
if not seg_name.startswith("LOAD"):
continue
print(f"\n[Segment: {seg_name}] {hex(seg.start_ea)} - {hex(seg.end_ea)}")
ea = seg.start_ea
while ea < seg.end_ea:
if is_data_item(ea):
force_convert_data_to_code(ea)
ea += idc.get_item_size(ea) or 1
print("[✔] 所有 data 型項目轉換為指令完成!")
import ida_auto
ida_auto.auto_wait()
convert_all_data_in_exec_load_segments()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment