首页
社区
课程
招聘
[原创]FastHook——巧妙利用动态代理实现非侵入式AOP
发表于: 2019-3-31 14:11 7339

[原创]FastHook——巧妙利用动态代理实现非侵入式AOP

2019-3-31 14:11
7339

FastHook框架要求调用者准备与原方法参数一致的Hook方法和Forward方法,这些限制将业务逻辑和Hook逻辑耦合在一起。若不了解FastHook原理,请移步FastHook——一种高效稳定、简洁易用的Android Hook框架
因此可能需要一种新实现方式,其可将业务逻辑和Hook逻辑解耦。一种简单的方案便是动态生成Hook方法和Forword方法。然而该方案对性能的影响比较大,动态生成dex文件并加载是耗时操作。
本文将介绍一种新的思路,统一Hook方法,利用动态代理创建Forward方法。该方案比上述方案更加高效、简洁
Hook方案

Hook方法必须能实现以下两点需求
1. 能正确识别原方法
2. 能正确解析原方法参数

以32位arm平台为例

1. r0寄存器存放被调用方法ArtMethod指针
2. r1~r3存放前3个参数
3. 从(sp+指针长度)地址起,按照参数顺序依次存放参数
显而易见,只需将被调用方法ArtMethod指针与sp指针传如Hook方法即可。32位指针长度为4字节,将以int类型传入,一种返回类型对应一个Hook方法

64位指针长度为8字节,将以long类型传入,一种返回类型对应一个Hook方法

ART类型

在ART里,除了long和double类型以8字节解析外,其余类型都以4字节解析
Java是没有指针概念的,所以必须将传入的指针转化为Java对象,而并没有直接将指针转化为Java的接口,只能先将指针转化为JNI对象jobject,从JNI返回Java时会将jobject转化为Java对象

1. ToReflectedMethod将ArtMethod指针转化为JNI对象jobject
2. FromReflectedMethod将JNI对象jobject转化为ArtMethod指针

1. NewLocalRef将对象实际指针转化为JNI对象jobject,符号为_ZN3art9JNIEnvExt11NewLocalRefEPNS_6mirror6ObjectE
2. DecodeJObject将JNI对象jobject转化为对象实际指针,符号为_ZNK3art6Thread13DecodeJObjectEP8_jobject

按照上述规则,可以正确解析出方法参数

newProxyInstance为Java动态代理接口,可以明显看出Java对于动态代理的范围限制在了接口上,非接口方法不能代理
为了实现AOP,需要想办法绕过JAVA接口限制,实现创建任意方法的代理方法的功能。

generateProxy进行实际代理类创建,可以接口方法最后是以Method对象的形式传入,做一个大胆的尝试,将Method[]替换为我们想要的任意方法,这样变实现了创建任意方法的代理方法的功能。如下所示:

interfaces设置为null即可。

构造方法的类型为Constructor,而不是Method,因此构造方法不能用generateProxy生成对应代理方法。需要想个办法将Constructor转为Method。
在ART里,所有方法对对应一个ArtMethod对象,构造方法也不例外。在前面的分析可知,ArtMethod对象和Java对象是可以相互转化的,如果ArtMethod是构造方法则转化为Constructor对象,反之,则转化为Method对象
如果想将Constructor对象转化为Method对象,可以这么做
1. 获取Constructor对象
2. 将其转化为ArtMethod对象
3. 将ArtMethod设置为非构造方法,通过清除kAccConstructor标志位实现
4. 将ArtMethod对象转化为Method对象
5. 还原ArtMethod为构造方法

至此,可以得到任意方法的代理方法,即Forward方法,在需要的时候反射调用即可。

假设有一个Test类

为其创建了代理类ProxyTest,如果需要调用原方法,只需调用ProxyTest的代理方法,并传入原方法参数。

1. 通过反射获取代理方法test
2. 反射调用代理方法,传入原方法参数,testObject为Test实例,即this参数
根据FastHook原理,这里调用必须是代理方法test,即Forward方法,不然将无法实现原方法的调用。而代理方法test是public方法,反射调用的实际方法将由testObject来决定,Test也有一个test方法,因此实际调用的将是Test类的test方法而不是ProxyTest的test方法
因此无论在什么情况下,必须保证反射调用代理方法时,调用的都是其本身,即代理方法必须是Direct方法

当代理方法不是构造方法时,强制将其设置为private方法,以实现静态分派代理方法

代理方法test属于ProxyTest类,因此需要一个ProxyTest类型的实例,而现在传入的testObject是Test类型的,类型不匹配。要想办法让testObject继承与ProxyTest类
FastHook采用一个巧妙的办法,将ProxyTest的父类置空,让ART误认为这是Object类,众所周知,任何对象都继承与Object类,完美解决类型不匹配问题

