验证方法

本页介绍了一种方法(具有最高的置信度),可确保您在 Pixel 设备上运行的是正版 Pixel 映像。此方法 包括将出厂映像下载到计算机上,并 在将下载的工件刷写到设备上之前对其执行日志包含证明

如何验证 Pixel 出厂映像

通过验证需要满足的前提条件

在继续验证工件(固件或 APK)是否符合声明之前,请确保网络连接的计算机上提供以下工具。 根据验证目标,您可能不需要所有这些工具。

Golang

验证工具 是用 Go 语言编写的。如需构建该工具,请从 Go 网站安装 Go 1.24 或更高版本 。

Android 调试桥 (ADB)

ADB 是一种与 Android 设备通信以检查映像或 提取 APK 的工具,可在 Android SDK 平台工具 网站上找到。

包含证明验证程序

包含证明验证程序是一个 Go 模块,用于查询透明度日志并输出工件是否包含在日志中。 它发布在 android-binary-transparency GitHub 代码库中,网址为 https://github.com/android/android-binary-transparency/tree/main

如需克隆 AOSP avb 代码库,请执行以下命令:

computer:~$ git clone https://android.googlesource.com/platform/external/avb

如需克隆 android-binary-transparency GitHub 代码库,请执行以下命令:

computer:~$ git clone https://github.com/android/android-binary-transparency

AAPT2

AAPT2(Android 资源打包工具)是一种构建工具,用于编译和打包 Android 应用的资源。 它可以在 Android SDK Build Tools 26.0.2 及更高版本中作为独立工具找到。

bundletool

bundletool 是一种用于构建 Android App Bundle (AAB) 的工具。它还可用于验证 APK 代码签名。 您可以从 GitHub 下载该工具。

wget 或 curl

wgetcurl 是可用于从互联网检索或下载文件的工具。 请务必安装其中一种工具,因为验证脚本可能依赖于这些工具。

Avbtool

avbtool 用于计算出厂映像的 VBMeta 摘要和验证 二进制文件。 克隆 avb 代码库(如包含证明验证程序 部分所示)后,您可以在克隆的 avb 目录中找到 avbtool.py。 您应将此目录添加到 $PATH 环境变量中。

computer:~$ PATH=$PATH:DIRECTORY_CONTAINING_AVBTOOL

Fbpacktool

fbpacktool 是一个 Python 脚本,用于解压缩 Pixel 的 bootloader.img中的各个组件。 如果您要验证 APK,则不需要此脚本。 此工具非常重要,因为它可让 avbtool.py 发现验证所需的映像。

如需使用此工具,您应将以下三个 Python 脚本 下载到 avbtool.py 所在的目录中, 使 fbpacktool.py 可执行, 并创建一个名为 fbpacktool 的符号链接到 fbpacktool.py,以方便使用。

如果您有 wget,请执行以下命令:

computer:dir_to_avbtool$ wget https://source.android.com/devices/bootloader/tools/pixel/fw_unpack/fbpacktool.py \
https://source.android.com/devices/bootloader/tools/pixel/fw_unpack/fbpack.py \
https://source.android.com/devices/bootloader/tools/pixel/fw_unpack/packedstruct.py
computer:dir_to_avbtool$ chmod +x fbpacktool.py
computer:dir_to_avbtool$ ln -s fbpacktool.py fbpacktool

如果您有 curl,请执行以下命令:

computer:dir_to_avbtool$ curl -O https://source.android.com/devices/bootloader/tools/pixel/fw_unpack/fbpacktool.py
computer:dir_to_avbtool$ curl -O https://source.android.com/devices/bootloader/tools/pixel/fw_unpack/fbpack.py
computer:dir_to_avbtool$ curl -O https://source.android.com/devices/bootloader/tools/pixel/fw_unpack/packedstruct.py
computer:dir_to_avbtool$ chmod +x fbpacktool.py
computer:dir_to_avbtool$ ln -s fbpacktool.py fbpacktool

OpenSSL

openssl 有助于在验证工作流中使用终端执行各种加密 操作。 请确保它已安装在您的系统上。 您可以在终端中运行以下命令并确保返回的结果不为空,以进行检查:

computer:~$ which openssl
/usr/bin/openssl

构建用于验证的载荷

