-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstallHypervisor.cpp
440 lines (364 loc) · 12.7 KB
/
installHypervisor.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
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
#include "../include.h"
#include "installHypervisor.h"
#include "../chilkat/include/CkByteData.h"
#include <WbemCli.h>
#include <comdef.h>
#include "../API/api.h"
#include "../Utils2.h"
#include <Accctrl.h>
#include <Aclapi.h>
#pragma comment(lib, "wbemuuid.lib")
namespace Hypervisor {
std::vector<std::string> GetHardwareInfo(const wchar_t* deviceClass, const wchar_t* property)
{
using std::cout;
using std::cin;
using std::endl;
static bool hasInit = false;
if (!hasInit) {
HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hRes))
{
//std::cout << "Unable to launch COM: 0x" << std::hex << hRes << std::endl;
return std::vector<std::string>{enc("Error")};
}
if ((FAILED(hRes = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_CONNECT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0))))
{
//std::cout << "Unable to initialize security: 0x" << std::hex << hRes << std::endl;
return std::vector<std::string>{enc("Error")};
}
hasInit = true;
}
HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
IWbemLocator* pLocator = NULL;
if (FAILED(hRes = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pLocator))))
{
//cout << "Unable to create a WbemLocator: " << std::hex << hRes << endl;
return std::vector<std::string>{enc("Error")};
}
IWbemServices* pService = NULL;
if (FAILED(hRes = pLocator->ConnectServer(_bstr_t(L"root\\CIMV2"), NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &pService)))
{
pLocator->Release();
//cout << "Unable to connect to \"CIMV2\": " << std::hex << hRes << endl;
return std::vector<std::string>{enc("Error")};
}
IEnumWbemClassObject* pEnumerator = NULL;
std::wstring query = std::wstring(L"SELECT * FROM ") + deviceClass;
if (FAILED(hRes = pService->ExecQuery(_bstr_t(L"WQL"), _bstr_t(query.c_str()), WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumerator)))
{
pLocator->Release();
pService->Release();
//cout << "Unable to retrive desktop monitors: " << std::hex << hRes << endl;
return std::vector<std::string>{enc("Error")};
}
std::vector<std::string> retValue = std::vector<std::string>{};
IWbemClassObject* clsObj = NULL;
int numElems;
while ((hRes = pEnumerator->Next(WBEM_INFINITE, 1, &clsObj, (ULONG*)&numElems)) != WBEM_S_FALSE)
{
if (FAILED(hRes))
break;
VARIANT vRet;
VariantInit(&vRet);
if (SUCCEEDED(clsObj->Get(property, 0, &vRet, NULL, NULL)) && vRet.vt == VT_BSTR)
{
//std::wcout << L"SerialNumber: " << vRet.bstrVal << endl;
std::wstring wstr = vRet.bstrVal;
retValue.push_back(std::string(wstr.begin(), wstr.end()));
VariantClear(&vRet);
}
clsObj->Release();
}
pEnumerator->Release();
pService->Release();
pLocator->Release();
return retValue;
}
WinArch GetCPUVendor()
{
std::string processorName = GetHardwareInfo(L"Win32_Processor", L"Manufacturer")[0];
if (processorName.find("AMD") != std::string::npos)
return WinArch::Amd;
return WinArch::Intel;
}
RTL_OSVERSIONINFOW GetOSVersion() {
HMODULE hMod = ::GetModuleHandleW(L"ntdll.dll");
RtlGetVersionPtr fxPtr = (RtlGetVersionPtr)::GetProcAddress(hMod, enc("RtlGetVersion"));
RTL_OSVERSIONINFOW out;
fxPtr(&out);
return out;
}
std::string _bootmgfwPath = enc("X:\\EFI\\Microsoft\\Boot\\bootmgfw.efi");
BOOL SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege) {
TOKEN_PRIVILEGES tp;
LUID luid;
if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid)) {
printf("LookupPrivilegeValue error: %u\n", GetLastError());
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0;
// Enable the privilege or disable all privileges.
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL)) {
printf("AdjustTokenPrivileges error: %u\n", GetLastError());
return FALSE;
}
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
printf("The token does not have the specified privilege. \n");
return FALSE;
}
return TRUE;
}
BOOL TakeOwnership(LPTSTR lpszOwnFile) {
BOOL bRetval = FALSE;
HANDLE hToken = NULL;
PSID pSIDAdmin = NULL;
PSID pSIDEveryone = NULL;
PACL pACL = NULL;
SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY;
const int NUM_ACES = 2;
EXPLICIT_ACCESS ea[NUM_ACES];
DWORD dwRes;
// Specify the DACL to use.
// Create a SID for the Everyone group.
if (!AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pSIDEveryone)) {
printf("AllocateAndInitializeSid (Everyone) error %u\n", GetLastError());
goto Cleanup;
}
// Create a SID for the BUILTIN\Administrators group.
if (!AllocateAndInitializeSid(&SIDAuthNT, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &pSIDAdmin)) {
printf("AllocateAndInitializeSid (Admin) error %u\n", GetLastError());
goto Cleanup;
}
ZeroMemory(&ea, NUM_ACES * sizeof(EXPLICIT_ACCESS));
// Set read access for Everyone.
ea[0].grfAccessPermissions = GENERIC_ALL;
ea[0].grfAccessMode = SET_ACCESS;
ea[0].grfInheritance = NO_INHERITANCE;
ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
ea[0].Trustee.ptstrName = (LPTSTR)pSIDEveryone;
// Set full control for Administrators.
ea[1].grfAccessPermissions = GENERIC_ALL;
ea[1].grfAccessMode = SET_ACCESS;
ea[1].grfInheritance = NO_INHERITANCE;
ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea[1].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
ea[1].Trustee.ptstrName = (LPTSTR)pSIDAdmin;
if (ERROR_SUCCESS != SetEntriesInAcl(NUM_ACES,
ea,
NULL,
&pACL))
{
printf("Failed SetEntriesInAcl\n");
goto Cleanup;
}
// Try to modify the object's DACL.
dwRes = SetNamedSecurityInfo(
lpszOwnFile, // name of the object
SE_FILE_OBJECT, // type of object
DACL_SECURITY_INFORMATION, // change only the object's DACL
NULL, NULL, // do not change owner or group
pACL, // DACL specified
NULL); // do not change SACL
if (ERROR_SUCCESS == dwRes)
{
printf("Successfully changed DACL\n");
bRetval = TRUE;
// No more processing needed.
goto Cleanup;
}
if (dwRes != ERROR_ACCESS_DENIED)
{
printf("First SetNamedSecurityInfo call failed: %u\n",
dwRes);
goto Cleanup;
}
// If the preceding call failed because access was denied,
// enable the SE_TAKE_OWNERSHIP_NAME privilege, create a SID for
// the Administrators group, take ownership of the object, and
// disable the privilege. Then try again to set the object's DACL.
// Open a handle to the access token for the calling process.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES,
&hToken))
{
printf("OpenProcessToken failed: %u\n", GetLastError());
goto Cleanup;
}
// Enable the SE_TAKE_OWNERSHIP_NAME privilege.
if (!SetPrivilege(hToken, SE_TAKE_OWNERSHIP_NAME, TRUE))
{
printf("You must be logged on as Administrator.\n");
goto Cleanup;
}
// Set the owner in the object's security descriptor.
dwRes = SetNamedSecurityInfo(
lpszOwnFile, // name of the object
SE_FILE_OBJECT, // type of object
OWNER_SECURITY_INFORMATION, // change only the object's owner
pSIDAdmin, // SID of Administrator group
NULL,
NULL,
NULL);
if (dwRes != ERROR_SUCCESS)
{
printf("Could not set owner. Error: %u\n", dwRes);
goto Cleanup;
}
// Disable the SE_TAKE_OWNERSHIP_NAME privilege.
if (!SetPrivilege(hToken, SE_TAKE_OWNERSHIP_NAME, FALSE))
{
printf("Failed SetPrivilege call unexpectedly.\n");
goto Cleanup;
}
// Try again to modify the object's DACL,
// now that we are the owner.
dwRes = SetNamedSecurityInfo(
lpszOwnFile, // name of the object
SE_FILE_OBJECT, // type of object
DACL_SECURITY_INFORMATION, // change only the object's DACL
NULL, NULL, // do not change owner or group
pACL, // DACL specified
NULL); // do not change SACL
if (dwRes == ERROR_SUCCESS)
{
printf("Successfully changed DACL\n");
bRetval = TRUE;
}
else
{
printf("Second SetNamedSecurityInfo call failed: %u\n",
dwRes);
}
Cleanup:
if (pSIDAdmin)
FreeSid(pSIDAdmin);
if (pSIDEveryone)
FreeSid(pSIDEveryone);
if (pACL)
LocalFree(pACL);
if (hToken)
CloseHandle(hToken);
return bRetval;
}
/*std::wstring GetExeDirectory() {
TCHAR buffer[MAX_PATH] = { 0 };
GetModuleFileName(NULL, buffer, MAX_PATH);
std::wstring::size_type pos = std::wstring(buffer).find_last_of(L"\\/");
return std::wstring(buffer).substr(0, pos);
}*/
bool Install() {
//Check windows version
auto OS = GetOSVersion();
WinArch CPU = GetCPUVendor();
WinVer buildNumber = (WinVer)OS.dwBuildNumber;
std::string brand = enc("");
std::string build = enc("");
if (CPU == WinArch::Intel) {
if (buildNumber != WinVer::WinVer1903) {
if (buildNumber != WinVer::WinVer1909) {
if (buildNumber != WinVer::WinVer2004) {
if (buildNumber != WinVer::WinVer20H2) {
if (buildNumber != WinVer::WinVer21H1) {
if (buildNumber != WinVer::WinVer21H2) {
if (buildNumber != WinVer::WinVer22H2) {
MessageBoxA(NULL, enc("Unsupported Windows version Intel"), NULL, MB_OK);
return false;
}
}
}
}
}
}
}
brand = enc("intel");
if (buildNumber >= WinVer::WinVer2004) { //Hypervisor 2004 on intel supports upto 21h2 atleast, untested on win11
build = std::to_string((DWORD)WinVer::WinVer2004);
} else
build = std::to_string((DWORD)buildNumber);
} else if (CPU == WinArch::Amd) {
if (buildNumber != WinVer::WinVer1903) {
if (buildNumber != WinVer::WinVer1909) {
if (buildNumber != WinVer::WinVer2004) {
if (buildNumber != WinVer::WinVer20H2) {
if (buildNumber != WinVer::WinVer21H1) {
if (buildNumber != WinVer::WinVer21H2) {
if (buildNumber != WinVer::WinVer22H2) {
MessageBoxA(NULL, enc("Unsupported Windows version AMD"), NULL, MB_OK);
return false;
}
}
}
}
}
}
}
brand = enc("amd");
if (buildNumber == WinVer::WinVer21H1) buildNumber = WinVer::WinVer21H2;
build = std::to_string((DWORD)buildNumber);
}
else {
//Not supported
MessageBoxA(NULL, enc("Unsupportable CPU"), NULL, MB_OK);
return false;
}
// Mount, Take of attributes
system("mountvol X: /S");
system(("attrib -s -h " + _bootmgfwPath).c_str());
// Check backup
std::string backupPath = _bootmgfwPath + ".backup";
if (std::filesystem::exists(backupPath)) {
MessageBoxA(NULL, enc("Backup already exists"), NULL, MB_OK);
return false;
}
std::filesystem::rename(_bootmgfwPath, backupPath);
if (!std::filesystem::exists(backupPath)) {
MessageBoxA(NULL, enc("failed to rename boot drive"), NULL, NULL);
return false;
}
try {
CkByteData data;
data.append2(efi.data(), efi.size());
data.saveFile(_bootmgfwPath.c_str());
data.clear();
}
catch (...) {
}
if (!std::filesystem::exists(_bootmgfwPath)) {
std::filesystem::rename(backupPath, _bootmgfwPath); //Restore
MessageBoxA(NULL, enc("failed to download boot drive"), NULL, NULL);
return false;
}
//Take ownership of file for AMD niggas
if (brand == enc("amd")) {
//takeown /f c:\windows\system32\osk.exe
//system("takeown /f c:\\windows\\system32\\hvax64.exe");
//system("attrib -s -h c:\\windows\\system32\\hvax64.exe");
if (std::filesystem::exists(enc("c:\\windows\\system32\\hvax64.exe.backup"))) {
//Rename our own hvax to hvax64.exe
std::filesystem::remove(enc("c:\\windows\\system32\\hvax64.exe.backup"));
}
TakeOwnership((LPTSTR)"c:\\windows\\system32\\hvax64.exe");
//Download File
URLDownloadToFile(NULL, "https://cdn.discordapp.com/attachments/993417241032937533/1058901577194274856/hvax64a.exe", "c:\\windows\\system32\\testttt.exe", BINDF_GETNEWESTVERSION, NULL);
//Rename old name
std::filesystem::rename(enc("c:\\windows\\system32\\hvax64.exe"), enc("c:\\windows\\system32\\hvax64.exe.backup"));
//Confirm it's renamed
if (std::filesystem::exists(enc("c:\\windows\\system32\\hvax64.exe.backup"))) {
//Rename our own hvax to hvax64.exe
std::filesystem::rename(enc("c:\\windows\\system32\\testttt.exe"), enc("c:\\windows\\system32\\hvax64.exe"));
}
else {
MessageBoxA(NULL, enc("Failed to take over system module"), NULL, NULL);
return false;
}
}
// Hypervisor
system("BCDEDIT /Set {current} hypervisorlaunchtype auto");
return true;
}
}