layout: post
title: 開發你自己的su
categories: Android
description: 開發你自己的su
keywords: android
url: https://lichao890427.github.io/ https://github.com/lichao890427/
開發你自己的su
背景
??市面上常見的Android Root工具,原理就是利用系統漏洞將su文件下載到可執行目錄。這樣第三方進程可以執行su提升子進程的權限(記住Root提權不是提升當前進程權限,而是給子進程提權)。比如在app中讀取文件可以用命令,su -c cat /data/data/com.example/xxx
,當前app進程并未提權,而是cat子進程提權,所以讀取到了其他App-com.example的文件。
??上面介紹了Root原理,那么很多Root工具除了su,還會有對應的授權管理app存在,它們記錄一個白名單,決定哪個app擁有權限,一般有3種權限:允許/詢問/拒絕。詢問的時候,第三方app調用su后,su會和授權管理app通信,導致系統彈窗詢問用戶是否允許。這個在有時候是很討厭的,那么怎么去除這個彈窗呢,這就必須我們自己編譯一個su,并設置SUID位。SUID位確保su執行的子進程以Root權限運行
??這里介紹的是絕對可行的root方式,對于android模擬器,無論x86 x64 ,無論4.0 5.0 6.0都適用。真機的話5.0和6.0,root成功率比較低,因此不適合在上面做root方面的測試,這時只能考慮虛擬機,因為虛擬機adb有root權限,利用adb root權限便可以提升su權限。網上盛傳的方法是http://androidsu.com/superuser https://www.0xaa55.com/forum.php ... tid=1648&extra=
需要superuser.apk,我分析了該網站的su,發現除了傳統su的流程(setuid setgid)外,還加入了和superuser.apk通信的這一步。superuser.apk用于單個app權限設置。遺憾的是該作者并沒完成mips架構,和5.0以上的su適配
??默認的su,是只允許root權限和shell權限來執行的,因此一般的app并不能使用,因此這也是我們修改的重點,同時還要給su加上必要的環境變量,我這種方式,去除了權限判斷部分,因此減少了某些情況下由于這部分帶來的root權限未成功獲取的bug。這里提供一種方式,可以支持全平臺
代碼
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#define AID_ROOT 0 /* traditional unix root user */
#define AID_SYSTEM 1000 /* system server */
#define AID_SHELL 2000 /* adb and debug shell user */
#define AID_NOBODY 9999
#define AID_APP 10000 /* first app user */
#define AID_USER 100000 /* offset for uid ranges for each user */
void pwtoid(const char* tok, uid_t* uid, gid_t* gid) {
struct passwd* pw = getpwnam(tok);
if (pw)
{
if (uid) *uid = pw->pw_uid;
if (gid) *gid = pw->pw_gid;
}
else
{
char* end;
errno = 0;
uid_t tmpid = strtoul(tok, &end, 10);
if (errno != 0 || end == tok)
printf("invalid uid/gid '%s'", tok);
if (uid) *uid = tmpid;
if (gid) *gid = tmpid;
}
}
void extract_uidgids(const char* uidgids, uid_t* uid, gid_t* gid, gid_t* gids, int* gids_count) {
char *clobberablegids;
char *nexttok;
char *tok;
int gids_found;
if (!uidgids || !*uidgids)
{
*gid = *uid = 0;
*gids_count = 0;
return;
}
clobberablegids = strdup(uidgids);
strcpy(clobberablegids, uidgids);
nexttok = clobberablegids;
tok = strsep(&nexttok, ",");
pwtoid(tok, uid, gid);
tok = strsep(&nexttok, ",");
if (!tok)
{
/* gid is already set above */
*gids_count = 0;
free(clobberablegids);
return;
}
pwtoid(tok, NULL, gid);
gids_found = 0;
while ((gids_found < *gids_count) && (tok = strsep(&nexttok, ",")))
{
pwtoid(tok, NULL, gids);
gids_found++;
gids++;
}
if (nexttok && gids_found == *gids_count)
{
fprintf(stderr, "too many group ids\n");
}
*gids_count = gids_found;
free(clobberablegids);
}
int main(int argc, char** argv)
{
uid_t current_uid = getuid();
// Handle -h and --help.
++argv;
if (*argv && (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0))
{
fprintf(stderr,
"usage: su [UID[,GID[,GID2]...]] [COMMAND [ARG...]]\n"
"\n"
"Switch to WHO (default 'root') and run the given command (default sh).\n"
"\n"
"where WHO is a comma-separated list of user, group,\n"
"and supplementary groups in that order.\n"
"\n");
return 0;
}
uid_t uid = 0;
gid_t gid = 0;
if (*argv)
{
gid_t gids[10];
int gids_count = sizeof(gids)/sizeof(gids[0]);
extract_uidgids(*argv, &uid, &gid, gids, &gids_count);
if (gids_count) {
if (setgroups(gids_count, gids))
{
printf("setgroups failed");
}
}
++argv;
}
if (setgid(gid))
printf("setgid failed");
if (setuid(uid))
printf("setuid failed");
//設置環境變量
setenv("PATH", "/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin", 1);
setenv("LD_LIBRARY_PATH", "/vendor/lib:/system/lib", 1);
setenv("ANDROID_BOOTLOGO", "1", 1);
setenv("ANDROID_ROOT", "/system", 1);
setenv("ANDROID_DATA", "/data", 1);
setenv("ANDROID_ASSETS", "/system/app", 1);
setenv("EXTERNAL_STORAGE", "/sdcard", 1);
setenv("ASEC_MOUNTPOINT", "/mnt/asec", 1);
setenv("LOOP_MOUNTPOINT", "/mnt/obb", 1);
char* exec_args[argc + 1];
size_t i = 0;
for (; *argv != NULL; ++i)
{
exec_args[i] = *argv++;
}
if (i == 0) exec_args[i++] = "/system/bin/sh";
exec_args[i] = NULL;
execvp(exec_args[0], exec_args);
printf("failed to exec %s", exec_args[0]);
}
相關代碼我的github上也有