Android root访问脚本

时间:2016-07-08 11:54:12

标签: java android android-intent android-activity apk

我需要这个脚本的帮助,有人可以告诉我,我做错了什么? 我试图在启动时制作root检查程序。

  1. 检查写入权限
  2. 没有根的ReturnaAlert
  3. 按退出(TO DO)退出
  4. 这是我构建的确切Java代码段

    using System;
    using System.Reflection;
    
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a method name: ");
    
            // user enters a method name
            string input = Console.ReadLine();
    
            var method = typeof(Program).GetMethod(input,
                BindingFlags.Static | BindingFlags.NonPublic);
            var func = (Func<string, string>) Delegate.CreateDelegate(
                typeof(Func<string, string>), method);
            RunMethod(func);
        }
    
        static void RunMethod(Func<string, string> method)
        {
            Console.WriteLine(method("John Doe"));
        }
    
        static string Method1(string name)
        {
            return "method 1 called : " + name;
        }
    
        static string Method2(string name)
        {
            return "method 2 called : " + name;
        }
    }
    

    APK文件为测试人员构建here

    根片段来源评论here

1 个答案:

答案 0 :(得分:1)

通常要检查root,您必须从Linux命令id检查“userid”。

所以而不是:

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

使用:

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

然后阅读回复,例如:

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

并检查结果:

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

编辑:

我在我的应用上使用以下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;
    }
}