FastHook:https://github.com/turing-technician/FastHook/tree/callback

 
 
 
    /*
     * Quick invocation stub internal.
     * On entry:
     *   r0 = method pointer
     *   r1 = argument array or null for no argument methods
     *   r2 = size of argument array in bytes
     *   r3 = (managed) thread pointer
     *   [sp] = JValue* result
     *   [sp + 4] = result_in_float
     *   [sp + 8] = core register argument array
     *   [sp + 12] = fp register argument array
     *  +-------------------------+
     *  | uint32_t* fp_reg_args   |
     *  | uint32_t* core_reg_args |
     *  |   result_in_float       | <- Caller frame
     *  |   Jvalue* result        |
     *  +-------------------------+
     *  |          lr             |
     *  |          r11            |
     *  |          r9             |
     *  |          r4             | <- r11
     *  +-------------------------+
     *  | uint32_t out[n-1]       |
     *  |    :      :             |        Outs
     *  | uint32_t out[0]         |
     *  | StackRef<ArtMethod>     | <- SP  value=null
     *  +-------------------------+
     */
private static Object hookHandleObject(int targetArtMethod, int sp) {
    FastHookParam param = hookHandle(targetArtMethod,sp);

    return param.result;
}

private static boolean hookHandleBoolean(int targetArtMethod, int sp) {
    FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Boolean) {
            return ((Boolean) param.result).booleanValue();
        }    

    return false;
}

private static byte hookHandleByte(int targetArtMethod, int sp) {
    FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Byte) {
            return ((Byte) param.result).byteValue();
        }

        return 0;
}

private static char hookHandleChar(int targetArtMethod, int sp) {
        FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Character) {
            return ((Character) param.result).charValue();
        }    

    return 0;
}

private static short hookHandleShort(int targetArtMethod, int sp) {
        FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Short) {
            return ((Short) param.result).shortValue();
        }    

    return 0;
}

private static int hookHandleInt(int targetArtMethod, int sp) {
        FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Integer) {
            return ((Integer) param.result).intValue();
        }

    return 0;
}

private static long hookHandleLong(int targetArtMethod, int sp) {
        FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Long) {
            return ((Long) param.result).longValue();
        }

    return 0;
}

private static float hookHandleFloat(int targetArtMethod, int sp) {
        FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Float) {
            return ((Float) param.result).floatValue();
        }

    return 0;
}

private static double hookHandleDouble(int targetArtMethod, int sp) {
        FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Double) {
            return ((Double) param.result).doubleValue();
        }

    return 0;
}

private static void hookHandleVoid(int targetArtMethod, int sp) {
        hookHandle(targetArtMethod,sp);
        return;
}
private static Object hookHandleObject(long targetArtMethod, long sp) {
    FastHookParam param = hookHandle(targetArtMethod,sp);

    return param.result;
}

private static boolean hookHandleBoolean(long targetArtMethod, long sp) {
     FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Boolean) {
            return ((Boolean) param.result).booleanValue();
        }

    return false;
}

private static byte hookHandleByte(long targetArtMethod, long sp) {
    FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Byte) {
            return ((Byte) param.result).byteValue();
        }

    return 0;
}

private static char hookHandleChar(long targetArtMethod, long sp) {
        FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Character) {
            return ((Character) param.result).charValue();
        }

    return 0;
}

private static short hookHandleShort(long targetArtMethod, long sp) {
        FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Short) {
            return ((Short) param.result).shortValue();
        }

    return 0;
}

private static int hookHandleInt(long targetArtMethod, long sp) {
        FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Integer) {
            return ((Integer) param.result).intValue();
        }

    return 0;
}

private static long hookHandleLong(long targetArtMethod, long sp) {
        FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Long) {
            return ((Long) param.result).longValue();
        }

    return 0;
}

private static float hookHandleFloat(long targetArtMethod, long sp) {
        FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Float) {
            return ((Float) param.result).floatValue();
        }

    return 0;
}

private static double hookHandleDouble(long targetArtMethod, long sp) {
        FastHookParam param = hookHandle(targetArtMethod,sp);

    if(param.result != null && param.result instanceof Double) {
            return ((Double) param.result).doubleValue();
        }

    return 0;
}

