zoukankan      html  css  js  c++  java
  • Android中部署自己的su

    本人博客原文

    首先把你的自己的su的放到Android应用程序project的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(" ");
            strBuffer.append("mount -o remount,rw /system /system");
            strBuffer.append(" ");
            strBuffer.append("cat ").append(localSuPath).append(">" + kSysSuPath);
            strBuffer.append(" ");
            strBuffer.append("chown 0:0 " + kSysSuPath);
            strBuffer.append(" ");
            strBuffer.append("chmod 6777 " + kSysSuPath);
            strBuffer.append(" ");
            strBuffer.append("mount -o remount,ro " + MountPoint.getDeviceName("/system") + " /system");
            strBuffer.append(" ");
            strBuffer.append("mount -o remount,ro /system /system");
            strBuffer.append(" ");
            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 ");
                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 ");
                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();
                }
            }
        }
    }


    结束!
  • 相关阅读:
    SQL Server SQLHelper帮助类
    Winform 常用的方法
    SQL Server 插入含有中文字符串出现乱码现象的解决办法
    ComboBox 中 DisplayMember 和 ValueMember 都是具体干什么的?
    HTML常用标签属性使用
    虚拟机安装windows server 2012 R2
    VS2017生成带图标的QT项目方法
    QSS 记录
    QT qss资源文件与代码分离
    pgsql 服务遇见的问题记录
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4064954.html
Copyright © 2011-2022 走看看