1. 程式人生 > >Android su開放root許可權

Android su開放root許可權

/*
**
** Copyright 2008, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/
#define LOG_TAG "su"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <pwd.h>
#include <private/android_filesystem_config.h>
void pwtoid(const char *tok, uid_t *uid, gid_t *gid)
{
    struct passwd *pw;
    pw = getpwnam(tok);
    if (pw) {
        if (uid) *uid = pw->pw_uid;
        if (gid) *gid = pw->pw_gid;
    } else {
        uid_t tmpid = atoi(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);
}
/*
 * SU can be given a specific command to exec. UID _must_ be
 * specified for this (ie argc => 3).
 *
 * Usage:
 *   su 1000
 *   su 1000 ls -l
 *  or
 *   su [uid[,gid[,group1]...] [cmd]]
 *  E.g.
 *  su 1000,shell,net_bw_acct,net_bw_stats id
 * will return
 *  uid=1000(system) gid=2000(shell) groups=3006(net_bw_stats),3007(net_bw_acct)
 */
int main(int argc, char **argv)
{
    struct passwd *pw;
    uid_t uid, myuid;
    gid_t gid, gids[10];
    /* Until we have something better, only root and the shell can use su. */
    myuid = getuid();
    if (myuid != AID_ROOT && myuid != AID_SHELL) {
        fprintf(stderr,"su: uid %d not allowed to su\n", myuid);
        return 1;
    }
    if(argc < 2) {
        uid = gid = 0;
    } else {
        int gids_count = sizeof(gids)/sizeof(gids[0]);
        extract_uidgids(argv[1], &uid, &gid, gids, &gids_count);
        if(gids_count) {
            if(setgroups(gids_count, gids)) {
                fprintf(stderr, "su: failed to set groups\n");
                return 1;
            }
        }
    }
    if(setgid(gid) || setuid(uid)) {
        fprintf(stderr,"su: permission denied\n");
        return 1;
    }
    /* User specified command for exec. */
    if (argc == 3 ) {
        if (execlp(argv[2], argv[2], NULL) < 0) {
            int saved_errno = errno;
            fprintf(stderr, "su: exec failed for %s Error:%s\n", argv[2],
                    strerror(errno));
            return -saved_errno;
        }
    } else if (argc > 3) {
        /* Copy the rest of the args from main. */
        char *exec_args[argc - 1];
        memset(exec_args, 0, sizeof(exec_args));
        memcpy(exec_args, &argv[2], sizeof(exec_args));
        if (execvp(argv[2], exec_args) < 0) {
            int saved_errno = errno;
            fprintf(stderr, "su: exec failed for %s Error:%s\n", argv[2],
                    strerror(errno));
            return -saved_errno;
        }
    }
    /* Default exec shell. */
    execlp("/system/bin/sh", "sh", NULL);
    fprintf(stderr, "su: exec failed\n");
    return 1;
}

相關推薦

Android su開放root許可權

