Skip to content

Commit 193f38a

Browse files
author
tiann
committed
add detail description for binder hook; tks [email protected].
1 parent 41ed5ac commit 193f38a

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

binder-hook/src/main/java/com/weishu/binder_hook/app/BinderProxyHookHandler.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.weishu.binder_hook.app;
22

3-
import android.os.IBinder;
4-
import android.os.IInterface;
5-
import android.util.Log;
6-
73
import java.lang.reflect.InvocationHandler;
84
import java.lang.reflect.Method;
95
import java.lang.reflect.Proxy;
106

7+
import android.os.IBinder;
8+
import android.util.Log;
9+
1110
/**
1211
* 由于ServiceManager里面的sCache里面存储的 IBinder类型基本上都是BinderProxy
1312
* 因此, ServiceManager的使用者调用getService之后不会直接使用这个map
@@ -66,7 +65,14 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
6665

6766
// asInterface 的时候会检测是否是特定类型的接口然后进行强制转换
6867
// 因此这里的动态代理生成的类型信息的类型必须是正确的
69-
new Class[] { IBinder.class, IInterface.class, this.iinterface },
68+
69+
// 这里面Hook的是一个BinderProxy对象(Binder代理) (代理Binder的queryLocalInterface正常情况下是返回null)
70+
// 因此, 正常情况下 在asInterface里面会由于BinderProxy的queryLocalInterface返回null导致系统创建一个匿名的代理对象, 这样我们就无法控制了
71+
// 所以我们要伪造一个对象, 瞒过这个if检测, 使得系统把这个queryLocalInterface返回的对象透传给asInterface的返回值;
72+
// 检测有两个要求, 其一: 非空, 其二, IXXInterface类型。
73+
// 所以, 其实返回的对象不需要是Binder对象, 我们把它当作普通的对象Hook掉就ok(拦截这个对象里面对于IXXInterface相关方法的调用)
74+
75+
new Class[] { this.iinterface },
7076
new BinderHookHandler(base, stub));
7177
}
7278

0 commit comments

Comments
 (0)