【问题标题】:Get file offset from a loaded DLL's function从加载的 DLL 函数中获取文件偏移量
【发布时间】:2014-12-10 23:28:11
【问题描述】:

我想问一下,我如何才能在 DLL 中找到特定的 (exported) 函数。例如,我想在 Kernel32 中找到 ReadProcessMemory。我不想依赖 Import 表,我想根据我使用自定义函数获得的地址来定位不同的 API。

我尝试对 VA、RVA 和文件偏移量进行小型研究,但没有成功。这是我尝试过的一个示例,但它不起作用(在所有情况下都返回 0)

DWORD Rva2Offset(DWORD dwRva, UINT_PTR uiBaseAddress)
{
    WORD wIndex = 0;
    PIMAGE_SECTION_HEADER pSectionHeader = NULL;
    PIMAGE_NT_HEADERS pNtHeaders = NULL;

    pNtHeaders = (PIMAGE_NT_HEADERS) (uiBaseAddress + ((PIMAGE_DOS_HEADER) uiBaseAddress)->e_lfanew);
    pSectionHeader = (PIMAGE_SECTION_HEADER) ((UINT_PTR) (&pNtHeaders->OptionalHeader) + pNtHeaders->FileHeader.SizeOfOptionalHeader);

    if (dwRva < pSectionHeader[0].PointerToRawData)
        return dwRva;

    for (wIndex = 0; wIndex < pNtHeaders->FileHeader.NumberOfSections; wIndex++)
    {
        if (dwRva >= pSectionHeader[wIndex].VirtualAddress && dwRva < (pSectionHeader[wIndex].VirtualAddress + pSectionHeader[wIndex].SizeOfRawData))
            return (dwRva - pSectionHeader[wIndex].VirtualAddress + pSectionHeader[wIndex].PointerToRawData);
    }

    return 0;
}

您能帮我完成这个简单的任务吗?

谢谢。

P.s.:我不坚持上面的功能,如果你能指出问题所在,或者提供更好的来源会很棒。

【问题讨论】:

  • 该死,没有答案? ://

标签: c++ visual-studio-2013 offset virtual-address-space


【解决方案1】:

这为您提供了相对虚拟地址

uintptr_t baseAddr = (uintptr_t)GetModuleHandle("nameOfExe.exe");
uintptr_t relativeAddr = functionAddress - baseAddr;

这会将相对虚拟地址转换为文件偏移量:

DWORD RVAToFileOffset(IMAGE_NT_HEADERS32* pNtHdr, DWORD dwRVA)
{
int i;
WORD wSections;
PIMAGE_SECTION_HEADER pSectionHdr;

pSectionHdr = IMAGE_FIRST_SECTION(pNtHdr);
wSections = pNtHdr->FileHeader.NumberOfSections;

for (i = 0; i < wSections; i++)
{
if (pSectionHdr->VirtualAddress <= dwRVA)
if ((pSectionHdr->VirtualAddress + pSectionHdr->Misc.VirtualSize) > dwRVA)
{
dwRVA -= pSectionHdr->VirtualAddress;
  dwRVA += pSectionHdr->PointerToRawData;

return (dwRVA);
}

pSectionHdr++;
}

return (-1);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-02
    • 2012-04-28
    • 2012-11-30
    • 2016-12-27
    • 1970-01-01
    • 2014-04-03
    • 2014-02-07
    • 1970-01-01
    相关资源
    最近更新 更多