From 0cfd30464ef45550470796ec561148f56dc83545 Mon Sep 17 00:00:00 2001 From: tr4m0ryp Date: Sun, 5 Apr 2026 01:58:29 +0200 Subject: [PATCH] Task 004: Device module (extended from research_icloud0) Extended device_info_t with CPID, ECID, MEID, chip name, checkm8 vulnerability flag, cellular detection, and DFU mode awareness. Integrates chip_db_lookup for automatic chip identification. Files created: - include/device/device.h - src/device/device.c Co-Authored-By: Claude Opus 4.6 (1M context) --- include/device/device.h | 76 ++++++++++ src/device/device.c | 298 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 374 insertions(+) create mode 100644 include/device/device.h create mode 100644 src/device/device.c diff --git a/include/device/device.h b/include/device/device.h new file mode 100644 index 0000000..8b1c759 --- /dev/null +++ b/include/device/device.h @@ -0,0 +1,76 @@ +/* + * device.h -- Device detection and info querying via libimobiledevice. + * + * Extended from research_icloud0/src/device.h with CPID, ECID, MEID, + * chip database integration, cellular detection, and DFU mode awareness. + */ + +#ifndef DEVICE_H +#define DEVICE_H + +#include +#include +#include +#include + +#define DEVICE_FIELD_LEN 128 +#define DEVICE_UDID_LEN 64 +#define DEVICE_CHIP_NAME_LEN 32 + +typedef struct { + /* Identification (existing) */ + char udid[DEVICE_UDID_LEN]; + char product_type[DEVICE_FIELD_LEN]; /* e.g. "iPad8,10" */ + char chip_id[DEVICE_FIELD_LEN]; /* e.g. "0x8027" (string) */ + char ios_version[DEVICE_FIELD_LEN]; /* e.g. "26.3" */ + char activation_state[DEVICE_FIELD_LEN]; + char serial[DEVICE_FIELD_LEN]; + char imei[DEVICE_FIELD_LEN]; + char hardware_model[DEVICE_FIELD_LEN]; /* e.g. "J418AP" */ + char device_name[DEVICE_FIELD_LEN]; /* user-assigned device name */ + + /* Handles kept open for service reuse. Caller must call device_free. */ + idevice_t handle; + lockdownd_client_t lockdown; + + /* --- New fields (task 004) --- */ + + /* Chip identification */ + uint32_t cpid; /* numeric chip ID (e.g. 0x8010) */ + uint64_t ecid; /* exclusive chip ID */ + char meid[DEVICE_FIELD_LEN]; /* MEID for CDMA devices */ + char chip_name[DEVICE_CHIP_NAME_LEN]; /* "A10", "A11", etc. from chip_db */ + int checkm8_vulnerable; /* 1 if A5-A11, 0 if A12+ */ + + /* Connectivity */ + int is_cellular; /* 1 if IMEI or MEID present */ + int is_dfu_mode; /* 0 in this module; set by usb_dfu */ + + /* USB handle for DFU mode (NULL when not in DFU) */ + libusb_device_handle *usb; +} device_info_t; + +/* + * device_detect -- Find and connect to the first available USB device. + * Populates dev->handle and dev->lockdown. Returns 0 on success, -1 on error. + */ +int device_detect(device_info_t *dev); + +/* + * device_query_info -- Query all device properties via lockdownd. + * Requires device_detect() to have succeeded first. + * Returns 0 on success, -1 on error (partial info may be populated). + */ +int device_query_info(device_info_t *dev); + +/* + * device_print_info -- Print all populated device fields to stdout. + */ +void device_print_info(const device_info_t *dev); + +/* + * device_free -- Release lockdown client and device handle. + */ +void device_free(device_info_t *dev); + +#endif /* DEVICE_H */ diff --git a/src/device/device.c b/src/device/device.c new file mode 100644 index 0000000..9d8cc3d --- /dev/null +++ b/src/device/device.c @@ -0,0 +1,298 @@ +/* + * device.c -- Device detection and info querying via libimobiledevice. + * + * Extended from research_icloud0/src/device.c with CPID, ECID, MEID, + * chip database integration, cellular detection, and DFU mode awareness. + */ + +#include +#include +#include +#include + +#include "device/device.h" +#include "device/chip_db.h" +#include "util/log.h" + +/* ------------------------------------------------------------------ */ +/* Lockdownd helpers */ +/* ------------------------------------------------------------------ */ + +/* Extract a string value from lockdownd for a given key. */ +static int query_string_value(lockdownd_client_t lockdown, + const char *domain, const char *key, + char *out, size_t out_len) +{ + plist_t val = NULL; + lockdownd_error_t err; + + err = lockdownd_get_value(lockdown, domain, key, &val); + if (err != LOCKDOWN_E_SUCCESS || !val) { + out[0] = '\0'; + return -1; + } + + if (plist_get_node_type(val) == PLIST_STRING) { + char *str = NULL; + plist_get_string_val(val, &str); + if (str) { + snprintf(out, out_len, "%s", str); + free(str); + } else { + out[0] = '\0'; + } + } else if (plist_get_node_type(val) == PLIST_UINT) { + uint64_t uval = 0; + plist_get_uint_val(val, &uval); + snprintf(out, out_len, "0x%llx", (unsigned long long)uval); + } else { + out[0] = '\0'; + plist_free(val); + return -1; + } + + plist_free(val); + return 0; +} + +/* Extract a uint64 value from lockdownd for a given key. */ +static int query_uint_value(lockdownd_client_t lockdown, + const char *domain, const char *key, + uint64_t *out) +{ + plist_t val = NULL; + lockdownd_error_t err; + + *out = 0; + err = lockdownd_get_value(lockdown, domain, key, &val); + if (err != LOCKDOWN_E_SUCCESS || !val) + return -1; + + if (plist_get_node_type(val) == PLIST_UINT) { + plist_get_uint_val(val, out); + plist_free(val); + return 0; + } + + plist_free(val); + return -1; +} + +/* ------------------------------------------------------------------ */ +/* device_detect */ +/* ------------------------------------------------------------------ */ + +int device_detect(device_info_t *dev) +{ + char **devices = NULL; + int count = 0; + idevice_error_t ierr; + lockdownd_error_t lerr; + + if (!dev) { + log_error("NULL device_info_t pointer"); + return -1; + } + + memset(dev, 0, sizeof(*dev)); + + /* Enumerate connected USB devices */ + ierr = idevice_get_device_list(&devices, &count); + if (ierr != IDEVICE_E_SUCCESS || count == 0) { + log_error("No devices found (idevice error %d)", (int)ierr); + if (devices) + idevice_device_list_free(devices); + return -1; + } + + log_info("Found %d device(s), using first: %s", count, devices[0]); + snprintf(dev->udid, sizeof(dev->udid), "%s", devices[0]); + idevice_device_list_free(devices); + + /* Open device handle */ + ierr = idevice_new(&dev->handle, dev->udid); + if (ierr != IDEVICE_E_SUCCESS) { + log_error("idevice_new failed (error %d)", (int)ierr); + return -1; + } + + /* Start lockdown session with handshake (validates pairing) */ + lerr = lockdownd_client_new_with_handshake(dev->handle, + &dev->lockdown, + "itr4m"); + if (lerr != LOCKDOWN_E_SUCCESS) { + log_error("lockdownd handshake failed (error %d)", (int)lerr); + idevice_free(dev->handle); + dev->handle = NULL; + return -1; + } + + log_info("Connected to device %s", dev->udid); + return 0; +} + +/* ------------------------------------------------------------------ */ +/* device_query_info */ +/* ------------------------------------------------------------------ */ + +int device_query_info(device_info_t *dev) +{ + int failures = 0; + uint64_t raw_cpid = 0; + + if (!dev || !dev->lockdown) { + log_error("No lockdown session"); + return -1; + } + + /* --- Existing string queries --- */ + + if (query_string_value(dev->lockdown, NULL, "ProductType", + dev->product_type, sizeof(dev->product_type)) < 0) { + log_warn("Could not query ProductType"); + failures++; + } + + if (query_string_value(dev->lockdown, NULL, "ChipID", + dev->chip_id, sizeof(dev->chip_id)) < 0) { + log_warn("Could not query ChipID (string)"); + failures++; + } + + if (query_string_value(dev->lockdown, NULL, "ProductVersion", + dev->ios_version, sizeof(dev->ios_version)) < 0) { + log_warn("Could not query ProductVersion"); + failures++; + } + + if (query_string_value(dev->lockdown, NULL, "ActivationState", + dev->activation_state, + sizeof(dev->activation_state)) < 0) { + log_warn("Could not query ActivationState"); + failures++; + } + + if (query_string_value(dev->lockdown, NULL, "SerialNumber", + dev->serial, sizeof(dev->serial)) < 0) { + log_warn("Could not query SerialNumber"); + failures++; + } + + /* IMEI may not exist on WiFi-only iPads */ + query_string_value(dev->lockdown, NULL, + "InternationalMobileEquipmentIdentity", + dev->imei, sizeof(dev->imei)); + + if (query_string_value(dev->lockdown, NULL, "HardwareModel", + dev->hardware_model, + sizeof(dev->hardware_model)) < 0) { + log_warn("Could not query HardwareModel"); + failures++; + } + + if (query_string_value(dev->lockdown, NULL, "DeviceName", + dev->device_name, sizeof(dev->device_name)) < 0) { + log_warn("Could not query DeviceName"); + failures++; + } + + /* --- New numeric queries (task 004) --- */ + + if (query_uint_value(dev->lockdown, NULL, "ChipID", &raw_cpid) == 0) { + dev->cpid = (uint32_t)raw_cpid; + } else { + log_warn("Could not query ChipID (numeric)"); + } + + if (query_uint_value(dev->lockdown, NULL, "UniqueChipID", + &dev->ecid) < 0) { + log_warn("Could not query UniqueChipID (ECID)"); + } + + /* MEID may not exist on GSM-only or WiFi-only devices */ + query_string_value(dev->lockdown, NULL, + "MobileEquipmentIdentifier", + dev->meid, sizeof(dev->meid)); + + /* --- Chip database lookup --- */ + + if (dev->cpid != 0) { + const chip_info_t *chip = chip_db_lookup(dev->cpid); + if (chip) { + snprintf(dev->chip_name, sizeof(dev->chip_name), "%s", + chip->name); + dev->checkm8_vulnerable = chip->checkm8_vulnerable; + } else { + snprintf(dev->chip_name, sizeof(dev->chip_name), "Unknown"); + dev->checkm8_vulnerable = 0; + log_warn("CPID 0x%04X not found in chip database", dev->cpid); + } + } + + /* --- Cellular detection --- */ + + dev->is_cellular = (dev->imei[0] != '\0' || dev->meid[0] != '\0'); + + /* DFU mode and USB handle are set externally by usb_dfu module */ + dev->is_dfu_mode = 0; + dev->usb = NULL; + + return (failures > 3) ? -1 : 0; +} + +/* ------------------------------------------------------------------ */ +/* device_print_info */ +/* ------------------------------------------------------------------ */ + +void device_print_info(const device_info_t *dev) +{ + if (!dev) return; + + printf("\n--- Device Information ---\n"); + printf(" UDID: %s\n", dev->udid); + printf(" Product Type: %s\n", dev->product_type); + printf(" Chip ID: %s\n", dev->chip_id); + printf(" iOS Version: %s\n", dev->ios_version); + printf(" Activation State: %s\n", dev->activation_state); + printf(" Serial: %s\n", dev->serial); + printf(" IMEI: %s\n", + dev->imei[0] ? dev->imei : "(none / WiFi-only)"); + printf(" MEID: %s\n", + dev->meid[0] ? dev->meid : "(none)"); + printf(" Hardware Model: %s\n", dev->hardware_model); + printf(" Device Name: %s\n", dev->device_name); + + /* New fields */ + printf(" CPID: 0x%04X\n", dev->cpid); + printf(" ECID: 0x%llX\n", (unsigned long long)dev->ecid); + printf(" Chip Name: %s\n", + dev->chip_name[0] ? dev->chip_name : "(unknown)"); + printf(" checkm8 vuln: %s\n", + dev->checkm8_vulnerable ? "YES" : "NO"); + printf(" Cellular: %s\n", + dev->is_cellular ? "YES" : "NO (WiFi-only)"); + printf(" DFU Mode: %s\n", + dev->is_dfu_mode ? "YES" : "NO"); + printf("-------------------------\n\n"); +} + +/* ------------------------------------------------------------------ */ +/* device_free */ +/* ------------------------------------------------------------------ */ + +void device_free(device_info_t *dev) +{ + if (!dev) return; + + if (dev->lockdown) { + lockdownd_client_free(dev->lockdown); + dev->lockdown = NULL; + } + if (dev->handle) { + idevice_free(dev->handle); + dev->handle = NULL; + } + + /* usb handle owned by usb_dfu module; not freed here */ + dev->usb = NULL; +}