Android应用程序如何获取root权限

在有些应用中,我们需要获取root权限,比如删除系统自带的应用程序等,下面介绍一般应用程序如何获取root:

1. root手机

    应用程序能获取root权限的前提是手机已经被root,一般手机厂商在出厂时,都会将su命令去掉,防止一般应用获取root权限,所以需要root手机。一般有两种root手机的方法:一种是手机厂商自己提供root工具,另一种是利用手机漏洞将su和superuser.apk push到手机中。superuser用于管理获取root权限的应用程序,所以其实,root手机就是将su命令放入手机/system/xbin目录以及安装superuser.apk。

2. 应用程序获取root权限

    应用程序通过执行su命令切换到root用户获取root权限,然后执行命令,如下所示:

[java] view plaincopy
  1.             Process process = null;  
  2.             DataOutputStream os = null;  
  3.             String cmd = "touch /system/app/phonekey.kk";  
  4.   
  5.             try {  
  6.                 process = Runtime.getRuntime().exec("su");  
  7.                 os = new DataOutputStream(process.getOutputStream());  
  8.                 DataInputStream error = new DataInputStream(process.getErrorStream());  
  9.                 DataInputStream input = new DataInputStream(process.getInputStream());  
  10.                 os.writeBytes(cmd + "\n");  
  11.                 os.flush();  
  12.                 os.writeBytes("exit\n");  
  13.                 os.flush();  
  14.                 process.waitFor();  
  15.             } catch (Exception e) {  
  16.                 return false;  
  17.             } finally {  
  18.                 try {  
  19.                     if (os != null) {  
  20.                         os.close();  
  21.                     }  
  22.                     process.destroy();  
  23.                 } catch (Exception e) {  
  24.                 }  
  25.             }  

3. root过程中可能遇到的问题

    最常见的一个错误就是 "su: uid xxxxx not allowed to su"。出现这个错误的原因是因为su限制了获取root的用户,看su的源代码如下(system/exras/su/su.c):

  1. /* 
  2.  * SU can be given a specific command to exec. UID _must_ be 
  3.  * specified for this (ie argc => 3). 
  4.  * 
  5.  * Usage: 
  6.  * su 1000 
  7.  * su 1000 ls -l 
  8.  */  
  9. int main(int argc, char **argv)  
  10. {  
  11.     struct passwd *pw;  
  12.     int uid, gid, myuid;  
  13.   
  14.     /* Until we have something better, only root and the shell can use su. */  
  15.     myuid = getuid();  
  16.     if (myuid != AID_ROOT && myuid != AID_SHELL) {  
  17.         fprintf(stderr,"su: uid %d not allowed to su-heihei\n", myuid);  
  18.         return 1;  
  19.     }  
  20.   
  21.     if(argc < 2) {  
  22.         uid = gid = 0;  
  23.     } else {  
  24.         pw = getpwnam(argv[1]);  
  25.   
  26.         if(pw == 0) {  
  27.             uid = gid = atoi(argv[1]);  
  28.         } else {  
  29.             uid = pw->pw_uid;  
  30.             gid = pw->pw_gid;  
  31.         }  
  32.     }  
  33.   
  34.     if(setgid(gid) || setuid(uid)) {  
  35.         fprintf(stderr,"su: permission denied\n");  
  36.         return 1;  
  37.     }  
  38.   
  39.     /* User specified command for exec. */  
  40.     if (argc == 3 ) {  
  41.         if (execlp(argv[2], argv[2], NULL) < 0) {  
  42.             fprintf(stderr, "su: exec failed for %s Error:%s\n", argv[2],  
  43.                     strerror(errno));  
  44.             return -errno;  
  45.         }  
  46.     } else if (argc > 3) {  
  47.         /* Copy the rest of the args from main. */  
  48.         char *exec_args[argc - 1];  
  49.         memset(exec_args, 0, sizeof(exec_args));  
  50.         memcpy(exec_args, &argv[2], sizeof(exec_args));  
  51.         if (execvp(argv[2], exec_args) < 0) {  
  52.             fprintf(stderr, "su: exec failed for %s Error:%s\n", argv[2],  
  53.                     strerror(errno));  
  54.             return -errno;  
  55.         }  
  56.     }  
  57.   
  58.     /* Default exec shell. */  
  59.     execlp("/system/bin/sh""sh", NULL);  
  60.   
  61.     fprintf(stderr, "su: exec failed\n");  
  62.     return 1;  
  63. }  

观察源代码16-17行发现 默认只有AID_ROOT和 AID_SHELL用户支持su命令,所以我们可以将18行的“return 1”这句话注释掉,即可。

原文地址:点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值