简体   繁体   English

Android root访问脚本

[英]Android root access script

I need help with this script, can someone tell me what did I do wrong? 我需要这个脚本的帮助,有人可以告诉我,我做错了什么? I'm trying to make a root checker on startup. 我正在尝试在启动时创建根检查器。

  1. Check write access 检查写访问权限
  2. ReturnaAlert of no root ReturnaAlert没有根
  3. Exit on press exit (TO DO) 退出退出(TO DO)

Here's the exact Java snippet I'm building 这是我正在构建的确切Java代码段

{
        Process p;
    try
    { 
        //  Run root command
        p = Runtime.getRuntime().exec("su"); 

        // Attempt to write a file to system
        DataOutputStream os = new DataOutputStream(p.getOutputStream()); 
        os.writeBytes("echo \"Do I have root?\" >/system/temporary.txt\n");

        // Close the stream
        os.writeBytes("exit\n"); 
        os.flush(); 
        try
        { 
            p.waitFor(); 
            if (p.exitValue() != 255)
            { 
                // Code to run on ROOTED
                //  NOTHING JUST GO FORWARD
            }
            else
            { 
                // Code to run on NON ROOTED

            } 
        }
        catch (InterruptedException e)
        { {
            // TODO Code to run with interrupted exception

            }
        } 
    }
    catch (IOException e)
    { 
        // TODO Code to run in input/output exception

    }}}

The APK file is built here for testers. APK文件是建立在这里进行测试。

The root snippets source comment here root snippets源代码在这里发表评论

Usually to check for root, you have to check your "userid", from the Linux command id . 通常要检查root,你必须从Linux命令id检查你的“userid”。

So instead of: 所以代替:

os.writeBytes("echo \\"Do I have root?\\" >/system/temporary.txt\\n");

use: 采用:

os.writeBytes("id\\n"); os.flush();

Then read the response, with something such as: 然后阅读响应,例如:

DataInputStream data = new DataInputStream(p.getInputStream());

And check the result with: 并检查结果:

if (data .readLine().contains("uid=0"));

Edit: 编辑:

I use the following Root Privileges class on my apps: 我在我的应用上使用以下Root Privileges类:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Root related operations.
 */
public class RootPrivileges {
    public static final String TAG = "RootPrivileges";

    private RootPrivileges() {
        Log.e(TAG, "RootPrivileges should not be instantiated");
    }

    /**
     * Checks and asks for Root privileges
     *
     * @return true if has root privileges, false otherwise
     */
    public static boolean hasRoot() {
        boolean resp = false;
        Process suProcess;
        try {
            suProcess = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
            DataInputStream osRes = new DataInputStream(suProcess.getInputStream());
            if (os != null && osRes != null) {
                os.writeBytes("id\n");
                os.flush();
                String currUid = osRes.readLine();
                boolean exitSu;
                if (null == currUid) {
                    resp = false;
                    exitSu = false;
                    Log.e(TAG, "No root privileges, or denied by user");
                } else if (currUid.contains("uid=0")) {
                    resp = true;
                    exitSu = true;
                    Log.v(TAG, "Root privileges given");
                } else {
                    resp = false;
                    exitSu = true;
                    Log.e(TAG, "Not enough privileges.\n   Received: " + currUid + "\n   Expected: 0");
                }
                if (exitSu) {
                    os.writeBytes("exit\n");
                    os.flush();
                }
            }
        } catch (Exception e) {
            resp = false;
            Log.e(TAG, "Root privileges denied. [" + e.getClass().getName() + "] : " + e.getMessage());
        }
        return resp;
    }

    /**
     * Executes a command as root.
     *
     * @param cmd the command.
     * @return if code was sent to execute
     */
    public static final boolean execute(String cmd) {
        try {
            if (cmd != null && cmd.length() > 0) {
                Process suProcess = Runtime.getRuntime().exec("su");
                DataOutputStream dataOutputStream = new DataOutputStream(suProcess.getOutputStream());
                DataInputStream dataInputStream = new DataInputStream(suProcess.getInputStream());
                DataInputStream dataErrorStream = new DataInputStream(suProcess.getErrorStream());

                dataOutputStream.writeBytes(cmd);
                dataOutputStream.writeBytes("\n");
                dataOutputStream.flush();
                dataOutputStream.writeBytes("exit\n");

                BufferedReader reader = new BufferedReader(new InputStreamReader(dataInputStream));
                BufferedReader err_reader = new BufferedReader(new InputStreamReader(dataErrorStream));
                String resp;
                while ((resp = reader.readLine()) != null) {
                    Log.v(TAG, "[resp]" + resp);
                }
                while ((resp = err_reader.readLine()) != null) {
                    Log.v(TAG, "[err_resp]" + resp);
                }
                reader.close();
                err_reader.close();
                dataOutputStream.flush();
                try {
                    int suProcessRetval = suProcess.waitFor();
                    suProcess.destroy();
                    return (suProcessRetval != 255);
                } catch (Exception ex) {
                    Log.e(TAG, "Error in Root command execution");
                    ex.printStackTrace();
                }
            } else {
                Log.e(TAG, "command is null or empty");
            }
        } catch (IOException ex) {
            Log.e(TAG, "IOException");
            ex.printStackTrace();
        } catch (SecurityException ex) {
            Log.e(TAG, "SecurityException");
            ex.printStackTrace();
        } catch (Exception ex) {
            Log.e(TAG, "Generic Exception");
            ex.printStackTrace();
        }
        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM