本章Android 根目录介绍了用于根目录 Android 设备的典型技术。我们将从生根的基础知识及其优缺点开始。然后,我们将继续讨论各种 Android 分区布局、引导加载程序、引导加载程序解锁技术等主题。本章为那些想要在设备上生根的人提供了指导,他们希望在继续之前了解生根概念的来龙去脉。
以下是我们将在本章中讨论的一些主要主题:
- 什么是生根?
- 利与弊
- 锁定和解锁引导加载程序
- 库存回收和定制回收
- 安卓设备的根
Android 构建在 Linux 内核之上。在基于 Unix 的机器(如 Linux)中,我们看到两种类型的用户帐户–普通用户帐户和根帐户。正常的用户帐户通常具有较低的权限,他们需要 root 用户的权限才能执行特权操作,如安装工具、更改操作系统等。而根帐户拥有应用更新、安装软件工具、运行任何命令等所有特权。从本质上说,该帐户对整个系统具有细粒度控制。这种特权分离模型是 Linux 的核心安全特性之一。
如前所述,Android 是构建在 Linux 内核之上的操作系统。我们在传统 Linux 系统中看到的许多功能也将出现在 Android 设备中。特权分离就是其中之一。当您购买全新的 Android 设备时,从技术上讲,您不是设备的所有者,这意味着您对设备的控制有限,无法执行根帐户可能执行的特权操作。因此,通过获得根访问来获得对设备的完全控制被称为根访问。
检查设备是否具有 root 访问权限的一种简单的方法是在 adb 外壳上运行su
命令。su
是 Unix 以其他用户的权限执行命令的方式:
shell@android:/ $ su
/system/bin/sh: su: not found
127|shell@android:/ $
正如我们在前面的摘录中所看到的,我们在设备上没有根访问权限。
在根设备上,我们通常有 UID 值0
,根 shell 有#
而不是$
表示根帐户。如下所示:
shell@android:/ $ su
root@android:/ # id
uid=0(root) gid=0(root)
root@android:/ #
正如前面提到的,由于硬件制造商和运营商的限制,我们无法完全控制 Android 设备。因此,根设备为我们提供了克服这些限制的额外特权。
然而,为设备生根的目标可能因人而异。例如,有些人通过安装自定义 ROM,使他们的设备具有更漂亮的主题、更好的外观和感觉,等等。有些人可能希望安装称为根应用的附加应用,这些应用在没有根访问权限的情况下无法安装。同样,其他人也可能有其他原因。在我们的案例中,我们将根设备用于渗透测试,因为根设备使我们能够完全控制文件系统和其他应用,如 Cydia Subscriber,可以安装这些应用来审核应用。
不管是什么原因,生根都有它自己的优点和缺点。下面将介绍其中的一些。
本节介绍了在 Android 设备上生根的一些优势。
默认情况下我们无法作为普通用户完全访问设备。在为安卓设备建立根目录后,我们可以完全控制该设备。让我们看看下面的例子。以下摘录显示,没有 root 访问权限的普通用户无法看到/data/data
目录中已安装应用包的列表:
shell@android:/ $ ls /data/data
opendir failed, Permission denied
1|shell@android:/ $
作为根用户,我们可以浏览完整的文件系统,修改系统文件,等等。
以下摘录显示 root 用户可以在/data/data
目录中看到已安装应用包的列表:
shell@android:/ $ su
root@android:/ # ls /data/data
com.android.backupconfirm
com.android.bluetooth
com.android.browser
com.android.calculator2
com.android.calendar
com.android.certinstaller
com.android.chrome
com.android.defcontainer
com.android.email
com.android.exchange
在设备上具有 root 访问权限的用户可以安装一些具有特殊功能的应用。这些通常被称为根应用。例如,BusyBox是一款提供更多有用 Linux 命令的应用,这些命令在 Android 设备上默认不可用。
通过在 Android 设备上安装自定义恢复和自定义 ROM,我们可以获得比供应商提供的库存操作系统更好的功能和自定义。
本节描述了安卓设备根目录的各种缺点,以及终端用户根目录设备的危险性。
一旦设备扎根,就会危及设备的安全性。
默认情况下,每个应用都在自己的沙箱中运行,并为其分配了单独的用户 ID。此用户 id 隔离可确保设备上运行 UID 的一个应用无法访问同一设备上运行不同 UID 的其他应用的资源或数据。在根设备上,具有根访问权限的恶意应用将没有此限制,因此它可以从设备上运行的任何其他应用读取数据。其他一些例子是绕过锁屏,从被盗/丢失的设备中提取所有数据,如短信、通话记录、联系人和其他特定于应用的数据。
让我们看一个实际的例子。 content://sms/draft
是 Android 中的内容提供商 URI,用于从设备访问短信草稿。要让设备上的任何应用通过此 URI 访问数据,它需要用户的READ_SMS
权限。当应用试图在没有适当权限的情况下访问此文件时,将导致异常。
使用 adb 通过 USB 打开 shell,并使用有限的用户 shell(无 root 访问权限)键入以下命令:
shell@android:/ $ content query --uri content://sms/draft
Error while accessing provider:sms
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.telephony.SemcSmsProvider from (null) (pid=4956, uid=2000) requires android.permission.READ_SMS or android.permission.WRITE_SMS
at android.os.Parcel.readException(Parcel.java:1425)
at android.os.Parcel.readException(Parcel.java:1379)
at android.app.ActivityManagerProxy.getContentProviderExternal(ActivityManagerNative.java:2373)
at com.android.commands.content.Content$Command.execute(Content.java:313)
at com.android.commands.content.Content.main(Content.java:444)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:293)
at dalvik.system.NativeStart.main(Native Method)
shell@android:/ $
正如我们在前面的摘录中所看到的,它抛出了一个异常,表示权限被拒绝。
现在,让我们看看使用根 shell 查询同一 URI 时的情况:
shell@android:/ $ su
root@android:/ # content query --uri content://sms/draft
Row: 0 _id=1, thread_id=1, address=, person=NULL, date=-1141447516, date_sent=0, protocol=NULL, read=1, status=-1, type=3, reply_path_present=NULL, subject=NULL, body=Android Rooting Test, service_center=NULL, locked=0, sub_id=0, error_code=0, seen=0, semc_message_priority=NULL, parent_id=NULL, delivery_status=NULL, star_status=NULL, delivery_date=0
root@android:/ #
正如我们在前面的输出中所看到的,我们不需要向用户寻求任何权限就可以使用 root 权限读取 SMS,从而损害设备上应用的数据。很常见的情况是,根应用在设备上执行 shell 命令来窃取敏感文件,如mmssms.db
。
根进程可能会阻塞您的设备。你能用砖头做什么?同样的道理也适用于一个被砖块覆盖/死掉的 Android 设备,这意味着它可能会变得毫无用处,你需要找到一种方法将其取回。
植根的设备无效保修。大多数制造商不为根设备提供免费支持。安装设备后,即使您处于保修期内,也可能会要求您支付维修费用。
引导加载程序是启动设备时运行的第一个程序。引导加载程序负责启动您的硬件和 Android 内核。没有这个程序,我们的设备无法启动。那些设备制造商通常编写引导加载程序,因此它们通常是锁定的。这确保最终用户无法对设备固件进行任何更改。要在设备上运行自定义映像,必须先解锁引导加载程序,然后才能继续。即使当您想使用锁定的引导加载程序根设备时,如果有可能且可用的方法,也需要首先解锁该设备。一些制造商提供了一种官方方法来解锁引导加载程序。在下一节中,我们将看到如何在索尼设备上解锁引导加载程序。如果无法解锁引导加载程序,我们将必须找到一个允许我们根设备的缺陷。
正如前面提到的,一些制造商提供了一种官方方法来解锁引导加载程序。
特别是在索尼设备上,我们可以键入以下代码并按照所示步骤进行操作:
*#*#7378423#*#*
注:这些设备代码可能因制造商而异,如果它们提供支持,可以从相应的制造商处获得。
当我们在索尼设备上键入上述号码时,会打开以下屏幕:
-
Click the Service Info button. It shows the following screen:
-
Click the Configuration button to see the status of your boot loader. If boot loader unlock is supported by the vendor, it will show the following output under Rooting status:
-
If the boot loader is already unlocked, then it will show the following output:
以下步骤显示了解锁 Sony 设备上的引导加载程序的过程。这让我们了解了供应商如何为解锁设备上的引导加载程序提供支持:
-
检查是否支持引导加载程序解锁。这在前面已经展示过。
-
Open up the following link:
http://developer.sonymobile.com/unlockbootloader/unlock-yourboot-loader/
-
Choose the device model and click Continue:
-
This then shows us a prompt for entering an e-mail address. Enter a valid email address here:
-
After entering a valid email address, click the Submit button. We should receive an email from Sony as shown in the following screenshot:
-
The email consists of a link that takes us to another link, where Sony verifies the IMEI number of the device whose boot loaders have to be unlocked. Enter your IMEI number here:
-
This IMEI number is required to generate the unlock code. Once we enter a valid IMEI number and click Submit, we should be greeted with a screen with an unlocking code followed by the steps to unlock:
-
一旦我们收到引导加载程序解锁代码,我们将以 fastboot 模式连接设备。进入 fastboot 模式的步骤可能因型号而异。大多数情况下,这是不同的,必须按下哪些硬件键才能进入 fastboot 模式。
对于索尼设备,请执行以下步骤:
- 关闭设备电源。
- 将 USB 电缆连接到设备。
- 按住音量增大按钮,将 USB 电缆的另一侧连接到笔记本电脑。
这些步骤应在 fastboot 模式下将设备连接到笔记本电脑。
我们可以使用以下命令检查连接的设备:
fastboot devices
设备以 fastboot 模式连接后,我们可以使用供应商提供的解锁代码运行以下命令来解锁设备:
前面的代码显示引导加载程序解锁已完成。
虽然这里的过程是专门针对索尼设备显示的,但这与大多数官方制造商的方法几乎相同。
警告:此过程有时可能会损坏您的设备。在写这本书的时候,制造商提供的这个引导加载程序解锁过程让我的索尼设备进入了引导循环。看一下堆栈溢出问题,我们注意到这发生在这些模型(C1504、C1505)上的许多其他人身上。后来,我们不得不使用供应商提供的库存操作系统闪存设备,以使设备重新工作。最后,它是安全的!除此之外,未锁定的引导加载程序只不过是一扇没有锁的门。因此,攻击者有可能从丢失/被盗的设备中窃取所有数据。
在这一节中,我们将讨论如何为使用三星定制版 Android 操作系统的解锁三星 note 2 设置根目录,我们还将了解库存恢复和自定义恢复之间的区别,最后我们将在 note 2 设备上安装自定义 ROM。
Android 的恢复功能对于科技用户和那些只使用手机打电话和定期冲浪的用户来说都是最重要的概念之一。当用户获得设备更新并应用它时,Android 的恢复系统通过替换现有图像来确保正确完成更新,并且不会影响用户数据。
通常由制造商提供的库存恢复图像在本质上是有限的。它包含很少的函数,允许用户执行诸如擦除缓存、用户数据和执行系统更新等操作。我们可以将设备引导到恢复模式,以执行指定的任何操作,如擦除缓存。用于引导到恢复模式的步骤/硬件密钥可能因制造商而异。
另一方面,自定义恢复提供了更多功能,如允许未签名的更新包,选择性地擦除数据;进行备份和设置恢复点,将文件复制到 SD 卡上,等等。ClockWorkMod 是一种流行的恢复图像,可以作为自定义恢复图像的示例显示。
如前所述,一些制造商提供了一种官方方法来解锁引导加载程序,而有些则是解锁的。如果你买了一部未锁定的手机,但它不是合同上的,那么你很可能有一个未锁定的引导加载程序。
警告:根目录和自定义 ROM 安装总是有数据丢失的风险,最糟糕的是,会阻塞手机,因此在继续根目录之前,您应该始终备份数据。您可以使用谷歌的同步数据选项或任何第三方应用备份数据/联系人等。
在开始我们的手机Root之旅之前,确保您具备以下先决条件:
-
Download Samsung USB driver from the following URL and install it on your computer:
http://developer.samsung.com/technical-doc/view.do?v=T000000117
-
You also need to enable USB debugging by following this path: Settings | Developer options | USB debugging. Your screen might be slightly different based on the Android version you are using, but look for USB debugging and check it:
如果您没有看到利用者选项,您可以通过以下路径启用它:设置关于手机构建编号(点击几次,通常是七到九次),然后返回菜单,您将看到【T8 利用者选项】如下图所示。
-
确保您的路径上有 adb,如本章前面的所示,Android Studio 在当前用户的
AppData
文件夹Android****平台工具下安装 Android SDK。打开命令提示符并键入adb
进行检查。 -
Connect the phone to the USB cable and type
adb devices
to check if the device is recognized: -
一旦您插入电缆,您可能会得到授权弹出窗口允许 USB 调试,请允许。
自定义 ROM 安装是一个三步过程,但是,如果您只对设备根目录感兴趣,不想安装自定义 ROM,则只需执行步骤 1 和步骤 2。以下是安装自定义 ROM 所涉及的步骤:
- 安装恢复软件,如 TWRP 或 CF。
- 安装超级 Su 应用。
- 将自定义 ROM 闪存到手机。
以下是安装 TWRP 或 CF 等恢复软件的两种常用方法:
- 使用奥丁
- 使用 Heimdall
在继续之前,我们需要从以下 URL 下载注释 2 的 TWRP 恢复 TAR 文件和 IMG,并将其保存在电话根目录下:
Odin 是三星设备最流行的恢复工具之一。本节显示了使用 Odin 的步骤:
-
从以下 URL 下载 Odin 3.09 压缩包,并将其解压缩到复制 TWRP 的同一文件夹中:http://odindownload.com/Samsung-Odin/#.VjW0Urcze7M
-
Click on Odin 3.09 to open it and you should see the following screen:
注意:确保扫描 EXE 文件中的病毒。作者有https://virustotal.com/ 确保它没有病毒。
-
我们需要关闭智能手机,同时按下音量增大、主页和电源按钮,将设备置于下载模式。
-
设备进入下载模式后,使用 USB 数据线将设备连接到计算机。
-
You will see a warning, accept the Continue option by pressing the volume up button. If you have installed the right USB drivers you will see Odin's ID:COM in blue text as shown in the following screenshot. Otherwise you need to reinstall the driver or check your cable for any fault:
-
Click on the AP button and select the TWRP recovery image file in Odin3 by clicking on the AP button. Make sure you enable Auto Reboot and F. Reset Time as shown here:
-
Now, click on the Start button in Odin3 to flash TWRP. It will take a few seconds to complete and if everything went well, you should see PASS! in green as shown in the following screenshot. Once the process is complete your phone will restart automatically:
-
现在您已经成功闪现了 TWRP 恢复。
本节显示了使用 Heimdall 的步骤:
-
从下载并安装 Heimdall 套件 http://glassechidna.com.au/heimdall/#downloads 。
-
Extract the Heimdall ZIP file and remember the directory, which is
heimdall.exe
: -
Open the command prompt in the directory and type
heimdall.exe
to check if Heimdall is working properly, you should see the following output:注意:如果您有任何错误,请确保您的计算机上安装了微软 Visual C++ 2012 可重分发软件包(x86/32 位)。
-
同时按下音量降低、主页和电源按钮,关闭手机并进入下载模式。收到警告信息后,请按音量升高继续。
-
Run
zadig.exe
which is present in Heimdall Suite's drivers directory: -
点击选项菜单,选择列出所有设备。
-
从下拉列表中选择三星USB 复合设备或 gadget serial或您的可用设备。如果遇到任何问题,请尝试从系统中卸载三星 USB 驱动程序或 KIE。
-
Click Install Driver and you should see the following screen:
-
在继续进行闪存恢复之前,请确保阅读 Heimdall 网站(上的最新说明 https://github.com/Benjamin-Dobell/Heimdall/tree/master/Win32 )最近的任何变更。返回步骤 3 中打开的命令提示符,执行以下命令:
heimdall flash --RECOVERY "..\Phone Rooting\twrp-2.8.7.0-t03g.img" --no-reboot
-
重新启动,就这样,我们完成了。
本节解释三星 Note 2 根目录的逐步过程:
-
从以下 URL 下载 SuperSU 并保存在手机根目录中:https://download.chainfire.eu/396/supersu/ 。
-
使用 USB 电缆将设备连接到计算机,并使用
adb push
命令将文件复制到/sdcard
并在完成后拔下电缆:C:\..\Phone Rooting> adb push UPDATE-SuperSU-v1.94.zip /sdcard
-
Switch off your device and boot it into the recovery mode by pressing the volume up, home, and power buttons simultaneously. You will see the Team Win Recovery Project (TWRP) screen, click on Install:
-
选择更新后的 Super-Su-Zip文件开始闪烁过程。
-
一旦安装完成,您将看到安装完成消息。点击重启系统重启手机。
-
Once your phone starts, you should see SuperSU added to your phone as shown following:
-
Connect to the device from the system using a USB cable and check if you can login as a root user by typing the following commands:
adb shell Su
祝贺您,您已成功将您的设备植根。
在部分中,我们将了解安装非常流行的定制 ROMCyanogenMod 11所涉及的安装步骤(该 ROM 将不断更新原始 Google Android 版本):
-
从以下 URL 下载 CyanogenMod 并将其保存在
Phone Rooting
目录中。我已经从下载了最新的 GSM 非 LTE 版本cm-11-20151004-NIGHTLY-n7100.zip
https://download.cyanogenmod.org/?device=n7100 。 -
使用 USB 电缆将设备连接到计算机,并使用
adb push
命令将文件复制到/sdcard
并拔下电缆。您也可以在系统的 Windows 资源管理器中打开设备进行拖放:C:\..\Phone Rooting> adb push cm-11-20151004-NIGHTLY-n7100.zip /sdcard
-
Switch off your device and boot it into the recovery mode by pressing the volume up, home, and power buttons simultaneously. You will see the TWRP screen, click on Install:
-
Select the Wipe option from the menu and Swipe to Factory Reset which clears the cache,data, and Dalvik VM:
-
You should see the Factory Reset Complete successful message as shown in the following screenshot:
-
Click the Back button and select Install. Select the
cm-11-20151004-NIGHTLY-n7100.zip
file as shown in the following screenshot: -
Once you select the ROM, the following screen will be displayed:
-
现在点击滑动确认闪烁开始自定义 ROM 的闪烁过程。
-
Once the installation is complete, you will see the Zip Install Complete message as shown following screenshot. Click on Reboot System to reboot the phone:
-
Once the device starts, you should see the following CyanogenMod screen:

- You will then see the usual Android system, set it up according to your liking and if you like to use Google Play Store, download and install it by following the same process as we have described here, and if you install GAPPS, make sure you install the updated SuperSU again. Finally, your screen should look like the following screenshot. You can find GAPPS at the following location:
[http://opengapps.org/?api=4.4 &变体=纳米](http://opengapps.org/?api=4.4&variant=nano)

- 使用 USB 电缆将从系统连接到设备,并检查您是否可以以 root 用户身份登录。
在本节中,我们了解了如何安装恢复软件 TWRP,如何使用 TWRP 为 Android 设备设置根目录,以及最后如何在智能手机上安装自定义 ROM。
这一过程与其他手机类似,但是,您必须确保使用的是正确版本的 CM、TWRP。
在本章中,我们讨论了锁定和解锁引导加载程序的概念,以及如何解锁锁定引导加载程序。我们讨论了根,它的优点和缺点,包括它在执行安全分析时提供的访问所有数据的能力。一旦我们建立了一个设备的根目录,我们就可以获得对设备文件系统的完全访问权,并探索设备上每个应用的内部结构和相关数据。我们将在本书后面的章节中探讨这些问题。我们还了解了如何根设备以及在 Android 设备上安装自定义 ROM 的步骤。