在 IDA Pro 中使用通配符查找直接值

逆向工程 艾达
2021-06-30 10:05:29

我正在对 ARM 固件进行逆向工程,并希望找到对内存映射外围设备的所有引用,在这种情况下,这意味着 0x40000000 - 0x40007FFF。

这些经常使用直接引用来引用。

我可以为 USART2 搜索 0x40004400,但更愿意搜索 0x4000????并找到所有参考。

反正有这样做吗?

1个回答

由于 ARM 操作码每个都是 4 字节,因此您将无法0x4000????使用简单的二进制搜索找到所以我们必须使用更复杂的方法。

操作数搜索

使用Sark,您可以从程序中的所有操作码中获取操作数值并检查它们:

import sark

for line in sark.lines():
    if not line.is_code:
        continue
    for operand in line.insn.operands:
        if operand.type.is_imm:
            if 0x40000000 <= operand.value < 0x40008000:
                print 'Reference Found at x{:08X}'.format(operand.value)

但这是很多代码,在大型固件中运行整个 IDB 可能需要一段时间。

参考搜索

由于内存映射外设不在固件文件本身中,我们需要为它创建一个新段。

idaapi.add_segm(0, 0x40000000, 0x40008000, 'peripherals', 'PERIPHERALS')
# You can use more advanced APIs to make the storate sparse, and properly set all the
# properties of the segment, but this is irrelevant now.

创建段后,IDA 将自动从所有相关操作数创建对它的引用(如果没有,您始终可以强制自动分析)。分析完成后,我们需要做的就是检查新段并列出对它的所有引用。

for line in sark.Segment(name='peripherals').lines:
    for xref in line.xrefs_to:
        print 'Found a reference at 0x{:08X}'.format(xref.frm)