Android 源码修改,使第三方应用可以直接使用su命令

    在android原生系统中,只有root权限和shell权限下才可以使用su命令,虽然在userdebug模式下编译的系统镜像有自带的su文件,但是第三方应用却无法使用。于是在这种场景下,有两种方式可以实现第三方应用使用su命令。

    1.修改原来的su相关的源码(所有的应用都可以使用)

    2.通过supersu.apk 的方式进行实现(可以通过supersu进行控制应用是否可以使用su)

一、修改原来的su相关的源码

1.修改 system/extras/su/su.c ,屏蔽如下代码:

修改前

uid_t current_uid = getuid();   
if (current_uid != AID_ROOT && current_uid != AID_SHELL) error(1, 0, "not allowed");

修改后

// uid_t current_uid = getuid();   
// if (current_uid != AID_ROOT && current_uid != AID_SHELL) error(1, 0, "not allowed");

将su文件中的判断是否是root,或是shell的用户id判断进行注释。

2.修改 system/core/libcutils/fs_config.c, 做如下修改:

修改 fs_path_config android_files结构体中的

修改前

{ 04750, AID_ROOT,      AID_SHELL,     0, "system/xbin/su" },

修改后

{ 06755, AID_ROOT,      AID_SHELL,     0, "system/xbin/su" },

修改su文件的访问权限,将其他用户的权限修改为可读,可执行权限。

3.修改 frameworks/base/cmds/app_process/app_main.cpp,做如下的修改:

注释 main 函数中的以下代码

修改前

if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
        // Older kernels don't understand PR_SET_NO_NEW_PRIVS and return
        // EINVAL. Don't die on such kernels.
        if (errno != EINVAL) {
            LOG_ALWAYS_FATAL("PR_SET_NO_NEW_PRIVS failed: %s", strerror(errno));
            return 12;
        }
 }

修改后

/* if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
        // Older kernels don't understand PR_SET_NO_NEW_PRIVS and return
        // EINVAL. Don't die on such kernels.
        if (errno != EINVAL) {
            LOG_ALWAYS_FATAL("PR_SET_NO_NEW_PRIVS failed: %s", strerror(errno));
            return 12;
        }
    } */

4、修改 frameworks/base/core/jni/com_android_internal_os_Zygote.cpp,做如下的修改:

注释 DropCapabilitiesBoundingSet 中的如下代码

修改前

for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
    int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
    if (rc == -1) {
      if (errno == EINVAL) {
        ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
              "your kernel is compiled with file capabilities support");
      } else {
        RuntimeAbort(env, __LINE__, "prctl(PR_CAPBSET_DROP) failed");
      }
    }
  }

修改后

/* for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
    int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
    if (rc == -1) {
      if (errno == EINVAL) {
        ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
              "your kernel is compiled with file capabilities support");
      } else {
        RuntimeAbort(env, __LINE__, "prctl(PR_CAPBSET_DROP) failed");
      }
    }
  } */

注释后就是一个空方法了。

5.修改 system/core/adb/daemon/main.cpp,做如下的修改

将 should_drop_privileges函数 直接返回false

修改前

