Change early hook location

This commit is contained in:
cdozdil
2025-04-29 16:24:38 +03:00
parent b6c0ca6384
commit 06f353ee2a
2 changed files with 119 additions and 13 deletions
+7 -4
View File
@@ -102,9 +102,15 @@ static void CheckWorkingMode()
{
LOG_FUNC();
if (Config::Instance()->EarlyHooking.value_or_default())
{
KernelHooks::Hook();
KernelHooks::HookBase();
}
bool modeFound = false;
std::string filename = Util::DllPath().filename().string();
std::string lCaseFilename(filename);
std::string lCaseFilename(filename);
wchar_t sysFolder[MAX_PATH];
GetSystemDirectory(sysFolder, MAX_PATH);
std::filesystem::path sysPath(sysFolder);
@@ -509,9 +515,6 @@ static void CheckWorkingMode()
if (!State::Instance().isWorkingAsNvngx || State::Instance().enablerAvailable)
{
if (Config::Instance()->EarlyHooking.value_or_default())
KernelHooks::Hook();
Config::Instance()->OverlayMenu.set_volatile_value((!State::Instance().isWorkingAsNvngx || State::Instance().enablerAvailable) &&
Config::Instance()->OverlayMenu.value_or_default());
+112 -9
View File
@@ -51,6 +51,8 @@ private:
inline static Kernel32Proxy::PFN_GetProcAddress o_K32_GetProcAddress = nullptr;
inline static KernelBaseProxy::PFN_FreeLibrary o_KB_FreeLibrary = nullptr;
inline static KernelBaseProxy::PFN_LoadLibraryA o_KB_LoadLibraryA = nullptr;
inline static KernelBaseProxy::PFN_LoadLibraryW o_KB_LoadLibraryW = nullptr;
inline static KernelBaseProxy::PFN_LoadLibraryExA o_KB_LoadLibraryExA = nullptr;
inline static KernelBaseProxy::PFN_LoadLibraryExW o_KB_LoadLibraryExW = nullptr;
inline static KernelBaseProxy::PFN_GetProcAddress o_KB_GetProcAddress = nullptr;
@@ -1075,6 +1077,104 @@ private:
return o_KB_FreeLibrary(lpLibrary);
}
static HMODULE hk_KB_LoadLibraryA(LPCSTR lpLibFileName)
{
if (lpLibFileName == nullptr)
return NULL;
std::string libName(lpLibFileName);
std::string lcaseLibName(libName);
for (size_t i = 0; i < lcaseLibName.size(); i++)
lcaseLibName[i] = std::tolower(lcaseLibName[i]);
if (State::SkipDllChecks())
{
if (State::SkipDllName() == "")
{
LOG_TRACE("Skip checks for: {}", lcaseLibName);
return o_KB_LoadLibraryA(lpLibFileName);
}
auto dllName = State::SkipDllName();
auto pos = lcaseLibName.rfind(dllName);
// -4 for extension `.dll`
if (pos == (lcaseLibName.length() - dllName.length()) || pos == (lcaseLibName.length() - dllName.length() - 4))
{
LOG_TRACE("Skip checks for: {}", lcaseLibName);
return o_KB_LoadLibraryA(lpLibFileName);
}
}
#if _DEBUG
LOG_TRACE("{}", lcaseLibName);
#endif
auto moduleHandle = LoadLibraryCheck(lcaseLibName, lpLibFileName);
// skip loading of dll
if (moduleHandle == (HMODULE)1)
{
SetLastError(ERROR_ACCESS_DENIED);
return NULL;
}
if (moduleHandle != nullptr)
return moduleHandle;
return o_KB_LoadLibraryA(lpLibFileName);
}
static HMODULE hk_KB_LoadLibraryW(LPCWSTR lpLibFileName)
{
if (lpLibFileName == nullptr)
return NULL;
std::wstring libName(lpLibFileName);
std::wstring lcaseLibName(libName);
for (size_t i = 0; i < lcaseLibName.size(); i++)
lcaseLibName[i] = std::towlower(lcaseLibName[i]);
if (State::SkipDllChecks())
{
if (State::SkipDllName() == "")
{
LOG_TRACE("Skip checks for: {}", wstring_to_string(lcaseLibName));
return o_KB_LoadLibraryW(lpLibFileName);
}
auto dllName = State::SkipDllName();
auto pos = wstring_to_string(lcaseLibName).rfind(dllName);
// -4 for extension `.dll`
if (pos == (lcaseLibName.length() - dllName.length()) || pos == (lcaseLibName.length() - dllName.length() - 4))
{
LOG_TRACE("Skip checks for: {}", wstring_to_string(lcaseLibName));
return o_KB_LoadLibraryW(lpLibFileName);
}
}
#if _DEBUG
LOG_TRACE("{}", wstring_to_string(lcaseLibName));
#endif
auto moduleHandle = LoadLibraryCheckW(lcaseLibName, lpLibFileName);
// skip loading of dll
if (moduleHandle == (HMODULE)1)
{
SetLastError(ERROR_ACCESS_DENIED);
return NULL;
}
if (moduleHandle != nullptr)
return moduleHandle;
return o_KB_LoadLibraryW(lpLibFileName);
}
static HMODULE hk_KB_LoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
{
if (lpLibFileName == nullptr)
@@ -1083,6 +1183,10 @@ private:
std::string libName(lpLibFileName);
std::string lcaseLibName(libName);
#if _DEBUG
LOG_TRACE("{}", lcaseLibName);
#endif
for (size_t i = 0; i < lcaseLibName.size(); i++)
lcaseLibName[i] = std::tolower(lcaseLibName[i]);
@@ -1105,10 +1209,6 @@ private:
}
}
#if _DEBUG
LOG_TRACE("{}", lcaseLibName);
#endif
auto moduleHandle = LoadLibraryCheck(lcaseLibName, lpLibFileName);
// skip loading of dll
@@ -1133,6 +1233,10 @@ private:
std::wstring libName(lpLibFileName);
std::wstring lcaseLibName(libName);
#if _DEBUG
LOG_TRACE("{}", wstring_to_string(lcaseLibName));
#endif
for (size_t i = 0; i < lcaseLibName.size(); i++)
lcaseLibName[i] = std::towlower(lcaseLibName[i]);
@@ -1155,10 +1259,6 @@ private:
}
}
#if _DEBUG
LOG_TRACE("{}", wstring_to_string(lcaseLibName));
#endif
auto moduleHandle = LoadLibraryCheckW(lcaseLibName, lpLibFileName);
// skip loading of dll
@@ -1483,9 +1583,12 @@ public:
// These hooks cause stability regressions
//o_KB_FreeLibrary = KernelBaseProxy::Hook_FreeLibrary(hk_KB_FreeLibrary);
if (State::Instance().gameQuirk == KernelBaseHooks)
{
o_KB_LoadLibraryExA = KernelBaseProxy::Hook_LoadLibraryExA(hk_KB_LoadLibraryExA);
//o_KB_LoadLibraryA = KernelBaseProxy::Hook_LoadLibraryA(hk_KB_LoadLibraryA);
//o_KB_LoadLibraryW = KernelBaseProxy::Hook_LoadLibraryW(hk_KB_LoadLibraryW);
//o_KB_LoadLibraryExA = KernelBaseProxy::Hook_LoadLibraryExA(hk_KB_LoadLibraryExA);
o_KB_LoadLibraryExW = KernelBaseProxy::Hook_LoadLibraryExW(hk_KB_LoadLibraryExW);
}