#!/usr/bin/env python3 """mk_trackb.py — generate the Track B probe/skeleton app in one shot. Writes a buildable XcodeGen project with: - selector sweep (finds IO_Prepare / IO_Start indices) - size bisection (recovers sizeof(S_AVE_UCInParam_Prepare) empirically) - prepare/start skeleton with the layout knobs isolated at the top - entitlement file, deploy and oracle scripts, README usage: python3 mk_trackb.py --team ABCDE12345 python3 mk_trackb.py --team ABCDE12345 --with-entitlement python3 mk_trackb.py --dir /home/zkennedy137/ios27/cve112479/jbtest/trackb --with-entitlement adds com.apple.security.iokit-user-client-class. Use it only if you have a paid team whose profile can carry private entitlements; with a free cert an unprovisioned entitlement fails the install. """ import argparse import os import stat import sys SWIFT_CLIENT = r'''import UIKit import IOKit // ---- knobs: all UNKNOWN until the sweep and bisection have run ---- let SEL_PREPARE: UInt32 = 0 let SEL_START: UInt32 = 1 var IN_SIZE: Int = 0x1000 let CLIENT_TYPE: UInt32 = 1 // offsets inside pInfo, NOT inside pIn. Relation between the two is unknown. let OFF_MODE: Int? = 0x5c let OFF_LAYERS: Int? = 0x68 let PPS_COUNT: Int? = 0x14d8 // pInfo side of pClient+0x2138; unverified func kr(_ s: UInt32) -> String { String(format: "0x%08x", s) } final class AVEClient { var conn: io_connect_t = 0 var service: io_service_t = 0 let docs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] func log(_ s: String) { NSLog("AVEC %@", s) let f = docs.appendingPathComponent("ave.txt") let line = (s + "\n").data(using: .utf8)! if let h = try? FileHandle(forWritingTo: f) { h.seekToEndOfFile(); h.write(line); try? h.close() } else { try? line.write(to: f) } } func match() -> Bool { guard let m = IOServiceMatching("AppleAVE2") else { log("no match dict"); return false } service = IOServiceGetMatchingService(kIOMainPortDefault, m) log("service \(service)") return service != 0 } func open(_ type: UInt32) -> Bool { if service == 0 { guard match() else { return false } } let r = IOServiceOpen(service, mach_task_self_, type, &conn) log("open type \(type) -> \(kr(r))") return r == KERN_SUCCESS } func close() { if conn != 0 { IOServiceClose(conn); conn = 0 } } func call(_ sel: UInt32, _ input: [UInt8]) -> kern_return_t { var out = [UInt8](repeating: 0, count: 0x1000) var outSize = out.count let r = input.withUnsafeBytes { ib in out.withUnsafeBytes { ob in IOConnectCallStructMethod(conn, sel, ib.baseAddress, ib.count, ob.baseAddress, &outSize) } } log("sel \(sel) in \(input.count) -> \(kr(r)) out \(outSize)") return r } // every type 0..5, to find which are accepted at all func sweepTypes() { guard match() else { return } for t: UInt32 in 0...5 { if open(t) { close() } } log("types done") } // non-Unsupported returns name the external-method surface func sweepSelectors(_ count: UInt32 = 40) { guard open(CLIENT_TYPE) else { return } let zero = [UInt8](repeating: 0, count: IN_SIZE) for s: UInt32 in 0.. \(kr(r))") } } close() log("selectors done") } // assert order is: pIn != null, inSize >= sizeof, pOut != null. // the size where the failure moves is the boundary. func bisect(_ sel: UInt32) { guard open(CLIENT_TYPE) else { return } var lo = 0x20, hi = 0x4000 while hi - lo > 8 { let mid = (lo + hi) / 2 let r = call(sel, [UInt8](repeating: 0, count: mid)) log("bisect \(mid) -> \(kr(r))") if r == kIOReturnBadArgument { lo = mid } else { hi = mid } } log("boundary \(lo)..\(hi)") close() } func buildInput(_ size: Int) -> [UInt8] { var b = [UInt8](repeating: 0, count: size) if let o = OFF_MODE, o < size { b[o] = 2 } if let o = OFF_LAYERS, o < size { b[o] = 2 } if let o = PPS_COUNT, o + 4 <= size { b[o] = 9 } return b } func prepareThenStart() { guard open(CLIENT_TYPE) else { return } let input = buildInput(IN_SIZE) let p = call(SEL_PREPARE, input) if p == KERN_SUCCESS { _ = call(SEL_START, input) } close() log("prepare/start done") } } ''' SWIFT_UI = r'''import SwiftUI @main struct AVEApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { @State private var text = "" @State private var selPrepare = "0" @State private var selStart = "1" let c = AVEClient() var body: some View { VStack(spacing: 10) { HStack { Button("Sweep types") { c.sweepTypes(); refresh() } Button("Sweep selectors") { c.sweepSelectors(); refresh() } } HStack { TextField("prepare sel", text: $selPrepare).textFieldStyle(.roundedBorder) TextField("start sel", text: $selStart).textFieldStyle(.roundedBorder) } HStack { Button("Bisect") { c.bisect(UInt32(selPrepare) ?? 0); refresh() } Button("Prepare+Start") { c.prepareThenStart(); refresh() } } ScrollView { Text(text).font(.system(size: 10, design: .monospaced)) .frame(maxWidth: .infinity, alignment: .leading) } } .padding() .onAppear(perform: refresh) } func refresh() { let u = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] text = (try? String(contentsOf: u.appendingPathComponent("ave.txt"))) ?? "" } } ''' INFO_PLIST = ''' CFBundleExecutableAVEProbe CFBundleIdentifierai.chollima.aveprobe CFBundleNameAVEProbe CFBundleVersion1 CFBundleShortVersionString0.1 LSRequiresIPhoneOS MinimumOSVersion17.0 UIFileSharingEnabled LSSupportsOpeningDocumentsInPlace UILaunchScreen UISupportedInterfaceOrientations UIInterfaceOrientationPortrait ''' ENTITLEMENTS = ''' com.apple.security.iokit-user-client-class AppleAVE2UserClient com.apple.security.exception.iokit-user-client-class AppleAVE2UserClient ''' PROJECT_YML = '''name: AVEProbe options: bundleIdPrefix: ai.chollima deploymentTarget: iOS: "17.0" targets: AVEProbe: type: application platform: iOS sources: [Sources] info: path: Resources/Info.plist settings: base: PRODUCT_BUNDLE_IDENTIFIER: ai.chollima.aveprobe DEVELOPMENT_TEAM: "{team}" CODE_SIGN_STYLE: Automatic {entitlement}''' LOG_SH = '''#!/usr/bin/env bash # oracle: the driver's own log is the real output of every run idevicesyslog -m AVE | grep -E 'AVE|parameter sets|layer|PS data|initial info' ''' DEPLOY_SH = '''#!/usr/bin/env bash set -euo pipefail UDID="${1:?usage: deploy.sh }" xcrun devicectl device install app --device "$UDID" build/Build/Products/Debug-iphoneos/AVEProbe.app echo "-- installed; now:" echo " xcrun devicectl device process launch --device $UDID ai.chilloma.aveprobe" ''' README = '''# Track B probe AppleAVE2 user-client probe and skeleton for iOS 27.0.1 / T8140 only. Does not exploit anything. It answers three questions: can the sandbox open the client, which external-method selectors exist, and how big is `S_AVE_UCInParam_Prepare`. ## Order 1. `Sweep types` — expect a success on some type, 1 is the one that matters. `0xe00002c1` on every type means the sandbox blocks it and this track needs mediaserverd instead. 2. `Sweep selectors` — any `CANDIDATE` line is a call that exists. `IO_Prepare` and `IO_Start` are among them; the driver log identifies which. 3. Put the prepare selector in the field, press `Bisect`. The size where the return code changes is `sizeof(S_AVE_UCInParam_Prepare)`. 4. Only then `Prepare+Start`, and only after the layout is filled in. ## Knobs, all in AVEClient.swift `SEL_PREPARE`, `SEL_START`, `IN_SIZE`, `OFF_MODE`, `OFF_LAYERS`, `PPS_COUNT`. Every one is a guess except `CLIENT_TYPE`. Nothing here has been run. ## Oracle `tools/log.sh` on the host, in a second terminal. Strings to read for, in order of depth: `wrong parameters`, `initial info is wrong`, `PS data overflows`. A run that produces none of them did not reach the driver. ''' BUILD_SH = '''#!/usr/bin/env bash set -euo pipefail command -v xcodegen >/dev/null || { echo "brew install xcodegen"; exit 1; } xcodegen generate xcodebuild -project AVEProbe.xcodeproj -scheme AVEProbe -configuration Debug \\ -derivedDataPath build -destination 'generic/platform=iOS' build ''' def write(path, body, mode=None): os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, "w") as fh: fh.write(body) if mode: os.chmod(path, mode) print(" " + path) def main(): ap = argparse.ArgumentParser() ap.add_argument("--dir", default=os.path.join(os.getcwd(), "trackb")) ap.add_argument("--team", default="TEAMID0000") ap.add_argument("--with-entitlement", action="store_true") a = ap.parse_args() root = os.path.abspath(a.dir) print("writing " + root) ent = "" if a.with_entitlement: ent = " CODE_SIGN_ENTITLEMENTS: AVEProbe.entitlements\n" write(os.path.join(root, "AVEProbe.entitlements"), ENTITLEMENTS) write(os.path.join(root, "project.yml"), PROJECT_YML.format(team=a.team, entitlement=ent)) write(os.path.join(root, "Sources/AVEClient.swift"), SWIFT_CLIENT) write(os.path.join(root, "Sources/ContentView.swift"), SWIFT_UI) write(os.path.join(root, "Resources/Info.plist"), INFO_PLIST) write(os.path.join(root, "tools/log.sh"), LOG_SH, 0o755) write(os.path.join(root, "tools/deploy.sh"), DEPLOY_SH, 0o755) write(os.path.join(root, "build.sh"), BUILD_SH, 0o755) write(os.path.join(root, "README.md"), README) print("\nnext:") print(" cd %s" % root) print(" ./build.sh") print(" ./tools/deploy.sh ") print(" ./tools/log.sh # second terminal, before launching") if not a.with_entitlement: print("\nentitlement not included; add --with-entitlement on a paid team") if __name__ == "__main__": main()