昨花了点时间,参考github issues 总算实现了基于xposed的系统服务注入,本文目的是为了“解决应用之间hook后数据共享,任意app ServiceManager.getService就
可以直接调用aidl实现了进程通信”(比如aidl service实现socket,http server等,或者从某app获取数据传递给另外个app等场景,能做什么大家自己想吧,当然也可以实现非xposed版本的,需要通过直接smali方式。因为需快速实现,我就基于xposed的方案凑活用着用吧)
Xposed我就不介绍了,Xposed有2个重要接口,一个是针对应用级别hook:IXposedHookLoadPackage,另一个就是针对系统级别的:IXposedHookZygoteInit
这里将使用IXposedHookZygoteInit实现aidl添加到系统服务中,当然,通过IXposedHookLoadPackage也是可以实现的,但是因为我们注入的服务是希望像系统服务一样,开机启动,关机停止,另外IXposedHookZygoteInit本身就是定位在针对系统hook,所以还是建议使用IXposedHookZygoteInit。
直接进入正题:
1.创建 android.os.ICustomService.aidl
package android.os;
// Declare any non-default types here with import statements
interface ICustomService {
String sayHello();
}
2.创建CustomService实现类
1 public class CustomService extends ICustomService.Stub { 2 private Context mContext; 3 public CustomService(Context context) { 4 mContext = context; 5 } 6 7 @Override 8 public String sayHello() throws RemoteException { 9 return "Just Hello World"; 10 } 11 12 //ActivityManagerService的systemReady在所有服务初始化完成后触发,这定义这个是为了实现自定义服务的初始化代码实现 13 public void systemReady() { 14 // Make your initialization here 15 } 16 }
3.创建ApplicationSocket,实现IXposedHookZygoteInit接口,这实现系统启动时候触发hook
1 public class ApplicationSocket implements IXposedHookZygoteInit{ 2 public static String TAG = "ApplicationSocket"; 3 @Override 4 public void initZygote(StartupParam startupParam) throws Throwable { 5 XposedBridge.hookAllMethods(ActivityThread.class, "systemMain",