使用 IDAPython API (user_cmts_*) 获取用户评论?

逆向工程 艾达 Python
2021-07-01 08:06:48

如何user_cmts_*从 IDAPython API调用函数?我对 SDK 和 IDAPython 很陌生,所以我对应该传递给这些函数的内容有点迷茫,因为它不是对用户最友好的文档 imo。我尝试传递这样的地图user_cmts_begin

import idaapi

def print_cmt(cmt):
   print cmt

cumap = map(print_cmt, [some address here to test])

idaapi.user_cmts_begin(cumap)

但它引发了一个类型错误,所以很明显我一定是做错了什么......

现在我不得不求助于这样做:

import idaapi
import re

addr = 0x80000000

while addr < 0x80200000:
    cmt = idaapi.get_cmt(addr, 0) # get non-repeatable comment at current address/line

    if cmt is not None: # skip if the line has no comment
        if re.search("insert regex for non-auto-generated comments here", cmt) is not None:
            print "%08X: %s" % (addr, cmt)

    addr = idaapi.next_not_tail(addr)

IDAPython 文档可以在这里找到:

https://www.hex-rays.com/products/ida/support/idapython_docs/

有人可以举个例子吗?

1个回答

您可能想要使用 IDAPython 包装函数。正如您所提到的,IDA API 的文档很差。真正理解它的最好但不是最简单的方法之一是检查IDA/Python/idc.py文件夹中的 IDAPython 包装器库

根据我在idc.py文件中看到的内容,您可能需要查看一些功能以帮助您。

创建对当前行的注释:

MakeComm(ScreenEA(), "Comment Test")

为当前行创建一个可重复的注释:

MakeRptCmt(ScreenEA(), "Repeatable Comment")

从当前行检索注释:

指定 1 以获取可重复注释或指定 0 以获取普通注释作为第二个参数。请注意,该函数CommentEx是围绕GetCommentEx.

c = GetCommentEx(ScreenEA(), 1)
print(c)

或者更简单的是使用Comment(ea)or RptCmt(ea),它们是以下内容的包装器GetCommentEx()

def Comment(ea):                return GetCommentEx(ea, 0)
"""Returns the regular comment or None"""

def RptCmt(ea):                 return GetCommentEx(ea, 1)
"""Returns the repeatable comment or None"""

然后你有所有的特殊函数来从特定的结构中检索注释,比如枚举、函数、常量……我不相信我见过一个特定的函数来检索所有注释,但它很容易构建,就像你所做的一样:

例子:

我制作了这段代码来查找包含“跳转:0x???”的行 评论/可重复评论。不是最好的代码(慢),但它说明了 IDA Python 注释函数的使用。

def get_comments(_startea, _endea, _filter):
    matches = []
    ea = _startea
    for ea in range(_startea, _endea):
        cmt1 = Comment(ea)
        cmt2 = RptCmt(ea)
        cmt = ""
        if cmt1:
            cmt += cmt1
        if cmt2:
            cmt += cmt2
        if (cmt):
            re_match = re.match(_filter, cmt, re.I)
            if (re_match and len(re_match.groups()) > 0):
                matches.append(re_match.group(3))
    return matches

MakeComm(ScreenEA(), 'jump: 0xBADC0DE')
filter = r'(.*)(jump: 0x)([0-9a-fA-F]+)(.*)'
addrs = get_comments(MinEA(), MaxEA(), filter)