Android应用获取root权限,修改系统文件
Android系统权限说明
首先对Android权限等级进行一下说明,简单的分为一般用户、系统用户和超级用户。
一般的用户权限在Manifest.xml文件中使用<uses-permission/>标签进行申请,比如sd卡读写权限、网络使用权限等等;
系统用户权限需要给Apk添加与Framework相同的签名,这样应用就能够调用Android的一些核心的接口,比如访问系统文件(read)、后台静默安装Apk……
超级用户即root用户,拥有这台Android机的所有权限,能够对包括系统文件在内所有文件进行操作,所以如果随意将root权限赋予应用的话是十分危险的。Android手机在出厂的时候大多不提供root权限。
获取root权限
Android root的根本原理是将su文件拷贝到/system/bin/或/system/xbin/目录下。目前获取root权限主要有两种方式:1、利用系统漏洞,窃取root权限;2、修改boot.img重新刷入机器。详情请参考:http://www.zhihu.com/question/21074979
Android应用申请root权限
对于已经root过的机器,应用可以直接申请获取root权限,实现对系统文件的操作。基本是使用shell命令进行操作,首先是使用“su”切换到root用户,然后是"chmod 777 " + pkgCodePath修改该应用的读写权限,如果以上两步能够执行通过则Apk就具有了root权限。代码如下:
public static boolean upgradeRootPermission(String pkgCodePath) {Process process = null;DataOutputStream os = null;try {String cmd = "chmod 777 " + pkgCodePath;process = Runtime.getRuntime().exec("su"); // 切换到root帐号os = new DataOutputStream(process.getOutputStream());os.writeBytes(cmd + "\n");os.writeBytes("exit\n");os.flush();process.waitFor();} catch (Exception e) {return false;} finally {try {if (os != null) {os.close();}process.destroy();} catch (Exception e) {}}return true;}
修改系统文件
获得root权限后就够执行root用户才能执行的shell命令,比如说修改系统文件。因为系统目录默认挂载为只读的,要修改系统文件首先要将该分区挂载分区挂载为可读的;首先执行命令“mount”,目的是找到该分区挂载的位置;然后执行重新挂载的命名mount -o remount,rw "+block+" /system"(block)是对应的挂载路径。挂载为可读写的之后就任你随意蹂躏了。
核心代码
执行shell命令方法:
public static String execRootCmd(String cmd) {String result = "";DataOutputStream dos = null;DataInputStream dis = null;try {Process p = Runtime.getRuntime().exec("su");dos = new DataOutputStream(p.getOutputStream());dis = new DataInputStream(p.getInputStream());dos.writeBytes(cmd + "\n");dos.flush();dos.writeBytes("exit\n");dos.flush();String line = null;while ((line = dis.readLine()) != null) {result += line+"\r\n";}p.waitFor();} catch (Exception e) {e.printStackTrace();} finally {if (dos != null) {try {dos.close();} catch (IOException e) {e.printStackTrace();}}if (dis != null) {try {dis.close();} catch (IOException e) {e.printStackTrace();}}}return result;}
具体操作步骤:
if (!upgradeRootPermission(getPackageCodePath())) {result="获取root权限失败";Toast.makeText(this, "获取root权限失败", Toast.LENGTH_SHORT).show();}button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String re = result;re += execRootCmd("mount");if (re.length() > 10) {block = re.substring(0, re.indexOf(" /system"));block = block.substring(block.lastIndexOf("\n") + 1);}Log.i("", re);textView.setText(re + "\r\n" +"..."+ block + "...");}});button2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if(block != null&&block.length()>0){String re = execRootCmd("mount -o remount,rw "+block+" /system");textView.setText(re);}else{Toast.makeText(MainActivity.this, "未找到system", Toast.LENGTH_SHORT).show();}}});button3.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String re = execRootCmd("cp -f " + filePath + " " + newFilePath);textView.setText(re);}});