1 /* 2 * Copyright (c) 2018 Virtuozzo International GmbH 3 * 4 * This work is licensed under the terms of the GNU GPL, version 2 or later. 5 * 6 */ 7 8 #include "qemu/osdep.h" 9 10 #include "err.h" 11 #include "addrspace.h" 12 #include "pe.h" 13 #include "pdb.h" 14 #include "kdbg.h" 15 #include "download.h" 16 #include "qemu/win_dump_defs.h" 17 18 #define SYM_URL_BASE "https://msdl.microsoft.com/download/symbols/" 19 #define PDB_NAME "ntkrnlmp.pdb" 20 #define PE_NAME "ntoskrnl.exe" 21 22 #define INITIAL_MXCSR 0x1f80 23 24 typedef struct idt_desc { 25 uint16_t offset1; /* offset bits 0..15 */ 26 uint16_t selector; 27 uint8_t ist; 28 uint8_t type_attr; 29 uint16_t offset2; /* offset bits 16..31 */ 30 uint32_t offset3; /* offset bits 32..63 */ 31 uint32_t rsrvd; 32 } __attribute__ ((packed)) idt_desc_t; 33 34 static uint64_t idt_desc_addr(idt_desc_t desc) 35 { 36 return (uint64_t)desc.offset1 | ((uint64_t)desc.offset2 << 16) | 37 ((uint64_t)desc.offset3 << 32); 38 } 39 40 static const uint64_t SharedUserData = 0xfffff78000000000; 41 42 #define KUSD_OFFSET_SUITE_MASK 0x2d0 43 #define KUSD_OFFSET_PRODUCT_TYPE 0x264 44 45 #define SYM_RESOLVE(base, r, s) ((s = pdb_resolve(base, r, #s)),\ 46 s ? printf(#s" = 0x%016"PRIx64"\n", s) :\ 47 eprintf("Failed to resolve "#s"\n"), s) 48 49 static uint64_t rol(uint64_t x, uint64_t y) 50 { 51 return (x << y) | (x >> (64 - y)); 52 } 53 54 /* 55 * Decoding algorithm can be found in Volatility project 56 */ 57 static void kdbg_decode(uint64_t *dst, uint64_t *src, size_t size, 58 uint64_t kwn, uint64_t kwa, uint64_t kdbe) 59 { 60 size_t i; 61 assert(size % sizeof(uint64_t) == 0); 62 for (i = 0; i < size / sizeof(uint64_t); i++) { 63 uint64_t block; 64 65 block = src[i]; 66 block = rol(block ^ kwn, (uint8_t)kwn); 67 block = __builtin_bswap64(block ^ kdbe) ^ kwa; 68 dst[i] = block; 69 } 70 } 71 72 static KDDEBUGGER_DATA64 *get_kdbg(uint64_t KernBase, struct pdb_reader *pdb, 73 struct va_space *vs, uint64_t KdDebuggerDataBlock) 74 { 75 const char OwnerTag[4] = "KDBG"; 76 KDDEBUGGER_DATA64 *kdbg = NULL; 77 DBGKD_DEBUG_DATA_HEADER64 kdbg_hdr; 78 bool decode = false; 79 uint64_t kwn, kwa, KdpDataBlockEncoded; 80 81 if (va_space_rw(vs, 82 KdDebuggerDataBlock + offsetof(KDDEBUGGER_DATA64, Header), 83 &kdbg_hdr, sizeof(kdbg_hdr), 0)) { 84 eprintf("Failed to extract KDBG header\n"); 85 return NULL; 86 } 87 88 if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) { 89 uint64_t KiWaitNever, KiWaitAlways; 90 91 decode = true; 92 93 if (!SYM_RESOLVE(KernBase, pdb, KiWaitNever) || 94 !SYM_RESOLVE(KernBase, pdb, KiWaitAlways) || 95 !SYM_RESOLVE(KernBase, pdb, KdpDataBlockEncoded)) { 96 return NULL; 97 } 98 99 if (va_space_rw(vs, KiWaitNever, &kwn, sizeof(kwn), 0) || 100 va_space_rw(vs, KiWaitAlways, &kwa, sizeof(kwa), 0)) { 101 return NULL; 102 } 103 104 printf("[KiWaitNever] = 0x%016"PRIx64"\n", kwn); 105 printf("[KiWaitAlways] = 0x%016"PRIx64"\n", kwa); 106 107 /* 108 * If KDBG header can be decoded, KDBG size is available 109 * and entire KDBG can be decoded. 110 */ 111 printf("Decoding KDBG header...\n"); 112 kdbg_decode((uint64_t *)&kdbg_hdr, (uint64_t *)&kdbg_hdr, 113 sizeof(kdbg_hdr), kwn, kwa, KdpDataBlockEncoded); 114 115 printf("Owner tag is \'%.4s\'\n", (char *)&kdbg_hdr.OwnerTag); 116 if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) { 117 eprintf("Failed to decode KDBG header\n"); 118 return NULL; 119 } 120 } 121 122 kdbg = malloc(kdbg_hdr.Size); 123 if (!kdbg) { 124 return NULL; 125 } 126 127 if (va_space_rw(vs, KdDebuggerDataBlock, kdbg, kdbg_hdr.Size, 0)) { 128 eprintf("Failed to extract entire KDBG\n"); 129 free(kdbg); 130 return NULL; 131 } 132 133 if (!decode) { 134 return kdbg; 135 } 136 137 printf("Decoding KdDebuggerDataBlock...\n"); 138 kdbg_decode((uint64_t *)kdbg, (uint64_t *)kdbg, kdbg_hdr.Size, 139 kwn, kwa, KdpDataBlockEncoded); 140 141 va_space_rw(vs, KdDebuggerDataBlock, kdbg, kdbg_hdr.Size, 1); 142 143 return kdbg; 144 } 145 146 static void win_context_init_from_qemu_cpu_state(WinContext64 *ctx, 147 QEMUCPUState *s) 148 { 149 WinContext64 win_ctx = (WinContext64){ 150 .ContextFlags = WIN_CTX_X64 | WIN_CTX_INT | WIN_CTX_SEG | WIN_CTX_CTL, 151 .MxCsr = INITIAL_MXCSR, 152 153 .SegCs = s->cs.selector, 154 .SegSs = s->ss.selector, 155 .SegDs = s->ds.selector, 156 .SegEs = s->es.selector, 157 .SegFs = s->fs.selector, 158 .SegGs = s->gs.selector, 159 .EFlags = (uint32_t)s->rflags, 160 161 .Rax = s->rax, 162 .Rbx = s->rbx, 163 .Rcx = s->rcx, 164 .Rdx = s->rdx, 165 .Rsp = s->rsp, 166 .Rbp = s->rbp, 167 .Rsi = s->rsi, 168 .Rdi = s->rdi, 169 .R8 = s->r8, 170 .R9 = s->r9, 171 .R10 = s->r10, 172 .R11 = s->r11, 173 .R12 = s->r12, 174 .R13 = s->r13, 175 .R14 = s->r14, 176 .R15 = s->r15, 177 178 .Rip = s->rip, 179 .FltSave = { 180 .MxCsr = INITIAL_MXCSR, 181 }, 182 }; 183 184 *ctx = win_ctx; 185 } 186 187 /* 188 * Finds paging-structure hierarchy base, 189 * if previously set doesn't give access to kernel structures 190 */ 191 static int fix_dtb(struct va_space *vs, QEMU_Elf *qe) 192 { 193 /* 194 * Firstly, test previously set DTB. 195 */ 196 if (va_space_resolve(vs, SharedUserData)) { 197 return 0; 198 } 199 200 /* 201 * Secondly, find CPU which run system task. 202 */ 203 size_t i; 204 for (i = 0; i < qe->state_nr; i++) { 205 QEMUCPUState *s = qe->state[i]; 206 207 if (is_system(s)) { 208 va_space_set_dtb(vs, s->cr[3]); 209 printf("DTB 0x%016"PRIx64" has been found from CPU #%zu" 210 " as system task CR3\n", vs->dtb, i); 211 return !(va_space_resolve(vs, SharedUserData)); 212 } 213 } 214 215 /* 216 * Thirdly, use KERNEL_GS_BASE from CPU #0 as PRCB address and 217 * CR3 as [Prcb+0x7000] 218 */ 219 if (qe->has_kernel_gs_base) { 220 QEMUCPUState *s = qe->state[0]; 221 uint64_t Prcb = s->kernel_gs_base; 222 uint64_t *cr3 = va_space_resolve(vs, Prcb + 0x7000); 223 224 if (!cr3) { 225 return 1; 226 } 227 228 va_space_set_dtb(vs, *cr3); 229 printf("DirectoryTableBase = 0x%016"PRIx64" has been found from CPU #0" 230 " as interrupt handling CR3\n", vs->dtb); 231 return !(va_space_resolve(vs, SharedUserData)); 232 } 233 234 return 1; 235 } 236 237 static int fill_header(WinDumpHeader64 *hdr, struct pa_space *ps, 238 struct va_space *vs, uint64_t KdDebuggerDataBlock, 239 KDDEBUGGER_DATA64 *kdbg, uint64_t KdVersionBlock, int nr_cpus) 240 { 241 uint32_t *suite_mask = va_space_resolve(vs, SharedUserData + 242 KUSD_OFFSET_SUITE_MASK); 243 int32_t *product_type = va_space_resolve(vs, SharedUserData + 244 KUSD_OFFSET_PRODUCT_TYPE); 245 DBGKD_GET_VERSION64 kvb; 246 WinDumpHeader64 h; 247 size_t i; 248 249 QEMU_BUILD_BUG_ON(KUSD_OFFSET_SUITE_MASK >= ELF2DMP_PAGE_SIZE); 250 QEMU_BUILD_BUG_ON(KUSD_OFFSET_PRODUCT_TYPE >= ELF2DMP_PAGE_SIZE); 251 252 if (!suite_mask || !product_type) { 253 return 1; 254 } 255 256 if (va_space_rw(vs, KdVersionBlock, &kvb, sizeof(kvb), 0)) { 257 eprintf("Failed to extract KdVersionBlock\n"); 258 return 1; 259 } 260 261 h = (WinDumpHeader64) { 262 .Signature = "PAGE", 263 .ValidDump = "DU64", 264 .MajorVersion = kvb.MajorVersion, 265 .MinorVersion = kvb.MinorVersion, 266 .DirectoryTableBase = vs->dtb, 267 .PfnDatabase = kdbg->MmPfnDatabase, 268 .PsLoadedModuleList = kdbg->PsLoadedModuleList, 269 .PsActiveProcessHead = kdbg->PsActiveProcessHead, 270 .MachineImageType = kvb.MachineType, 271 .NumberProcessors = nr_cpus, 272 .BugcheckCode = LIVE_SYSTEM_DUMP, 273 .KdDebuggerDataBlock = KdDebuggerDataBlock, 274 .DumpType = 1, 275 .Comment = "Hello from elf2dmp!", 276 .SuiteMask = *suite_mask, 277 .ProductType = *product_type, 278 .SecondaryDataState = kvb.KdSecondaryVersion, 279 .PhysicalMemoryBlock = (WinDumpPhyMemDesc64) { 280 .NumberOfRuns = ps->block_nr, 281 }, 282 .RequiredDumpSpace = sizeof(h), 283 }; 284 285 for (i = 0; i < ps->block_nr; i++) { 286 h.PhysicalMemoryBlock.NumberOfPages += 287 ps->block[i].size / ELF2DMP_PAGE_SIZE; 288 h.PhysicalMemoryBlock.Run[i] = (WinDumpPhyMemRun64) { 289 .BasePage = ps->block[i].paddr / ELF2DMP_PAGE_SIZE, 290 .PageCount = ps->block[i].size / ELF2DMP_PAGE_SIZE, 291 }; 292 } 293 294 h.RequiredDumpSpace += 295 h.PhysicalMemoryBlock.NumberOfPages << ELF2DMP_PAGE_BITS; 296 297 *hdr = h; 298 299 return 0; 300 } 301 302 static int fill_context(KDDEBUGGER_DATA64 *kdbg, 303 struct va_space *vs, QEMU_Elf *qe) 304 { 305 int i; 306 307 for (i = 0; i < qe->state_nr; i++) { 308 uint64_t Prcb; 309 uint64_t Context; 310 WinContext64 ctx; 311 QEMUCPUState *s = qe->state[i]; 312 313 if (va_space_rw(vs, kdbg->KiProcessorBlock + sizeof(Prcb) * i, 314 &Prcb, sizeof(Prcb), 0)) { 315 eprintf("Failed to read CPU #%d PRCB location\n", i); 316 return 1; 317 } 318 319 if (!Prcb) { 320 eprintf("Context for CPU #%d is missing\n", i); 321 continue; 322 } 323 324 if (va_space_rw(vs, Prcb + kdbg->OffsetPrcbContext, 325 &Context, sizeof(Context), 0)) { 326 eprintf("Failed to read CPU #%d ContextFrame location\n", i); 327 return 1; 328 } 329 330 printf("Filling context for CPU #%d...\n", i); 331 win_context_init_from_qemu_cpu_state(&ctx, s); 332 333 if (va_space_rw(vs, Context, &ctx, sizeof(ctx), 1)) { 334 eprintf("Failed to fill CPU #%d context\n", i); 335 return 1; 336 } 337 } 338 339 return 0; 340 } 341 342 static int pe_get_data_dir_entry(uint64_t base, void *start_addr, int idx, 343 void *entry, size_t size, struct va_space *vs) 344 { 345 const char e_magic[2] = "MZ"; 346 const char Signature[4] = "PE\0\0"; 347 IMAGE_DOS_HEADER *dos_hdr = start_addr; 348 IMAGE_NT_HEADERS64 nt_hdrs; 349 IMAGE_FILE_HEADER *file_hdr = &nt_hdrs.FileHeader; 350 IMAGE_OPTIONAL_HEADER64 *opt_hdr = &nt_hdrs.OptionalHeader; 351 IMAGE_DATA_DIRECTORY *data_dir = nt_hdrs.OptionalHeader.DataDirectory; 352 353 QEMU_BUILD_BUG_ON(sizeof(*dos_hdr) >= ELF2DMP_PAGE_SIZE); 354 355 if (memcmp(&dos_hdr->e_magic, e_magic, sizeof(e_magic))) { 356 return 1; 357 } 358 359 if (va_space_rw(vs, base + dos_hdr->e_lfanew, 360 &nt_hdrs, sizeof(nt_hdrs), 0)) { 361 return 1; 362 } 363 364 if (memcmp(&nt_hdrs.Signature, Signature, sizeof(Signature)) || 365 file_hdr->Machine != 0x8664 || opt_hdr->Magic != 0x020b) { 366 return 1; 367 } 368 369 if (va_space_rw(vs, 370 base + data_dir[idx].VirtualAddress, 371 entry, size, 0)) { 372 return 1; 373 } 374 375 printf("Data directory entry #%d: RVA = 0x%08"PRIx32"\n", idx, 376 (uint32_t)data_dir[idx].VirtualAddress); 377 378 return 0; 379 } 380 381 static int write_dump(struct pa_space *ps, 382 WinDumpHeader64 *hdr, const char *name) 383 { 384 FILE *dmp_file = fopen(name, "wb"); 385 size_t i; 386 387 if (!dmp_file) { 388 eprintf("Failed to open output file \'%s\'\n", name); 389 return 1; 390 } 391 392 printf("Writing header to file...\n"); 393 394 if (fwrite(hdr, sizeof(*hdr), 1, dmp_file) != 1) { 395 eprintf("Failed to write dump header\n"); 396 fclose(dmp_file); 397 return 1; 398 } 399 400 for (i = 0; i < ps->block_nr; i++) { 401 struct pa_block *b = &ps->block[i]; 402 403 printf("Writing block #%zu/%zu to file...\n", i, ps->block_nr); 404 if (fwrite(b->addr, b->size, 1, dmp_file) != 1) { 405 eprintf("Failed to write dump header\n"); 406 fclose(dmp_file); 407 return 1; 408 } 409 } 410 411 return fclose(dmp_file); 412 } 413 414 static bool pe_check_export_name(uint64_t base, void *start_addr, 415 struct va_space *vs) 416 { 417 IMAGE_EXPORT_DIRECTORY export_dir; 418 const char *pe_name; 419 420 if (pe_get_data_dir_entry(base, start_addr, IMAGE_FILE_EXPORT_DIRECTORY, 421 &export_dir, sizeof(export_dir), vs)) { 422 return false; 423 } 424 425 pe_name = va_space_resolve(vs, base + export_dir.Name); 426 if (!pe_name) { 427 return false; 428 } 429 430 return !strcmp(pe_name, PE_NAME); 431 } 432 433 static int pe_get_pdb_symstore_hash(uint64_t base, void *start_addr, 434 char *hash, struct va_space *vs) 435 { 436 const char sign_rsds[4] = "RSDS"; 437 IMAGE_DEBUG_DIRECTORY debug_dir; 438 OMFSignatureRSDS rsds; 439 char *pdb_name; 440 size_t pdb_name_sz; 441 size_t i; 442 443 if (pe_get_data_dir_entry(base, start_addr, IMAGE_FILE_DEBUG_DIRECTORY, 444 &debug_dir, sizeof(debug_dir), vs)) { 445 eprintf("Failed to get Debug Directory\n"); 446 return 1; 447 } 448 449 if (debug_dir.Type != IMAGE_DEBUG_TYPE_CODEVIEW) { 450 return 1; 451 } 452 453 if (va_space_rw(vs, 454 base + debug_dir.AddressOfRawData, 455 &rsds, sizeof(rsds), 0)) { 456 return 1; 457 } 458 459 printf("CodeView signature is \'%.4s\'\n", rsds.Signature); 460 461 if (memcmp(&rsds.Signature, sign_rsds, sizeof(sign_rsds))) { 462 return 1; 463 } 464 465 pdb_name_sz = debug_dir.SizeOfData - sizeof(rsds); 466 pdb_name = malloc(pdb_name_sz); 467 if (!pdb_name) { 468 return 1; 469 } 470 471 if (va_space_rw(vs, base + debug_dir.AddressOfRawData + 472 offsetof(OMFSignatureRSDS, name), pdb_name, pdb_name_sz, 0)) { 473 free(pdb_name); 474 return 1; 475 } 476 477 printf("PDB name is \'%s\', \'%s\' expected\n", pdb_name, PDB_NAME); 478 479 if (strcmp(pdb_name, PDB_NAME)) { 480 eprintf("Unexpected PDB name, it seems the kernel isn't found\n"); 481 free(pdb_name); 482 return 1; 483 } 484 485 free(pdb_name); 486 487 sprintf(hash, "%.08x%.04x%.04x%.02x%.02x", rsds.guid.a, rsds.guid.b, 488 rsds.guid.c, rsds.guid.d[0], rsds.guid.d[1]); 489 hash += 20; 490 for (i = 0; i < 6; i++, hash += 2) { 491 sprintf(hash, "%.02x", rsds.guid.e[i]); 492 } 493 494 sprintf(hash, "%.01x", rsds.age); 495 496 return 0; 497 } 498 499 int main(int argc, char *argv[]) 500 { 501 int err = 0; 502 QEMU_Elf qemu_elf; 503 struct pa_space ps; 504 struct va_space vs; 505 QEMUCPUState *state; 506 idt_desc_t first_idt_desc; 507 uint64_t KernBase; 508 void *nt_start_addr = NULL; 509 WinDumpHeader64 header; 510 char pdb_hash[34]; 511 char pdb_url[] = SYM_URL_BASE PDB_NAME 512 "/0123456789ABCDEF0123456789ABCDEFx/" PDB_NAME; 513 struct pdb_reader pdb; 514 uint64_t KdDebuggerDataBlock; 515 KDDEBUGGER_DATA64 *kdbg; 516 uint64_t KdVersionBlock; 517 bool kernel_found = false; 518 519 if (argc != 3) { 520 eprintf("usage:\n\t%s elf_file dmp_file\n", argv[0]); 521 return 1; 522 } 523 524 if (QEMU_Elf_init(&qemu_elf, argv[1])) { 525 eprintf("Failed to initialize QEMU ELF dump\n"); 526 return 1; 527 } 528 529 if (pa_space_create(&ps, &qemu_elf)) { 530 eprintf("Failed to initialize physical address space\n"); 531 err = 1; 532 goto out_elf; 533 } 534 535 state = qemu_elf.state[0]; 536 printf("CPU #0 CR3 is 0x%016"PRIx64"\n", state->cr[3]); 537 538 va_space_create(&vs, &ps, state->cr[3]); 539 if (fix_dtb(&vs, &qemu_elf)) { 540 eprintf("Failed to find paging base\n"); 541 err = 1; 542 goto out_elf; 543 } 544 545 printf("CPU #0 IDT is at 0x%016"PRIx64"\n", state->idt.base); 546 547 if (va_space_rw(&vs, state->idt.base, 548 &first_idt_desc, sizeof(first_idt_desc), 0)) { 549 eprintf("Failed to get CPU #0 IDT[0]\n"); 550 err = 1; 551 goto out_ps; 552 } 553 printf("CPU #0 IDT[0] -> 0x%016"PRIx64"\n", idt_desc_addr(first_idt_desc)); 554 555 KernBase = idt_desc_addr(first_idt_desc) & ~(ELF2DMP_PAGE_SIZE - 1); 556 printf("Searching kernel downwards from 0x%016"PRIx64"...\n", KernBase); 557 558 for (; KernBase >= 0xfffff78000000000; KernBase -= ELF2DMP_PAGE_SIZE) { 559 nt_start_addr = va_space_resolve(&vs, KernBase); 560 if (!nt_start_addr) { 561 continue; 562 } 563 564 if (*(uint16_t *)nt_start_addr == 0x5a4d) { /* MZ */ 565 if (pe_check_export_name(KernBase, nt_start_addr, &vs)) { 566 kernel_found = true; 567 break; 568 } 569 } 570 } 571 572 if (!kernel_found) { 573 eprintf("Failed to find NT kernel image\n"); 574 err = 1; 575 goto out_ps; 576 } 577 578 printf("KernBase = 0x%016"PRIx64", signature is \'%.2s\'\n", KernBase, 579 (char *)nt_start_addr); 580 581 if (pe_get_pdb_symstore_hash(KernBase, nt_start_addr, pdb_hash, &vs)) { 582 eprintf("Failed to get PDB symbol store hash\n"); 583 err = 1; 584 goto out_ps; 585 } 586 587 sprintf(pdb_url, "%s%s/%s/%s", SYM_URL_BASE, PDB_NAME, pdb_hash, PDB_NAME); 588 printf("PDB URL is %s\n", pdb_url); 589 590 if (download_url(PDB_NAME, pdb_url)) { 591 eprintf("Failed to download PDB file\n"); 592 err = 1; 593 goto out_ps; 594 } 595 596 if (pdb_init_from_file(PDB_NAME, &pdb)) { 597 eprintf("Failed to initialize PDB reader\n"); 598 err = 1; 599 goto out_pdb_file; 600 } 601 602 if (!SYM_RESOLVE(KernBase, &pdb, KdDebuggerDataBlock) || 603 !SYM_RESOLVE(KernBase, &pdb, KdVersionBlock)) { 604 err = 1; 605 goto out_pdb; 606 } 607 608 kdbg = get_kdbg(KernBase, &pdb, &vs, KdDebuggerDataBlock); 609 if (!kdbg) { 610 err = 1; 611 goto out_pdb; 612 } 613 614 if (fill_header(&header, &ps, &vs, KdDebuggerDataBlock, kdbg, 615 KdVersionBlock, qemu_elf.state_nr)) { 616 err = 1; 617 goto out_kdbg; 618 } 619 620 if (fill_context(kdbg, &vs, &qemu_elf)) { 621 err = 1; 622 goto out_kdbg; 623 } 624 625 if (write_dump(&ps, &header, argv[2])) { 626 eprintf("Failed to save dump\n"); 627 err = 1; 628 goto out_kdbg; 629 } 630 631 out_kdbg: 632 free(kdbg); 633 out_pdb: 634 pdb_exit(&pdb); 635 out_pdb_file: 636 unlink(PDB_NAME); 637 out_ps: 638 pa_space_destroy(&ps); 639 out_elf: 640 QEMU_Elf_exit(&qemu_elf); 641 642 return err; 643 } 644