如何检查 Android 中的 root 访问权限?
我创建了一个方法来检查Android手机是否已root。这样做的方法如下,
public int checkrootcommand(String string) {
// TODO Auto-generated method stub
Process exec;
try {
exec = Runtime.getRuntime().exec(new String[]{"su","-c"});
final OutputStreamWriter out = new OutputStreamWriter(exec.getOutputStream());
out.write("exit");
out.flush();
Log.i(SUPER_USER_COMMAND, "su command executed successfully");
return 0; // returns zero when the command is executed successfully
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 1; //returns one when the command execution fails
}
但问题是,首先执行方法 checkrootcommand() 它工作正常,但是当再次调用相同的方法时,超级用户会话仍在运行。有没有办法在方法执行后结束超级用户会话?
I created a method for checking whether the android phone is rooted or not. This is done as follows
public int checkrootcommand(String string) {
// TODO Auto-generated method stub
Process exec;
try {
exec = Runtime.getRuntime().exec(new String[]{"su","-c"});
final OutputStreamWriter out = new OutputStreamWriter(exec.getOutputStream());
out.write("exit");
out.flush();
Log.i(SUPER_USER_COMMAND, "su command executed successfully");
return 0; // returns zero when the command is executed successfully
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 1; //returns one when the command execution fails
}
But the problem is that the method checkrootcommand() is executed first it works perfectly, but when the same method called again the superuser session is still running. Is there any way to end the superuser session once the method is executed??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于通过利用软件漏洞克服硬件保护的设备,没有可靠的方法来检测设备上的 root 情况。
最好的情况是,您可以检测特定工具集的存在或扫描不应该存在的东西或文件中的更改 - 但这需要了解给定的安装应该是什么样子,并假设您正在使用的操作系统功能用于进行检查的方法尚未被修改以隐藏更改。
为了可靠地扫描,您需要确保受信任的代码运行在比不受信任的代码更低的级别上;获得 root 权限的设备是这种保证从根本上被破坏的设备,或者是最终用户比开发人员更受信任的设备。
There is no reliable means of detecting a rooted condition on a device where hardware protections have been overcome by exploiting software vulnerabilities.
At best you can detect the presence of particular toolsets or scan for things that aren't supposed to be there or changes in files that are - but that requires knowledge of what a given installation should look like, and assumes that the OS functionality you are using to make the checks hasn't been modified to hide the changes.
To reliably scan, you need to be sure trusted code runs at a lower level than untrusted code; a rooted device is one where this assurance has been fundamentally broken, or where the end user is trusted more than you the developer are.
在您的情况下,您应该在执行返回之前完成的作业后终止该进程。对代码进行以下更改应该可以完成此操作。
您可能无法普遍检测手机是否已 root,但您应该能够请求并确认您的应用程序是否可以通过以 root 身份运行 id 来访问 root,例如
su -c id
验证是否命令执行成功,输出包含uid=0
,即root用户的uid。In your case, you should kill the process after executing it for the job which is done before returning. The following changes to your code should do the thing.
You may not be able to universally detect whether phone is rooted or not but you should be able to request and then confirm is your app can access root by running id as root e.g.,
su -c id
validate if the command executed successfully and the output containsuid=0
i.e., the uid of the root user.方法 1:应用程序要求 ROOT 访问权限:
将其添加到应用程序级 gradle 构建文件中:
现在,
方法 2:应用程序不要求 ROOT 访问权限:
Method 1 : Application asks for ROOT access :
Add this in your app-level gradle build file :
Now,
Method 2 : Application doesn't asks for ROOT :
使用此代码:
Use this code:
您可以通过终端命令实现此目的,并且可以在应用程序中运行终端命令。
您的应用程序也不需要这种方式的 root 访问权限。
You can achieve this from a terminal command and you can run terminal commands within an app.
Your application also doesn't require root access this way.
在搜索解决方案来测试是否授予超级用户访问权限后,只有Lexip 的解决方案与扫描仪工作正常。
要完成他的回复:
在已 root 的手机上使用
isRooted = true
,如果没有,则返回 false。如果授予超级用户访问权限,则
isRootGranted = true
;如果没有,则为 false。每次你打电话时都在工作。
问候。
After searching for a solution to test if SuperUser Access granted or not, only Lexip's solution with Scanner works fine.
To complete his response:
isRooted = true
on a rooted phone, and false if not.isRootGranted = true
if superUser Access is granted, and false if not.Working everytime you call it.
Regards.