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
| #include <jni.h> #include <sys/types.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include<pthread.h> #include <unistd.h> #include <android/log.h> #include <dlfcn.h>
extern "C" {
#define EXPORT __attribute__((visibility("default"))) __attribute__((used)) #define LOG_TAG "C_COLA" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) static char* loadPkgNameStr = NULL;
char *loadPkgName(JNIEnv *env, jstring appDataDir) { if (!appDataDir) return NULL;
const char *app_data_dir = env->GetStringUTFChars(appDataDir, NULL); int user = 0; static char package_name[256]; if (sscanf(app_data_dir, "/data/%*[^/]/%d/%s", &user, package_name) != 2) { if (sscanf(app_data_dir, "/data/%*[^/]/%s", package_name) != 1) { package_name[0] = '\0'; return NULL; } } env->ReleaseStringUTFChars(appDataDir, app_data_dir); return package_name; }
void getPkgNameFromFile(char* hookPackageName){ FILE* file = NULL; file = fopen("/data/local/tmp/pkg_name", "r"); if (file != NULL) { fgets(hookPackageName,200,file); fclose(file); } }
void *hack_thread(void *arg) { LOGD("hook thread :%d", gettid()); sleep(0); const char* soPath_32 = "/system/lib/libcola.so"; const char* soPath_64 = "/system/lib64/libcola.so"; void* handle_64 = dlopen( soPath_64, RTLD_LAZY ); if( !handle_64 ) { LOGD("[%s](%d) dlopen 64bit lib get error: %s\n", __FILE__, __LINE__, dlerror()); void* handle_32 = dlopen( soPath_32, RTLD_LAZY ); if( !handle_32 ) { LOGD("[%s](%d) dlopen 32bit lib get error: %s\n", __FILE__, __LINE__, dlerror()); }
} LOGD("so inject finish"); return NULL; }
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, jboolean *isTopApp, jobjectArray *pkgDataInfoList, jobjectArray *whitelistedDataInfoList, jboolean *bindMountAppDataDirs, jboolean *bindMountAppStorageDirs) {
loadPkgNameStr = loadPkgName(env, *appDataDir);
}
EXPORT int nativeForkAndSpecializePost(JNIEnv *env, jclass clazz, jint res) { if (res == 0) { if (loadPkgNameStr != NULL) { char hookPkgName[200] = " "; getPkgNameFromFile(hookPkgName); if(!strncmp(hookPkgName,loadPkgNameStr,strlen(loadPkgNameStr))){ int ret; pthread_t ntid; if((ret = pthread_create(&ntid, NULL, hack_thread, NULL))) { LOGD("can't create thread: %s\n", strerror(ret)); } } } } else { } return 0; }
EXPORT __attribute__((visibility("default"))) void specializeAppProcessPre( JNIEnv *env, jclass clazz, jint *_uid, jint *gid, jintArray *gids, jint *runtimeFlags, jobjectArray *rlimits, jint *mountExternal, jstring *seInfo, jstring *niceName, jboolean *startChildZygote, jstring *instructionSet, jstring *appDataDir, jboolean *isTopApp, jobjectArray *pkgDataInfoList, jobjectArray *whitelistedDataInfoList, jboolean *bindMountAppDataDirs, jboolean *bindMountAppStorageDirs) { }
EXPORT __attribute__((visibility("default"))) int specializeAppProcessPost( JNIEnv *env, jclass clazz) { return 0; }
EXPORT void nativeForkSystemServerPre( JNIEnv *env, jclass clazz, uid_t *uid, gid_t *gid, jintArray *gids, jint *runtimeFlags, jobjectArray *rlimits, jlong *permittedCapabilities, jlong *effectiveCapabilities) {
}
EXPORT int nativeForkSystemServerPost(JNIEnv *env, jclass clazz, jint res) { if (res == 0) { } else { } return 0; }
EXPORT int shouldSkipUid(int uid) { return false; }
EXPORT void onModuleLoaded() { } }
|