如需验证设备上的映像或从 Pixel 出厂映像网站下载的映像,您必须先从 build 指纹和 VBMeta 摘要构建与该映像对应的载荷。有两种选项:手动或自动,如下所述。

  • build 指纹:出厂映像的字符串标识符
    • 示例: google/oriole/oriole:12/SD1A.210817.015.A4/7697517:user/release-keys
  • VBMeta 摘要:将出厂映像的不同部分联系在一起的 SHA-256 加密摘要
    • 示例: 4d5e41b7c675a821de81f7d2c744623fe808c8c2d3e19a83e894dab5d0829dbe

手动构建载荷

如需手动生成用于验证的载荷,您需要创建一个文本 文件,payload.txt其中包含一行 build 指纹,然后是下一行的 VBMeta 摘要,并包含一个空的新行(请参阅 日志内容部分,了解 确切格式)。

按照 出厂映像网站中的步骤操作后, payload.txt文件的内容应如下所示:

google/oriole/oriole:12/SD1A.210817.015.A4/7697517:user/release-keys
4d5e41b7c675a821de81f7d2c744623fe808c8c2d3e19a83e894dab5d0829dbe
从 Pixel 出厂映像网站提取 VBMeta 摘要和 build 指纹

Pixel 出厂映像网站下载出厂映像时,您 需要解压缩下载的文件,这会在 原位置创建另一个文件夹。前往新创建的文件夹,然后解压缩关联的 image-*.zip 文件以获取各个 *.img 文件。这些二进制文件是分区映像。确保 img 文件中存在 vbmeta.img 文件。 您应会看到类似下图的内容:

computer:oriole-sd1a.210817.015.a4$ unzip image-oriole-sd1a.210817.015.a4.zip
Archive:  image-oriole-sd1a.210817.015.a4.zip
  inflating: android-info.txt
  inflating: boot.img
  inflating: vendor_boot.img
  inflating: system.img
  inflating: vendor.img
  inflating: product.img
  inflating: system_ext.img
  inflating: system_other.img
  inflating: dtbo.img
  inflating: vbmeta_system.img
  inflating: vbmeta.img
  inflating: super_empty.img

您可以提取 build 指纹并计算所有下载文件的 VBMeta 摘要。

avbtool.py 可用于 $PATH 后,您可以在终端中发出以下命令,以提取 build 指纹并计算刚刚下载的出厂映像的 VBMeta 摘要:

computer:dir_containing_unzipped_factory_image~$ grep -a 'ro\..*build\.fingerprint=google/.*/release-keys' system.img | cut -f2 -d'='
google/oriole/oriole:12/SD1A.210817.015.A4/7697517:user/release-keys
computer:dir_containing_unzipped_factory_image$ avbtool.py calculate_vbmeta_digest --image vbmeta.img
4d5e41b7c675a821de81f7d2c744623fe808c8c2d3e19a83e894dab5d0829dbe
手动验证下载的出厂映像

为了正确验证下载的出厂映像,您需要 下载 fbpacktool.py 和它所依赖的其他两个 Python 文件 (fbpack.pypackedstruct.py) 以解压缩 bootloader.img 中的组件,因为它们在 VBMeta 结构中单独描述 。获得解压缩程序后,您可以使用 fbpacktool.py 解压缩引导加载程序映像。例如:

computer:dir_containing_unzipped_images$ python3 fbpacktool.py unpack bootloader-oriole-slider-1.0-7683913.img

您应该能够在该目录中看到其他 .img 文件。 现在,您可以继续使用 avbtool.py 手动验证各个分区的签名是否匹配。