static bool should_drop_privileges() {
#if defined(ALLOW_ADBD_ROOT)
    char value[PROPERTY_VALUE_MAX];

    // The properties that affect `adb root` and `adb unroot` are ro.secure and
    // ro.debuggable. In this context the names don't make the expected behavior
    // particularly obvious.
    //
    // ro.debuggable:
    //   Allowed to become root, but not necessarily the default. Set to 1 on
    //   eng and userdebug builds.
    //
    // ro.secure:
    //   Drop privileges by default. Set to 1 on userdebug and user builds.
    property_get("ro.secure", value, "1");
    bool ro_secure = (strcmp(value, "1") == 0);

    property_get("ro.debuggable", value, "");
    bool ro_debuggable = (strcmp(value, "1") == 0);


修改后

static bool should_drop_privileges() {
    return false;
#if defined(ALLOW_ADBD_ROOT)
    char value[PROPERTY_VALUE_MAX];

    // The properties that affect `adb root` and `adb unroot` are ro.secure and
    // ro.debuggable. In this context the names don't make the expected behavior
    // particularly obvious.
    //
    // ro.debuggable:
    //   Allowed to become root, but not necessarily the default. Set to 1 on
    //   eng and userdebug builds.
    //
    // ro.secure:
    //   Drop privileges by default. Set to 1 on userdebug and user builds.
    property_get("ro.secure", value, "1");
    bool ro_secure = (strcmp(value, "1") == 0);

    property_get("ro.debuggable", value, "");
    bool ro_debuggable = (strcmp(value, "1") == 0);

6、修改 system/core/init/init.cpp,做如下的修改:

将selinux_is_enforcing函数直接返回false

修改前

static bool selinux_is_enforcing(void)
{
    if (ALLOW_PERMISSIVE_SELINUX) {
        return selinux_status_from_cmdline() == SELINUX_ENFORCING;
    }
    return true;
}

修改后

static bool selinux_is_enforcing(void)
{
    return false;
    if (ALLOW_PERMISSIVE_SELINUX) {
        return selinux_status_from_cmdline() == SELINUX_ENFORCING;
    }
    return true;
}

7、修改 system/core/init/Android.mk,做如下修改:

修改前

init_options += -DALLOW_LOCAL_PROP_OVERRIDE=0 -DALLOW_DISABLE_SELINUX=0

修改后

init_options += -DALLOW_LOCAL_PROP_OVERRIDE=0 -DALLOW_DISABLE_SELINUX=1

8、修改  device/**/aa/init.aa.rc,在文件最后添加如下代码 (在device下,通过grep "init.rc"  ./* -r -n  查找自己设备的 init.设备名.rc文件)

# wtw let other user can use su 
service	superuser /system/xbin/su --daemon
    class super-user
    user root
    oneshot
 
on property:superuser.start=on
    class_start super-user

9、修改 system/core/adb/Android.mk,做如下修改:

找到  # adbd device daemon 位置,注释掉判断。

修改前

ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
LOCAL_CFLAGS += -DALLOW_ADBD_DISABLE_VERITY=1
LOCAL_CFLAGS += -DALLOW_ADBD_ROOT=1
endif

修改后

# ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
LOCAL_CFLAGS += -DALLOW_ADBD_DISABLE_VERITY=1
LOCAL_CFLAGS += -DALLOW_ADBD_ROOT=1
# endif

10、对源码进行 make installclean,并且删除下device/linaro/generic/linaro_arm64 下面的 .config和.config.old 文件再进行编译。生成镜像就可以了。

二、通过supersu.apk 的方式进行实现

    这种方式,我是在源码编译完之后,生成的镜像进行修改。

    1.删除/system/xbin/su

    2.拷贝SuperSU.apk到  system/app/SuperSU/SuperSU.apk  给与权限 chmod 0644 /system/app/SuperSU/SuperSU.apk

    3.拷贝新的su文件到  /system/xbin/su ,chmod 0755 /system/xbin/su 给与权限

    4.拷贝新的su文件到  /system/xbin/daemonsu ,chmod 0755 /system/xbin/daemonsu 给与权限

    5.拷贝新的supolicy文件到  /system/xbin/supolicy,chmod 0755 /system/xbin/supolicy给与权限

    6.拷贝新的libsupol.so文件到  /system/lib64/libsupol.so ,chmod 0755 /system/lib64/libsupol.so 给与权限

    7. 执行  daemonsu --auto-daemon

    8.执行 su --install

    重启即可。

方法二中的相关的文件下在地址:

rootfile.rar_可以在userdebug模式su改个名字-Android文档类资源-CSDN下载

参考博客

华硕TinkerBoard(rk3288处理器)开发之 Android7.1.2修改源码,让app可以调用su获取权限--上部 - 灰信网(软件开发博客聚合)

### 回答1: APK是安卓应用程序的安装包文件,而执行shell命令是指在安卓系统中运行命令行脚本或者Linux命令的操作。 在APK中执行shell命令可以通过Java的Runtime类和Process类来实现。以下是一个简单的示例: 首先,需要在APK的AndroidManifest.xml文件中添加适当的权限,使得应用程序可以执行shell命令。例如,添加以下权限: <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/> 然后,在APK的Java代码中,可以使用以下代码来运行shell命令: String command = "your_shell_command"; try { Process process = Runtime.getRuntime().exec(command); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); StringBuilder output = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { output.append(line + "\n"); } reader.close(); process.waitFor(); String result = output.toString(); // 处理命令执行的结果 } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } 上面的代码中,先创建一个Process对象,然后通过exec方法执行shell命令使用BufferedReader读取命令执行结果,并将结果存储在一个StringBuilder对象中。最后,通过调用waitFor方法等待命令执行完成。你可以根据需要,对命令执行结果进行处理。 需要注意的是,在执行shell命令时,可能需要在命令前面加上"su",以获取root权限,从而执行一些需要root权限的命令。 总之,通过在APK中执行shell命令,可以实现一些系统级操作,例如修改系统配置、安装软件、访问设备文件等。但是需要注意的是,使用这种功能需要小心谨慎,确保只执行可信的shell命令,以避免对系统造成损害。 ### 回答2: 在Android开发中,apk是Android应用程序的安装包后缀名。在apk中执行shell命令可以通过Java中的Runtime类实现。 首先,需要在AndroidManifest.xml文件中添加以下权限: ``` <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> ``` 然后,在代码中使用如下方法执行shell命令: ```java try { Process process = Runtime.getRuntime().exec("shell命令"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line); stringBuilder.append("\n"); } String result = stringBuilder.toString(); // 处理命令执行结果 } catch (IOException e) { e.printStackTrace(); } ``` 在exec()方法中传入要执行的shell命令,然后通过BufferedReader读取命令执行的结果,并将结果存储在StringBuilder中。最后可以对结果进行处理。 需要注意的是,执行shell命令可能需要一些特定的权限,例如写入外部存储、读取手机状态等。因此,在使用这种方式执行shell命令时,要确保已经获取了必要的权限,以避免运行时错误。 最后,值得注意的是,在使用apk执行shell命令时,要确保所做的操作符合Android平台的安全策略,以防止不当的使用或滥用。 ### 回答3: APK是Android应用程序包的文件扩展名,它包含了Android应用程序的所有组件和资源。在APK中执行shell命令可以通过使用Android的Runtime类来实现。 要在APK中执行shell命令,首先需要获取Runtime对象,可以使用以下代码: Runtime runtime = Runtime.getRuntime(); 然后,使用该Runtime对象的exec()方法来执行所需的shell命令。以下是一个示例代码: try { Process process = runtime.exec("shell命令"); // 获取命令执行的输入流 InputStream inputStream = process.getInputStream(); // 获取命令执行的错误流 InputStream errorStream = process.getErrorStream(); // 读取输入流和错误流的内容 BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream)); BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorStream)); String line; // 打印命令执行结果 while ((line = inputReader.readLine()) != null) { System.out.println(line); } // 打印错误信息(如果有) while ((line = errorReader.readLine()) != null) { System.out.println(line); } // 关闭输入流和错误流 inputReader.close(); errorReader.close(); } catch (IOException e) { e.printStackTrace(); } 需要注意的是,执行shell命令可能需要相应的权限,因此在执行前需要确保APK的AndroidManifest.xml文件中已经声明了所需的权限。 以上是在APK中执行shell命令的基本步骤和示例代码。通过在APK中执行shell命令,可以实现与系统交互、执行一些系统级操作等功能。但请注意,使用不当可能导致应用程序崩溃或产生安全风险,所以在开发过程中需谨慎使用
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值