- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的目标是手动填写 Dll 的导入表,以便 Hook 内部 LoadLibrary 调用(当您加载库时,它可能会在其 DllMain 中加载另一个库)。
这是我的代码,它为依赖层次结构中的每个 dll 递归地填充导入表,它工作正常,除了一些 dll (api-ms-win-crt-locale-l1-1-0.dll 在这种情况下)。
void PEUtility::fillImportTable(HMODULE loadedModule, FillImportFlag flag, std::function<void(HMODULE&)> callback)
{
std::stack<HMODULE> modules;
modules.push(loadedModule);
while (modules.size())
{
auto module = modules.top();
modules.pop();
auto imageBase = (DWORD_PTR)module;
auto header = ImageNtHeader(module);
auto importTable = (PIMAGE_IMPORT_DESCRIPTOR)(DWORD_PTR)(header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress + imageBase);
while (importTable->OriginalFirstThunk)
{
// !!!
// HERE I've got an error: importTable->Name stores 0x0000FFFF instead dll name
// !!!
auto importedModuleName = (char*)(DWORD_PTR)(importTable->Name + imageBase);
auto importedModule = GetModuleHandleA(importedModuleName);
if (!importedModule)
{
importedModule = LoadLibraryExA(importedModuleName, 0, DONT_RESOLVE_DLL_REFERENCES);
if (flag == FillImportFlag::Recursive)
modules.push(importedModule);
}
auto nameAddressPtr = (PIMAGE_THUNK_DATA)(DWORD_PTR)(importTable->OriginalFirstThunk + imageBase); //Import Lookup Table address (functions names)
auto functionAddressPtr = (PIMAGE_THUNK_DATA)(DWORD_PTR)(importTable->FirstThunk + imageBase); //Import Address Table (IAT) address (functions addresses)
while (nameAddressPtr->u1.Function)
{
FARPROC importedFunctionPtr = NULL;
if (nameAddressPtr->u1.Ordinal & IMAGE_ORDINAL_FLAG)
{
importedFunctionPtr = GetProcAddress(importedModule, MAKEINTRESOURCEA(nameAddressPtr->u1.Ordinal));
}
else
{
auto impotByNameImage = (PIMAGE_IMPORT_BY_NAME)(DWORD_PTR)(nameAddressPtr->u1.AddressOfData + imageBase);
importedFunctionPtr = GetProcAddress(importedModule, (char*)impotByNameImage->Name);
}
if (importedFunctionPtr)
{
auto oldProt = 0ul;
VirtualProtect(functionAddressPtr, sizeof(IMAGE_THUNK_DATA), PAGE_EXECUTE_READWRITE, &oldProt);
functionAddressPtr->u1.Function = (DWORD_PTR)importedFunctionPtr;
}
nameAddressPtr++;
functionAddressPtr++;
}
importTable++;
}
// HERE in callback I hook LoadLibrary & RegQueryVAlue calls if 'module' has such dependencies
callback(module);
}
}
问题是 IMAGE_IMPORT_DESCRIPTOR
结构的 Name
字段存储 0x0000FFFF
而不是 Dll 名称。
所以我的问题是我该如何解决这个问题? 0x0000FFFF
是什么意思?也许这是一些“特殊”模块(请参阅下面屏幕截图中的 api-ms-win-crt-locale-l1-1-0.dll)?
也许 0x0000FFFF 意味着 dll 没有依赖项?
我在 Dependency Walker 中查看了 api-ms-win-crt-locale-l1-1-0.dll 依赖项,似乎此 dll 没有可导入的内容。
最佳答案
从您的屏幕截图中可以清楚地看到 importTable
指向 imageBase
(即 IMAGE_DOS_HEADER
)。当 header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress == 0
时会发生这种情况 - 您不检查此条件,作为结果和错误
api-ms-win-crt-locale-l1-1-0.dll
根本没有导入 - DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
为零。需要检查 - 在处理它之前导入是否存在
关于c++ - 手动填写DLL导入表 : IMAGE_IMPORT_DESCRIPTOR's Name field stores 0x0000FFFF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40606514/
我的目标是手动填写 Dll 的导入表,以便 Hook 内部 LoadLibrary 调用(当您加载库时,它可能会在其 DllMain 中加载另一个库)。 这是我的代码,它为依赖层次结构中的每个 dll
我是一名优秀的程序员,十分优秀!