The preferred ImageBase of a PE file, or None for anything else. A PE symboliser wants VA = ImageBase + RVA, and the dump gives RVA + runtime base.
(binary: str)
| 112 | |
| 113 | |
| 114 | def pe_image_base(binary: str) -> int | None: |
| 115 | """The preferred ImageBase of a PE file, or None for anything else. A PE |
| 116 | symboliser wants VA = ImageBase + RVA, and the dump gives RVA + runtime base.""" |
| 117 | try: |
| 118 | with open(binary, "rb") as fh: |
| 119 | head = fh.read(4096) |
| 120 | except OSError: |
| 121 | return None |
| 122 | if head[:2] != b"MZ" or len(head) < 0x40: |
| 123 | return None |
| 124 | pe = int.from_bytes(head[0x3C:0x40], "little") |
| 125 | if head[pe:pe + 4] != b"PE\0\0": |
| 126 | return None |
| 127 | opt = pe + 24 |
| 128 | magic = int.from_bytes(head[opt:opt + 2], "little") |
| 129 | if magic == 0x20B: # PE32+ |
| 130 | return int.from_bytes(head[opt + 24:opt + 32], "little") |
| 131 | if magic == 0x10B: # PE32 |
| 132 | return int.from_bytes(head[opt + 28:opt + 32], "little") |
| 133 | return None |
| 134 | |
| 135 | |
| 136 | def symbolise(binary: str | None, base: str, addrs: list[str]) -> dict[str, str]: |