-
Notifications
You must be signed in to change notification settings - Fork 31
/
Util.c
380 lines (337 loc) · 9.66 KB
/
Util.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
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
#include "VSTriage.h"
extern PNODE pnReportHead; // global variable - pointer to head node.
int cmp(const void* a, const void* b)
{
const wchar_t** ia = (const wchar_t**)a;
const wchar_t** ib = (const wchar_t**)b;
if ((wcslen(*ia) > 0) && (wcslen(*ib) > 0))
{
if ((*ia[0] == L'\t') && (*ib[0] != L'\t'))
{
return 1;
}
if ((*ia[0] != L'\t') && (*ib[0] == L'\t'))
{
return -1;
}
}
return _wcsicmp(*ia, *ib);
}
VOID SortUniqueMultilineWchar(PWSTR pwszStringToSort, PWSTR* pwszSortedString)
{
PWSTR* ppwstrLines;
DWORD dwCurrentLineNumber = 0;
wchar_t* wcToken;
wchar_t* next_token = NULL;
wchar_t seps[] = L"\r\n";
PWSTR pwszNewString;
size_t stStringSizeBytes;
DWORD dwMaxLines = SIZE_1KB;
stStringSizeBytes = LocalSize(pwszStringToSort);
ALLOCORCRASH(ppwstrLines, sizeof(PWSTR) * dwMaxLines);
ALLOCORCRASH(pwszNewString, stStringSizeBytes);
wcToken = wcstok_s(pwszStringToSort, seps, &next_token);
while (NULL != wcToken)
{
ppwstrLines[dwCurrentLineNumber++] = wcToken;
wcToken = wcstok_s(NULL, seps, &next_token);
if (dwCurrentLineNumber == dwMaxLines)
{
//too many lines. reallocate
PWSTR* ppwstrNewLines;
dwMaxLines = 2 * dwMaxLines;
ALLOCORCRASH(ppwstrNewLines, sizeof(PWSTR) * dwMaxLines);
memcpy_s(ppwstrNewLines, LocalSize(ppwstrNewLines), ppwstrLines, LocalSize(ppwstrLines));
LocalFree(ppwstrLines);
ppwstrLines = ppwstrNewLines;
}
}
if (dwCurrentLineNumber < 2) //no touch
{
LocalFree(ppwstrLines);
wmemcpy_s(pwszNewString, stStringSizeBytes / sizeof(WCHAR), pwszStringToSort, wcslen(pwszStringToSort));
if (1 == dwCurrentLineNumber)
{
StringCchCatW(pwszNewString, stStringSizeBytes / sizeof(WCHAR), L"\r\n");
}
*pwszSortedString = pwszNewString;
return;
}
qsort(ppwstrLines, dwCurrentLineNumber, sizeof(PWSTR), cmp);
// add line 0
StringCchCatW(pwszNewString, stStringSizeBytes / sizeof(WCHAR), ppwstrLines[0]);
StringCchCatW(pwszNewString, stStringSizeBytes / sizeof(WCHAR), L"\r\n");
// add lines from 1, if != previous.
for (DWORD i = 1; i < dwCurrentLineNumber; i++)
{
if (0 != _wcsicmp(ppwstrLines[i - 1], ppwstrLines[i]))
{
StringCchCatW(pwszNewString, stStringSizeBytes / sizeof(WCHAR), ppwstrLines[i]);
StringCchCatW(pwszNewString, stStringSizeBytes / sizeof(WCHAR), L"\r\n");
}
}
*pwszSortedString = pwszNewString;
LocalFree(ppwstrLines);
}
VOID ResizeWcharBufIfNeeded(PWSTR* pwszBuffer, size_t* pstBufferSizeBytes)
{
//check if we have 50% of buffer. If yes - double the buffer.
if (wcslen(*pwszBuffer) > *pstBufferSizeBytes / (2 * sizeof(WCHAR)))
{
PWSTR buf2;
*pstBufferSizeBytes *= 2;
buf2 = LocalAlloc(LPTR, *pstBufferSizeBytes);
CRASHIFNULLALLOC(buf2);
wmemcpy_s(buf2, wcslen(*pwszBuffer), *pwszBuffer, wcslen(*pwszBuffer));
LocalFree(*pwszBuffer);
*pwszBuffer = buf2;
}
}
VOID ShrinkWcharBuffer(PWSTR* pwszBuffer)
{
if (NULL == pwszBuffer)
{
return; //nothing to do
}
PWSTR buf1;
size_t stLenWchars;
size_t stMaxLenBytes;
stMaxLenBytes = LocalSize(*pwszBuffer);
stLenWchars = wcsnlen(*pwszBuffer, stMaxLenBytes / sizeof(WCHAR));
buf1 = LocalAlloc(LPTR, stLenWchars * sizeof(WCHAR) + 2);
CRASHIFNULLALLOC(buf1);
if (stLenWchars > 0)
{
errno_t iRet;
iRet = wmemcpy_s(buf1, stLenWchars + 1, *pwszBuffer, stLenWchars);
if (iRet)
//cannot copy for some reason. the best thing is to return original buffer. It wastes the memory but works.
{
LocalFree(buf1);
return;
}
}
LocalFree(*pwszBuffer);
*pwszBuffer = buf1;
}
VOID AddCheckHeader(PWSTR pwszBuffer, size_t stBufferSizeBytes, PWSTR pwszHeaderContent, BOOL bStartWithCRLF)
{
PWSTR pwszTimeStr;
HRESULT hrStr;
pwszTimeStr = GetCurrentTimeZ();
if (bStartWithCRLF)
{
hrStr = StringCchCatW(pwszBuffer, stBufferSizeBytes / sizeof(WCHAR), L"\r\n\r\n");
CHECKSTRINGHR(hrStr);
}
hrStr = StringCchCatW(pwszBuffer, stBufferSizeBytes / sizeof(WCHAR), L"====");
CHECKSTRINGHR(hrStr);
hrStr = StringCchCatW(pwszBuffer, stBufferSizeBytes / sizeof(WCHAR), pwszHeaderContent);
CHECKSTRINGHR(hrStr);
hrStr = StringCchCatW(pwszBuffer, stBufferSizeBytes / sizeof(WCHAR), L"==== ");
CHECKSTRINGHR(hrStr);
hrStr = StringCchCatW(pwszBuffer, stBufferSizeBytes / sizeof(WCHAR), pwszTimeStr);
CHECKSTRINGHR(hrStr);
hrStr = StringCchCatW(pwszBuffer, stBufferSizeBytes / sizeof(WCHAR), L"\r\n");
CHECKSTRINGHR(hrStr);
LocalFree(pwszTimeStr);
}
//Creates a new Node and returns pointer to it.
PNODE GetNewNode(PWSTR x)
{
PNODE newNode;
newNode = (PNODE)LocalAlloc(LPTR, sizeof(NODE));
CRASHIFNULLALLOC(newNode);
newNode->data = x;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
//Inserts a Node at head of doubly linked list
VOID InsertAtHead(PWSTR x)
{
PNODE newNode = GetNewNode(x);
if (pnReportHead == NULL)
{
pnReportHead = newNode;
return;
}
pnReportHead->prev = newNode;
newNode->next = pnReportHead;
pnReportHead = newNode;
}
//Inserts a Node at tail of Doubly linked list
VOID InsertAtOutputTail(PWSTR x)
{
PNODE temp = pnReportHead;
PNODE newNode = GetNewNode(x);
if (pnReportHead == NULL)
{
pnReportHead = newNode;
return;
}
while (temp->next != NULL)
{
temp = temp->next; // Go To last Node
}
temp->next = newNode;
newNode->prev = temp;
}
//Prints all the elements in linked list in forward traversal order
VOID PrintOutput(void)
{
PNODE temp = pnReportHead;
wprintf(L"Forward: ");
while (temp != NULL)
{
wprintf(L"%s ", temp->data);
temp = temp->next;
}
wprintf(L"\r\n");
}
//Prints all elements in linked list in reverse traversal order.
VOID ReversePrint(void)
{
PNODE temp = pnReportHead;
if (temp == NULL)
{
return; // empty list, exit
}
// Going to last Node
while (temp->next != NULL)
{
temp = temp->next;
}
// Traversing backward using prev pointer
wprintf(L"Reverse: ");
while (temp != NULL)
{
wprintf(L"%s ", temp->data);
temp = temp->prev;
}
wprintf(L"\r\n");
}
//Enables all privileges present in the token, one by one.
VOID EnableAllPrivileges(void) //or at least try...
{
HANDLE hToken;
BOOL bStatus;
DWORD dwLastError;
DWORD dwTokenPrivilegeBytes;
PTOKEN_PRIVILEGES pPrivs;
DWORD i;
WCHAR wszPrivilegeName[MAX_PRIVILEGE_NAME_LEN];
DWORD dwLen;
TOKEN_PRIVILEGES tp;
bStatus = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
if (!bStatus)
{
dwLastError = GetLastError();
wprintf(L"ERROR: OpenProcessToken() returned %u\r\n", dwLastError);
return;
}
//get the token information size, check if not zero
GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &dwTokenPrivilegeBytes);
if (0 == dwTokenPrivilegeBytes)
{
dwLastError = GetLastError();
wprintf(L"ERROR: GetTokenInformation() can't obtain token size. Error %u\r\n", dwLastError);
CloseHandle(hToken);
return;
}
//allocate buffer for storing token information
pPrivs = LocalAlloc(LPTR, dwTokenPrivilegeBytes);
if (NULL == pPrivs)
{
wprintf(L"ERROR: Cannot allocate buffer.\r\n");
CloseHandle(hToken);
return;
}
//put the token data to the buffer
bStatus = GetTokenInformation(hToken, TokenPrivileges, pPrivs, dwTokenPrivilegeBytes, &dwTokenPrivilegeBytes);
if (!bStatus)
{
dwLastError = GetLastError();
wprintf(L"ERROR: GetTokenInformation() returned %u\r\n", dwLastError);
CloseHandle(hToken);
LocalFree(pPrivs);
return;
}
//iterate through privileges
//I can do it with one AdjustTokenPrivileges() call but iterating through an array allows me to display names and catch failed attempts.
//the all-at-once approach is nicely demonstrated at https://docs.microsoft.com/en-us/windows/win32/wmisdk/executing-privileged-operations-using-c-
for (i = 0; i < pPrivs->PrivilegeCount; i++)
{
//wprintf(L"Enabling privilege 0x%02x ", pPrivs->Privileges[i].Luid.LowPart);
dwLen = MAX_PRIVILEGE_NAME_LEN;
LookupPrivilegeNameW(NULL, &(pPrivs->Privileges[i].Luid), wszPrivilegeName, &dwLen);
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = pPrivs->Privileges[i].Luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);
}
CloseHandle(hToken);
LocalFree(pPrivs);
}
//LocalFree required
PWSTR SystemTimeToISO8601(SYSTEMTIME stTime)
{
PWSTR pwszISOTimeZ;
//2022-05-02T07:34:56Z
// GetSystemTime(&stTime);
pwszISOTimeZ = LocalAlloc(LPTR, (ISO_TIME_LEN + 3) * sizeof(WCHAR));
if (pwszISOTimeZ)
{
StringCchPrintfW(
pwszISOTimeZ,
ISO_TIME_LEN + 3,
ISO_TIME_FORMAT_W,
stTime.wYear,
stTime.wMonth,
stTime.wDay,
stTime.wHour,
stTime.wMinute,
stTime.wSecond);
}
return pwszISOTimeZ;
}
//LocalFree required
PWSTR GetCurrentTimeZ(void)
{
SYSTEMTIME stTime;
GetSystemTime(&stTime);
return (SystemTimeToISO8601(stTime));
}
BOOL KernelDump(PWSTR pwszDumpName)
{
HANDLE hFile;
NTSTATUS status;
ULONG ulReturnLength;
hFile = CreateFile(
pwszDumpName,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
0,
NULL
);
if (NULL == hFile)
{
wprintf(L"Error creating dump file: %i\r\n", GetLastError());
return FALSE;
}
SYSDBG_LIVEDUMP_CONTROL slcDumpControl = {0};
slcDumpControl.Version = 1;
slcDumpControl.BugCheckCode = LIVE_SYSTEM_DUMP;
slcDumpControl.DumpFileHandle = hFile;
status = NtSystemDebugControl(SysDbgGetLiveKernelDump, &slcDumpControl, sizeof(slcDumpControl), NULL, 0, &ulReturnLength);
if (STATUS_SUCCESS != status)
{
wprintf(L"Error writing dump: 0x%08x\r\n", status);
CloseHandle(hFile);
return FALSE;
}
CloseHandle(hFile);
return TRUE;
}