从加载的 DLL 函数中获取文件偏移量
Get file offset from a loaded DLL's function
我想问,如何在DLL中定位特定的(导出)函数。例如,我想在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;
}
你能帮我怎样才能完成这项简单的任务吗?
谢谢。
附言:我不坚持上面的函数,如果你能指出问题所在,或者提供一个更好的来源,那就太棒了
这将为您提供相对虚拟地址
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);
}
相关文章:
- 获取文件数据预处理器宏
- 如何在Windows内核中获取文件大小
- 从 C 中的变量获取文件的路径,C++
- 如何使用 C/C++ 和 system() 系统调用以外的其他方法在 Linux 中获取文件功能?
- 如何使用 jpeglib.h 获取文件是否为 JPEG 类型
- 谁能告诉我,程序中的错误是什么?该程序仅用于获取文件扩展名
- 我需要通过窗口句柄(HWND)获取文件,我该怎么办?
- 如何用C++部分地获取文件中的行
- 使用内置的Windows方法获取文件夹的内容
- 在 Apache Velocity 模板语言中获取文件目录
- 如何在C#或C++中获取文件或文件夹的有效权限?有什么 API 吗?
- 对字符求和并减去以获取文件
- 从 FutureAccessList 同步获取文件夹路径
- 通过HTTPS从POCO StreamCopier获取文件下载进度
- 如何使用C 17获取文件大小
- getline 无法获取文件的第一行
- 如何使用 Win API 获取文件夹中的文件列表(带过滤器)
- 无法从零MQ ZMQ_SERVER套接字中获取文件描述符
- HttpQueryInfo 获取文件大小
- 通过文件资源管理器获取文件夹目录