-
Notifications
You must be signed in to change notification settings - Fork 5
/
LiveDump.cpp
569 lines (494 loc) · 14.3 KB
/
LiveDump.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#include "LiveDump.hpp"
//
// Globals
//
static NtSystemDebugControl g_NtSystemDebugControl = NULL;
///=========================================================================
/// PrintUsage()
///
/// <summary>
/// Print program usage
/// </summary>
/// <returns></returns>
/// <remarks>
/// </remarks>
///=========================================================================
VOID
PrintUsage (
VOID
)
{
printf("\n\n");
printf("LiveDump.exe [type] [options] <FileName>\n");
printf("Type:\n");
printf("\ttriage : create a triage dump (parameter 29)\n");
printf("\tkernel : create a kernel dump (parameter 37)\n");
printf("Options (triage dump only):\n");
printf("\t-p : PID to dump\n");
printf("Options (kernel dump only):\n");
printf("\t-c : compress memory pages in dump\n");
printf("\t-d : Use dump stack (currently not implemented in Windows 8.1, 9600.16404.x86fre.winblue_gdr.130913-2141)\n");
printf("\t-h : add hypervisor pages\n");
printf("\t-u : also dump user space memory\n");
printf("FileName is the full path to the dump file to create.");
printf("\n");
}
///=========================================================================
/// main()
///
/// <summary>
/// Main console program
/// </summary>
/// <returns>0 on success, 1 on failure</returns>
/// <remarks>
/// </remarks>
///=========================================================================
INT
wmain (
__in INT Argc,
__in PWCHAR Argv[]
)
{
HANDLE handle;
HMODULE module;
DWORD result;
INT i;
SYSDBG_LIVEDUMP_CONTROL_FLAGS flags;
SYSDBG_LIVEDUMP_CONTROL_ADDPAGES pages;
PWCHAR outfile;
PWCHAR type;
NTSTATUS status;
ULONG pid;
ULONG fileFlags;
handle = INVALID_HANDLE_VALUE;
result = NO_ERROR;
flags.AsUlong = 0;
pages.AsUlong = 0;
outfile = NULL;
type = NULL;
pid = 0;
//
// Parse and validate arguments
//
if (Argc < 3)
{
printf("Invalid number of arguments.");
PrintUsage();
return 1;
}
for (i = 1; i < Argc; ++i)
{
if (i == 1)
{
type = Argv[i];
continue;
}
if (_wcsicmp(Argv[i], L"-c") == 0)
{
flags.CompressMemoryPagesData = 1;
}
else if (_wcsicmp(Argv[i], L"-d") == 0)
{
flags.UseDumpStorageStack = 1;
}
else if (_wcsicmp(Argv[i], L"-h") == 0)
{
pages.HypervisorPages = 1;
}
else if (_wcsicmp(Argv[i], L"-u") == 0)
{
flags.IncludeUserSpaceMemoryPages = 1;
}
else if (_wcsicmp(Argv[i], L"-p") == 0)
{
if ((i + 1) >= Argc)
{
printf("You must specify a PID.\n");
PrintUsage();
return 1;
}
pid = wcstoul(Argv[++i], '\0', 10);
if (pid == ULONG_MAX)
{
printf("Invalid PID.\n");
PrintUsage();
return 1;
}
}
else
{
outfile = Argv[i];
}
}
if (outfile == NULL)
{
printf("You must specify a file name.\n");
PrintUsage();
return 1;
}
if (_wcsicmp(type, L"triage") == 0)
{
//
// WriteFile can't cope with buffers that aren't sector-aligned when we specify
// FILE_FLAG_NO_BUFFERING (which is a requirement for kernel dump creation).
// Since the returned triage buffer data can be unaligned in this manner, it's
// easiest to just prevent WriteFile failing by not specifying that flag during
// the call to CreateFile.
//
fileFlags = FILE_ATTRIBUTE_NORMAL;
if (pid == 0)
{
printf("A non-zero PID is required for triage dumps.\n");
PrintUsage();
return 1;
}
}
else if (_wcsicmp(type, L"kernel") == 0)
{
//
// We have to use synchronous/no-buffering I/O for kernel dump creation.
//
fileFlags = FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING;
}
else
{
printf("Valid dump types are 'triage' and 'kernel'.\n");
PrintUsage();
return 1;
}
//
// Get function addresses
//
module = LoadLibrary(L"ntdll.dll");
if (module == NULL)
{
printf("Failed to load ntdll.dll\n");
return 1;
}
g_NtSystemDebugControl = (NtSystemDebugControl)
GetProcAddress(module,
"NtSystemDebugControl");
FreeLibrary(module);
if (g_NtSystemDebugControl == NULL)
{
printf("Failed to resolve NtSystemDebugControl.\n");
return 1;
}
//
// Get SeDebugPrivilege
//
if (!EnablePrivilege(SE_DEBUG_NAME, TRUE))
{
result = GetLastError();
printf("Failed to enable SeDebugPrivilege: %lu\n", result);
goto Exit;
}
//
// Create the target file (must specify synchronous I/O)
//
handle = CreateFileW(outfile,
GENERIC_WRITE | GENERIC_READ,
0,
NULL,
CREATE_ALWAYS,
fileFlags,
NULL);
if (handle == INVALID_HANDLE_VALUE)
{
result = GetLastError();
printf("CreateFileW failed: %d\n", result);
goto Exit;
}
//
// Try to create the requested dump
//
if (_wcsicmp(type, L"triage") == 0)
{
status = CreateTriageDump(handle, pid);
}
else if (_wcsicmp(type, L"kernel") == 0)
{
status = CreateKernelDump(handle, flags, pages);
}
if (NT_SUCCESS(status))
{
printf("Dump file '%ws' written successfully!\n", outfile);
result = NO_ERROR;
}
else
{
printf("Failed to create dump file.\n");
result = 1;
}
Exit:
//
// Remove privileges regardless of earlier success.
//
EnablePrivilege(SE_DEBUG_NAME, FALSE);
if (handle != INVALID_HANDLE_VALUE)
{
CloseHandle(handle);
if (!NT_SUCCESS(status))
{
DeleteFile(outfile);
}
}
return (result == NO_ERROR) ? 0 : 1;
}
///=========================================================================
/// EnablePrivilege()
///
/// <summary>
/// Enables or disables a privilege in an access token
/// </summary>
/// <parameter>PrivilegeName - name of privilege</parameter>
/// <parameter>Acquire - TRUE to add, FALSE to remove</parameter>
/// <returns>TRUE on success, FALSE on failure</returns>
/// <remarks>
/// </remarks>
///=========================================================================
BOOL
EnablePrivilege (
__in PCWSTR PrivilegeName,
__in BOOLEAN Acquire
)
{
HANDLE tokenHandle;
BOOL ret;
ULONG tokenPrivilegesSize = FIELD_OFFSET(TOKEN_PRIVILEGES, Privileges[1]);
PTOKEN_PRIVILEGES tokenPrivileges = static_cast<PTOKEN_PRIVILEGES>(calloc(1, tokenPrivilegesSize));
if (tokenPrivileges == NULL)
{
printf("Failed to allocate token privileges structure\n");
return FALSE;
}
tokenHandle = NULL;
tokenPrivileges->PrivilegeCount = 1;
ret = LookupPrivilegeValue(NULL,
PrivilegeName,
&tokenPrivileges->Privileges[0].Luid);
if (ret == FALSE)
{
printf("Failed to lookup privilege value by name: %lu\n", GetLastError());
goto Exit;
}
tokenPrivileges->Privileges[0].Attributes = Acquire ? SE_PRIVILEGE_ENABLED
: SE_PRIVILEGE_REMOVED;
ret = OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES,
&tokenHandle);
if (ret == FALSE)
{
printf("Failed to open current process token: %lu\n", GetLastError());
goto Exit;
}
ret = AdjustTokenPrivileges(tokenHandle,
FALSE,
tokenPrivileges,
tokenPrivilegesSize,
NULL,
NULL);
if (ret == FALSE)
{
printf("Failed to adjust current process token privileges: %lu\n", GetLastError());
goto Exit;
}
Exit:
if (tokenHandle != NULL)
{
CloseHandle(tokenHandle);
}
free(tokenPrivileges);
return ret;
}
///=========================================================================
/// CreateTriageDump()
///
/// <summary>
/// Creates a triage dump using NtDebugSystemControl parameter 29 and the
/// first 16 threads of the supplied process.
/// </summary>
/// <parameter>FileHandle - Handle to dump file to write</parameter>
/// <parameter>Pid - ID of the process to dump</parameter>
/// <returns>NTSTATUS code</returns>
/// <remarks>
/// </remarks>
///=========================================================================
NTSTATUS
CreateTriageDump (
__in HANDLE FileHandle,
__in ULONG Pid
)
{
NTSTATUS status;
SYSDBG_TRIAGE_DUMP dump;
PUCHAR dumpData;
ULONG returnLength;
ULONG bytesWritten;
HANDLE enumHandle;
THREADENTRY32 thread;
HANDLE threadHandles[MAX_TRIAGE_THREADS];
INT threadCount;
HANDLE threadHandle;
HANDLE processHandle;
printf("Attempting to create a triage dump...\n");
enumHandle = NULL;
dumpData = NULL;
threadCount = 0;
status = -1;
//
// Open the process
//
processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, Pid);
if (processHandle == INVALID_HANDLE_VALUE)
{
printf("Failed to open PID %lu: %lu", Pid, GetLastError());
goto Exit;
}
//
// Enumerate the first 16 threads
//
enumHandle = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, Pid);
if (enumHandle == INVALID_HANDLE_VALUE)
{
printf("Failed to get thread list: %lu", GetLastError());
goto Exit;
}
thread.dwSize = sizeof(thread);
if (!Thread32First(enumHandle, &thread))
{
printf("Failed to get first thread: %lu", GetLastError());
goto Exit;
}
do
{
if (thread.dwSize >=
FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
sizeof(thread.th32OwnerProcessID))
{
threadHandle = OpenThread(THREAD_ALL_ACCESS, FALSE, thread.th32ThreadID);
if (threadHandle == INVALID_HANDLE_VALUE)
{
printf("Failed to open thread %lu, skipping.\n", thread.th32ThreadID);
continue;
}
threadHandles[threadCount++] = threadHandle;
}
} while ((Thread32Next(enumHandle, &thread)) && threadCount < MAX_TRIAGE_THREADS);
if (threadCount == 0)
{
printf("No suitable threads found in PID %lu\n", Pid);
goto Exit;
}
printf("Triage dump is for PID %lu with %lu threads.\n",
Pid,
threadCount);
//
// Allocate buffer for triage dump data
//
dumpData = (PUCHAR)(calloc(1, TRIAGE_SIZE));
if (dumpData == NULL)
{
printf("Failed to allocate %lu bytes\n", TRIAGE_SIZE);
goto Exit;
}
memset(&dump, 0, sizeof(dump));
memset(dumpData, 0, TRIAGE_SIZE);
dump.ThreadHandles = threadCount;
dump.Handles = &threadHandles[0];
assert(g_NtSystemDebugControl != NULL);
status = g_NtSystemDebugControl(CONTROL_TRIAGE_DUMP,
(PVOID)(&dump),
sizeof(dump),
dumpData,
TRIAGE_SIZE,
&returnLength);
if (!NT_SUCCESS(status))
{
printf("NtSystemDebugControl failed: %08x\n", status);
goto Exit;
}
if (returnLength == 0)
{
printf("Triage data buffer is empty. Try a different process.\n");
status = -1;
goto Exit;
}
//
// Write to target dump file
//
if (!WriteFile(FileHandle, dumpData, returnLength, &bytesWritten, NULL))
{
printf("WriteFile failed: %lu\n", GetLastError());
status = -1;
goto Exit;
}
assert(bytesWritten == returnLength);
Exit:
if (enumHandle != INVALID_HANDLE_VALUE)
{
CloseHandle(enumHandle);
}
for (INT i = 0; i <= threadCount; i++)
{
CloseHandle(threadHandles[i]);
}
if (processHandle != INVALID_HANDLE_VALUE)
{
CloseHandle(processHandle);
}
if (dumpData != NULL)
{
free(dumpData);
}
return status;
}
///=========================================================================
/// CreateKernelDump()
///
/// <summary>
/// Creates a kernel dump using NtDebugSystemControl parameter 37
/// </summary>
/// <parameter>FileHandle - Handle to dump file to write</parameter>
/// <parameter>Flags - Kernel dump flags</parameter>
/// <parameter>Pages - Flags for memory pages to write</parameter>
/// <returns>NTSTATUS code</returns>
/// <remarks>
/// </remarks>
///=========================================================================
NTSTATUS
CreateKernelDump (
__in HANDLE FileHandle,
__in SYSDBG_LIVEDUMP_CONTROL_FLAGS Flags,
__in SYSDBG_LIVEDUMP_CONTROL_ADDPAGES Pages
)
{
NTSTATUS status;
SYSDBG_LIVEDUMP_CONTROL liveDumpControl;
ULONG returnLength;
printf("Attempting to create a kernel dump with flags %08x and pages %08x...\n",
Flags.AsUlong,
Pages.AsUlong);
printf("Please be patient, this could take a minute or two...\n");
memset(&liveDumpControl, 0, sizeof(liveDumpControl));
//
// The only thing the kernel looks at in the struct we pass is the handle,
// the flags and the pages to dump.
//
liveDumpControl.DumpFileHandle = (PVOID)(FileHandle);
liveDumpControl.AddPagesControl = Pages;
liveDumpControl.Flags = Flags;
status = g_NtSystemDebugControl(CONTROL_KERNEL_DUMP,
(PVOID)(&liveDumpControl),
sizeof(liveDumpControl),
NULL,
0,
&returnLength);
if (!NT_SUCCESS(status))
{
printf("NtSystemDebugControl failed: %08x\n", status);
goto Exit;
}
Exit:
return status;
}