|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from functools import cached_property |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +from dissect.util.ts import from_unix |
| 7 | + |
| 8 | +from dissect.executable.pe.c_pe import c_pe |
| 9 | +from dissect.executable.pe.directory.base import DataDirectory |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + import datetime |
| 13 | + |
| 14 | + |
| 15 | +class BoundImportDirectory(DataDirectory): |
| 16 | + """The bound import directory of a PE file.""" |
| 17 | + |
| 18 | + def __repr__(self) -> str: |
| 19 | + return f"<BoundImportDirectory modules={len(self.modules)}>" |
| 20 | + |
| 21 | + def __len__(self) -> int: |
| 22 | + return len(self.modules) |
| 23 | + |
| 24 | + def __getitem__(self, idx: str | int) -> BoundImportModule: |
| 25 | + if isinstance(idx, int): |
| 26 | + return self.modules[idx] |
| 27 | + if isinstance(idx, str): |
| 28 | + if idx not in self._by_name: |
| 29 | + raise KeyError(f"Bound import module {idx!r} not found") |
| 30 | + return self._by_name[idx] |
| 31 | + raise TypeError(f"BoundImportDirectory indices must be str or int, not {type(idx).__name__}") |
| 32 | + |
| 33 | + def __contains__(self, name: str) -> bool: |
| 34 | + if isinstance(name, str): |
| 35 | + return name in self._by_name |
| 36 | + return False |
| 37 | + |
| 38 | + @cached_property |
| 39 | + def modules(self) -> list[BoundImportModule]: |
| 40 | + """List of bound imported modules.""" |
| 41 | + result = [] |
| 42 | + |
| 43 | + self.pe.vfh.seek(self.address) |
| 44 | + while self.pe.vfh.tell() < self.address + self.size: |
| 45 | + descriptor = c_pe.IMAGE_BOUND_IMPORT_DESCRIPTOR(self.pe.vfh) |
| 46 | + if not descriptor: |
| 47 | + break |
| 48 | + |
| 49 | + forwarders = [] |
| 50 | + for _ in range(descriptor.NumberOfModuleForwarderRefs): |
| 51 | + forwarder = c_pe.IMAGE_BOUND_FORWARDER_REF(self.pe.vfh) |
| 52 | + if not forwarder: |
| 53 | + break |
| 54 | + |
| 55 | + forwarders.append(BoundImportForwardReference(self, forwarder)) |
| 56 | + |
| 57 | + result.append(BoundImportModule(self, descriptor, forwarders)) |
| 58 | + |
| 59 | + return result |
| 60 | + |
| 61 | + @cached_property |
| 62 | + def _by_name(self) -> dict[str, BoundImportModule]: |
| 63 | + """A mapping of module names to their :class:`DelayImportModule`.""" |
| 64 | + return {module.name: module for module in self.modules} |
| 65 | + |
| 66 | + |
| 67 | +class BoundImportModule: |
| 68 | + """A module bound imported by a PE file, containing its functions.""" |
| 69 | + |
| 70 | + def __init__( |
| 71 | + self, |
| 72 | + directory: BoundImportDirectory, |
| 73 | + descriptor: c_pe.IMAGE_BOUND_IMPORT_DESCRIPTOR, |
| 74 | + forwarders: list[BoundImportForwardReference], |
| 75 | + ): |
| 76 | + self.directory = directory |
| 77 | + self.descriptor = descriptor |
| 78 | + self.forwarders = forwarders |
| 79 | + |
| 80 | + @property |
| 81 | + def timestamp(self) -> datetime.datetime | None: |
| 82 | + """The timestamp of this bound import module, or ``None`` if the PE file is compiled as reproducible.""" |
| 83 | + if self.directory.pe.is_reproducible(): |
| 84 | + return None |
| 85 | + return from_unix(self.descriptor.TimeDateStamp) |
| 86 | + |
| 87 | + @property |
| 88 | + def name(self) -> str: |
| 89 | + self.directory.pe.vfh.seek(self.directory.address + self.descriptor.OffsetModuleName) |
| 90 | + return c_pe.CHAR[None](self.directory.pe.vfh).decode() |
| 91 | + |
| 92 | + |
| 93 | +class BoundImportForwardReference: |
| 94 | + """A forward reference in a bound import module.""" |
| 95 | + |
| 96 | + def __init__( |
| 97 | + self, |
| 98 | + directory: BoundImportDirectory, |
| 99 | + descriptor: c_pe.IMAGE_BOUND_FORWARDER_REF, |
| 100 | + ): |
| 101 | + self.directory = directory |
| 102 | + self.descriptor = descriptor |
| 103 | + |
| 104 | + @property |
| 105 | + def timestamp(self) -> datetime.datetime | None: |
| 106 | + """The timestamp of this bound import module, or ``None`` if the PE file is compiled as reproducible.""" |
| 107 | + if self.directory.pe.is_reproducible(): |
| 108 | + return None |
| 109 | + return from_unix(self.descriptor.TimeDateStamp) |
| 110 | + |
| 111 | + @property |
| 112 | + def name(self) -> str: |
| 113 | + self.directory.pe.vfh.seek(self.directory.address + self.descriptor.OffsetModuleName) |
| 114 | + return c_pe.CHAR[None](self.directory.pe.vfh).decode() |
0 commit comments