-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathsharedcache.py
261 lines (206 loc) · 6.14 KB
/
sharedcache.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import os
import ctypes
import dataclasses
import traceback
import binaryninja
from binaryninja._binaryninjacore import BNFreeStringList, BNAllocString, BNFreeString
from . import _sharedcachecore as sccore
from .sharedcache_enums import *
@dataclasses.dataclass
class DSCMemoryMapping:
name: str
vmAddress: int
size: int
def __str__(self):
return repr(self)
def __repr__(self):
return f"<DSCMemoryMapping '{self.name}': {self.vmAddress:x}+{self.size:x}>"
@dataclasses.dataclass
class LoadedRegion:
name: str
headerAddress: int
mappings: list[DSCMemoryMapping]
def __str__(self):
return repr(self)
def __repr__(self):
return f"<LoadedRegion {self.name} @ {self.headerAddress:x}>"
@dataclasses.dataclass
class DSCBackingCacheMapping:
vmAddress: int
size: int
fileOffset: int
def __str__(self):
return repr(self)
def __repr__(self):
return f"<DSCBackingCacheMapping {self.vmAddress:x}+{self.size:x} @ {self.fileOffset:x}"
@dataclasses.dataclass
class DSCBackingCache:
path: str
cacheType: BackingCacheType
mappings: list[DSCBackingCacheMapping]
def __str__(self):
return repr(self)
def __repr__(self):
match self.cacheType:
case BackingCacheType.BackingCacheTypePrimary:
cacheTypeStr = 'Primary'
case BackingCacheType.BackingCacheTypeSecondary:
cacheTypeStr = 'Secondary'
case BackingCacheType.BackingCacheTypeSymbols:
cacheTypeStr = 'Symbols'
return f"<DSCBackingCache {self.path} {cacheTypeStr} | {len(self.mappings)} mappings>"
@dataclasses.dataclass
class DSCImageMemoryMapping:
filePath: str
name: str
vmAddress: int
size: int
loaded: bool
rawViewOffset: int
def __str__(self):
return repr(self)
def __repr__(self):
return f"<DSCImageMemoryMapping '{self.name}' {os.path.basename(self.filePath)} raw<{self.rawViewOffset:x}>: {self.vmAddress:x}+{self.size:x}>"
@dataclasses.dataclass
class DSCImage:
name: str
headerAddress: int
mappings: list[DSCImageMemoryMapping]
def __str__(self):
return repr(self)
def __repr__(self):
return f"<DSCImage {self.name} @ {self.headerAddress:x}>"
@dataclasses.dataclass
class DSCSymbol:
name: str
image: str
address: int
def __str__(self):
return repr(self)
def __repr__(self):
return f"<DSCSymbol {self.name} @ {self.address:x} ({self.image}>"
class SharedCache:
def __init__(self, view):
self.handle = sccore.BNGetSharedCache(view.handle)
def load_image_with_install_name(self, installName):
return sccore.BNDSCViewLoadImageWithInstallName(self.handle, installName)
def load_section_at_address(self, addr):
return sccore.BNDSCViewLoadSectionAtAddress(self.handle, addr)
def load_image_containing_address(self, addr):
return sccore.BNDSCViewLoadImageContainingAddress(self.handle, addr)
@property
def caches(self):
count = ctypes.c_ulonglong()
value = sccore.BNDSCViewGetBackingCaches(self.handle, count)
if value is None:
return []
result = []
for i in range(count.value):
mappings = []
for j in range(value[i].mappingCount):
mapping = DSCBackingCacheMapping(
value[i].mappings[j].vmAddress,
value[i].mappings[j].size,
value[i].mappings[j].fileOffset
)
mappings.append(mapping)
result.append(DSCBackingCache(
value[i].path,
value[i].cacheType,
mappings
))
sccore.BNDSCViewFreeBackingCaches(value, count)
return result
@property
def images(self):
count = ctypes.c_ulonglong()
value = sccore.BNDSCViewGetAllImages(self.handle, count)
if value is None:
return []
result = []
for i in range(count.value):
mappings = []
for j in range(value[i].mappingCount):
mapping = DSCImageMemoryMapping(
value[i].mappings[j].filePath,
value[i].mappings[j].name,
value[i].mappings[j].vmAddress,
value[i].mappings[j].size,
value[i].mappings[j].loaded,
value[i].mappings[j].rawViewOffset
)
mappings.append(mapping)
result.append(DSCImage(
value[i].name,
value[i].headerAddress,
mappings
))
sccore.BNDSCViewFreeAllImages(value, count)
return result
@property
def loaded_regions(self):
"""
Get all loaded regions in the shared cache
The internal logic for loading images treats a region as 'loaded' whenever
that region has been mapped into memory, and, if it's located within an image, header information has been applied to that region.
Individual segments within an image can be loaded independently of the image itself.
Only once all regions of an image are loaded will the header processor refuse to run on that region.
:return:
"""
count = ctypes.c_ulonglong()
value = sccore.BNDSCViewGetLoadedRegions(self.handle, count)
if value is None:
return []
result = []
for i in range(count.value):
mapping = DSCMemoryMapping(
value[i].name,
value[i].vmAddress,
value[i].size,
)
result.append(mapping)
sccore.BNDSCViewFreeLoadedRegions(value, count)
return result
def load_all_symbols_and_wait(self):
count = ctypes.c_ulonglong()
value = sccore.BNDSCViewLoadAllSymbolsAndWait(self.handle, count)
if value is None:
return []
result = []
for i in range(count.value):
sym = DSCSymbol(
value[i].name,
value[i].image,
value[i].address
)
result.append(sym)
sccore.BNDSCViewFreeSymbols(value, count)
return result
@property
def image_names(self):
count = ctypes.c_ulonglong()
value = sccore.BNDSCViewGetInstallNames(self.handle, count)
if value is None:
return []
result = []
for i in range(count.value):
result.append(value[i].decode('utf-8'))
BNFreeStringList(value, count)
return result
@property
def state(self):
return DSCViewState(sccore.BNDSCViewGetState(self.handle))
def get_name_for_address(self, address):
name = sccore.BNDSCViewGetNameForAddress(self.handle, address)
if name is None:
return ""
result = name
return result
def get_image_name_for_address(self, address):
name = sccore.BNDSCViewGetImageNameForAddress(self.handle, address)
if name is None:
return ""
result = name
return result
def find_symbol_at_addr_and_apply_to_addr(self, symbolAddress, targetAddress, triggerReanalysis) -> None:
sccore.BNDSCFindSymbolAtAddressAndApplyToAddress(self.handle, symbolAddress, targetAddress, triggerReanalysis)