search 登录 注册
arrow_back返回列表
ID:10010
light_modedark_modedark_modestarstar
Lv.1 韶华一笑间
edit_note帖子 247
stars积分 2,130
event加入 2011-02-06
安卓交流

Android 获取ROOT权限原理解析

schedule发表于 2013-04-03 08:30:00 visibility查看 4,965 chat_bubble回复 76
#1 楼主
android 获取root权限原理解析一、 概述本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的“越狱”有一个更深层次的认识。二、 root 的介绍1. root 的目的可以让我们拥有掌控手机系统的权限,比如删除一些system/app下面的无用软件,更换开关机铃声和动画,拦截状态栏弹出的广告等。2. root的原理介绍谷歌的android系统管理员用户就叫做root,该帐户拥有整个系统至高无上的权利,它可以访问和修改你手机几乎所有的文件,只有root才具备最高级别的管理权限。我们root手机的过程也就是获得手机最高使用权限的过程。同时为了防止不良软件也取得root用户的权限,当我们在root的过程中,还会给系统装一个程序,用来作为运行提示,由用户来决定,是否给予最高权限。这个程序的名字叫做superuser.apk。当某些程序执行su指令想取得系统最高权限的时候,superuser就会自动启动,拦截该动作并作出询问,当用户认为该程序可以安全使用的时候,那么我们就选择允许,否则,可以禁止该程序继续取得最高权限。root的过程其实就是把su文件放到/system/bin/ superuser.apk 放到system/app下面,还需要设置/system/bin/su可以让任意用户可运行,有set uid和set gid的权限。即要在android机器上运行命令:adb shell chmod 4755 /system/bin/su。而通常,厂商是不会允许我们随便这么去做的,我们就需要利用操作系统的各种漏洞,来完成这个过程。特别说明:我们烧机中的eng版本并没有root权限3. root的方法从root的原理我们了解到,root 过程分三步:a. adb push su /system/binb. adb push superuser.apk /system/appc. adb shell chmod 4755 /system/bin/su若系统是eng版的,做到以上三步,那么我们root就大功告成,但实际是不行的。为什么呢?原因有三:1、user版的/system路径是只读权限,不能简单写入2、 chmod需要root权才能运行(死循环了)3、有些系统在启动时会自动将su的4755权限设成755,甚至直接删除su那么针对这种情况,我们怎么办呢?非常简单:烧一个eng版本的boot.img就行了可以用展讯的烧录工具,或者用fastboot模式从sd卡烧一个boot.img文件即可至此,我们root就成功了,可以用r.e(root explorer)在根目录创建和删除文件。三、 深入理解root机制其流程是:1. su 被用户调用2. su 创建了一个socket监听3. su 向superuser发送了一个广播,说是有一个程序要请求root4. su 等待socket 数据接收。有超时处理。5. superuser 界面收到广播后,弹出一个对话框,询问用户6. superuser 向传来的数据中的socket写回用户应答结果。7. su 根据socket得到的结果处理应该不应该继续执行8. 完成提权管理superuser.apk这个程序是root成功后,专门用来管理root权限使用的,防止被恶意程序滥用。源码地址: http://superuser.googlecode.com/svn/trunk我们有两点疑问:1. superuser是怎么知道谁想用root权限? 2. superuser是如何把用户的选择告诉su程序的?即superuser和su程序是如何通讯的,他们俩位于不通的时空,一个在java虚拟中,一个在linux的真实进程中。superuser共有两个activity: superuseractivity 和 superuserrequestactivity ,其中superuseractivity 主要是用来管理白名单的,就是记住哪个程序已经被允许使用root权限了,省的每次用时都问用户。superuserrequestactivity 就是用来询问用户目前有个程序想使用root权限,是否允许,是否一直允许,即放入白名单。这个白名单比较关键,是一个sqlite数据库文件,位置:/data/data/com.koushikdutta.superuser/databases/superuser.sqlite上文说过,root的本质就是往 /system/bin/ 下放一个su文件,不检查调用者权限的su文件。普通程序可以调用该su来运行root权限的命令。superuser.apk中就自带了一个这样的su程序。一开始superuser会检测/system/bin/su是否存在:file su = new file(“/system/bin/su“);// 检测su文件是否存在,如果不存在则直接返回if (!su.exists()) {toast toast = toast.maketext(this, “unable to find /system/bin/su.“, toast.length_long);toast.show();return;}//如果大小一样,则认为su文件正确,直接返回了事。if (su.length() == sustream.available()) { sustream.close(); return; //}// 如果检测到/system/bin/su 文件存在,但是不对头,则把自带的su先写到“/data/data/com.koushikdutta.superuser/su“//再写到/system/bin/su。byte[] bytes = new byte[sustream.available()];datainputstream dis = new datainputstream(sustream);dis.readfully(bytes);fileoutputstream suoutstream = new fileoutputstream(“/data/data/com.koushikdutta.superuser/su“); suoutstream.write(bytes); suoutstream.close(); process process = runtime.getruntime().*(“su“); dataoutputstream os = new dataoutputstream(process.getoutputstream()); os.writebytes(“mount -oremount,rw /dev/block/mtdblock3 /system\n“); os.writebytes(“busybox cp /data/data/com.koushikdutta.superuser/su /system/bin/su\n“); os.writebytes(“busybox chown 0:0 /system/bin/su\n“); os.writebytes(“chmod 4755 /system/bin/su\n“); os.writebytes(“exit\n“); os.flush();有进程使用root权限,superuser是怎么知道的呢,关键是句: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); if (system(syscmd)) return *utionfailure(“am.“); 原理是am命令,am的用法: usage: am [sub*] [options] start an activity: am start [-d] [-w] <intent> -d: enable debugging -w: wait for launch to complete start a service: am startservice <intent> send a broadcast intent: am broadcast <intent> start an instrumentation: am instrument [flags] <component> -r: print raw results (otherwise decode report_key_streamresult) -e <name> <value>: set argument <name> to <value> -p <file>: write profiling data to <file> -w: wait for instrumentation to finish before returning start profiling: am profile <process> start <file> stop profiling: am profile <process> stop <intent> specifications include these flags: [-a <action>] [-d ] [-t <mime_type>] [-c <category> [-c <category>] ...] [-e|--es <extra_key> <extra_string_value> ...] [--esn <extra_key> ...] [--ez <extra_key> <extra_boolean_value> ...] [-e|--ei <extra_key> <extra_int_value> ...] [-n <component>] [-f <flags>] [--grant-read-uri-permission] [--grant-write-uri-permission] [--debug-log-resolution] [--activity-brought-to-front] [--activity-clear-top] [--activity-clear-when-task-reset] [--activity-exclude-from-recents] [--activity-launched-from-history] [--activity-multiple-task] [--activity-no-animation] [--activity-no-history] [--activity-no-user-action] [--activity-previous-is-top] [--activity-reorder-to-front] [--activity-reset-task-if-needed] [--activity-single-top] [--receiver-registered-only] [--receiver-replace-pending] [<uri>]还有个疑点,就是su怎么知道用户是允许root权限还是反对呢?原来是上面提到的白名单起来作用,superuser把用户的选择放入:/data/data/com.koushikdutta.superuser/databases/superuser.sqlite 数据库中,然后su进程再去读该数据库来判断是否允许。static int checkwhitelist(){ sqlite3 *db; int rc = sqlite3_open_v2(dbpath, &db, sqlite_open_readwrite, null); if (!rc) { char *errormessage; char query[1024]; sprintf(query, “* * from whitelist where _id=%d limit 1;“, g_puid); struct whitelistcall* call*; call*.count = 0; call*.db = db; rc = sqlite3_*(db, query, whitelistcallback, &call*, &errormessage); if (rc != sqlite_ok) { sqlite3_close(db); return 0; } return call*.count; } sqlite3_close(db); return 0;}四、 资源文件的获取从上文的源码地址获取源代码,替换系统的system/extras/su/下面的su.c 和android.mk文件,使用编译命令./mk td28 u adr system/extras/su/ 编译成功后会生成out/target/product/hsdroid/system/xbin/su 文件,而superuser.apk就是普通的apk文件,都在源码地址里面可以下载,下载后倒入到eclipse即可直接运行。

