mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-15 09:43:34 +01:00
[AltServer] Fetches anisette data without Mail plug-in
Works on all macOS versions supported by AltServer.
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import OSLog
|
||||
|
||||
private extension Bundle
|
||||
{
|
||||
@@ -30,6 +31,19 @@ private extension ALTAnisetteData
|
||||
}
|
||||
}
|
||||
|
||||
@objc private protocol AOSUtilitiesProtocol
|
||||
{
|
||||
static var machineSerialNumber: String? { get }
|
||||
static var machineUDID: String? { get }
|
||||
|
||||
static func retrieveOTPHeadersForDSID(_ dsid: String) -> [String: Any]?
|
||||
|
||||
// Non-static versions used for respondsToSelector:
|
||||
var machineSerialNumber: String? { get }
|
||||
var machineUDID: String? { get }
|
||||
func retrieveOTPHeadersForDSID(_ dsid: String) -> [String: Any]?
|
||||
}
|
||||
|
||||
class AnisetteDataManager: NSObject
|
||||
{
|
||||
static let shared = AnisetteDataManager()
|
||||
@@ -53,40 +67,121 @@ class AnisetteDataManager: NSObject
|
||||
|
||||
func requestAnisetteData(_ completion: @escaping (Result<ALTAnisetteData, Error>) -> Void)
|
||||
{
|
||||
self.requestAnisetteDataFromXPCService { (result) in
|
||||
self.requestAnisetteDataFromAOSKit { (result) in
|
||||
do
|
||||
{
|
||||
let anisetteData = try result.get()
|
||||
completion(.success(anisetteData))
|
||||
}
|
||||
catch CocoaError.xpcConnectionInterrupted
|
||||
catch let aosKitError
|
||||
{
|
||||
// SIP and/or AMFI are not disabled, so fall back to Mail plug-in.
|
||||
self.requestAnisetteDataFromPlugin { (result) in
|
||||
completion(result)
|
||||
// Fall back to XPC in case SIP is disabled.
|
||||
self.requestAnisetteDataFromXPCService { (result) in
|
||||
do
|
||||
{
|
||||
let anisetteData = try result.get()
|
||||
completion(.success(anisetteData))
|
||||
}
|
||||
catch CocoaError.xpcConnectionInterrupted
|
||||
{
|
||||
// SIP and/or AMFI are not disabled, so fall back to Mail plug-in as last resort.
|
||||
self.requestAnisetteDataFromPlugin { (result) in
|
||||
do
|
||||
{
|
||||
let anisetteData = try result.get()
|
||||
completion(.success(anisetteData))
|
||||
}
|
||||
catch
|
||||
{
|
||||
Logger.main.error("Failed to fetch anisette data via Mail plug-in. \(error.localizedDescription, privacy: .public)")
|
||||
|
||||
// Return original error.
|
||||
completion(.failure(aosKitError))
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
Logger.main.error("Failed to fetch anisette data via XPC service. \(error.localizedDescription, privacy: .public)")
|
||||
|
||||
// Return original error.
|
||||
completion(.failure(aosKitError))
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isXPCAvailable(completion: @escaping (Bool) -> Void)
|
||||
{
|
||||
guard let proxy = self.xpcConnection.remoteObjectProxyWithErrorHandler({ (error) in
|
||||
completion(false)
|
||||
}) as? AltXPCProtocol else { return }
|
||||
|
||||
proxy.ping {
|
||||
completion(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension AnisetteDataManager
|
||||
{
|
||||
func requestAnisetteDataFromAOSKit(completion: @escaping (Result<ALTAnisetteData, Error>) -> Void)
|
||||
{
|
||||
do
|
||||
{
|
||||
let aosKitURL = URL(fileURLWithPath: "/System/Library/PrivateFrameworks/AOSKit.framework")
|
||||
|
||||
guard let aosKit = Bundle(url: aosKitURL) else { throw AnisetteError.aosKitFailure() }
|
||||
try aosKit.loadAndReturnError()
|
||||
|
||||
guard let AOSUtilitiesClass = NSClassFromString("AOSUtilities"),
|
||||
AOSUtilitiesClass.responds(to: #selector(AOSUtilitiesProtocol.retrieveOTPHeadersForDSID(_:))),
|
||||
AOSUtilitiesClass.responds(to: #selector(getter: AOSUtilitiesProtocol.machineSerialNumber)),
|
||||
AOSUtilitiesClass.responds(to: #selector(getter: AOSUtilitiesProtocol.machineUDID))
|
||||
else { throw AnisetteError.aosKitFailure() }
|
||||
|
||||
let AOSUtilities = unsafeBitCast(AOSUtilitiesClass, to: AOSUtilitiesProtocol.Type.self)
|
||||
|
||||
// -2 = Production environment (via https://github.com/ionescu007/Blackwood-4NT)
|
||||
guard let requestHeaders = AOSUtilities.retrieveOTPHeadersForDSID("-2") else { throw AnisetteError.missingValue("oneTimePassword") }
|
||||
|
||||
guard let machineID = requestHeaders["X-Apple-MD-M"] as? String else { throw AnisetteError.missingValue("machineID") }
|
||||
guard let oneTimePassword = requestHeaders["X-Apple-MD"] as? String else { throw AnisetteError.missingValue("oneTimePassword") }
|
||||
|
||||
guard let deviceID = AOSUtilities.machineUDID else { throw AnisetteError.missingValue("deviceUniqueIdentifier") }
|
||||
guard let localUserID = deviceID.data(using: .utf8)?.base64EncodedString() else { throw AnisetteError.missingValue("localUserID") }
|
||||
|
||||
let serialNumber = AOSUtilities.machineSerialNumber ?? "C02LKHBBFD57" // serialNumber can be nil, so provide valid fallback serial number.
|
||||
let routingInfo: UInt64 = 84215040 // Other known values: 17106176, 50660608
|
||||
|
||||
let osVersion: OperatingSystemVersion
|
||||
let buildVersion: String
|
||||
|
||||
if let build = ProcessInfo.processInfo.operatingSystemBuildVersion
|
||||
{
|
||||
osVersion = ProcessInfo.processInfo.operatingSystemVersion
|
||||
buildVersion = build
|
||||
}
|
||||
else
|
||||
{
|
||||
// Unknown build, so fall back to known valid macOS version.
|
||||
osVersion = OperatingSystemVersion(majorVersion: 13, minorVersion: 4, patchVersion: 0)
|
||||
buildVersion = "22F66"
|
||||
}
|
||||
|
||||
let deviceModel = ProcessInfo.processInfo.deviceModel ?? "iMac21,1"
|
||||
let osName = (osVersion.majorVersion < 11) ? "Mac OS X" : "macOS"
|
||||
|
||||
let serverFriendlyDescription = "<\(deviceModel)> <\(osName);\(osVersion.stringValue);\(buildVersion)> <com.apple.AuthKit/1 (com.apple.dt.Xcode/3594.4.19)>"
|
||||
|
||||
let anisetteData = ALTAnisetteData(machineID: machineID,
|
||||
oneTimePassword: oneTimePassword,
|
||||
localUserID: localUserID,
|
||||
routingInfo: routingInfo,
|
||||
deviceUniqueIdentifier: deviceID,
|
||||
deviceSerialNumber: serialNumber,
|
||||
deviceDescription: serverFriendlyDescription,
|
||||
date: Date(),
|
||||
locale: .current,
|
||||
timeZone: .current)
|
||||
completion(.success(anisetteData))
|
||||
}
|
||||
catch
|
||||
{
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
|
||||
func requestAnisetteDataFromXPCService(completion: @escaping (Result<ALTAnisetteData, Error>) -> Void)
|
||||
{
|
||||
guard let proxy = self.xpcConnection.remoteObjectProxyWithErrorHandler({ (error) in
|
||||
|
||||
Reference in New Issue
Block a user