1

我正在尝试以系统用户身份执行以下命令

su - system -c "ls -l /"

这是输出

shell@android:/data/xxxx/xxxx # su - system -c "ls -l /"
su - system -c "ls -l /"
SuperSU - Copyright (C) 2012 - Chainfire

这是否意味着在 Android 中我不能使用 shell 作为另一个用户执行命令?还是su二进制问题?

如何让 shell 作为另一个用户执行?我也尝试为此创建一个 shell 脚本,但输出仍然相同。请指教。

设备已经植根。

4

1 回答 1

0

问题是 SuperSU 包含一个su与标准 GNU 不兼容的二进制文件su

如果您su使用 NDK 为 android 编译,并替换二进制文件就可以了。

您可以使用以下代码:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
int main (int ac, char **av, char **env) {
    int iret;

    if (ac == 1) { // Run /bin/bash as root
        iret = setuid(0);
        if (iret == -1) {
            fprintf(stderr, "This binary is u-s, use chmod u+s %s as root.\n", av[0]);
            exit(-1);
        }

        strcpy(av[0], "/bin/bash");
        iret = execve(av[0], av, env);
        if (iret == -1) {
            fprintf(stderr, "Can't execute a shell.\n");
            exit(-1);
        }
    } else {
        struct passwd *pswd = getpwnam(av[1]); 
        //printf("%d\n", pswd->pw_uid);
        if (pswd == NULL) {
            fprintf(stderr, "User %s does not exist.\n", av[1]);
            exit(-1);
        }
        iret = setuid(pswd->pw_uid);
        if (iret == -1) {
            fprintf(stderr, "This binary is u-s, use chmod u+s %s as root.\n", av[0]);
            exit(-1);
        }
        iret = execve(av[2], (char **)&av[2], env);
        if (iret == -1) {
            fprintf(stderr, "%s\n", strerror(errno));
            exit(-1);
        }
    }
    return 0;
}

编译它并将所有者更改为root。然后以 root 身份运行 chmod u+s ./su。最后作为普通用户运行您的命令:

$ ./su 系统 /bin/bash -c "ls -l /"

于 2016-02-11T18:50:58.620 回答