Android中部署自己的su

本人博客原文

首先把你的自己的su的放到Android应用程序工程的assets目录,为了和系统的su区分,我自己的su文件叫做sur。

另外我这里没有考虑x86架构的cpu的手机。

废话不多说,直接上代码吧!

Util.java文件

package cdut.robin.root.utils;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import ledroid.nac.NacShellCommand;

import android.content.Context;

import android.util.Log;

public class Util {

    private static String getDeployMySuShellScript(String localSuPath) {

        StringBuffer strBuffer = new StringBuffer();

        strBuffer.append("mount -o remount,rw " + MountPoint.getDeviceName("/system") + " /system");

        strBuffer.append("\n");

        strBuffer.append("mount -o remount,rw /system /system");

        strBuffer.append("\n");

        strBuffer.append("cat ").append(localSuPath).append(">" + kSysSuPath);

        strBuffer.append("\n");

        strBuffer.append("chown 0:0 " + kSysSuPath);

        strBuffer.append("\n");

        strBuffer.append("chmod 6777 " + kSysSuPath);

        strBuffer.append("\n");

        strBuffer.append("mount -o remount,ro " + MountPoint.getDeviceName("/system") + " /system");

        strBuffer.append("\n");

        strBuffer.append("mount -o remount,ro /system /system");

        strBuffer.append("\n");

        return strBuffer.toString();

    }

    final static String kSysSuPath = "/system/xbin/sur";

    private static boolean isMySuExists() {

        return new File(kSysSuPath).exists();

    }

    private static boolean writeMySu(Context context) {

        Process processShell = null;

        DataOutputStream osShell = null;

        String mySuTempPath = context.getFilesDir().getPath() + "/sur";

        File file = new File(mySuTempPath);

        if (file.exists()) {

            file.delete();

        }

        InputStream open = null;

        FileOutputStream out = null;

        try {

            open = context.getResources().getAssets().open("sur");

            out = context.openFileOutput("sur", Context.MODE_WORLD_WRITEABLE);

            byte buffer[] = new byte[4 * 1024];

            int len = 0;

            while ((len = open.read(buffer)) != -1) {

                out.write(buffer, 0, len);

            }

            out.flush();

        } catch (IOException e) {

            LogHelper.e("TAG", "errMessage" + e.getMessage());

        } finally {

            if (out != null) {

                try {

                    out.close();

                    if (open != null) {

                        open.close();

                    }

                } catch (Exception e) {

                    LogHelper.e("TAG", "errMessage" + e.getMessage());

                }

            }

        }

        Runtime runTime = Runtime.getRuntime();

        try {

            processShell = runTime.exec("su");

            osShell = new DataOutputStream(processShell.getOutputStream());

            String str = getDeployMySuShellScript(mySuTempPath);

            osShell.writeBytes(str);

            osShell.writeBytes("exit\n");

            osShell.flush();

            processShell.waitFor();

        } catch (IOException e) {

            

            e.printStackTrace();

        } catch (InterruptedException e) {

          

            e.printStackTrace();

        } finally {

            if (processShell != null) {

                try {

                    processShell.destroy();

                } catch (Exception e) {

                    // e.printStackTrace();

                }

                processShell = null;

            }

            if (osShell != null) {

                try {

                    osShell.close();

                    osShell = null;

                } catch (IOException e1) {

                    // e1.printStackTrace();

                }

            }

        }

        return new File(kSysSuPath).exists();

    }

    public static boolean doSthBySu(Context context) {

        if (!isMySuExists()) {

            boolean res = writeMySu(context);

            if (res) {

                Log.i("robin", "deploy My Su success!");

            }

            else

            {

                Log.i("robin", "deploy My Su fail!");

            }

        } else{

            Log.i("robin", "My su exsit!");

        }

        Process processShell = null;

        DataOutputStream osShell = null;

        //do something here by su

        try {

            Runtime runTime = Runtime.getRuntime();

            processShell = runTime.exec("sur");

            osShell = new DataOutputStream(processShell.getOutputStream());

            String str = getBussinessShellScript();

            osShell.writeBytes(str);

            osShell.writeBytes("exit\n");

            osShell.flush();

        } catch (Exception e) {

            e.printStackTrace();

            return false;

        } finally {

            if (processShell != null) {

                try {

                    processShell.destroy();

                } catch (Exception e) {

                     e.printStackTrace();

                }

                processShell = null;

            }

            if (osShell != null) {

                try {

                    osShell.close();

                    osShell = null;

                } catch (IOException e1) {

                    // e1.printStackTrace();

                }

            }

        }

        return true;

    }

    public static String getBussinessShellScript() {

        return "echo hello";

    }

}

MountPoint.java文件

package cdut.robin.root.utils;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

public final class MountPoint {

    private static HashMap<String, String> MOUNT_POINT_CACH = new HashMap(10);

    private static HashMap<String, List<String>> DEVICE_CACH = new HashMap(10);

    public static boolean isMountPoint(String mountPoint) {

        return getDeviceName(mountPoint) != null;

    }

    public static String getDeviceName(String mountPoint) {

        if (mountPoint == null) {

            return null;

        }

        String deviceName = null;

        if (MOUNT_POINT_CACH.containsKey(mountPoint)) {

            deviceName = (String) MOUNT_POINT_CACH.get(mountPoint);

        }

        return deviceName;

    }

    public static boolean hasMultiMountPoint(String deviceName) {

        List list = getMountPoints(deviceName);

        return (list != null) && (list.size() > 1);

    }

