【Android安全-IDA Python常用函数】此文章归类为:Android安全。
IDA Python 是 IDA Pro 的 Python API,用于静态分析、逆向工程和自动化脚本开发。下面给大家举例一下经常用的函数.
获取当前程序的所有函数地址和名称
1 2 3 4 5 | import idautils
for func_ea in idautils.Functions():
func_name = idc.get_func_name(func_ea)
print (f "Function: {func_name} at {hex(func_ea)}" )
|
✅ idautils.Functions()
遍历所有函数地址
✅ idc.get_func_name(ea)
获取函数名称
获取某个函数的所有基本块(Basic Blocks)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import idaapi
def get_basic_blocks(func_ea):
func = idaapi.get_func(func_ea)
if not func:
print ( "Invalid function address" )
return
flowchart = idaapi.FlowChart(func)
for block in flowchart:
print (f "Basic Block {hex(block.start_ea)} - {hex(block.end_ea)}" )
target_func_ea = 0x401000
get_basic_blocks(target_func_ea)
|
FlowChart(func)
遍历函数的基本块(CFG)
block.start_ea / block.end_ea
获取基本块范围
遍历某个函数的所有指令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import idautils
import idc
def list_instructions(func_ea):
func = idaapi.get_func(func_ea)
if not func:
print ( "Invalid function address" )
return
for head in idautils.Heads(func.start_ea, func.end_ea):
if idc.is_code(idc.get_full_flags(head)):
print (f "{hex(head)}: {idc.GetDisasm(head)}" )
target_func_ea = 0x401000
list_instructions(target_func_ea)
|
idautils.Heads(start, end)
遍历地址范围内的指令
idc.GetDisasm(addr)
获取反汇编代码
查找 call
指令
1 2 3 4 5 6 7 8 9 10 | import idautils
import idc
def find_calls():
for seg_ea in idautils.Segments():
for head in idautils.Heads(idc.get_segm_start(seg_ea), idc.get_segm_end(seg_ea)):
if idc.print_insn_mnem(head) = = "call" :
print (f "CALL at {hex(head)}: {idc.GetDisasm(head)}" )
find_calls()
|
idc.print_insn_mnem(addr)
获取指令助记符
idc.GetDisasm(addr)
反汇编当前指令
查找特定字符串
1 2 3 4 5 6 7 8 | import idautils
def find_strings():
for s in idautils.Strings():
if "password" in str (s):
print (f "Found string: {s} at {hex(s.ea)}" )
find_strings()
|
idautils.Strings()
遍历 IDA 识别的字符串
s.ea
获取字符串所在地址
查找调用某个函数的地方
1 2 3 4 5 6 7 8 9 10 | import idautils
import idc
def find_function_xrefs(func_ea):
for xref in idautils.CodeRefsTo(func_ea, 0 ):
print (f "Reference found at {hex(xref)}: {idc.GetDisasm(xref)}" )
target_func_ea = 0x401000
find_function_xrefs(target_func_ea)
|
idautils.CodeRefsTo(ea, flow)
获取调用某个地址的代码
idc.GetDisasm(addr)
获取反汇编指令
查找某个地址的所有交叉引用
1 2 3 4 5 6 7 | import idautils
def find_xrefs(target_ea):
for xref in idautils.XrefsTo(target_ea):
print (f "Xref at {hex(xref.frm)}: {idc.GetDisasm(xref.frm)}" )
find_xrefs( 0x401000 )
|
idautils.XrefsTo(addr)
查找所有引用该地址的位置
修改某条指令
1 2 3 4 5 6 7 | import idc
target_ea = 0x401000
new_instr = "nop"
idc.patch_bytes(target_ea, b "\x90" )
idc.SetAsmCmt(target_ea, "Patched!" , 1 )
|
idc.patch_bytes(ea, bytes)
修改指令字节
idc.SetAsmCmt(ea, comment, 1)
添加汇编注释
修复 IDA 识别错误的字符串
1 2 3 4 5 6 7 8 9 | import idautils
import idc
def fix_strings():
for s in idautils.Strings():
if not idc.get_strlit_contents(s.ea):
idc.create_strlit(s.ea, idc.BADADDR)
fix_strings()
|
idc.create_strlit(addr, end)
让 IDA 重新识别字符串
打开一个二进制文件并自动执行分析
1 2 3 4 5 6 7 | import idaapi
def auto_analyze():
idaapi.auto_wait()
print ( "Analysis finished!" )
auto_analyze()
|
idaapi.auto_wait()
让 IDA 自动完成基础分析
功能 |
关键 API |
获取所有函数 |
idautils.Functions() |
遍历基本块 |
idaapi.FlowChart(func) |
遍历指令 |
idautils.Heads(start, end) |
查找特定指令 |
idc.print_insn_mnem(addr) |
交叉引用分析 |
idautils.CodeRefsTo(ea, flow) |
修改指令 |
idc.patch_bytes(ea, bytes) |
修复乱码字符串 |
idc.create_strlit(addr, end) |
自动分析 |
idaapi.auto_wait() |
欧克,朋友们,上面就是常见的函数了,大家阔以去实践一下.
更多【Android安全-IDA Python常用函数】相关视频教程:www.yxfzedu.com