Skip to content

Commit 54e1ace

Browse files
committed
Add extra module check
1 parent 13311d9 commit 54e1ace

1 file changed

Lines changed: 41 additions & 2 deletions

File tree

auth.cpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@
3434

3535
#pragma comment(lib, "rpcrt4.lib")
3636
#pragma comment(lib, "httpapi.lib")
37+
#pragma comment(lib, "psapi.lib")
3738

3839
#include <cstdio>
3940
#include <iostream>
4041
#include <memory>
4142
#include <algorithm>
43+
#include <psapi.h>
4244
#include <stdexcept>
4345
#include <string>
4446
#include <array>
@@ -80,6 +82,7 @@ void integrity_watchdog();
8082
std::string extract_host(const std::string& url);
8183
bool hosts_override_present(const std::string& host);
8284
bool module_paths_ok();
85+
bool duplicate_system_modules_present();
8386
std::string seed;
8487
void cleanUpSeedData(const std::string& seed);
8588
std::string signature;
@@ -1795,6 +1798,42 @@ bool module_paths_ok()
17951798
return true;
17961799
}
17971800

1801+
bool duplicate_system_modules_present()
1802+
{
1803+
const wchar_t* kModules[] = { L"ntdll.dll", L"kernel32.dll", L"kernelbase.dll", L"user32.dll" };
1804+
const wchar_t* sysroot_env = _wgetenv(L"SystemRoot");
1805+
std::wstring sysroot = sysroot_env ? sysroot_env : L"C:\\Windows";
1806+
std::wstring sys32 = to_lower_ws(sysroot + L"\\System32\\");
1807+
std::wstring syswow = to_lower_ws(sysroot + L"\\SysWOW64\\");
1808+
1809+
HMODULE mods[1024] = {};
1810+
DWORD needed = 0;
1811+
if (!EnumProcessModules(GetCurrentProcess(), mods, sizeof(mods), &needed))
1812+
return false;
1813+
1814+
const size_t count = needed / sizeof(HMODULE);
1815+
for (size_t i = 0; i < count; ++i) {
1816+
wchar_t path[MAX_PATH] = {};
1817+
if (!GetModuleFileNameExW(GetCurrentProcess(), mods[i], path, MAX_PATH))
1818+
continue;
1819+
std::wstring p = to_lower_ws(path);
1820+
const auto name_pos = p.find_last_of(L"\\/");
1821+
const std::wstring name = (name_pos == std::wstring::npos) ? p : p.substr(name_pos + 1);
1822+
bool is_target = false;
1823+
for (const auto* modname : kModules) {
1824+
if (name == modname) {
1825+
is_target = true;
1826+
break;
1827+
}
1828+
}
1829+
if (!is_target)
1830+
continue;
1831+
if (p.rfind(sys32, 0) != 0 && p.rfind(syswow, 0) != 0)
1832+
return true;
1833+
}
1834+
return false;
1835+
}
1836+
17981837
void KeyAuth::api::setDebug(bool value) {
17991838
KeyAuth::api::debug = value;
18001839
}
@@ -2181,7 +2220,7 @@ void checkInit() {
21812220
const auto last_mod = last_module_check.load();
21822221
if (now - last_mod > 60) {
21832222
last_module_check.store(now);
2184-
if (!module_paths_ok()) {
2223+
if (!module_paths_ok() || duplicate_system_modules_present()) {
21852224
error(XorStr("module path check failed, possible side-load detected."));
21862225
}
21872226
}
@@ -2213,7 +2252,7 @@ void integrity_watchdog() {
22132252
const auto last_mod = last_module_check.load();
22142253
if (now - last_mod > 120) {
22152254
last_module_check.store(now);
2216-
if (!module_paths_ok()) {
2255+
if (!module_paths_ok() || duplicate_system_modules_present()) {
22172256
error(XorStr("module path check failed, possible side-load detected."));
22182257
}
22192258
}

0 commit comments

Comments
 (0)