Android 手机Root 原理解析

来源:互联网 发布:音效 知乎 编辑:程序博客网 时间:2024/04/30 10:51

Root 手机的原理 ?

  Root 是Linux 等类Unix 系统的超级管理员用户账户,手机Root 也就是系统破解(在 iOS 设备中叫做“越狱”),当手机被Root ,其他用户就可以以超级管理员的身份运行程序。

  Root 的原理是 修改系统的/system/bin/su 文件

su 源代码 下载地址:http://download.csdn.net/detail/jinzhu117/4821630

SuperUser 是什么?

SuperUser 是用来管理用户Root 权限的软件。当手机被Root 之后,相当于所有的用户都有了以Root 用户的身份运行只能Root 用户才能执行的程序和命令,用户可以任意的修改系统密码,删除系统重要文件等等,系统变得非常不安全,SuperUser 就是用来管理其他用户是否有权限使用超级管理员身份的运行应用程序的系统软件。当有用户(程序)想以root 权限 或者以root 权限执行命令及程序时,su.c 会先启动SuperUser ,询问用户是否给予该程序(用户)Root 权限,如果给则将该进程设置为root 

安装SuperUser 后,会替换掉 /system/bin/su 文件,

SuperUser 源代码下载地址:http://download.csdn.net/detail/jinzhu117/4821569

一个软件获取Root 权限之后,能给其他应用程序 Root 权限吗?

1.把su.c 替换成自己修改后的su.c

2.把共享应用程序的UserId.

Root 手机?

其实就是修改系统的/system/bin/su 文件

应用获得Root 权限

Root 之前,只有Root 用户才能以超级管理员的权限运行应用程序,看su.c 的代码:

[cpp] view plaincopyprint?
  1. if (myuid != AID_ROOT && myuid != AID_SHELL) {  
  2.         fprintf(stderr,"su: uid %d not allowed to su\n", myuid);  
  3.         return 1;  
  4.     }  
  5.       
  6. if(setgid(gid) || setuid(uid)) {  
  7.         fprintf(stderr,"su: permission denied\n");  
  8.         return 1;  
  9.     }  

[cpp] view plaincopyprint?
  1. NAME  
  2.   
  3. setuid - set user ID  
  4. SYNOPSIS  
  5.   
  6. #include <unistd.h>  
  7.   
  8. int setuid(uid_t uid);  
  9.   
  10. DESCRIPTION  
  11.   
  12. If the process has appropriate privileges, setuid() shall set the real user ID, effective user ID, and the saved set-user-ID of the calling process to uid.  
  13.   
  14. If the process does not have appropriate privileges, but uid is equal to the real user ID or the saved set-user-ID, setuid() shall set the effective user ID to uid; the real user ID and saved set-user-ID shall remain unchanged.  
  15.   
  16. The setuid() function shall not affect the supplementary group list in any way.  
  17.   
  18. RETURN VALUE  
  19.   
  20. Upon successful completion, 0 shall be returned. Otherwise, -1 shall be returned and errno set to indicate the error.  
  21.   
  22. ERRORS  
  23.   
  24. The setuid() function shall fail, return -1, and set errno to the corresponding value if one or more of the following are true:  
  25.   
  26. [EINVAL]  
  27. The value of the uid argument is invalid and not supported by the implementation.  
  28. [EPERM]  
  29. The process does not have appropriate privileges and uid does not match the real user ID or the saved set-user-ID.  


Root 之后,使用SuperUser 管理系统的Root 权限,SuperUser 有一个白名单,白名单中都是允许以使用超级管理员的用户。

[cpp] view plaincopyprint?
  1. if (!checkWhitelist())  
  2.     {  
  3.         char sysCmd[1024];  
  4.         sprintf(sysCmd, "am start -a android.intent.action.MAIN -n com.koushikdutta.superuser/com.koushikdutta.superuser.SuperuserRequestActivity --ei uid %d --ei pid %d > /dev/null", g_puid, ppid);  
  5.         if (system(sysCmd))  
  6.             return executionFailure("am.");  
  7.   
  8.         int found = 0;  
  9.         int i;  
  10.         for (i = 0; i < 10; i++)  
  11.         {  
  12.             sleep(1);  
  13.             // 0 means waiting for user input  
  14.             // > 0 means yes/always  
  15.             // < 0 means no  
  16.             int checkResult = checkWhitelist();  
  17.             if (checkResult > 0)  
  18.             {  
  19.                 found = 1;  
  20.                 break;  
  21.             }  
  22.             else if (checkResult < 0)  
  23.             {  
  24.                 // user hit no  
  25.                 return permissionDenied();  
  26.             }  
  27.         }  


Root 之后,自己写一个程序,替换系统的su.c 文件,当其他应用程序想以 Root 用户的身份运行程序时,都放行。


看完之后可以试着回答下面几个问题:

1,Root 是什么?

2,Android 手机Root 的原理?

3,什么是SuperUser ?

4,su.c 的作用?

5,setUid() 和setGid() 的左右?


su 源代码 下载地址:http://download.csdn.net/detail/jinzhu117/4821630

SuperUser 源代码下载地址:http://download.csdn.net/detail/jinzhu117/4821569

转自http://blog.csdn.net/jinzhu117/article/details/8233255


0 0
原创粉丝点击