2016-07-08 56 views
3

我需要关于此脚本的帮助,有人能告诉我我做错了什么吗? 我想在启动时做一个根检查器。Android root访问脚本

  1. 检查写访问
  2. 没有根的ReturnaAlert
  3. 上按EXIT退出(TO DO)

这里是确切的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 

    }}} 

的APK文件是为测试人员构建的here

根片段源评论here

+0

虽然这个问题可能已经固定,从来没有“擦除“ 你的问题。如果您希望将其删除,请将其删除。如果没有,请保持原样,因为其他用户可能会遇到类似的问题,并且可以阅读,并尝试了解您的问题,以及所提供的解决方案是否可以帮助他们。 – Bonatti

+0

@Bonatti谢谢Bonatti,我会为其他人解决原来的问题...我发现我写了错误的ID,所以我解决了一个额外的问题'ELSE IF' – Zillinium

+0

请不要破坏你自己的问题/答案删除所有内容 –

回答

1

平时要检查的根,你必须检查你的“用户id”,从Linux命令id

所以不是:

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权限类在我的应用程序:

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; 
    } 
} 
+0

你能编辑我提供的代码并重新回答! - 我是新手! ..你也可以看看退出代码,我找不到任何工作方法来完全关闭应用程序 – Zillinium

+0

就Android而言,你永远不应该“关闭应用程序”,它需要更多的电气准备一切和分配比它在Android内存管理系统中执行。您应该阅读[“Android生命周期”](https://developer.android.com/reference/android/app/Activity.html)以更好地理解应用程序的行为。如果这是在一个Activity的上下文中,那么只需“完成()”该活动。至于超级用户/切换用户,这个班一直对我很好。 – Bonatti

+0

通常情况下这是真的,但该应用程序仅用于按下安装或删除按钮,删除按钮也会通过运行.sh脚本文件关闭并卸载它自己的应用程序....所以不断运行它是不可能的。 – Zillinium