rovo89 / XposedBridge

The Java part of the Xposed framework.
3.86k stars 1.1k forks source link

Hooking a method with a specific parameter #294

Open bluekapo opened 1 year ago

bluekapo commented 1 year ago

I need to change device's info like brand and fingerprint. android.os.SystemProperties has different methods like get, getInt or getBoolean, but the actual value, lets say for brand, is retrieved after calling "get" with a parameter "ro.product.brand" like this (returns "Class: google"): Class<?> newClass = Class.forName("android.os.SystemProperties"); XposedBridge.log("Class: " + Arrays.toString(newClass.getMethods())); Method method = newClass.getDeclaredMethod("get", String.class); XposedBridge.log("Class: " + method.invoke(null, "ro.product.brand").toString());

Now to the point. My method to hook this calls (there're more cases in switch (param), I deleted it so it doesn't take much place :

` XposedBridge.hookAllMethods(Class.forName("android.os.SystemProperties"), "get", new XC_MethodReplacement() {

@Override
protected Object replaceHookedMethod(MethodHookParam methodHookParam) throws Throwable {
    XposedBridge.log(methodHookParam.args.toString());
    String[] paramList = Arrays.copyOf(methodHookParam.args, methodHookParam.args.length, String[].class);
    String param = paramList[0];
    XposedBridge.log(paramList.toString());
    for (int i = 0; i < methodHookParam.args.length; i++) {
        try {
            XposedBridge.log("1:  " + paramList[i]);

        } catch (NullPointerException ex) {
            XposedBridge.log("Error: "+ ex);
        }
    }
    switch (param) {
         case "ro.build.tags":
            return "release-keys";
        case "ro.product.model":
            return "SCH-I739";
        case "ro.product.brand":
            return "samsung";
        case "ro.product.name":
            return "kyleplusctc";
    }
    return "";
}

});`

So if we talk about brand, parameter has to be "ro.product.brand". However, when the function above is called, I don't get parameters I need (what I mean is throughout the cycle It only "recognises" 6 different parameters, like ro.build.nickname, but not the ro.product.brand or ro.build.id etc.). When I try to change it like methodHookParam.args[0] = "ro.product.brand"; , at some point I will get an error and crash an app, because the original param at that time required int as a return value (correct me if I'm wrong, but I get this error: Crash unexpextedly: java.lang.NumberFormatException: For input string: "samsung").

Btw, also tried to implement it like this (doesn't work either, same error): ` XposedHelpers.findAndHookMethod(Class.forName("android.os.SystemProperties"), "get", String.class, new XC_MethodHook() {

        @Override
        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
            param.args[0] = "ro.product.brand";
            XposedBridge.log("Before: " + String.valueOf(param.getResult()));
            param.setResult("samsung");
        }

        @Override
        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
            XposedBridge.log(String.valueOf("After: " + param.getResult()));
        }
    });

`

Haven't tried downgrading yet. Any suggestions? Thanks in advance!

ps. I'm sorry for a bad code wrap, Idk what happened to it.