This repository was archived by the owner on Nov 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path__init__.py
101 lines (83 loc) · 3.94 KB
/
__init__.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
# Copyright(c) 2020 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 binaryninja import BackgroundTaskThread
from binaryninja.binaryview import BinaryReader
from binaryninja.log import log_error, log_warn, log_alert
from binaryninja.interaction import OpenFileNameField, get_form_input
from binaryninja.plugin import PluginCommand
from .bridge import BinjaBridge
from .mapped_model import AnalysisSession
from elftools.elf.elffile import ELFFile
import os
class DWARF_loader(BackgroundTaskThread):
def __init__(self, bv, debug_file=None):
BackgroundTaskThread.__init__(self)
self.view = bv
self.debug_file = debug_file
self.progress = ""
def run(self):
# Open the binary.
analysis_session = AnalysisSession(binary_view = self.view, debug_file = self.debug_file)
if analysis_session.binary_view is None or analysis_session.binary_view.arch is None:
log_error("Unable to import dwarf")
# Setup the translator.
bridge = BinjaBridge(analysis_session)
bridge.translate_model()
# Finalize the analysis.
analysis_session.binary_view.update_analysis()
def load_symbols(bv):
log_alert("This plugin is deprecated. While it will still work, we suggest that you uninstall this plugin and use our new one. See this plugin's readme.md for details.")
try:
if bv.query_metadata("dwarf_info_applied") == 1:
log_warn("DWARF Debug Info has already been applied to this binary view")
return
except KeyError:
bv.store_metadata("dwarf_info_applied", True)
DWARF_loader(bv).start()
def load_symbols_from_file(bv):
log_alert("This plugin is deprecated. While it will still work, we suggest that you uninstall this plugin and use our new one. See this plugin's readme.md for details.")
try:
if bv.query_metadata("dwarf_info_applied") == 1:
log_warn("DWARF Debug Info has already been applied to this binary view")
return
except KeyError:
bv.store_metadata("dwarf_info_applied", True)
file_choice = OpenFileNameField("Debug file")
get_form_input([file_choice], "Open debug file")
if not file_choice.result or not os.path.exists(file_choice.result):
log_error(f"Input file `{file_choice.result}` does not exist")
bv.store_metadata("dwarf_info_applied", False)
return
DWARF_loader(bv, file_choice.result).start()
def is_valid(bv):
raw = False
elf = False
if bv.parent_view is None:
return False
for view in bv.parent_view.available_view_types:
if view.name == "ELF":
elf = True
elif view.name == "Raw":
raw = True
reader = BinaryReader(bv.file.raw)
reader.tell = lambda: reader.offset
return raw and elf and ELFFile(reader).has_dwarf_info()
PluginCommand.register("DWARF Import (Deprecated)\\Load DWARF Symbols", "Load DWARF Symbols from the current file", load_symbols, is_valid)
PluginCommand.register("DWARF Import (Deprecated)\\Load DWARF Symbols From File", "Load DWARF Symbols from another file", load_symbols_from_file, lambda bv: True)