針對應用隱藏su權限

#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif

#include <sched.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <fcntl.h>
#include <vector>
#include <string>

using namespace std;

#define str_contains(s, ss) ((ss) != NULL && (s).find(ss) != std::string::npos)

void file_readline(const char *file, vector<string>* val, bool trim = true) {
    FILE *fp = fopen(file, "re");
    if (fp == NULL)
        return;
    size_t len = 1024;
    char *buf = (char *) malloc(len);
    char *start;
    ssize_t read;
    while ((read = getline(&buf, &len, fp)) >= 0) {
        start = buf;
        if (trim) {
            while (read && (buf[read - 1] == '\n' || buf[read - 1] == ' '))
            --read;
            buf[read] = '\0';
            while (*start == ' ')
            ++start;
        }
        string newStr = start;
        
        if (str_contains(newStr, "tmpfs /system/") || str_contains(newStr, "tmpfs /vendor/") ||
            str_contains(newStr, "tmpfs /sbin")) {
            printf("mount : %s.\n", start);
            
            strtok_r(NULL, " ", &start);
            val->push_back(strtok_r(NULL, " ", &start));
        }
    }
    fclose(fp);
    free(buf);
}

int main(int argc,char *argv[]) {
    int pid;
    sscanf(argv[1], "%d", &pid);
    
    kill(pid, SIGSTOP);
    
    char path[128];
    char cmdline[1024];
   
    sprintf(path, "/proc/%d/cmdline", pid);
    
    FILE *f = fopen(path, "re");
    fgets(cmdline, sizeof(cmdline), f);
    fclose(f);
    
    sprintf(path, "/proc/%d", pid);
    struct stat st;
    lstat(path, &st);
    
    printf("uid:%d, ino:%llu, dev:%llu.\n", st.st_uid, st.st_ino, st.st_dev);
    
    char mnt[32];
    snprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid);
    
    if (access(mnt, R_OK) == -1) {
        printf("%s not access.", mnt);
        return 1;
    }
    
    int fd, ret;
    
    fd = open(mnt, O_RDONLY);
    if (fd < 0) {
        printf("%s not open.\n", mnt);
        return 1;
    }
    
    ret = setns(fd, 0);
    printf("setns:%d.\n", ret);
    
    close(fd);
    
    //manage selinux
    char val;
    fd = open("/sys/fs/selinux/enforce", O_RDONLY);
    read(fd, &val, sizeof(val));
    
    close(fd);
    
    if (val == '0') {
        chmod("/sys/fs/selinux/enforce", 0640);
        chmod("/sys/fs/selinux/policy", 0440);
    }
    
    printf("manage selinux \n");
    //start unmount
    vector<string> targets;
    
    sprintf(path, "/proc/%d/mounts", pid);
    
    file_readline(path, &targets);
    
    for (auto &mountpoint : targets){
        if (umount2(mountpoint.data(), MNT_DETACH) != -1)
            printf("hide_daemon: Unmounted (%s)\n", mountpoint.data());
    }
    targets.clear();
    
    kill(pid, SIGCONT);
    
    printf("SIGCONT \n");
    
    return 0;
}

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 9月14日任務2.14 文件和目錄權限chmod2.15 更改所有者和所屬組chown2.16 umask2.17...
    awsome365閱讀 1,524評論 0 1
  • 1. 文件默認權限: umask 2. 文件隱藏屬性: chattr, lsattr chattr只能在Ext2/...
    epiiplus1equal0閱讀 621評論 0 0
  • 我的家鄉(xiāng)在四川省巴縣木洞鎮(zhèn)五層坎,據(jù)悉現(xiàn)在已經(jīng)屬于重慶市巴南區(qū)了。雖然木洞鎮(zhèn)沒有上海附近的烏鎮(zhèn)呀,周莊呀等有...
    田官吉閱讀 682評論 1 1
  • 國慶的第一天是我爸的生日,我們計劃好在那天早早的起來開車回鄉(xiāng)下給爸祝壽,以躲開路上塞車,誰知在國慶的前一天,先生說...
    阿紋_f348閱讀 191評論 0 0
  • 不知道這個點的你是否依舊在夢鄉(xiāng)就像不知道這個點的我是否還是一個正常人 我不知道這一夜我在干什么 沒有入睡也沒有思考...
    會飛的羊Felix閱讀 142評論 0 0

友情鏈接更多精彩內(nèi)容