computer:dir_containing_unzipped_images$ avbtool.py verify_image --image vbmeta.img --follow_chain_partitions
Verifying image vbmeta.img using embedded public key
vbmeta: Successfully verified SHA256_RSA4096 vbmeta struct in vbmeta.img
vbmeta_system: Chained but ROLLBACK_SLOT (which is 1) and KEY (which has sha1 df529646b7225015196a714006346f0768b87fcf) not specified
--
Verifying image vbmeta_system.img using embedded public key
vbmeta: Successfully verified SHA256_RSA4096 vbmeta struct in vbmeta_system.img
product: Successfully verified sha256 hashtree of product.img for image of 2700468224 bytes
system: Successfully verified sha256 hashtree of system.img for image of 878493696 bytes
system_ext: Successfully verified sha256 hashtree of system_ext.img for image of 251334656 bytes
vbmeta_vendor: Chained but ROLLBACK_SLOT (which is 3) and KEY (which has sha1 85322346680a860c091fa14a64cef1fe4a3ffe31) not specified
--
Verifying image vbmeta_vendor.img using embedded public key
vbmeta: Successfully verified SHA256_RSA4096 vbmeta struct in vbmeta_vendor.img
vendor: Successfully verified sha256 hashtree of vendor.img for image of 472940544 bytes
boot: Chained but ROLLBACK_SLOT (which is 2) and KEY (which has sha1 85322346680a860c091fa14a64cef1fe4a3ffe31) not specified
--
Verifying image boot.img using embedded public key
vbmeta: Successfully verified footer and SHA256_RSA4096 vbmeta struct in boot.img
boot: Successfully verified sha256 hash of boot.img for image of 24481792 bytes
abl: Successfully verified sha256 hash of abl.img for image of 1744896 bytes
bl1: Successfully verified sha256 hash of bl1.img for image of 12288 bytes
bl2: Successfully verified sha256 hash of bl2.img for image of 544768 bytes
bl31: Successfully verified sha256 hash of bl31.img for image of 86016 bytes
dtbo: Successfully verified sha256 hash of dtbo.img for image of 2152234 bytes
gsa: Successfully verified sha256 hash of gsa.img for image of 262144 bytes
ldfw: Successfully verified sha256 hash of ldfw.img for image of 4096000 bytes
pbl: Successfully verified sha256 hash of pbl.img for image of 49152 bytes
tzsw: Successfully verified sha256 hash of tzsw.img for image of 4304896 bytes
vendor_boot: Successfully verified sha256 hash of vendor_boot.img for image of 25718784 bytes
vendor_dlkm: Successfully verified sha256 hashtree of vendor_dlkm.img for image of 54505472 bytes

如果您没有看到任何错误或失败消息,则表示下载的映像的 VBMeta 现已成功验证。

自动构建载荷

为方便起见,我们还提供了一个 Python 脚本供您使用。借助此工具,您只需指定出厂映像的网址,而无需手动提取和解压缩文件。

computer:dir_to_avb_repo/tools/transparency$ python3 ./pixel_factory_image_verify.py https://dl.google.com/dl/android/aosp/oriole-sd1a.210817.015.a4-factory-074b7f51.zip
Fetching file from: https://dl.google.com/dl/android/aosp/oriole-sd1a.210817.015.a4-factory-074b7f51.zip
Successfully downloaded file.
Successfully unpacked factory image.
Successfully unpacked factory image partitions.
Successfully unpacked bootloader image.
Successfully verified VBmeta.
Successfully extracted build fingerprint.
Successfully calculated VBMeta Digest.
The build fingerprint for factory image is: google/oriole/oriole:12/SD1A.210817.015.A4/7697517:user/release-keys
The VBMeta Digest for factory image is: 4d5e41b7c675a821de81f7d2c744623fe808c8c2d3e19a83e894dab5d0829dbe

A corresponding "payload.txt" file has been created.

执行上述命令将下载出厂映像软件包、解压缩软件包并验证已签名分区的签名。验证成功后,它将创建一个名为 payload.txt 的新文件,其内容应与以下内容类似:

google/oriole/oriole:12/SD1A.210817.015.A4/7697517:user/release-keys
4d5e41b7c675a821de81f7d2c744623fe808c8c2d3e19a83e894dab5d0829dbe

成功创建 payload.txt 后,您就可以开始执行 验证 映像包含步骤。

验证映像包含(包含证明)

构建载荷后,设置一个 shell 变量(例如 $PAYLOAD_PATH),使其指向载荷的路径。

包含证明工具已发布在 avb代码库 Android 开源项目中。如需运行该工具,请执行以下操作:

computer:external/avb/tools/transparency/verify$ PAYLOAD_PATH=../payload.txt
computer:external/avb/tools/transparency/verify$ go build cmd/verifier/verifier.go
computer:external/avb/tools/transparency/verify$ ./verifier --payload_path=${PAYLOAD_PATH} --log_type=pixel

验证程序使用检查点和日志内容(位于 tile 目录中)来检查您的映像载荷是否在 透明度日志中,也就是说,它是否由 Google 发布。

该命令的输出将写入 stdout:

  • 如果映像包含在日志中,则输出 OK. inclusion check success!
  • 否则输出 FAILURE