首页
社区
课程
招聘
[求助]请问各位大佬 IDAPython在设置断点后如何添加断点回调呢?
2023-3-20 17:44 5805

[求助]请问各位大佬 IDAPython在设置断点后如何添加断点回调呢?

2023-3-20 17:44
5805

如图所示 我想自动在OpenMemoey下断点 断点触发后自动执行dump函数 达到类似于frida那样的自动化效果


[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
点赞0
打赏
分享
最新回复 (8)
雪    币: 1197
活跃值: (2639)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
iyue_t 2023-3-20 20:46
2
0
我之前搞ida脚本的时候 翻了两天官方文档,你也可以多看看官网
雪    币: 1793
活跃值: (2422)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
mb_wpitiize 2023-3-21 15:15
3
0
在 IDAPython 中,可以通过添加断点回调函数来实现在程序执行到断点处时自动触发某些操作。可以使用 idaapi 模块中的 add_bpt() 函数来添加断点,并使用 idaapi 模块中的 add_bpt_chngev_cnd() 函数为断点设置回调。

以下是一个示例代码,它添加了一个断点并设置了回调函数,当程序执行到该断点时,将会输出一条消息到 IDA 输出窗口中:

python
Copy code
import idaapi

# 定义回调函数
def my_bpt_callback(bptno):
    print("Breakpoint %d hit!" % bptno)

# 添加断点
bpt_addr = 0x401000
bpt_size = 1
bpt = idaapi.add_bpt(bpt_addr, bpt_size)

# 设置断点回调
idaapi.add_bpt_chngev_cnd(bpt, idaapi.BPT_EXEC, my_bpt_callback)
在上面的示例中,my_bpt_callback() 函数被定义为回调函数,并使用 add_bpt_chngev_cnd() 函数将其添加到断点中。在添加回调函数时,需要指定断点事件类型,例如 idaapi.BPT_EXEC 表示执行事件。

当程序执行到断点时,将会触发回调函数,并输出消息到 IDA 输出窗口中。

注意:要确保在添加断点和设置回调函数之前,已经成功加载了待调试的二进制文件。
雪    币: 62
活跃值: (533)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
万里星河 2023-3-22 15:44
4
0
mb_wpitiize 在 IDAPython 中,可以通过添加断点回调函数来实现在程序执行到断点处时自动触发某些操作。可以使用 idaapi 模块中的 add_bpt() 函数来添加断点,并使用 idaapi 模块中的 a ...

无意冒犯 为什么有一股浓浓的ChatGPT味儿 那玩意能解决的话 我也不必求助了

最后于 2023-3-22 15:44 被万里星河编辑 ,原因:
雪    币: 9337
活跃值: (3266)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
Lightal 2 2023-3-22 15:51
5
1
可以看一下ida_dbg.DBG_Hooks,dbg_bpt函数可以作为断点回调

def dbg_bpt(self, tid, ea):
        #self.log("Break point at 0x%x pid=%d" % (ea, tid))
        
        # Print fisrt argument of printf and return
        if (ea == funcEntry_printf):
            formatStrAddr = ida_dbg.get_reg_val("R0")
            formatStr = idc.get_strlit_contents(formatStrAddr)
            self.printf("Break at printf: %s" % formatStr)
            idc.set_reg_value(funcRet_printf, "PC")
            ida_dbg.continue_process()
雪    币: 62
活跃值: (533)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
万里星河 2023-3-22 17:30
6
1
Lightal 可以看一下ida_dbg.DBG_Hooks,dbg_bpt函数可以作为断点回调 def dbg_bpt(self, tid, ea): #self.log("Bre ...
import idc
import idaapi
import ida_dbg

# OpenMemory的断点地址
openMemory_break_point_address = None


# 继承ida_dbg.DBG_Hooks类 重写dbg_bpt方法
class MyHook(ida_dbg.DBG_Hooks):
    def dbg_bpt(self, tid, bptea) -> "int":
        r"""
            A user defined breakpoint was reached.
            dbg_bpt(self, tid, bptea) -> int
                @param tid (C++: thid_t)
                @param bptea (C++: ea_t)
        """
        print('meet break point...')
        if bptea == openMemory_break_point_address:
            # openMemory处断点触发 调用dump函数dump dex
            dump()
        return super().dbg_bpt(tid, bptea)

    def dbg_bpt_changed(self, *args) -> "void":
        print('bpt change...')
        return super().dbg_bpt_changed(*args)


def dump():
    dex_addr = idc.get_reg_value('R1')
    size_addr = dex_addr + 0x20
    size = idc.get_wide_dword(size_addr)
    print("base:{} size:{}".format(hex(dex_addr), hex(size)))
    dex_data = idaapi.get_bytes(dex_addr, size)
    with open('C:/dump_' + str(hex(dex_addr)) + '_' + str(size) + '_.dex', 'wb') as dex:
        dex.write(dex_data)
        print('dump ok')
    idaapi.continue_process()
    return False


# 枚举所有modules
modules = idc._get_modules()
for m in modules:
    target_name: str = m.name
    if target_name.endswith('libart.so'):
        # idc.add_bpt(m.base + 0x1202b8)
        # idaapi.add_bpt(m.base + 0x1202b8, 0, idaapi.BPT_EXEC, dump)
        # bpt = idaapi.bpt_t()
        # bpt.ea = m.base + 0x1202b8
        # 在openMemory下断
        openMemory_break_point_address = m.base + 0x1202b8
        idaapi.jumpto(openMemory_break_point_address)
        idaapi.add_bpt(openMemory_break_point_address)
        # 设置调试器回调
        myHook = MyHook()
        myHook.hook()

感谢大佬指点 已成功解决 完整代码如上 但依然有些美中不足 就是偏移是直接写死了的 虽然目前可以枚举模块找到libart.so 但我还不知道如何像frida那样枚举symbol名从而实现通过几个关键字直接定位到关键位置 从提高通用性 不知大佬有何看法?

雪    币: 9337
活跃值: (3266)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
Lightal 2 2023-3-23 13:12
7
0
万里星河 import idc import idaapi import ida_dbg # OpenMemory的断点地址 ...
我不是搞移动安全的,frida没怎么用过,不太清楚你说的枚举symbol名指的具体是什么操作
如果是想通过函数命获取地址的话可以用ida_name.get_name_ea(BADADDR, “funcName”)试试
或者google一下你想要的操作,找到API以后去这里看看具体说明
https://www.hex-rays.com/products/ida/support/idapython_docs/index.html
雪    币: 62
活跃值: (533)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
万里星河 2023-3-23 17:57
8
0
Lightal 我不是搞移动安全的,frida没怎么用过,不太清楚你说的枚举symbol名指的具体是什么操作 如果是想通过函数命获取地址的话可以用ida_name.get_name_ea(BADADDR, “fun ...
好的 多谢
雪    币: 129
活跃值: (1243)
能力值: ( LV2,RANK:15 )
在线值:
发帖
回帖
粉丝
琳宇 2023-6-26 16:39
9
0
万里星河 import idc import idaapi import ida_dbg # OpenMemory的断点地址 ...
请问这种可不可以,设置断点回调但是不会断下来啊,每次都断下来还是蛮麻烦的
游客
登录 | 注册 方可回帖
返回