AndroidMethodHook
Introduction: android art hook like Sophix
Tags:
结合阿里 Sophix 热修复方案(https://yq.aliyun.com/articles/74598?t=t1#)
使用了 dexmaker 库,主要用来动态生成 dex 文件。
使用方法:
HookManager.findAndHookMethod(MainActivity.class, "onCreate", Bundle.class, new MethodCallback() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
super.beforeHookedMethod(param);
Log.d("panda", "onCreate:"+param.thisObject.getClass().getName());
}
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
Log.d("panda", "i'm in method " +param.method.getName()+" afterHookedMethod");
}
});
HookManager.startHooks(base);
通过对 Native JmethodId 内容替换实现 method hook,为了达到类似于 xposed 的效果,需要动态生成 method,使用了 dexmaker 库用以动态生成 mehtod。
每个需要 hook 的方法都会 dexmaker 生成一个一摸一样的方法,将 this 和传入参数封装成 Object[] args 传给 MethodUtil 类的 invoke 函数,然后回调 MethodCallback 实现类似于 xposed mehtod hook。
生成代理 method 方式如下:
package com.panda.hook.andhook;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public double test(Object thiz,int a,int b,char cr){
return (a+0.0)/b;
}
}
public class com_panda_hook_andhook_MainActivity {
public com_panda_hook_andhook_MainActivity() {
super();
}
public double test(Object arg12, int arg13, int arg14, char arg15) {
return MethodUtil.invoke("com_panda_hook_andhook_MainActivity_testLIICD", this, new Object[]{
arg12, Integer.valueOf(arg13), Integer.valueOf(arg14), Character.valueOf(arg15)}).doubleValue();
}
}
