-
Notifications
You must be signed in to change notification settings - Fork 7
/
mapped_model.py
84 lines (73 loc) · 3.19 KB
/
mapped_model.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
# Copyright(c) 2021-2023 Vector 35 Inc
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files(the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and / or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
from typing import Optional
import os
from binaryninja import BinaryView, BinaryViewType
from .model.analysis_model import AnalysisModel
from .mapping import BinjaMap
class AnalysisSession(object):
def __init__(
self,
filename: Optional[str] = None,
debug_root: Optional[str] = None,
debug_file: Optional[str] = None,
binary_view: Optional[BinaryView] = None,
logger=None
):
if filename is None and binary_view is None:
raise ValueError('Must specify either a filename or binary view.')
self.model: AnalysisModel
self.binary_view: BinaryView
self.debug_root = debug_root
self.debug_file = debug_file
if binary_view is None:
if filename is None:
raise ValueError('A filename must be specified when binary_view is None.')
bv = BinaryViewType.get_view_of_file(filename, update_analysis=False)
if bv is None:
raise Exception(f'Unable to get binary view for file: {filename}')
self.binary_view = bv
else:
self.binary_view = binary_view
if self.binary_view.arch is None:
raise Exception('The binary view has no valid architecture.')
# Create the root module with the binary name as the module name.
if filename is None:
filename = self.binary_view.file.original_filename
assert(isinstance(filename, str))
binary_name = os.path.basename(filename)
debug_source = filename
if self.debug_file is not None:
debug_source = self.debug_file
m = AnalysisModel.from_dwarf(debug_source, self.debug_root, name=binary_name, logger=logger)
self.model = m if m else AnalysisModel(binary_name)
assert(self.binary_view.arch)
self.arch = self.binary_view.arch
assert(self.binary_view.platform)
self.platform = self.binary_view.platform
self.mapping = BinjaMap(self.binary_view)
def __del__(self):
if hasattr(self, 'binary_view') and self.binary_view is not None:
self.binary_view.abort_analysis()
self.binary_view.file.close() # type: ignore
del self.binary_view
# @property
# def name(self) -> str:
# return os.path.basename(self.binary_view.file.original_filename)