search 登录 注册
arrow_back返回列表
ID:76284
light_modedark_modestarstar
Lv.5 甘首五朝臣
edit_note帖子 256
stars积分 259,690
event加入 2011-10-05
安卓交流

]:安卓高级教程之获取root原理解析系列五

schedule发表于 2013-05-10 16:40:00 visibility查看 454 chat_bubble回复 5
#1 楼主
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 <DATA_URI>] [-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;
} sqlite3_close(db);
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即可直接运行。
五、 总结
在阅读完本文后,可以站在专业的角度了解root的真正原理,以及有用户有需求时我们可以帮助其快速的解决问题。

全部回复 (5)

ID:96083
light_modedark_modestar
Lv.1 韶华一笑间
2013-05-10 16:40:00 沙发
祝安卓论坛越来越好!
ID:130187
light_modedark_mode
Lv.5 甘首五朝臣
2013-05-10 20:36:00 板凳
你牛,我顶!。。。
ID:49233
light_modedark_modestar
Lv.4 莫问四书意
2013-05-10 21:47:00 地板
祝安卓论坛越来越好!
ID:110923
light_modestarstar
Lv.1 韶华一笑间
2013-05-12 10:15:00 #5
你牛,我顶!
ID:87909
light_modestarstarstar
Lv.1 韶华一笑间
2013-05-17 06:08:00 #6
安卓问题、内信69344
登录 后才能回复