-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathagent.js
275 lines (245 loc) · 7.05 KB
/
agent.js
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
"use strict";
let classHandle = {};
let method_filter;
let method_exclude;
let method_exclude_set = false;
rpc.exports = {
setMethodFilter: function (filter) {
method_filter = new RegExp(filter);
},
setMethodExlude: function(filter) {
if (filter)
method_exclude_set = true;
method_exclude = new RegExp(filter);
},
enumerateClasses: function() {
startEnumerateClasses();
},
providedClassesHook: function(providedClasses) {
startProvidedClassesHook(providedClasses);
}
}
function startEnumerateClasses() {
if (Java.available) {
Java.perform(function(){
try{
Java.enumerateLoadedClasses({
onMatch: function(class_discovered) {
send({ type: "class_discovered", data: class_discovered });
},
onComplete: function() {
}
});
} catch (err) {
send({ type: "errorGeneric", data: "Java.perform error" });
console.error(err);
}
});
} else {
send({ type: "errorGeneric", data: "Java.available error" });
}
}
function startProvidedClassesHook(providedClasss){
//TODO performNow or perform?
Java.performNow(function(){
providedClasss.map(function(classNameToHook){
hookClass(classNameToHook);
});
});
}
/******CLASS HOOK FUNCTION******/
function hookClass(classNameToHook){
try {
classHandle = Java.use(classNameToHook);
} catch (err) {
send({ type: "errorGeneric", data: "Java.use error in class: " +
classNameToHook + " - skipping class" });
console.error(err);
return;
}
/*HOOK CONSTRUCTORS*/
try {
if (classHandle.$init.overloads.length > 0) {
hookConstructors(classNameToHook);
} else {
send({ type: "info", data: "No constructor to hook in class: " + classNameToHook });
}
} catch (err) {
console.error(err);
}
/*HOOK FUNCTIONS*/
var allPropertyNames = getAllPropertyNames(classHandle);
var allFunctionNames = getAllFunctionNames(allPropertyNames);
allFunctionNames.map(function(methodNameToHook){
/*return if method name matches user exlcude regex*/
if(method_exclude_set && method_exclude.test(methodNameToHook)){
return;
}
/*perform hook if method name matches user filter regex*/
if(method_filter.test(methodNameToHook)){
try{
if (!(classHandle[methodNameToHook].overloads.length > 1)){
hookMethod(classNameToHook, methodNameToHook);
} else {
hookOverloadedMethod(classNameToHook, methodNameToHook);
}
} catch (err) {
console.error(err);
}
}
});
}
/*
METHOD AND CONSTRUCTOR HOOK FUNCTIONS
*/
function hookConstructors(classNameToHook){
var constructorMethods = classHandle.$init.overloads;
for (var i in constructorMethods){
var argTypes = constructorMethods[i].argumentTypes.map(function(a) {return a.className;});
try{
send({
type: "constructorHooked",
data: {
methodType: "CONSTRUCTOR",
className: classNameToHook,
args: argTypes
}
});
classHandle.$init.overload.apply(this, argTypes).implementation = function() {
var args = Array.prototype.slice.call(arguments);
// send message on hook
send({
type: "constructorCalled",
data: {
methodType: "CONSTRUCTOR",
className: classNameToHook,
// args: JSON.stringify(args)
argTypes: argTypes,
args: args + ""
}
});
return this.$init.apply(this, args);
}
} catch (err){
console.error(err);
}
}
}
function hookMethod(classNameToHook, methodNameToHook){
var argTypes = classHandle[methodNameToHook].argumentTypes.map(function(a) {return a.className;});
try{
// send message to indicate the method is being hooked
send({
type: "methodHooked",
data: {
methodType: "METHOD",
className: classNameToHook,
methodName: methodNameToHook,
args: argTypes
}
});
classHandle[methodNameToHook].implementation = function() {
var args = Array.prototype.slice.call(arguments);
var retVal = this[methodNameToHook].apply(this, args);
// send message on hook
send({
type: "methodCalled",
data: {
methodType: "METHOD",
className: classNameToHook,
methodName: methodNameToHook,
argTypes: argTypes,
// args: JSON.stringify(args)
args: args + "",
ret: retVal + ""
}
});
return retVal;
};
}catch (err){
send({
type: "errorHook",
data: {
methodType: "METHOD",
className: classNameToHook,
methodName: methodNameToHook,
args: argTypes
}
});
console.error(err);
}
}
function hookOverloadedMethod(classNameToHook, methodNameToHook){
var overloadedMethods = classHandle[methodNameToHook].overloads;
for (var i in overloadedMethods){
var argTypes = overloadedMethods[i].argumentTypes.map(function(a) {return a.className;});
try{
// send message to indicate the overloaded method is being hooked
send({
type: "methodHooked",
data: {
methodType: "OVERLOADED METHOD",
className: classNameToHook,
methodName: methodNameToHook,
args: argTypes
}
});
classHandle[methodNameToHook].overload.apply(this, argTypes).implementation = function() {
var args = Array.prototype.slice.call(arguments);
var retVal = this[methodNameToHook].apply(this, args);
// send message on hook
send({
type: "methodCalled",
data: {
methodType: "OVERLOADED METHOD",
className: classNameToHook,
methodName: methodNameToHook,
// argTypes: argTypes,
// args: JSON.stringify(args)
args: args + "",
ret: retVal + ""
}
});
// return this[methodNameToHook].apply(this, args);
return retVal;
};
} catch (err){
send({
type: "errorHook",
data: {
methodType: "OVERLOADED METHOD",
className: classNameToHook,
methodName: methodNameToHook,
args: argTypes
}
});
console.error(err);
}
}
}
/*
CUSTOM FUNCTIONS
*/
/*
return all the property names for an object by walking up the prototype chain
enum/nonenum, self/inherited..
*/
function getAllPropertyNames( obj ) {
var props = [];
do {
props= props.concat(Object.getOwnPropertyNames( obj ));
} while ( obj = Object.getPrototypeOf( obj ) );
return props;
}
/*cheap hack to only get the function names of the intended class*/
//TODO do it better I guess
function getAllFunctionNames( propertyNames ) {
var begin_pos = propertyNames.indexOf("$className");
var end_pos = propertyNames.indexOf("constructor", begin_pos);
var functionNames = propertyNames.slice(begin_pos+1, end_pos);
return functionNames.filter(function(funcName){
if (typeof(classHandle[funcName]) === "function"){
return funcName;
}
});
}