-
Notifications
You must be signed in to change notification settings - Fork 2
/
VMDetect.cpp
195 lines (160 loc) · 4.21 KB
/
VMDetect.cpp
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
#include "VMDetect.hpp"
#include "InstructionSet_MS.hpp"
#if _WIN32 && !(_WIN64)
// Inline assembly for possible quicker execution.
DWORD64 VMDetect::CalcRDTSCDifference()
{
unsigned long long cycles_one, cycles_two;
unsigned cycles_high, cycles_low;
__asm
{
RDTSC
MOV cycles_high, eax
MOV cycles_low, edx
}
cycles_one = ((unsigned long long)cycles_high) | (((unsigned long long)cycles_low) << 32);
__asm
{
RDTSC
MOV cycles_high, eax
MOV cycles_low, edx
}
cycles_two = ((unsigned long long)cycles_high) | (((unsigned long long)cycles_low) << 32);
return cycles_two - cycles_one;
}
#endif
#if _WIN64
// Inline assembly is not available when compiling using 64 bit.
DWORD64 VMDetect::CalcRDTSCDifference()
{
DWORD64 dwCyclesFirst = ReadTimeStampCounter();
DWORD64 dwCyclesSecond = ReadTimeStampCounter();
return dwCyclesSecond - dwCyclesFirst;
}
#endif
BOOLEAN VMDetect::CheckRTDSCTimings(VOID)
{
unsigned long long i = 0, average = 0;
for (; i < 10; i++)
{
average += CalcRDTSCDifference();
Sleep(500);
}
average /= i;
return (average < 750 && average > 0) ? FALSE : TRUE;
}
BOOLEAN VMDetect::CheckCPUIDLeafResponse(VOID)
{
unsigned int invalid_leaf = 0x13371337;
unsigned int valid_leaf = 0x40000000;
struct _HV_DETAILS
{
unsigned int Data[4];
};
_HV_DETAILS InvalidLeafResponse = { 0 };
_HV_DETAILS ValidLeafResponse = { 0 };
__cpuid((int*)&InvalidLeafResponse, invalid_leaf);
__cpuid((int*)&ValidLeafResponse, valid_leaf);
if ((InvalidLeafResponse.Data[0] != ValidLeafResponse.Data[0]) ||
(InvalidLeafResponse.Data[1] != ValidLeafResponse.Data[1]) ||
(InvalidLeafResponse.Data[2] != ValidLeafResponse.Data[2]) ||
(InvalidLeafResponse.Data[3] != ValidLeafResponse.Data[3]))
return TRUE;
return FALSE;
}
BOOLEAN VMDetect::CheckSyntheticMSRs(VOID)
{
__try
{
__readmsr(HV_SYNTHETIC_MSR_RANGE_START);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
return FALSE;
}
return TRUE;
}
BOOLEAN VMDetect::FileExists(std::string FileName)
{
std::fstream FileToTest(FileName);
if (FileToTest.is_open())
{
FileToTest.close();
return TRUE;
}
else
{
return FALSE;
}
}
BOOLEAN VMDetect::CheckDebuggerPresent(VOID)
{
return IsDebuggerPresent(); // wraps so we get clean output.
}
// https://github.com/a0rtega/pafish/blob/master/pafish/vbox.c#L118
BOOLEAN VMDetect::VirtualBox::CheckSystemFiles(VOID)
{
std::vector<std::string> Files2Check = {
"C:\\WINDOWS\\system32\\vboxdisp.dll",
"C:\\WINDOWS\\system32\\vboxhook.dll",
"C:\\WINDOWS\\system32\\vboxmrxnp.dll",
"C:\\WINDOWS\\system32\\vboxogl.dll",
"C:\\WINDOWS\\system32\\vboxoglarrayspu.dll",
"C:\\WINDOWS\\system32\\vboxoglcrutil.dll",
"C:\\WINDOWS\\system32\\vboxoglerrorspu.dll",
"C:\\WINDOWS\\system32\\vboxoglfeedbackspu.dll",
"C:\\WINDOWS\\system32\\vboxoglpackspu.dll",
"C:\\WINDOWS\\system32\\vboxoglpassthroughspu.dll",
"C:\\WINDOWS\\system32\\vboxservice.exe",
"C:\\WINDOWS\\system32\\vboxtray.exe",
"C:\\WINDOWS\\system32\\VBoxControl.exe",
"C:\\program files\\oracle\\virtualbox guest additions\\"
};
for (auto File : Files2Check)
{
if (FileExists(File))
{
std::cout << "!! Found: " << File << std::endl;
return TRUE;
}
}
return FALSE;
}
BOOLEAN VMDetect::VirtualBox::CheckTrayWindowExists(VOID)
{
HWND VBoxTrayToolClass = FindWindow(L"VBoxTrayToolWndClass", NULL);
HWND VBoxTrayToolWnd = FindWindow(NULL, L"VBoxTrayToolWnd");
return (VBoxTrayToolClass || VBoxTrayToolWnd);
}
BOOLEAN VMDetect::CPU::CheckCpuVendor(VOID)
{
std::vector<std::string> DisallowedNames = {
"KVMKVMKVM\0\0\0",
"Microsoft Hv",
"VMwareVMware",
"XenVMMXenVMM",
"prl hyperv ",
"VBoxVBoxVBox"
};
auto CurrentCPUName = InstructionSet::Vendor();
for (auto Vender : DisallowedNames)
{
if (Vender == CurrentCPUName)
{
std::cout << "!! Vendor name matches disallowed \"" << Vender << "\"" << std::endl;
return TRUE;
}
}
return FALSE;
}
// https://github.com/a0rtega/pafish/blob/184b3fc3d5431e7334485a1eb33f30d3be125dc3/pafish/bochs.c#L18
BOOLEAN VMDetect::CPU::CheckBochsAMD(VOID)
{
return (!memcmp(InstructionSet::Brand().data(), "AMD Athlon(tm) processor", 24)) ? TRUE : FALSE;
}
BOOLEAN VMDetect::CPU::CheckHVBit(VOID)
{
int* values = new int[4];
__cpuid(values, 0x01);
return (values[2] >> 32) & 0x1;
}