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