#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#define TARGET_PKG "com.proximabeta.mf.uamo" // 目标应用包名
#define TARGET_LIB "libanogs.so" // 目标动态库名
#define CHECK_INTERVAL 3 // 检查间隔(秒)
#define MAX_RETRY 5 // 最大重试次数
#define PAGE_SIZE sysconf(_SC_PAGESIZE)
#define ARM64_INSTRUCTION_SIZE 8 // ARM64指令长度(8字节)
#define PTRACE_DATA_TYPE uint64_t // 64位数据类型
// Hook点配置结构(ARM64专用,64位指令)
typedef struct {
long offset; // 模块内偏移(64位,需与基址相加得到绝对地址)
PTRACE_DATA_TYPE shellcode; // 64位机器码(如NOP指令:0xD503201F)
PTRACE_DATA_TYPE original; // 原始64位指令(运行时读取)
} HookPoint;
// 线程控制结构体(共享状态)
struct ThreadControl {
pthread_mutex_t lock; // 互斥锁
int active; // 监控状态(1=运行,0=停止)
int hook_success; // Hook结果(1=成功,0=失败)
};
// 示例Hook点(需根据实际反汇编填写,注意8字节对齐)
HookPoint hooks[] = {
{0x1A8D08, 0xD503201F, 0}, // 偏移+64位NOP指令+原始指令(初始0)
// 添加更多Hook点:{offset, 0x十六进制机器码, 0},
};
int num_hooks = sizeof(hooks) / sizeof(HookPoint);
// 全局控制变量
struct ThreadControl ctrl = {
.active = 1,
.hook_success = 0
};
// 信号处理函数(处理中断信号)
void handle_signal(int sig) {
printf("\n接收到信号 %d,正在清理...\n", sig);
pthread_mutex_lock(&ctrl.lock);
ctrl.active = 0; // 停止监控
pthread_mutex_unlock(&ctrl.lock);
}
// 获取目标进程PID(带缓存)
static int get_pid_cached(int *cached_pid) {
static int last_pid = -1;
if (*cached_pid > 0) return *cached_pid; // 使用缓存PID
char cmd[128];
snprintf(cmd, sizeof(cmd), "pidof -s %s", TARGET_PKG);
FILE *fp = popen(cmd, "r");
if (!fp) return -1;
int pid = -1;
if (fscanf(fp, "%d", &pid) == 1) {
last_pid = pid;
}
pclose(fp);
return (*cached_pid = pid); // 更新缓存并返回
}
// 获取模块基址(带缓存,64位处理)
static long get_module_base_cached(int pid, long *cached_base) {
static long last_base = -1L;
if (*cached_base != -1L) return *cached_base; // 使用缓存基址
char path[64];
snprintf(path, sizeof(path), "/proc/%d/maps", pid);
FILE *fp = fopen(path, "r");
if (!fp) return -1L;
char line[256];
while (fgets(line, sizeof(line), fp)) {
if (strstr(line, TARGET_LIB)) {
char *start_addr = strtok(line, "-");
*cached_base = strtoul(start_addr, NULL, 16); // 解析64位十六进制地址
fclose(fp);
return *cached_base;
}
}
fclose(fp);
return (*cached_base = -1L); // 模块未加载
}
// ARM64专用Hook安装函数(64位指令操作)
static int install_hooks(int pid, long base) {
if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) == -1) {
perror("ptrace 附加失败");
return -1;
}
int status;
waitpid(pid, &status, 0); // 等待进程暂停
int success_count = 0;
for (int i = 0; i < num_hooks; i++) {
HookPoint *hook = &hooks[i];
uintptr_t addr = (uintptr_t)base + hook->offset; // 计算绝对地址
// ARM64地址对齐检查(必须8字节对齐)
if ((addr & (ARM64_INSTRUCTION_SIZE - 1)) != 0) {
fprintf(stderr, "错误:Hook地址0x%lx未8字节对齐(ARM64要求)\n", addr);
continue;
}
// 读取原始64位指令
hook->original = ptrace(PTRACE_PEEKDATA, pid, (void*)addr, NULL);
if (errno != 0) {
fprintf(stderr, "读取0x%lx原始指令失败: %s\n", addr, strerror(errno));
continue;
}
// 写入新64位指令
if (ptrace(PTRACE_POKEDATA, pid, (void*)addr, hook->shellcode) == -1) {
fprintf(stderr, "写入0x%lx新指令失败: %s\n", addr, strerror(errno));
continue;
}
// 验证写入结果
PTRACE_DATA_TYPE verify = ptrace(PTRACE_PEEKDATA, pid, (void*)addr, NULL);
if (verify == hook->shellcode) {
success_count++;
printf("Hook[%d] 成功:0x%016" PRIxPTR " 原始=0x%016" PRIx64 " 新=0x%016" PRIx64 "\n",
i, addr, hook->original, hook->shellcode);
} else {
fprintf(stderr, "Hook[%d] 验证失败:预期0x%016" PRIx64 " 实际0x%016" PRIx64 "\n",
i, hook->shellcode, verify);
}
}
ptrace(PTRACE_DETACH, pid, NULL, NULL); // 分离进程
return success_count == num_hooks ? 0 : -1; // 所有Hook成功才返回0
}
// 监控线程函数(循环尝试Hook)
void* monitor_thread(void *arg) {
int cached_pid = -1;
long cached_base = -1L;
int attempt = 0;
while (1) {
pthread_mutex_lock(&ctrl.lock);
if (!ctrl.active) { // 检查是否停止监控
pthread_mutex_unlock(&ctrl.lock);
break;
}
pthread_mutex_unlock(&ctrl.lock);
int pid = get_pid_cached(&cached_pid);
if (pid <= 0) {
printf("目标进程未运行,等待%d秒...\n", CHECK_INTERVAL);
sleep(CHECK_INTERVAL);
continue;
}
long base = get_module_base_cached(pid, &cached_base);
if (base == -1L) {
printf("目标模块未加载,等待%d秒...\n", CHECK_INTERVAL);
sleep(CHECK_INTERVAL);
continue;
}
printf("尝试 %d/%d:PID=%d,模块基址=0x%lx\n", ++attempt, MAX_RETRY, pid, base);
if (install_hooks(pid, base) == 0) {
pthread_mutex_lock(&ctrl.lock);
ctrl.hook_success = 1; // 标记Hook成功
pthread_mutex_unlock(&ctrl.lock);
printf("所有Hook安装成功!\n");
break;
}
if (attempt >= MAX_RETRY) {
printf("超过最大尝试次数%d,Hook失败\n", MAX_RETRY);
break;
}
sleep(CHECK_INTERVAL); // 间隔后重试
}
return NULL;
}
int main() {
// 初始化互斥锁
if (pthread_mutex_init(&ctrl.lock, NULL) != 0) {
perror("互斥锁初始化失败");
return EXIT_FAILURE;
}
// 注册信号处理(Ctrl+C退出)
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);
pthread_t monitor;
if (pthread_create(&monitor, NULL, monitor_thread, NULL) != 0) {
perror("监控线程创建失败");
pthread_mutex_destroy(&ctrl.lock);
return EXIT_FAILURE;
}
// 等待监控线程结束
pthread_join(monitor, NULL);
// 清理资源
pthread_mutex_destroy(&ctrl.lock);
if (ctrl.hook_success) {
printf("Hook安装成功,程序正常退出\n");
return EXIT_SUCCESS;
} else {
printf("Hook安装失败,程序退出\n");
return EXIT_FAILURE;
}
}完整修复这个代码。