gpt4 book ai didi

java - Android root 访问脚本

转载 作者:太空狗 更新时间:2023-10-29 13:57:29 25 4
gpt4 key购买 nike

我需要有关此脚本的帮助,有人可以告诉我我做错了什么吗?我正在尝试在启动时制作一个根检查器。

  1. 检查写入权限
  2. 没有root的ReturnaAlert
  3. 按退出键退出(TO DO)

这是我正在构建的确切 Java fragment

{
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对于测试人员。

根 fragment 来源评论here

最佳答案

通常要检查 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"));

编辑:

我在我的应用程序中使用以下根权限类:

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

关于java - Android root 访问脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38265957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com