forked from ANSSI-FR/ORADAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filters.cpp
366 lines (319 loc) · 7.64 KB
/
Filters.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
#include <Windows.h>
#include <Sddl.h>
#include <stdio.h>
#include "ORADAD.h"
#include "Constants.h"
extern HANDLE g_hHeap;
extern BOOL g_bSupportsAnsi;
#define NEVER_VALUE_1 9223372036854775807 // cf. https://docs.microsoft.com/en-us/windows/win32/adschema/a-accountexpires
#define NEVER_VALUE_2 9223372036854775808
#define STR_UNABLE_CONVERT_SID L"Unable to convert SID"
#define STR_NEVER L"Never"
//
// Filter functions
//
_Success_(return)
BOOL
pFilterFlagsType(
_In_ PVOID pvData,
_In_ PVOID pvParam,
_Outptr_ LPWSTR *szResult
);
BOOL
pFilterSid(
_In_ PVOID pvData,
_In_ PVOID pvParam,
_Outptr_ LPWSTR *szResult
);
BOOL
pFilterFiletime(
_In_ PVOID pvData,
_In_ PVOID pvParam,
_Out_ LPWSTR *szResult
);
BOOL
pFilterNegFiletime(
_In_ PVOID pvData,
_In_ PVOID pvParam,
_Out_ LPWSTR *szResult
);
//
// Private functions
//
LPWSTR
pHeapAllocAndCopyString(
_In_z_ LPCWSTR szString
);
BOOL
GetFilter (
_Inout_ PATTRIBUTE_CONFIG pAttributes,
_In_z_ LPCWSTR szFilter
)
{
if (_wcsicmp(szFilter, L"userAccountControl") == 0)
{
pAttributes->fFilter = (tFilter)pFilterFlagsType;
pAttributes->pvFilterData = cUserAccountControl;
}
else if (_wcsicmp(szFilter, L"supportedEncryptionTypes") == 0)
{
pAttributes->fFilter = (tFilter)pFilterFlagsType;
pAttributes->pvFilterData = cSupportedEncryptionTypes;
}
else if (_wcsicmp(szFilter, L"groupType") == 0)
{
pAttributes->fFilter = (tFilter)pFilterFlagsType;
pAttributes->pvFilterData = cGroupType;
}
else if (_wcsicmp(szFilter, L"trustAttributes") == 0)
{
pAttributes->fFilter = (tFilter)pFilterFlagsType;
pAttributes->pvFilterData = cTrustAttributes;
}
else if (_wcsicmp(szFilter, L"trustDirection") == 0)
{
pAttributes->fFilter = (tFilter)pFilterFlagsType;
pAttributes->pvFilterData = cTrustDirection;
}
else if (_wcsicmp(szFilter, L"trustType") == 0)
{
pAttributes->fFilter = (tFilter)pFilterFlagsType;
pAttributes->pvFilterData = cTrustType;
}
else if (_wcsicmp(szFilter, L"systemFlags") == 0)
{
pAttributes->fFilter = (tFilter)pFilterFlagsType;
pAttributes->pvFilterData = cSystemFlags;
}
else if (_wcsicmp(szFilter, L"searchFlags") == 0)
{
pAttributes->fFilter = (tFilter)pFilterFlagsType;
pAttributes->pvFilterData = cSearchFlags;
}
else if (_wcsicmp(szFilter, L"schemaFlagsEx") == 0)
{
pAttributes->fFilter = (tFilter)pFilterFlagsType;
pAttributes->pvFilterData = cSchemaFlagsEx;
}
else if (_wcsicmp(szFilter, L"sid") == 0)
{
pAttributes->fFilter = (tFilter)pFilterSid;
pAttributes->pvFilterData = NULL;
}
else if (_wcsicmp(szFilter, L"Filetime") == 0)
{
pAttributes->fFilter = (tFilter)pFilterFiletime;
pAttributes->pvFilterData = NULL;
}
else if (_wcsicmp(szFilter, L"NegFiletime") == 0)
{
pAttributes->fFilter = (tFilter)pFilterNegFiletime;
pAttributes->pvFilterData = NULL;
}
else
{
Log(
__FILE__, __FUNCTION__, __LINE__, LOG_LEVEL_ERROR,
"[!] %sUnknown filter%s '%S'.",
COLOR_RED, COLOR_RESET, szFilter
);
return FALSE;
}
return TRUE;
}
LPWSTR
ApplyFilter (
_In_ PATTRIBUTE_CONFIG pAttributes,
_In_z_ PVOID pvData
)
{
BOOL bResult;
LPWSTR szResult = NULL;
if (pvData == NULL)
return NULL;
if (pAttributes->fFilter == NULL)
return NULL;
bResult = pAttributes->fFilter(pvData, pAttributes->pvFilterData, &szResult);
if (bResult == FALSE)
{
_SafeHeapRelease(szResult);
return NULL;
}
else
return szResult;
}
//
// Filter functions
//
_Success_(return)
BOOL
pFilterFlagsType (
_In_ PVOID pvData,
_In_ PVOID pvParam,
_Outptr_ LPWSTR *szResult
)
{
BOOL bFirst = TRUE;
DWORD dwValue;
DWORD dwRest;
LPWSTR szOut;
PCONST_TXT pFlagsList;
DWORD dwFilterMode;
szOut = (LPWSTR)_HeapAlloc(CONST_MAX_SIZE * sizeof(WCHAR));
if (szOut == NULL)
return FALSE;
dwValue = *(PDWORD)pvData;
dwRest = dwValue;
pFlagsList = (PCONST_TXT)pvParam;
//
// Pass 1: Get filter Mode
//
pFlagsList = (PCONST_TXT)pvParam;
while (pFlagsList->szTxt)
{
pFlagsList++;
}
dwFilterMode = pFlagsList->dwConst;
//
// Pass 2: Apply filter
//
pFlagsList = (PCONST_TXT)pvParam;
switch (dwFilterMode)
{
case FILTER_FLAG:
{
while (pFlagsList->szTxt)
{
if (pFlagsList->dwConst & dwValue)
{
if (bFirst)
bFirst = FALSE;
else
wcscat_s(szOut, CONST_MAX_SIZE, L" | ");
wcscat_s(szOut, CONST_MAX_SIZE, pFlagsList->szTxt);
dwRest &= ~pFlagsList->dwConst;
}
pFlagsList++;
}
//
// Add remaining flags
//
if (dwRest)
{
WCHAR szRest[SIZE_NUMBER_TXT];
swprintf_s(szRest, SIZE_NUMBER_TXT, L"%lu", dwRest);
if (bFirst == FALSE)
wcscat_s(szOut, CONST_MAX_SIZE, L" | ");
wcscat_s(szOut, CONST_MAX_SIZE, szRest);
}
}
break;
case FILTER_TYPE:
{
while (pFlagsList->szTxt)
{
if (pFlagsList->dwConst == dwValue)
{
wcscpy_s(szOut, CONST_MAX_SIZE, pFlagsList->szTxt);
break;
}
pFlagsList++;
}
}
break;
}
*szResult = szOut;
return TRUE;
}
BOOL
pFilterSid (
_In_ PVOID pvData,
_In_ PVOID pvParam,
_Outptr_ LPWSTR *szResult
)
{
UNREFERENCED_PARAMETER(pvParam);
BOOL bResult;
LPWSTR szSid;
bResult = ConvertSidToStringSid(pvData, &szSid);
if (bResult == TRUE)
{
*szResult = pHeapAllocAndCopyString(szSid);
LocalFree(szSid);
}
else
{
*szResult = pHeapAllocAndCopyString(STR_UNABLE_CONVERT_SID);
}
return TRUE;
}
BOOL
pFilterFiletime (
_In_ PVOID pvData,
_In_ PVOID pvParam,
_Out_ LPWSTR *szResult
)
{
UNREFERENCED_PARAMETER(pvParam);
LONGLONG llValue;
*szResult = NULL;
llValue = *(PLONGLONG)pvData;
if (llValue <= 0)
*szResult = NULL;
else if ((llValue == NEVER_VALUE_1) || (llValue == NEVER_VALUE_2))
*szResult = pHeapAllocAndCopyString(STR_NEVER);
else
{
LONGLONG llDay;
LONG lHour;
LONG lMinute;
LONG lSecond;
llValue = llValue / 10000000;
llDay = llValue / 86400;
llValue = llValue - (llDay * 86400);
lHour = (LONG)(llValue / 3600);
llValue = llValue - ((LONGLONG)lHour * 3600);
lMinute = (LONG)(llValue / 60);
llValue = llValue - ((LONGLONG)lMinute * 60);
lSecond = (LONG)(llValue / 60);
llValue = llValue - ((LONGLONG)lSecond * 60);
*szResult = (LPWSTR)_HeapAlloc((25 + 1) * sizeof(WCHAR));
if (*szResult != NULL)
{
swprintf_s(*szResult, 25, L"%llu:%02u:%02u:%02u", llDay, lHour, lMinute, lSecond);
}
}
return TRUE;
}
BOOL
pFilterNegFiletime (
_In_ PVOID pvData,
_In_ PVOID pvParam,
_Out_ LPWSTR *szResult
)
{
UNREFERENCED_PARAMETER(pvParam);
LONGLONG llValue;
llValue = -(*(PLONGLONG)pvData);
pFilterFiletime(&llValue, pvParam, szResult);
return TRUE;
}
//
// Private functions
//
LPWSTR
pHeapAllocAndCopyString (
_In_z_ LPCWSTR szString
)
{
LPWSTR szReturn;
size_t SizeString;
SizeString = wcslen(szString);
SizeString++; // NULL char
szReturn = (LPWSTR)_HeapAlloc(SizeString * sizeof(WCHAR));
if (szReturn != NULL)
{
wcscpy_s(szReturn, SizeString, szString);
}
return szReturn;
}