-
Notifications
You must be signed in to change notification settings - Fork 18
/
ASRenum.cpp
392 lines (334 loc) · 12 KB
/
ASRenum.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
#define _WIN32_DCOM
#include <comdef.h>
#include <wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")
static unsigned short int step = 1;
int main(int iArgCnt, char** argv) {
wchar_t * guidRule[] = {
L"Block abuse of exploited vulnerable signed drivers",
L"Block Adobe Reader from creating child processes",
L"Block all Office applications from creating child processes",
L"Block credential stealing from the Windows local security authority subsystem (lsass.exe)",
L"Block executable content from email client and webmail",
L"Block executable files from running unless they meet a prevalence, age, or trusted list criterion",
L"Block execution of potentially obfuscated scripts",
L"Block JavaScript or VBScript from launching downloaded executable content",
L"Block Office applications from creating executable content",
L"Block Office applications from injecting code into other processes",
L"Block Office communication application from creating child processes",
L"Block persistence through WMI event subscription",
L"Block process creations originating from PSExec and WMI commands",
L"Block untrusted and unsigned processes that run from USB",
L"Block Win32 API calls from Office macros",
L"Use advanced protection against ransomware"};
wchar_t * guidID[] = {
L"56a863a9-875e-4185-98a7-b882c64b5ce5",
L"7674ba52-37eb-4a4f-a9a1-f0f9a1619a2c",
L"d4f940ab-401b-4efc-aadc-ad5f3c50688a",
L"9e6c4e1f-7d60-472f-ba1a-a39ef669e4b2",
L"be9ba2d9-53ea-4cdc-84e5-9b1eeee46550",
L"01443614-cd74-433a-b99e-2ecdc07bfc25",
L"5beb7efe-fd9a-4556-801d-275e5ffc04cc",
L"d3e037e1-3eb8-44c8-a917-57927947596d",
L"3b576869-a4ec-4529-8536-b80a7769e899",
L"75668c1f-73b5-4cf0-bb93-3ecf5cb7cc84",
L"26190899-1602-49e8-8b27-eb1d0a1ce869",
L"e6db77e5-3df2-4cf1-b95a-636979351e5b",
L"d1e49aac-8f56-4280-b9ba-993a6d77406c",
L"b2b3f03d-6a65-4f7b-a9c7-1c7ef74a9ba4",
L"92e97fa1-2edf-4476-bdd6-9dd0b4dddc7b",
L"c1db55ab-c21a-4637-bb3f-a12568109d35"};
wchar_t * rules[16];
int orderNumber[16] = {NULL};
int len = sizeof(guidID)/sizeof(guidID[0]);
HRESULT hres;
// Initialize COM
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres)) {
printf("[!] Failed to initialize COM library. Error code = %#x\n", hres);
return 1;
}
// Initialize COM process security
hres = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
NULL
);
if (FAILED(hres)) {
printf("[!] Failed to initialize security. Error code = %#x\n", hres);
CoUninitialize();
return 1;
}
// Obtain the initial locator to WMI
IWbemLocator* pLoc = NULL;
hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLoc);
if (FAILED(hres)) {
printf("[!] Failed to create IWbemLocator object. Err code = %#x\n", hres);
CoUninitialize();
return 1;
}
// Connect to the local root\cimv2 namespace through the IWbemLocator::ConnectServer method and obtain pointer pSvc to make IWbemServices calls.
IWbemServices* pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\Microsoft\\Windows\\Defender"),
NULL,
NULL,
0,
NULL,
0,
0,
&pSvc
);
if (FAILED(hres)) {
printf("[!] Could not connect. Error code = %#x\n", hres);
pLoc->Release();
CoUninitialize();
return 1;
}
printf("[*] Connected to ROOT\\Microsoft\\Windows\\Defender\n");
// Set security levels on the proxy so the WMI service can impersonate the client
hres = CoSetProxyBlanket(
pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if (FAILED(hres)) {
printf("[!] Could not set proxy blanket. Error code = %#x\n", hres);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
// Use the IWbemServices pointer to make requests of WMI
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("Select * from MSFT_MpPreference"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres)) {
printf("[-] Query for MpPrefence failed with = 0x%#x\n", hres);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
// Get the data from the query
IWbemClassObject* pclsObj = NULL;
ULONG uReturn = 0;
unsigned long ulIndex = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (0 == uReturn)
{
break;
}
VARIANT vtProp;
hr = pclsObj->Get(L"AttackSurfaceReductionRules_Ids", 0, &vtProp, 0, 0);
printf("\n[*] ASR Rules: \n");
printf(" ==============================================================================================================\n");
printf(" | Action | Rule\t\t\t\t\t\t\t\t\t\t\t\t |\n");
printf(" ==============================================================================================================\n");
step++;
if (!FAILED(hr))
{
if (!((vtProp.vt == VT_NULL) || (vtProp.vt == VT_EMPTY)))
{
if ((vtProp.vt & VT_ARRAY))
{
long lower, upper;
BSTR Element;
SAFEARRAY* pSafeArray = vtProp.parray;
step++;
hres = SafeArrayGetLBound(pSafeArray, 1, &lower);
if (FAILED(hres))
{
// Cleanup regimen
}
else
{
step++;
}
SafeArrayGetUBound(pSafeArray, 1, &upper);
if (FAILED(hres))
{
// Cleanup regimen
}
else
{
step++;
}
for (long i = lower; i <= upper; i++)
{
hres = SafeArrayGetElement(pSafeArray, &i, &Element);
if (FAILED(hres))
{
break;
}
else
{
++ulIndex;
unsigned int j;
for(j = 0; j < len; ++j) {
if (wcscmp(guidID[j], Element) == 0) {
orderNumber[i] = j;
rules[j] = guidRule[j];
}
}
}
}
pSafeArray = NULL;
}
}
}
VariantClear(&vtProp);
hr = pclsObj->Get(L"AttackSurfaceReductionRules_Actions", 0, &vtProp, 0, 0);
step++;
if (!FAILED(hr))
{
if (!((vtProp.vt == VT_NULL) || (vtProp.vt == VT_EMPTY)))
{
if ((vtProp.vt & VT_ARRAY))
{
long lower, upper;
BSTR Element;
SAFEARRAY* pSafeArray = vtProp.parray;
step++;
hres = SafeArrayGetLBound(pSafeArray, 1, &lower);
if (FAILED(hres))
{
// Cleanup regimen
}
else
{
step++;
}
SafeArrayGetUBound(pSafeArray, 1, &upper);
if (FAILED(hres))
{
// Cleanup regimen
}
else
{
step++;
}
for (long i = lower; i <= upper; i++)
{
hres = SafeArrayGetElement(pSafeArray, &i, &Element);
if (FAILED(hres))
{
break;
}
else
{
++ulIndex;
for (int j = 0; j < 16; j++){
if (orderNumber[j] != NULL) {
if (int(Element) == 1){
printf(" BLOCK | %S\n", rules[orderNumber[i]]);
break;
}
if (int(Element) == 2) {
printf(" AUDIT | %S\n", rules[orderNumber[i]]);
break;
}
if(int(Element) == 6) {
printf(" WARN | %S\n", rules[orderNumber[i]]);
break;
}
if(int(Element) == 0) {
printf(" DISABLED | %S\n", rules[orderNumber[i]]);
break;
}
}
}
}
}
pSafeArray = NULL;
}
}
}
VariantClear(&vtProp);
hr = pclsObj->Get(L"AttackSurfaceReductionOnlyExclusions", 0, &vtProp, 0, 0);
printf("\n[*] ASR Exclusions: \n");
step++;
if (!FAILED(hr))
{
if (!((vtProp.vt == VT_NULL) || (vtProp.vt == VT_EMPTY)))
{
if ((vtProp.vt & VT_ARRAY))
{
long lower, upper;
BSTR Element;
SAFEARRAY* pSafeArray = vtProp.parray;
step++;
hres = SafeArrayGetLBound(pSafeArray, 1, &lower);
if (FAILED(hres))
{
// Cleanup regimen
}
else
{
step++;
}
SafeArrayGetUBound(pSafeArray, 1, &upper);
if (FAILED(hres))
{
// Cleanup regimen
}
else
{
step++;
}
for (long i = lower; i <= upper; i++)
{
hres = SafeArrayGetElement(pSafeArray, &i, &Element);
if (FAILED(hres))
{
break;
}
else
{
++ulIndex;
printf(" %S\n", (wchar_t*)Element);
}
}
pSafeArray = NULL;
}
}
}
VariantClear(&vtProp);
}
if (pEnumerator != NULL)
{
pEnumerator->Reset();
pEnumerator = NULL;
step++;
}
if (pSvc != NULL)
{
pSvc->Release();
step++;
}
if (pLoc != NULL)
{
pLoc->Release();
step++;
}
CoUninitialize();
step++;
return 0;
}