如何使用 PortEx 从 Import Table 中获取 DLL、函数名称和地址?

How to get DLLs, function names, and addresses from the Import Table with PortEx?

我正在将 PortEx Java library for PE32 parsing 与 Capstone 反汇编程序一起使用,我希望能够让反汇编程序将适当的 call 0x404040 行替换为类似于 call SomeDLL:TheFunc .为此,我需要从 Import Table 中导入。我能够获取 DLL 名称和函数,但 PortEx 报告的地址有偏差,例如:0x32E8 与 pefile Python 模块报告的 0x402004。我已经尝试查看一些偏移量作为 PortEx 中 ImportSectionImportDLLNameImport 类 的一部分,但它并没有接近。有什么想法吗?

import com.github.katjahahn.parser.*;
public class ImportsExtractor {
    public static Map<Integer,String> extract(PEData exe) throws IOException {
        Map<Integer,String> importList = new HashMap<>();

        SectionLoader loader = new SectionLoader(exe);
        ImportSection idata = loader.loadImportSection();
        List<ImportDLL> imports = idata.getImports();
        for(ImportDLL dll : imports) {
            for(NameImport nameImport : dll.getNameImports()) {
                long addr = nameImport.getRVA(); // Some offset needed?
                System.out.format("0x%X\t%s:%s%n", addr, dll.getName(), nameImport.getName());
                importList.put((int)addr, dll.getName() + ":" + nameImport.getName());
            }
        }
        return importList;
    }
}

我希望能够从一行汇编中获取地址,看看它是否在 importList 中,如果是,则将地址替换为 importList 中的值。

来自作者:

public static Map<Integer,String> extract(PEData exe) throws IOException {
    Map<Integer,String> importList = new HashMap<>();

    SectionLoader loader = new SectionLoader(exe);
    ImportSection idata = loader.loadImportSection();
    List<ImportDLL> imports = idata.getImports();
    for(ImportDLL dll : imports) {
        for(NameImport nameImport : dll.getNameImports()) {
            long iat = nameImport
                    .getDirEntryValue(DirectoryEntryKey.I_ADDR_TABLE_RVA);
            long ilt = nameImport
                    .getDirEntryValue(DirectoryEntryKey.I_LOOKUP_TABLE_RVA);
            long imageBase = exe.getOptionalHeader().get(
                    WindowsEntryKey.IMAGE_BASE);
            long addr = nameImport.getRVA() + imageBase;
            if(ilt != 0) addr = addr - ilt + iat;
            System.out.format("0x%X\t%s:%s%n", addr, dll.getName(), nameImport.getName());
            importList.put((int)addr, dll.getName() + ":" + nameImport.getName());
        }
    }

    return importList;
}