feat(bypass): Add Path B bypass for A12+ devices

Orchestrates DFU identity manipulation, signal detection, drmHandshake
session activation, and deletescript cleanup for A12+ chips. Identity
manipulation offsets stubbed with TODO pending hardware testing.

Files created:
- include/bypass/path_b.h
- src/bypass/path_b.c
- src/bypass/path_b_identity.c

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
tr4m0ryp 2026-04-05 02:22:11 +02:00
parent 38d01955cb
commit ef6bff0999
3 changed files with 610 additions and 0 deletions

42
include/bypass/path_b.h Normal file
View file

@ -0,0 +1,42 @@
/*
* path_b.h -- Path B bypass module for A12+ devices.
*
* Chains: DFU identity manipulation -> signal detection ->
* drmHandshake session activation -> deletescript cleanup -> verify.
* Does NOT use a BootROM exploit -- works through identity
* manipulation and the session-mode activation protocol.
*/
#ifndef PATH_B_H
#define PATH_B_H
#include "bypass/bypass.h"
/* The Path B bypass module for registration with bypass_register(). */
extern const bypass_module_t path_b_module;
/*
* path_b_manipulate_identity -- Modify device serial descriptors in DFU.
* Appends PWND:[checkm8] marker to the device serial string via USB
* control transfers to the DFU interface. Required before session
* activation on A12+ devices.
* Returns 0 on success, -1 on error.
*/
int path_b_manipulate_identity(device_info_t *dev);
/*
* path_b_read_serial -- Read current serial descriptor from DFU mode.
* Copies the serial string into buf (up to len bytes including NUL).
* Returns 0 on success, -1 on error.
*/
int path_b_read_serial(device_info_t *dev, char *buf, size_t len);
/*
* path_b_write_serial -- Write a modified serial descriptor to the device.
* Sends a USB control transfer to overwrite the serial string descriptor
* in the DFU interface.
* Returns 0 on success, -1 on error.
*/
int path_b_write_serial(device_info_t *dev, const char *new_serial);
#endif /* PATH_B_H */

283
src/bypass/path_b.c Normal file
View file