private static void hookHandleVoid(long targetArtMethod, long sp) {
        hookHandle(targetArtMethod,sp);
        return;
}
static jobject ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID mid, jboolean isStatic)
static jmethodID FromReflectedMethod(JNIEnv* env, jobject method)
jobject NewLocalRef(mirror::Object* obj)
ObjPtr<mirror::Object> Thread::DecodeJObject(jobject obj)
private static FastHookParam parseParam(long sp, Class[] paramType, boolean isStatic) {
        FastHookParam param = new FastHookParam();

    int offset = 0;
        List<Object> args = new ArrayList<Object>();

    if(!isStatic) {
            param.receiver = getObjectParam(sp,offset);
            offset += 4;
        }

    if(paramType == null)
            return param;

    for(Class type : paramType) {
        if(type.equals(boolean.class)) {
                boolean b = getBooleanParam(sp,offset);
                args.add(new Boolean(b));
                offset += 4;
            }else if(type.equals(byte.class)) {
                byte b2 = getByteParam(sp,offset);
                args.add(new Byte(b2));
                offset += 4;
            }else if(type.equals(char.class)) {
                char c = getCharParam(sp,offset);
                args.add(new Character(c));
                offset += 4;
            }else if(type.equals(short.class)) {
                short s = getShortParam(sp,offset);
                args.add(new Short(s));
                offset += 4;
            }else if(type.equals(int.class)) {
                int i = getIntParam(sp,offset);
                args.add(new Integer(i));
                offset += 4;
            }else if(type.equals(long.class)) {
                long l = getLongParam(sp,offset);
                args.add(new Long(l));
                offset += 8;
            }else if(type.equals(float.class)) {
                float f = getFloatParam(sp,offset);
                args.add(new Float(f));
                offset += 4;
            }else if(type.equals(double.class)) {
                double d = getDoubleParam(sp,offset);
                args.add(new Double(d));
                offset += 8;
            }else if(type.equals(void.class)) {
            }else {
                Object obj = getObjectParam(sp,offset);
                args.add(obj);
                offset += 4;
            }
        }

    if(!args.isEmpty()) {
            param.args = args.toArray(new Object[args.size()]);
        }

    return param;
}
jobject GetReflectedMethod(JNIEnv *env, jclass clazz, jlong art_method) {
    jobject result = (*env)->ToReflectedMethod(env,clazz,(void *)art_method,JNI_FALSE);
    return result;
}

jboolean GetBooleanParam(JNIEnv *env, jclass clazz, jlong sp, jint offset) {
    jboolean result = (jboolean)ReadInt32((unsigned char *)sp + pointer_size_ + offset);
    return result;
}

jbyte GetByteParam(JNIEnv *env, jclass clazz, jlong sp, jint offset) {
    jbyte result = (jbyte)ReadInt32((unsigned char *)sp + pointer_size_ + offset);
    return result;
}

jchar GetCharParam(JNIEnv *env, jclass clazz, jlong sp, jint offset) {
    jchar result = (jchar)ReadInt32((unsigned char *)sp + pointer_size_ + offset);
    return result;
}

jshort GetShortParam(JNIEnv *env, jclass clazz, jlong sp, jint offset) {
    jshort result = (jshort)ReadInt32((unsigned char *)sp + pointer_size_ + offset);
    return result;
}

jint GetIntParam(JNIEnv *env, jclass clazz, jlong sp, jint offset) {
    jint result = (jint)ReadInt32((unsigned char *)sp + pointer_size_ + offset);
    return result;
}

jlong GetLongParam(JNIEnv *env, jclass clazz, jlong sp, jint offset) {
    jlong result = (jlong)ReadInt64((unsigned char *)sp + pointer_size_ + offset);
    return result;
}

jfloat GetFloatParam(JNIEnv *env, jclass clazz, jlong sp, jint offset) {
    jfloat result = (jfloat)ReadFloat((unsigned char *)sp + pointer_size_ + offset);
    return result;
}

jdouble GetDoubleParam(JNIEnv *env, jclass clazz, jlong sp, jint offset) {
    jdouble result = (jdouble)ReadDouble((unsigned char *)sp + pointer_size_ + offset);
    return result;
}

jobject GetObjectParam(JNIEnv *env, jclass clazz, jlong sp, jint offset) {
    void *obj = (void *)ReadInt32((unsigned char *)sp + pointer_size_ + offset);
    jobject result = new_local_ref_(env,obj);
    return result;
}
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces,  InvocationHandler h)
private static native Class<?> generateProxy(String name, Class<?>[] interfaces,
                                                 ClassLoader loader, Method[] methods,
                                                 Class<?>[][] exceptions);

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

最后于 2019-3-31 14:19 被图灵技师编辑 ,原因:
收藏
免费 1
支持
分享
最新回复 (2)
雪    币: 187
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
2
学习了
2019-4-11 09:02
0
雪    币: 2420
活跃值: (2872)
能力值: ( LV5,RANK:61 )
在线值:
发帖
回帖
粉丝
3
已经解决。谢谢
最后于 2019-10-17 15:29 被陈可牛编辑 ,原因: 换了别的库
2019-10-17 13:43
0
游客
登录 | 注册 方可回帖
返回
//