-
Notifications
You must be signed in to change notification settings - Fork 45
/
detect.c
226 lines (188 loc) · 4.52 KB
/
detect.c
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
#include "detect.h"
#include "util.h"
BOOL
wsb_detect_state_dev(
VOID
)
{
UNICODE_STRING usPath;
usPath.Buffer = SANDBOX_STATE_DEV;
usPath.Length = sizeof(SANDBOX_STATE_DEV);
OBJECT_ATTRIBUTES oaDev;
InitializeObjectAttributes(&oaDev, &usPath, OBJ_CASE_INSENSITIVE, NULL, NULL);
HANDLE hDev;
if (SUCCEEDED(NtCreateFile(&hDev, MAXIMUM_ALLOWED, &oaDev, NULL, NULL, 0, 0, 0,
FILE_OPEN_FOR_BACKUP_INTENT, NULL, 0)))
{
CloseHandle(hDev);
return TRUE;
}
return FALSE;
}
BOOL
wsb_detect_username(
VOID
)
{
WCHAR wcUser[UNLEN + 1];
RtlSecureZeroMemory(wcUser, sizeof(wcUser));
DWORD dwLength = (UNLEN + 1);
if (GetUserNameW(wcUser, &dwLength))
{
return (wcscmp(wcUser, SANDBOX_USER) == 0);
}
return FALSE;
}
BOOL
wsb_detect_proc(
VOID
)
{
BOOL bFound = FALSE;
HANDLE hProcesses = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcesses == INVALID_HANDLE_VALUE)
{
return FALSE;
}
PROCESSENTRY32 pe32Entry;
pe32Entry.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hProcesses, &pe32Entry))
{
CloseHandle(hProcesses);
return FALSE;
}
do
{
if (wcscmp(pe32Entry.szExeFile, HV_CONTAINER_NAME) == 0)
{
bFound = TRUE;
break;
}
}
while (Process32Next(hProcesses, &pe32Entry));
CloseHandle(hProcesses);
return bFound;
}
BOOL
wsb_detect_suffix(
VOID
)
{
BOOL bFound = FALSE;
DWORD dwAdapterFlags = 0;
DWORD dwAddrSize = 0;
if (GetAdaptersAddresses(AF_INET, dwAdapterFlags, NULL, NULL, &dwAddrSize) == 0)
{
return FALSE;
}
PIP_ADAPTER_ADDRESSES pAdapterAddrs;
PIP_ADAPTER_ADDRESSES pAdapt;
if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES)GlobalAlloc(GPTR, dwAddrSize)) == NULL)
{
return FALSE;
}
if (GetAdaptersAddresses(AF_INET, dwAdapterFlags, NULL, pAdapterAddrs, &dwAddrSize) != ERROR_SUCCESS)
{
GlobalFree(pAdapterAddrs);
return FALSE;
}
for (pAdapt = pAdapterAddrs; pAdapt; pAdapt = pAdapt->Next)
{
if (wcscmp(pAdapt->DnsSuffix, SANDBOX_DNS_SUFFIX) == 0)
{
bFound = TRUE;
break;
}
}
GlobalFree(pAdapterAddrs);
return bFound;
}
BOOL
wsb_detect_dev(
VOID
)
{
return util_path_exists(HV_VMSMB_DEV, 0);
}
BOOL wsb_detect_genuine(VOID)
{
GUID spUID;
RtlSecureZeroMemory(&spUID, sizeof(spUID));
RPC_WSTR spRPC = (RPC_WSTR)L"55c92734-d682-4d71-983e-d6ec3f16059f";
if (UuidFromString(spRPC, &spUID) != RPC_S_OK)
{
return FALSE;
}
SL_GENUINE_STATE slState;
if (SLIsGenuineLocal(&spUID, &slState, NULL) != S_OK)
{
return FALSE;
}
return (slState != SL_GEN_STATE_IS_GENUINE);
}
BOOL
wsb_detect_cmd(
VOID
)
{
BOOL bFound = FALSE;
DWORD dwFlags = KEY_READ;
#ifndef _WIN64
dwFlags &= KEY_WOW64_64KEY;
#endif
HKEY hKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"), 0, dwFlags, &hKey) != ERROR_SUCCESS)
{
return bFound;
}
WCHAR achKey[MAX_KEY_LENGTH];
WCHAR achValue[MAX_VALUE_NAME];
RtlSecureZeroMemory(achKey, sizeof(achKey));
RtlSecureZeroMemory(achValue, sizeof(achValue));
DWORD cchKey = MAX_KEY_LENGTH;
DWORD cchValue = MAX_VALUE_NAME;
// we don't know the exact key, so just get the value at pos 0
if (RegEnumValue(hKey, 0, achKey, &cchKey, NULL, NULL, (LPBYTE)achValue, &cchValue) == ERROR_SUCCESS)
{
if (wcscmp(achValue, SANDBOX_LOGON_CMD) == 0)
{
bFound = TRUE;
}
}
RegCloseKey(hKey);
return bFound;
}
BOOL
wsb_detect_time(
VOID
)
{
BOOL bReturn = FALSE;
WCHAR wcPath[MAX_PATH + 1];
RtlSecureZeroMemory(wcPath, sizeof(wcPath));
if (GetSystemDirectory(wcPath, MAX_PATH) == 0)
{
return bReturn;
}
if (wsprintf(wcPath, SANDBOX_MOUNT_DRIV_FMT, wcPath) == 0)
{
return bReturn;
}
HANDLE hFile = CreateFile(wcPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return bReturn;
}
FILETIME ftCreate;
if (GetFileTime(hFile, &ftCreate, NULL, NULL) == 0)
{
CloseHandle(hFile);
return bReturn;
}
if (ftCreate.dwLowDateTime == SANDBOX_TS_LOWER && ftCreate.dwHighDateTime == SANDBOX_TS_HIGHER)
{
bReturn = TRUE;
}
CloseHandle(hFile);
return bReturn;
}