重新定位 ELF 符号

逆向工程 小精灵 符号
2021-06-20 07:19:00

如何在 elffile 中应用符号的重定位?我目前正在尝试使用 pyelftools 将其存档。奇怪的是,我几乎找不到任何关于如何做到这一点的信息,尽管有些项目必须实现这样的东西(例如 ida、angr、amoco 等)。

换句话说:我想知道精灵导入在虚拟内存中的位置。当然,这些部分应该遵守这些重定位。

非常感谢代码片段。

1个回答

因此,在玩弄 elftools 之后,我找到了以下导入符号的解决方案:

    for section in self._elf.iter_sections():
        if not isinstance(section, RelocationSection):
            continue
    symtable = self._elf.get_section(section['sh_link'])
    for relocation in section.iter_relocations():
        if not relocation['r_info_sym']:
            continue
        symbol = symtable.get_symbol(relocation['r_info_sym'])
        yield ImportSymbol(relocation['r_offset'], symbol.name)

代码灵感来自pyelftools readelf 实现

尽管 pyelftools 提供了一些重定位处理,但我还没有看到真正有效的二进制文件。它会抱怨它不知道某些重定位类型,但是对于将来,我将发布此代码段:

def _reloc_section(self, section):
    rh = RelocationHandler(self._elf)
    data = section.data()
    relocation_section = rh.find_relocations_for_section(section)
    if relocation_section:
        try:
            rh.apply_section_relocations(data, relocation_section)
        except Exception as e:
            print 'Could not relocate %s' % section.name
    return data