Xposed使用知识
2021-02-07 本文已影响0人
Sharkchilli
前言
前面写了两篇关于Xposed的一些基本使用,现在我们就把关注点集中到它的一些api上。
目录
Xposed Hook 构造方法
Xposed修改类属性
Xposed方法主动调用
Xposed Hook 构造方法
我们先看一下我们要hook的类
public class SharkUtils {
public String name;
private int age;
public SharkUtils(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Hook其构造方法的代码如下
public class HookTest implements IXposedHookLoadPackage {
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
if (loadPackageParam.packageName.equals("com.shark.constructiontarget")) {
XposedBridge.log(" has Hooked!");
Class clazz = loadPackageParam.classLoader.loadClass(
"com.shark.uitls.SharkUtils");
XposedHelpers.findAndHookConstructor(clazz, String.class, int.class, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
super.beforeHookedMethod(param);
XposedBridge.log("SharkUtils() is called!!beforeHookedMethod");
//得到参数
java.lang.Object[] params = param.args;
String name = (String) params[0];
int age = (int) params[1];
XposedBridge.log("name is:" + name);
XposedBridge.log("age is:" + age);
}
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
XposedBridge.log("SharkUtils() is called!!beforeHookedMethod");
}
});
}
}
}
Hook 构造方法使用findAndHookConstructor方法,param.args是参数数组他是Object[]类型,所以使用的时候记得转换一下。
Xposed修改类属性
这里可以使用反射去修改,但是Xposed已经提供了一套api供我们使用。它的核心也是反射。
需要hook的类如下
public class Student {
public static String name = "fj";
private int age;
public Student(int age) {
this.age = age;
}
public static String getName() {
return name;
}
public static void setName(String name) {
Student.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
修改静态属性
public class HookTest implements IXposedHookLoadPackage {
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
if (loadPackageParam.packageName.equals("com.shark.constructiontarget")) {
XposedBridge.log(" has Hooked!");
//得到Class
Class clazz = loadPackageParam.classLoader.loadClass(
"com.shark.uitls.Student");
XposedBridge.log("Student Class->"+clazz);
//修改静态属性
XposedHelpers.setStaticObjectField(clazz, "name","shark");
//获取静态属性
String name= (String) XposedHelpers.getStaticObjectField(clazz,"name");
XposedBridge.log("name is :"+name);
}
}
}
只要调用XposedHelpers.setStaticObjectField方法就可以完成修改了。对应的属性还有setStaticByteField、setStaticBooleanField等。
修改普通属性
public class HookTest implements IXposedHookLoadPackage {
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
if (loadPackageParam.packageName.equals("com.shark.constructiontarget")) {
XposedBridge.log(" has Hooked!");
//得到Class
Class clazz = loadPackageParam.classLoader.loadClass(
"com.shark.uitls.Student");
//hook构造方法
XposedHelpers.findAndHookConstructor(clazz, int.class, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
super.beforeHookedMethod(param);
}
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
//得到构造的对象
Object thisobj = param.thisObject;
//修改普通属性
XposedHelpers.setIntField(thisobj, "age", 24);
}
});
}
}
}
修改普通属性我们客户已hook其构造方法,使用param.thisObject得到对象,再调用 XposedHelpers.setIntField修改属性值。
Xposed方法主动调用
主动调用静态方法
public class HookTest implements IXposedHookLoadPackage {
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
if (loadPackageParam.packageName.equals("com.shark.constructiontarget")) {
XposedBridge.log(" has Hooked!");
//得到Class
Class clazz = loadPackageParam.classLoader.loadClass(
"com.shark.uitls.Student");
//反射调用
// Method getName = clazz.getDeclaredMethod("getName");
// String invoke = (String) getName.invoke(null);
// XposedBridge.log(invoke);
//Xposed 提供的方式 设置name
java.lang.Class<?>[] parameterTypes = {String.class};
XposedHelpers.callStaticMethod(clazz,"setName",parameterTypes,"shark");
//Xposed 提供的方式 获得name
String getName1 = (String)XposedHelpers.callStaticMethod(clazz, "getName");
XposedBridge.log(getName1);
}
}
}
同样你可以使用反射(在注释中已给出),Xposed也提供了XposedHelpers.callStaticMethod直接调用类的静态方法。
主动调用普通方法
public class HookTest implements IXposedHookLoadPackage {
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
if (loadPackageParam.packageName.equals("com.shark.constructiontarget")) {
XposedBridge.log(" has Hooked!");
//得到Class
Class clazz = loadPackageParam.classLoader.loadClass(
"com.shark.uitls.Student");
//直接创建对象
// Object student = XposedHelpers.newInstance(clazz, 30);
//的到构造方法 创建对象
Constructor declaredConstructor = clazz.getDeclaredConstructor(int.class);
Object student = declaredConstructor.newInstance(33);
//调用方法
int getAge = (int) XposedHelpers.callMethod(student, "getAge");
XposedBridge.log("age is:" + getAge);
}
}
}
Xposed提供了XposedHelpers.newInstance直接构造出对象,以此在调用XposedHelpers.callMethod就可以调用普通方法了。你也可以得到构造方法来new出对象。
但很多时候我们需要的是已经new出来的对象,而不是自己去new因为这个对象里面可能有我们需要用到的上下文。其实我们在Hook这个类的任意普通方法都可以通过param.thisObject得到该对象并保存到成员属性中,这样就能够去调用其对应的方法了
给个例子
public class HookTest implements IXposedHookLoadPackage {
//主动调用的对象
public Object studentObj;
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
if (loadPackageParam.packageName.equals("com.shark.constructiontarget")) {
XposedBridge.log(" has Hooked!");
//得到Class
Class Student = loadPackageParam.classLoader.loadClass(
"com.shark.uitls.Student");
Class SharkUtils = loadPackageParam.classLoader.loadClass(
"com.shark.uitls.SharkUtils");
//Hook Student的构造方法
XposedHelpers.findAndHookConstructor(Student, int.class, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
super.beforeHookedMethod(param);
}
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
studentObj = param.thisObject;
}
});
//Hook getName 在调用Student.getAge之前修改它
XposedHelpers.findAndHookMethod(SharkUtils, "getName", new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
super.beforeHookedMethod(param);
XposedHelpers.callMethod(studentObj,"setAge",80);
}
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
}
});
}
}
}