Skip to content

Commit 6302206

Browse files
committed
Add hypervisor detection
1 parent 01149af commit 6302206

1 file changed

Lines changed: 54 additions & 2 deletions

File tree

auth.cpp

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <wintrust.h>
4646
#include <softpub.h>
4747
#include <cwctype>
48+
#include <intrin.h>
4849
#include <stdexcept>
4950
#include <string>
5051
#include <array>
@@ -90,6 +91,7 @@ bool duplicate_system_modules_present();
9091
bool user_writable_module_present();
9192
bool module_has_rwx_section(HMODULE mod);
9293
bool core_modules_signed();
94+
bool hypervisor_present();
9395
std::string seed;
9496
void cleanUpSeedData(const std::string& seed);
9597
std::string signature;
@@ -1937,6 +1939,56 @@ bool user_writable_module_present()
19371939
return false;
19381940
}
19391941

1942+
static bool reg_key_exists(HKEY root, const wchar_t* path)
1943+
{
1944+
HKEY h = nullptr;
1945+
const LONG res = RegOpenKeyExW(root, path, 0, KEY_READ, &h);
1946+
if (res == ERROR_SUCCESS) {
1947+
RegCloseKey(h);
1948+
return true;
1949+
}
1950+
return false;
1951+
}
1952+
1953+
static bool file_exists(const std::wstring& path)
1954+
{
1955+
const DWORD attr = GetFileAttributesW(path.c_str());
1956+
return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_DIRECTORY);
1957+
}
1958+
1959+
bool hypervisor_present()
1960+
{
1961+
int cpu_info[4] = {};
1962+
__cpuid(cpu_info, 1);
1963+
const bool hv_bit = (cpu_info[2] & (1 << 31)) != 0;
1964+
if (hv_bit) {
1965+
return true;
1966+
}
1967+
1968+
// registry artifacts (conservative)
1969+
if (reg_key_exists(HKEY_LOCAL_MACHINE, L"HARDWARE\\ACPI\\DSDT\\VBOX__") ||
1970+
reg_key_exists(HKEY_LOCAL_MACHINE, L"HARDWARE\\ACPI\\DSDT\\VMWARE") ||
1971+
reg_key_exists(HKEY_LOCAL_MACHINE, L"HARDWARE\\ACPI\\DSDT\\XEN") ||
1972+
reg_key_exists(HKEY_LOCAL_MACHINE, L"SOFTWARE\\VMware, Inc.\\VMware Tools") ||
1973+
reg_key_exists(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Oracle\\VirtualBox Guest Additions")) {
1974+
return true;
1975+
}
1976+
1977+
// file artifacts (drivers/tools)
1978+
if (file_exists(L"C:\\Windows\\System32\\drivers\\VBoxGuest.sys") ||
1979+
file_exists(L"C:\\Windows\\System32\\drivers\\VBoxMouse.sys") ||
1980+
file_exists(L"C:\\Windows\\System32\\drivers\\VBoxSF.sys") ||
1981+
file_exists(L"C:\\Windows\\System32\\drivers\\VBoxVideo.sys") ||
1982+
file_exists(L"C:\\Windows\\System32\\drivers\\vmhgfs.sys") ||
1983+
file_exists(L"C:\\Windows\\System32\\drivers\\vmmouse.sys") ||
1984+
file_exists(L"C:\\Windows\\System32\\drivers\\vm3dmp.sys") ||
1985+
file_exists(L"C:\\Windows\\System32\\drivers\\xen.sys")) {
1986+
return true;
1987+
}
1988+
1989+
return false;
1990+
}
1991+
19401992
void KeyAuth::api::setDebug(bool value) {
19411993
KeyAuth::api::debug = value;
19421994
}
@@ -2323,7 +2375,7 @@ void checkInit() {
23232375
const auto last_mod = last_module_check.load();
23242376
if (now - last_mod > 60) {
23252377
last_module_check.store(now);
2326-
if (!module_paths_ok() || duplicate_system_modules_present() || user_writable_module_present() || !core_modules_signed()) {
2378+
if (!module_paths_ok() || duplicate_system_modules_present() || user_writable_module_present() || !core_modules_signed() || hypervisor_present()) {
23272379
error(XorStr("module path check failed, possible side-load detected."));
23282380
}
23292381
}
@@ -2355,7 +2407,7 @@ void integrity_watchdog() {
23552407
const auto last_mod = last_module_check.load();
23562408
if (now - last_mod > 120) {
23572409
last_module_check.store(now);
2358-
if (!module_paths_ok() || duplicate_system_modules_present() || user_writable_module_present() || !core_modules_signed()) {
2410+
if (!module_paths_ok() || duplicate_system_modules_present() || user_writable_module_present() || !core_modules_signed() || hypervisor_present()) {
23592411
error(XorStr("module path check failed, possible side-load detected."));
23602412
}
23612413
}

0 commit comments

Comments
 (0)