-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAutoResolv.py
211 lines (164 loc) · 6.22 KB
/
AutoResolv.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
# This file is part of AutoResolv.
# Copyright 2022 - Airbus, thibault poncetta
# AutoResolv is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
# AutoResolv is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with AutoResolv. If not, see <http://www.gnu.org/licenses/>.
import idaapi
import idc
import sys
import idautils
import ida_funcs
import time
import os
import subprocess
import sys
from collections import defaultdict
from libautoresolv.resultshower import ResultShower
from libautoresolv.util import *
from libautoresolv.error import *
from libautoresolv.dbcache import *
from libautoresolv.GUI.gui_main import *
from libautoresolv.GUI.gui_start import *
VERSION = "dev-v0.90p"
idaapi.require("libautoresolv.GUI.gui_main")
idaapi.require("libautoresolv.GUI.gui_export")
idaapi.require("libautoresolv.GUI.gui_start")
class Kp_Menu_Context(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
@classmethod
def get_name(self):
return self.__name__
@classmethod
def get_label(self):
return self.label
@classmethod
def register(self, plugin, label):
self.plugin = plugin
self.label = label
instance = self()
return idaapi.register_action(idaapi.action_desc_t(
self.get_name(), # Name. Acts as an ID. Must be unique.
instance.get_label(), # Label. That's what users see.
instance # Handler. Called when activated, and for updating
))
@classmethod
def unregister(self):
"""Unregister the action.
After unregistering the class cannot be used.
"""
idaapi.unregister_action(self.get_name())
@classmethod
def activate(self, ctx):
# dummy method
return 1
@classmethod
def update(self, ctx):
if ctx.widget_type == idaapi.BWN_DISASM:
return idaapi.AST_ENABLE_FOR_WIDGET
return idaapi.AST_DISABLE_FOR_WIDGET
class Searcher(Kp_Menu_Context):
def activate(self, ctx):
self.plugin.search()
return 1
p_initialized = False
#--------------------------------------------------------------------------
# Plugin
#--------------------------------------------------------------------------
class AutoResolv(idaapi.plugin_t):
comment = "Resolve imports to find libname/paths and refactor call type from signature file"
help = "AutoResolv Help"
wanted_name = "AutoResolv"
flags = idaapi.PLUGIN_KEEP
wanted_hotkey= "Ctrl+Shift-A"
def init(self):
global p_initialized
# register popup menu handlers
try:
Searcher.register(self, "AutoResolv")
except:
pass
if p_initialized is False:
p_initialized = True
idaapi.register_action(idaapi.action_desc_t(
"AutoResolv",
"AutoResolv imports",
Searcher(),
None,
None,
0))
idaapi.attach_action_to_menu("Search", "AutoResolv", idaapi.SETMENU_APP)
print("*" * 80)
print(f"AutoResolv {VERSION} | Thibault Poncetta")
print("*" * 80)
return idaapi.PLUGIN_KEEP
def term(self):
pass
def _decompile_then_write_on_fd(self, fd, funs):
lenf = len(funs)
cpt = 0
for fun in funs:
ea = funs[fun]
cfunc = str(idaapi.decompile(ea)).split("\n")[0] + ";"
print(f"Exporting {cfunc}, Progression: {(cpt/lenf)*100}%")
cpt += 1
fd.write(cfunc + "\n")
def _signature_export(self, fd):
start,end = get_seg(".text")
if start == None or end == None:
print("[AutoResolv] Error when parsing Segments() address.")
funs = get_funs(start, end)
nb_fun = len(funs)
print(f"[AutoResolv] Exporting {nb_fun} function from .text segment")
self._decompile_then_write_on_fd(fd, funs)
print(f"[AutoResolv] Done")
def main(self):
print(f"AutoResolv {VERSION}")
module_path = os.path.dirname(__file__) + "/libautoresolv/db/"
binary_name = idaapi.get_root_filename()
db_path = module_path + ".cache_" + binary_name + ".db"
bin_path = os.getcwd() + "/" + binary_name
cache = DB_CACHE_MANAGER(db_path, module_path, bin_path)
con = cache.check_cache_con()
if con:
cache.parse_conf_cache()
cache.parse_libinfo_cache()
cache.parse_rpath_cache()
cache.parse_data_cache()
cache.parse_bininfo_cache()
bin_path = cache.bin_path
if not os.path.exists(bin_path):
gui_start = GUI_START(bin_path)
gui_start.exec_()
newpath = gui_start.newpath
if newpath is None:
raise Exception("[AutoResolv] No path given ! Aborting")
if not os.path.exists(newpath):
raise Exception("[AutoResolv] Given path for binary location incorrect ! Aborting")
else:
print(f"[AutoResolv] Set new binary path to {newpath}")
bin_path = newpath
if not con:
print(f"[AutoResolv] DB Cache seem empty, Creating cache")
libs,rpath = getLibsFromBin(bin_path)
if rpath is not None:
print(f"[AutoResolv] I got a rpath : {rpath}")
cache.create_cache(libs, bin_path, rpath)
cache.rpath = rpath.split(":")
else:
cache.create_cache(libs, bin_path)
cache.rpath = []
cache.cache_save_bininfo(bin_path)
gui_main = GUI_MAIN(cache)
gui_main.exec_()
def run(self, arg):
self.main()
def PLUGIN_ENTRY():
return AutoResolv()