-
Notifications
You must be signed in to change notification settings - Fork 1
/
detector.cpp
317 lines (270 loc) · 8.79 KB
/
detector.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
#include "detector.hpp"
#include <errhandlingapi.h>
#include <string>
#include <windows.h>
#include <iostream>
#include <winnt.h>
#include <winreg.h>
#include <vector>
#include <psapi.h>
#include <imagehlp.h>
#include <winternl.h>
bool dirExists(const std::wstring& path) {
DWORD attrs = GetFileAttributesW(path.c_str());
return (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY));
}
struct Export{
std::wstring dll;
std::string name;
};
bool exportExists(std::wstring module, std::string exportName) {
HMODULE hModule = GetModuleHandleW(module.c_str());
if (hModule) {
FARPROC proc = GetProcAddress(hModule, exportName.c_str());
if (proc) {
return true;
}
}
return false;
}
bool FileExists(const std::wstring& filePath) {
DWORD attributes = GetFileAttributesW(filePath.c_str());
if (attributes != INVALID_FILE_ATTRIBUTES && !(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
return true;
}
return false;
}
bool ProcessExists(const std::wstring& processName) {
DWORD processes[1024];
DWORD bytesReturned;
if (!EnumProcesses(processes, sizeof(processes), &bytesReturned)) {
std::cerr << "EnumProcesses failed. Error code: " << GetLastError() << std::endl;
return false;
}
DWORD processCount = bytesReturned / sizeof(DWORD);
for (DWORD i = 0; i < processCount; ++i) {
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processes[i]);
if (hProcess) {
WCHAR processFileName[MAX_PATH];
if (GetModuleFileNameExW(hProcess, NULL, processFileName, MAX_PATH)) {
std::wstring currentProcessName = processFileName;
size_t pos = currentProcessName.find_last_of(L"\\/");
if (pos != std::wstring::npos) {
currentProcessName = currentProcessName.substr(pos + 1);
}
if (_wcsicmp(currentProcessName.c_str(), processName.c_str()) == 0) {
CloseHandle(hProcess);
return true;
}
}
CloseHandle(hProcess);
}
}
return false;
}
bool ServiceExists(const std::wstring& serviceName) {
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
if (hSCManager == NULL) {
std::cerr << "OpenSCManager failed. Error code: " << GetLastError() << std::endl;
return false;
}
DWORD bytesNeeded = 0;
DWORD servicesReturned = 0;
DWORD resumeHandle = 0;
std::vector<BYTE> buffer;
// Get the required buffer size
EnumServicesStatusExW(
hSCManager,
SC_ENUM_PROCESS_INFO,
SERVICE_WIN32,
SERVICE_STATE_ALL,
NULL,
0,
&bytesNeeded,
&servicesReturned,
&resumeHandle,
NULL
);
buffer.resize(bytesNeeded);
// Get the service information
if (!EnumServicesStatusExW(
hSCManager,
SC_ENUM_PROCESS_INFO,
SERVICE_WIN32,
SERVICE_STATE_ALL,
buffer.data(),
bytesNeeded,
&bytesNeeded,
&servicesReturned,
&resumeHandle,
NULL
)) {
std::cerr << "EnumServicesStatusExW failed. Error code: " << GetLastError() << std::endl;
CloseServiceHandle(hSCManager);
return false;
}
// Check if the specified service exists
ENUM_SERVICE_STATUS_PROCESSW* services = reinterpret_cast<ENUM_SERVICE_STATUS_PROCESSW*>(buffer.data());
for (DWORD i = 0; i < servicesReturned; ++i) {
if (serviceName == services[i].lpServiceName) {
CloseServiceHandle(hSCManager);
return true;
}
}
CloseServiceHandle(hSCManager);
return false;
}
bool KeyExists(HKEY hKeyRoot, const std::wstring& subKey) {
HKEY hKey;
LONG result = RegOpenKeyExW(hKeyRoot, subKey.c_str(), 0, KEY_READ, &hKey);
if (result == ERROR_SUCCESS) {
RegCloseKey(hKey);
return true;
} else {
return false;
}
}
Detect Detector::registryTest() {
Detect detect;
detect.name = "Registry";
detect.score = 20;
detect.detected = false;
if (KeyExists(HKEY_LOCAL_MACHINE, L"Software\\Wow6432Node\\Wine")) {
detect.detected = true;
// return detect;
}
if (KeyExists(HKEY_CURRENT_USER, L"Software\\Wine")) {
detect.detected = true;
// return detect;
}
this->totalScore += detect.score;
this->score += detect.detected ? detect.score : 0;
return detect;
}
Detect Detector::drivesTest() {
Detect detect;
detect.name = "Drives";
detect.score = 10;
detect.detected = false;
DWORD bufferSize = 1024; // Adjust as needed
WCHAR buffer[1024];
// Retrieve the drive strings
DWORD result = GetLogicalDriveStringsW(bufferSize, buffer);
if (result == 0) {
std::cerr << "Failed to get logical drive strings. Error code: " << GetLastError() << std::endl;
return detect;
}
// Print all drive strings
WCHAR* drive = buffer;
while (*drive) {
// std::wcout << L"Drive: " << drive << std::endl;
std::wstring driveStr(drive);
if (driveStr[0] == L'Z') {
std::wstring path = driveStr + L"bin";
if (dirExists(path)) {
detect.detected = true;
break;
}
}
drive += wcslen(drive) + 1;
}
this->totalScore += detect.score;
this->score += detect.detected ? detect.score : 0;
return detect;
}
Detect Detector::unimplementedTest() {
Detect detect;
detect.name = "Unimplemented APIs";
detect.score = 20;
detect.detected = false;
HANDLE hHeap = GetProcessHeap();
NTSTATUS status = RtlSetHeapInformation(
hHeap,
(HEAP_INFORMATION_CLASS)-133713371337, // Wine returns success, but it doesn't work: https://github.com/wine-mirror/wine/blob/3a736901cdd588ba7fbb4318e5f5069793268a01/dlls/ntdll/heap.c#L2607
NULL,
0
); // TODO: Gaming OS also returns success :/
if (status == 0) {
detect.detected = true;
}
this->totalScore += detect.score;
this->score += detect.detected ? detect.score : 0;
return detect;
}
Detect Detector::servicesTest() {
Detect detect;
detect.name = "Services";
detect.score = 20;
detect.detected = ServiceExists(L"Winedevice1");
this->totalScore += detect.score;
this->score += detect.detected ? detect.score : 0;
return detect;
}
Detect Detector::dllExportTest() {
// References:
// https://www.reddit.com/r/linux_gaming/comments/1f2jsgy/comment/lkajyvh/
// https://www.hexacorn.com/blog/2016/03/27/detecting-wine-via-internal-and-legacy-apis/
// https://github.com/wine-mirror/wine/blob/master/dlls/winecrt0/debug.c
Detect detect;
detect.name = "DLL Exports";
detect.score = 30;
detect.detected = false;
std::vector<Export> exports = {
Export{L"ntdll.dll", "wine_get_version"},
Export{L"ntdll.dll", "wine_get_host_version"},
Export{L"ntdll.dll", "wine_server_call"},
Export{L"ntdll.dll", "__wine_dbg_output"},
Export{L"ntdll.dll", "__wine_dbg_strdup"},
Export{L"ntdll.dll", "__wine_dbg_get_channel_flags"},
};
for(auto& e : exports) {
if (exportExists(e.dll, e.name)) {
detect.detected = true;
// break;
}
}
this->totalScore += detect.score;
this->score += detect.detected ? detect.score : 0;
return detect;
}
Detect Detector::legacyApiTest() {
Detect detect;
detect.name = "Legacy APIs";
detect.score = 20;
detect.detected = false;
if (exportExists(L"kernel32.dll", "RegisterServiceProcess")) {
detect.detected = true;
}
this->totalScore += detect.score;
this->score += detect.detected ? detect.score : 0;
return detect;
}
Detect Detector::muldivTest() {
// https://www.reddit.com/r/linux_gaming/comments/1f2jsgy/comment/lkajyvh/ but it doesn't work
Detect detect;
detect.name = "MulDiv";
detect.score = 20;
detect.detected = MulDiv(1, 0x80000000, 0x80000000) != 2;
// std::cout << MulDiv(1, 0x80000000, 0x80000000);
this->totalScore += detect.score;
this->score += detect.detected ? detect.score : 0;
return detect;
}
Detect Detector::processTest() {
Detect detect;
detect.name = "Process";
detect.score = 1;
detect.detected = ProcessExists(L"winedevice.exe");
this->totalScore += detect.score;
this->score += detect.detected ? detect.score : 0;
return detect;
}
Detect Detector::filesTest() {
Detect detect;
detect.name = "Files";
detect.score = 1;
detect.detected = FileExists(L"C:\\windows\\syswow64\\wineboot.exe");
this->totalScore += detect.score;
this->score += detect.detected ? detect.score : 0;
return detect;
}