pecoff_extractor.c (63612B)
1 /* 2 This file is part of libextractor. 3 Copyright (C) 2026 Vidyut Samanta and Christian Grothoff 4 5 libextractor is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published 7 by the Free Software Foundation; either version 3, or (at your 8 option) any later version. 9 10 libextractor is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with libextractor; see the file COPYING. If not, write to the 17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 Boston, MA 02110-1301, USA. 19 */ 20 /** 21 * @file plugins/pecoff_extractor.c 22 * @brief plugin to support Windows PE/COFF images (.exe, .dll, .sys) 23 * @author Christian Grothoff 24 * 25 * What makes a PE worth looking at in a forensic pass is not the code 26 * but the residue the build left behind: the Rich header records every 27 * tool that touched the object files, the CodeView record names the 28 * directory the developer built in, the import table fingerprints what 29 * the binary can do, and the version resource carries the vendor's own 30 * claim about what it is. All of that lives in a handful of small 31 * structures, so this plugin reads headers and directories only and 32 * never walks the code. 33 * 34 * References: 35 * "PE Format", https://learn.microsoft.com/windows/win32/debug/pe-format 36 * "Windows Authenticode Portable Executable Signature Format" 37 * Rich header: no vendor documentation exists; the layout used here is 38 * the one every published analysis agrees on (DanS ... Rich, all 39 * dwords between the two masked with the key that follows `Rich'). 40 */ 41 #include "platform.h" 42 #include "extractor.h" 43 #include "forensics.h" 44 45 46 /** 47 * Most sections we will look at. The field is 16 bits wide, but a 48 * real image has a few dozen at most and the table has to fit in 49 * SizeOfHeaders. 50 */ 51 #define PE_MAX_SECTIONS 96 52 53 /** 54 * Largest DOS stub we will search for a Rich header. The stub is 55 * 64 bytes plus the Rich header itself in every real image. 56 */ 57 #define PE_MAX_DOS_STUB 4096 58 59 /** 60 * Most Rich header records we will report. 61 */ 62 #define PE_MAX_RICH_ENTRIES 32 63 64 /** 65 * Most imported modules we will look at. 66 */ 67 #define PE_MAX_IMPORT_DLLS 64 68 69 /** 70 * Most imported functions we will fold into the import hash. Beyond 71 * this the hash would not match what other tools compute, so we drop 72 * it rather than report a value that cannot be compared. 73 */ 74 #define PE_MAX_IMPORT_FUNCS 8192 75 76 /** 77 * How much of the section holding the import directory we map in one 78 * read. The descriptors, the lookup tables and the hint/name table 79 * are laid out next to each other by every linker, so one read 80 * normally resolves the whole import table. 81 */ 82 #define PE_IMPORT_WINDOW (512 * 1024) 83 84 /** 85 * How much of the resource directory tree we read. The tree sits at 86 * the front of the section; the leaves point elsewhere and are read 87 * separately. 88 */ 89 #define PE_MAX_RESOURCE_DIR (64 * 1024) 90 91 /** 92 * Largest VS_VERSIONINFO resource we will parse. 93 */ 94 #define PE_MAX_VERSION_RESOURCE (32 * 1024) 95 96 /** 97 * How many bytes of a section we hash to estimate its entropy. 16 KiB 98 * is far more than enough to tell packed data from code. 99 */ 100 #define PE_ENTROPY_SAMPLE (16 * 1024) 101 102 /** 103 * How many sections we sample for entropy. 104 */ 105 #define PE_MAX_ENTROPY_SECTIONS 8 106 107 /** 108 * Smallest section we bother computing entropy for. Below this the 109 * estimate is dominated by the sample size. 110 */ 111 #define PE_MIN_ENTROPY_SECTION 512 112 113 /** 114 * How much of the certificate table we scan for subject names. 115 */ 116 #define PE_MAX_CERT_SCAN (16 * 1024) 117 118 /** 119 * Longest single name (module, function, section) we accept. 120 */ 121 #define PE_MAX_NAME 256 122 123 124 /** 125 * One entry of the section table, in the only four fields we need. 126 */ 127 struct pe_section 128 { 129 /** 130 * Address of the section in the loaded image, relative to the image 131 * base. 132 */ 133 uint32_t vaddr; 134 135 /** 136 * Size of the section in the loaded image. 137 */ 138 uint32_t vsize; 139 140 /** 141 * Offset of the section data in the file. 142 */ 143 uint32_t raw_ptr; 144 145 /** 146 * Number of bytes of section data in the file. 147 */ 148 uint32_t raw_size; 149 150 /** 151 * Section name, NUL-terminated. 152 */ 153 char name[9]; 154 }; 155 156 157 /** 158 * Everything the individual directory parsers need to know about the 159 * image. 160 */ 161 struct pe_context 162 { 163 /** 164 * Extraction context we were called with. 165 */ 166 struct EXTRACTOR_ExtractContext *ec; 167 168 /** 169 * Size of the file, or 0 if it could not be determined. 170 */ 171 uint64_t fsize; 172 173 /** 174 * Section table. 175 */ 176 struct pe_section sections[PE_MAX_SECTIONS]; 177 178 /** 179 * Number of valid entries in @e sections. 180 */ 181 unsigned int num_sections; 182 183 /** 184 * Number of bytes the headers occupy; an RVA below this maps to the 185 * same file offset. 186 */ 187 uint32_t size_of_headers; 188 189 /** 190 * True if this is a PE32+ image, in which case the import lookup 191 * table holds 64-bit entries. 192 */ 193 int pe32plus; 194 }; 195 196 197 /* ------------------------------------------------------------------ */ 198 /* MD5, for the import hash */ 199 /* ------------------------------------------------------------------ */ 200 201 /* 202 * The import hash is only useful if it equals what every other tool 203 * computes for the same file, which pins it to MD5. libextractor does 204 * not link a crypto library and pulling one in for 60 lines of 205 * arithmetic would be a poor trade, so RFC 1321 is implemented here. 206 * It is never used for anything security relevant. 207 */ 208 209 /** 210 * Running MD5 state. 211 */ 212 struct md5_context 213 { 214 /** 215 * Chaining value. 216 */ 217 uint32_t state[4]; 218 219 /** 220 * Number of bytes fed in so far. 221 */ 222 uint64_t count; 223 224 /** 225 * Partial block not yet compressed. 226 */ 227 unsigned char block[64]; 228 229 /** 230 * Number of bytes used in @e block. 231 */ 232 size_t used; 233 }; 234 235 236 /** 237 * The per-round additive constants, floor(2^32 * |sin(i + 1)|). 238 */ 239 static const uint32_t md5_k[64] = { 240 0xd76aa478U, 0xe8c7b756U, 0x242070dbU, 0xc1bdceeeU, 241 0xf57c0fafU, 0x4787c62aU, 0xa8304613U, 0xfd469501U, 242 0x698098d8U, 0x8b44f7afU, 0xffff5bb1U, 0x895cd7beU, 243 0x6b901122U, 0xfd987193U, 0xa679438eU, 0x49b40821U, 244 0xf61e2562U, 0xc040b340U, 0x265e5a51U, 0xe9b6c7aaU, 245 0xd62f105dU, 0x02441453U, 0xd8a1e681U, 0xe7d3fbc8U, 246 0x21e1cde6U, 0xc33707d6U, 0xf4d50d87U, 0x455a14edU, 247 0xa9e3e905U, 0xfcefa3f8U, 0x676f02d9U, 0x8d2a4c8aU, 248 0xfffa3942U, 0x8771f681U, 0x6d9d6122U, 0xfde5380cU, 249 0xa4beea44U, 0x4bdecfa9U, 0xf6bb4b60U, 0xbebfbc70U, 250 0x289b7ec6U, 0xeaa127faU, 0xd4ef3085U, 0x04881d05U, 251 0xd9d4d039U, 0xe6db99e5U, 0x1fa27cf8U, 0xc4ac5665U, 252 0xf4292244U, 0x432aff97U, 0xab9423a7U, 0xfc93a039U, 253 0x655b59c3U, 0x8f0ccc92U, 0xffeff47dU, 0x85845dd1U, 254 0x6fa87e4fU, 0xfe2ce6e0U, 0xa3014314U, 0x4e0811a1U, 255 0xf7537e82U, 0xbd3af235U, 0x2ad7d2bbU, 0xeb86d391U 256 }; 257 258 259 /** 260 * The per-round rotation amounts. 261 */ 262 static const unsigned char md5_r[64] = { 263 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 264 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 265 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 266 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 267 }; 268 269 270 /** 271 * Rotate @a v left by @a n bits. 272 * 273 * @param v value to rotate 274 * @param n number of bits, 1 to 31 275 * @return the rotated value 276 */ 277 static uint32_t 278 md5_rotl (uint32_t v, 279 unsigned int n) 280 { 281 return (v << n) | (v >> (32 - n)); 282 } 283 284 285 /** 286 * Compress one 64-byte block into @a ctx. 287 * 288 * @param[in,out] ctx hash state 289 * @param p the block 290 */ 291 static void 292 md5_compress (struct md5_context *ctx, 293 const unsigned char *p) 294 { 295 uint32_t m[16]; 296 uint32_t a = ctx->state[0]; 297 uint32_t b = ctx->state[1]; 298 uint32_t c = ctx->state[2]; 299 uint32_t d = ctx->state[3]; 300 301 for (unsigned int i = 0; i < 16; i++) 302 m[i] = EXTRACTOR_forensic_le32_ (&p[4 * i]); 303 for (unsigned int i = 0; i < 64; i++) 304 { 305 uint32_t f; 306 unsigned int g; 307 uint32_t tmp; 308 309 if (i < 16) 310 { 311 f = (b & c) | ((~b) & d); 312 g = i; 313 } 314 else if (i < 32) 315 { 316 f = (d & b) | ((~d) & c); 317 g = (5 * i + 1) % 16; 318 } 319 else if (i < 48) 320 { 321 f = b ^ c ^ d; 322 g = (3 * i + 5) % 16; 323 } 324 else 325 { 326 f = c ^ (b | (~d)); 327 g = (7 * i) % 16; 328 } 329 tmp = d; 330 d = c; 331 c = b; 332 b = b + md5_rotl (a + f + md5_k[i] + m[g], 333 md5_r[i]); 334 a = tmp; 335 } 336 ctx->state[0] += a; 337 ctx->state[1] += b; 338 ctx->state[2] += c; 339 ctx->state[3] += d; 340 } 341 342 343 /** 344 * Start a new MD5 computation. 345 * 346 * @param[out] ctx hash state to initialise 347 */ 348 static void 349 md5_init (struct md5_context *ctx) 350 { 351 ctx->state[0] = 0x67452301U; 352 ctx->state[1] = 0xefcdab89U; 353 ctx->state[2] = 0x98badcfeU; 354 ctx->state[3] = 0x10325476U; 355 ctx->count = 0; 356 ctx->used = 0; 357 } 358 359 360 /** 361 * Feed @a len bytes into the hash. 362 * 363 * @param[in,out] ctx hash state 364 * @param data the bytes 365 * @param len number of bytes in @a data 366 */ 367 static void 368 md5_update (struct md5_context *ctx, 369 const void *data, 370 size_t len) 371 { 372 const unsigned char *p = data; 373 374 ctx->count += len; 375 while (0 != len) 376 { 377 size_t take = sizeof (ctx->block) - ctx->used; 378 379 if (take > len) 380 take = len; 381 memcpy (&ctx->block[ctx->used], 382 p, 383 take); 384 ctx->used += take; 385 p += take; 386 len -= take; 387 if (sizeof (ctx->block) == ctx->used) 388 { 389 md5_compress (ctx, 390 ctx->block); 391 ctx->used = 0; 392 } 393 } 394 } 395 396 397 /** 398 * Finish the hash and write the 16-byte digest. 399 * 400 * @param[in,out] ctx hash state 401 * @param[out] digest where to store the result 402 */ 403 static void 404 md5_final (struct md5_context *ctx, 405 unsigned char *digest) 406 { 407 uint64_t bits = ctx->count * 8; 408 unsigned char tail[8]; 409 static const unsigned char pad = 0x80; 410 static const unsigned char zero = 0x00; 411 412 for (unsigned int i = 0; i < 8; i++) 413 tail[i] = (unsigned char) ((bits >> (8 * i)) & 0xFF); 414 md5_update (ctx, 415 &pad, 416 1); 417 while (56 != ctx->used) 418 md5_update (ctx, 419 &zero, 420 1); 421 /* md5_update() has bumped ctx->count past the real length, but the 422 length we are about to append was captured before the padding. */ 423 md5_update (ctx, 424 tail, 425 8); 426 for (unsigned int i = 0; i < 4; i++) 427 for (unsigned int j = 0; j < 4; j++) 428 digest[4 * i + j] = (unsigned char) ((ctx->state[i] >> (8 * j)) & 0xFF); 429 } 430 431 432 /* ------------------------------------------------------------------ */ 433 /* address translation */ 434 /* ------------------------------------------------------------------ */ 435 436 437 /** 438 * Translate a relative virtual address into a file offset. 439 * 440 * Every directory in a PE is addressed by RVA, so this is the one 441 * place where a forged section table turns into an out-of-range read. 442 * The result is checked against the file size and the amount of data 443 * the section actually has in the file. 444 * 445 * @param ctx image being parsed 446 * @param rva address relative to the image base 447 * @param[out] offset where to store the file offset 448 * @param[out] avail where to store how many bytes are readable there 449 * @return 1 on success, 0 if @a rva is not backed by file data 450 */ 451 static int 452 pe_rva_to_offset (const struct pe_context *ctx, 453 uint32_t rva, 454 uint64_t *offset, 455 uint64_t *avail) 456 { 457 for (unsigned int i = 0; i < ctx->num_sections; i++) 458 { 459 const struct pe_section *s = &ctx->sections[i]; 460 uint32_t span = s->vsize; 461 uint32_t delta; 462 463 if (span < s->raw_size) 464 span = s->raw_size; /* the loader maps whatever is in the file */ 465 if (0 == span) 466 continue; 467 if ( (rva < s->vaddr) || 468 (rva - s->vaddr >= span) ) 469 continue; 470 delta = rva - s->vaddr; 471 if (delta >= s->raw_size) 472 return 0; /* in the BSS part of the section; nothing in the file */ 473 if (((uint64_t) s->raw_ptr) + delta >= ctx->fsize) 474 return 0; 475 *offset = ((uint64_t) s->raw_ptr) + delta; 476 *avail = s->raw_size - delta; 477 if (*avail > ctx->fsize - *offset) 478 *avail = ctx->fsize - *offset; 479 return 1; 480 } 481 /* Addresses below SizeOfHeaders are mapped one-to-one; a few linkers 482 put small directories there. */ 483 if ( (0 != ctx->size_of_headers) && 484 (rva < ctx->size_of_headers) && 485 (rva < ctx->fsize) ) 486 { 487 *offset = rva; 488 *avail = ctx->size_of_headers - rva; 489 if (*avail > ctx->fsize - *offset) 490 *avail = ctx->fsize - *offset; 491 return 1; 492 } 493 return 0; 494 } 495 496 497 /** 498 * Read @a len bytes from the address @a rva. 499 * 500 * @param ctx image being parsed 501 * @param rva address relative to the image base 502 * @param[out] buf where to store the data 503 * @param len number of bytes to read 504 * @return 1 on success, 0 if the address is not backed by that much 505 * file data 506 */ 507 static int 508 pe_read_rva (struct pe_context *ctx, 509 uint32_t rva, 510 void *buf, 511 size_t len) 512 { 513 uint64_t off; 514 uint64_t avail; 515 516 if (! pe_rva_to_offset (ctx, 517 rva, 518 &off, 519 &avail)) 520 return 0; 521 if (avail < len) 522 return 0; 523 return EXTRACTOR_forensic_read_ (ctx->ec, 524 (int64_t) off, 525 buf, 526 len); 527 } 528 529 530 /* ------------------------------------------------------------------ */ 531 /* small formatting helpers */ 532 /* ------------------------------------------------------------------ */ 533 534 535 /** 536 * Append @a what to the comma-separated list in @a buf. 537 * 538 * @param buf destination buffer 539 * @param size number of bytes in @a buf 540 * @param what text to append 541 */ 542 static void 543 pe_append (char *buf, 544 size_t size, 545 const char *what) 546 { 547 size_t used = strlen (buf); 548 size_t need = strlen (what); 549 550 if (0 != used) 551 { 552 if (used + 2 >= size) 553 return; 554 buf[used++] = ','; 555 buf[used++] = ' '; 556 buf[used] = '\0'; 557 } 558 if (used + need + 1 > size) 559 return; 560 memcpy (&buf[used], 561 what, 562 need + 1); 563 } 564 565 566 /** 567 * Name of a COFF machine type. 568 * 569 * @param machine the raw field 570 * @return a static string, NULL if the value is not one we know 571 */ 572 static const char * 573 pe_machine_name (uint16_t machine) 574 { 575 switch (machine) 576 { 577 case 0x014c: 578 return "i386"; 579 case 0x0162: 580 return "MIPS R3000"; 581 case 0x0166: 582 return "MIPS little-endian"; 583 case 0x0169: 584 return "MIPS WCE v2"; 585 case 0x01a2: 586 return "Hitachi SH3"; 587 case 0x01a3: 588 return "Hitachi SH3 DSP"; 589 case 0x01a6: 590 return "Hitachi SH4"; 591 case 0x01a8: 592 return "Hitachi SH5"; 593 case 0x01c0: 594 return "ARM"; 595 case 0x01c2: 596 return "ARM Thumb"; 597 case 0x01c4: 598 return "ARM Thumb-2"; 599 case 0x01d3: 600 return "Matsushita AM33"; 601 case 0x01f0: 602 return "PowerPC"; 603 case 0x01f1: 604 return "PowerPC with FPU"; 605 case 0x0200: 606 return "Itanium"; 607 case 0x0266: 608 return "MIPS16"; 609 case 0x0284: 610 return "Alpha 64"; 611 case 0x0366: 612 return "MIPS with FPU"; 613 case 0x0466: 614 return "MIPS16 with FPU"; 615 case 0x0ebc: 616 return "EFI byte code"; 617 case 0x5032: 618 return "RISC-V 32"; 619 case 0x5064: 620 return "RISC-V 64"; 621 case 0x5128: 622 return "RISC-V 128"; 623 case 0x6232: 624 return "LoongArch 32"; 625 case 0x6264: 626 return "LoongArch 64"; 627 case 0x8664: 628 return "x86-64"; 629 case 0x9041: 630 return "Mitsubishi M32R"; 631 case 0xaa64: 632 return "ARM64"; 633 case 0xa641: 634 return "ARM64EC"; 635 case 0xa64e: 636 return "ARM64X"; 637 default: 638 return NULL; 639 } 640 } 641 642 643 /** 644 * Name of a Windows subsystem. 645 * 646 * @param subsystem the raw field 647 * @return a static string, NULL if the value is not one we know 648 */ 649 static const char * 650 pe_subsystem_name (uint16_t subsystem) 651 { 652 switch (subsystem) 653 { 654 case 1: 655 return "native"; 656 case 2: 657 return "Windows GUI"; 658 case 3: 659 return "Windows console"; 660 case 5: 661 return "OS/2 console"; 662 case 7: 663 return "POSIX console"; 664 case 8: 665 return "native Windows 9x driver"; 666 case 9: 667 return "Windows CE GUI"; 668 case 10: 669 return "EFI application"; 670 case 11: 671 return "EFI boot service driver"; 672 case 12: 673 return "EFI runtime driver"; 674 case 13: 675 return "EFI ROM"; 676 case 14: 677 return "Xbox"; 678 case 16: 679 return "Windows boot application"; 680 default: 681 return NULL; 682 } 683 } 684 685 686 /* ------------------------------------------------------------------ */ 687 /* Rich header */ 688 /* ------------------------------------------------------------------ */ 689 690 691 /** 692 * Decode the Rich header sitting between the DOS stub and the PE 693 * signature. 694 * 695 * Microsoft's linker records one (product id, build number, use count) 696 * triple for every tool that contributed to the image, masks the whole 697 * run with a checksum and terminates it with the literal `Rich' plus 698 * that mask. Nothing but MSVC writes it, and the build numbers pin 699 * down the exact toolchain installation, which is why it is the 700 * standard clustering feature for related binaries. 701 * 702 * @param ec extraction context 703 * @param stub bytes between the end of the DOS header and the PE header 704 * @param len number of bytes in @a stub 705 * @return 1 if the caller should stop extracting, 0 to continue 706 */ 707 static int 708 pe_parse_rich (struct EXTRACTOR_ExtractContext *ec, 709 const unsigned char *stub, 710 size_t len) 711 { 712 size_t rich = 0; 713 size_t dans = 0; 714 uint32_t key = 0; 715 int found = 0; 716 unsigned int tries = 0; 717 718 if (len < 16) 719 return 0; 720 for (size_t i = 0; (i + 8 <= len) && (tries < 8); i++) 721 { 722 size_t p; 723 724 if ( ('R' != stub[i]) || 725 ('i' != stub[i + 1]) || 726 ('c' != stub[i + 2]) || 727 ('h' != stub[i + 3]) ) 728 continue; 729 tries++; 730 key = EXTRACTOR_forensic_le32_ (&stub[i + 4]); 731 p = i; 732 while (p >= 4) 733 { 734 p -= 4; 735 if (0x536E6144U == (EXTRACTOR_forensic_le32_ (&stub[p]) ^ key)) 736 { 737 rich = i; 738 dans = p; 739 found = 1; 740 break; 741 } 742 } 743 if (found) 744 break; 745 } 746 if (! found) 747 return 0; 748 if (0 != 749 EXTRACTOR_forensic_emit_ (ec, 750 "pecoff", 751 EXTRACTOR_METATYPE_BUILD_ID, 752 "rich:%08x", 753 (unsigned int) key)) 754 return 1; 755 /* DanS is followed by three masked zero dwords, then the triples. */ 756 for (size_t q = dans + 16, n = 0; 757 (q + 8 <= rich) && (n < PE_MAX_RICH_ENTRIES); 758 q += 8, n++) 759 { 760 uint32_t comp = EXTRACTOR_forensic_le32_ (&stub[q]) ^ key; 761 uint32_t uses = EXTRACTOR_forensic_le32_ (&stub[q + 4]) ^ key; 762 763 if ( (0 == comp) && 764 (0 == uses) ) 765 continue; 766 if (0 != 767 EXTRACTOR_forensic_emit_ (ec, 768 "pecoff", 769 EXTRACTOR_METATYPE_TOOLCHAIN, 770 "Rich: prodID 0x%04x, build %u, count %u", 771 (unsigned int) (comp >> 16), 772 (unsigned int) (comp & 0xFFFF), 773 (unsigned int) uses)) 774 return 1; 775 } 776 return 0; 777 } 778 779 780 /* ------------------------------------------------------------------ */ 781 /* debug directory */ 782 /* ------------------------------------------------------------------ */ 783 784 785 /** 786 * Report the CodeView record a debug directory entry points at. 787 * 788 * The RSDS record carries the absolute path of the PDB as it existed 789 * on the build machine, which routinely leaks the developer's user 790 * name and source tree layout, plus the GUID and age that identify the 791 * build on a symbol server. 792 * 793 * @param ctx image being parsed 794 * @param rva address of the record 795 * @param size number of bytes in the record 796 * @return 1 if the caller should stop extracting, 0 to continue 797 */ 798 static int 799 pe_parse_codeview (struct pe_context *ctx, 800 uint32_t rva, 801 uint32_t size) 802 { 803 unsigned char buf[PE_MAX_NAME + 32]; 804 size_t want = size; 805 806 if (size < 24) 807 return 0; 808 if (want > sizeof (buf)) 809 want = sizeof (buf); 810 if (! pe_read_rva (ctx, 811 rva, 812 buf, 813 want)) 814 return 0; 815 if ( ('R' == buf[0]) && 816 ('S' == buf[1]) && 817 ('D' == buf[2]) && 818 ('S' == buf[3]) ) 819 { 820 uint32_t age = EXTRACTOR_forensic_le32_ (&buf[20]); 821 822 /* The symbol-server spelling: the GUID with its first three fields 823 printed as numbers, then the age. That is the string a lookup 824 against a symbol store is keyed on. */ 825 if (0 != 826 EXTRACTOR_forensic_emit_ (ctx->ec, 827 "pecoff", 828 EXTRACTOR_METATYPE_BUILD_ID, 829 "%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X%X", 830 (unsigned int) EXTRACTOR_forensic_le32_ ( 831 &buf[4]), 832 (unsigned int) EXTRACTOR_forensic_le16_ ( 833 &buf[8]), 834 (unsigned int) EXTRACTOR_forensic_le16_ ( 835 &buf[10]), 836 buf[12], buf[13], buf[14], buf[15], 837 buf[16], buf[17], buf[18], buf[19], 838 (unsigned int) age)) 839 return 1; 840 return EXTRACTOR_forensic_emit_text_ (ctx->ec, 841 "pecoff", 842 EXTRACTOR_METATYPE_DEBUG_PATH, 843 (const char *) &buf[24], 844 want - 24); 845 } 846 if ( ('N' == buf[0]) && 847 ('B' == buf[1]) && 848 ('1' == buf[2]) && 849 ('0' == buf[3]) ) 850 { 851 if (0 != 852 EXTRACTOR_forensic_emit_ (ctx->ec, 853 "pecoff", 854 EXTRACTOR_METATYPE_BUILD_ID, 855 "NB10:%08X%X", 856 (unsigned int) EXTRACTOR_forensic_le32_ ( 857 &buf[12]), 858 (unsigned int) EXTRACTOR_forensic_le32_ ( 859 &buf[16]))) 860 return 1; 861 return EXTRACTOR_forensic_emit_text_ (ctx->ec, 862 "pecoff", 863 EXTRACTOR_METATYPE_DEBUG_PATH, 864 (const char *) &buf[16], 865 want - 16); 866 } 867 return 0; 868 } 869 870 871 /** 872 * Walk the debug directory. 873 * 874 * @param ctx image being parsed 875 * @param rva address of the directory 876 * @param size number of bytes in the directory 877 * @return 1 if the caller should stop extracting, 0 to continue 878 */ 879 static int 880 pe_parse_debug (struct pe_context *ctx, 881 uint32_t rva, 882 uint32_t size) 883 { 884 unsigned int count = size / 28; 885 886 if (count > 16) 887 count = 16; 888 for (unsigned int i = 0; i < count; i++) 889 { 890 unsigned char ent[28]; 891 892 if (! pe_read_rva (ctx, 893 rva + i * 28, 894 ent, 895 sizeof (ent))) 896 return 0; 897 if (2 != EXTRACTOR_forensic_le32_ (&ent[12])) 898 continue; /* not IMAGE_DEBUG_TYPE_CODEVIEW */ 899 if (0 != 900 pe_parse_codeview (ctx, 901 EXTRACTOR_forensic_le32_ (&ent[20]), 902 EXTRACTOR_forensic_le32_ (&ent[16]))) 903 return 1; 904 break; /* one CodeView record is all any image has */ 905 } 906 return 0; 907 } 908 909 910 /* ------------------------------------------------------------------ */ 911 /* version resource */ 912 /* ------------------------------------------------------------------ */ 913 914 915 /** 916 * Round @a v up to the next multiple of four. 917 * 918 * Every block in a VS_VERSIONINFO starts on a 32-bit boundary measured 919 * from the start of the resource. 920 * 921 * @param v the value 922 * @return @a v rounded up, or 0 on overflow 923 */ 924 static size_t 925 pe_align4 (size_t v) 926 { 927 if (v > SIZE_MAX - 3) 928 return 0; 929 return (v + 3) & ~((size_t) 3); 930 } 931 932 933 /** 934 * Number of bytes the UTF-16LE key at @a p occupies, terminator 935 * included. 936 * 937 * @param p start of the key 938 * @param avail number of bytes readable at @a p 939 * @return the length, 0 if the key is not terminated within @a avail 940 */ 941 static size_t 942 pe_key_bytes (const unsigned char *p, 943 size_t avail) 944 { 945 for (size_t i = 0; i + 1 < avail; i += 2) 946 if (0 == EXTRACTOR_forensic_le16_ (&p[i])) 947 return i + 2; 948 return 0; 949 } 950 951 952 /** 953 * Does the UTF-16LE key at @a p spell @a ascii? 954 * 955 * @param p start of the key 956 * @param avail number of bytes readable at @a p 957 * @param ascii the name to compare against 958 * @return 1 if they match, 0 if not 959 */ 960 static int 961 pe_key_is (const unsigned char *p, 962 size_t avail, 963 const char *ascii) 964 { 965 size_t i = 0; 966 967 while ('\0' != ascii[i]) 968 { 969 if (2 * i + 1 >= avail) 970 return 0; 971 if (EXTRACTOR_forensic_le16_ (&p[2 * i]) != (uint16_t) (unsigned char) 972 ascii[i]) 973 return 0; 974 i++; 975 } 976 if (2 * i + 1 >= avail) 977 return 0; 978 return (0 == EXTRACTOR_forensic_le16_ (&p[2 * i])); 979 } 980 981 982 /** 983 * Mapping from a VERSIONINFO string name to the meta data type we 984 * report it as. 985 */ 986 struct pe_version_field 987 { 988 /** 989 * Name as it appears in the StringTable. 990 */ 991 const char *key; 992 993 /** 994 * Type to report the value under. 995 */ 996 enum EXTRACTOR_MetaType type; 997 }; 998 999 1000 /** 1001 * The StringFileInfo names worth reporting. Everything else in there 1002 * is vendor-specific noise. 1003 */ 1004 static const struct pe_version_field pe_version_fields[] = { 1005 { "CompanyName", EXTRACTOR_METATYPE_COMPANY }, 1006 { "FileDescription", EXTRACTOR_METATYPE_DESCRIPTION }, 1007 { "ProductName", EXTRACTOR_METATYPE_PRODUCT_NAME }, 1008 { "ProductVersion", EXTRACTOR_METATYPE_PRODUCT_VERSION }, 1009 { "FileVersion", EXTRACTOR_METATYPE_SOFTWARE_VERSION }, 1010 { "OriginalFilename", EXTRACTOR_METATYPE_FILENAME }, 1011 { "LegalCopyright", EXTRACTOR_METATYPE_COPYRIGHT }, 1012 { "LegalTrademarks", EXTRACTOR_METATYPE_LICENSE }, 1013 { "InternalName", EXTRACTOR_METATYPE_TITLE }, 1014 { "Comments", EXTRACTOR_METATYPE_COMMENT }, 1015 { NULL, EXTRACTOR_METATYPE_RESERVED } 1016 }; 1017 1018 1019 /** 1020 * Report one String entry of a StringTable. 1021 * 1022 * @param ec extraction context 1023 * @param b start of the String structure 1024 * @param len number of bytes readable at @a b 1025 * @return 1 if the caller should stop extracting, 0 to continue 1026 */ 1027 static int 1028 pe_parse_version_string (struct EXTRACTOR_ExtractContext *ec, 1029 const unsigned char *b, 1030 size_t len) 1031 { 1032 size_t keylen; 1033 size_t value; 1034 1035 if (len < 8) 1036 return 0; 1037 keylen = pe_key_bytes (&b[6], 1038 len - 6); 1039 if (0 == keylen) 1040 return 0; 1041 value = pe_align4 (6 + keylen); 1042 if ( (0 == value) || 1043 (value >= len) ) 1044 return 0; 1045 for (unsigned int i = 0; NULL != pe_version_fields[i].key; i++) 1046 { 1047 if (! pe_key_is (&b[6], 1048 len - 6, 1049 pe_version_fields[i].key)) 1050 continue; 1051 /* The declared value length is in characters for text values and in 1052 bytes for binary ones, and writers disagree about which applies. 1053 The enclosing block length is unambiguous, so hand the rest of 1054 the block to a converter that stops at the NUL. */ 1055 return EXTRACTOR_forensic_emit_utf16le_ (ec, 1056 "pecoff", 1057 pe_version_fields[i].type, 1058 &b[value], 1059 len - value); 1060 } 1061 return 0; 1062 } 1063 1064 1065 /** 1066 * Walk one StringTable, which holds the strings for a single language 1067 * and code page. 1068 * 1069 * @param ec extraction context 1070 * @param b start of the StringTable structure 1071 * @param len number of bytes readable at @a b 1072 * @return 1 if the caller should stop extracting, 0 to continue 1073 */ 1074 static int 1075 pe_parse_string_table (struct EXTRACTOR_ExtractContext *ec, 1076 const unsigned char *b, 1077 size_t len) 1078 { 1079 size_t keylen; 1080 size_t pos; 1081 1082 if (len < 8) 1083 return 0; 1084 keylen = pe_key_bytes (&b[6], 1085 len - 6); 1086 if (0 == keylen) 1087 return 0; 1088 pos = pe_align4 (6 + keylen); 1089 for (unsigned int n = 0; 1090 (0 != pos) && (pos + 6 <= len) && (n < EXTRACTOR_FORENSIC_MAX_ITEMS); 1091 n++) 1092 { 1093 size_t slen = EXTRACTOR_forensic_le16_ (&b[pos]); 1094 size_t have; 1095 1096 if (slen < 8) 1097 break; /* no forward progress possible */ 1098 have = len - pos; 1099 if (have > slen) 1100 have = slen; 1101 if (0 != 1102 pe_parse_version_string (ec, 1103 &b[pos], 1104 have)) 1105 return 1; 1106 pos = pe_align4 (pos + slen); 1107 } 1108 return 0; 1109 } 1110 1111 1112 /** 1113 * Walk a StringFileInfo block and its per-language string tables. 1114 * 1115 * @param ec extraction context 1116 * @param b start of the StringFileInfo structure 1117 * @param len number of bytes readable at @a b 1118 * @return 1 if the caller should stop extracting, 0 to continue 1119 */ 1120 static int 1121 pe_parse_string_file_info (struct EXTRACTOR_ExtractContext *ec, 1122 const unsigned char *b, 1123 size_t len) 1124 { 1125 size_t keylen; 1126 size_t pos; 1127 1128 if (len < 8) 1129 return 0; 1130 keylen = pe_key_bytes (&b[6], 1131 len - 6); 1132 if (0 == keylen) 1133 return 0; 1134 pos = pe_align4 (6 + keylen); 1135 for (unsigned int n = 0; 1136 (0 != pos) && (pos + 6 <= len) && (n < 8); 1137 n++) 1138 { 1139 size_t tlen = EXTRACTOR_forensic_le16_ (&b[pos]); 1140 size_t have; 1141 1142 if (tlen < 8) 1143 break; 1144 have = len - pos; 1145 if (have > tlen) 1146 have = tlen; 1147 if (0 != 1148 pe_parse_string_table (ec, 1149 &b[pos], 1150 have)) 1151 return 1; 1152 pos = pe_align4 (pos + tlen); 1153 } 1154 return 0; 1155 } 1156 1157 1158 /** 1159 * Parse a VS_VERSIONINFO resource. 1160 * 1161 * @param ec extraction context 1162 * @param b start of the resource 1163 * @param len number of bytes readable at @a b 1164 * @return 1 if the caller should stop extracting, 0 to continue 1165 */ 1166 static int 1167 pe_parse_version_info (struct EXTRACTOR_ExtractContext *ec, 1168 const unsigned char *b, 1169 size_t len) 1170 { 1171 size_t total; 1172 size_t vallen; 1173 size_t keylen; 1174 size_t pos; 1175 1176 if (len < 8) 1177 return 0; 1178 total = EXTRACTOR_forensic_le16_ (&b[0]); 1179 if ( (total < 8) || 1180 (total > len) ) 1181 total = len; 1182 vallen = EXTRACTOR_forensic_le16_ (&b[2]); 1183 keylen = pe_key_bytes (&b[6], 1184 total - 6); 1185 if (0 == keylen) 1186 return 0; 1187 if (! pe_key_is (&b[6], 1188 total - 6, 1189 "VS_VERSION_INFO")) 1190 return 0; 1191 pos = pe_align4 (6 + keylen); 1192 if ( (0 == pos) || 1193 (pos >= total) ) 1194 return 0; 1195 if ( (52 <= vallen) && 1196 (pos + 52 <= total) && 1197 (0xFEEF04BDU == EXTRACTOR_forensic_le32_ (&b[pos])) ) 1198 { 1199 uint32_t fv_ms = EXTRACTOR_forensic_le32_ (&b[pos + 8]); 1200 uint32_t fv_ls = EXTRACTOR_forensic_le32_ (&b[pos + 12]); 1201 1202 if (0 != 1203 EXTRACTOR_forensic_emit_ (ec, 1204 "pecoff", 1205 EXTRACTOR_METATYPE_FORMAT_VERSION, 1206 "%u.%u.%u.%u", 1207 (unsigned int) (fv_ms >> 16), 1208 (unsigned int) (fv_ms & 0xFFFF), 1209 (unsigned int) (fv_ls >> 16), 1210 (unsigned int) (fv_ls & 0xFFFF))) 1211 return 1; 1212 } 1213 pos = pe_align4 (pos + vallen); 1214 for (unsigned int n = 0; 1215 (0 != pos) && (pos + 6 <= total) && (n < 8); 1216 n++) 1217 { 1218 size_t clen = EXTRACTOR_forensic_le16_ (&b[pos]); 1219 size_t have; 1220 1221 if (clen < 8) 1222 break; 1223 have = total - pos; 1224 if (have > clen) 1225 have = clen; 1226 if (pe_key_is (&b[pos + 6], 1227 have - 6, 1228 "StringFileInfo")) 1229 { 1230 if (0 != 1231 pe_parse_string_file_info (ec, 1232 &b[pos], 1233 have)) 1234 return 1; 1235 } 1236 pos = pe_align4 (pos + clen); 1237 } 1238 return 0; 1239 } 1240 1241 1242 /** 1243 * Find the RT_VERSION leaf in the resource directory tree and parse 1244 * the resource it points at. 1245 * 1246 * The tree is three levels deep (type, name, language); we want the 1247 * first leaf under type 16 and do not care which language it is. 1248 * 1249 * @param ctx image being parsed 1250 * @param rva address of the resource directory 1251 * @param size number of bytes in the resource directory 1252 * @return 1 if the caller should stop extracting, 0 to continue 1253 */ 1254 static int 1255 pe_parse_resources (struct pe_context *ctx, 1256 uint32_t rva, 1257 uint32_t size) 1258 { 1259 unsigned char *tree; 1260 unsigned char *res; 1261 size_t tlen = size; 1262 uint32_t node = 0; 1263 uint32_t leaf = 0; 1264 uint32_t data_rva; 1265 uint32_t data_size; 1266 int found = 0; 1267 int ret = 0; 1268 1269 if (size < 16) 1270 return 0; 1271 if (tlen > PE_MAX_RESOURCE_DIR) 1272 tlen = PE_MAX_RESOURCE_DIR; 1273 if (NULL == (tree = malloc (tlen))) 1274 return 0; 1275 if (! pe_read_rva (ctx, 1276 rva, 1277 tree, 1278 tlen)) 1279 { 1280 free (tree); 1281 return 0; 1282 } 1283 /* level 1: resource types */ 1284 { 1285 unsigned int named = EXTRACTOR_forensic_le16_ (&tree[12]); 1286 unsigned int ids = EXTRACTOR_forensic_le16_ (&tree[14]); 1287 size_t base; 1288 1289 if (named > 1024) 1290 named = 1024; 1291 if (ids > 1024) 1292 ids = 1024; 1293 base = 16 + ((size_t) named) * 8; 1294 for (unsigned int i = 0; i < ids; i++) 1295 { 1296 size_t at = base + ((size_t) i) * 8; 1297 1298 if (at + 8 > tlen) 1299 break; 1300 if (16 != EXTRACTOR_forensic_le32_ (&tree[at])) 1301 continue; /* not RT_VERSION */ 1302 node = EXTRACTOR_forensic_le32_ (&tree[at + 4]); 1303 if (0 == (node & 0x80000000U)) 1304 { 1305 free (tree); 1306 return 0; /* a type entry must point at a subdirectory */ 1307 } 1308 node &= 0x7FFFFFFFU; 1309 found = 1; 1310 break; 1311 } 1312 } 1313 if (! found) 1314 { 1315 free (tree); 1316 return 0; 1317 } 1318 /* levels 2 and 3: take the first entry each time */ 1319 for (unsigned int level = 0; level < 2; level++) 1320 { 1321 unsigned int named; 1322 unsigned int ids; 1323 1324 if (((size_t) node) + 16 > tlen) 1325 { 1326 free (tree); 1327 return 0; 1328 } 1329 named = EXTRACTOR_forensic_le16_ (&tree[node + 12]); 1330 ids = EXTRACTOR_forensic_le16_ (&tree[node + 14]); 1331 if (0 == named + ids) 1332 { 1333 free (tree); 1334 return 0; 1335 } 1336 if (((size_t) node) + 24 > tlen) 1337 { 1338 free (tree); 1339 return 0; 1340 } 1341 leaf = EXTRACTOR_forensic_le32_ (&tree[node + 20]); 1342 if (0 == (leaf & 0x80000000U)) 1343 break; /* a data entry: we are at the bottom */ 1344 node = leaf & 0x7FFFFFFFU; 1345 leaf = 0; 1346 } 1347 if ( (0 == leaf) || 1348 (0 != (leaf & 0x80000000U)) || 1349 (((size_t) leaf) + 16 > tlen) ) 1350 { 1351 free (tree); 1352 return 0; 1353 } 1354 data_rva = EXTRACTOR_forensic_le32_ (&tree[leaf]); 1355 data_size = EXTRACTOR_forensic_le32_ (&tree[leaf + 4]); 1356 free (tree); 1357 if (data_size < 8) 1358 return 0; 1359 if (data_size > PE_MAX_VERSION_RESOURCE) 1360 data_size = PE_MAX_VERSION_RESOURCE; 1361 if (NULL == (res = malloc (data_size))) 1362 return 0; 1363 if (pe_read_rva (ctx, 1364 data_rva, 1365 res, 1366 data_size)) 1367 ret = pe_parse_version_info (ctx->ec, 1368 res, 1369 data_size); 1370 free (res); 1371 return ret; 1372 } 1373 1374 1375 /* ------------------------------------------------------------------ */ 1376 /* imports */ 1377 /* ------------------------------------------------------------------ */ 1378 1379 1380 /** 1381 * A window of file data mapped by relative virtual address. 1382 */ 1383 struct pe_window 1384 { 1385 /** 1386 * The bytes, or NULL if nothing is mapped. 1387 */ 1388 unsigned char *buf; 1389 1390 /** 1391 * Address @e buf starts at. 1392 */ 1393 uint32_t base; 1394 1395 /** 1396 * Number of bytes in @e buf. 1397 */ 1398 size_t len; 1399 }; 1400 1401 1402 /** 1403 * Point at @a len bytes at @a rva inside @a w. 1404 * 1405 * @param w the window 1406 * @param rva address wanted 1407 * @param len number of bytes wanted 1408 * @return pointer into the window, NULL if the range is not mapped 1409 */ 1410 static const unsigned char * 1411 pe_window_at (const struct pe_window *w, 1412 uint32_t rva, 1413 size_t len) 1414 { 1415 uint32_t delta; 1416 1417 if ( (NULL == w->buf) || 1418 (rva < w->base) ) 1419 return NULL; 1420 delta = rva - w->base; 1421 if ( (delta > w->len) || 1422 (w->len - delta < len) ) 1423 return NULL; 1424 return &w->buf[delta]; 1425 } 1426 1427 1428 /** 1429 * Copy a NUL-terminated ASCII name out of @a w. 1430 * 1431 * @param w the window 1432 * @param rva address of the name 1433 * @param[out] out where to store the name 1434 * @param size number of bytes in @a out 1435 * @param lower 1 to fold the name to lower case, as the import hash 1436 * requires, 0 to keep it as the linker wrote it 1437 * @return 1 on success, 0 if the name is not mapped or not terminated 1438 */ 1439 static int 1440 pe_window_name (const struct pe_window *w, 1441 uint32_t rva, 1442 char *out, 1443 size_t size, 1444 int lower) 1445 { 1446 const unsigned char *p = pe_window_at (w, 1447 rva, 1448 1); 1449 size_t avail; 1450 size_t i; 1451 1452 if (NULL == p) 1453 return 0; 1454 avail = w->len - (size_t) (rva - w->base); 1455 if (avail > size - 1) 1456 avail = size - 1; 1457 for (i = 0; i < avail; i++) 1458 { 1459 unsigned char c = p[i]; 1460 1461 if ('\0' == c) 1462 break; 1463 if ( (c < 0x20) || 1464 (c > 0x7E) ) 1465 return 0; /* module and function names are ASCII */ 1466 if ( (lower) && 1467 ('A' <= c) && 1468 ('Z' >= c) ) 1469 c = (unsigned char) (c - 'A' + 'a'); 1470 out[i] = (char) c; 1471 } 1472 if (i == avail) 1473 return 0; /* not terminated inside the window */ 1474 out[i] = '\0'; 1475 return (0 != i); 1476 } 1477 1478 1479 /** 1480 * Compute the import hash over the import table. 1481 * 1482 * The hash is MD5 over `module.function' pairs, lower-cased, joined by 1483 * commas, in the order the linker wrote them; the module's `.dll', 1484 * `.ocx' or `.sys' suffix is dropped. That is the definition every 1485 * other tool uses, so any deviation makes the value useless for 1486 * comparison -- which is why this bails out instead of guessing 1487 * whenever part of the table is out of reach. 1488 * 1489 * @param ctx image being parsed 1490 * @param w window covering the import table 1491 * @param rva address of the import descriptor array 1492 * @param[out] hash where to store the 16-byte digest 1493 * @return 1 on success, 0 if no hash could be computed 1494 */ 1495 static int 1496 pe_import_hash (struct pe_context *ctx, 1497 const struct pe_window *w, 1498 uint32_t rva, 1499 unsigned char *hash) 1500 { 1501 struct md5_context md5; 1502 size_t thunk_size = ctx->pe32plus ? 8 : 4; 1503 unsigned int total = 0; 1504 int any = 0; 1505 1506 md5_init (&md5); 1507 for (unsigned int d = 0; d < PE_MAX_IMPORT_DLLS; d++) 1508 { 1509 const unsigned char *desc = pe_window_at (w, 1510 rva + d * 20, 1511 20); 1512 char lib[PE_MAX_NAME]; 1513 size_t liblen; 1514 uint32_t thunks; 1515 1516 if (NULL == desc) 1517 return 0; 1518 if ( (0 == EXTRACTOR_forensic_le32_ (&desc[0])) && 1519 (0 == EXTRACTOR_forensic_le32_ (&desc[12])) && 1520 (0 == EXTRACTOR_forensic_le32_ (&desc[16])) ) 1521 break; /* the all-zero terminator */ 1522 if (! pe_window_name (w, 1523 EXTRACTOR_forensic_le32_ (&desc[12]), 1524 lib, 1525 sizeof (lib), 1526 1)) 1527 return 0; 1528 liblen = strlen (lib); 1529 if ( (liblen > 4) && 1530 ('.' == lib[liblen - 4]) && 1531 ( (0 == strcmp (&lib[liblen - 3], "dll")) || 1532 (0 == strcmp (&lib[liblen - 3], "ocx")) || 1533 (0 == strcmp (&lib[liblen - 3], "sys")) ) ) 1534 { 1535 liblen -= 4; 1536 lib[liblen] = '\0'; 1537 } 1538 thunks = EXTRACTOR_forensic_le32_ (&desc[0]); 1539 if (0 == thunks) 1540 thunks = EXTRACTOR_forensic_le32_ (&desc[16]); 1541 if (0 == thunks) 1542 return 0; 1543 for (unsigned int t = 0;; t++) 1544 { 1545 const unsigned char *te; 1546 uint64_t v; 1547 char func[PE_MAX_NAME]; 1548 1549 if (total >= PE_MAX_IMPORT_FUNCS) 1550 return 0; /* would no longer match anyone else's value */ 1551 te = pe_window_at (w, 1552 thunks + t * (uint32_t) thunk_size, 1553 thunk_size); 1554 if (NULL == te) 1555 return 0; 1556 v = ctx->pe32plus 1557 ? EXTRACTOR_forensic_le64_ (te) 1558 : (uint64_t) EXTRACTOR_forensic_le32_ (te); 1559 if (0 == v) 1560 break; 1561 if (0 != (v & (ctx->pe32plus 1562 ? 0x8000000000000000ULL 1563 : 0x80000000ULL))) 1564 { 1565 /* Imported by ordinal. Tools that know the export tables of a 1566 few system libraries substitute the real name here; without 1567 those tables the documented fallback spelling is used. */ 1568 snprintf (func, 1569 sizeof (func), 1570 "ord%u", 1571 (unsigned int) (v & 0xFFFFU)); 1572 } 1573 else 1574 { 1575 uint32_t name_rva = (uint32_t) (v & 0x7FFFFFFFU); 1576 1577 if (name_rva > UINT32_MAX - 2) 1578 return 0; 1579 if (! pe_window_name (w, 1580 name_rva + 2, 1581 func, 1582 sizeof (func), 1583 1)) 1584 return 0; 1585 } 1586 if (any) 1587 md5_update (&md5, 1588 ",", 1589 1); 1590 md5_update (&md5, 1591 lib, 1592 liblen); 1593 md5_update (&md5, 1594 ".", 1595 1); 1596 md5_update (&md5, 1597 func, 1598 strlen (func)); 1599 any = 1; 1600 total++; 1601 } 1602 } 1603 if (! any) 1604 return 0; 1605 md5_final (&md5, 1606 hash); 1607 return 1; 1608 } 1609 1610 1611 /** 1612 * Report the imported modules and the import hash. 1613 * 1614 * @param ctx image being parsed 1615 * @param rva address of the import descriptor array 1616 * @param size number of bytes in the import directory 1617 * @return 1 if the caller should stop extracting, 0 to continue 1618 */ 1619 static int 1620 pe_parse_imports (struct pe_context *ctx, 1621 uint32_t rva, 1622 uint32_t size) 1623 { 1624 struct pe_window w = { NULL, 0, 0 }; 1625 unsigned char hash[16]; 1626 unsigned int count = 0; 1627 int ret = 0; 1628 1629 if (size < 20) 1630 return 0; 1631 /* Map the section the descriptors live in: the lookup tables and the 1632 hint/name table are in there too, so one read covers the lot. */ 1633 for (unsigned int i = 0; i < ctx->num_sections; i++) 1634 { 1635 const struct pe_section *s = &ctx->sections[i]; 1636 size_t len = s->raw_size; 1637 1638 if ( (0 == s->raw_size) || 1639 (rva < s->vaddr) || 1640 (rva - s->vaddr >= s->raw_size) ) 1641 continue; 1642 if (((uint64_t) s->raw_ptr) >= ctx->fsize) 1643 break; 1644 if (len > ctx->fsize - s->raw_ptr) 1645 len = (size_t) (ctx->fsize - s->raw_ptr); 1646 if (len > PE_IMPORT_WINDOW) 1647 len = PE_IMPORT_WINDOW; 1648 if (NULL == (w.buf = malloc (len))) 1649 break; 1650 if (! EXTRACTOR_forensic_read_ (ctx->ec, 1651 (int64_t) s->raw_ptr, 1652 w.buf, 1653 len)) 1654 { 1655 free (w.buf); 1656 w.buf = NULL; 1657 break; 1658 } 1659 w.base = s->vaddr; 1660 w.len = len; 1661 break; 1662 } 1663 if (NULL == w.buf) 1664 return 0; 1665 for (unsigned int d = 0; 1666 (d < PE_MAX_IMPORT_DLLS) && (count < EXTRACTOR_FORENSIC_MAX_ITEMS); 1667 d++) 1668 { 1669 const unsigned char *desc = pe_window_at (&w, 1670 rva + d * 20, 1671 20); 1672 char lib[PE_MAX_NAME]; 1673 1674 if (NULL == desc) 1675 break; 1676 if ( (0 == EXTRACTOR_forensic_le32_ (&desc[0])) && 1677 (0 == EXTRACTOR_forensic_le32_ (&desc[12])) && 1678 (0 == EXTRACTOR_forensic_le32_ (&desc[16])) ) 1679 break; 1680 if (! pe_window_name (&w, 1681 EXTRACTOR_forensic_le32_ (&desc[12]), 1682 lib, 1683 sizeof (lib), 1684 0)) 1685 break; 1686 count++; 1687 if (0 != 1688 EXTRACTOR_forensic_emit_text_ (ctx->ec, 1689 "pecoff", 1690 EXTRACTOR_METATYPE_LIBRARY_DEPENDENCY, 1691 lib, 1692 strlen (lib))) 1693 { 1694 ret = 1; 1695 goto cleanup; 1696 } 1697 } 1698 if (pe_import_hash (ctx, 1699 &w, 1700 rva, 1701 hash)) 1702 ret = EXTRACTOR_forensic_emit_hex_ (ctx->ec, 1703 "pecoff", 1704 EXTRACTOR_METATYPE_IMPORT_HASH, 1705 hash, 1706 sizeof (hash)); 1707 cleanup: 1708 free (w.buf); 1709 return ret; 1710 } 1711 1712 1713 /* ------------------------------------------------------------------ */ 1714 /* exports, certificates, entropy */ 1715 /* ------------------------------------------------------------------ */ 1716 1717 1718 /** 1719 * Report the internal name a DLL exports itself under and how many 1720 * entries it exports. 1721 * 1722 * The name in the export directory is the name the module had when it 1723 * was linked, which is often not the name of the file on disk. 1724 * 1725 * @param ctx image being parsed 1726 * @param rva address of the export directory 1727 * @param size number of bytes in the export directory 1728 * @return 1 if the caller should stop extracting, 0 to continue 1729 */ 1730 static int 1731 pe_parse_exports (struct pe_context *ctx, 1732 uint32_t rva, 1733 uint32_t size) 1734 { 1735 unsigned char dir[40]; 1736 char name[PE_MAX_NAME]; 1737 uint64_t off; 1738 uint64_t avail; 1739 uint32_t functions; 1740 1741 if (size < sizeof (dir)) 1742 return 0; 1743 if (! pe_read_rva (ctx, 1744 rva, 1745 dir, 1746 sizeof (dir))) 1747 return 0; 1748 functions = EXTRACTOR_forensic_le32_ (&dir[20]); 1749 if (functions <= 0xFFFF) 1750 { 1751 if (0 != 1752 EXTRACTOR_forensic_emit_ (ctx->ec, 1753 "pecoff", 1754 EXTRACTOR_METATYPE_ENTRY_COUNT, 1755 "%u", 1756 (unsigned int) functions)) 1757 return 1; 1758 } 1759 if (! pe_rva_to_offset (ctx, 1760 EXTRACTOR_forensic_le32_ (&dir[12]), 1761 &off, 1762 &avail)) 1763 return 0; 1764 if (avail > sizeof (name)) 1765 avail = sizeof (name); 1766 if (! EXTRACTOR_forensic_read_ (ctx->ec, 1767 (int64_t) off, 1768 name, 1769 (size_t) avail)) 1770 return 0; 1771 return EXTRACTOR_forensic_emit_text_ (ctx->ec, 1772 "pecoff", 1773 EXTRACTOR_METATYPE_FILENAME, 1774 name, 1775 (size_t) avail); 1776 } 1777 1778 1779 /** 1780 * Report what the certificate table says. 1781 * 1782 * Parsing PKCS#7 properly means an ASN.1 decoder, which is more than 1783 * this pass can justify. The subject common names are still findable 1784 * without one: the DER encoding of the commonName attribute is the 1785 * fixed five-byte sequence 06 03 55 04 03 followed by a tagged string, 1786 * so a scan finds every CN in the chain. Which one is the signer and 1787 * which the issuer is not decided here, so they are all reported. 1788 * 1789 * @param ctx image being parsed 1790 * @param offset file offset of the certificate table 1791 * @param size number of bytes in the certificate table 1792 * @return 1 if the caller should stop extracting, 0 to continue 1793 */ 1794 static int 1795 pe_parse_certificates (struct pe_context *ctx, 1796 uint32_t offset, 1797 uint32_t size) 1798 { 1799 unsigned char *buf; 1800 size_t len = size; 1801 unsigned int found = 0; 1802 int ret = 0; 1803 1804 if ( (size < 8) || 1805 (offset >= ctx->fsize) || 1806 (size > ctx->fsize - offset) ) 1807 return 0; 1808 if (len > PE_MAX_CERT_SCAN) 1809 len = PE_MAX_CERT_SCAN; 1810 if (NULL == (buf = malloc (len))) 1811 return 0; 1812 if (! EXTRACTOR_forensic_read_ (ctx->ec, 1813 (int64_t) offset, 1814 buf, 1815 len)) 1816 { 1817 free (buf); 1818 return 0; 1819 } 1820 for (size_t i = 0; 1821 (i + 7 < len) && (found < 8); 1822 i++) 1823 { 1824 size_t vlen; 1825 1826 if ( (0x06 != buf[i]) || 1827 (0x03 != buf[i + 1]) || 1828 (0x55 != buf[i + 2]) || 1829 (0x04 != buf[i + 3]) || 1830 (0x03 != buf[i + 4]) ) 1831 continue; 1832 /* 0x0C UTF8String, 0x13 PrintableString, 0x16 IA5String */ 1833 if ( (0x0C != buf[i + 5]) && 1834 (0x13 != buf[i + 5]) && 1835 (0x16 != buf[i + 5]) ) 1836 continue; 1837 vlen = buf[i + 6]; 1838 if ( (vlen < 1) || 1839 (vlen > 127) || 1840 (i + 7 + vlen > len) ) 1841 continue; 1842 found++; 1843 if (0 != 1844 EXTRACTOR_forensic_emit_text_ (ctx->ec, 1845 "pecoff", 1846 EXTRACTOR_METATYPE_SIGNER, 1847 (const char *) &buf[i + 7], 1848 vlen)) 1849 { 1850 ret = 1; 1851 break; 1852 } 1853 i += 6 + vlen; 1854 } 1855 free (buf); 1856 if ( (0 == ret) && 1857 (0 == found) ) 1858 ret = EXTRACTOR_forensic_emit_ (ctx->ec, 1859 "pecoff", 1860 EXTRACTOR_METATYPE_SIGNER, 1861 "present"); 1862 return ret; 1863 } 1864 1865 1866 /** 1867 * Report the entropy of the most disordered section. 1868 * 1869 * A section above roughly 7.2 bits per byte holds compressed, 1870 * encrypted or packed data rather than code, which is the cheapest 1871 * signal there is that an executable is not what it appears to be. 1872 * 1873 * @param ctx image being parsed 1874 * @return 1 if the caller should stop extracting, 0 to continue 1875 */ 1876 static int 1877 pe_parse_entropy (struct pe_context *ctx) 1878 { 1879 unsigned char *buf; 1880 double best = -1.0; 1881 const char *best_name = NULL; 1882 unsigned int sampled = 0; 1883 1884 if (NULL == (buf = malloc (PE_ENTROPY_SAMPLE))) 1885 return 0; 1886 for (unsigned int i = 0; 1887 (i < ctx->num_sections) && (sampled < PE_MAX_ENTROPY_SECTIONS); 1888 i++) 1889 { 1890 const struct pe_section *s = &ctx->sections[i]; 1891 size_t len = s->raw_size; 1892 double e; 1893 1894 if (s->raw_size < PE_MIN_ENTROPY_SECTION) 1895 continue; 1896 if (((uint64_t) s->raw_ptr) >= ctx->fsize) 1897 continue; 1898 if (len > ctx->fsize - s->raw_ptr) 1899 len = (size_t) (ctx->fsize - s->raw_ptr); 1900 if (len > PE_ENTROPY_SAMPLE) 1901 len = PE_ENTROPY_SAMPLE; 1902 if (len < PE_MIN_ENTROPY_SECTION) 1903 continue; 1904 if (! EXTRACTOR_forensic_read_ (ctx->ec, 1905 (int64_t) s->raw_ptr, 1906 buf, 1907 len)) 1908 continue; 1909 sampled++; 1910 e = EXTRACTOR_forensic_entropy_ (buf, 1911 len); 1912 if (e > best) 1913 { 1914 best = e; 1915 best_name = s->name; 1916 } 1917 } 1918 free (buf); 1919 if (NULL == best_name) 1920 return 0; 1921 return EXTRACTOR_forensic_emit_ (ctx->ec, 1922 "pecoff", 1923 EXTRACTOR_METATYPE_ENTROPY, 1924 "%.2f bits/byte (%s)", 1925 best, 1926 best_name); 1927 } 1928 1929 1930 /* ------------------------------------------------------------------ */ 1931 /* entry point */ 1932 /* ------------------------------------------------------------------ */ 1933 1934 1935 /** 1936 * Main entry method for the PE/COFF extraction plugin. 1937 * 1938 * @param ec extraction context provided to the plugin 1939 */ 1940 void 1941 EXTRACTOR_pecoff_extract_method (struct EXTRACTOR_ExtractContext *ec); 1942 1943 void 1944 EXTRACTOR_pecoff_extract_method (struct EXTRACTOR_ExtractContext *ec) 1945 { 1946 struct pe_context ctx; 1947 unsigned char dos[64]; 1948 unsigned char coff[24]; 1949 unsigned char opt[256]; 1950 uint32_t e_lfanew; 1951 uint16_t machine; 1952 uint16_t nsec; 1953 uint32_t timestamp; 1954 uint16_t opt_size; 1955 uint16_t characteristics; 1956 uint16_t magic; 1957 uint16_t subsystem; 1958 uint16_t dll_flags; 1959 uint32_t entry_point; 1960 uint32_t num_dirs; 1961 size_t dir_at; 1962 uint32_t dirs[16][2]; 1963 const char *s; 1964 char list[256]; 1965 1966 memset (&ctx, 1967 0, 1968 sizeof (ctx)); 1969 ctx.ec = ec; 1970 /* Bail out on the first two bytes: almost nothing we are handed is a 1971 PE, and the magic is at offset zero. */ 1972 if (! EXTRACTOR_forensic_read_ (ec, 1973 0, 1974 dos, 1975 sizeof (dos))) 1976 return; 1977 if ( ('M' != dos[0]) || 1978 ('Z' != dos[1]) ) 1979 return; 1980 ctx.fsize = ec->get_size (ec->cls); 1981 if ( (0 == ctx.fsize) || 1982 (UINT64_MAX == ctx.fsize) ) 1983 return; 1984 e_lfanew = EXTRACTOR_forensic_le32_ (&dos[0x3C]); 1985 if ( (e_lfanew < sizeof (dos)) || 1986 (((uint64_t) e_lfanew) + 24 > ctx.fsize) ) 1987 return; 1988 if (! EXTRACTOR_forensic_read_ (ec, 1989 (int64_t) e_lfanew, 1990 coff, 1991 sizeof (coff))) 1992 return; 1993 if ( ('P' != coff[0]) || 1994 ('E' != coff[1]) || 1995 ('\0' != coff[2]) || 1996 ('\0' != coff[3]) ) 1997 return; 1998 machine = EXTRACTOR_forensic_le16_ (&coff[4]); 1999 nsec = EXTRACTOR_forensic_le16_ (&coff[6]); 2000 timestamp = EXTRACTOR_forensic_le32_ (&coff[8]); 2001 opt_size = EXTRACTOR_forensic_le16_ (&coff[20]); 2002 characteristics = EXTRACTOR_forensic_le16_ (&coff[22]); 2003 if (0 != 2004 ec->proc (ec->cls, 2005 "pecoff", 2006 EXTRACTOR_METATYPE_MIMETYPE, 2007 EXTRACTOR_METAFORMAT_UTF8, 2008 "text/plain", 2009 "application/vnd.microsoft.portable-executable", 2010 strlen ("application/vnd.microsoft.portable-executable") + 1)) 2011 return; 2012 if (opt_size < 96) 2013 return; /* an object file, not an image */ 2014 memset (opt, 2015 0, 2016 sizeof (opt)); 2017 if (! EXTRACTOR_forensic_read_ (ec, 2018 (int64_t) e_lfanew + 24, 2019 opt, 2020 (opt_size > sizeof (opt)) 2021 ? sizeof (opt) 2022 : opt_size)) 2023 return; 2024 magic = EXTRACTOR_forensic_le16_ (&opt[0]); 2025 if (0x20b == magic) 2026 ctx.pe32plus = 1; 2027 else if (0x10b != magic) 2028 return; /* 0x107 is a ROM image, which has no optional header */ 2029 if (ctx.pe32plus && (opt_size < 112)) 2030 return; /* the 64-bit header cannot be that short */ 2031 entry_point = EXTRACTOR_forensic_le32_ (&opt[16]); 2032 subsystem = EXTRACTOR_forensic_le16_ (&opt[68]); 2033 dll_flags = EXTRACTOR_forensic_le16_ (&opt[70]); 2034 ctx.size_of_headers = EXTRACTOR_forensic_le32_ (&opt[60]); 2035 dir_at = ctx.pe32plus ? 112 : 96; 2036 num_dirs = EXTRACTOR_forensic_le32_ (&opt[dir_at - 4]); 2037 if (num_dirs > 16) 2038 num_dirs = 16; 2039 memset (dirs, 2040 0, 2041 sizeof (dirs)); 2042 for (uint32_t i = 0; i < num_dirs; i++) 2043 { 2044 if (dir_at + ((size_t) i) * 8 + 8 > opt_size) 2045 break; 2046 if (dir_at + ((size_t) i) * 8 + 8 > sizeof (opt)) 2047 break; 2048 dirs[i][0] = EXTRACTOR_forensic_le32_ (&opt[dir_at + ((size_t) i) * 8]); 2049 dirs[i][1] = EXTRACTOR_forensic_le32_ (&opt[dir_at + ((size_t) i) * 8 + 4]); 2050 } 2051 2052 if (0 != 2053 EXTRACTOR_forensic_emit_ (ec, 2054 "pecoff", 2055 EXTRACTOR_METATYPE_FORMAT, 2056 "%s", 2057 ctx.pe32plus ? "PE32+" : "PE32")) 2058 return; 2059 if (0 != (characteristics & 0x2000)) 2060 s = "DLL"; 2061 else if (0 != (characteristics & 0x1000)) 2062 s = "system file"; 2063 else if (0 != (characteristics & 0x0002)) 2064 s = "executable"; 2065 else 2066 s = NULL; 2067 if ( (NULL != s) && 2068 (0 != 2069 EXTRACTOR_forensic_emit_ (ec, 2070 "pecoff", 2071 EXTRACTOR_METATYPE_RESOURCE_TYPE, 2072 "%s", 2073 s)) ) 2074 return; 2075 if (NULL != (s = pe_machine_name (machine))) 2076 { 2077 if (0 != 2078 EXTRACTOR_forensic_emit_ (ec, 2079 "pecoff", 2080 EXTRACTOR_METATYPE_TARGET_ARCHITECTURE, 2081 "%s", 2082 s)) 2083 return; 2084 } 2085 else if (0 != 2086 EXTRACTOR_forensic_emit_ (ec, 2087 "pecoff", 2088 EXTRACTOR_METATYPE_TARGET_ARCHITECTURE, 2089 "0x%04x", 2090 (unsigned int) machine)) 2091 { 2092 return; 2093 } 2094 if (0 != 2095 EXTRACTOR_forensic_emit_ (ec, 2096 "pecoff", 2097 EXTRACTOR_METATYPE_TARGET_OS, 2098 "Windows")) 2099 return; 2100 if (NULL != (s = pe_subsystem_name (subsystem))) 2101 { 2102 if (0 != 2103 EXTRACTOR_forensic_emit_ (ec, 2104 "pecoff", 2105 EXTRACTOR_METATYPE_SUBSYSTEM, 2106 "%s", 2107 s)) 2108 return; 2109 } 2110 if (0 != 2111 EXTRACTOR_forensic_emit_ (ec, 2112 "pecoff", 2113 EXTRACTOR_METATYPE_MINIMUM_OS_VERSION, 2114 "%u.%u", 2115 (unsigned int) EXTRACTOR_forensic_le16_ ( 2116 &opt[40]), 2117 (unsigned int) EXTRACTOR_forensic_le16_ ( 2118 &opt[42]))) 2119 return; 2120 if (0 != 2121 EXTRACTOR_forensic_emit_ (ec, 2122 "pecoff", 2123 EXTRACTOR_METATYPE_TOOLCHAIN, 2124 "linker %u.%u", 2125 (unsigned int) opt[2], 2126 (unsigned int) opt[3])) 2127 return; 2128 if (0 != 2129 EXTRACTOR_forensic_emit_ (ec, 2130 "pecoff", 2131 EXTRACTOR_METATYPE_ENTRY_POINT, 2132 "0x%08x", 2133 (unsigned int) entry_point)) 2134 return; 2135 /* A reproducible build puts a hash here rather than a time; the 2136 helper drops anything outside a plausible range, which is exactly 2137 the behaviour we want. */ 2138 if (0 != 2139 EXTRACTOR_forensic_emit_unix_time_ (ec, 2140 "pecoff", 2141 EXTRACTOR_METATYPE_BUILD_DATE, 2142 (int64_t) timestamp)) 2143 return; 2144 list[0] = '\0'; 2145 if (0 != (dll_flags & 0x0020)) 2146 pe_append (list, sizeof (list), "HIGH_ENTROPY_VA"); 2147 if (0 != (dll_flags & 0x0040)) 2148 pe_append (list, sizeof (list), "DYNAMIC_BASE"); 2149 if (0 != (dll_flags & 0x0080)) 2150 pe_append (list, sizeof (list), "FORCE_INTEGRITY"); 2151 if (0 != (dll_flags & 0x0100)) 2152 pe_append (list, sizeof (list), "NX_COMPAT"); 2153 if (0 != (dll_flags & 0x0200)) 2154 pe_append (list, sizeof (list), "NO_ISOLATION"); 2155 if (0 != (dll_flags & 0x0400)) 2156 pe_append (list, sizeof (list), "NO_SEH"); 2157 if (0 != (dll_flags & 0x0800)) 2158 pe_append (list, sizeof (list), "NO_BIND"); 2159 if (0 != (dll_flags & 0x1000)) 2160 pe_append (list, sizeof (list), "APPCONTAINER"); 2161 if (0 != (dll_flags & 0x2000)) 2162 pe_append (list, sizeof (list), "WDM_DRIVER"); 2163 if (0 != (dll_flags & 0x4000)) 2164 pe_append (list, sizeof (list), "GUARD_CF"); 2165 if (0 != (dll_flags & 0x8000)) 2166 pe_append (list, sizeof (list), "TERMINAL_SERVER_AWARE"); 2167 if ( ('\0' != list[0]) && 2168 (0 != 2169 EXTRACTOR_forensic_emit_ (ec, 2170 "pecoff", 2171 EXTRACTOR_METATYPE_SECURITY_MITIGATIONS, 2172 "%s", 2173 list)) ) 2174 return; 2175 2176 /* section table */ 2177 if (nsec > PE_MAX_SECTIONS) 2178 nsec = PE_MAX_SECTIONS; 2179 for (uint16_t i = 0; i < nsec; i++) 2180 { 2181 struct pe_section *sec = &ctx.sections[ctx.num_sections]; 2182 unsigned char sh[40]; 2183 uint64_t at = ((uint64_t) e_lfanew) + 24 + opt_size + ((uint64_t) i) * 40; 2184 2185 if (at + sizeof (sh) > ctx.fsize) 2186 break; 2187 if (! EXTRACTOR_forensic_read_ (ec, 2188 (int64_t) at, 2189 sh, 2190 sizeof (sh))) 2191 break; 2192 memcpy (sec->name, 2193 sh, 2194 8); 2195 sec->name[8] = '\0'; 2196 for (unsigned int k = 0; k < 8; k++) 2197 { 2198 unsigned char c = (unsigned char) sec->name[k]; 2199 2200 if ( (c < 0x20) || 2201 (c > 0x7E) ) 2202 sec->name[k] = '\0'; 2203 } 2204 sec->vsize = EXTRACTOR_forensic_le32_ (&sh[8]); 2205 sec->vaddr = EXTRACTOR_forensic_le32_ (&sh[12]); 2206 sec->raw_size = EXTRACTOR_forensic_le32_ (&sh[16]); 2207 sec->raw_ptr = EXTRACTOR_forensic_le32_ (&sh[20]); 2208 /* Clamp what the section claims to have in the file to what the 2209 file actually holds, once, so that everything downstream can 2210 trust raw_ptr + raw_size. */ 2211 if (((uint64_t) sec->raw_ptr) >= ctx.fsize) 2212 sec->raw_size = 0; 2213 else if (sec->raw_size > ctx.fsize - sec->raw_ptr) 2214 sec->raw_size = (uint32_t) (ctx.fsize - sec->raw_ptr); 2215 ctx.num_sections++; 2216 } 2217 2218 /* Rich header, in the gap between the DOS header and the PE header */ 2219 if (e_lfanew > sizeof (dos)) 2220 { 2221 size_t stub_len = e_lfanew - sizeof (dos); 2222 unsigned char *stub; 2223 2224 if (stub_len > PE_MAX_DOS_STUB) 2225 stub_len = PE_MAX_DOS_STUB; 2226 if (NULL != (stub = malloc (stub_len))) 2227 { 2228 int stop = 0; 2229 2230 if (EXTRACTOR_forensic_read_ (ec, 2231 (int64_t) sizeof (dos), 2232 stub, 2233 stub_len)) 2234 stop = pe_parse_rich (ec, 2235 stub, 2236 stub_len); 2237 free (stub); 2238 if (stop) 2239 return; 2240 } 2241 } 2242 2243 if ( (0 != dirs[6][0]) && 2244 (0 != pe_parse_debug (&ctx, 2245 dirs[6][0], 2246 dirs[6][1])) ) 2247 return; 2248 if ( (0 != dirs[2][0]) && 2249 (0 != pe_parse_resources (&ctx, 2250 dirs[2][0], 2251 dirs[2][1])) ) 2252 return; 2253 if ( (0 != dirs[1][0]) && 2254 (0 != pe_parse_imports (&ctx, 2255 dirs[1][0], 2256 dirs[1][1])) ) 2257 return; 2258 if ( (0 != dirs[0][0]) && 2259 (0 != pe_parse_exports (&ctx, 2260 dirs[0][0], 2261 dirs[0][1])) ) 2262 return; 2263 /* Data directory 4 is the one field in the whole format that holds a 2264 file offset rather than an address. */ 2265 if ( (0 != dirs[4][0]) && 2266 (0 != pe_parse_certificates (&ctx, 2267 dirs[4][0], 2268 dirs[4][1])) ) 2269 return; 2270 (void) pe_parse_entropy (&ctx); 2271 } 2272 2273 2274 /* end of pecoff_extractor.c */