Guest User

RevStealer Scoring-Based Evasion PoC

a guest
Aug 31st, 2026
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 43.68 KB | Cybersecurity | 0 0
  1. // cl /nologo /std:c++17 /EHsc /W4 evasion-score-poc.cpp /Fe:evasion-score-poc.exe
  2. #define NOMINMAX
  3. #include <windows.h>
  4. #include <winternl.h>
  5. #include <tlhelp32.h>
  6. #include <mfapi.h>
  7. #include <mfidl.h>
  8. #include <mftransform.h>
  9. #include <intrin.h>
  10.  
  11. #include <algorithm>
  12. #include <array>
  13. #include <cstdint>
  14. #include <cstring>
  15. #include <iomanip>
  16. #include <iterator>
  17. #include <iostream>
  18. #include <optional>
  19. #include <sstream>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23.  
  24. #pragma comment(lib, "mfuuid.lib")
  25. #pragma comment(lib, "ole32.lib")
  26. #pragma comment(lib, "advapi32.lib")
  27. #pragma comment(lib, "user32.lib")
  28.  
  29. namespace evasion_poc {
  30.  
  31. /*
  32.  * This cpp POC reproduces only the environment observations, delays,
  33.  * score accumulation, and decisions recovered from FUN_140005470.
  34.  *
  35.  * VEH control flow, PEB walking, hashed API resolution, lazy string decoding,
  36.  * and unrelated RDTSC/backoff routines are anti-analysis implementation
  37.  * details but do not contribute to this score, so they are not reproduced.
  38.  */
  39.  
  40. constexpr int TERMINATION_THRESHOLD = 7;
  41. constexpr uint32_t SCORE_XOR_KEY = 0xC1172093u;
  42. constexpr uint32_t HASH_INITIAL = 0x11DDE2E3u;
  43. constexpr uint32_t HASH_MULTIPLIER = 0x01000259u;
  44. constexpr ULONG MF_VERSION_USED_BY_SAMPLE = 0x00020070u;
  45.  
  46. // The suite does not execute checks in numeric order. These two tables are
  47. // stored at RVA 0x425C0 and 0x425E8 and are byte-identical in both samples.
  48. constexpr std::array<unsigned, 10> CHECK_DISPATCH_ORDER = {
  49.     9, 3, 1, 0, 2, 5, 6, 4, 7, 8
  50. };
  51.  
  52. constexpr std::array<DWORD, 10> PRE_CHECK_DELAYS_MS = {
  53.     111, 341, 453, 390, 406, 166, 336, 430, 146, 326
  54. };
  55.  
  56. using Observation = std::pair<std::string, std::string>;
  57. using Observations = std::vector<Observation>;
  58. using TextItems = std::vector<std::string>;
  59.  
  60. struct CheckResult {
  61.     std::string name;
  62.     int score = 0;
  63.     Observations observed;
  64.     TextItems matches;
  65.     TextItems logic;
  66.     bool immediate_gate_match = false;
  67.     bool is_direct_gate = false;
  68. };
  69.  
  70. CheckResult make_check_result(
  71.         std::string name,
  72.         int score,
  73.         Observations observed,
  74.         TextItems matches,
  75.         TextItems logic,
  76.         bool immediate_gate_match = false,
  77.         bool is_direct_gate = false) {
  78.     CheckResult result;
  79.     result.name = std::move(name);
  80.     result.score = score;
  81.     result.observed = std::move(observed);
  82.     result.matches = std::move(matches);
  83.     result.logic = std::move(logic);
  84.     result.immediate_gate_match = immediate_gate_match;
  85.     result.is_direct_gate = is_direct_gate;
  86.     return result;
  87. }
  88.  
  89. std::string hex_value(uint32_t value, int width) {
  90.     std::ostringstream out;
  91.     out << "0x" << std::uppercase << std::hex
  92.         << std::setw(width) << std::setfill('0') << value;
  93.     return out.str();
  94. }
  95.  
  96. std::string wide_to_utf8(const wchar_t* value) {
  97.     if (!value || *value == L'\0') return "<empty>";
  98.     const int required = WideCharToMultiByte(
  99.         CP_UTF8, 0, value, -1, nullptr, 0, nullptr, nullptr);
  100.     if (required <= 1) return "<conversion-failed>";
  101.  
  102.     std::string output(static_cast<size_t>(required), '\0');
  103.     WideCharToMultiByte(
  104.         CP_UTF8, 0, value, -1, output.data(), required, nullptr, nullptr);
  105.     output.pop_back(); // remove the converted NUL terminator
  106.     return output;
  107. }
  108.  
  109. std::string format_fields(
  110.         const Observations& fields) {
  111.     std::string output;
  112.     if (fields.empty()) return "\n  none";
  113.     for (const auto& field : fields) {
  114.         output += "\n  " + field.first + ": " + field.second;
  115.     }
  116.     return output;
  117. }
  118.  
  119. std::string format_items(const TextItems& items) {
  120.     std::string output;
  121.     if (items.empty()) return "\n  - none";
  122.     for (const auto& item : items) {
  123.         output += "\n  - " + item;
  124.     }
  125.     return output;
  126. }
  127.  
  128. std::string language_name(LANGID language_id) {
  129.     wchar_t name[128]{};
  130.     const LCID locale_id = MAKELCID(language_id, SORT_DEFAULT);
  131.     if (GetLocaleInfoW(
  132.             locale_id,
  133.             LOCALE_SENGLISHDISPLAYNAME,
  134.             name,
  135.             static_cast<int>(std::size(name))) > 0) {
  136.         return wide_to_utf8(name);
  137.     }
  138.     return "unknown language";
  139. }
  140.  
  141. const char* display_vendor_name(uint32_t vendor) {
  142.     switch (vendor) {
  143.         case 0x1234: return "QEMU/Bochs virtual VGA";
  144.         case 0x15AD: return "VMware";
  145.         case 0x80EE: return "Oracle VirtualBox";
  146.         case 0x1B36: return "Red Hat QXL";
  147.         case 0x1AF4: return "Red Hat VirtIO";
  148.         case 0x1AE0: return "Google virtual hardware";
  149.         case 0x10DE: return "NVIDIA";
  150.         case 0x1002: return "AMD/ATI";
  151.         case 0x8086: return "Intel";
  152.         case 0x5143: return "Qualcomm";
  153.         default: return "unknown vendor";
  154.     }
  155. }
  156.  
  157. std::wstring lowercase(std::wstring s) {
  158.     // The sample only folds ASCII A-Z, rather than using locale-sensitive
  159.     // Unicode case conversion.
  160.     std::transform(s.begin(), s.end(), s.begin(), [](wchar_t ch) {
  161.         if (ch >= L'A' && ch <= L'Z') return static_cast<wchar_t>(ch + 0x20);
  162.         return ch;
  163.     });
  164.     return s;
  165. }
  166.  
  167. uint32_t hash_bytes(const uint8_t* data, size_t size) {
  168.     uint32_t h = HASH_INITIAL;
  169.     for (size_t i = 0; i < size; ++i) {
  170.         h = (h ^ data[i]) * HASH_MULTIPLIER;
  171.     }
  172.     return h;
  173. }
  174.  
  175. uint32_t hash_lowercase_utf16le(const std::wstring& input) {
  176.     const std::wstring lower = lowercase(input);
  177.     std::vector<uint8_t> bytes;
  178.     bytes.reserve(lower.size() * 2);
  179.     for (wchar_t ch : lower) {
  180.         const uint16_t u = static_cast<uint16_t>(ch);
  181.         bytes.push_back(static_cast<uint8_t>(u & 0xFF));
  182.         bytes.push_back(static_cast<uint8_t>((u >> 8) & 0xFF));
  183.     }
  184.     return hash_bytes(bytes.data(), bytes.size());
  185. }
  186.  
  187. uint32_t hash_primary_language_id(LANGID language_id) {
  188.     const uint16_t primary = static_cast<uint16_t>(language_id & 0x03FFu);
  189.     const uint8_t bytes[2] = {
  190.         static_cast<uint8_t>(primary & 0xFF),
  191.         static_cast<uint8_t>((primary >> 8) & 0xFF)
  192.     };
  193.     return hash_bytes(bytes, 2);
  194. }
  195.  
  196. template <size_t N>
  197. bool hash_in_table(uint32_t value, const std::array<uint32_t, N>& table) {
  198.     return std::find(table.begin(), table.end(), value) != table.end();
  199. }
  200.  
  201. constexpr std::array<uint32_t, 10> LANGUAGE_HASHES = {
  202.     0xC207A15A, 0x1ECD79B1, 0x6CC7F6C0, 0xE562491C, 0x2C1798A0,
  203.     0x42282173, 0xDE1D1B91, 0x1304971B, 0xFCF40E48, 0xDB1AA2DF
  204. };
  205.  
  206. constexpr std::array<uint32_t, 74> PROCESS_HASHES = {
  207.     0x09519D8D, 0x2094991E, 0x01DF72C4, 0x2EE27352, 0x9276E989,
  208.     0x14C8FBCB, 0xEEC68898, 0xB96C24DA, 0x05D400AD, 0x43139C6F,
  209.     0x9FE58E56, 0xD040A394, 0x8648793D, 0x36885C35, 0xC8884BCF,
  210.     0xD9D10B01, 0xA66FF643, 0xC74DFE5D, 0x987FA3B8, 0xE34D4614,
  211.     0xB428E51D, 0x0AC62710, 0x2395BA29, 0x783150A6, 0x2A6F17C7,
  212.     0x8E23D946, 0xE3728F6A, 0x995CDA05, 0x4B690E00, 0x685FAFAF,
  213.     0xB5232951, 0x74DFB190, 0x94547A1C, 0xAC6614B7, 0xCF06E47E,
  214.     0x71DFBD44, 0x3A601014, 0xF4557BD1, 0xE0CC6228, 0xA5A93A43,
  215.     0xBA42BF26, 0xF2427F3B, 0xC5B94BE3, 0x20899E9B, 0xE5204F02,
  216.     0x5E0B7867, 0x778C9825, 0x04001DA6, 0x15C7C164, 0x86A8FE8F,
  217.     0x88650340, 0x2F8F38FF, 0x33C2DC49, 0x92C365C8, 0x9E179F0A,
  218.     0x9EA60B3D, 0x9080C7AC, 0x2DE8A4F2, 0x67074B90, 0x639393B5,
  219.     0x369A7FB4, 0xABBF6A5D, 0x5DFBBA07, 0xC69E76E5, 0xAE67993B,
  220.     0x1E8717F8, 0x62CE2DE1, 0x810D7F11, 0xF560ED9E, 0xB4E7E5BA,
  221.     0x4CB7C91F, 0xFD643CFF, 0x7CA0EFE4, 0x0C137440
  222. };
  223.  
  224. constexpr std::array<uint32_t, 18> HOST_IDENTITY_HASHES = {
  225.     0xCBB7E8D2, 0x61646C80, 0x15CBCEC2, 0x6384F985, 0x4C009DEE,
  226.     0x3FC28A0E, 0x869FE768, 0xCC8D3CCE, 0xB1861E30, 0x87D5C8DE,
  227.     0xF480AA57, 0xD2E77172, 0xE4D589E5, 0x541B17A5, 0x143FCEF1,
  228.     0x20A4E88D, 0x84C267E9, 0xF798BB83
  229. };
  230.  
  231. // These are substring hashes used to recognize physical GPU naming tokens
  232. // before parsing a VEN_XXXX identifier from the display-device ID.
  233. constexpr std::array<uint32_t, 4> DISPLAY_TOKEN_HASHES = {
  234.     0x02C172CE, // nvidia
  235.     0x61A8D6F0, // radeon
  236.     0xA3B4D6D9, // intel
  237.     0xB74790D6  // qualcomm
  238. };
  239.  
  240. // FUN_14002AE88 stores these values XOR-obfuscated. The decoded PCI vendor
  241. // identifiers are virtualization/synthetic display vendors.
  242. constexpr std::array<uint32_t, 6> BLOCKED_DISPLAY_VENDOR_IDS = {
  243.     0x1234, // QEMU/Bochs
  244.     0x15AD, // VMware
  245.     0x80EE, // VirtualBox
  246.     0x1B36, // Red Hat/QXL
  247.     0x1AF4, // Red Hat/VirtIO
  248.     0x1AE0  // Google virtual hardware
  249. };
  250.  
  251. CheckResult check_language_gate() {
  252.     // Export-hash resolution confirms these exact APIs:
  253.     // GetUserDefaultUILanguage, GetSystemDefaultUILanguage, GetKeyboardLayout.
  254.     const LANGID a = GetUserDefaultUILanguage();
  255.     const LANGID b = GetSystemDefaultUILanguage();
  256.     const LANGID c = LOWORD(reinterpret_cast<ULONG_PTR>(GetKeyboardLayout(0)));
  257.  
  258.     const uint32_t hash_a = hash_primary_language_id(a);
  259.     const uint32_t hash_b = hash_primary_language_id(b);
  260.     const uint32_t hash_c = hash_primary_language_id(c);
  261.     const bool blocked_a = hash_in_table(hash_a, LANGUAGE_HASHES);
  262.     const bool blocked_b = hash_in_table(hash_b, LANGUAGE_HASHES);
  263.     const bool blocked_c = hash_in_table(hash_c, LANGUAGE_HASHES);
  264.     const bool blocked = blocked_a || blocked_b || blocked_c;
  265.  
  266.     std::vector<std::string> hits;
  267.     if (blocked_a) {
  268.         hits.push_back(
  269.             "userUI='" + language_name(a) + "' (" + hex_value(a, 4) + ")");
  270.     }
  271.     if (blocked_b) {
  272.         hits.push_back(
  273.             "systemUI='" + language_name(b) + "' (" + hex_value(b, 4) + ")");
  274.     }
  275.     if (blocked_c) {
  276.         hits.push_back(
  277.             "keyboard='" + language_name(c) + "' (" + hex_value(c, 4) + ")");
  278.     }
  279.  
  280.     return make_check_result(
  281.         "language gate",
  282.         0,
  283.         {
  284.             {"user UI",
  285.              "'" + language_name(a) + "', LANGID=" + hex_value(a, 4) +
  286.                  ", hash=" + hex_value(hash_a, 8)},
  287.             {"system UI",
  288.              "'" + language_name(b) + "', LANGID=" + hex_value(b, 4) +
  289.                  ", hash=" + hex_value(hash_b, 8)},
  290.             {"keyboard",
  291.              "'" + language_name(c) + "', LANGID=" + hex_value(c, 4) +
  292.                  ", hash=" + hex_value(hash_c, 8)}
  293.         },
  294.         hits,
  295.         {
  296.             "Mask each LANGID to its 10-bit primary-language ID and hash it.",
  297.             "Any hash in the 10-entry language blocklist triggers immediate termination.",
  298.             "This is a direct gate outside the ten-entry scorer; it is not a score input."
  299.         },
  300.         blocked,
  301.         true);
  302. }
  303.  
  304. CheckResult check_process_environment() {
  305.     unsigned count = 0;
  306.     bool blacklisted = false;
  307.     bool snapshot_available = false;
  308.     std::vector<std::string> blacklist_hits;
  309.  
  310.     HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  311.     if (snapshot != INVALID_HANDLE_VALUE) {
  312.         snapshot_available = true;
  313.         PROCESSENTRY32W pe{};
  314.         pe.dwSize = sizeof(pe);
  315.         if (Process32FirstW(snapshot, &pe)) {
  316.             do {
  317.                 ++count;
  318.                 const uint32_t h = hash_lowercase_utf16le(pe.szExeFile);
  319.                 if (hash_in_table(h, PROCESS_HASHES)) {
  320.                     blacklisted = true;
  321.                     const std::string hit =
  322.                         "'" + wide_to_utf8(pe.szExeFile) +
  323.                         "' hash=" + hex_value(h, 8);
  324.                     if (std::find(
  325.                             blacklist_hits.begin(),
  326.                             blacklist_hits.end(),
  327.                             hit) == blacklist_hits.end()) {
  328.                         blacklist_hits.push_back(hit);
  329.                     }
  330.                 }
  331.             } while (Process32NextW(snapshot, &pe));
  332.         }
  333.         CloseHandle(snapshot);
  334.     }
  335.  
  336.     // A blacklist hit adds 99; it does not suppress the process-count score.
  337.     // Consequently a hit on a <=50-process host contributes 104, not 99.
  338.     int score = 0;
  339.     if (blacklisted) score += 99;
  340.     if (count <= 50) score += 5;
  341.     else if (count <= 75) score += 2;
  342.     else if (count <= 100) score += 1;
  343.  
  344.     return make_check_result(
  345.         "process environment",
  346.         score,
  347.         {
  348.             {"snapshot", snapshot_available ? "success" : "failed"},
  349.             {"process count", std::to_string(count)}
  350.         },
  351.         blacklist_hits,
  352.         {
  353.             "Any process-name hash in the 74-entry blocklist adds 99.",
  354.             "Then process count <=50 adds 5.",
  355.             "Process count 51..75 adds 2.",
  356.             "Process count 76..100 adds 1.",
  357.             "Process count >100 adds 0.",
  358.             "Snapshot failure leaves count at zero and therefore still adds 5."
  359.         });
  360. }
  361.  
  362. struct CpuTopology {
  363.     unsigned cores = 0;
  364.     unsigned logical = 0;
  365.     bool available = false;
  366. };
  367.  
  368. CpuTopology query_cpu_topology() {
  369.     CpuTopology out{};
  370.     // The sample uses the legacy fixed-size API with a 0x600-byte buffer.
  371.     std::array<SYSTEM_LOGICAL_PROCESSOR_INFORMATION,
  372.                0x600 / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION)> buffer{};
  373.     DWORD length = static_cast<DWORD>(sizeof(buffer));
  374.     if (!GetLogicalProcessorInformation(buffer.data(), &length)) return out;
  375.  
  376.     out.available = true;
  377.     const size_t count =
  378.         std::min<size_t>(buffer.size(), length / sizeof(buffer.front()));
  379.     for (size_t i = 0; i < count; ++i) {
  380.         const auto& info = buffer[i];
  381.         if (info.Relationship == RelationProcessorCore) {
  382.             ++out.cores;
  383.             out.logical +=
  384.                 static_cast<unsigned>(__popcnt64(info.ProcessorMask));
  385.         }
  386.     }
  387.     return out;
  388. }
  389.  
  390. CheckResult check_cpu_topology() {
  391.     const CpuTopology cpu = query_cpu_topology();
  392.  
  393.     if (!cpu.available) {
  394.         // FUN_140005470 adds zero if GetLogicalProcessorInformation fails.
  395.         return make_check_result(
  396.             "CPU topology",
  397.             0,
  398.             {{"API result", "GetLogicalProcessorInformation failed"}},
  399.             {},
  400.             {"Query failure adds 0."});
  401.     }
  402.  
  403.     // Exact branches at 0x1400057D7..0x140005805. The logical<=4 branch is
  404.     // reachable only when cores==2; systems with more than two cores score 0.
  405.     int score = 0;
  406.     if (cpu.cores < 2) score = 7;
  407.     else if (cpu.cores == 2 && cpu.logical <= 2) score = 3;
  408.     else if (cpu.cores == 2 && cpu.logical <= 4) score = 1;
  409.  
  410.     return make_check_result(
  411.         "CPU topology",
  412.         score,
  413.         {
  414.             {"physical cores", std::to_string(cpu.cores)},
  415.             {"logical processors", std::to_string(cpu.logical)}
  416.         },
  417.         {},
  418.         {
  419.             "Fewer than 2 cores adds 7.",
  420.             "Exactly 2 cores and <=2 logical processors adds 3.",
  421.             "Exactly 2 cores and 3..4 logical processors adds 1.",
  422.             "Every other topology adds 0."
  423.         });
  424. }
  425.  
  426. CheckResult check_physical_memory() {
  427.     MEMORYSTATUSEX ms{};
  428.     ms.dwLength = sizeof(ms);
  429.     if (!GlobalMemoryStatusEx(&ms)) {
  430.         return make_check_result(
  431.             "physical memory",
  432.             0,
  433.             {{"API result", "GlobalMemoryStatusEx failed"}},
  434.             {},
  435.             {"Query failure adds 0."});
  436.     }
  437.  
  438.     const uint64_t mib = ms.ullTotalPhys / (1024ull * 1024ull);
  439.     int score = 0;
  440.     if (mib <= 1536) score = 7;
  441.     else if (mib <= 3584) score = 2;
  442.     else if (mib <= 5632) score = 1;
  443.  
  444.     return make_check_result(
  445.         "physical memory",
  446.         score,
  447.         {{"total RAM", std::to_string(mib) + " MiB"}},
  448.         {},
  449.         {
  450.             "<=1536 MiB adds 7.",
  451.             "1537..3584 MiB adds 2.",
  452.             "3585..5632 MiB adds 1.",
  453.             ">5632 MiB adds 0."
  454.         });
  455. }
  456.  
  457. bool contains_recognized_display_token(const std::wstring& text);
  458. std::optional<uint32_t> parse_display_vendor_id(const std::wstring& device_id);
  459.  
  460. bool is_blocked_display_vendor(uint32_t vendor) {
  461.     return std::find(BLOCKED_DISPLAY_VENDOR_IDS.begin(),
  462.                      BLOCKED_DISPLAY_VENDOR_IDS.end(),
  463.                      vendor) != BLOCKED_DISPLAY_VENDOR_IDS.end();
  464. }
  465.  
  466. std::string describe_display_device(const DISPLAY_DEVICEW& device) {
  467.     const std::optional<uint32_t> vendor =
  468.         parse_display_vendor_id(device.DeviceID);
  469.     std::string description =
  470.         "'" + wide_to_utf8(device.DeviceString) +
  471.         "' [DeviceID='" + wide_to_utf8(device.DeviceID) + "'";
  472.     if (vendor) {
  473.         description +=
  474.             ", vendor=" + std::string(display_vendor_name(*vendor)) +
  475.             " (VEN_" + hex_value(*vendor, 4).substr(2) + ")";
  476.     } else {
  477.         description += ", vendor=not present";
  478.     }
  479.     description += "]";
  480.     return description;
  481. }
  482.  
  483. CheckResult check_primary_display() {
  484.     /*
  485.      * Index 3 searches overlapping substrings of DeviceString for one of the
  486.      * four physical-GPU tokens on attached desktop devices. A device is
  487.      * accepted only when its DeviceID does not contain one of the six blocked
  488.      * virtual VEN_ identifiers.
  489.      */
  490.     unsigned enumerated = 0;
  491.     unsigned attached = 0;
  492.     unsigned token_matches = 0;
  493.     std::vector<std::string> attached_devices;
  494.     std::vector<std::string> blocked_vendor_hits;
  495.  
  496.     DISPLAY_DEVICEW dd{};
  497.     dd.cb = sizeof(dd);
  498.     for (DWORD i = 0; EnumDisplayDevicesW(nullptr, i, &dd, 0); ++i) {
  499.         ++enumerated;
  500.         const bool is_attached =
  501.             (dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) != 0;
  502.         if (is_attached) {
  503.             ++attached;
  504.             attached_devices.push_back(describe_display_device(dd));
  505.         }
  506.         if (is_attached && contains_recognized_display_token(dd.DeviceString)) {
  507.             ++token_matches;
  508.             const std::optional<uint32_t> vendor =
  509.                 parse_display_vendor_id(dd.DeviceID);
  510.             if (!vendor || !is_blocked_display_vendor(*vendor)) {
  511.                 return make_check_result(
  512.                     "primary display validation",
  513.                     0,
  514.                     {
  515.                         {"enumerated devices", std::to_string(enumerated)},
  516.                         {"attached devices", std::to_string(attached)},
  517.                         {"recognized token matches", std::to_string(token_matches)},
  518.                         {"accepted device", describe_display_device(dd)}
  519.                     },
  520.                     {},
  521.                     {
  522.                         "Consider only devices attached to the desktop.",
  523.                         "DeviceString must contain nvidia, radeon, intel, or qualcomm.",
  524.                         "Its VEN value must be absent or outside the six-entry virtual-vendor blocklist.",
  525.                         "At least one accepted device adds 0; otherwise add 5."
  526.                     });
  527.             }
  528.             blocked_vendor_hits.push_back(describe_display_device(dd));
  529.         }
  530.  
  531.         dd = {};
  532.         dd.cb = sizeof(dd);
  533.     }
  534.  
  535.     std::vector<std::pair<std::string, std::string>> observed = {
  536.         {"enumerated devices", std::to_string(enumerated)},
  537.         {"attached devices", std::to_string(attached)},
  538.         {"recognized token matches", std::to_string(token_matches)}
  539.     };
  540.     for (const auto& device : attached_devices) {
  541.         observed.push_back({"attached device", device});
  542.     }
  543.  
  544.     return make_check_result(
  545.         "primary display validation",
  546.         5,
  547.         std::move(observed),
  548.         blocked_vendor_hits,
  549.         {
  550.             "Consider only devices attached to the desktop.",
  551.             "DeviceString must contain nvidia, radeon, intel, or qualcomm.",
  552.             "Its VEN value must be absent or outside the six-entry virtual-vendor blocklist.",
  553.             "No acceptable token/vendor pair adds 5."
  554.         });
  555. }
  556.  
  557. bool contains_recognized_display_token(const std::wstring& text) {
  558.     const std::wstring lower = lowercase(text);
  559.  
  560.     // The sample hashes overlapping UTF-16 substrings of lengths 5..15.
  561.     for (size_t start = 0; start < lower.size(); ++start) {
  562.         const size_t max_length = std::min<size_t>(15, lower.size() - start);
  563.         for (size_t length = 5; length <= max_length; ++length) {
  564.             if (hash_in_table(
  565.                     hash_lowercase_utf16le(lower.substr(start, length)),
  566.                     DISPLAY_TOKEN_HASHES)) {
  567.                 return true;
  568.             }
  569.         }
  570.     }
  571.     return false;
  572. }
  573.  
  574. std::optional<uint32_t> parse_display_vendor_id(const std::wstring& device_id) {
  575.     const std::wstring folded = lowercase(device_id);
  576.     const size_t marker = folded.find(L"ven_");
  577.     if (marker == std::wstring::npos) return std::nullopt;
  578.  
  579.     // FUN_14002AE88 accumulates every valid hex digit after VEN_ until '&'
  580.     // or NUL. Unexpected characters are skipped rather than ending parsing.
  581.     uint32_t value = 0;
  582.     size_t digits = 0;
  583.     for (size_t i = marker + 4;
  584.          i < device_id.size() && device_id[i] != L'&';
  585.          ++i) {
  586.         const wchar_t ch = device_id[i];
  587.         unsigned nibble = 0;
  588.         if (ch >= L'0' && ch <= L'9') nibble = static_cast<unsigned>(ch - L'0');
  589.         else if (ch >= L'A' && ch <= L'F') nibble = 10u + static_cast<unsigned>(ch - L'A');
  590.         else if (ch >= L'a' && ch <= L'f') nibble = 10u + static_cast<unsigned>(ch - L'a');
  591.         else continue;
  592.  
  593.         value = (value << 4) | nibble;
  594.         ++digits;
  595.     }
  596.  
  597.     if (digits == 0) return std::nullopt;
  598.     return value;
  599. }
  600.  
  601. CheckResult check_additional_display_vendor() {
  602.     unsigned enumerated = 0;
  603.     unsigned attached = 0;
  604.     std::vector<std::string> attached_devices;
  605.  
  606.     // Index 9 is independent of the token table: any attached desktop
  607.     // DeviceID whose VEN_ value is in the virtual-vendor set contributes 5.
  608.     DISPLAY_DEVICEW dd{};
  609.     dd.cb = sizeof(dd);
  610.     for (DWORD i = 0; EnumDisplayDevicesW(nullptr, i, &dd, 0); ++i) {
  611.         ++enumerated;
  612.         const bool is_attached =
  613.             (dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) != 0;
  614.         if (is_attached) {
  615.             ++attached;
  616.             attached_devices.push_back(describe_display_device(dd));
  617.         }
  618.  
  619.         const std::optional<uint32_t> vendor =
  620.             parse_display_vendor_id(dd.DeviceID);
  621.         if (is_attached && vendor && is_blocked_display_vendor(*vendor)) {
  622.             return make_check_result(
  623.                 "additional display vendor scan",
  624.                 5,
  625.                 {
  626.                     {"enumerated devices", std::to_string(enumerated)},
  627.                     {"attached devices", std::to_string(attached)}
  628.                 },
  629.                 {describe_display_device(dd)},
  630.                 {
  631.                     "Consider only devices attached to the desktop.",
  632.                     "Any VEN value in the six-entry virtual-vendor blocklist adds 5.",
  633.                     "No blocked VEN match adds 0."
  634.                 });
  635.         }
  636.  
  637.         dd = {};
  638.         dd.cb = sizeof(dd);
  639.     }
  640.  
  641.     std::vector<std::pair<std::string, std::string>> observed = {
  642.         {"enumerated devices", std::to_string(enumerated)},
  643.         {"attached devices", std::to_string(attached)}
  644.     };
  645.     for (const auto& device : attached_devices) {
  646.         observed.push_back({"attached device", device});
  647.     }
  648.  
  649.     return make_check_result(
  650.         "additional display vendor scan",
  651.         0,
  652.         std::move(observed),
  653.         {},
  654.         {
  655.             "Consider only devices attached to the desktop.",
  656.             "Any VEN value in the six-entry virtual-vendor blocklist adds 5.",
  657.             "No blocked VEN match adds 0."
  658.         });
  659. }
  660.  
  661. CheckResult check_computer_name_and_username_gate() {
  662.     /*
  663.      * The entry suite applies the same 18-hash table first to
  664.      * GetComputerNameW and then to GetUserNameW. The sample uses a 256-WCHAR
  665.      * computer-name buffer and a 16-WCHAR username buffer. Failure of either
  666.      * query is non-fatal; any successful hash match terminates immediately.
  667.      */
  668.     bool computer_name_match = false;
  669.     bool username_match = false;
  670.     bool computer_name_available = false;
  671.     bool username_available = false;
  672.     uint32_t computer_name_hash = 0;
  673.     uint32_t username_hash = 0;
  674.  
  675.     wchar_t computer_name[256]{};
  676.     DWORD computer_name_size = static_cast<DWORD>(std::size(computer_name));
  677.     if (GetComputerNameW(computer_name, &computer_name_size)) {
  678.         computer_name_available = true;
  679.         computer_name_hash = hash_lowercase_utf16le(computer_name);
  680.         computer_name_match =
  681.             hash_in_table(computer_name_hash, HOST_IDENTITY_HASHES);
  682.     }
  683.  
  684.     wchar_t username[16]{};
  685.     DWORD username_size = static_cast<DWORD>(std::size(username));
  686.     if (GetUserNameW(username, &username_size)) {
  687.         username_available = true;
  688.         username_hash = hash_lowercase_utf16le(username);
  689.         username_match = hash_in_table(username_hash, HOST_IDENTITY_HASHES);
  690.     }
  691.  
  692.     const bool blocked = computer_name_match || username_match;
  693.     std::vector<std::string> identity_hits;
  694.     if (computer_name_match) {
  695.         identity_hits.push_back(
  696.             "computer-name='" + wide_to_utf8(computer_name) +
  697.             "' hash=" + hex_value(computer_name_hash, 8));
  698.     }
  699.     if (username_match) {
  700.         identity_hits.push_back(
  701.             "username='" + wide_to_utf8(username) +
  702.             "' hash=" + hex_value(username_hash, 8));
  703.     }
  704.  
  705.     return make_check_result(
  706.         "computer-name / username gate",
  707.         0,
  708.         {
  709.             {"computer name",
  710.              "'" +
  711.                  (computer_name_available
  712.                       ? wide_to_utf8(computer_name)
  713.                       : "unavailable") +
  714.                  "', hash=" +
  715.                  (computer_name_available
  716.                       ? hex_value(computer_name_hash, 8)
  717.                       : "n/a")},
  718.             {"username",
  719.              "'" +
  720.                  (username_available
  721.                       ? wide_to_utf8(username)
  722.                       : "unavailable") +
  723.                  "', hash=" +
  724.                  (username_available ? hex_value(username_hash, 8) : "n/a")}
  725.         },
  726.         identity_hits,
  727.         {
  728.             "Hash computer name and username with the shared 18-entry table.",
  729.             "Either hash matching takes the delayed-termination branch immediately.",
  730.             "Dispatcher index 4 is a direct gate inside the suite; it is not a score input."
  731.         },
  732.         blocked,
  733.         true);
  734. }
  735.  
  736. CheckResult check_uptime() {
  737.     const uint64_t ms = GetTickCount64();
  738.     int score = 0;
  739.     if (ms <= 120000) score = 3;
  740.     else if (ms <= 600000) score = 1;
  741.     return make_check_result(
  742.         "system uptime",
  743.         score,
  744.         {{"current uptime", std::to_string(ms) + " ms"}},
  745.         {},
  746.         {
  747.             "<=120000 ms adds 3.",
  748.             "120001..600000 ms adds 1.",
  749.             ">600000 ms adds 0."
  750.         });
  751. }
  752.  
  753. CheckResult check_sleep_acceleration() {
  754.     const uint64_t before = GetTickCount64();
  755.     Sleep(1000);
  756.     const uint64_t observed = GetTickCount64() - before;
  757.     const int score = observed < 400 ? 7 : 0;
  758.     return make_check_result(
  759.         "sleep acceleration",
  760.         score,
  761.         {
  762.             {"requested sleep", "1000 ms"},
  763.             {"observed elapsed", std::to_string(observed) + " ms"}
  764.         },
  765.         {},
  766.         {
  767.             "Observed elapsed time <400 ms adds 7.",
  768.             "Observed elapsed time >=400 ms adds 0."
  769.         });
  770. }
  771.  
  772. struct OsVersion {
  773.     DWORD major = 0;
  774.     DWORD minor = 0;
  775.     bool available = false;
  776. };
  777.  
  778. OsVersion query_os_version() {
  779.     using RtlGetVersionFn = LONG(WINAPI*)(PRTL_OSVERSIONINFOW);
  780.     OsVersion out{};
  781.     HMODULE ntdll = GetModuleHandleW(L"ntdll.dll");
  782.     auto fn = reinterpret_cast<RtlGetVersionFn>(GetProcAddress(ntdll, "RtlGetVersion"));
  783.     if (!fn) return out;
  784.     RTL_OSVERSIONINFOW vi{};
  785.     vi.dwOSVersionInfoSize = sizeof(vi);
  786.     if (fn(&vi) == 0) {
  787.         out.major = vi.dwMajorVersion;
  788.         out.minor = vi.dwMinorVersion;
  789.         out.available = true;
  790.     }
  791.     return out;
  792. }
  793.  
  794. CheckResult check_h264_hardware_encoder() {
  795.     const OsVersion os = query_os_version();
  796.     const std::string os_detail = os.available
  797.         ? std::to_string(os.major) + "." + std::to_string(os.minor)
  798.         : "unavailable";
  799.     const std::vector<std::string> h264_logic = {
  800.         "Windows 6.0..6.3 bypasses the Media Foundation probe and adds 0.",
  801.         "Module, API, COM, or MFStartup initialization failure adds 0.",
  802.         "MFTEnumEx failure or zero hardware H.264 encoders adds 5.",
  803.         "One or more hardware H.264 encoders adds 0."
  804.     };
  805.     if (os.major == 6 && os.minor <= 3) {
  806.         return make_check_result(
  807.             "hardware H.264 encoder",
  808.             0,
  809.             {
  810.                 {"OS version", os_detail},
  811.                 {"probe status", "bypassed for Windows 6.0..6.3"}
  812.             },
  813.             {},
  814.             h264_logic);
  815.     }
  816.  
  817.     using CoInitializeExFn = HRESULT(WINAPI*)(LPVOID, DWORD);
  818.     using CoUninitializeFn = void(WINAPI*)();
  819.     using MFStartupFn = HRESULT(WINAPI*)(ULONG, DWORD);
  820.     using MFShutdownFn = HRESULT(WINAPI*)();
  821.     using MFTEnumExFn = HRESULT(WINAPI*)(
  822.         GUID, UINT32, const MFT_REGISTER_TYPE_INFO*,
  823.         const MFT_REGISTER_TYPE_INFO*, IMFActivate***, UINT32*);
  824.  
  825.     /*
  826.      * The original resolves these APIs after loading combase.dll/mfplat.dll.
  827.      * Missing modules or any of the four initialization APIs bypass the probe
  828.      * with score 0; failure to resolve MFTEnumEx is an enumeration failure and
  829.      * therefore contributes 5.
  830.      */
  831.     HMODULE combase = LoadLibraryW(L"combase.dll");
  832.     HMODULE mfplat = LoadLibraryW(L"mfplat.dll");
  833.     const auto release_modules = [&]() {
  834.         if (mfplat) FreeLibrary(mfplat);
  835.         if (combase) FreeLibrary(combase);
  836.     };
  837.  
  838.     if (!combase || !mfplat) {
  839.         release_modules();
  840.         return make_check_result(
  841.             "hardware H.264 encoder",
  842.             0,
  843.             {
  844.                 {"OS version", os_detail},
  845.                 {"combase.dll", combase ? "available" : "unavailable"},
  846.                 {"mfplat.dll", mfplat ? "available" : "unavailable"}
  847.             },
  848.             {},
  849.             h264_logic);
  850.     }
  851.  
  852.     const auto co_initialize = reinterpret_cast<CoInitializeExFn>(
  853.         GetProcAddress(combase, "CoInitializeEx"));
  854.     const auto co_uninitialize = reinterpret_cast<CoUninitializeFn>(
  855.         GetProcAddress(combase, "CoUninitialize"));
  856.     const auto mf_startup = reinterpret_cast<MFStartupFn>(
  857.         GetProcAddress(mfplat, "MFStartup"));
  858.     const auto mf_shutdown = reinterpret_cast<MFShutdownFn>(
  859.         GetProcAddress(mfplat, "MFShutdown"));
  860.     const auto mft_enum = reinterpret_cast<MFTEnumExFn>(
  861.         GetProcAddress(mfplat, "MFTEnumEx"));
  862.  
  863.     if (!co_initialize || !co_uninitialize || !mf_startup || !mf_shutdown) {
  864.         release_modules();
  865.         return make_check_result(
  866.             "hardware H.264 encoder",
  867.             0,
  868.             {
  869.                 {"OS version", os_detail},
  870.                 {"CoInitializeEx", co_initialize ? "resolved" : "missing"},
  871.                 {"CoUninitialize", co_uninitialize ? "resolved" : "missing"},
  872.                 {"MFStartup", mf_startup ? "resolved" : "missing"},
  873.                 {"MFShutdown", mf_shutdown ? "resolved" : "missing"}
  874.             },
  875.             {},
  876.             h264_logic);
  877.     }
  878.  
  879.     /*
  880.      * The sample first requests MTA (flags 0), retries STA (flags 2) after a
  881.      * normal failure, and accepts RPC_E_CHANGED_MODE as usable.
  882.      */
  883.     HRESULT hr_co = co_initialize(nullptr, COINIT_MULTITHREADED);
  884.     if (FAILED(hr_co) && hr_co != RPC_E_CHANGED_MODE) {
  885.         hr_co = co_initialize(nullptr, COINIT_APARTMENTTHREADED);
  886.     }
  887.  
  888.     if (FAILED(hr_co) && hr_co != RPC_E_CHANGED_MODE) {
  889.         release_modules();
  890.         return make_check_result(
  891.             "hardware H.264 encoder",
  892.             0,
  893.             {
  894.                 {"OS version", os_detail},
  895.                 {"CoInitializeEx HRESULT",
  896.                  hex_value(static_cast<uint32_t>(hr_co), 8)},
  897.                 {"COM status", "initialization failed"}
  898.             },
  899.             {},
  900.             h264_logic);
  901.     }
  902.  
  903.     // The sample calls MFStartup(0x00020070, 0), i.e. MFSTARTUP_FULL.
  904.     HRESULT hr_mf = mf_startup(MF_VERSION_USED_BY_SAMPLE, MFSTARTUP_FULL);
  905.     if (FAILED(hr_mf)) {
  906.         // It balances CoInitializeEx only when the returned HRESULT was S_OK.
  907.         if (hr_co == S_OK) co_uninitialize();
  908.         release_modules();
  909.         return make_check_result(
  910.             "hardware H.264 encoder",
  911.             0,
  912.             {
  913.                 {"OS version", os_detail},
  914.                 {"CoInitializeEx HRESULT",
  915.                  hex_value(static_cast<uint32_t>(hr_co), 8)},
  916.                 {"MFStartup HRESULT",
  917.                  hex_value(static_cast<uint32_t>(hr_mf), 8)},
  918.                 {"Media Foundation status", "startup failed"}
  919.             },
  920.             {},
  921.             h264_logic);
  922.     }
  923.  
  924.     MFT_REGISTER_TYPE_INFO output{};
  925.     output.guidMajorType = MFMediaType_Video;
  926.     output.guidSubtype = MFVideoFormat_H264;
  927.  
  928.     IMFActivate** activates = nullptr;
  929.     UINT32 count = 0;
  930.     HRESULT hr = E_NOINTERFACE;
  931.     if (mft_enum) {
  932.         hr = mft_enum(
  933.             MFT_CATEGORY_VIDEO_ENCODER,
  934.             MFT_ENUM_FLAG_HARDWARE,
  935.             nullptr,
  936.             &output,
  937.             &activates,
  938.             &count);
  939.     }
  940.  
  941.     if (activates != nullptr) {
  942.         for (UINT32 i = 0; i < count; ++i) activates[i]->Release();
  943.         CoTaskMemFree(activates);
  944.     }
  945.  
  946.     mf_shutdown();
  947.     if (hr_co == S_OK) co_uninitialize();
  948.     release_modules();
  949.  
  950.     const int score = (FAILED(hr) || count == 0) ? 5 : 0;
  951.     return make_check_result(
  952.         "hardware H.264 encoder",
  953.         score,
  954.         {
  955.             {"OS version", os_detail},
  956.             {"MFTEnumEx HRESULT", hex_value(static_cast<uint32_t>(hr), 8)},
  957.             {"enumeration status", FAILED(hr) ? "failed" : "success"},
  958.             {"hardware H.264 encoders", std::to_string(count)}
  959.         },
  960.         {},
  961.         h264_logic);
  962. }
  963.  
  964. std::string cpuid_vendor() {
  965.     int regs[4]{};
  966.     __cpuid(regs, 0);
  967.     char vendor[13]{};
  968.     std::memcpy(vendor + 0, &regs[1], 4); // EBX
  969.     std::memcpy(vendor + 4, &regs[3], 4); // EDX
  970.     std::memcpy(vendor + 8, &regs[2], 4); // ECX
  971.     return vendor;
  972. }
  973.  
  974. std::string cpuid_brand() {
  975.     int regs[4]{};
  976.     __cpuid(regs, 0x80000000);
  977.     const unsigned max_leaf = static_cast<unsigned>(regs[0]);
  978.     if (max_leaf < 0x80000004) return {};
  979.  
  980.     char brand[49]{};
  981.     for (unsigned leaf = 0x80000002, off = 0; leaf <= 0x80000004; ++leaf, off += 16) {
  982.         __cpuid(regs, static_cast<int>(leaf));
  983.         std::memcpy(brand + off, regs, 16);
  984.     }
  985.     return brand;
  986. }
  987.  
  988. bool contains_with_folded_first(const std::string& text,
  989.                                 const std::string& token) {
  990.     if (token.empty()) return true;
  991.     if (token.size() > text.size()) return false;
  992.  
  993.     // FUN_140025010 ASCII-folds the candidate's first byte at each position;
  994.     // the remaining bytes are compared directly with "intel", "amd", "aMD".
  995.     for (size_t start = 0; start + token.size() <= text.size(); ++start) {
  996.         unsigned char first = static_cast<unsigned char>(text[start]);
  997.         if (first >= 'A' && first <= 'Z') first += 0x20;
  998.         if (first != static_cast<unsigned char>(token[0])) continue;
  999.         if (text.compare(start + 1, token.size() - 1, token, 1) == 0) {
  1000.             return true;
  1001.         }
  1002.     }
  1003.     return false;
  1004. }
  1005.  
  1006. CheckResult check_cpuid_consistency() {
  1007.     const std::string vendor = cpuid_vendor();
  1008.     const std::string brand = cpuid_brand();
  1009.  
  1010.     // The binary compares only CPUID.0:EBX: "Genu" or "Auth".
  1011.     const bool vendor_intel = vendor.rfind("Genu", 0) == 0;
  1012.     const bool vendor_amd = vendor.rfind("Auth", 0) == 0;
  1013.     const bool brand_intel = contains_with_folded_first(brand, "intel");
  1014.     const bool brand_amd =
  1015.         contains_with_folded_first(brand, "amd") ||
  1016.         contains_with_folded_first(brand, "aMD");
  1017.  
  1018.     // A brand containing both families is not scored by the original branch.
  1019.     const bool mismatch =
  1020.         (vendor_amd && brand_intel && !brand_amd) ||
  1021.         (vendor_intel && brand_amd && !brand_intel);
  1022.     return make_check_result(
  1023.         "CPUID consistency",
  1024.         mismatch ? 5 : 0,
  1025.         {
  1026.             {"CPUID vendor", "'" + vendor + "'"},
  1027.             {"CPUID brand", "'" + brand + "'"},
  1028.             {"vendor family",
  1029.              vendor_intel ? "Intel" : (vendor_amd ? "AMD" : "other")},
  1030.             {"Intel token in brand", brand_intel ? "yes" : "no"},
  1031.             {"AMD token in brand", brand_amd ? "yes" : "no"}
  1032.         },
  1033.         {},
  1034.         {
  1035.             "AMD vendor plus Intel-only brand adds 5.",
  1036.             "Intel vendor plus AMD-only brand adds 5.",
  1037.             "Every other combination adds 0."
  1038.         });
  1039. }
  1040.  
  1041. void print_wrapped_field(const std::string& label,
  1042.                          const std::string& value) {
  1043.     constexpr size_t LINE_WIDTH = 100;
  1044.     constexpr size_t LABEL_WIDTH = 15;
  1045.  
  1046.     const std::string prefix =
  1047.         label + std::string(LABEL_WIDTH - label.size(), ' ') + ": ";
  1048.     const std::string continuation(prefix.size(), ' ');
  1049.  
  1050.     std::istringstream explicit_lines(value);
  1051.     std::string explicit_line;
  1052.     bool first_output_line = true;
  1053.     while (std::getline(explicit_lines, explicit_line)) {
  1054.         const size_t leading_spaces =
  1055.             explicit_line.find_first_not_of(' ') == std::string::npos
  1056.                 ? explicit_line.size()
  1057.                 : explicit_line.find_first_not_of(' ');
  1058.         const std::string indent(leading_spaces, ' ');
  1059.         const std::string content = explicit_line.substr(leading_spaces);
  1060.         std::string wrap_indent = indent;
  1061.         if (content.rfind("- ", 0) == 0) {
  1062.             wrap_indent += "  ";
  1063.         } else {
  1064.             const size_t value_separator = content.find(": ");
  1065.             if (value_separator != std::string::npos) {
  1066.                 wrap_indent += std::string(value_separator + 2, ' ');
  1067.             }
  1068.         }
  1069.         std::istringstream words(content);
  1070.         std::string word;
  1071.         bool wrote_word = false;
  1072.         size_t column = continuation.size() + indent.size();
  1073.  
  1074.         std::cout << (first_output_line ? prefix : continuation) << indent;
  1075.         first_output_line = false;
  1076.  
  1077.         while (words >> word) {
  1078.             const size_t separator = wrote_word ? 1 : 0;
  1079.             if (wrote_word &&
  1080.                 column + separator + word.size() > LINE_WIDTH) {
  1081.                 std::cout << "\n" << continuation << wrap_indent;
  1082.                 column = continuation.size() + wrap_indent.size();
  1083.                 wrote_word = false;
  1084.             }
  1085.             if (wrote_word) {
  1086.                 std::cout << ' ';
  1087.                 ++column;
  1088.             }
  1089.             std::cout << word;
  1090.             column += word.size();
  1091.             wrote_word = true;
  1092.         }
  1093.         std::cout << "\n";
  1094.     }
  1095. }
  1096.  
  1097. void print_check_block(const CheckResult& result,
  1098.                        const std::string& check_id,
  1099.                        std::optional<DWORD> pre_delay_ms,
  1100.                        uint32_t cumulative_score) {
  1101.     std::cout << "\n------------------------------------------------------------\n";
  1102.     print_wrapped_field("CHECK ID", check_id);
  1103.     print_wrapped_field("NAME", result.name);
  1104.     print_wrapped_field(
  1105.         "CLASSIFICATION",
  1106.         result.is_direct_gate
  1107.             ? "DIRECT TERMINATION GATE (not scored)"
  1108.             : "SCORED ENVIRONMENT CHECK");
  1109.     print_wrapped_field("OBSERVED", format_fields(result.observed));
  1110.     print_wrapped_field("MATCHES", format_items(result.matches));
  1111.     print_wrapped_field("LOGIC", format_items(result.logic));
  1112.     std::cout << "CONTRIBUTION   : ";
  1113.     if (result.is_direct_gate) {
  1114.         std::cout << "n/a (direct gate; score unchanged)\n";
  1115.     } else {
  1116.         std::cout << (result.score >= 0 ? "+" : "") << result.score << "\n";
  1117.     }
  1118.     std::cout << "CUMULATIVE     : " << cumulative_score;
  1119.     if (result.is_direct_gate) std::cout << " (unchanged)";
  1120.     std::cout << "\n";
  1121.     std::cout << "PRE-CHECK DELAY: ";
  1122.     if (pre_delay_ms) std::cout << *pre_delay_ms << " ms\n";
  1123.     else std::cout << "n/a\n";
  1124.     std::cout << "DIRECT GATE    : ";
  1125.     if (result.is_direct_gate) {
  1126.         std::cout << (result.immediate_gate_match ? "MATCH" : "no match");
  1127.     } else {
  1128.         std::cout << "n/a";
  1129.     }
  1130.     std::cout << "\n";
  1131. }
  1132.  
  1133. } // namespace evasion_poc
  1134.  
  1135. int wmain() {
  1136.     using namespace evasion_poc;
  1137.  
  1138.     const CheckResult language = check_language_gate();
  1139.     print_check_block(language, "LANG", std::nullopt, 0);
  1140.  
  1141.     /*
  1142.      * The physical-GPU predicate is computed before the ten-entry dispatcher
  1143.      * in FUN_140005470, although its result is consumed by check index 3.
  1144.      */
  1145.     const CheckResult primary_display = check_primary_display();
  1146.     bool identity_gate_match = false;
  1147.     uint32_t encoded_score = SCORE_XOR_KEY; // encoded representation of zero
  1148.  
  1149.     for (size_t slot = 0; slot < CHECK_DISPATCH_ORDER.size(); ++slot) {
  1150.         // These short sleeps are benign and reproduce the table-driven suite
  1151.         // cadence. Check 6 performs its separate measured 1000 ms sleep.
  1152.         Sleep(PRE_CHECK_DELAYS_MS[slot]);
  1153.  
  1154.         const unsigned index = CHECK_DISPATCH_ORDER[slot];
  1155.         CheckResult result;
  1156.         switch (index) {
  1157.             case 0: result = check_process_environment(); break;
  1158.             case 1: result = check_cpu_topology(); break;
  1159.             case 2: result = check_physical_memory(); break;
  1160.             case 3: result = primary_display; break;
  1161.             case 4:
  1162.                 result = check_computer_name_and_username_gate();
  1163.                 identity_gate_match = result.immediate_gate_match;
  1164.                 break;
  1165.             case 5: result = check_uptime(); break;
  1166.             case 6: result = check_sleep_acceleration(); break;
  1167.             case 7: result = check_h264_hardware_encoder(); break;
  1168.             case 8: result = check_cpuid_consistency(); break;
  1169.             case 9: result = check_additional_display_vendor(); break;
  1170.             default:
  1171.                 result = make_check_result(
  1172.                     "invalid dispatcher index",
  1173.                     0,
  1174.                     {{"index", std::to_string(index)}},
  1175.                     {},
  1176.                     {"Unknown dispatcher index; adds 0."});
  1177.                 break;
  1178.         }
  1179.  
  1180.         // Direct gates do not read or update the encoded score. Scored checks
  1181.         // update the global after their contribution.
  1182.         uint32_t plain_score = encoded_score ^ SCORE_XOR_KEY;
  1183.         if (!result.is_direct_gate) {
  1184.             plain_score += static_cast<uint32_t>(result.score);
  1185.             encoded_score = plain_score ^ SCORE_XOR_KEY;
  1186.         }
  1187.         print_check_block(
  1188.             result,
  1189.             std::to_string(index),
  1190.             PRE_CHECK_DELAYS_MS[slot],
  1191.             plain_score);
  1192.     }
  1193.  
  1194.     /*
  1195.      * This diagnostic intentionally completes every observation so it can
  1196.      * print a score. The original sample would stop immediately at either
  1197.      * direct gate and would not execute the later checks.
  1198.      */
  1199.     const uint32_t total = encoded_score ^ SCORE_XOR_KEY;
  1200.     std::string decision;
  1201.     if (language.immediate_gate_match) {
  1202.         decision = "WOULD TERMINATE at language gate";
  1203.     } else if (identity_gate_match) {
  1204.         decision = "WOULD TERMINATE at computer-name/username gate";
  1205.     } else if (total >= TERMINATION_THRESHOLD) {
  1206.         decision = "WOULD TERMINATE because score >= threshold";
  1207.     } else {
  1208.         decision = "WOULD ALLOW execution to continue";
  1209.     }
  1210.  
  1211.     std::cout << "\n============================================================\n";
  1212.     std::cout << "SUMMARY\n";
  1213.     print_wrapped_field("TOTAL SCORE", std::to_string(total));
  1214.     print_wrapped_field("THRESHOLD", std::to_string(TERMINATION_THRESHOLD));
  1215.     print_wrapped_field("DECISION", decision);
  1216.  
  1217.     return 0;
  1218. }
  1219.  
Advertisement
Add Comment
Please, Sign In to add comment