@ -0,0 +1,283 @@
/*
* path_b.c -- Path B bypass orchestration for A12+ devices.
*
* Orchestrates the full A12+ bypass flow:
* 1. DFU identity manipulation (serial descriptor PWND marker)
* 2. Signal type detection (cellular vs WiFi-only)
* 3. drmHandshake session activation protocol
* 4. Post-bypass deletescript cleanup
* 5. Activation state verification
*
* Identity manipulation internals are in path_b_identity.c.
*/
#include <string.h>
#include <plist/plist.h>
#include "bypass/path_b.h"
#include "bypass/signal.h"
#include "bypass/deletescript.h"
#include "activation/activation.h"
#include "activation/session.h"
#include "activation/record.h"
#include "util/log.h"
/* Forward declarations for module callbacks. */
static int path_b_probe(device_info_t *dev);
static int path_b_execute(device_info_t *dev);
const bypass_module_t path_b_module = {
.name = "path_b_a12plus",
.description = "A12+ bypass via identity manipulation + session activation",
.probe = path_b_probe,
.execute = path_b_execute
};
/*
* path_b_probe -- Check if this device is eligible for Path B.
*
* Path B targets A12+ devices (not checkm8-vulnerable) that are
* currently in DFU mode. Returns 1 if compatible, 0 if not, -1 on error.
*/
static int path_b_probe(device_info_t *dev)
{
if (!dev) {
log_error("[path_b] NULL device in probe");
return -1;
}
/* A12+ devices have checkm8_vulnerable == 0 */
if (dev->checkm8_vulnerable != 0) {
log_debug("[path_b] Device is checkm8-vulnerable (A5-A11), skipping");
return 0;
}
/* Must be in DFU mode for identity manipulation */
if (dev->is_dfu_mode != 1) {
log_debug("[path_b] Device not in DFU mode, skipping");
return 0;
}
log_info("[path_b] A12+ device in DFU mode detected -- Path B compatible");
return 1;
}
/*
* step_manipulate_identity -- Step 1: modify serial descriptors in DFU.
*/
static int step_manipulate_identity(device_info_t *dev)
{
int rc;
log_info("[path_b] Step 1/9: Manipulating device identity in DFU...");
rc = path_b_manipulate_identity(dev);
if (rc != 0) {
log_error("[path_b] Identity manipulation failed");
return -1;
}
log_info("[path_b] Identity manipulation succeeded");
return 0;
}
/*
* step_detect_signal -- Step 2-3: detect and display signal info.
*/
static int step_detect_signal(device_info_t *dev)
{
signal_type_t sig;
log_info("[path_b] Step 2/9: Detecting signal type...");
sig = signal_detect_type(dev);
(void)sig; /* Used implicitly by signal_print_info */
log_info("[path_b] Step 3/9: Signal info:");
signal_print_info(dev);
return 0;
}
/*
* step_session_handshake -- Steps 4-6: session info + drmHandshake + activation info.
* On success, *out_response and *out_info are set (caller must free both).
*/
static int step_session_handshake(device_info_t *dev,
plist_t *out_response,
plist_t *out_info)
{
plist_t session = NULL;
plist_t response = NULL;
plist_t info = NULL;
int rc;
/* Step 4: get session info blob from device */
log_info("[path_b] Step 4/9: Requesting session info...");
rc = session_get_info(dev, &session);
if (rc != 0 || !session) {
log_error("[path_b] Failed to get session info");
return -1;
}
log_info("[path_b] Session info retrieved");
/* Step 5: perform local drmHandshake */
log_info("[path_b] Step 5/9: Performing drmHandshake...");
rc = session_drm_handshake(dev, session, &response);
if (rc != 0 || !response) {
log_error("[path_b] drmHandshake failed");
plist_free(session);
return -1;
}
log_info("[path_b] drmHandshake succeeded");
/* Step 6: create activation info with session response */
log_info("[path_b] Step 6/9: Creating activation info...");
rc = session_create_activation_info(dev, response, &info);
if (rc != 0 || !info) {
log_error("[path_b] Failed to create activation info");
plist_free(session);
plist_free(response);
return -1;
}
log_info("[path_b] Activation info created");
plist_free(session);
*out_response = response;
*out_info = info;
return 0;
}
/*
* step_activate -- Step 7: build A12+ record and activate with session.
*/
static int step_activate(device_info_t *dev, plist_t response)
{
plist_t record = NULL;
int rc;
log_info("[path_b] Step 7/9: Building A12+ activation record...");
record = record_build_a12(dev);
if (!record) {
log_error("[path_b] Failed to build A12+ activation record");
return -1;
}
log_info("[path_b] Activation record built");
log_info("[path_b] Activating device with session...");
rc = session_activate(dev, record, response);
if (rc != 0) {
log_error("[path_b] Session activation failed");
record_free(record);
return -1;
}
log_info("[path_b] Session activation succeeded");
record_free(record);
return 0;
}
/*
* step_deletescript -- Step 8: run post-bypass cleanup.
*/
static int step_deletescript(device_info_t *dev)
{
int rc;
log_info("[path_b] Step 8/9: Running deletescript cleanup...");
rc = deletescript_run(dev);
if (rc != 0) {
log_warn("[path_b] Deletescript reported errors (non-fatal)");
/* Continue -- activation may still be valid even if cleanup
* partially fails (e.g. Setup.app already removed). */
} else {
log_info("[path_b] Deletescript completed");
}
return 0;
}
/*
* step_verify -- Step 9: check if activation was successful.
*/
static int step_verify(device_info_t *dev)
{
int rc;
log_info("[path_b] Step 9/9: Verifying activation state...");
rc = activation_is_activated(dev);
if (rc == 1) {
log_info("[path_b] Verification PASSED -- device is activated");
return 0;
}
if (rc == 0) {
log_error("[path_b] Verification FAILED -- device is NOT activated");
} else {
log_error("[path_b] Verification ERROR -- could not query state");
}
return -1;
}
/*
* path_b_execute -- Run the full A12+ bypass flow.
*
* Steps are separated into helper functions for clarity and to keep
* the top-level flow readable. Each step logs its own progress.
*/
static int path_b_execute(device_info_t *dev)
{
plist_t response = NULL;
plist_t info = NULL;
int rc;
log_info("[path_b] === Starting A12+ bypass (Path B) ===");
log_info("[path_b] Device: %s (CPID 0x%04X, ECID 0x%llX)",
dev->product_type, dev->cpid,
(unsigned long long)dev->ecid);
/* Step 1: identity manipulation */
rc = step_manipulate_identity(dev);
if (rc != 0)
return -1;
/* Steps 2-3: signal detection */
rc = step_detect_signal(dev);
if (rc != 0)
return -1;
/* Steps 4-6: session handshake */
rc = step_session_handshake(dev, &response, &info);
if (rc != 0)
return -1;
/* Step 7: build record + activate */
rc = step_activate(dev, response);
if (rc != 0) {
plist_free(response);
plist_free(info);
return -1;
}
/* Free session plists before proceeding to cleanup */
plist_free(response);
plist_free(info);
/* Step 8: deletescript cleanup */
step_deletescript(dev);
/* Step 9: verify activation */
rc = step_verify(dev);
if (rc == 0) {
log_info("[path_b] === A12+ bypass completed successfully ===");
} else {
log_error("[path_b] === A12+ bypass FAILED at verification ===");
}
return rc;
}

