该函数的作用是检测当前进程的 SO 库路径是否被重定向,并返回重定向次数。它通过获取当前进程的 pid 和名称,打开 /proc/pid/maps 文件,逐行读取其中的数据,并通过匹配特定的 SO 库路径来判断是否被重定向。如果被重定向,则返回重定向次数。

int detectRidectedPath() {
    int pid = getpid();
    char *process = getProcessName();
    if (process == NULL) {
        return 0;
    }
    size_t p_len = strlen(process);
    int i = 0;
    char data[256];
    char s[64] = {0};

    sprintf(s, "/proc/%d/maps", pid);
    FILE *fd = fopen(s, "r");
    if (fd == NULL) {
        return 0;
    }

    char path[128] = {0};
    char uid[10] = {0};

    while (fgets(data, 256, fd)) {
        int len = (int)strlen(data);
        if (len <= 0) {
            continue;
        }
        data[--len] = '\0';
        if (sscanf(data, "%*llx-%*llx %s %*s %*s %*s %s", uid, path) != 2) {
            continue;
        }
        // Anti_taiji
        if(strstr(path,"libexp824.so") !=NULL || strstr(path,"me.weishu.exp") !=NULL) {
            i++;
        }
        if((strcmp(uid, "r-xp") == 0 && endsWith(path, "libegis.so")) ||
           (strcmp(uid, "r--p") == 0 && endsWith(path, "libegis.so"))) {
            const char *lib = path;
            if (strstr(lib, process) != NULL) {
                //If app is running in VA, the path will be re-directed
                if (startsWith(lib, SO_APP_LIB)) {
                    if (strncmp(lib + SO_APP_LIB_LEN, process, p_len)) {
                        /*
                        On Android 11 Phones, the so path in 'maps' would be like this,
                        "data/app/~~VnOGMt_4AOV97A55kUYglg==/com.nd.shihua-37XqLaoLK_7gFgthT0j-3w==/lib/arm64/libegis.so"
                        So, that's why we will add 27 to find the real package name
                        */
                        if (strncmp(lib + 27 + SO_APP_LIB_LEN, process, p_len)){
                            i++;
                        }
                    }
                } else if (startsWith(lib, SO_DATA_APP)) {
                    if (strncmp(lib + SO_DATA_APP_LEN, process, p_len)) {
                        if (strncmp(lib + 27 + SO_DATA_APP_LEN, process, p_len)){
                            i++;
                        }
                    }
                } else if (startsWith(lib, SO_DATA_DATA)) {
                    if (strncmp(lib + SO_DATA_DATA_LEN, process, p_len)) {
                        if (strncmp(lib + 27 + SO_DATA_DATA_LEN, process, p_len)){
                            i++;
                        }
                    }
                }
            }
        }
    }
    fclose(fd);
    free(process);
    return i;
}

该函数使用以下步骤检测 SO 库路径是否被重定向:

  1. 获取当前进程的 pid 和名称。
  2. 打开 /proc/pid/maps 文件,该文件包含了进程内存映射的信息。
  3. 逐行读取文件内容,并解析每行数据。
  4. 匹配特定的 SO 库路径,例如 libegis.so
  5. 判断 SO 库路径是否被重定向。如果路径被重定向,则计数器 i 加 1。
  6. 返回重定向次数 i

该函数可以用于检测应用程序的 SO 库路径是否被恶意软件或其他程序修改,从而保护应用程序的安全。

C语言函数 detectRidectedPath():检测 SO 库路径重定向

原文地址: https://www.cveoy.top/t/topic/oZnb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录