-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexport.py
More file actions
128 lines (100 loc) · 4.48 KB
/
Copy pathexport.py
File metadata and controls
128 lines (100 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from __future__ import annotations
from dataclasses import dataclass
from functools import cached_property
from typing import TYPE_CHECKING
from dissect.util.ts import from_unix
from dissect.executable.pe.c_pe import c_pe
from dissect.executable.pe.directory.base import DataDirectory
if TYPE_CHECKING:
import datetime
from collections.abc import Iterator
class ExportDirectory(DataDirectory):
"""The export directory of a PE file."""
def __repr__(self) -> str:
return f"<ExportDirectory name={self.name!r} functions={len(self.functions)}>"
def __iter__(self) -> Iterator[ExportFunction]:
return iter(self.functions)
def __getitem__(self, idx: str | int) -> ExportFunction:
if isinstance(idx, int):
return self._by_ordinal[idx]
if isinstance(idx, str):
if idx not in self._by_name:
raise KeyError(f"Export function {idx!r} not found in directory {self.name!r}")
return self._by_name[idx]
raise TypeError(f"ImportModule indices must be str or int, not {type(idx).__name__}")
def __contains__(self, idx: str | int) -> bool:
if isinstance(idx, int):
return idx in self._by_ordinal
if isinstance(idx, str):
return idx in self._by_name
return False
@cached_property
def header(self) -> c_pe.IMAGE_EXPORT_DIRECTORY:
"""The export directory header."""
self.pe.vfh.seek(self.address)
return c_pe.IMAGE_EXPORT_DIRECTORY(self.pe.vfh)
@property
def timestamp(self) -> datetime.datetime | None:
"""The timestamp of the export directory, or ``None`` if the PE file is compiled as reproducible."""
if self.pe.is_reproducible():
return None
return from_unix(self.header.TimeDateStamp)
@cached_property
def name(self) -> str | None:
"""The name of the export directory, if available."""
if self.header.Name:
self.pe.vfh.seek(self.header.Name)
return c_pe.char[None](self.pe.vfh).decode()
return None
@property
def base(self) -> int:
"""The base ordinal of the exported functions."""
return self.header.Base
@cached_property
def functions(self) -> list[ExportFunction]:
"""List of exported functions."""
result = []
self.pe.vfh.seek(self.header.AddressOfFunctions)
addresses = c_pe.ULONG[self.header.NumberOfFunctions](self.pe.vfh)
self.pe.vfh.seek(self.header.AddressOfNames)
names = c_pe.ULONG[self.header.NumberOfNames](self.pe.vfh)
self.pe.vfh.seek(self.header.AddressOfNameOrdinals)
ordinals = c_pe.USHORT[self.header.NumberOfNames](self.pe.vfh)
for name_ptr, ordinal in zip(names, ordinals, strict=False):
self.pe.vfh.seek(name_ptr)
name = c_pe.CHAR[None](self.pe.vfh).decode()
address = addresses[ordinal]
forwarder = None
if self.address <= address < self.address + self.size:
self.pe.vfh.seek(address)
forwarder = c_pe.CHAR[None](self.pe.vfh).decode()
result.append(ExportFunction(self, ordinal, name, address, forwarder))
return result
@cached_property
def _by_name(self) -> dict[str, ExportFunction]:
"""A mapping of exported function names to their :class:`ExportFunction`."""
return {func.name: func for func in self.functions}
@cached_property
def _by_ordinal(self) -> dict[int, ExportFunction]:
"""A mapping of exported function ordinals to their :class:`ExportFunction`."""
return {func.ordinal: func for func in self.functions}
@dataclass
class ExportFunction:
directory: ExportDirectory
"""The export directory this function belongs to."""
unbiased_ordinal: int
"""The unbiased ordinal of the exported function."""
name: str
"""The name of the exported function."""
address: int
"""The address of the exported function."""
forwarder: str | None = None
"""The forwarder of the exported function, if it is a forwarder."""
def __repr__(self) -> str:
if self.forwarder:
return f"<ExportFunction ordinal={self.ordinal} name={self.name!r} forwarder={self.forwarder!r}>"
return f"<ExportFunction ordinal={self.ordinal} name={self.name!r} address={self.address:#x}>"
@property
def ordinal(self) -> int:
"""The unbiased ordinal with the base ordinal added."""
return self.unbiased_ordinal + self.directory.base