如何让应用程序获取system权限、root权限 您所在的位置:网站首页 如何查看应用程序的代码设置权限 如何让应用程序获取system权限、root权限

如何让应用程序获取system权限、root权限

2024-07-17 00:04| 来源: 网络整理| 查看: 265

        第一个方法简单点,不过需要在Android系统源码的环境下用make来编译:

        1. 在应用程序的AndroidManifest.xml中的manifest节点中加入android:sharedUserId="android.uid.system"这个属性。

        2. 修改Android.mk文件,加入LOCAL_CERTIFICATE := platform这一行

        3. 使用mm命令来编译,生成的apk就有修改系统时间的权限了。

        第二个方法是直接把eclipse编出来的apk用系统的签名文件签名

        1. 加入android:sharedUserId="android.uid.system"这个属性。

        2. 使用eclipse编译出apk文件。

        3. 使用目标系统的platform密钥来重新给apk文件签名。首先找到密钥文件,在Android源码目录中的位置是"build/target/product/security",下面的platform.pk8和platform.x509.pem两个文件。然后用Android提供的Signapk工具来签名,signapk的源代码是在"build/tools/signapk"下,编译后在out/host/linux-x86/framework下,命令为

java -jar signapk.jar platform.x509.pem platform.pk8 input.apk output.apk

       加入android:sharedUserId="android.uid.system"这个属性。通过sharedUserId,拥有同一个UserId的多个APK可以配置成运行在同一个进程中。那么把程序的UID配成android.uid.system,也就是要让程序运行在系统进程中,这样就有权限来修改系统时间了。

        只是加入UID还不够,如果这时候安装APK的话发现无法安装,提示签名不符,原因是程序想要运行在系统进程中还要有目标系统的platform key,就是上面第二个方法提到的platform.pk8和platform.x509.pem两个文件。用这两个key签名后apk才真正可以放入系统进程中。第一个方法中加入LOCAL_CERTIFICATE := platform其实就是用这两个key来签名。

        这也有一个问题,就是这样生成的程序只有在原始的Android系统或者是自己编译的系统中才可以用,因为这样的系统才可以拿到platform.pk8和platform.x509.pem两个文件。要是别家公司做的Android上连安装都安装不了。试试原始的Android中的key来签名,程序在模拟器上运行OK,不过放到G3上安装直接提示"Package ... has no signatures that match those in shared user android.uid.system",这样也是保护了系统的安全。

  获取Root权限

  一般linux 获取root权限是通过执行su命令,那能不能在apk程序中也同样执行一下该命令呢?Linux 编程中,有一组函数族:

 

1   int execl(const char *path, const char *arg, ...); 2 3   int execlp(const char *file, const char *arg, ...); 4 5   int execle(const char *path, const char *arg, ..., char *const envp[]); 6 7   int execv(const char *path, char *const argv[]); 8 9   int execvp(const char *file, char *const argv[]); 10 11   int execve(const char *path, char *const argv[], char *const envp[]);

 

  在java中我们可以借助 Runtime.getRuntime().exec(String command)访问底层Linux下的程序或脚本,这样就能执行su命令,使apk具有root权限,能够访问系统中需要root权限才能执行的程序或脚本了。

  还是举个例子,

 

1 public class VisitRootfileActivity extends Activity { 2 private static final String TAG = "VisitRootfileActivity"; 3 Process process = null; 4 Process process1 = null; 5 DataOutputStream os = null; 6 DataInputStream is = null; 7 /** Called when the activity is first created. */ 8 @Override 9 public void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.main); 12 try { 13 process = Runtime.getRuntime().exec("/system/xbin/su"); /*这里可能需要修改su 14 15 的源代码 (注掉 if (myuid != AID_ROOT && myuid != AID_SHELL) {*/ 16 17 os = new DataOutputStream(process.getOutputStream()); 18 is = new DataInputStream(process.getInputStream()); 19 os.writeBytes("/system/bin/ls" + " \n"); //这里可以执行具有root 权限的程序了 20 os.writeBytes(" exit \n"); 21 os.flush(); 22 process.waitFor(); 23 } catch (Exception e) { 24 Log.e(TAG, "Unexpected error - Here is what I know:" + e.getMessage()); 25 } finally { 26 try { 27 if (os != null) { 28 os.close(); 29 } 30 if (is != null) { 31 is.close(); 32 } 33 process.destroy(); 34 } catch (Exception e) { 35 } 36 }// get the root privileges 37 } 38 }

 

 

 

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有