From 82bec20452343850ebe3a0a245f86b757bee1a38 Mon Sep 17 00:00:00 2001 From: tr4m0ryp Date: Sun, 5 Apr 2026 02:11:25 +0200 Subject: [PATCH] feat(exploit): Add DFU protocol state machine Implemented DFU protocol operations (DNLOAD, UPLOAD, GETSTATUS, CLRSTATUS, GETSTATE, ABORT) with proper byte ordering for status parsing. Foundation for checkm8 exploit delivery. Files created: - include/exploit/dfu_proto.h - src/exploit/dfu_proto.c Co-Authored-By: Claude Opus 4.6 (1M context) --- include/exploit/dfu_proto.h | 113 ++++++++++++++ src/exploit/dfu_proto.c | 290 ++++++++++++++++++++++++++++++++++++ 2 files changed, 403 insertions(+) create mode 100644 include/exploit/dfu_proto.h create mode 100644 src/exploit/dfu_proto.c diff --git a/include/exploit/dfu_proto.h b/include/exploit/dfu_proto.h new file mode 100644 index 0000000..233461e --- /dev/null +++ b/include/exploit/dfu_proto.h @@ -0,0 +1,113 @@ +/* dfu_proto.h -- DFU protocol state machine for checkm8 exploit delivery */ + +#ifndef DFU_PROTO_H +#define DFU_PROTO_H + +#include +#include +#include + +/* ------------------------------------------------------------------ */ +/* DFU class-specific request codes (USB DFU 1.1 spec, Table 3.2) */ +/* ------------------------------------------------------------------ */ + +#define DFU_REQ_DETACH 0x00 +#define DFU_REQ_DNLOAD 0x01 +#define DFU_REQ_UPLOAD 0x02 +#define DFU_REQ_GETSTATUS 0x03 +#define DFU_REQ_CLRSTATUS 0x04 +#define DFU_REQ_GETSTATE 0x05 +#define DFU_REQ_ABORT 0x06 + +/* ------------------------------------------------------------------ */ +/* 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 + +/* ------------------------------------------------------------------ */ +/* DFU status values (bStatus field in status response) */ +/* ------------------------------------------------------------------ */ + +#define DFU_STATUS_OK 0x00 +#define DFU_STATUS_ERR_TARGET 0x01 +#define DFU_STATUS_ERR_FILE 0x02 +#define DFU_STATUS_ERR_WRITE 0x03 +#define DFU_STATUS_ERR_UNKNOWN 0x07 +#define DFU_STATUS_ERR_STALLED 0x0F + +/* ------------------------------------------------------------------ */ +/* DFU status structure */ +/* ------------------------------------------------------------------ */ + +typedef struct { + uint8_t bStatus; + uint32_t bwPollTimeout; /* 3 bytes on wire, stored as uint32 */ + uint8_t bState; + uint8_t iString; +} dfu_status_t; + +/* ------------------------------------------------------------------ */ +/* Protocol API */ +/* ------------------------------------------------------------------ */ + +/* + * Query device status. Sends DFU_GETSTATUS and parses the 6-byte + * response into the dfu_status_t structure. + * Returns 0 on success, -1 on failure. + */ +int dfu_get_status(libusb_device_handle *dev, dfu_status_t *status); + +/* + * Clear error status. Sends DFU_CLRSTATUS with no data stage. + * Returns 0 on success, -1 on failure. + */ +int dfu_clr_status(libusb_device_handle *dev); + +/* + * Query current DFU state byte. + * Returns 0 on success, -1 on failure. + */ +int dfu_get_state(libusb_device_handle *dev, uint8_t *state); + +/* + * Send a DFU_DNLOAD request with the given block number and data. + * A zero-length transfer (len=0) signals end of download. + * Returns 0 on success, -1 on failure. + */ +int dfu_dnload(libusb_device_handle *dev, uint16_t block_num, + const void *data, size_t len); + +/* + * Send a DFU_UPLOAD request to read data from the device. + * On success, *actual is set to the number of bytes received. + * Returns 0 on success, -1 on failure. + */ +int dfu_upload(libusb_device_handle *dev, uint16_t block_num, + void *buf, size_t len, size_t *actual); + +/* + * Abort the current DFU operation. + * Returns 0 on success, -1 on failure. + */ +int dfu_abort(libusb_device_handle *dev); + +/* + * Send a data buffer via DFU_DNLOAD followed by a GETSTATUS poll. + * Returns 0 on success, -1 on failure. + */ +int dfu_send_data(libusb_device_handle *dev, const void *data, size_t len); + +/* + * Reset the DFU state machine to dfuIDLE by clearing errors and + * aborting any in-progress operation. + * Returns 0 on success, -1 on failure. + */ +int dfu_reset_to_idle(libusb_device_handle *dev); + +#endif /* DFU_PROTO_H */ diff --git a/src/exploit/dfu_proto.c b/src/exploit/dfu_proto.c new file mode 100644 index 0000000..c0d1d02 --- /dev/null +++ b/src/exploit/dfu_proto.c @@ -0,0 +1,290 @@ +/* dfu_proto.c -- DFU protocol state machine for checkm8 exploit delivery */ + +#include +#include + +#include "exploit/dfu_proto.h" +#include "util/usb_helpers.h" +#include "util/log.h" + +/* ------------------------------------------------------------------ */ +/* bmRequestType values for DFU class requests */ +/* ------------------------------------------------------------------ */ + +/* Host-to-device, class, interface (OUT) */ +#define DFU_BMRT_OUT 0x21 + +/* Device-to-host, class, interface (IN) */ +#define DFU_BMRT_IN 0xA1 + +/* Default timeout for DFU control transfers (ms) */ +#define DFU_TIMEOUT 5000 + +/* DFU GETSTATUS response is always 6 bytes */ +#define DFU_STATUS_LEN 6 + +/* DFU GETSTATE response is always 1 byte */ +#define DFU_STATE_LEN 1 + +/* Maximum retries for reset-to-idle loop */ +#define DFU_MAX_RETRIES 5 + +/* ------------------------------------------------------------------ */ +/* dfu_get_status */ +/* ------------------------------------------------------------------ */ + +int dfu_get_status(libusb_device_handle *dev, dfu_status_t *status) +{ + unsigned char buf[DFU_STATUS_LEN]; + int ret; + + if (!dev || !status) + return -1; + + memset(status, 0, sizeof(*status)); + + ret = usb_ctrl_transfer(dev, DFU_BMRT_IN, DFU_REQ_GETSTATUS, + 0, 0, buf, DFU_STATUS_LEN, DFU_TIMEOUT); + if (ret < 0) { + log_error("dfu_get_status: control transfer failed: %s", + libusb_strerror(ret)); + return -1; + } + + if (ret != DFU_STATUS_LEN) { + log_error("dfu_get_status: expected %d bytes, got %d", + DFU_STATUS_LEN, ret); + return -1; + } + + /* + * Wire format (6 bytes, little-endian): + * [0] bStatus + * [1..3] bwPollTimeout (24-bit LE) + * [4] bState + * [5] iString + */ + status->bStatus = buf[0]; + status->bwPollTimeout = (uint32_t)buf[1] + | ((uint32_t)buf[2] << 8) + | ((uint32_t)buf[3] << 16); + status->bState = buf[4]; + status->iString = buf[5]; + + log_debug("dfu_get_status: status=0x%02X state=0x%02X poll=%u ms", + status->bStatus, status->bState, status->bwPollTimeout); + return 0; +} + +/* ------------------------------------------------------------------ */ +/* dfu_clr_status */ +/* ------------------------------------------------------------------ */ + +int dfu_clr_status(libusb_device_handle *dev) +{ + int ret; + + if (!dev) + return -1; + + ret = usb_ctrl_transfer_no_data(dev, DFU_BMRT_OUT, DFU_REQ_CLRSTATUS, + 0, 0, DFU_TIMEOUT); + if (ret < 0) { + log_error("dfu_clr_status: control transfer failed: %s", + libusb_strerror(ret)); + return -1; + } + + log_debug("dfu_clr_status: cleared"); + return 0; +} + +/* ------------------------------------------------------------------ */ +/* dfu_get_state */ +/* ------------------------------------------------------------------ */ + +int dfu_get_state(libusb_device_handle *dev, uint8_t *state) +{ + unsigned char buf[DFU_STATE_LEN]; + int ret; + + if (!dev || !state) + return -1; + + ret = usb_ctrl_transfer(dev, DFU_BMRT_IN, DFU_REQ_GETSTATE, + 0, 0, buf, DFU_STATE_LEN, DFU_TIMEOUT); + if (ret < 0) { + log_error("dfu_get_state: control transfer failed: %s", + libusb_strerror(ret)); + return -1; + } + + if (ret != DFU_STATE_LEN) { + log_error("dfu_get_state: expected %d byte, got %d", + DFU_STATE_LEN, ret); + return -1; + } + + *state = buf[0]; + log_debug("dfu_get_state: state=0x%02X", *state); + return 0; +} + +/* ------------------------------------------------------------------ */ +/* dfu_dnload */ +/* ------------------------------------------------------------------ */ + +int dfu_dnload(libusb_device_handle *dev, uint16_t block_num, + const void *data, size_t len) +{ + int ret; + + if (!dev || (!data && len > 0)) + return -1; + + /* wValue = block number, wIndex = interface 0 */ + ret = usb_ctrl_transfer(dev, DFU_BMRT_OUT, DFU_REQ_DNLOAD, + block_num, 0, + (unsigned char *)(uintptr_t)data, + (uint16_t)len, DFU_TIMEOUT); + if (ret < 0) { + log_error("dfu_dnload: block %u failed: %s", + block_num, libusb_strerror(ret)); + return -1; + } + + log_debug("dfu_dnload: block %u, %zu bytes sent", block_num, len); + return 0; +} + +/* ------------------------------------------------------------------ */ +/* dfu_upload */ +/* ------------------------------------------------------------------ */ + +int dfu_upload(libusb_device_handle *dev, uint16_t block_num, + void *buf, size_t len, size_t *actual) +{ + int ret; + + if (!dev || !buf || !actual) + return -1; + + *actual = 0; + + ret = usb_ctrl_transfer(dev, DFU_BMRT_IN, DFU_REQ_UPLOAD, + block_num, 0, + (unsigned char *)buf, + (uint16_t)len, DFU_TIMEOUT); + if (ret < 0) { + log_error("dfu_upload: block %u failed: %s", + block_num, libusb_strerror(ret)); + return -1; + } + + *actual = (size_t)ret; + log_debug("dfu_upload: block %u, %zu bytes received", block_num, *actual); + return 0; +} + +/* ------------------------------------------------------------------ */ +/* dfu_abort */ +/* ------------------------------------------------------------------ */ + +int dfu_abort(libusb_device_handle *dev) +{ + int ret; + + if (!dev) + return -1; + + ret = usb_ctrl_transfer_no_data(dev, DFU_BMRT_OUT, DFU_REQ_ABORT, + 0, 0, DFU_TIMEOUT); + if (ret < 0) { + log_error("dfu_abort: control transfer failed: %s", + libusb_strerror(ret)); + return -1; + } + + log_debug("dfu_abort: sent"); + return 0; +} + +/* ------------------------------------------------------------------ */ +/* dfu_send_data -- DNLOAD + GETSTATUS poll */ +/* ------------------------------------------------------------------ */ + +int dfu_send_data(libusb_device_handle *dev, const void *data, size_t len) +{ + dfu_status_t st; + + if (!dev || (!data && len > 0)) + return -1; + + /* Send the payload as block 0 */ + if (dfu_dnload(dev, 0, data, len) != 0) + return -1; + + /* Poll status -- device transitions through DNLOAD_SYNC */ + if (dfu_get_status(dev, &st) != 0) + return -1; + + if (st.bStatus != DFU_STATUS_OK) { + log_error("dfu_send_data: device reported error status 0x%02X", + st.bStatus); + return -1; + } + + log_debug("dfu_send_data: %zu bytes delivered, state=0x%02X", + len, st.bState); + return 0; +} + +/* ------------------------------------------------------------------ */ +/* dfu_reset_to_idle -- bring device back to dfuIDLE */ +/* ------------------------------------------------------------------ */ + +int dfu_reset_to_idle(libusb_device_handle *dev) +{ + dfu_status_t st; + int attempt; + + if (!dev) + return -1; + + for (attempt = 0; attempt < DFU_MAX_RETRIES; attempt++) { + if (dfu_get_status(dev, &st) != 0) + return -1; + + /* Already idle -- done */ + if (st.bState == DFU_STATE_IDLE && st.bStatus == DFU_STATUS_OK) { + log_debug("dfu_reset_to_idle: device is idle"); + return 0; + } + + /* In error state -- clear it */ + if (st.bState == DFU_STATE_ERROR) { + log_debug("dfu_reset_to_idle: clearing error state"); + if (dfu_clr_status(dev) != 0) + return -1; + continue; + } + + /* Any other state -- abort to return to idle */ + log_debug("dfu_reset_to_idle: aborting (state=0x%02X)", st.bState); + if (dfu_abort(dev) != 0) + return -1; + } + + /* Final check after retries */ + if (dfu_get_status(dev, &st) != 0) + return -1; + + if (st.bState != DFU_STATE_IDLE) { + log_error("dfu_reset_to_idle: failed after %d attempts " + "(state=0x%02X, status=0x%02X)", + DFU_MAX_RETRIES, st.bState, st.bStatus); + return -1; + } + + return 0; +}