参数:函数的起始地址
返回结果:一个迭代器类型,里面包含了函数的所有 item 的起始地址。这个迭代器可以转成 list 类型。
dism_addr = list(idautils.FuncItems(idc.here()))
type(dism_addr)
Out[7]: list
print(dism_addr)
[4198512, 4198513, 4198515, 4198521, 4198526, 4198528, 4198531, 4198535, 4198545, 4198555, 4198560, 4198565, 4198568, 4198574, 4198575, 4198578, 4198579, 4198585, 4198588, 4198591, 4198592, 4198597, 4198600, 4198606, 4198616, 4198618, 4198624, 4198627, 4198633, 4198639, 4198645, 4198647, 4198653, 4198658, 4198664, 4198670, 4198672, 4198675, 4198676, 4198681, 4198686, 4198689, 4198694, 4198699, 4198702, 4198708, 4198709, 4198712, 4198713, 4198719, 4198722, 4198725, 4198726, 4198732, 4198735, 4198741, 4198747, 4198748, 4198754, 4198755, 4198760, 4198763, 4198766, 4198770, 4198772, 4198774, 4198779, 4198784, 4198787, 4198789, 4198794, 4198799, 4198802, 4198808, 4198810, 4198813, 4198815, 4198820, 4198822, 4198823]
for line in dism_addr:
print(hex(line), idc.generate_disasm_line(line, 0))
0x401070 push ebp
0x401071 mov ebp, esp
0x401073 sub esp, 94h
0x401079 mov eax, ___security_cookie
0x40107e xor eax, ebp
0x401080 mov [ebp+var_4], eax
0x401083 mov [ebp+var_7D], 0
0x401087 mov [ebp+var_88], 0
0x401091 mov [ebp+Size], 8
0x40109b push offset Format; "Pone un User\\n"
...
下面看一个例子:
for func in idautils.Functions():
flags = idc.get_func_attr(func, idc.FUNCATTR_FLAGS)
if flags & idc.FUNC_LIB or flags & idc.FUNC_THUNK:
continue
dism_addr = list(idautils.FuncItems(func))
for line in dism_addr:
m = idc.print_insn_mnem(line)
if m == 'call' or m == 'jmp':
op = idc.get_operand_type(line, 0)
if op == idc.o_reg:
print("0x%x %s" % (line, idc.generate_disasm_line(line, 0)))
0x401a9e call edi
0x401ac9 call edi
上面的代码工作流程如下:
像 call eax 或者 jmp edi 这种都是动态调用。
在 idapython 编程中,将迭代器类型转成 list 非常好用,如果直接对迭代器求长度,会报错:
len(idautils.FuncItems(idc.here()))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[4], line 1
----> 1 len(idautils.FuncItems(idc.here()))
TypeError: object of type 'func_item_iterator_t' has no len()
用list转一下,再求长度就可以:
len(list(idautils.FuncItems(idc.here())))
Out[5]: 0x14
获取ea下一个指令的起始地址。
ea = idc.here()
print(hex(ea))
0x401ac0
print(hex(idc.next_head(ea)))
0x401ac2