【问题标题】:How to grant root access without SuperSU如何在没有 SuperSU 的情况下授予 root 访问权限
【发布时间】:2017-02-21 17:43:37
【问题描述】:

我目前能够在使用 SuperSU 的根设备上的应用程序中获得根访问权限。当我的应用程序请求 root 访问权限时,会显示来自 SuperSu 的对话框,然后用户单击“授予”以允许权限。

我希望能够绕过这一步,让我的应用程序以编程方式获得 root 权限,而无需单击 SuperSu 对话框上的“授予”按钮。

我该怎么做?

【问题讨论】:

  • 嗯,是不是因为一个非常具体的原因,用户应该明确授予该权限?
  • 是的,理想情况下用户应该授予该权限。在这种情况下,我希望自动授予权限,因为 SuperSU 可以做到,那么我的应用程序应该也能做到吗?
  • 我的意思是,如果用户需要明确授予他的权限,那么自动获得该权限可能会被视为恶意行为,这是因为他应该知道您的应用程序需要 root 权限。或者我只是太怀疑了......
  • 是的,我知道这一点。我不想详细说明业务需求、我们的用户以及为什么我需要自动授予它。该应用程序未公开分发(Play 商店),它预安装在已植根的设备上,然后可供用户访问。这不应该是关于我为什么要自动授予 root 访问权限的讨论,而是我应该如何去做的讨论。
  • 我只是在检查,我认为如果您寻求帮助来做一些非法的事情,就应该知道,仅此而已。顺便说一句,我没有投反对票

标签: android


【解决方案1】:

我希望能够绕过这一步,让我的应用程序以编程方式获得 root 权限,而无需单击 SuperSu 对话框上的“授予”按钮。

你不能。用户必须授予您的应用 root 访问权限。除非您构建自己的 SU 管理应用程序,否则您无法绕过此初始权限请求。

我正在为设备提供应用程序,然后用户使用该设备(我拥有)。只是为了提供更多上下文,当我发布设备时,它已经拥有应用程序并且已经被 SuperSu 授予 root 访问权限。因此,我要避免的是应用程序可能会失去许可,然后在现场再次请求许可的情况。用户不精通技术,他们不知道 SuperSu 对话框是什么或如何处理它。

如果您拥有该设备,则可以在 SuperSu 设置中将“默认访问权限”更改为“授予”。您也可以针对每个应用进行更改。

您在 cmets 中提到您的应用已经拥有超级用户权限。您拥有 root 访问权限,因此您可以更改 SuperSu 的共享首选项,让您的应用始终被授予访问权限。同样,您暗示您拥有该设备,因此您不需要以编程方式修改 SuperSu 首选项。

如果您确实需要修改 SuperSu 的首选项,则以下代码应更改应用的访问设置以始终允许和禁用通知(使用 android-shell):

@WorkerThread
public static boolean tryChangingSuperSuDefaultAccess(Context context) throws Exception {
  String packageName = context.getPackageName();
  PackageManager pm = context.getPackageManager();

  // Get the preferences for SuperSu
  Context packageContext = context.createPackageContext("eu.chainfire.supersu", 0);
  SharedPreferences superSuPrefs = PreferenceManager.getDefaultSharedPreferences(packageContext);
  File superSuPrefsFile = getSharedPreferencesFile(superSuPrefs);

  // Copy SuperSu preferences to our app's shared_prefs directory
  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
  File directory = getSharedPreferencesFile(preferences).getParentFile();
  File destination = new File(directory, "eu.chainfire.supersu.xml");
  int uid = pm.getApplicationInfo(context.getPackageName(), 0).uid;
  destination.getParentFile().mkdirs();
  Shell.SU.run("cp \"" + superSuPrefsFile + "\" \"" + destination + "\"");
  Shell.SU.run("chmod 0660 \"" + destination + "\"");
  Shell.SU.run("chown " + uid + " " + uid + " \"" + destination + "\"");

  // Now we can edit the shared preferences
  superSuPrefs = context.getSharedPreferences("eu.chainfire.supersu", Context.MODE_PRIVATE);

  SharedPreferences.Editor editor = superSuPrefs.edit();
  editor.putString(String.format("config_%s_notify", packageName), "no"); // disable SuperSu notifications
  editor.putString(String.format("config_%s_access", packageName), "grant"); // Set access to grant for this app
  // noinspection all
  editor.commit();

  // Copy the edited shared preferences back
  return Shell.SU.run("cp \"" + destination + "\" \"" + superSuPrefsFile + "\"").isSuccessful();
}

private static File getSharedPreferencesFile(SharedPreferences preferences)
    throws NoSuchFieldException, IllegalAccessException {
  Field field = preferences.getClass().getDeclaredField("mFile");
  if (!field.isAccessible()) field.setAccessible(true);
  return (File) field.get(preferences);
}

我不建议使用上面的代码。您应该保持透明并教育您的用户。如果您声称拥有该设备,则可以使用该应用自行更改这些设置。

【讨论】:

  • 但是 SuperSU 如何获得 root 访问权限?应用程序不能使用相同的机制吗?
  • 我也想知道。对我们来说,问题是 SuperSu 配置在出厂重置之间不会持续存在。我们还提供基于现成 Android 平板电脑的专用设备(带有不同的启动器,因此它只是一个应用程序)
猜你喜欢
  • 2012-09-09
  • 1970-01-01
  • 2012-07-09
  • 2011-10-23
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
  • 2023-02-23
  • 1970-01-01
相关资源
最近更新 更多