IDAPython一些常用的函数
参考:https://www.hex-rays.com/products/ida/support/idapython_docs/
使用IDA Pro进行自动化分析时一些常用到的函数
详见源码idautils.py
类
Class Strings
允许迭代的字符串列表。该组字符串不会被修改。(使用setup()函数更改)
s = Strings()
#...
for i in s:
print "%x: len=%d type=%d -> '%s'" % (i.ea, i.length, i.type, str(i))
一些实例方法
#清除字符串列表缓存
clear_cache(self)
#初始化
#Parameters
# default_setup:是否使用默认的setup(C strings, min len 5, ...)函数来更新String字符串
__init__(self, default_setup=False)
#刷新字符串列表
refresh(self, ea1=None, ea2=None)
#更新字符串
setup(self, strtypes=1, minlen=5, only_7bit=True, ignore_instructions=False, ea1=None, ea2=None, display_only_existing_strings=False)
#返回string or none
__getitem__(self, index)
嵌套类StringItem
class StringItem(object):
def __init__(self, si):
self.ea = si.ea
"""String ea"""
self.type = si.type
"""string type (ASCSTR_xxxxx)"""
self.length = si.length
"""string length"""
#....
函数
获取代码的引用列表
#Parameters:
# ea - 目标地址
# flow (Boolean (0/1, False/True)) - Follow normal code flow or not
#Returns:
# list of references (may be empty list) - 引用列表
# 实例
for ref in CodeRefsTo(ScreenEA(), 1):
print ref
获取代码引用的列表
#方法如上
for ref in CodeRefsFrom(ScreenEA(), 1):
print ref
获取数据引用的列表
#同上
for ref in DataRefsTo(ScreenEA()):
print ref
for ref in DataRefsFrom(ScreenEA()):
print ref
将交叉引用的代码转化为可读名称
#Parameters:
# typecode - cross-reference type code - 交叉引用型代码
XrefTypeName(typecode)
返回所有引用的地址,以list形式返回
#Parameters:
# ea - Reference address
# flags - any of ida_xref.XREF_* flags
for xref in XrefsFrom(here(), 0):
print xref.type, XrefTypeName(xref.type),'from', hex(xref.frm), 'to', hex(xref.to)
for xref in XrefsTo(here(), 0):
print xref.type, XrefTypeName(xref.type),'from', hex(xref.frm), 'to', hex(xref.to)
获取数据或者说明地址
#Parameters:
# start - start address (default: inf.minEA)
# end - end address (default: inf.maxEA)
for ref in Heads(start, end):
print hex(ref)
#Returns:
# list of heads between start and end - 获取地址之间的数据段、代码段的地址(好绕)
获取两地址间的函数地址
# 说明同上
for ref in Functions(star, end):
print hex(ref)
#Returns:
# list of heads between start and end - 返回的是所有引用到的函数地址
# NOTE:即使超出'end',也会包含'end'之前开始的最后一个函数。所有分散在多个段中的块的函数将被多次列出,但是每个段中只会列出一次。
获取功能块起始地址和结束地址
for ref in Chunks(star):
print ref
# 返回 形式为(start_ea, end_ea)的列表
# 功能块可能为未识别的代码段
获取所有名称(函数名称、变量名称)
for ref in Names():
print ref
# 返回(address, name)列表,name为所有函数、变量等名称
获取二进制文件中的段(部分)列表
for ref in Segments():
print hex(ref)
# 返回各段起始位置的地址(代码段,数据段,名称段等等......)
# eg:
# Segment type:
# Pure code
# Externs
获取函数入口点
for ref in Entries():
print ref
# 以元组(index, ordinal, ea, name)形式返回
# 返回数据结构为list
获取函数
# Parameters:
# address - 函数中任意地址
for ref in FuncItems(address):
print hex(ref)
# 返回函数所有代码地址
获取结构体
for ref in Structs():
print ref
# 返回所有结构体
# 格式为(idx, sid, name)元组的列表
获取结构体成员信息
# Parameters:
# sid - 结构体ID.
for ref in StructMembers(sid):
print
# 返回(offset, name, size)元组的列表
# Note: 如果'sid'不是指向有效的结构体,则会引发异常。
# 只返回定义的结构成员。
解码执行流程中的前一条指令(这个暂时不知道怎么用,贴出源码来)
# @param ea: address to decode
def DecodePrecedingInstruction(ea):
prev_addr, farref = idaapi.decode_preceding_insn(ea)
if prev_addr == idaapi.BADADDR:
return (None, False)
else:
return (idaapi.cmd.copy(), farref)
# return: (None or the decode instruction, farref),farref will contain 'true' if followed an xref, false otherwise
# @param ea: address to decode
def DecodePreviousInstruction(ea):
prev_addr = idaapi.decode_prev_insn(ea)
if prev_addr == idaapi.BADADDR:
return None
return idaapi.cmd.copy()
# return: None or a new insn_t instance - 返回insn_t实例
# 解码指令
def DecodeInstruction(ea):
inslen = idaapi.decode_insn(ea)
if inslen == 0:
return None
return idaapi.cmd.copy()
# 返回none或insn_t实例
数据处理
# 以map的形式存储数据
# Parameters:
# ea - start address
# length - number of words to map - 隐射字数
# func - mapping function - 映射函数
# wordsize - size of words to map [default: 1 byte]
MapDataList(ea, length, func, wordsize=1)
# return: none
Hash输出
GetInputFileMD5()
# 返回二进制文件的Hash值
输出文件目录
GetIdbDir()
# 返回IDB文件目录字符串
汇编语言转换成机器码
Assemble(ea, line)
# 将line(汇编语句字符串,也可以是一个汇编语句list)转换成机器码
# Returns:
# (False, "Error message") or (True, asm_buf) or (True, [asm_buf1, asm_buf2, asm_buf3])
未完待续