-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanalyse_java.py
289 lines (221 loc) · 8.99 KB
/
analyse_java.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from __future__ import print_function
import itertools
import sys
import re
import os
import json
from pwn import log
"""
Try to monitor all methods of a class
"""
import frida
CONSTRUCTOR_METHOD = 1
STATIC_METHOD = 2
INSTANCE_METHOD = 3
CONFIG_FILE_NAME = "config/java.json"
CLASSES = [{"name": "javax.crypto.Cipher"},
{"name": "javax.crypto.spec.IvParameterSpec"}]
jscode = """
Java.perform(function () {
var c = null;
try {
c = Java.use('%s');
}
catch(err) {
// class not found...
send("<none>");
return;
}
if (c !== null) {
var cproto = Object.getPrototypeOf(c);
var members = Object.getOwnPropertyNames(cproto);
}
else {
send("<none>");
return;
}
method_list = [];
for (var m in members) {
var member = members[m];
if (member !== '$className' && member !== '$new' && 'overloads' in c[member]) {
// a method can have several overloads (same name, but different params)
// therefore iterate over them
for (var ovld in c[member].overloads) {
method_list.push({'name': member, 'parameters': c[member].overloads[ovld].argumentTypes,
'type': c[member].overloads[ovld].type, 'returnType': c[member].overloads[ovld].returnType});
}
}
}
send(method_list);
// test hooking
c.onEnter.overload().implementation = function() {
send("onEnter called");
return this.onEnter();
}
});
"""
hook_jscode_header = """
Java.perform(function() {{
var c = Java.use('{0}');
"""
hook_jscode = """
c.{0}.overload({1}).implementation = function ({2}) {{
var r=this.{0}.overload({1}).call({3});
send({4});
return r;
}}
"""
hook_jscode_constructor = """
c.{0}.overload({1}).implementation = function ({2}) {{
this.{0}.overload({1}).call({3});
send({4});
}}
"""
hook_jscode_footer = """
}); // end of Java.perform
"""
def _gen_hook_jscode_for_method(class_name, method_description):
name = method_description["name"]
return_type = method_description["returnType"]["className"]
#if name == "$init":
# # do not hook $init (constructor) - causes problems (probably because of return value)
# return ""
#if method_description["type"] == CONSTRUCTOR_METHOD:
# #TODO: don't know how to handle constructor methods
# return ""
parameters = method_description["parameters"]
signature_list = [p["className"] for p in parameters]
num_parameters = len(parameters)
signature = ",".join("'{0}'".format(s) for s in signature_list)
dummy_params_list = ["a{0}".format(i+1) for i in range(num_parameters)]
dummy_params = ",".join(dummy_params_list)
# for non-static methods, 'this' is the first parameter
if method_description["type"] != STATIC_METHOD:
signature_list_and_this = [class_name] + signature_list
this_and_dummy_params_list = ["this"] + dummy_params_list
else:
signature_list_and_this = signature_list
this_and_dummy_params_list = dummy_params_list
#needed for constructing the call to the original method
this_and_dummy_params_string = ",".join(itertools.chain(["this"], dummy_params_list))
param_info = ",".join("{{'name':'{0}','content':{0},'type':'{1}'}}".format(n,t) for n,t in zip(this_and_dummy_params_list, signature_list_and_this))
#use slightly different code for special method $init, although the general hook code also seemed to work
if name == "$init":
fstring = "{{'name': '{0}.{1}', 'parameters':[{2}], 'returns':{{'type':'{3}'}} }}".format(class_name, name, param_info, return_type)
return hook_jscode_constructor.format(name, signature, dummy_params, this_and_dummy_params_string, fstring)
fstring = "{{'name': '{0}.{1}', 'parameters':[{2}], 'returns':{{'content':r,'type':'{3}'}} }}".format(class_name, name, param_info, return_type)
return hook_jscode.format(name, signature, dummy_params, this_and_dummy_params_string, fstring)
def pretty_print(msg, print_function):
"""Print a monitoring message in a more readable way"""
print_function("{0}:".format(msg["name"]))
print_function(" parameters:")
for entry in msg["parameters"]:
print_function(" {0} ({1}): {2}".format(entry["name"], entry["type"], entry["content"]))
print_function(" returns:")
ret = msg["returns"]
if "content" in ret:
content = ret["content"]
else:
content = ""
print_function(" ({0}): {1}".format(ret["type"], content))
print_function("")
class Method_List_Receiver:
"""Retrieves methods of given classes and adds hooks for monitoring"""
def __init__(self, process, config, path, callback_when_finished):
self.class_counter = 0
self.script = ""
self.classes = config["classes"]
self.print_calls = config["settings"]["print_calls"]
self.write_results = config["settings"]["write_results"]
self.callback = callback_when_finished
self.process = process # frida process object
self.num_classes = len(self.classes)
self.path = path
def start_hooking(self):
log.info("Hooking classes. This may take a while...")
self._hook_next()
def _hook_next(self):
if self.class_counter < self.num_classes:
self.script = self.process.create_script(jscode % self.classes[self.class_counter]["name"])
self.script.on('message', self._on_method_receive)
self.script.load()
else:
self.callback()
def _on_message(self, message, data):
"""Process the method call messages"""
if message['type'] == 'send':
#info = json.loads(str(message['payload']).encode('string-escape'), strict=False)
info = message['payload']
if self.print_calls:
pretty_print(info, log.info)
if self.write_results:
filename = os.path.join(self.path, info["name"] + ".dat")
with open(filename, "a+") as f:
json.dump(info, f)
f.write("\n")
else:
log.warning(str(message).encode('string-escape'))
def _on_method_receive(self, message, data):
"""After method list is received - upload the hook script"""
class_counter = self.class_counter
if message['type'] != 'send':
return
class_name = self.classes[self.class_counter]["name"]
if message['payload'] != '<none>':
method_list = message['payload']
#print(method_list)
self.script.unload()
hook_script_code = (hook_jscode_header.format(class_name) +
''.join(_gen_hook_jscode_for_method(class_name, m) for m in method_list) +
hook_jscode_footer)
#print(hook_script_code)
hook_script = self.process.create_script(hook_script_code)
hook_script.on('message', self._on_message)
hook_script.load()
log.info("Hooked methods of class {0}".format(class_name))
else:
log.info("Class {0} does not exist.".format(class_name))
self.class_counter += 1
self._hook_next()
def callback_when_finished():
log.info("Finished hooking classes. Ready... Exit with CTRL-C")
def hook_classes(process, classes, path):
receiver = Method_List_Receiver(process, classes, path, callback_when_finished)
receiver.start_hooking()
def main(target_process):
with open(CONFIG_FILE_NAME) as j:
config = json.load(j)
try:
session = frida.get_usb_device().attach(target_process)
except frida.ServerNotRunningError:
try:
log.error("Please start frida server first")
except:
sys.exit(-1)
except frida.TimedOutError:
try:
log.error("Frida timeout...")
except:
sys.exit(-1)
PATH = ""
if config["settings"]["write_results"]:
PATH = os.path.join(PATH, "results", sys.argv[1])
if not os.path.exists(PATH):
os.makedirs(PATH)
runnr = len([x for x in os.listdir(PATH) if os.path.isdir(os.path.join(PATH,x))])
PATH = os.path.join(PATH, "run_" + str(runnr))
if not os.path.exists(PATH):
os.makedirs(PATH)
hook_classes(session, config, PATH)
sys.stdin.read()
if __name__ == '__main__':
if len(sys.argv) != 2:
try:
log.error("Usage: %s <process name or PID>" % __file__)
except:
sys.exit(-1)
try:
target_process = int(sys.argv[1])
except ValueError:
target_process = sys.argv[1]
main(target_process)