全部回复 (76)

ID:16513
light_modedark_mode
Lv.1 韶华一笑间
2013-04-07 19:40:00 #22
真的好难啊
ID:71675
dark_modedark_modestarstar
Lv.1 韶华一笑间
2013-04-07 20:29:00 #23
你牛,我顶!
ID:10105
light_modestarstarstar
Lv.6 胸怀六国志
2013-04-08 00:48:00 #24
你牛,我顶!
ID:103235
dark_modedark_modestarstar
Lv.1 韶华一笑间
2013-04-08 02:06:00 #25
你牛,我顶!
ID:131779
dark_modedark_modedark_modestarstar
Lv.1 韶华一笑间
2013-04-08 08:04:00 #26
你牛,我顶!
ID:81682
light_mode
Lv.1 韶华一笑间
2013-04-08 08:19:00 #27
这帖不错,该顶!
ID:59936
dark_modedark_modedark_modestarstar
Lv.1 韶华一笑间
2013-04-08 08:41:00 #28
这帖不错,该顶!
ID:128592
dark_modedark_modedark_modestar
Lv.1 韶华一笑间
2013-04-08 09:27:00 #29
安卓能手组!欢迎你的加入
ID:66267
light_modestarstarstar
Lv.1 韶华一笑间
2013-04-08 09:36:00 #30
安卓我最行!
ID:125768
dark_modedark_modedark_mode
Lv.1 韶华一笑间
2013-04-08 09:37:00 #31
安卓我最行!
ID:44839
light_modestarstarstar
Lv.1 韶华一笑间
2013-04-08 10:00:00 #32
祝安卓论坛越来越好!
ID:131779
dark_modedark_modedark_modestarstar
Lv.1 韶华一笑间
2013-04-08 10:55:00 #33
看帖回帖是美德!
ID:12642
light_modestarstar
Lv.1 韶华一笑间
2013-04-08 15:21:00 #34
安卓能手组!欢迎你的加入
ID:118229
dark_modedark_modedark_mode
Lv.1 韶华一笑间
2013-04-08 21:14:00 #35
你牛,我顶!
ID:74542
light_modestarstarstar
Lv.1 韶华一笑间
2013-04-09 03:08:00 #36
安卓能手组!欢迎你的加入
ID:126499
dark_modedark_modedark_modestar
Lv.1 韶华一笑间
2013-04-09 11:47:00 #37
看帖回帖是美德!查看版块
ID:126499
dark_modedark_modedark_modestar
Lv.1 韶华一笑间
2013-04-09 11:47:00 #38
安卓我最行!
ID:38772
light_modestarstar
Lv.2 独赏二月雪
2013-04-09 14:25:00 #39
这帖不错,该顶!
ID:1084
light_modedark_modedark_modestarstarstar
Lv.2 独赏二月雪
2013-04-11 13:43:00 #40
这帖不错,该顶!虽不明,但觉厉
ID:134979
dark_modedark_modestar
Lv.1 韶华一笑间
2013-04-11 14:01:00 #41
感同53楼
登录 后才能回复