/* ** ** Copyright 2008, The Android Open Source Project ** ** Licensed under the Apache License, Version 2.0 (the "License");  ** you may not use this fil

Android應用向su申請root許可權,以及Superuser進行授權管理的原理淺析

最近研究了好幾天su+Superuser的原始碼,感覺大概梳理通了整個大體的思路框架,mark一下。  一.su和Suepruser進行root授權的處理流程 對於su命令列程式在對來自Android應用的Root許可權請求處理流程大致如下圖所示(因為快要找工作了,為了節約

android emulator 獲取 Root 許可權

參考:https://blog.csdn.net/luvsnow/article/details/79963025  在2018.4的博文,搜尋時排在前面,我嘗試了一下。 浪費將近1小時,下載,按步驟,等;些方法是如此之差。其實有更簡單的方法! 看我的文章:笨方法,原本簡單的方

rk3288 android5.1 java 層使用 su 獲取 root 許可權

 用法:1 Runtime.getRuntime().exec("su , tinymix"); 要連在一起寫,或者寫一指令碼 public void onClick(View v) {              Log.d("test by Wade", "su test

android apk獲取root許可權執行相應的操作 demo除錯

在apk中,有時候需要root許可權,例如通過apk更新系統庫等system的檔案等,避免升級韌體,或者在apk中需要直接訪問某些裝置等。下面是在apk中獲取root許可權的方法,前提是裝置已經root過了。 關鍵點在於下面這句,通過執行su產生一個具有roo

判斷手機是否ROOT 程式碼實現+ Android 作業系統 獲取Root許可權 原理解析

判斷手機是否具有ROOT限                                                    許多機友新購來的Android機器沒有破解過Root許可權,無法使用一些需要高許可權的軟體,以及進行一些高許可權的操作,其實破解手機Root

【30秒】android模擬器獲取ROOT許可權!!!安卓

秒殺所有ROOT方法,30秒就讓你的模擬器獲得ROOT許可權!!! 1、取得root許可權  adb shell mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system cd /system/bin cat

獲取Android系統的ROOT許可權

在 Android 開發中,有很多時候都需要獲得系統的 root 許可權,比如檢視某些應用的 SQLite Database 等等。這裡簡要地提供一些應用與思路。 PS:測試機用的是已經淘汰不用好久的小米2S,新手最好不要用好機子來root,萬一root壞了會很麻煩。

ubuntu su進入root許可權

轉載1: sudo指令 功能: 以root的身分執行命令 語法: sudo 其他指令 使用者: 被root加入『/etc/sudoers』檔案中的使用者 1.root的密碼除了root本人知道外,不需被其他需要用到root許可權使用者知道,因為使用sudo時,要

android studio 下 terminal adb中的root許可權問題 su not found

今天遇到問題,android studio 下的terminal沒有root許可權,無法檢視程序的fd,幾番搜尋, 看到了完整的解決方案,是部落格園的一篇文章:點選開啟連結,  需要下載su檔案,su

pc進入android手機shell的root許可權(su: not found)

1、將android的tools目錄加入到path中(我sdk\tools目錄沒有adb,但卻在sdk\platform-tools裡發現了adb.exe,索性兩個tools全加入到了path中) 2、adb shell 進入手機後,發現是 $ ,不是 # 號,而視訊上是

android 下,su chomd 666/777 root許可權程式碼方式申請

檔案/目錄許可權設定命令:chmod 這是Linux系統管理員最常用到的命令之一,它用於改變檔案或目錄的訪問許可權。該命令有兩種用法: 用包含字母和操作符表示式的文字設定法 其語法格式為:c

android studio模擬器怎樣獲得root許可權

前言: 菜菜最近和同學一起報了學校的機電大賽,我們做的專案是一個基於人臉識別的防盜門系統。專案中用到了安卓手機端開發,但是本人andriod真的學的不好,為了不拖團隊後腿,無耐只能看著視訊自學。其中就遇到了android studio模擬器的許可權問題。 問題描述如下: (1)file

how to mount /system as read/write in android? 在除錯RK3288的OV2718的驅動時,需要remount /system目錄為rw以push檔案到/system/lib/hw目錄下,常規的是用adb登入上去後以root許可權執行mount -o re

在除錯RK3288的OV2718的驅動時,需要remount  /system目錄為rw以push檔案到/system/lib/hw目錄下,常規的是用adb登入上去後以root許可權執行mount -o remount,rw /system即可,然而在拿的新板子後這麼做失效了,於是百

android 判斷app是否具有root許可權

    應用判斷是否具有root許可權,只需要看能否在data分割槽建立檔案,如果能夠在data分割槽建立檔案,那麼應用具有root許可權 public static boolean upgradeRootPermission( ) {  &nbs

Android利用root許可權開關機、休眠和喚醒

    在android的裝置中如果我們想重啟手機或者關機或,一般是需要在原始碼的環境下編譯apk,並賦予其相應地系統許可權,而如果想喚醒裝置則需要wakelack。原始碼編譯APP還是比較麻煩的,不過由於android的核心屬於linux,那麼在獲取root許可權的and

Android應用程式獲得root許可權

Android應用程式獲得root許可權 我在博文《Android程式的安全系統》中提到兩種讓root許可權的辦法。最近在網上發現很多朋友轉載那篇文章,但是對那篇文章中提到的第一種方法怎樣實現,不是很明白。本文將會以一個例子實現來演示怎樣讓一個Android應用程式獲得roo

Android獲取Root許可權方法

1、把ADB解壓後,隨便放在任一磁碟下,最好把目錄名改短點,不然DOS下進比較麻煩。5 U6 n) i, D2 w6 t3 b( C4 B3 z) w 2、把SU檔案解壓,放到卡上,最好是根目錄下2 v1 ]; G6 p# G" u- L 3、把V5 連線上電腦 ,電腦會

Android系統system使用者許可權root許可權的獲取

在Android系統中,系統為每一個應用程式(apk)建立了一個使用者和組。這個使用者和組都是受限使用者,不能訪問系統的資料,只能訪問自己的檔案和目錄,當然它也不能訪問其他應用程式的資料。這樣設計可以儘可能地保護應用程式的私有資料,增強系統的安全性和健壯性。      

MTK Android user版本如何開啟root許可權

(4). 如何內建Google default su  4.1 放開Google default su 只准shell/root 使用者使用的限制.     system/extras/su/su.c 中刪除下面3行程式碼     if (myuid != AID_ROOT && myuid