-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathvmaware_MIT.hpp
More file actions
11716 lines (10094 loc) · 398 KB
/
vmaware_MIT.hpp
File metadata and controls
11716 lines (10094 loc) · 398 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* ██╗ ██╗███╗ ███╗ █████╗ ██╗ ██╗ █████╗ ██████╗ ███████╗
* ██║ ██║████╗ ████║██╔══██╗██║ ██║██╔══██╗██╔══██╗██╔════╝
* ██║ ██║██╔████╔██║███████║██║ █╗ ██║███████║██████╔╝█████╗
* ╚██╗ ██╔╝██║╚██╔╝██║██╔══██║██║███╗██║██╔══██║██╔══██╗██╔══╝
* ╚████╔╝ ██║ ╚═╝ ██║██║ ██║╚███╔███╔╝██║ ██║██║ ██║███████╗
* ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚══╝╚══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ 2.1.1 (April 2025)
*
* C++ VM detection library
*
* - Made by: kernelwernel (https://github.com/kernelwernel)
* - Co-maintained by: Requiem (https://github.com/NotRequiem)
* - Contributed by:
* - Alex (https://github.com/greenozon)
* - Marek Knápek (https://github.com/MarekKnapek)
* - Vladyslav Miachkov (https://github.com/fameowner99)
* - Alan Tse (https://github.com/alandtse)
* - Georgii Gennadev (https://github.com/D00Movenok)
* - utoshu (https://github.com/utoshu)
* - Jyd (https://github.com/jyd519)
* - Repository: https://github.com/kernelwernel/VMAware
* - Docs: https://github.com/kernelwernel/VMAware/docs/documentation.md
* - Full credits: https://github.com/kernelwernel/VMAware#credits-and-contributors-%EF%B8%8F
* - License: MIT
*
* MIT License
*
* Copyright (c) 2024 kernelwernel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
* ============================== SECTIONS ==================================
* - enums for publicly accessible techniques => line 573
* - struct for internal cpu operations => line 759
* - struct for internal memoization => line 1214
* - struct for internal utility functions => line 1340
* - struct for internal core components => line 9869
* - start of VM detection technique list => line 2547
* - start of public VM detection functions => line 10544
* - start of externally defined variables => line 11469
*
*
* ============================== EXAMPLE ===================================
* #include "vmaware.hpp"
* #include <iostream>
*
* int main() {
* if (VM::detect()) {
* std::cout << "Virtual machine detected!" << "\n";
* } else {
* std::cout << "Running on baremetal" << "\n";
* }
*
* std::cout << "VM name: " << VM::brand() << "\n";
* std::cout << "VM type: " << VM::type() << "\n";
* std::cout << "VM certainty: " << (int)VM::percentage() << "%" << "\n";
* }
*
*
* ========================== CODE DOCUMENTATION =============================
*
* Welcome! This is just a preliminary text to lay the context of how it works,
* how it's structured, and guide anybody who's trying to understand the whole code.
* Reading over 12k+ lines of other people's C++ code is obviously not an easy task,
* and that's perfectly understandable. I'd struggle as well if I was in your position
* while not even knowing where to start. So here's a more human-friendly explanation:
*
*
* Firstly, the lib is completely static, meaning that there's no need for struct
* constructors to be initiated (unless you're using the VM::vmaware struct).
* The main focus of the lib are the tables:
* - the TECHNIQUE table stores all the VM detection technique information in a std::map
*
* - the BRAND table stores every VM brand as a std::map as well, but as a scoreboard.
* This mean that if a VM detection has detected a VM brand, that brand will have an
* incremented score. After every technique is run, the brand with the highest score
* is chosen as the officially detected brand.
*
* The techniques are all static functions, which all return a boolean. There are a few
* categories of techniques that target vastly different things such as OS queires, CPU
* values, other hardware values, firmware data, and system files just to name a few.
*
*
* Secondly, there are multiple modules in the lib that are combined to integrate with
* the functionalities needed:
* - core module:
* This contains many important components such as the aforementioned tables,
* the standard structure for how VM techniques are organised, functionalities
* to run all the techniques in the technique table, functionalities to run
* custom-made techniques by the user, and an argument handler based on the
* arguments inputted by the user.
*
* - cpu module:
* As the name suggests, this contains functionalities for the CPU. There are
* many techniques that utilise some kind of low-level CPU interaction, so
* this module was added to further standardise it.
*
* - memo module:
* This contains functionalities for memoizing technique results (not to be
* confused with "memorization"). More specifically, this allows us to cache
* a technique result in a table where each entry contains a technique and its
* result. This allows us to avoid re-running techniques which happens a lot
* internally. Some techniques are more costlier than others in terms of
* performance, so this is a crucial module that allows us to save a lot of
* time. Additionally, it contains other memoization caches for various other
* things for convenience purposes.
*
* - util module:
* This contains many utility functionalities to be used by the techniques.
* Examples of functionalities include file I/O, registries, permission
* checks, system commands, HDD sizes, RAM sizes, debugs, process checking,
* OS queries, Hyper-X, and so on. (It should be mentioned that this is
* probably the least enjoyable part of the lib to read, since it's really messy)
*
*
* Thirdly, I'll explain in this section how all of these facets of the lib interact with
* each other. Let's take an example with VM::detect(), where it returns a boolean true or
* false if a VM has been detected or not. The chain of steps it takes goes like this:
* 1. The function tries to handle the user arguments (if there's
* any), and generates a std::bitset. This bitset has a length of
* every VM detection technique + settings, where each bit
* corresponds to whether this technique will be ran or not,
* and which settings were selected.
*
* 2. After the bitset has been generated, this information is then
* passed to the core module of the lib. It analyses the bitset,
* and runs every VM detection technique that has been selected,
* while ignoring the ones that weren't selected (by default most
* of them are already selected anyway). The function that does
* this mechanism is core::run_all()
*
* 3. While the core::run_all() function is being ran, it checks if
* each technique has already been memoized or not. If it has,
* retrieve the result from the cache and move to the next technique.
* If it hasn't, run the technique and cache the result to the
* cache table.
*
* 4. After every technique has been looped through, this generates a
* uint16_t score. Every technique has a score value between 0 to
* 100, and if they are detected then this score is accumulated to
* a total score. If the total is above 150, that means it's a VM[1].
*
*
* There are other functions such as VM::brand(), which returns a std::string of the most
* likely brand that your system is running on. It has a bit of a different mechanism:
* 1. Same as step 1 in VM::detect()
*
* 2. Check if the majority of techniques have been run already and stored
* in the cache. If not, invoke core::run_all(). The reason why this is
* important is because a lot of techniques increment a point for its
* respected brand that was detected. For example, if the VM::QEMU_USB
* technique has detected a VM, it'll add a score to the QEMU brand in
* the scoreboard. If no technique has been ran, then there's no way to
* populate the scoreboard with any points. After every VM detection
* technique has been invoked/retrieved, the brand scoreboard is now
* ready to be analysed.
*
* 3. Create a filter for the scoreboard, where every brand that has a score
* of 0 are erased for abstraction purposes. Now the scoreboard is only
* populated with relevant brands where they all have at least a single
* point. These are the contenders for which brand will be outputted.
*
* 4. Merge certain brand combinations together. For example, Azure's cloud
* is based on Hyper-V, but Hyper-V may have a higher score due to the
* prevalence of it in a practical setting, which will put Azure to the
* side. In reality, there should be an indication that Azure is involved
* since it's a better idea to let the user know that the brand is "Azure
* Hyper-V" instead of just "Hyper-V". So what this step does is "merge"
* the brands together to form a more accurate idea of the brand(s) involved.
*
* 5. After all of this, the scoreboard is sorted in descending order, where
* the brands with the highest points are now selected as the official
* output of the VM::brand() function.
*
* 6. The result is then cached to the memo module, so if another function
* invokes VM:brand() again, the result is retrieved from the cache
* without having it run all of the previous steps mentioned.
*
* (NOTE: it's a bit more complicated than this, but that's the gist of how this function works)
*
* Most of the functions provided usually depend on the 2 techniques covered.
* And they serve as a functionality base for other components of the lib.
*
*
* [1]: If the user has inputted a setting argument called VM::HIGH_THRESHOLD,
* the threshold becomes 300 instead of 150.
*/
#pragma once
#if defined(_WIN32) || defined(_WIN64)
#define WINDOWS 1
#define LINUX 0
#define APPLE 0
#elif (defined(__linux__))
#define WINDOWS 0
#define LINUX 1
#define APPLE 0
#elif (defined(__APPLE__) || defined(__APPLE_CPP__) || defined(__MACH__) || defined(__DARWIN))
#define WINDOWS 0
#define LINUX 0
#define APPLE 1
#else
#define WINDOWS 0
#define LINUX 0
#define APPLE 0
#endif
#ifdef _MSC_VER
#define MSVC 1
#endif
// shorter and succinct macros
#if __cplusplus > 202100L
#define CPP 23
#ifdef __VMAWARE_DEBUG__
#pragma message("using post-C++23, set back to C++23 standard")
#endif
#elif __cplusplus == 202100L
#define CPP 23
#ifdef __VMAWARE_DEBUG__
#pragma message("using C++23")
#endif
#elif __cplusplus == 202002L
#define CPP 20
#ifdef __VMAWARE_DEBUG__
#pragma message("using C++20")
#endif
#elif __cplusplus == 201703L
#define CPP 17
#ifdef __VMAWARE_DEBUG__
#pragma message("using C++17")
#endif
#elif __cplusplus == 201402L
#define CPP 14
#ifdef __VMAWARE_DEBUG__
#pragma message("using C++14")
#endif
#elif __cplusplus == 201103L
#define CPP 11
#ifdef __VMAWARE_DEBUG__
#pragma message("using C++11")
#endif
#elif __cplusplus < 201103L
#define CPP 1
#ifdef __VMAWARE_DEBUG__
#pragma message("using pre-C++11")
#endif
#else
#define CPP 0
#ifdef __VMAWARE_DEBUG__
#pragma message("Unknown C++ standard")
#endif
#endif
#if (CPP < 11 && !WINDOWS)
#error "VMAware only supports C++11 or above, set your compiler flag to '-std=c++20' for gcc/clang, or '/std:c++20' for MSVC"
#endif
// unused for now, maybe in the future idk
#if (WINVER == 0x0501) // Windows XP, (0x0701 for Windows 7)
#define WIN_XP 1
#else
#define WIN_XP 0
#endif
#if defined(__x86_64__) || defined(_M_X64)
#define x86_64 1
#else
#define x86_64 0
#endif
#if defined(__i386__) || defined(_M_IX86)
#define x86_32 1
#else
#define x86_32 0
#endif
#if x86_32 || x86_64
#define x86 1
#else
#define x86 0
#endif
#if (defined(__arm__) || defined(__ARM_LINUX_COMPILER__) || defined(__aarch64__) || defined(_M_ARM64))
#define ARM 1
#else
#define ARM 0
#endif
#if defined(__clang__)
#define GCC 0
#define CLANG 1
#elif defined(__GNUC__)
#define GCC 1
#define CLANG 0
#else
#define GCC 0
#define CLANG 0
#endif
#if !(defined(WINDOWS) || defined(LINUX) || defined(APPLE))
#warning "Unknown OS detected, tests will be severely limited"
#endif
#ifdef _MSC_VER
#pragma warning(push) // Save current warning state and disable all warnings for external Windows header files
#pragma warning(disable : 4365)
#pragma warning(disable : 4668)
#pragma warning(disable : 4820)
#pragma warning(disable : 5039)
#pragma warning(disable : 5204)
#endif
#if (CPP >= 23)
#include <limits>
#endif
#if (CPP >= 20)
#include <bit>
#include <ranges>
#include <source_location>
#endif
#if (CPP >= 17)
#include <filesystem>
#include <optional>
#endif
#ifdef __VMAWARE_DEBUG__
#include <iomanip>
#include <ios>
#include <locale>
#include <codecvt>
#endif
#include <functional>
#include <cstring>
#include <string>
#include <fstream>
#include <regex>
#include <thread>
#include <cstdint>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <array>
#include <algorithm>
#include <iostream>
#include <cassert>
#include <cmath>
#include <sstream>
#include <bitset>
#include <type_traits>
#include <atomic>
#if (WINDOWS)
#include <windows.h>
#include <intrin.h>
#include <tchar.h>
#include <iphlpapi.h>
#include <winioctl.h>
#include <winternl.h>
#include <winuser.h>
#include <psapi.h>
#include <shlwapi.h>
#include <shlobj_core.h>
#include <dshow.h>
#include <io.h>
#include <winspool.h>
#include <powerbase.h>
#include <setupapi.h>
#include <mmdeviceapi.h>
#include <Functiondiscoverykeys_devpkey.h>
#include <mmsystem.h>
#include <queue>
#include <dxgi.h>
#include <d3d9.h>
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "setupapi.lib")
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "MPR")
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "strmiids.lib")
#pragma comment(lib, "uuid.lib")
#pragma comment(lib, "ntdll.lib")
#pragma comment(lib, "setupapi.lib")
#pragma comment(lib, "powrprof.lib")
#elif (LINUX)
#if (x86)
#include <cpuid.h>
#include <x86intrin.h>
#include <immintrin.h>
#endif
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <sys/sysinfo.h>
#include <net/if.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <memory>
#include <cctype>
#include <fcntl.h>
#include <limits.h>
#elif (APPLE)
#include <sys/types.h>
#include <sys/sysctl.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <chrono>
#endif
#ifdef _MSC_VER
#pragma warning(pop) // Restore external Windows header file warnings
#endif
#if (!WINDOWS)
#define TCHAR char
#endif
#ifdef _UNICODE
#define tregex std::wregex
#else
#define tregex std::regex
#endif
// macro shortcut to disable MSVC warnings
#if (WINDOWS)
#define MSVC_DISABLE_WARNING(...) __pragma(warning(disable : __VA_ARGS__))
#define MSVC_ENABLE_WARNING(...) __pragma(warning(default : __VA_ARGS__))
#else
#define MSVC_DISABLE_WARNING(...)
#define MSVC_ENABLE_WARNING(...)
#endif
// MSVC-specific errors
#define SPECTRE 5045
#define ASSIGNMENT_OPERATOR 4626
#define NO_INLINE_FUNC 4514
#define PADDING 4820
#define FS_HANDLE 4733
MSVC_DISABLE_WARNING(ASSIGNMENT_OPERATOR NO_INLINE_FUNC SPECTRE)
#ifdef __VMAWARE_DEBUG__
#define debug(...) VM::util::debug_msg(__VA_ARGS__)
#define core_debug(...) VM::util::core_debug_msg(__VA_ARGS__)
#else
#define debug(...)
#define core_debug(...)
#endif
/**
* Official aliases for VM brands. This is added to avoid accidental typos
* which could really fuck up the result. Also, no errors/warnings are
* issued if the string is invalid in case of a typo. For example:
* scoreboard[VBOX]++;
* is much better and safer against typos than:
* scoreboard["VirtualBox"]++;
* Hopefully this makes sense.
*
* TL;DR I have wonky fingers :(
*/
namespace brands {
static constexpr const char* VBOX = "VirtualBox";
static constexpr const char* VMWARE = "VMware";
static constexpr const char* VMWARE_EXPRESS = "VMware Express";
static constexpr const char* VMWARE_ESX = "VMware ESX";
static constexpr const char* VMWARE_GSX = "VMware GSX";
static constexpr const char* VMWARE_WORKSTATION = "VMware Workstation";
static constexpr const char* VMWARE_FUSION = "VMware Fusion";
static constexpr const char* VMWARE_HARD = "VMware (with VmwareHardenedLoader)";
static constexpr const char* BHYVE = "bhyve";
static constexpr const char* KVM = "KVM";
static constexpr const char* QEMU = "QEMU";
static constexpr const char* QEMU_KVM = "QEMU+KVM";
static constexpr const char* KVM_HYPERV = "KVM Hyper-V Enlightenment";
static constexpr const char* QEMU_KVM_HYPERV = "QEMU+KVM Hyper-V Enlightenment";
static constexpr const char* HYPERV = "Microsoft Hyper-V";
static constexpr const char* HYPERV_VPC = "Microsoft Virtual PC/Hyper-V";
static constexpr const char* PARALLELS = "Parallels";
static constexpr const char* XEN = "Xen HVM";
static constexpr const char* ACRN = "ACRN";
static constexpr const char* QNX = "QNX hypervisor";
static constexpr const char* HYBRID = "Hybrid Analysis";
static constexpr const char* SANDBOXIE = "Sandboxie";
static constexpr const char* DOCKER = "Docker";
static constexpr const char* WINE = "Wine";
static constexpr const char* VPC = "Virtual PC";
static constexpr const char* ANUBIS = "Anubis";
static constexpr const char* JOEBOX = "JoeBox";
static constexpr const char* THREATEXPERT = "ThreatExpert";
static constexpr const char* CWSANDBOX = "CWSandbox";
static constexpr const char* COMODO = "Comodo";
static constexpr const char* BOCHS = "Bochs";
static constexpr const char* NVMM = "NetBSD NVMM";
static constexpr const char* BSD_VMM = "OpenBSD VMM";
static constexpr const char* INTEL_HAXM = "Intel HAXM";
static constexpr const char* UNISYS = "Unisys s-Par";
static constexpr const char* LMHS = "Lockheed Martin LMHS"; // lol
static constexpr const char* CUCKOO = "Cuckoo";
static constexpr const char* BLUESTACKS = "BlueStacks";
static constexpr const char* JAILHOUSE = "Jailhouse";
static constexpr const char* APPLE_VZ = "Apple VZ";
static constexpr const char* INTEL_KGT = "Intel KGT (Trusty)";
static constexpr const char* AZURE_HYPERV = "Microsoft Azure Hyper-V";
static constexpr const char* NANOVISOR = "Xbox NanoVisor (Hyper-V)";
static constexpr const char* SIMPLEVISOR = "SimpleVisor";
static constexpr const char* HYPERV_ARTIFACT = "Hyper-V artifact (not an actual VM)";
static constexpr const char* UML = "User-mode Linux";
static constexpr const char* POWERVM = "IBM PowerVM";
static constexpr const char* GCE = "Google Compute Engine (KVM)";
static constexpr const char* OPENSTACK = "OpenStack (KVM)";
static constexpr const char* KUBEVIRT = "KubeVirt (KVM)";
static constexpr const char* AWS_NITRO = "AWS Nitro System EC2 (KVM-based)";
static constexpr const char* PODMAN = "Podman";
static constexpr const char* WSL = "WSL";
static constexpr const char* OPENVZ = "OpenVZ";
static constexpr const char* BAREVISOR = "Barevisor";
static constexpr const char* HYPERPLATFORM = "HyperPlatform";
static constexpr const char* MINIVISOR = "MiniVisor";
static constexpr const char* INTEL_TDX = "Intel TDX";
static constexpr const char* LKVM = "LKVM";
static constexpr const char* AMD_SEV = "AMD SEV";
static constexpr const char* AMD_SEV_ES = "AMD SEV-ES";
static constexpr const char* AMD_SEV_SNP = "AMD SEV-SNP";
static constexpr const char* NEKO_PROJECT = "Neko Project II";
static constexpr const char* NOIRVISOR = "NoirVisor";
static constexpr const char* QIHOO = "Qihoo 360 Sandbox";
static constexpr const char* NSJAIL = "nsjail";
static constexpr const char* NULL_BRAND = "Unknown";
}
struct VM {
private:
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using i8 = std::int8_t;
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
public:
enum enum_flags : u8 {
VMID = 0,
CPU_BRAND,
HYPERVISOR_BIT,
HYPERVISOR_STR,
TIMER,
THREADCOUNT,
MAC,
TEMPERATURE,
SYSTEMD,
CVENDOR,
CTYPE,
DOCKERENV,
DMIDECODE,
DMESG,
HWMON,
SIDT5,
DLL,
REGISTRY,
VM_FILES,
HWMODEL,
DISK_SIZE,
VBOX_DEFAULT,
VBOX_NETWORK,
VM_PROCESSES,
LINUX_USER_HOST,
GAMARUE,
BOCHS_CPU,
MSSMBIOS,
MAC_MEMSIZE,
MAC_IOKIT,
IOREG_GREP,
MAC_SIP,
HKLM_REGISTRIES,
QEMU_GA,
VPC_INVALID,
SIDT,
SGDT,
SLDT,
OFFSEC_SIDT,
OFFSEC_SGDT,
OFFSEC_SLDT,
VPC_SIDT,
VMWARE_IOMEM,
VMWARE_IOPORTS,
VMWARE_SCSI,
VMWARE_DMESG,
VMWARE_STR,
VMWARE_BACKDOOR,
VMWARE_PORT_MEM,
SMSW,
MUTEX,
ODD_CPU_THREADS,
INTEL_THREAD_MISMATCH,
XEON_THREAD_MISMATCH,
NETTITUDE_VM_MEMORY,
CUCKOO_DIR,
CUCKOO_PIPE,
HYPERV_HOSTNAME,
GENERAL_HOSTNAME,
SCREEN_RESOLUTION,
DEVICE_STRING,
BLUESTACKS_FOLDERS,
CPUID_SIGNATURE,
KVM_BITMASK,
KGT_SIGNATURE,
QEMU_VIRTUAL_DMI,
QEMU_USB,
HYPERVISOR_DIR,
UML_CPU,
KMSG,
VM_PROCS,
VBOX_MODULE,
SYSINFO_PROC,
DEVICE_TREE,
DMI_SCAN,
SMBIOS_VM_BIT,
PODMAN_FILE,
WSL_PROC,
DRIVER_NAMES,
VM_SIDT,
HDD_SERIAL,
PORT_CONNECTORS,
GPU_VM_STRINGS,
GPU_CAPABILITIES,
VM_DEVICES,
IDT_GDT_SCAN,
PROCESSOR_NUMBER,
NUMBER_OF_CORES,
ACPI_TEMPERATURE,
PROCESSOR_ID,
SYS_QEMU,
LSHW_QEMU,
VIRTUAL_PROCESSORS,
HYPERV_QUERY,
BAD_POOLS,
AMD_SEV,
AMD_THREAD_MISMATCH,
NATIVE_VHD,
VIRTUAL_REGISTRY,
FIRMWARE,
FILE_ACCESS_HISTORY,
AUDIO,
UNKNOWN_MANUFACTURER,
OSXSAVE,
NSJAIL_PID,
PCI_VM,
// ADD NEW TECHNIQUE ENUM NAME HERE
// special flags, different to settings
DEFAULT,
ALL,
NULL_ARG, // does nothing, just a placeholder flag mainly for the CLI
// start of settings technique flags (THE ORDERING IS VERY SPECIFIC HERE AND MIGHT BREAK SOMETHING IF RE-ORDERED)
NO_MEMO,
HIGH_THRESHOLD,
DYNAMIC,
MULTIPLE
};
private:
static constexpr u8 enum_size = MULTIPLE; // get enum size through value of last element
static constexpr u8 settings_count = MULTIPLE - NO_MEMO + 1; // get number of settings technique flags like VM::NO_MEMO for example
static constexpr u8 INVALID = 255; // explicit invalid technique macro
static constexpr u16 base_technique_count = NO_MEMO; // original technique count, constant on purpose (can also be used as a base count value if custom techniques are added)
static constexpr u16 maximum_points = 5510; // theoretical total points if all VM detections returned true (which is practically impossible)
static constexpr u16 high_threshold_score = 300; // new threshold score from 150 to 300 if VM::HIGH_THRESHOLD flag is enabled
static constexpr bool SHORTCUT = true; // macro for whether VM::core::run_all() should take a shortcut by skipping the rest of the techniques if the threshold score is already met
// intended for loop indexes
static constexpr u8 enum_begin = 0;
static constexpr u8 enum_end = enum_size + 1;
static constexpr u8 technique_begin = enum_begin;
static constexpr u8 technique_end = NO_MEMO;
static constexpr u8 settings_begin = NO_MEMO;
static constexpr u8 settings_end = enum_end;
// this is specifically meant for VM::detected_count() to
// get the total number of techniques that detected a VM
static u8 detected_count_num;
private:
#if (WINDOWS)
using brand_score_t = i32;
#else
using brand_score_t = u8;
#endif
// for the flag bitset structure
using flagset = std::bitset<enum_size + 1>;
public:
// this will allow the enum to be used in the public interface as "VM::TECHNIQUE"
enum enum_flags tmp_ignore_this = NO_MEMO;
// constructor stuff ignore this
VM() = delete;
VM(const VM&) = delete;
VM(VM&&) = delete;
private:
// macro for bypassing unused parameter/variable warnings
#define UNUSED(x) ((void)(x))
// likely and unlikely macros
#if (LINUX)
# define VMAWARE_UNLIKELY(x) __builtin_expect(!!(x), 0)
# define VMAWARE_LIKELY(x) __builtin_expect(!!(x), 1)
#else
# define VMAWARE_UNLIKELY
# define VMAWARE_LIKELY
#endif
// specifically for util::hyper_x() and memo::hyperv
enum hyperx_state : u8 {
HYPERV_UNKNOWN_VM = 0,
HYPERV_REAL_VM,
HYPERV_ARTIFACT_VM,
HYPERV_ENLIGHTENMENT
};
// various cpu operation stuff
struct cpu {
// cpuid leaf values
struct leaf {
static constexpr u32
func_ext = 0x80000000,
proc_ext = 0x80000001,
brand1 = 0x80000002,
brand2 = 0x80000003,
brand3 = 0x80000004,
hypervisor = 0x40000000,
amd_easter_egg = 0x8fffffff;
};
// cross-platform wrapper function for linux and MSVC cpuid
static void cpuid
(
u32& a, u32& b, u32& c, u32& d,
const u32 a_leaf,
const u32 c_leaf = 0xFF // dummy value if not set manually
) {
#if (x86)
// may be unmodified for older 32-bit processors, clearing just in case
b = 0;
c = 0;
#if (WINDOWS)
int32_t x[4]{};
__cpuidex((int32_t*)x, static_cast<int>(a_leaf), static_cast<int>(c_leaf));
a = static_cast<u32>(x[0]);
b = static_cast<u32>(x[1]);
c = static_cast<u32>(x[2]);
d = static_cast<u32>(x[3]);
#elif (LINUX || APPLE)
__cpuid_count(a_leaf, c_leaf, a, b, c, d);
#endif
#else
return;
#endif
};
// same as above but for array type parameters (MSVC specific)
static void cpuid
(
int32_t x[4],
const u32 a_leaf,
const u32 c_leaf = 0xFF
) {
#if (x86)
// may be unmodified for older 32-bit processors, clearing just in case
x[1] = 0;
x[2] = 0;
#if (WINDOWS)
__cpuidex((int32_t*)x, static_cast<int>(a_leaf), static_cast<int>(c_leaf));
#elif (LINUX || APPLE)
__cpuid_count(a_leaf, c_leaf, x[0], x[1], x[2], x[3]);
#endif
#else
return;
#endif
};
// check for maximum function leaf
static bool is_leaf_supported(const u32 p_leaf) {
u32 eax, unused = 0;
cpu::cpuid(eax, unused, unused, unused, cpu::leaf::func_ext);
debug("CPUID function: highest leaf = ", eax);
return (p_leaf <= eax);
}
// check AMD
[[nodiscard]] static bool is_amd() {
constexpr u32 amd_ecx = 0x444d4163; // "cAMD"
u32 unused, ecx = 0;
cpuid(unused, unused, ecx, unused, 0);
return (ecx == amd_ecx);
}
// check Intel
[[nodiscard]] static bool is_intel() {
constexpr u32 intel_ecx1 = 0x6c65746e; // "ntel"
constexpr u32 intel_ecx2 = 0x6c65746f; // "otel", this is because some Intel CPUs have a rare manufacturer string of "GenuineIotel"
u32 unused, ecx = 0;
cpuid(unused, unused, ecx, unused, 0);
return ((ecx == intel_ecx1) || (ecx == intel_ecx2));
}
// check for POSSIBILITY of hyperthreading, I don't think there's a
// full-proof method to detect if you're actually hyperthreading imo.
[[nodiscard]] static bool has_hyperthreading() {
u32 unused, ebx, edx;
cpuid(unused, ebx, unused, edx, 1);
UNUSED(unused);
bool htt_available = (edx & (1 << 28));
if (!htt_available) {
return false;
}
u32 logical_cores = ((ebx >> 16) & 0xFF);
i32 physical_cores = 0;
#if (WINDOWS)
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
physical_cores = static_cast<i32>(sysinfo.dwNumberOfProcessors);
#elif (LINUX)
physical_cores = static_cast<i32>(sysconf(_SC_NPROCESSORS_CONF));
#elif (APPLE)
// sysctlbyname("hw.physicalcpu", &physical_cores, sizeof(physical_cores), NULL, 0);
// the code under this is the same as the one commented right above, removed due to non-backwards compatibility
i32 mib[2];
std::size_t size = sizeof(physical_cores);
mib[0] = CTL_HW; // hardware information
mib[1] = HW_NCPU; // physical CPU count
if (sysctl(mib, 2, &physical_cores, &size, NULL, 0) != 0) {
debug("HAS_HYPERTHREADING(): sysctl failed, returned false");
return false;
}
#else
return false;
#endif
return (logical_cores > static_cast<u32>(physical_cores));
}
// get the CPU product
[[nodiscard]] static std::string get_brand() {
if (memo::cpu_brand::is_cached()) {
return memo::cpu_brand::fetch();
}
#if (!x86)
return "Unknown";
#else
if (!cpu::is_leaf_supported(cpu::leaf::brand3)) {
return "Unknown";
}
std::array<u32, 4> buffer{};
constexpr std::size_t buffer_size = sizeof(int32_t) * buffer.size();
std::array<char, 64> charbuffer{};
constexpr std::array<u32, 3> ids = { {
cpu::leaf::brand1,
cpu::leaf::brand2,
cpu::leaf::brand3
} };
std::string brand = "";
for (const u32& id : ids) {
cpu::cpuid(buffer.at(0), buffer.at(1), buffer.at(2), buffer.at(3), id);
std::memcpy(charbuffer.data(), buffer.data(), buffer_size);
const char* convert = charbuffer.data();
brand += convert;
}
debug("BRAND: ", "cpu brand = ", brand);
memo::cpu_brand::store(brand);
return brand;
#endif
}
// cpu manufacturer id
[[nodiscard]] static std::string cpu_manufacturer(const u32 p_leaf) {
auto cpuid_thingy = [](const u32 p_leaf, u32* regs, std::size_t start = 0, std::size_t end = 4) -> bool {
u32 x[4]{};
cpu::cpuid(x[0], x[1], x[2], x[3], p_leaf);
for (; start < end; start++) {
*regs++ = x[start];
}
return true;
};
u32 sig_reg[3] = { 0 };
// Start at index 1 to get EBX, ECX, EDX (x[1], x[2], x[3])
if (!cpuid_thingy(p_leaf, sig_reg, 1, 4)) {
return "";
}
if ((sig_reg[0] == 0) && (sig_reg[1] == 0) && (sig_reg[2] == 0)) {
return "";
}
auto strconvert = [](u32 n) -> std::string {
const char* bytes = reinterpret_cast<const char*>(&n);
return std::string(bytes, 4);
};
std::stringstream ss;
if (p_leaf >= 0x40000000) {
// Hypervisor vendor string order: EBX, ECX, EDX
ss << strconvert(sig_reg[0]) << strconvert(sig_reg[1]) << strconvert(sig_reg[2]);
}
else {
// Standard vendor string (leaf 0x0) order: EBX, EDX, ECX
ss << strconvert(sig_reg[0]) << strconvert(sig_reg[2]) << strconvert(sig_reg[1]);
}
return ss.str();
}
struct stepping_struct {
u8 model;
u8 family;
u8 extmodel;
};
[[nodiscard]] static stepping_struct fetch_steppings() {
struct stepping_struct steps {};
u32 unused, eax = 0;
cpu::cpuid(eax, unused, unused, unused, 1);
UNUSED(unused);
steps.model = ((eax >> 4) & 0b1111);
steps.family = ((eax >> 8) & 0b1111);
steps.extmodel = ((eax >> 16) & 0b1111);
return steps;
}
// check if the CPU is an intel celeron