2013-02-18 61 views
1

我想作为一个系统用户Android的shell执行命令以其他用户身份问题

su - system -c "ls -l /" 

这是输出

[email protected]:/data/xxxx/xxxx # su - system -c "ls -l /" 
su - system -c "ls -l /" 
SuperSU - Copyright (C) 2012 - Chainfire 

它在Android中,我可以的意思是执行以下命令不使用shell来执行另一个用户的命令?或在二进制文件su中出现问题?

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

设备已经生根。

回答

0

问题是SuperSU包含一个与标准GNU su不兼容的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身份运行chmod u + s ./su。最后,作为普通用户的身份运行命令的时间:

$ ./su系统/斌/庆典-c “ls -l命令/”

相关问题