-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathkernelcache.py
More file actions
210 lines (177 loc) · 6.19 KB
/
Copy pathkernelcache.py
File metadata and controls
210 lines (177 loc) · 6.19 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
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
import ctypes
import dataclasses
from typing import Optional
import binaryninja
from binaryninja import BinaryView
from binaryninja._binaryninjacore import BNFreeStringList, BNAllocString, BNFreeString
from . import _kernelcachecore as kccore
from .kernelcache_enums import *
@dataclasses.dataclass
class CacheImage:
name: str
header_virtual_address: int
header_file_address: int
def __str__(self):
return repr(self)
def __repr__(self):
return f"<CacheImage '{self.name}': 0x{self.header_virtual_address:x} (@0x{self.header_file_address:x})>"
@dataclasses.dataclass
class CacheSymbol:
symbol_type: kccore.SymbolTypeEnum
address: int
name: str
def __str__(self):
return repr(self)
def __repr__(self):
return f"<CacheSymbol '{self.name}': 0x{self.address:x}>"
def image_from_api(image: kccore.BNKernelCacheImage) -> CacheImage:
return CacheImage(
name=image.name,
header_file_address=image.headerFileAddress,
header_virtual_address=image.headerVirtualAddress
)
def image_to_api(image: CacheImage) -> kccore.BNKernelCacheImage:
return kccore.BNKernelCacheImage(
_name=BNAllocString(image.name),
headerFileAddress=image.header_file_address,
headerVirtualAddress=image.header_virtual_address,
)
def symbol_from_api(symbol: kccore.BNKernelCacheSymbol) -> CacheSymbol:
return CacheSymbol(
symbol_type=symbol.symbolType,
address=symbol.address,
name=symbol.name
)
def symbol_to_api(symbol: CacheSymbol) -> kccore.BNKernelCacheSymbol:
return kccore.BNKernelCacheSymbol(
symbolType=symbol.symbol_type,
address=symbol.address,
_name=BNAllocString(symbol.name)
)
class KernelCacheController:
def __init__(self, view: BinaryView):
"""
Retrieve the shared cache controller for a given view.
Call `is_valid` to check if the controller is valid.
"""
self.handle = kccore.BNGetKernelCacheController(view.handle)
def __del__(self):
if self.handle is not None:
kccore.BNFreeKernelCacheControllerReference(self.handle)
def __str__(self):
return repr(self)
def __repr__(self):
return f"<KernelCacheController: {len(self.images)} images>"
def is_valid(self) -> bool:
return self.handle is not None
def apply_image(self, view: BinaryView, image: CacheImage) -> bool:
api_image: kccore.BNKernelCacheImage = image_to_api(image)
result = kccore.BNKernelCacheControllerApplyImage(self.handle, view.handle, api_image)
kccore.BNKernelCacheFreeImage(api_image)
return result
def is_image_loaded(self, image: CacheImage) -> bool:
api_image: kccore.BNKernelCacheImage = image_to_api(image)
result = kccore.BNKernelCacheControllerIsImageLoaded(self.handle, api_image)
kccore.BNKernelCacheFreeImage(api_image)
return result
def get_image_at(self, address: int) -> Optional[CacheImage]:
api_image = kccore.BNKernelCacheImage()
if not kccore.BNKernelCacheControllerGetImageAt(self.handle, address, api_image):
return None
image = image_from_api(api_image)
kccore.BNKernelCacheFreeImage(api_image)
return image
def get_image_containing(self, address: int) -> Optional[CacheImage]:
api_image = kccore.BNKernelCacheImage()
if not kccore.BNKernelCacheControllerGetImageContaining(self.handle, address, api_image):
return None
image = image_from_api(api_image)
kccore.BNKernelCacheFreeImage(api_image)
return image
def get_image_with_name(self, name: str) -> Optional[CacheImage]:
api_image = kccore.BNKernelCacheImage()
if not kccore.BNKernelCacheControllerGetImageWithName(self.handle, name, api_image):
return None
image = image_from_api(api_image)
kccore.BNKernelCacheFreeImage(api_image)
return image
def get_image_dependencies(self, image: CacheImage) -> [str]:
"""
Returns a list of image names that this image depends on.
"""
count = ctypes.c_ulonglong()
api_image: kccore.BNKernelCacheImage = image_to_api(image)
value = kccore.BNKernelCacheControllerGetImageDependencies(self.handle, api_image, count)
kccore.BNKernelCacheFreeImage(api_image)
if value is None:
return []
result = []
for i in range(count.value):
result.append(value[i].decode("utf-8"))
BNFreeStringList(value, count)
return result
def get_symbol_at(self, address: int) -> Optional[CacheSymbol]:
api_symbol = kccore.BNKernelCacheSymbol()
if not kccore.BNKernelCacheControllerGetSymbolAt(self.handle, address, api_symbol):
return None
symbol = symbol_from_api(api_symbol)
kccore.BNKernelCacheFreeSymbol(api_symbol)
return symbol
def get_symbol_with_name(self, name: str) -> Optional[CacheSymbol]:
api_symbol = kccore.BNKernelCacheSymbol()
if not kccore.BNKernelCacheControllerGetSymbolWithName(self.handle, name, api_symbol):
return None
symbol = symbol_from_api(api_symbol)
kccore.BNKernelCacheFreeSymbol(api_symbol)
return symbol
@property
def images(self) -> [CacheImage]:
count = ctypes.c_ulonglong()
value = kccore.BNKernelCacheControllerGetImages(self.handle, count)
if value is None:
return []
result = []
for i in range(count.value):
result.append(image_from_api(value[i]))
kccore.BNKernelCacheFreeImageList(value, count)
return result
@property
def loaded_images(self) -> [CacheImage]:
"""
Get a list of images that are currently loaded in the view.
"""
count = ctypes.c_ulonglong()
value = kccore.BNKernelCacheControllerGetLoadedImages(self.handle, count)
if value is None:
return []
result = []
for i in range(count.value):
result.append(image_from_api(value[i]))
kccore.BNKernelCacheFreeImageList(value, count)
return result
@property
def symbols(self) -> [CacheSymbol]:
count = ctypes.c_ulonglong()
value = kccore.BNKernelCacheControllerGetSymbols(self.handle, count)
if value is None:
return []
result = []
for i in range(count.value):
result.append(symbol_from_api(value[i]))
kccore.BNKernelCacheFreeSymbolList(value, count)
return result
def _get_kernel_cache(instance: binaryninja.PythonScriptingInstance):
if instance.interpreter.active_view is None:
return None
controller = KernelCacheController(instance.interpreter.active_view)
if not controller.is_valid():
return None
return controller
binaryninja.PythonScriptingProvider.register_magic_variable(
"kc",
_get_kernel_cache
)
binaryninja.PythonScriptingProvider.register_magic_variable(
"kernel_cache",
_get_kernel_cache
)