Android自曝签名剖析

注:本次解读是对尼古拉斯赵四项目中自曝签名解读
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
/**
*content: Activity
*signed:签名信息
*/
public static void hookPMS(Context context, String signed){
try{
Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
Method currentActivityThreadMethod =
activityThreadClass.getDeclaredMethod("currentActivityThread");
Object currentActivityThread = currentActivityThreadMethod.invoke(null);
Field sPackageManagerField = activityThreadClass.getDeclaredField("sPackageManager");
sPackageManagerField.setAccessible(true);
Object sPackageManager = sPackageManagerField.get(currentActivityThread);
Class<?> iPackageManagerInterface = Class.forName("android.content.pm.IPackageManager");

// 代码编号1
Object proxy = Proxy.newProxyInstance(
iPackageManagerInterface.getClassLoader(),
new Class<?>[] { iPackageManagerInterface },
new PmsHookBinderInvocationHandler(sPackageManager, signed));
//代码编号2
sPackageManagerField.set(currentActivityThread, proxy);
PackageManager pm = context.getPackageManager();
Log.i("jw", "pm:"+pm);
Field mPmField = pm.getClass().getDeclaredField("mPM");
mPmField.setAccessible(true);
mPmField.set(pm, proxy);
}catch (Exception e){
Log.d("jw", "hook pms error:"+Log.getStackTraceString(e));
}
}

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

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.util.Log;

/**
* 动态代理
*/
public class PmsHookBinderInvocationHandler implements InvocationHandler{

private Object base;

private String SIGN;

public PmsHookBinderInvocationHandler(Object base, String sign) {
try {
this.base = base;
this.SIGN = sign;
} catch (Exception e) {
Log.d("jw", "error:"+Log.getStackTraceString(e));
}
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try{
if("getPackageInfo".equals(method.getName())){
String pkgName = (String)args[0];
Integer flag = (Integer)args[1];
if(flag == PackageManager.GET_SIGNATURES && "lang.com.douyin".equals(pkgName)){
Signature sign = new Signature(SIGN);
PackageInfo info = (PackageInfo) method.invoke(base, args);
info.signatures[0] = sign;
return info;
}
}
}catch(Exception e){
}
return method.invoke(base, args);
}

}

代码编号1-理解动态代理

核心原理:在内存中生存继承PmsHookBinderInvocationHandler代理类,实现iPackageManagerInterface接口的类

代码编号2-替换pm

替换系统中的PackageManager

里面设计到的ActivityThread,IPackageManager等貌似不能Android代码中导入,还需要进一步调试Android源码,查明原因