This repository has been archived by the owner on Jun 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AntiVirtualization.cpp
402 lines (322 loc) · 11.5 KB
/
AntiVirtualization.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
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
#include "AntiVirtualization.h"
#pragma comment(lib, "wbemuuid.lib")
std::mutex AntiVirtualization::mutex;
std::unique_ptr<AntiVirtualization> AntiVirtualization::instance;
AntiVirtualization* AntiVirtualization::getInstance() {
if (!instance) {
std::lock_guard<std::mutex> lock(mutex);
if (!instance) {
instance.reset(new AntiVirtualization());
}
}
return instance.get();
}
bool AntiVirtualization::isSandboxiePresent() const {
return (GetModuleHandle("SbieDll.dll") != nullptr);
}
bool AntiVirtualization::isComodoSandboxPresent() const {
return (GetModuleHandle("cmdvrt32.dll") != nullptr || GetModuleHandle("cmdvrt64.dll") != nullptr);
}
bool AntiVirtualization::isQihoo360SandboxPresent() const {
return (GetModuleHandle("SxIn.dll") != nullptr);
}
bool AntiVirtualization::isCuckooSandboxPresent() const {
return (GetModuleHandle("cuckoomon.dll") != nullptr);
}
bool AntiVirtualization::isEmulationPresent() const {
ULONGLONG tick = GetTickCount64();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
ULONGLONG tick2 = GetTickCount64();
return ((tick2 - tick) < 500);
}
bool AntiVirtualization::isWinePresent() const {
HMODULE moduleHandle = GetModuleHandle("kernel32.dll");
if (moduleHandle != nullptr) {
FARPROC procAddress = GetProcAddress(moduleHandle, "wine_get_unix_file_name");
return (procAddress != nullptr);
}
return false;
}
bool AntiVirtualization::checkForHyperV() const {
std::vector<std::string> servicesToCheck = { "vmbus", "VMBusHID", "hyperkbd" };
// Enumerate all services on the system
SC_HANDLE hSCManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_ENUMERATE_SERVICE);
if (hSCManager == nullptr) {
return false;
}
DWORD bytesNeeded, servicesCount;
if (!EnumServicesStatus(hSCManager, SERVICE_WIN32, SERVICE_ACTIVE, nullptr, 0, &bytesNeeded, &servicesCount, nullptr)) {
CloseServiceHandle(hSCManager);
return false;
}
std::vector<BYTE> buffer(bytesNeeded);
ENUM_SERVICE_STATUS* services = reinterpret_cast<ENUM_SERVICE_STATUS*>(buffer.data());
if (!EnumServicesStatus(hSCManager, SERVICE_WIN32, SERVICE_ACTIVE, services, bytesNeeded, &bytesNeeded, &servicesCount, nullptr)) {
CloseServiceHandle(hSCManager);
return false;
}
CloseServiceHandle(hSCManager);
// Check each service name
for (DWORD i = 0; i < servicesCount; ++i) {
std::string serviceName(services[i].lpServiceName);
std::transform(serviceName.begin(), serviceName.end(), serviceName.begin(), ::tolower);
for (const auto& serviceToCheck : servicesToCheck) {
if (serviceName.find(serviceToCheck) != std::string::npos) {
return true;
}
}
}
return false;
}
bool AntiVirtualization::badVMProcessNames() const {
std::vector<std::string> badProcessNames = { "vboxservice", "VGAuthService", "vmusrvc", "qemu-ga" };
HANDLE hProcessSnap;
PROCESSENTRY32 pe32 = { 0 }; // Explicitly initialize PROCESSENTRY32
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE) {
return false;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hProcessSnap, &pe32)) {
CloseHandle(hProcessSnap);
return false;
}
do {
std::string processName = pe32.szExeFile;
for (const auto& badProcessName : badProcessNames) {
if (processName == badProcessName) {
CloseHandle(hProcessSnap);
return true;
}
}
} while (Process32Next(hProcessSnap, &pe32));
CloseHandle(hProcessSnap);
return false;
}
bool AntiVirtualization::checkDevices() const {
std::string devices[] = { "\\\\.\\pipe\\cuckoo", "\\\\.\\HGFS", "\\\\.\\vmci", "\\\\.\\VBoxMiniRdrDN", "\\\\.\\VBoxGuest", "\\\\.\\pipe\\VBoxMiniRdDN", "\\\\.\\VBoxTrayIPC", "\\\\.\\pipe\\VBoxTrayIPC" };
for (const auto& device : devices) {
HANDLE pipe = CreateFileA(device.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (pipe != INVALID_HANDLE_VALUE) {
CloseHandle(pipe);
return true;
}
}
return false;
}
bool AntiVirtualization::checkForBlacklistedNames() const {
std::vector<std::string> badNames = { "Johnson", "Miller", "malware", "maltest", "CurrentUser", "Sandbox", "virus", "John Doe", "test user", "sand box", "WDAGUtilityAccount" };
char username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
if (!GetUserName(username, &username_len)) {
return false;
}
std::string usernameStr(username);
std::transform(usernameStr.begin(), usernameStr.end(), usernameStr.begin(), ::tolower);
for (const auto& badUsername : badNames) {
std::string lowerBadUsername = badUsername;
std::transform(lowerBadUsername.begin(), lowerBadUsername.end(), lowerBadUsername.begin(), ::tolower);
if (usernameStr == lowerBadUsername) {
return true;
}
}
return false;
}
bool AntiVirtualization::badVMFilesDetection() const {
try {
std::vector<std::string> badFileNames = { "VBoxMouse.sys", "VBoxGuest.sys", "VBoxSF.sys", "VBoxVideo.sys", "vmmouse.sys", "vboxogl.dll" };
std::vector<std::string> badDirs = { "C:\\Program Files\\VMware", "C:\\Program Files\\oracle\\virtualbox guest additions" };
char systemPath[MAX_PATH];
if (!GetSystemDirectory(systemPath, MAX_PATH)) {
return false;
}
std::string systemDir(systemPath);
for (const auto& entry : std::filesystem::directory_iterator(systemDir)) {
if (entry.is_regular_file()) {
std::string fileName = entry.path().filename().string();
std::transform(fileName.begin(), fileName.end(), fileName.begin(), ::tolower);
for (const auto& badFileName : badFileNames) {
std::string lowerBadFileName = badFileName;
std::transform(lowerBadFileName.begin(), lowerBadFileName.end(), lowerBadFileName.begin(), ::tolower);
if (fileName == lowerBadFileName) {
return true;
}
}
}
}
for (const auto& badDir : badDirs) {
std::string lowerBadDir = badDir;
std::transform(lowerBadDir.begin(), lowerBadDir.end(), lowerBadDir.begin(), ::tolower);
if (std::filesystem::exists(lowerBadDir)) {
return true;
}
}
}
catch (...) {
// handle any exceptions if needed
}
return false;
}
bool AntiVirtualization::checkForParallels() const {
std::vector<std::string> badDriversList = { "prl_sf", "prl_tg", "prl_eth" };
for (const auto& driver : std::filesystem::directory_iterator("C:\\Windows\\System32")) {
std::string driverName = driver.path().filename().string();
for (const auto& badDriver : badDriversList) {
if (driverName.find(badDriver) != std::string::npos) {
return true;
}
}
}
return false;
}
bool AntiVirtualization::checkForQemu() const {
const std::string badDrivers[] = { "qemu-ga", "qemuwmi" };
char* systemRoot;
size_t len;
errno_t err = _dupenv_s(&systemRoot, &len, "SystemRoot");
if (err != 0 || systemRoot == nullptr) {
// Handle error if _dupenv_s fails
return false;
}
std::filesystem::path systemRootPath(systemRoot);
free(systemRoot); // Free the memory allocated by _dupenv_s
for (const auto& driver : std::filesystem::directory_iterator(systemRootPath / "System32")) {
for (const auto& badDriver : badDrivers) {
if (driver.path().string().find(badDriver) != std::string::npos) {
return true;
}
}
}
return false;
}
bool AntiVirtualization::triageCheck() const {
HRESULT hres;
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres)) {
CoUninitialize();
return false; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres)) {
CoUninitialize();
return false; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator* pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID*)&pLoc);
if (FAILED(hres)) {
CoUninitialize();
return false; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices* pSvc = NULL;
// Connect to the root\cimv2 namespace with the current user and obtain pointer pSvc to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres)) {
pLoc->Release();
CoUninitialize();
return false; // Program has failed.
}
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres)) {
pSvc->Release();
pLoc->Release();
CoUninitialize();
return false; // Program has failed.
}
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
// For example, query for operating system data
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_DiskDrive"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres)) {
pSvc->Release();
pLoc->Release();
CoUninitialize();
return false; // Program has failed.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
IWbemClassObject* pclsObj;
ULONG uReturn = 0;
while (pEnumerator) {
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (0 == uReturn) {
break;
}
VARIANT vtProp;
VariantInit(&vtProp); // Initialize VARIANT
// Get the value of the Name property
hr = pclsObj->Get(L"Model", 0, &vtProp, 0, 0);
if (FAILED(hr)) {
VariantClear(&vtProp);
pclsObj->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
return false;
}
std::wstring modelName = vtProp.bstrVal;
VariantClear(&vtProp);
if (modelName.find(L"DADY HARDDISK") != std::wstring::npos || modelName.find(L"QEMU HARDDISK") != std::wstring::npos) {
pclsObj->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
return true;
}
pclsObj->Release();
}
// Cleanup
// =========
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();
return false; // Program successfully completed.
}