ServiceManager Hook原理

4. ServiceManager Hook原理

如果调用getSystemService方法获取系统服务对应的API接口,

mLoactionManager = (LoactionManager) getSystemService(Context.LOCATION_SERVICE);

获取系统服务的使用其实就分为两步

IBinder b = ServiceManager.getService("service_name"); // 获取原始的IBinder对象  
IXXInterface in = IXXInterface.Stub.asInterface(b); // 转换为Service接口  

4.1 IBinder代理的Hook

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;  
}  

获取的是系统服务的IBinder代理对象,但是,进行Hook时,添加到sCache中的是对应的ServiceManagerCacheBinderHook对象。

也就是说, ServiceManagerCacheBinderHook Hook了IBinder代理对象。

4.2 queryLocalInterface方法的Hook

调用IBinder代理对象的方法时,其实就会调用ServiceManagerCacheBinderHook对象的invoke方法,调用流程图如下,


invoke方法主要逻辑如下,

1,获取原来的系统服务对应的IBinder对象,如果不可用的直接调用IBinder对象的方法,相当于未Hook 一样。

IBinder originService = MyServiceManager.getOriginService(mServiceName);
if (!isEnable()) {
      return method.invoke(originService, args);
}

2,Hook方法的处理

HookedMethodHandler hookedMethodHandler = mHookHandles.getHookedMethodHandler(method);
if (hookedMethodHandler != null) {
    return hookedMethodHandler.doHookInner(originService, method, args);
} else {
    return method.invoke(originService, args);
}

首先获取Hook 的方法对应的对象,因为是按照方法来Hook的,一个方法对应一个Hook对象。

mHookHandles 一般在createHookHandle方法调用实际实现的Hook 的init方法中添加的,

这句话说的有点拗口,再次啰嗦一下,

ServiceManagerCacheBinderHook的createHookHandle方法如下,

protected BaseHookHandle createHookHandle() {
    return new ServiceManagerHookHandle(mHostContext);
}

ServiceManagerHookHandle的init方法如下,

protected void init() {
sHookedMethodHandlers.put("queryLocalInterface", new queryLocalInterface(mHostContext));
}

说明仅仅Hook了IBinder对象的queryLocalInterface方法,其他方法都没有Hook。方法未Hook的还是调用原理IBinder

对象的方法,如果是queryLocalInterface就调用

queryLocalInterface类的doHookInner方法,

queryLocalInterface类的父类HookedMethodHandler的doHookInner方法如下,

boolean suc = beforeInvoke(receiver, method, args);
Object invokeResult = null;
if (!suc) {
  invokeResult = method.invoke(receiver, args);
}
  afterInvoke(receiver, method, args, invokeResult);

一般会调用子类的beforeInvoke方法,如果返回false,就相当于该方法未Hook一样。

最后会调用afterInvoke方法,该方法在此就不论述了,主要看beforeInvoke方法。

当然如果子类未实现beforeInvoke方法,就默认返回false,相当于该方法未Hook一样. HookedMethodHandler的beforeInvoke方法如下,

protected boolean beforeInvoke(Object receiver, Method method, Object[] args) throws Throwable {
    return false;
 }

queryLocalInterface类的定义如下,

class queryLocalInterface extends HookedMethodHandler {
   public queryLocalInterface(Context context) {
        super(context);
   }

   @Override
 protected void afterInvoke(Object receiver, Method method, Object[] args, Object invokeResult) throws Throwable {
        Object localInterface = invokeResult;
        Object proxiedObj = MyServiceManager.getProxiedObj(mServiceName);
        if (localInterface == null && proxiedObj != null) {
             setFakedResult(proxiedObj);
          }
      }
}

这里仅仅实现了  afterInvoke方法。一般情况下,系统服务(例如LoactionManagerService)一般在queryLocalInterface方法中实现,

但是在前面的论述中,初始化时已经实现了,所以这里就没有覆盖beforeInvoke方法了。

 

在此也可以看出一些逻辑,调用IBinder对象的queryLocalInterface方法其实就是调用queryLocalInterface 类的beforeInvoke

 和 afterInvoke方法。

4.3 结构图

ServiceManagerCacheBinderHook的结构图如下,


ServiceManagerCacheBinderHookHook了IBinder对象,有三个主要的方法,

1, onInstall方法,初始化。

2, invoke 方法,Hook的地方。

3,createHookHandle方法中真正创建执行Hook的类,类似于委托。

ServiceManagerHookHandle是ServiceManagerCacheBinderHook的内部类,主要有一个方法,

Init:构造Hook 方法执行的类。ServiceManagerHookHandle其实并不是执行类。

queryLocalInterface是ServiceManagerHookHandle的内部类,一般会实现beforeInvoke/afterInvoke方法。

这里的beforeInvoke/afterInvoke方法才是真正Hook IBinder对象的方法执行的地方。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ServiceManagerAndroid 系统中的一个系统服务,它负责管理系统中所有的服务。它使用 Binder 机制来实现跨进程的服务管理。在 Android 中,所有的服务都是通过 ServiceManager 来进行注册和获取的,这也就是为什么我们可以在不同的应用程序中获取同一个服务的原因。 ServiceManager 的源码位于 frameworks/native/cmds/servicemanager 中,它主要由以下几个文件组成: - ServiceManager.cpp:ServiceManager 的主要实现文件,包含了 ServiceManager 类的定义和实现。 - BpServiceManager.cpp:BpServiceManagerServiceManager 的客户端代理类,它通过 Binder 机制与 ServiceManager 进行通信。 - IServiceManager.cpp:IServiceManagerServiceManager 的接口文件,定义了与 ServiceManager 相关的所有方法。 - IServiceManager.h:IServiceManager 的头文件,定义了 IServiceManager 接口类的定义。 - ServiceManager.h:ServiceManager 的头文件,包含了 ServiceManager 类的定义和一些宏定义。 ServiceManager 的主要实现是在 ServiceManager.cpp 文件中的 ServiceManager 类中。ServiceManager 类主要实现了 IServiceManager 接口类中的所有方法,包括: - addService:用于添加一个服务到 ServiceManager 中,并将服务的名字和服务的 Binder 对象映射起来。 - getService:用于获取一个已注册的服务。 - checkService:用于检查一个服务是否已经注册。 - listServices:用于列出所有已经注册的服务。 ServiceManager 的初始化是在 init_service_manager 函数中完成的,它主要完成了以下几个步骤: - 创建一个 ServiceManager 对象,并将其注册为系统服务。 - 注册系统服务,包括 Package Manager Service、Activity Manager Service、Window Manager Service 等。 - 注册其他系统服务。 总之,ServiceManagerAndroid 系统中非常重要的一个系统服务,它是整个系统中所有服务的管理者。通过 ServiceManager,我们可以方便地获取到系统中已经注册的服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值