avoid c++ exceptions in import hook (#2367)

This commit is contained in:
marzent 2025-08-11 20:23:15 +02:00 committed by GitHub
parent 4a62138b30
commit ae58aaff30
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -82,19 +82,36 @@ void hooks::getprocaddress_singleton_import_hook::initialize() {
s_dllChanged = 1; s_dllChanged = 1;
if (notiReason == LDR_DLL_NOTIFICATION_REASON_LOADED) { if (notiReason == LDR_DLL_NOTIFICATION_REASON_LOADED) {
const auto dllName = unicode::convert<std::string>(pData->Loaded.FullDllName->Buffer); const auto dllName = unicode::convert<std::string>(pData->Loaded.FullDllName->Buffer);
std::wstring version = L"<unknown>";
std::wstring description = L"<unknown>";
DWORD versionSize = GetFileVersionInfoSizeA(dllName.c_str(), NULL);
utils::loaded_module mod(pData->Loaded.DllBase); if (versionSize > 0) {
std::wstring version, description; std::vector<BYTE> versionData(versionSize);
try { if (GetFileVersionInfoA(dllName.c_str(), 0, versionSize, versionData.data())) {
version = utils::format_file_version(mod.get_file_version()); struct LANGANDCODEPAGE {
} catch (...) { WORD wLanguage;
version = L"<unknown>"; WORD wCodePage;
} *translate = nullptr;
UINT uLen = 0;
LPVOID lpBuffer;
if (VerQueryValueW(versionData.data(), L"\\VarFileInfo\\Translation", (LPVOID*)&translate, &uLen) && uLen >= sizeof(LANGANDCODEPAGE)) {
// Use the first language/codepage
wchar_t subBlock[256];
swprintf(subBlock, 256, L"\\StringFileInfo\\%04x%04x\\FileDescription", translate[0].wLanguage, translate[0].wCodePage);
if (VerQueryValueW(versionData.data(), subBlock, &lpBuffer, &uLen)) {
description = std::wstring((wchar_t *)lpBuffer, uLen - 1);
} }
try { swprintf(subBlock, 256, L"\\StringFileInfo\\%04x%04x\\FileVersion", translate[0].wLanguage, translate[0].wCodePage);
description = mod.get_description();
} catch (...) { if (VerQueryValueW(versionData.data(), subBlock, &lpBuffer, &uLen)) {
description = L"<unknown>"; version = std::wstring((wchar_t*)lpBuffer, uLen - 1);
}
}
}
} }
logging::I(R"({} "{}" ("{}" ver {}) has been loaded at 0x{:X} ~ 0x{:X} (0x{:X}); finding import table items to hook.)", logging::I(R"({} "{}" ("{}" ver {}) has been loaded at 0x{:X} ~ 0x{:X} (0x{:X}); finding import table items to hook.)",