我们已将掌握了段操作,现在来看一下函数操作。

获取所有函数

for func in idautils.Functions():
    print( hex(func), idc.get_func_name(func))
    
0x401000 sub_401000
0x401010 sub_401010
0x401030 sub_401030
0x401040 sub_401040
0x401070 _main
0x4011b0 sub_4011B0
0x4011f0 @__security_check_cookie@4
...

idautils.Functions() 会返回一个列表,里面包含了从开始地址到结束地址(默认值是最小到最大值)的所有函数的起始地址。

Functions(start=None, end=None)

Get a list of functions

start: start address (default: inf.min_ea)
end:   end address (default: inf.max_ea)

return: list of function entrypoints between start and end

note: The last function that starts before 'end' is included even

更多其他函数看官方文档:

https://python.docs.hex-rays.com/idautils

idc.get_func_name(ea) 会获取 ea 这个地址所属的函数名字。

获取指定函数

IDAPython 里面包含了大量的函数相关的 API,我们从简单的开始。

看下面的一段汇编代码:

.text:00401030
.text:00401030                               ; =============== S U B R O U T I N E =======================================
.text:00401030
.text:00401030                               ; Attributes: bp-based frame
.text:00401030
.text:00401030                               sub_401030 proc near                    ; CODE XREF: sub_401040+13↓p
.text:00401030                                                                       ; ___scrt_initialize_default_local_stdio_options↓p
.text:00401030 55                            push    ebp
.text:00401031 8B EC                         mov     ebp, esp
.text:00401033 B8 18 30 40 00                mov     eax, offset unk_403018
.text:00401038 5D                            pop     ebp
.text:00401039 C3                            retn
.text:00401039
.text:00401039                               sub_401030 endp
.text:00401039
.text:00401039                               ; ---------------------------------------------------------------------------

它描述了一个函数 sub_401030。

我们将光标放到这个函数里面,然后获取这个函数的信息:

func = idaapi.get_func(idc.here())

print(type(func))
<class 'ida_funcs.func_t'>

可以看到,idaapi.get_func() 返回了的是一个 ida_funcs.func_t 的类类型,也可以参考官方文档: