1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| int ismyapp = 0; #define PAGE_START(addr) ((addr) & PAGE_MASK) #define PAGE_END(addr) (PAGE_START(addr) + PAGE_SIZE)
extern "C" { #define EXPORT __attribute__((visibility("default"))) __attribute__((used)) EXPORT void nativeForkAndSpecializePre( JNIEnv *env, jclass clazz, jint *_uid, jint *gid, jintArray *gids, jint *runtimeFlags, jobjectArray *rlimits, jint *mountExternal, jstring *seInfo, jstring *niceName, jintArray *fdsToClose, jintArray *fdsToIgnore, jboolean *is_child_zygote, jstring *instructionSet, jstring *appDataDir, jstring *packageName, jobjectArray *packagesForUID, jstring *sandboxId) {
const char *app_packageName = (env)->GetStringUTFChars(*appDataDir, 0); const char *app_niceName = (env)->GetStringUTFChars(*niceName, 0);
if(strcmp(app_niceName,"com.xxx.xxx") ==0 ){ ismyapp = 1; }else{ ismyapp = 0; }
} EXPORT int nativeForkAndSpecializePost(JNIEnv *env, jclass clazz, jint res) { if (res == 0) { if(ismyapp){ int ret = -1; pthread_t tid1; ret = pthread_create(&tid1, NULL, reinterpret_cast<void *(*)(void *)>(thread_func1), NULL); LOGD("create thread ret: %d\n", ret);
}
} else { } return 0; } void thread_func1() {
LOGD(" in Riru testhook thread_func1 "); void *base_addr; while(true){
base_addr = get_module_base(-1,"libxxx.so"); LOGD("find libxxx.so %p", base_addr); if (base_addr != 0) { break; } }
void * symbol = lookup_symbol("/data/data/com.xxx.xxx/lib/libxxx.so","createxxx"); MSHookFunction(symbol, (void*)&new_debug, (void**)&old_debug);
int offset = 0x1234; void * symboladdr = base_addr + offset; MSHookFunction(symboladdr, (void*)&new_debug, (void**)&old_debug);
mprotect((void *)PAGE_START(symboladdr), PAGE_SIZE, PROT_READ | PROT_WRITE); *(void **)symboladdr = new_debug; __builtin___clear_cache((void *)PAGE_START(addr), (void *)PAGE_END(addr));
int reg =xhook_register(".*/libxxx.so$", "createxxx", (void *) new_debug, (void **)(&old_debug)); int ref = xhook_refresh(0);
LOGD(" [cn.xxx.xxx] xhook_register %d , xhook_refresh %d ",reg,ref);
} void* get_module_base(pid_t pid, const char* module_name) { FILE *fp; long addr = 0; char *pch; char filename[32]; char line[1024]; if (pid < 0) { snprintf(filename, sizeof(filename), "/proc/self/maps"); } else { snprintf(filename, sizeof(filename), "/proc/%d/maps", pid); } fp = fopen(filename, "r"); if (fp != NULL) { while (fgets(line, sizeof(line), fp)) { if (strstr(line, module_name)) { pch = strtok(line, "-");
addr = strtoul(pch, NULL, 16 );
if (addr == 0x8000) addr = 0;
break; } } fclose( fp ); } return (void *)addr; } void* lookup_symbol(char* libraryname,char* symbolname) { void *handle = dlopen(libraryname, RTLD_GLOBAL | RTLD_NOW); if (handle != NULL){ void * symbol = dlsym(handle, symbolname); if (symbol != NULL){ return symbol; }else{ LOGD("dl error: %s", dlerror()); return NULL; } }else{ return NULL; } } int (*old_debug)(int a1, const char *a2); int new_debug(int a1, const char *a2){
LOGD(" debug %s",a2);
return old_debug(a1,a2); }
|