View file

@ -0,0 +1,285 @@
/*
* path_b_identity.c -- DFU identity manipulation for A12+ devices.
*
* Modifies the device serial string descriptor in DFU mode to append
* the PWND:[checkm8] marker via USB control transfers. V1 uses correct
* transfer structure; exact SRAM write offsets are TODO (hardware testing).
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <libusb-1.0/libusb.h>
#include "bypass/path_b.h"
#include "device/usb_dfu.h"
#include "util/log.h"
/* USB descriptor request types */
#define USB_REQ_GET_DESCRIPTOR 0x06
#define USB_REQ_SET_DESCRIPTOR 0x07
#define USB_DT_STRING 0x03
/* Serial string descriptor index for Apple DFU devices */
#define DFU_SERIAL_DESC_INDEX 3
/* PWND marker appended to the serial string */
#define PWND_MARKER " PWND:[checkm8]"
/* Maximum USB string descriptor length per USB 2.0 spec */
#define USB_DESC_MAX_LEN 255
/*
* usb_get_string_descriptor -- GET_DESCRIPTOR control transfer for a
* USB string descriptor. Returns bytes received, or -1 on error.
*/
static int usb_get_string_descriptor(libusb_device_handle *usb,
uint8_t index,
uint8_t *buf, size_t len)
{
int rc;
if (!usb || !buf || len == 0) {
log_error("[path_b_id] Invalid arguments to get_string_descriptor");
return -1;
}
rc = libusb_control_transfer(
usb,
0x80, /* bmRequestType */
USB_REQ_GET_DESCRIPTOR, /* bRequest */
(uint16_t)((USB_DT_STRING << 8) | index), /* wValue */
0x0409, /* wIndex (lang) */
buf, /* data */
(uint16_t)(len < USB_DESC_MAX_LEN ? len : USB_DESC_MAX_LEN),
DFU_USB_TIMEOUT /* timeout ms */
);
if (rc < 0) {
log_error("[path_b_id] GET_DESCRIPTOR failed: %s",
libusb_error_name(rc));
return -1;
}
/* Validate minimum descriptor header */
if (rc < 2 || buf[1] != USB_DT_STRING) {
log_error("[path_b_id] Invalid string descriptor (len=%d, type=0x%02X)",
rc, rc >= 2 ? buf[1] : 0);
return -1;
}
return rc;
}
/*
* utf16le_to_ascii -- Convert USB string descriptor UTF-16LE to ASCII.
* Skips the 2-byte header. Non-ASCII characters become '?'.
*/
static int utf16le_to_ascii(const uint8_t *desc, int desc_len,
char *out, size_t out_len)
{
int i;
size_t pos = 0;
int str_bytes;
if (!desc || desc_len < 2 || !out || out_len == 0)
return -1;
/* String data starts at byte 2, each char is 2 bytes (UTF-16LE) */
str_bytes = desc_len - 2;
for (i = 0; i < str_bytes && pos < out_len - 1; i += 2) {
uint8_t lo = desc[2 + i];
uint8_t hi = (i + 1 < str_bytes) ? desc[2 + i + 1] : 0;
if (hi == 0 && lo >= 0x20 && lo <= 0x7E)
out[pos++] = (char)lo;
else
out[pos++] = '?';
}
out[pos] = '\0';
return (int)pos;
}
/*
* ascii_to_utf16le_desc -- Build USB string descriptor from ASCII.
* Returns total descriptor length, or -1 on error.
*/
static int ascii_to_utf16le_desc(const char *ascii,
uint8_t *desc, size_t desc_max)
{
size_t slen;
size_t total;
size_t i;
if (!ascii || !desc || desc_max < 4)
return -1;
slen = strlen(ascii);
total = 2 + (slen * 2);
if (total > desc_max || total > USB_DESC_MAX_LEN)
return -1;
desc[0] = (uint8_t)total; /* bLength */
desc[1] = USB_DT_STRING; /* bDescriptorType */
for (i = 0; i < slen; i++) {
desc[2 + (i * 2)] = (uint8_t)ascii[i]; /* low byte */
desc[2 + (i * 2) + 1] = 0x00; /* high byte */
}
return (int)total;
}
/* path_b_read_serial -- Read serial descriptor from DFU, decode to ASCII. */
int path_b_read_serial(device_info_t *dev, char *buf, size_t len)
{
uint8_t desc[USB_DESC_MAX_LEN];
int desc_len;
if (!dev || !buf || len == 0) {
log_error("[path_b_id] Invalid arguments to read_serial");
return -1;
}
if (!dev->usb) {
log_error("[path_b_id] No USB handle -- device not in DFU mode?");
return -1;
}
desc_len = usb_get_string_descriptor(dev->usb, DFU_SERIAL_DESC_INDEX,
desc, sizeof(desc));
if (desc_len < 0)
return -1;
if (utf16le_to_ascii(desc, desc_len, buf, len) < 0) {
log_error("[path_b_id] Failed to decode serial descriptor");
return -1;
}
log_debug("[path_b_id] Read serial: %s", buf);
return 0;
}
/*
* path_b_write_serial -- Write modified serial descriptor via SET_DESCRIPTOR.
*
* TODO: A12+ DFU serial resides in writable SRAM. The exact write offset
* varies by chip (T8020/T8030/T8101/T8110/T8120/T8103/T8112) and must be
* determined by hardware testing. May need vendor-specific control transfer
* instead of standard SET_DESCRIPTOR if the DFU firmware rejects it.
*/
int path_b_write_serial(device_info_t *dev, const char *new_serial)
{
uint8_t desc[USB_DESC_MAX_LEN];
int desc_len;
int rc;
if (!dev || !new_serial) {
log_error("[path_b_id] Invalid arguments to write_serial");
return -1;
}
if (!dev->usb) {
log_error("[path_b_id] No USB handle -- device not in DFU mode?");
return -1;
}
desc_len = ascii_to_utf16le_desc(new_serial, desc, sizeof(desc));
if (desc_len < 0) {
log_error("[path_b_id] Serial string too long for USB descriptor");
return -1;
}
/* SET_DESCRIPTOR: host-to-device, standard, device recipient */
rc = libusb_control_transfer(
dev->usb,
0x00, /* bmRequestType */
USB_REQ_SET_DESCRIPTOR, /* bRequest */
(uint16_t)((USB_DT_STRING << 8) | DFU_SERIAL_DESC_INDEX),
0x0409, /* wIndex (lang) */
desc, /* data */
(uint16_t)desc_len, /* wLength */
DFU_USB_TIMEOUT /* timeout ms */
);
if (rc < 0) {
log_warn("[path_b_id] SET_DESCRIPTOR returned: %s",
libusb_error_name(rc));
/* TODO: fallback vendor-specific transfer for A12+ DFU firmware */
return -1;
}
log_info("[path_b_id] Serial descriptor written (%d bytes)", desc_len);
return 0;
}
/* path_b_manipulate_identity -- Read, append PWND marker, write, verify. */
int path_b_manipulate_identity(device_info_t *dev)
{
char current[DFU_SERIAL_MAX];
char modified[DFU_SERIAL_MAX];
char verify[DFU_SERIAL_MAX];
int rc;
if (!dev) {
log_error("[path_b_id] NULL device");
return -1;
}
if (!dev->usb) {
log_error("[path_b_id] No USB handle for DFU mode");
return -1;
}
/* Step 1: read current serial */
log_info("[path_b_id] Reading current serial descriptor...");
rc = path_b_read_serial(dev, current, sizeof(current));
if (rc != 0) {
log_error("[path_b_id] Cannot read serial descriptor");
return -1;
}
log_info("[path_b_id] Current serial: %s", current);
/* Step 2: check if already manipulated */
if (strstr(current, "PWND:") != NULL) {
log_info("[path_b_id] PWND marker already present -- skipping");
return 0;
}
/* Step 3: build modified serial with PWND marker */
rc = snprintf(modified, sizeof(modified), "%s%s", current, PWND_MARKER);
if (rc < 0 || (size_t)rc >= sizeof(modified)) {
log_error("[path_b_id] Modified serial exceeds buffer size");
return -1;
}
log_info("[path_b_id] Modified serial: %s", modified);
/* Step 4: write modified serial */
log_info("[path_b_id] Writing modified serial descriptor...");
rc = path_b_write_serial(dev, modified);
if (rc != 0) {
log_error("[path_b_id] Failed to write modified serial");
return -1;
}
/* Step 5: verify by reading back */
log_info("[path_b_id] Verifying serial descriptor...");
rc = path_b_read_serial(dev, verify, sizeof(verify));
if (rc != 0) {
log_warn("[path_b_id] Could not verify serial (read-back failed)");
/* Non-fatal: write may have succeeded even if re-read fails. */
return 0;
}
if (strstr(verify, "PWND:") == NULL) {
log_error("[path_b_id] Verification failed: PWND marker not found");
log_error("[path_b_id] Read-back serial: %s", verify);
return -1;
}
log_info("[path_b_id] Identity manipulation verified");
return 0;
}