forked from P4nda0s/IDAFrida
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIDAFrida.py
233 lines (181 loc) · 6.82 KB
/
IDAFrida.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
import idaapi
###################
# from: https://github.com/igogo-x86/HexRaysPyTools
class ActionManager(object):
def __init__(self):
self.__actions = []
def register(self, action):
self.__actions.append(action)
idaapi.register_action(
idaapi.action_desc_t(action.name, action.description, action, action.hotkey)
)
def initialize(self):
pass
def finalize(self):
for action in self.__actions:
idaapi.unregister_action(action.name)
action_manager = ActionManager()
class Action(idaapi.action_handler_t):
"""
Convenience wrapper with name property allowing to be registered in IDA using ActionManager
"""
description = None
hotkey = None
def __init__(self):
super(Action, self).__init__()
@property
def name(self):
return "FridaIDA:" + type(self).__name__
def activate(self, ctx):
# type: (idaapi.action_activation_ctx_t) -> None
raise NotImplementedError
def update(self, ctx):
# type: (idaapi.action_activation_ctx_t) -> None
raise NotImplementedError
############################################################################
import ida_funcs
import json
import os
from PyQt5 import QtCore
from PyQt5.Qt import QApplication
from PyQt5.QtWidgets import QDialog, QHBoxLayout, QVBoxLayout, QTextEdit
# [offset] => offset of target function in hex value format.
# [funcname] => function name
# [filename] => input file name of IDA. e.g. xxx.so / xxx.exe
default_template = """
try {
Interceptor.attach(Module.findBaseAddress("[filename]").add([offset]), {
onEnter: function (args) {
console.log("enter: [funcname]");
},
onLeave: function (arg) {
console.log("leave: [funcname]");
}
});
}
catch(err) {
console.log(err);
}
"""
class Configuration:
def __init__(self) -> None:
self.frida_cmd = """frida -U --attach-name="com.example.app" -l gen.js --no-pause"""
self.template = default_template
if os.path.exists("IDAFrida.json"):
self.load()
def set_frida_cmd(self, s):
self.frida_cmd = s
self.store()
def set_template(self, s):
self.template = s
self.store()
def reset(self):
self.__init__()
def store(self):
try:
data = {"frida_cmd" : self.frida_cmd, "template": self.template}
open("IDAFrida.json", "w").write(json.dumps(data))
except Exception as e:
print(e)
def load(self):
try:
data = json.loads(open("IDAFrida.json", "r").read())
self.frida_cmd = data["frida_cmd"]
self.template = data["template"]
except Exception as e:
print(e)
global_config = Configuration()
class ConfigurationUI(QDialog):
def __init__(self, conf : Configuration) -> None:
super(ConfigurationUI, self).__init__()
self.conf = conf
self.edit_template = QTextEdit()
self.edit_template.setPlainText(self.conf.template)
layout = QHBoxLayout()
layout.addWidget(self.edit_template)
self.setLayout(layout)
def closeEvent(self, a0) -> None:
self.conf.set_template(self.edit_template.toPlainText())
self.conf.store()
return super().closeEvent(a0)
class ScriptGenerator:
def __init__(self, configuration : Configuration) -> None:
self.conf = configuration
@staticmethod
def get_idb_filename():
return os.path.basename(idaapi.get_input_file_path())
@staticmethod
def get_idb_path():
return os.path.dirname(idaapi.get_input_file_path())
def generate_stub(self, repdata: dict):
s = self.conf.template
for key, v in repdata.items():
s = s.replace("[%s]" % key, v)
return s
def generate_for_funcs(self, func_addr_list) -> str:
stubs = []
for func_addr in func_addr_list:
repdata = {
"filename" : self.get_idb_filename(),
"funcname" : ida_funcs.get_func_name(func_addr),
"offset" : hex(func_addr)
}
stubs.append(self.generate_stub(repdata))
return "\n".join(stubs)
def generate_for_funcs_to_file(self, func_addr_list, filename) -> bool:
data = self.generate_for_funcs(func_addr_list)
try:
open(filename, "w").write(data)
except Exception as e:
print(e)
return False
return True
class Frida:
def __init__(self, conf: Configuration) -> None:
self.conf = conf
class IDAFridaMenuAction(Action):
TopDescription = "IDAFrida"
def __init__(self):
super(IDAFridaMenuAction, self).__init__()
def activate(self, ctx) -> None:
raise NotImplemented
def update(self, ctx) -> None:
if ctx.form_type == idaapi.BWN_FUNCS:
idaapi.attach_action_to_popup(ctx.widget, None, self.name, self.TopDescription + "/")
return idaapi.AST_ENABLE_FOR_WIDGET
return idaapi.AST_DISABLE_FOR_WIDGET
class GenerateFridaHookScript(IDAFridaMenuAction):
description = "Generate Frida Script"
def __init__(self):
super(GenerateFridaHookScript, self).__init__()
def activate(self, ctx):
gen = ScriptGenerator(global_config)
idb_path = os.path.dirname(idaapi.get_input_file_path())
out_file = os.path.join(idb_path, "IDAhook.js")
selected = [idaapi.getn_func(idx).start_ea for idx in ctx.chooser_selection] #from "idaapi.getn_func(idx - 1)" to "idaapi.getn_func(idx)"
gen.generate_for_funcs_to_file(selected, out_file)
print("generate success out: " + out_file)
class RunGeneratedScript(IDAFridaMenuAction):
description = "Run Generated Script"
def __init__(self):
super(RunGeneratedScript, self).__init__()
def activate(self, ctx):
print("template")
class ViewFridaTemplate(IDAFridaMenuAction):
description = "View Frida Template"
def __init__(self):
super(ViewFridaTemplate, self).__init__()
def activate(self, ctx):
ui = ConfigurationUI(global_config)
ui.show()
ui.exec_()
class SetFridaRunCommand(IDAFridaMenuAction):
description = "Set Frida Command"
def __init__(self):
super(SetFridaRunCommand, self).__init__()
def activate(self, ctx):
print("template")
action_manager.register(GenerateFridaHookScript())
# action_manager.register(RunGeneratedScript())
action_manager.register(ViewFridaTemplate())
# action_manager.register(SetFridaRunCommand())