    public static List<String> getMountPoints(String deviceName) {

        return (List) DEVICE_CACH.get(deviceName);

    }

    static {

        BufferedReader mountPointReader = null;

        try {

            mountPointReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("/proc/mounts"))));

            String buffer = null;

            while ((buffer = mountPointReader.readLine()) != null) {

                MOUNT_POINT_CACH.put(buffer.split(" ")[1], buffer.split(" ")[0]);

                List list = (List) DEVICE_CACH.get(buffer.split(" ")[0]);

                if (list == null) {

                    list = new ArrayList(1);

                }

                list.add(buffer.split(" ")[1]);

                DEVICE_CACH.put(buffer.split(" ")[0], list);

            }

        } catch (IOException e) {

        } finally {

            try {

                if (mountPointReader != null)

                    mountPointReader.close();

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

    }

}

结束!

Android中部署自己的su

时间: 2024-08-01 00:47:30

Android中部署自己的su的相关文章

Android中如何自己制作su

本文原博客:http://hubingforever.blog.163.com/blog/static/171040579201372915716149/ 在Android源码的system\extras(比如Android4.0\system\extras)下新建一个目录,比如su_robin目录 在su_robin目录下包含以三个文件: su.h文件 #ifndef SU_h  #define SU_h 1 #ifdef LOG_TAG #undef LOG_TAG #endif #defi

Android动态部署五:如何从插件apk中启动Service

转载请注明出处:http://blog.csdn.net/ximsfei/article/details/51072332 github地址:https://github.com/ximsfei/DynamicDeploymentApk Android动态部署一:Google原生Split APK浅析 Android动态部署二:APK安装及AndroidManifest.xml解析流程分析 Android动态部署三:如何从插件apk中启动Activity(一) Android动态部署四:如何从插

Android动态部署五:怎样从插件apk中启动Service

转载请注明出处:http://blog.csdn.net/ximsfei/article/details/51072332 github地址:https://github.com/ximsfei/DynamicDeploymentApk Android动态部署一:Google原生Split APK浅析 Android动态部署二:APK安装及AndroidManifest.xml解析流程分析 Android动态部署三:怎样从插件apk中启动Activity(一) Android动态部署四:怎样从插

Android中的权限管理(基于uid gid gids setUid)

我们首先来说一下传统的Linux基于uid,gid的权限管理机制: 1.用户的uid gid gids: Ubuntu操作系统当前登陆的用户是jltxgcy,那么该用户的uid为jltxgcy,gid也是jltxgcy,那么gids怎么查看呢? 答案是使用命令:cat /etc/group | grep jltxgcy.如下图: 用户的gids的名字为adm,dialout,cdrom,plugdev,lpadmin,admin,sambashare.此本分请参考linux用户组./etc/gr

Android中ViewMapping注解

1.Java.lang包中常用的注解有@Override,@Deprected(已经废弃),@SupressWarning(屏蔽掉一些警告.)我们可以自定义注解. 2.Java注解之@Retention,@Documented,@Inherited. Retention注解,保留注解说明,这种类型的猪儿会被保留到哪个阶段,有三个值:RetentionPolicy.SOURCE(只有源码级别保留,编译时被忽略),RetentionPolicy.CLASS(class文件中保留,Jvm被忽略),Re

Android中多线程编程(四)AsyncTask类的详细解释(附源码)

Android中多线程编程中AsyncTask类的详细解释 1.Android单线程模型 2.耗时操作放在非主线程中执行 Android主线程和子线程之间的通信封装类:AsyncTask类 1.子线程中更新UI 2.封装.简化异步操作. 3.AsyncTask机制:底层是通过线程池来工作的,当一个线程没有执行完毕,后边的线程是无法执行的.必须等前边的线程执行完毕后,后边的线程才能执行. AsyncTask类使用注意事项: 1.在UI线程中创建AsyncTask的实例 2.必须在UI线程中调用As

Android中客户端请求服务器端的方式讲解(一)附源码

Android中客户端请求服务器端的两种方式:Post方式和Get方式 在这里不直接赘述了,直接上源码如下: (1).Post的方式: /** * Post的请求方式 * * @param model * 请求序号 * @param paramList * 客户端请求的数据参数列表 * @return */ public JSONObject doPost(int model, List<NameValuePair> paramList) { try { // 客户端向服务器发送请求的数据 L

在Android中调用C#写的WebService(附源代码)

由于项目中要使用Android调用C#写的WebService,于是便有了这篇文章.在学习的过程中,发现在C#中直接调用WebService方便得多,直接添加一个引用,便可以直接使用将WebService当做一个对象使用,利用Vs2010中的代码提示功能就能爽歪歪地把想要的东西全部点出来.在Android调用,麻烦了一点,但是也还好.主要是我们需要自己在代码中确定要调用WebService的方法名是什么,要传给WebService什么参数以及对应的参数名,另外,一些额外的信息比如soap的版本号

在Android中修改快捷方式的图标

最近在做项目开发时用到了MySql数据库,在看了一些有关MySql的文章后,很快就上手使用了.在使用的过程中还是出现了一些问题,因为使用的是绿色免安装版的MySql所以在配置的时候出现了一些问题,该篇文章就主要针对MySql绿色版的配置及其使用进行讨论. 一.MySql概述 MySql数据库是有瑞典MySql AB公司开发,现在该公司被Oracle收购属于Oracle所有.同SQL Server类似,它也是基于关系型数据库的数据库管理系统,在Web应用方面MySQL是最好的RDBMS之一,因为它