决定添加一个小代码片段来展示如何遍历 .text 段,获取所有开关表位置并将跳转目标存储在字典中。
text_seg = idaapi.get_segm_by_name('.text')
jump_table = dict()
# iterate through all items within the segment
for head_ea in idautils.Heads(text_seg.startEA, text_seg.endEA):
if idc.isCode(idc.GetFlags(head_ea)):
switch_info = idaapi.get_switch_info_ex(head_ea)
if (switch_info and switch_info.jumps != 0):
loc = switch_info.jumps
jump_table[loc] = list()
element_num = switch_info.get_jtable_size()
element_size = switch_info.get_jtable_element_size()
for num in range(0, element_num):
table_entry = loc+num*element_size
jump_table[loc].append(idc.GetManyBytes(table_entry), element_size)
更新:在此基础上为新版本的 IDA 构建
def find_jumps(si: ida_nalt.switch_info_t) -> list:
jtable = []
e_size = si.get_jtable_element_size()
for num in range(0, si.get_jtable_size()):
jtable.append(int.from_bytes(ida_bytes.get_bytes(si.jumps + (num * e_size), e_size), 'little') + si.elbase)
return jtable
ea = 0x0000000000000000 # some ea
si = ida_nalt.switch_info_t()
if (ida_nalt.get_switch_info(si, ea) is not None): # jump table
jtable = find_jumps(si)
如果成功,这将解决IDA 提供elbase
的switch_info_t
结构的偏移量get_switch_info
。请参阅:https : //hex-rays.com/products/ida/support/idapython_docs/ida_nalt.html#ida_nalt.switch_info_t.elbase了解更多信息,c++
如果需要,请在https://hex交叉参考支持-rays.com/products/ida/support/sdkdoc/structswitch__info__t.html