-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathXServer.js
369 lines (328 loc) · 15.8 KB
/
XServer.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
XServer Frida 加载脚本
功能:使用Frida在目标应用中加载并实现XServer
特点:无需Xposed框架
原理:使用Frida创建并注册XServer的Hook Provider
用法:frida -U [app] -l XServer.js
*/
/*
Hook Manager
{
method:[hook]
}
*/
var Hooks = {}
var typeTranslation = {
"Z":"java.lang.Boolean",
"B":"java.lang.Byte",
"S":"java.lang.Short",
"I":"java.lang.Integer",
"J":"java.lang.Long",
"F":"java.lang.Float",
"D":"java.lang.Double"
}
var XServerCL = null
var XServerFactory = null
const DEBUG = false
/*
核心方法
技术原理:使用Frida注册符合该接口的Provider类
*/
function registerHookProvider(){
var provideInterface = XServerFactory.use("monkeylord.XServer.handler.HookHandler$HookProvider")
var hookMethod = function (hookMethod, mycallback) {
// 反射方法在Frida中无法使用,需要通过Frida得到这个方法
console.log("[XServer] Hook ", hookMethod.getName(), "with", mycallback)
var refMethod = Java.use(hookMethod.$className)
var method = Java.cast(hookMethod, refMethod)
var clazz = method.getDeclaringClass().getName()
var mtdname = (hookMethod.$className=="java.lang.reflect.Constructor")? "$init": method.getName()
var overload = method.getParameterTypes().map(function(clz){return clz.getName()})
var fridaMethod = Java.use(clazz)[mtdname].overload.apply(Java.use(clazz)[mtdname], overload)
// 现在我们拿到了Frida方法,以及需要的信息
// 开始注册Hook
// XServer可能会大量Hook,所以这里必须复用Hook以节省内存开销
// fullname是方法的唯一标识符
var fullname = clazz+"->"+mtdname+"("+overload.join(",")+")"
// 保存这个反射方法的全局引用,之后有用
var paramMethod = Java.retain(hookMethod)
// 注册Callbacks
if(!Hooks[fullname]){
// New Hook
Hooks[fullname] = [Java.retain(mycallback)]
}else{
// 筛选过滤,如果已经有了就不要重复Hook
var isNewCallback = Hooks[fullname].every(function(callback){
if(DEBUG)console.log("isEqual: " + callback.equals(mycallback))
return !callback.equals(mycallback)
})
if(isNewCallback)Hooks[fullname].push(Java.retain(mycallback))
}
// 实施Hook
fridaMethod.implementation = function(){
console.log("[XServer]", "Hooked method called:", paramMethod.getName())
console.log("[XServer]", "Receiver:", this)
console.log("[XServer]", "Hooks", Hooks[fullname])
var param = XServerFactory.use("monkeylord.XServer.handler.Hook.XServer_Param").$new()
param.method.value = paramMethod
param.thisObject.value = (fridaMethod.type == 3)? this : null
// 准备参数
var args = arguments
var jarr = Object.keys(arguments).map(function(key){return args[key]})
fridaMethod.argumentTypes.forEach(function(type,index){
var env = Java.vm.getEnv()
if(DEBUG){
console.log("[DECBUG]ArgInfo "+index)
console.log(JSON.stringify(type))
console.log(jarr[index])
console.log(JSON.stringify(type.toJni(jarr[index], env)))
console.log(type.toJni(jarr[index], env).isNull)
}
if(jarr[index]==null)jarr[index] = null
else if(type.type != "pointer")jarr[index] = Java.use(typeTranslation[type.name]).valueOf(jarr[index])
else if(type.className == "java.lang.String")jarr[index]=Java.use("java.lang.String").$new(jarr[index])
else jarr[index] = Java.classFactory._getType("java.lang.Object").fromJni(type.toJni(jarr[index], env), env, false)
})
param.args.value = Java.array("java.lang.Object", jarr)
// 开始调用Callbacks
for(var i = 0; i<Hooks[fullname].length; i++){
try{
Hooks[fullname][i].beforeHookedMethod(param)
}catch(e){
console.log(e)
}
if(param.returnEarly.value)break;
}
// 处理原方法调用
if(DEBUG){
console.log("[DEBUG]", "Unwrap arguments")
console.log("[DEBUG]", "arguments length", param.args.value.length)
console.log("[DEBUG]", "arguments array", JSON.stringify(param.args.value))
param.args.value.forEach(function(arg, index){
console.log("[DEBUG]", "argument"+index, JSON.stringify(arg))
})
}
var newargs = []
for(var i = 0; i<param.args.value.length; i++)newargs[i] = param.args.value[i]
fridaMethod.argumentTypes.forEach(function(type,index){
if(newargs[index]==null)newargs[index] = null
else if(type.type!="pointer"){
//console.log("CAST: ",JSON.stringify(Object.keys(jarr[index])))
var value
var basicObj = Java.cast(jarr[index],Java.use(typeTranslation[type.name]))
console.log(JSON.stringify(type))
console.log(JSON.stringify(basicObj))
switch(type.name){
case "Z":
value = basicObj.booleanValue();break;
case "B":
value = basicObj.byteValue();break;
case "S":
value = basicObj.shortValue();break;
case "I":
value = basicObj.intValue();break;
case "J":
value = basicObj.longValue();break;
case "F":
value = basicObj.floatValue();break;
case "D":
value = basicObj.doubleValue();break;
}
newargs[index]=value
}else if(type.name.startsWith("[")){
var env = Java.vm.getEnv()
newargs[index] = type.fromJni(Java.classFactory._getType("java.lang.Object").toJni(newargs[index], env), env, true)
}else{
newargs[index] = Java.cast(newargs[index], Java.use(newargs[index].$className))
}
})
try{
if(!param.returnEarly.value){
var env = Java.vm.getEnv()
if(DEBUG){
console.log("[DEBUG]", "Invoking original method", fridaMethod.methodName)
console.log("[DEBUG]", "Method type:", fridaMethod.type)
newargs.forEach(function(arg, index){
console.log("[DEBUG]","argument"+index, JSON.stringify(arg))
})
}
var result = fridaMethod.apply((fridaMethod.type == 3)? Java.cast(param.thisObject.value, Java.use(this.$className)): Java.use(clazz), newargs)
if(DEBUG){
console.log("[DEBUG] Original Returned Object:"+ JSON.stringify(result))
}
var resultObject
if(result == undefined)resultObject = null
else if(fridaMethod._p[4].type!="pointer")resultObject = Java.use(typeTranslation[fridaMethod._p[4].name]).valueOf(result)
else resultObject = Java.classFactory._getType("java.lang.Object").fromJni(fridaMethod._p[4].toJni(result, env), env, false)
param.setResult(resultObject) // Call original
}
}catch(e){
console.log(e)
if(e.getCause){
param.setThrowable(e.getCause())
}
}
//param.result.value = "123"
for(var i = Hooks[fullname].length - 1; i >= 0; i--){
var lastResult = param.getResult()
var lastThrowable = param.getThrowable()
try{
Hooks[fullname][i].afterHookedMethod(param)
}catch(e){
console.log(e)
if (lastThrowable == null) {
param.setResult(lastResult);
} else {
param.setThrowable(lastThrowable);
}
}
//if(param.returnEarly.value)break;
}
//var r = Hooks[fullname][0].afterHookedMethod(param)
if(DEBUG){
var env = Java.vm.getEnv()
console.log("[DEBUG]","Return early:",param.returnEarly.value)
//console.log("[DEBUG]","Return Object Type:",JSON.stringify(param.result.value.getClass()))
console.log("[DEBUG]","Return Object:", JSON.stringify(param.result.value))
console.log("[DEBUG]","Return Object Original Type:",JSON.stringify(fridaMethod._p[4]))
if(fridaMethod._p[4].name.startsWith("["))console.log("[DEBUG]Return Object Casted:"+ JSON.stringify(fridaMethod._p[4].fromJni(Java.classFactory._getType("java.lang.Object").toJni(param.result.value, env), env, false)))
}
var returnObject
if(fridaMethod._p[4].type=="void"){
returnObject = undefined
}else if(fridaMethod._p[4].name.startsWith("[")&&!fridaMethod._p[4].name.startsWith("[L")){
returnObject = fridaMethod._p[4].fromJni(Java.classFactory._getType("java.lang.Object").toJni(param.result.value, env), env, false)
}else{
returnObject = param.result.value
}
return returnObject
//return (fridaMethod._p[4].type=="void") ? undefined : param.result.value
}
var unhook = XServerFactory.use("monkeylord.XServer.handler.Hook.Unhook").$new(hookMethod, mycallback)
return unhook
}
var unhookMethod = function (hookMethod, additionalObj) {
// 反射方法在Frida中无法使用,从Frida得到这个方法
// Frida Hook里additionalObj是对应的callback
console.log("Unhook")
var refMethod = Java.use(hookMethod.$className)
var method = Java.cast(hookMethod, refMethod)
var clazz = method.getDeclaringClass().getName()
var mtdname = (hookMethod.$className=="java.lang.reflect.Constructor")? "$init": method.getName()
var overload = method.getParameterTypes().map(function(clz){return clz.getName()})
// fullname是方法的唯一标识符
var fullname = clazz+"->"+mtdname+"("+overload.join(",")+")"
if(Hooks[fullname]){
Hooks[fullname] = Hooks[fullname].filter(function(callback){
console.log("Comparing...")
callback.equals(additionalObj)
})
}
}
var HookHandler = XServerFactory.use("monkeylord.XServer.handler.HookHandler");
var provider
try{
provider = XServerFactory.registerClass({
name: 'FridaHookProvider',
implements: [provideInterface],
methods: {
hookMethod: [{
returnType: 'monkeylord.XServer.handler.Hook.Unhook',
argumentTypes: ['java.lang.reflect.Member', 'monkeylord.XServer.handler.Hook.XServer_MethodHook'],
implementation: hookMethod
}],
unhookMethod: [{
returnType: 'void',
argumentTypes: ['java.lang.reflect.Member', 'java.lang.Object'],
implementation: unhookMethod
}]
}
});
}catch(e){
console.log("Ops, registerClass Failed, try DummyProvider way")
provider = XServerFactory.use("monkeylord.XServer.handler.Hook.DummyProvider");
provider.hookMethod.implementation = hookMethod
provider.unhookMethod.implementation = unhookMethod
}
console.log("[XServer]HookProvider created:" + provider)
//var myprovider = Java.ClassFactory.cast(provider, provideInterface)
//console.log("casted")
//console.log(myprovider)
HookHandler.setProvider(provider.$new())
console.log("[XServer]HookProvider registered")
}
/*
功能性方法:完成并调用XServer的加载和启动流程
*/
function loadXServer(path){
// Fix CodeCache, in case App does not have file permission
var ActivityThread = Java.use("android.app.ActivityThread")
var app = ActivityThread.currentApplication()
var cacheDir = "/data/data/" + app.getPackageName() + "/cache"
var codeCacheDir = "/data/data/" + app.getPackageName() + "/code_cache"
var SystemProperties = Java.use("android.os.SystemProperties")
if(SystemProperties.getInt("ro.build.version.sdk", 0)>=21){
codeCacheDir = app.getCodeCacheDir().toString()
cacheDir = app.getCacheDir().toString()
}
Java.classFactory.cacheDir = cacheDir
Java.classFactory.codeCacheDir = codeCacheDir
// Load
//Java.openClassFile(path).load()
var DexClassLoader = Java.use("dalvik.system.DexClassLoader");
XServerCL = DexClassLoader.$new(path, Java.classFactory.codeCacheDir, null, DexClassLoader.getSystemClassLoader());
XServerFactory = Java.ClassFactory.get(XServerCL)
XServerFactory.cacheDir = cacheDir
XServerFactory.codeCacheDir = codeCacheDir
var XServer = XServerFactory.use("monkeylord.XServer.XServer");
console.log("[XServer]XServer loaded in APP: " + XServer)
}
function setAsset(path){
var AssetManager = Java.use("android.content.res.AssetManager")
var assets = AssetManager.$new()
assets.addAssetPath(path)
var XServer = XServerFactory.use("monkeylord.XServer.XServer");
XServer.assetManager.value = assets
console.log("[XServer]XServer Assets loaded from " + path)
}
function setClassLoader(){
var XServer = XServerFactory.use("monkeylord.XServer.XServer");
var ActivityThread = Java.use("android.app.ActivityThread")
console.log("[XServer]ClassLoader used by currentApplication:" + ActivityThread.currentApplication().getClassLoader())
XServer.classLoader.value = ActivityThread.currentApplication().getClassLoader()
}
function startXServer(port){
var XServer = XServerFactory.use("monkeylord.XServer.XServer");
XServer.$new(port)
console.log("[XServer]HttpService started at " + port)
console.log("[XServer]Tips: adb forward tcp:8000 tcp:"+port)
console.log("[XServer]Access XServer at http://127.0.0.1:8000/")
}
Java.performNow(function(){
console.log("XServer Frida Tool")
console.log("By Monkeylord")
var javaFile = Java.use("java.io.File")
var possiblePaths = [
"/data/app/monkeylord.xserver-1/base.apk",
"/data/app/monkeylord.xserver-2/base.apk",
"/data/local/tmp/xserver.apk"
]
var paths = possiblePaths.filter(function(path){
return javaFile.$new(path).exists()
})
if(paths.length == 0){
console.log("XServer APK not found")
console.log("Please make sure XServer is installed, or at '/data/local/tmp/xserver.apk'")
console.log("You can 'adb push [XServer APK Path] /data/local/tmp/xserver.apk' if you don't want to install XServer")
return
}
//var path = "/data/local/tmp/xserver.apk"
//var path = "/data/app/monkeylord.xserver-1/base.apk"
var path = paths[0]
console.log("[XServer]Loading XServer from "+ path)
loadXServer(path)
registerHookProvider()
setAsset(path)
setClassLoader()
startXServer(8000)
})