如何在android中检查root访问权限?
Posted
技术标签:
【中文标题】如何在android中检查root访问权限?【英文标题】:How to check root access in android? 【发布时间】:2011-10-07 20:41:44 【问题描述】:我创建了一个检查安卓手机是否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() 方法首先执行它工作正常,但是当再次调用相同的方法时,超级用户会话仍在运行。方法执行后有什么方法可以结束超级用户会话吗??
【问题讨论】:
尝试在出口的末尾添加一个 \n。但这不是测试手机是否已root的可靠方法。 @antlersoft : 你能建议一个合适的方法吗? code.google.com/p/roottools 也许你对此感兴趣 @Badr Hari:感谢您的建议。 这不仅不可靠,尝试使用 su 来检查一个没有宣传自己使用 root 权限来做对用户有益的事情的应用程序可能会导致用户将您的应用程序报告为邪恶。即使他们不报告,他们也可能会使用 su 包装器将您的应用程序列入黑名单,同时允许他们希望拥有 root 访问权限的应用程序这样做 - 因此您可能会错误地认为它没有 root 权限,而不仅仅是 root 权限,但以最常见的方式之一设置为 root 访问。 【参考方案1】:没有可靠的方法检测通过利用软件漏洞克服了硬件保护的设备上的root状态。
您最多可以检测特定工具集的存在或扫描不应该存在的东西或文件中的更改 - 但这需要了解给定安装的外观,并假设操作系统您用于进行检查的功能尚未修改以隐藏更改。
要可靠地扫描,您需要确保受信任代码的运行级别低于不受信任的代码;有根设备是这种保证从根本上被破坏的设备,或者最终用户比开发人员更受信任的设备。
【讨论】:
【参考方案2】:在您的情况下,您应该在执行返回之前完成的作业后终止该进程。对您的代码进行以下更改应该可以做到这一点。
public int checkrootcommand(String string)
// TODO Auto-generated method stub
Process exec = null;
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();
finally
if (exec != null)
try
exec.destroy();
catch (Exception ignored)
return 1; //returns one when the command execution fails
您可能无法普遍检测手机是否已root,但您应该能够请求然后确认您的应用程序是否可以通过以root身份运行id来访问root,例如su -c id
验证命令是否成功执行并且输出包含 uid=0
即 root 用户的 uid。
【讨论】:
【参考方案3】:方法一:应用请求ROOT权限:
将此添加到您的应用级 gradle 构建文件中:
dependencies
compile 'eu.chainfire:libsuperuser:201501111220'
现在,
System.out.println(Shell.Su.available());
//Outputs true if user-granted else false
方法2:应用程序不要求ROOT:
boolean deviceisRooted()
String[] filespaths = "/system/app/Superuser.apk","/sbin/su", "/system/bin/su","/system/xbin/su";
for (String xyz : filespaths)
if (new File(xyz).exists()) return true;
return false;
System.out.prinln(deviceisRooted());
//Outputs true if device is ROOTED else false
//Doesn't asks user
//Also returns true IF NOT PROPERLY ROOTED (But ROOTED somehow)
【讨论】:
【参考方案4】:使用此代码:
Process executor = Runtime.getRuntime().exec("su -c ls /data/data");
executor.waitFor();
int iabd = executor.exitValue();
if(iabd != 0) /*process exit value is not 0, so user is not root*/ else /* user is root*/
【讨论】:
【参考方案5】:您可以通过终端命令实现此目的,也可以在应用程序中运行终端命令。
if [ ! -z "$(/system/bin/ps -A | grep -v grep | grep -c daemonsu)" ]; then echo "device is rooted"; else echo "device is not rooted"; fi
您的应用程序也不需要这种方式的 root 访问权限。
【讨论】:
以上是关于如何在android中检查root访问权限?的主要内容,如果未能解决你的问题,请参考以下文章
安全研究 | CentOS 7系统利用suid提权获取Root Shell
如何在没有 root 访问权限的情况下获取已安装应用程序的 APK?
堆栈冲突漏洞,Linux及其他UNIX操作系统的root访问权拱手让人!
Android 4.1 SuperSU Shell 不授予 root 访问权限