Hook机制学习(二) - weishu博客学习笔记(Binde
一:系统服务注册与获取流程
1.下面为一些系统Servic的注册过程: ContextImpl.registerService()
registerService(ACCOUNT_SERVICE, new ServiceFetcher() {
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(ACCOUNT_SERVICE);
IAccountManager service = IAccountManager.Stub.asInterface(b);
return new AccountManager(ctx, service);
}});
registerService(ACTIVITY_SERVICE, new ServiceFetcher() {
public Object createService(ContextImpl ctx) {
return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
}});
registerService(ALARM_SERVICE, new ServiceFetcher() {
public Object createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(ALARM_SERVICE);
IAlarmManager service = IAlarmManager.Stub.asInterface(b);
return new AlarmManager(service, ctx);
}});
2. ContextImpl: 系统中的各种服务(ServiceFetcher)主要以键值对的形式存储在SYSTEM_SERVICE_MAP中。
private static final HashMap<String, ServiceFetcher> SYSTEM_SERVICE_MAP =
new HashMap<String, ServiceFetcher>();
private static int sNextPerContextServiceCacheIndex = 0;
private static void registerService(String serviceName, ServiceFetcher fetcher) {
if (!(fetcher instanceof StaticServiceFetcher)) {
fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++;
//每个ServiceFetcher对应的Service对象。
}
SYSTEM_SERVICE_MAP.put(serviceName, fetcher);
}
3. ContextImpl.getSystemService(): 从SYSTEM_SERVICE_MAP取出ServiceFecher。然后在从ServiceFetcher取出对应的Service。
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
@Override
public Object getSystemService(String name) {
ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
return fetcher == null ? null : fetcher.getService(this);
}
4. ServiceFetcher: 存储的为Service对象。
static class ServiceFetcher {
int mContextCacheIndex = -1;
public Object getService(ContextImpl ctx) {
ArrayList<Object> cache = ctx.mServiceCache;
Object service;
synchronized (cache) {
if (cache.size() == 0) {
// Initialize the cache vector on first access.
// At this point sNextPerContextServiceCacheIndex
// is the number of potential services that are
// cached per-Context.
for (int i = 0; i < sNextPerContextServiceCacheIndex; i++) {
cache.add(null);
}
} else {
service = cache.get(mContextCacheIndex);
if (service != null) {
return service;
}
}
service = createService(ctx);
cache.set(mContextCacheIndex, service);
return service;
}
public Object createService(ContextImpl ctx) {
throw new RuntimeException("Not implemented");
}
5. ServiceManager.getService():
IServiceManager是一个IInterface接口;
ServiceManager的getService(),addService(),checkService()等方法都是通过getIServiceManager().getService(name)等相应方法来实现的。
getIServiceManager()返回的是ServiceManagerProxy对象。ServiceManagerProxy是ServiceManagerNative的代理对象。
public static IBinder getService(String name) {
try {
IBinder service = sCache.get(name);
if (service != null) {
return service;
} else {
return getIServiceManager().getService(name);
}
} catch (RemoteException e) {
Log.e(TAG, "error in getService", e);
}
return null;
}
ServiceManager,ServiceManagerNative,ServiceMangerNative,IServiceManager三者的关系图。
所以通过ServiceManager获取Service经过了一个跨进程Binder调用的过程。
关系图因此,通过分析我们得知,通过
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
获取系统Service的使用其实就分为两步:
IBinder b = ServiceManager.getService("service_name"); // 跨进程获取原始的IBinder对象
IXXInterface in = xxxInterface.Stub.asInterface(b); // 转换为Service Proxy对象(应用进程)
关系图
二:Binder Hook
观察获取系统Service的使用就分为第一部分所总结的两步;
对于第一步
IBinder b = ServiceManager.getService("service_name"); // 跨进程获取原始的IBinder对象
ServiceManager使用缓存来获取Service BinderProxy对象,如果该Service BinderProxy对象获取过,就直接从缓存Map里面取Service BinderProxy对象 ;否则通过一次Binder IPC来获取。
** ServiceManager.getService("service_name")**
public static getService(String name) {
try {
IBinder service = sCache.get(name);
if (service != null) {
return service;
} else {
return getIServiceManager().getService(name);
}
} catch (RemoteException e) {
Log.e(TAG, "error in getService", e);
}
return null;
}
对于第二步:首先判断这个xxxInterface.Stub.asInterface(b)
首先判断该IBinder是否为跨进程调用,如果是则返回Proxy,如果不是直接返回该IBinder。
public static android.content.IClipboard asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); // Hook点
if (((iin != null) && (iin instanceof android.content.IClipboard))) {
return ((android.content.IClipboard) iin);
}
return new android.content.IClipboard.Stub.Proxy(obj);
}
Hook 思路:
1 :首先Hook掉第一部返回的IBinder,具体做法是在ServiceManager的Map cache里面存放我们的IBinder Hook Proxy对象,即一个动态代理类,那么以后我们获取服务时,第一步获取到的都是我们设置的这个IBinder Hook Proxy对象。
2:当获取服务时,第二步调用方法是xxxInterface.Stub.asInterface(b)
,而此时我们传入的b参数是IBinder Hook Proxy对象,即会IBinder Hook Proxy对象的queryLocalInterface(DESCRIPTOR); // Hook点
,此时我们Hook掉此方法,让它返回我们的Hook过的服务。
3 Hook掉相应的服务。