|
| 1 | +#include <intrin.h> |
| 2 | +#include <iostream> |
| 3 | +#include <Windows.h> |
| 4 | +#include <format> |
| 5 | +#include <functional> |
| 6 | +#include <future> |
| 7 | +#include <map> |
| 8 | + |
| 9 | +const std::string ascii = R"( |
| 10 | + _ _ _ |
| 11 | + | | | | (_) |
| 12 | + | |__| |_ _ _ __ ___ _ ____ ___ ___ ___ _ __ |
| 13 | + | __ | | | | '_ \ / _ \ '__\ \ / / / __|/ _ \| '__| |
| 14 | + | | | | |_| | |_) | __/ | \ V /| \__ \ (_) | | |
| 15 | + |_| |_|\__, | .__/ \___|_| \_/ |_|___/\___/|_| |
| 16 | + __/ | | |
| 17 | + _____ |___/|_| _ _ |
| 18 | + | __ \ | | | | (_) |
| 19 | + | | | | ___| |_ ___ ___| |_ _ ___ _ __ |
| 20 | + | | | |/ _ \ __/ _ \/ __| __| |/ _ \| '_ \ |
| 21 | + | |__| | __/ || __/ (__| |_| | (_) | | | | |
| 22 | + |_____/ \___|\__\___|\___|\__|_|\___/|_| |_| |
| 23 | +
|
| 24 | + https://github.com/void-stack |
| 25 | +
|
| 26 | +)"; |
| 27 | + |
| 28 | +#ifdef _WIN64 |
| 29 | +extern "C" void __fastcall _asm_fyl2xp1(void); |
| 30 | +#endif |
| 31 | + |
| 32 | +struct _cpuid_buffer_t |
| 33 | +{ |
| 34 | + uint32_t EAX; |
| 35 | + uint32_t EBX; |
| 36 | + uint32_t ECX; |
| 37 | + uint32_t EDX; |
| 38 | +}; |
| 39 | + |
| 40 | +// resources [check #Improvement Part https://secret.club/2020/01/12/battleye-hypervisor-detection.html] |
| 41 | +bool take_time() |
| 42 | +{ |
| 43 | + // If the CPUID instruction execution time is longer than the arithmetic |
| 44 | + // instruction it’s a reliable indication that the system is virtualized |
| 45 | + // because under no circumstances should the arithmetic instruction take |
| 46 | + // longer than the CPUID execution to grab vendor, or version information. |
| 47 | + // This detection will also catch those using TSC offsetting/scaling. |
| 48 | + |
| 49 | + constexpr auto measure_time = 5; |
| 50 | + |
| 51 | + long long __cpuid_time = 0; |
| 52 | + long long __fyl2xp1_time = 0; |
| 53 | + |
| 54 | + LARGE_INTEGER frequency = {}; |
| 55 | + LARGE_INTEGER start = {}; |
| 56 | + LARGE_INTEGER end = {}; |
| 57 | + |
| 58 | + QueryPerformanceFrequency(&frequency); |
| 59 | + |
| 60 | + // count the average time it takes to execute a CPUID instruction |
| 61 | + for (std::size_t i = 0; i < measure_time; ++i) |
| 62 | + { |
| 63 | + QueryPerformanceCounter(&start); |
| 64 | + _cpuid_buffer_t cpuid_data; |
| 65 | + __cpuid(reinterpret_cast<int*>(&cpuid_data), 1); |
| 66 | + QueryPerformanceCounter(&end); |
| 67 | + |
| 68 | + auto delta = end.QuadPart - start.QuadPart; |
| 69 | + |
| 70 | + delta *= 1000000000; |
| 71 | + delta /= frequency.QuadPart; |
| 72 | + |
| 73 | + __cpuid_time += delta; |
| 74 | + } |
| 75 | + |
| 76 | + // count the average time it takes to execute a FYL2XP1 instruction |
| 77 | + for (std::size_t i = 0; i < measure_time; ++i) |
| 78 | + { |
| 79 | + QueryPerformanceCounter(&start); |
| 80 | + #ifdef _WIN64 |
| 81 | + _asm_fyl2xp1(); |
| 82 | + #else |
| 83 | + _asm FYL2XP1 |
| 84 | + #endif |
| 85 | + QueryPerformanceCounter(&end); |
| 86 | + |
| 87 | + auto delta = end.QuadPart - start.QuadPart; |
| 88 | + |
| 89 | + delta *= 1000000000; |
| 90 | + delta /= frequency.QuadPart; |
| 91 | + |
| 92 | + __fyl2xp1_time += delta; |
| 93 | + } |
| 94 | + |
| 95 | + return __fyl2xp1_time <= __cpuid_time; |
| 96 | +} |
| 97 | + |
| 98 | +// resources [check https://secret.club/2020/01/12/battleye-hypervisor-detection.html] #Improvement Part |
| 99 | +bool take_time_cpuid_against_fyl2xp1() |
| 100 | +{ |
| 101 | + constexpr auto measure_times = 5; |
| 102 | + auto positives = 0; |
| 103 | + auto negatives = 0; |
| 104 | + |
| 105 | + // run the internal VM check multiple times to get an average result |
| 106 | + for (auto i = measure_times; i != 0; --i) |
| 107 | + take_time() ? ++positives : ++negatives; |
| 108 | + |
| 109 | + // if there are more positive results than negative results, the |
| 110 | + // process is likely running inside a VM |
| 111 | + const bool decision = (positives >= negatives); |
| 112 | + |
| 113 | + return decision; |
| 114 | +} |
| 115 | + |
| 116 | +// resources https://secret.club/2020/04/13/how-anti-cheats-detect-system-emulation.html |
| 117 | +bool check_invalid_leaf() |
| 118 | +{ |
| 119 | + constexpr unsigned int invalid_leaf = 0x04201337; |
| 120 | + constexpr unsigned int valid_leaf = 0x40000000; |
| 121 | + |
| 122 | + _cpuid_buffer_t InvalidLeafResponse = {}; |
| 123 | + _cpuid_buffer_t ValidLeafResponse = {}; |
| 124 | + |
| 125 | + __cpuid(reinterpret_cast<int32_t*>(&InvalidLeafResponse), invalid_leaf); |
| 126 | + __cpuid(reinterpret_cast<int32_t*>(&ValidLeafResponse), valid_leaf); |
| 127 | + |
| 128 | + if ((InvalidLeafResponse.EAX != ValidLeafResponse.EAX) || |
| 129 | + (InvalidLeafResponse.EBX != ValidLeafResponse.EBX) || |
| 130 | + (InvalidLeafResponse.ECX != ValidLeafResponse.ECX) || |
| 131 | + (InvalidLeafResponse.EDX != ValidLeafResponse.EDX)) |
| 132 | + return true; |
| 133 | + |
| 134 | + return false; |
| 135 | +} |
| 136 | + |
| 137 | +// resources https://secret.club/2020/04/13/how-anti-cheats-detect-system-emulation.html |
| 138 | +bool check_highest_low_function_leaf() |
| 139 | +{ |
| 140 | + constexpr auto queryVendorIdMagic = 0x40000000; |
| 141 | + |
| 142 | + _cpuid_buffer_t regs = {}; |
| 143 | + __cpuid(reinterpret_cast<int32_t*>(®s), queryVendorIdMagic); |
| 144 | + |
| 145 | + _cpuid_buffer_t reserved_regs = {}; |
| 146 | + __cpuid(reinterpret_cast<int32_t*>(&reserved_regs), 1); |
| 147 | + |
| 148 | + __cpuid(reinterpret_cast<int32_t*>(&reserved_regs), reserved_regs.EAX); |
| 149 | + |
| 150 | + if (reserved_regs.EAX != regs.EAX || |
| 151 | + reserved_regs.EBX != regs.EBX || |
| 152 | + reserved_regs.ECX != regs.ECX || |
| 153 | + reserved_regs.EDX != regs.EDX) |
| 154 | + return true; |
| 155 | + |
| 156 | + return false; |
| 157 | +} |
| 158 | + |
| 159 | +// resouces https://kb.vmware.com/s/article/1009458 |
| 160 | +bool check_for_known_hypervisor() |
| 161 | +{ |
| 162 | + _cpuid_buffer_t cpuInfo = {}; |
| 163 | + __cpuid(reinterpret_cast<int32_t*>(&cpuInfo), 1); |
| 164 | + |
| 165 | + if (!(cpuInfo.ECX & (1 << 31))) // check bit 31 of register ECX, which is “hypervisor present bit” |
| 166 | + return false; // if not present return |
| 167 | + |
| 168 | + // we know hypervisor is present we can query the vendor id. |
| 169 | + constexpr auto queryVendorIdMagic = 0x40000000; |
| 170 | + __cpuid(reinterpret_cast<int32_t*>(&cpuInfo), queryVendorIdMagic); |
| 171 | + |
| 172 | + // construct string for our vendor name |
| 173 | + constexpr auto size = 13; |
| 174 | + const auto presentVendor = new char[size]; |
| 175 | + memcpy(presentVendor + 0, &cpuInfo.EBX, 4); |
| 176 | + memcpy(presentVendor + 4, &cpuInfo.ECX, 4); |
| 177 | + memcpy(presentVendor + 8, &cpuInfo.EDX, 4); |
| 178 | + presentVendor[12] = '\0'; |
| 179 | + |
| 180 | + // check against known vendor names |
| 181 | + const char* vendors[]{ |
| 182 | + "KVMKVMKVM\0\0\0", // KVM |
| 183 | + "Microsoft Hv", // Microsoft Hyper-V or Windows Virtual PC */ |
| 184 | + "VMwareVMware", // VMware |
| 185 | + "XenVMMXenVMM", // Xen |
| 186 | + "prl hyperv ", // Parallels |
| 187 | + "VBoxVBoxVBox" // VirtualBox |
| 188 | + }; |
| 189 | + |
| 190 | + for (const auto& vendor : vendors) |
| 191 | + { |
| 192 | + if (!memcmp(vendor, presentVendor, size)) |
| 193 | + { |
| 194 | + std::cout << "\tFound known hypervisor: " << presentVendor << std::endl; |
| 195 | + return true; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + std::cout << "\tFound unknown hypervisor: " << presentVendor << std::endl; |
| 200 | + return false; |
| 201 | +} |
| 202 | + |
| 203 | +void arch() |
| 204 | +{ |
| 205 | + if constexpr (const int* pInt = nullptr; sizeof(pInt) == 8) |
| 206 | + std::cout << "Version: x64" << std::endl; |
| 207 | + else if constexpr (sizeof(pInt) == 4) |
| 208 | + std::cout << "Version: x86" << std::endl; |
| 209 | +} |
| 210 | + |
| 211 | +int main() |
| 212 | +{ |
| 213 | + // test all functions |
| 214 | + const std::map<std::string_view, std::function<bool()>> callbacks = { |
| 215 | + {"Profiling CPUID against FYL2XP1", &take_time_cpuid_against_fyl2xp1}, |
| 216 | + {"Checking highest low function leaf", &check_highest_low_function_leaf}, |
| 217 | + {"Checking invalid leaf", &check_invalid_leaf}, |
| 218 | + {"Checking for known hypervisor vendors", &check_for_known_hypervisor} |
| 219 | + }; |
| 220 | + |
| 221 | + std::cout << ascii << std::endl; |
| 222 | + |
| 223 | + arch(); |
| 224 | + |
| 225 | + const auto log = [&](const std::function<bool()> callback, |
| 226 | + const std::string_view label) |
| 227 | + { |
| 228 | + const std::uint32_t condition = callback(); |
| 229 | + |
| 230 | + (condition |
| 231 | + ? std::cout |
| 232 | + << "* [" << label << "] ~ " << "Detected [!]" |
| 233 | + : std::cout |
| 234 | + << "* " << label << " ~ " << "Passed [+]") << std::endl; |
| 235 | + }; |
| 236 | + |
| 237 | + for (const auto& [callback, value] : callbacks) |
| 238 | + log(value, callback); |
| 239 | + |
| 240 | + std::cin.get(); |
| 241 | +} |
0 commit comments