Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

[Patchless] Add BuildManifest Hash Update

Maximilian Paßb5466c205b34
diff --git a/.gitignore b/.gitignore
index 3d74e12e..09387eb3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -117,7 +117,6 @@ share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
-MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
diff --git a/sources/FirmwarePatcher/Filesystem/CryptexFilesystemPatcher.swift b/sources/FirmwarePatcher/Filesystem/CryptexFilesystemPatcher.swift
new file mode 100644
index 00000000..dd914ccf
--- /dev/null
+++ b/sources/FirmwarePatcher/Filesystem/CryptexFilesystemPatcher.swift
@@ -0,0 +1,68 @@
+
+// 1. Collect the AppOS and SystemOS Cryptex from the iPhone BuildManifest
+// 2. With the OS, AppOS, and SystemOS images, attach them and copy them to a target image
+// 3. Create mtree for resulting image
+// 4. Download apfs_sealvolume
+// 5. Generate digest.db
+// 6. Join mtree and digest.db to Ap,SystemVolumeCanonicalMetadata
+// 7. Create SystemVolume root_hash
+
+import Foundation
+import CryptoKit
+import Img4tool
+
+/// Patcher for DeviceTree payloads.
+public final class CryptexFilesystemPatcher: Patcher {
+ public let component = "Manifest"
+ public let restoreDir: URL?
+ public let verbose: Bool
+
+ let buffer: BinaryBuffer
+ var patches: [PatchRecord] = []
+ var rebuiltData: Data?
+
+ // MARK: - Init
+
+ public init(data: Data, restoreDir: URL?, verbose: Bool = true) {
+ buffer = BinaryBuffer(data)
+ self.restoreDir = restoreDir
+ self.verbose = verbose
+ }
+
+ // MARK: - Patcher
+
+ public func findAll() throws -> [PatchRecord] {
+ rebuiltData = nil
+ let root = try parsePayload(buffer.data)
+ let newRoot = try applyPatches(buildManifest: root)
+ rebuiltData = try serializePayload(newRoot)
+
+ patches = [PatchRecord(
+ patchID: "manifest.hash",
+ component: "",
+ fileOffset: 0,
+ originalBytes: Data(),
+ patchedBytes: Data(),
+ description: "Updated the file hashes according to the actual files",
+ )]
+ return patches
+ }
+
+ @discardableResult
+ public func apply() throws -> Int {
+ if patches.isEmpty {
+ let _ = try findAll()
+ }
+ if let rebuiltData {
+ buffer.data = rebuiltData
+ } else {
+ throw PatcherError.patchSiteNotFound("ManifestHash")
+ }
+ return patches.count
+ }
+
+ /// Get the patched data.
+ public var patchedData: Data {
+ buffer.data
+ }
+}
diff --git a/sources/FirmwarePatcher/Manifest/ManifestHashPatcher.swift b/sources/FirmwarePatcher/Manifest/ManifestHashPatcher.swift
new file mode 100644
index 00000000..0c698185
--- /dev/null
+++ b/sources/FirmwarePatcher/Manifest/ManifestHashPatcher.swift
@@ -0,0 +1,150 @@
+// ManifestHashPatcher.swift — ManifestHashPatcher.
+//
+// Update the hash in the firmware manifest according to the actual hash of the corresponding files.
+//
+
+import Foundation
+import CryptoKit
+import Img4tool
+
+/// Patcher for Manifest payloads.
+public final class ManifestHashPatcher: Patcher {
+ public let component = "Manifest"
+ public let restoreDir: URL?
+ public let verbose: Bool
+
+ let buffer: BinaryBuffer
+ var patches: [PatchRecord] = []
+ var rebuiltData: Data?
+
+ // MARK: - Init
+
+ public init(data: Data, restoreDir: URL?, verbose: Bool = true) {
+ buffer = BinaryBuffer(data)
+ self.restoreDir = restoreDir
+ self.verbose = verbose
+ }
+
+ // MARK: - Patcher
+
+ public func findAll() throws -> [PatchRecord] {
+ rebuiltData = nil
+ let root = try parsePayload(buffer.data)
+ let newRoot = try applyPatches(buildManifest: root)
+ rebuiltData = try serializePayload(newRoot)
+
+ patches = [PatchRecord(
+ patchID: "manifest.hash",
+ component: "",
+ fileOffset: 0,
+ originalBytes: Data(),
+ patchedBytes: Data(),
+ description: "Updated the file hashes according to the actual files",
+ )]
+ return patches
+ }
+
+ @discardableResult
+ public func apply() throws -> Int {
+ if patches.isEmpty {
+ let _ = try findAll()
+ }
+ if let rebuiltData {
+ buffer.data = rebuiltData
+ } else {
+ throw PatcherError.patchSiteNotFound("ManifestHash")
+ }
+ return patches.count
+ }
+
+ /// Get the patched data.
+ public var patchedData: Data {
+ buffer.data
+ }
+
+ private func parsePayload(_ blob: Data) throws -> PlistDict {
+ guard let buildManifest = try PropertyListSerialization.propertyList(
+ from: blob,
+ options: [],
+ format: nil
+ ) as? PlistDict else {
+ throw FirmwareManifest.ManifestError.invalidPlist("")
+ }
+ return buildManifest
+ }
+
+ func applyPatches(buildManifest: PlistDict) throws -> PlistDict {
+ var buildManifest = buildManifest
+ guard let restoreDir else {
+ throw FirmwareManifest.ManifestError.fileNotFound("Restore Directory")
+ }
+
+ // We assume that FirmwareManifest has generated the manifest containing a single build identity.
+ guard let buildIdentities = buildManifest["BuildIdentities"] as? [Any],
+ buildIdentities.count == 1 else {
+ throw FirmwareManifest.ManifestError.missingKey("BuildIdentities in BuildManifest")
+ }
+ guard var buildIdentity = buildIdentities.first! as? PlistDict else {
+ throw FirmwareManifest.ManifestError.missingKey("BuildIdentity in BuildIdentities")
+ }
+ guard let identityManifest = buildIdentity["Manifest"] as? PlistDict else {
+ throw FirmwareManifest.ManifestError.missingKey("Manifest in BuildIdentity")
+ }
+
+ var newBuildIdentityManifest = PlistDict()
+ for (comp, dict) in identityManifest {
+ guard var dict = dict as? PlistDict else {
+ throw FirmwareManifest.ManifestError.missingKey("component in build identity")
+ }
+ guard let info = dict["Info"] as? PlistDict else {
+ throw FirmwareManifest.ManifestError.missingKey("Info in build identity component")
+ }
+ guard let path = info["Path"] as? String else {
+ throw FirmwareManifest.ManifestError.missingKey("Path in build identity component info")
+ }
+
+ let componentData = try Data(contentsOf: restoreDir.appendingPathComponent(path))
+ let finalData = try patchIm4pTypeTag(comp, info["Img4PayloadType"] as? String, componentData)
+ let shaHash = SHA384.hash(data: finalData)
+ dict["Digest"] = Data(shaHash)
+ newBuildIdentityManifest[comp] = dict
+ }
+
+ buildIdentity["Manifest"] = newBuildIdentityManifest
+ buildManifest["BuildIdentities"] = [buildIdentity]
+ return buildManifest
+ }
+
+ private func serializePayload(_ buildManifest: PlistDict) throws -> Data {
+ return try PropertyListSerialization.data(
+ fromPropertyList: buildManifest,
+ format: .xml,
+ options: 0
+ )
+ }
+}
+
+func patchIm4pTypeTag(_ component: String, _ declaredType: String?, _ data: Data) throws -> Data {
+ guard [
+ "RestoreKernelCache",
+ "RestoreDeviceTree",
+ "RestoreSEP",
+ "RestoreLogo",
+ "RestoreTrustCache",
+ "RestoreDCP",
+ "Ap,RestoreDCP2",
+ "Ap,RestoreTMU",
+ "Ap,RestoreCIO",
+ "Ap,DCP2",
+ "Ap,RestoreSecureM3Firmware",
+ "Ap,RestoreSecurePageTableMonitor",
+ "Ap,RestoreTrustedExecutionMonitor",
+ "Ap,RestorecL4"
+ ].contains(component) else {
+ return data
+ }
+ guard let declaredType else {
+ throw FirmwareManifest.ManifestError.missingKey("Img4PayloadType in build identity component info")
+ }
+ return try IM4P(data).renamed(to: declaredType).data
+}
diff --git a/sources/FirmwarePatcher/Pipeline/FirmwarePipeline.swift b/sources/FirmwarePatcher/Pipeline/FirmwarePipeline.swift
index f0292683..0991c563 100644
--- a/sources/FirmwarePatcher/Pipeline/FirmwarePipeline.swift
+++ b/sources/FirmwarePatcher/Pipeline/FirmwarePipeline.swift
@@ -136,6 +136,8 @@ public final class FirmwarePipeline {
componentRecords.append(contentsOf: records)
if let deviceTreePatcher = patcher as? DeviceTreePatcher {
currentData = deviceTreePatcher.patchedData
+ } else if let manifestPatcher = patcher as? ManifestHashPatcher {
+ currentData = manifestPatcher.patchedData
} else {
for record in records {
let range = record.fileOffset ..< record.fileOffset + record.patchedBytes.count
@@ -279,6 +281,23 @@ public final class FirmwarePipeline {
}]
))
+ // 8. Firmware Manifest - Only required when excluding the img4 signature patches.
+ components.append(ComponentDescriptor(
+ name: "Manifest",
+ inRestoreDir: true,
+ searchPatterns: ["BuildManifest.plist"],
+ patcherFactories: {
+ return switch variant {
+ case .regular:
+ [{ data, verbose in
+ ManifestHashPatcher(data: data, restoreDir: try? self.findRestoreDirectory(), verbose: verbose)
+ }]
+ case .dev, .jb:
+ []
+ }
+ }()
+ ))
+
return components
}