public static final int kArm = 1; // ARMv7架构
public static final int kArm64 = 2; // ARMv8/ARM64架构
public static final int kX86 = 3; // x86架构
public static final int kX86_64 = 4; // x86_64架构public static final int FLAG_FIELD_BACKUP_INSTANCE = 0x40; // 启用实例字段备份
public static final int FLAG_FIELD_BACKUP_STATIC = 0x80; // 启用静态字段备份
public static final int FLAG_FIELD_BACKUP_BAN = 0x100; // 禁用字段备份
public static final int FLAG_FIELD_DISABLED = 0x200; // 字段功能禁用
public static final int FLAG_FIELD_INVALID = FLAG_FIELD_BACKUP_BAN | FLAG_FIELD_DISABLED;public static final int FLAG_INIT_CLASS = 0x1; // 强制类初始化
public static final int FLAG_DEBUG = 0x2; // 调试模式
public static final int FLAG_LOADER_FROM_CALLER = 0x4; // 使用调用者的类加载器
public static final int FLAG_DISABLE_JIT = 0x8; // 禁用JIT编译
public static final int FLAG_SUSPEND_VM = 0x10; // 操作期间挂起VM
public static final int FLAG_NO_COMPILE = 0x20; // 禁用编译
public static final int FLAG_DISABLE_LOG = 0x400; // 禁用日志
public static final int FLAG_INJECT = 0x800; // 注入模式
public static final int FLAG_INIT_RPC = 0x1000; // 初始化RPC
public static final int FLAG_CALL_CHAIN = 0x2000; // 调用链模式public final static int STATUS_INIT_OK = 1; // 初始化成功
public final static int STATUS_DISABLED = 2; // 已禁用
public final static int STATUS_NOT_INIT = 4; // 未初始化
public final static int STATUS_INIT_FAIL = 8; // 初始化失败public static final int SEARCH_STATIC = 1; // 搜索静态方法
public static final int SEARCH_INSTANCE = 2; // 搜索实例方法
public static final int SEARCH_ALL = SEARCH_STATIC | SEARCH_INSTANCE; // 搜索所有方法public static boolean init(int flags);功能: 初始化Albatross框架
参数:
flags: 初始化标志位组合
返回:true如果初始化成功,否则false
public static boolean loadLibrary(String library);
public static boolean loadLibrary(String library, int loadFlags);功能: 加载本地库并初始化Albatross
参数:
library: 要加载的本地库名称loadFlags: 加载标志位组合
返回:true如果Albatross初始化成功
public native static boolean initRpcClass(Class<?> clz);功能: 初始化RPC类
参数:
clz: 需要RPC初始化的类
返回:true如果成功,否则false
public static String supportFeatures();功能: 获取当前支持的功能列表
返回: 支持的功能字符串,如 "jit,aot,instruction"
public static boolean backupAndHook(Member target, Method hook, Method backup) throws AlbatrossException;功能: 备份并Hook一个方法
参数:
target: 目标方法或构造函数hook: Hook方法backup: 备份方法(可为null)
返回:true如果成功
抛出:AlbatrossException如果失败
public static boolean backup(Member target, Method backup) throws AlbatrossException;功能: 创建方法备份而不Hook
参数:
target: 目标方法或构造函数backup: 备份方法
返回:true如果成功
public static boolean replace(Member target, Method hook) throws AlbatrossException;功能: 替换方法而不创建备份
参数:
target: 目标方法或构造函数hook: 替换方法
返回:true如果成功
public static int hookClass(Class<?> hooker) throws AlbatrossErr;
public static int hookClass(Class<?> hooker, Class<?> defaultClass) throws AlbatrossErr;
public static int hookObject(Class<?> hooker, Object instance) throws AlbatrossErr;
public static int hookClass(Class<?> hooker, ClassLoader loader, Class<?> defaultClass, Object instance) throws AlbatrossErr;功能: 应用Hooker类中定义的所有Hook
参数:
hooker: Hooker类defaultClass: 默认目标类loader: 类加载器instance: 目标实例
返回: 成功Hook的数量
public static native <T> T convert(Object object, Class<T> hooker);功能: 将对象转换为Hooker类型以进行方法/字段访问
public static boolean isCompiled(Method method);功能: 检查方法是否已编译为机器码
public static int compileClass(Class<?> clazz, int compileOption);功能: 使用指定编译策略编译类
public static boolean compileMethod(Member method);功能: 将特定方法编译为机器码
public static boolean setMethodExecMode(Member method, int execMode);功能: 设置方法的执行模式
public static void setExecConfiguration(int targetExecMode, int hookerExecMode);
public static void setExecConfiguration(int targetExecMode, int hookerExecMode, int hookerBackupExec);功能: 设置执行配置
参数:
targetExecMode: 目标方法执行模式hookerExecMode: Hooker方法执行模式hookerBackupExec: Hooker备份方法执行模式
public static void disableCompileBackupCall();功能: 禁用备份方法调用的编译
public static boolean backupField(Field target, Field backup) throws FieldException, AlbatrossErr;功能: 创建字段实现的备份
public static boolean isFieldEnable();功能: 检查字段Hook是否启用(Android ≤7.0默认禁用)
public static void disableFieldBackup(); // 禁用字段备份
public static void enableAlbatross(); // 重新启用框架
public static void disableAlbatross(); // 禁用整个框架public static int transactionBegin();
public static synchronized native int transactionBegin(boolean disableHidden);功能: 开始Hook事务,用于批量处理
public static int transactionEnd(boolean doTask);
public static int transactionEnd(boolean doTask, boolean suspendVM);功能: 提交或回滚事务
参数:
doTask: 执行待处理的HooksuspendVM: 操作期间挂起VM
public static synchronized native int transactionLevel();功能: 返回当前事务嵌套级别
public static InstructionListener hookInstruction(Member member, int dexPc, InstructionCallback callback);
public static InstructionListener hookInstruction(Member member, int minDexPc, int maxDexPc, InstructionCallback callback);
public static InstructionListener hookInstruction(Member member, int minDexPc, int maxDexPc, InstructionCallback callback, int compile);功能: Hook方法指令执行
参数:
member: 目标方法dexPc: DEX程序计数器位置minDexPc/maxDexPc: DEX PC范围callback: 指令回调compile: 编译选项
返回:InstructionListener实例,用于取消Hook
public static boolean addToVisit(Class<?> clz);功能: 注册类以便将来Hook
public static native boolean isMainThread();功能: 检查当前线程是否为主应用线程
public static native boolean isHooked(Class<?> clz);功能: 验证类是否有活动的Hook
public static native Application currentApplication();功能: 获取当前Application上下文
public native static Class<?> getCallerClass();功能: 获取调用者类
public static Class<?> findClass(String className);
public static Class<?> findClass(String[] className);
public static Class<?> findClass(String[] className, ClassLoader loader);
public static Class<?> findClassFromApplication(String className);功能: 从所有类加载器中查找类
public static native int getRuntimeISA();功能: 获取运行时指令集架构
返回: 当前CPU架构,如 kArm, kArm64, kX86, 或 kX86_64
public native static long getObjectAddress(Object object);功能: 获取对象的内存地址
public static boolean disableMethod(Method method);
public static native boolean disableMethod(Method method, boolean throwException);功能: 禁用方法
public static void resetLogger(Method infoLogger, Method errLogger);功能: 重置日志器
public synchronized static void disableLog();功能: 禁用日志
public static DlInfo openLib(String libName);功能: 打开动态库
参数:
libName: 库名称
返回:DlInfo实例,用于符号查找
public static class DlInfo {
public long getSymbolAddress(String symbol); // 获取符号地址
public void close(); // 关闭库句柄
}@TargetClass(Activity.class)
public class ActivityHooker {
@MethodHookBackup
private void onCreate(Bundle savedInstanceState) {
Log.d("Albatross", "Activity created!");
onCreate(savedInstanceState);
}
}
// 应用Hook
Albatross.hookClass(ActivityHooker.class);Method targetMethod = MyClass.class.getDeclaredMethod("targetMethod");
InstructionListener listener = Albatross.hookInstruction(targetMethod, 0, 10,
(method, self, dexPc, invocationContext) -> {
Log.d("Albatross", "Instruction at dexPc: " + dexPc);
});Albatross.transactionBegin();
Albatross.setExecConfiguration(
ExecOption.JIT_OPTIMIZED,
ExecOption.JIT_OPTIMIZED
);
Albatross.hookClass(MyHooker.class);
Albatross.transactionEnd(true);@TargetClass(className = "com.example.TargetClass")
public class TargetHooker {
@FieldRef
static int mSecretField;
public static void readSecretField(Object target) {
if (Albatross.isFieldEnable()) {
// 通过convert访问字段
TargetHooker hooker = Albatross.convert(target, TargetHooker.class);
int value = hooker.mSecretField;
Log.d("Albatross", "Secret value: " + value);
}
}
}DlInfo lib = Albatross.openLib("libc.so");
if (lib != null) {
long mallocAddr = lib.getSymbolAddress("malloc");
Log.d("Albatross", "malloc address: 0x" + Long.toHexString(mallocAddr));
lib.close();
}-
字段Hook限制:
- Android ≤7.1默认禁用
-
编译策略:
- 调试模式: 无优化,保持原有的执行方式
- 发布模式: JIT/AOT,机器码执行
-
架构支持:
- x86, x86_64, ARMv7, ARM64
-
错误处理:
- 始终将Hook操作包装在try-catch中
- 检查返回值以确认成功状态
-
事务使用:
- 始终配对使用
transactionBegin/End调用 - 事务失败时会自动回滚
- 始终配对使用
-
性能优化:
- 使用事务进行批量Hook操作
- 合理设置编译选项
- 避免频繁的类查找操作
本API文档提供了使用Albatross构建高级Hook场景的基础。要获得完整功能,请结合注解参考文档中描述的注解系统使用这些方法。