Sync worktree updates into main repo folder
This commit is contained in:
parent
bc1a502c1d
commit
c4d2b5d9d1
21 changed files with 1565 additions and 347 deletions
|
|
@ -1,8 +1,10 @@
|
|||
/* 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"
|
||||
|
|
@ -116,10 +118,49 @@ int checkm8_verify_pwned(device_info_t *dev)
|
|||
|
||||
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) {
|
||||
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);
|
||||
|
|
@ -133,6 +174,39 @@ int checkm8_verify_pwned(device_info_t *dev)
|
|||
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 */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
|
@ -148,6 +222,23 @@ int checkm8_exploit(device_info_t *dev)
|
|||
|
||||
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;
|
||||
|
|
@ -162,9 +253,28 @@ int checkm8_exploit(device_info_t *dev)
|
|||
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;
|
||||
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",
|
||||
attempt - 1);
|
||||
return -1;
|
||||
|
|
@ -175,6 +285,21 @@ int checkm8_exploit(device_info_t *dev)
|
|||
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);
|
||||
|
|
@ -191,7 +316,39 @@ int checkm8_exploit(device_info_t *dev)
|
|||
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);
|
||||
|
||||
if (pwned == 1) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue