diff options
Diffstat (limited to 'tools/idevicebtlogger.c')
| -rw-r--r-- | tools/idevicebtlogger.c | 458 |
1 files changed, 458 insertions, 0 deletions
diff --git a/tools/idevicebtlogger.c b/tools/idevicebtlogger.c new file mode 100644 index 0000000..ca68b59 --- /dev/null +++ b/tools/idevicebtlogger.c | |||
| @@ -0,0 +1,458 @@ | |||
| 1 | /* | ||
| 2 | * idevicebt_packet_logger.c | ||
| 3 | * Capture Bluetooth HCI traffic to native PKLG or PCAP | ||
| 4 | * | ||
| 5 | * Copyright (c) 2021 Geoffrey Kruse, All Rights Reserved. | ||
| 6 | * Copyright (c) 2022 Matthias Ringwald, All Rights Reserved. | ||
| 7 | * | ||
| 8 | * This library is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * This library is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with this library; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #ifdef HAVE_CONFIG_H | ||
| 24 | #include "config.h" | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #define TOOL_NAME "idevicebtlogger" | ||
| 28 | |||
| 29 | #include <stdio.h> | ||
| 30 | #include <string.h> | ||
| 31 | #include <errno.h> | ||
| 32 | #include <signal.h> | ||
| 33 | #include <stdlib.h> | ||
| 34 | #include <unistd.h> | ||
| 35 | #include <getopt.h> | ||
| 36 | #include <assert.h> | ||
| 37 | #include <fcntl.h> | ||
| 38 | |||
| 39 | #ifdef _WIN32 | ||
| 40 | #include <windows.h> | ||
| 41 | #define sleep(x) Sleep(x*1000) | ||
| 42 | #else | ||
| 43 | #include <arpa/inet.h> | ||
| 44 | #endif | ||
| 45 | |||
| 46 | |||
| 47 | #include <libimobiledevice/libimobiledevice.h> | ||
| 48 | #include <libimobiledevice/bt_packet_logger.h> | ||
| 49 | |||
| 50 | typedef enum { | ||
| 51 | HCI_COMMAND = 0x00, | ||
| 52 | HCI_EVENT = 0x01, | ||
| 53 | SENT_ACL_DATA = 0x02, | ||
| 54 | RECV_ACL_DATA = 0x03, | ||
| 55 | SENT_SCO_DATA = 0x08, | ||
| 56 | RECV_SCO_DATA = 0x09, | ||
| 57 | } PacketLoggerPacketType; | ||
| 58 | |||
| 59 | static int quit_flag = 0; | ||
| 60 | static int exit_on_disconnect = 0; | ||
| 61 | |||
| 62 | static char* udid = NULL; | ||
| 63 | static idevice_t device = NULL; | ||
| 64 | static bt_packet_logger_client_t bt_packet_logger = NULL; | ||
| 65 | static int use_network = 0; | ||
| 66 | static char* out_filename = NULL; | ||
| 67 | static char* log_format_string = NULL; | ||
| 68 | static FILE * packetlogger_file = NULL; | ||
| 69 | |||
| 70 | static enum { | ||
| 71 | LOG_FORMAT_PACKETLOGGER, | ||
| 72 | LOG_FORMAT_PCAP | ||
| 73 | } log_format = LOG_FORMAT_PACKETLOGGER; | ||
| 74 | |||
| 75 | const uint8_t pcap_file_header[] = { | ||
| 76 | // Magic Number | ||
| 77 | 0xA1, 0xB2, 0xC3, 0xD4, | ||
| 78 | // Major / Minor Version | ||
| 79 | 0x00, 0x02, 0x00, 0x04, | ||
| 80 | // Reserved1 | ||
| 81 | 0x00, 0x00, 0x00, 0x00, | ||
| 82 | // Reserved2 | ||
| 83 | 0x00, 0x00, 0x00, 0x00, | ||
| 84 | // Snaplen == max packet size - use 2kB (larger than any ACL) | ||
| 85 | 0x00, 0x00, 0x08, 0x00, | ||
| 86 | // LinkType: DLT_BLUETOOTH_HCI_H4_WITH_PHDR | ||
| 87 | 0x00, 0x00, 0x00, 201, | ||
| 88 | }; | ||
| 89 | |||
| 90 | static uint32_t big_endian_read_32(const uint8_t * buffer, int position) | ||
| 91 | { | ||
| 92 | return ((uint32_t) buffer[position+3]) | (((uint32_t)buffer[position+2]) << 8) | (((uint32_t)buffer[position+1]) << 16) | (((uint32_t) buffer[position]) << 24); | ||
| 93 | } | ||
| 94 | |||
| 95 | static void big_endian_store_32(uint8_t * buffer, uint16_t position, uint32_t value) | ||
| 96 | { | ||
| 97 | uint16_t pos = position; | ||
| 98 | buffer[pos++] = (uint8_t)(value >> 24); | ||
| 99 | buffer[pos++] = (uint8_t)(value >> 16); | ||
| 100 | buffer[pos++] = (uint8_t)(value >> 8); | ||
| 101 | buffer[pos++] = (uint8_t)(value); | ||
| 102 | } | ||
| 103 | |||
| 104 | /** | ||
| 105 | * Callback from the packet logger service to handle packets and log to PacketLogger format | ||
| 106 | */ | ||
| 107 | static void bt_packet_logger_callback_packetlogger(uint8_t * data, uint16_t len, void *user_data) | ||
| 108 | { | ||
| 109 | (void) fwrite(data, 1, len, packetlogger_file); | ||
| 110 | } | ||
| 111 | |||
| 112 | /** | ||
| 113 | * Callback from the packet logger service to handle packets and log to pcap | ||
| 114 | */ | ||
| 115 | static void bt_packet_logger_callback_pcap(uint8_t * data, uint16_t len, void *user_data) | ||
| 116 | { | ||
| 117 | // check len | ||
| 118 | if (len < 13) { | ||
| 119 | return; | ||
| 120 | } | ||
| 121 | |||
| 122 | // parse packet header (ignore len field) | ||
| 123 | uint32_t ts_secs = big_endian_read_32(data, 4); | ||
| 124 | uint32_t ts_us = big_endian_read_32(data, 8); | ||
| 125 | uint8_t packet_type = data[12]; | ||
| 126 | data += 13; | ||
| 127 | len -= 13; | ||
| 128 | |||
| 129 | // map PacketLogger packet type onto PCAP direction flag and hci_h4_type | ||
| 130 | uint8_t direction_in = 0; | ||
| 131 | uint8_t hci_h4_type = 0xff; | ||
| 132 | switch(packet_type) { | ||
| 133 | case HCI_COMMAND: | ||
| 134 | hci_h4_type = 0x01; | ||
| 135 | direction_in = 0; | ||
| 136 | break; | ||
| 137 | case SENT_ACL_DATA: | ||
| 138 | hci_h4_type = 0x02; | ||
| 139 | direction_in = 0; | ||
| 140 | break; | ||
| 141 | case RECV_ACL_DATA: | ||
| 142 | hci_h4_type = 0x02; | ||
| 143 | direction_in = 1; | ||
| 144 | break; | ||
| 145 | case SENT_SCO_DATA: | ||
| 146 | hci_h4_type = 0x03; | ||
| 147 | direction_in = 0; | ||
| 148 | break; | ||
| 149 | case RECV_SCO_DATA: | ||
| 150 | hci_h4_type = 0x03; | ||
| 151 | direction_in = 1; | ||
| 152 | break; | ||
| 153 | case HCI_EVENT: | ||
| 154 | hci_h4_type = 0x04; | ||
| 155 | direction_in = 1; | ||
| 156 | break; | ||
| 157 | default: | ||
| 158 | // unknown packet logger type, drop packet | ||
| 159 | return; | ||
| 160 | } | ||
| 161 | |||
| 162 | // setup pcap record header, 4 byte direction flag, 1 byte HCI H4 packet type, data | ||
| 163 | uint8_t pcap_record_header[21]; | ||
| 164 | big_endian_store_32(pcap_record_header, 0, ts_secs); // Timestamp seconds | ||
| 165 | big_endian_store_32(pcap_record_header, 4, ts_us); // Timestamp microseconds | ||
| 166 | big_endian_store_32(pcap_record_header, 8, 4 + 1 + len); // Captured Packet Length | ||
| 167 | big_endian_store_32(pcap_record_header, 12, 4 + 1 + len); // Original Packet Length | ||
| 168 | big_endian_store_32(pcap_record_header, 16, direction_in); // Direction: Incoming = 1 | ||
| 169 | pcap_record_header[20] = hci_h4_type; | ||
| 170 | |||
| 171 | // write header | ||
| 172 | (void) fwrite(pcap_record_header, 1, sizeof(pcap_record_header), packetlogger_file); | ||
| 173 | |||
| 174 | // write packet | ||
| 175 | (void) fwrite(data, 1, len, packetlogger_file); | ||
| 176 | |||
| 177 | // flush | ||
| 178 | (void) fflush(packetlogger_file); | ||
| 179 | } | ||
| 180 | |||
| 181 | /** | ||
| 182 | * Disable HCI log capture | ||
| 183 | */ | ||
| 184 | static void stop_logging(void) | ||
| 185 | { | ||
| 186 | fflush(NULL); | ||
| 187 | |||
| 188 | if (bt_packet_logger) { | ||
| 189 | bt_packet_logger_client_free(bt_packet_logger); | ||
| 190 | bt_packet_logger = NULL; | ||
| 191 | } | ||
| 192 | |||
| 193 | if (device) { | ||
| 194 | idevice_free(device); | ||
| 195 | device = NULL; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | /** | ||
| 200 | * Enable HCI log capture | ||
| 201 | */ | ||
| 202 | static int start_logging(void) | ||
| 203 | { | ||
| 204 | idevice_error_t ret = idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX); | ||
| 205 | if (ret != IDEVICE_E_SUCCESS) { | ||
| 206 | fprintf(stderr, "Device with udid %s not found!?\n", udid); | ||
| 207 | return -1; | ||
| 208 | } | ||
| 209 | |||
| 210 | lockdownd_client_t lockdown = NULL; | ||
| 211 | lockdownd_error_t lerr = lockdownd_client_new_with_handshake(device, &lockdown, TOOL_NAME); | ||
| 212 | if (lerr != LOCKDOWN_E_SUCCESS) { | ||
| 213 | fprintf(stderr, "ERROR: Could not connect to lockdownd: %d\n", lerr); | ||
| 214 | idevice_free(device); | ||
| 215 | device = NULL; | ||
| 216 | return -1; | ||
| 217 | } | ||
| 218 | |||
| 219 | /* start bt_packet_logger service */ | ||
| 220 | bt_packet_logger_client_start_service(device, &bt_packet_logger, TOOL_NAME); | ||
| 221 | |||
| 222 | /* start capturing bt_packet_logger */ | ||
| 223 | void (*callback)(uint8_t * data, uint16_t len, void *user_data); | ||
| 224 | switch (log_format){ | ||
| 225 | case LOG_FORMAT_PCAP: | ||
| 226 | callback = bt_packet_logger_callback_pcap; | ||
| 227 | break; | ||
| 228 | case LOG_FORMAT_PACKETLOGGER: | ||
| 229 | callback = bt_packet_logger_callback_packetlogger; | ||
| 230 | break; | ||
| 231 | default: | ||
| 232 | assert(0); | ||
| 233 | return 0; | ||
| 234 | } | ||
| 235 | bt_packet_logger_error_t serr = bt_packet_logger_start_capture(bt_packet_logger, callback, NULL); | ||
| 236 | if (serr != BT_PACKET_LOGGER_E_SUCCESS) { | ||
| 237 | fprintf(stderr, "ERROR: Unable to start capturing bt_packet_logger.\n"); | ||
| 238 | bt_packet_logger_client_free(bt_packet_logger); | ||
| 239 | bt_packet_logger = NULL; | ||
| 240 | idevice_free(device); | ||
| 241 | device = NULL; | ||
| 242 | return -1; | ||
| 243 | } | ||
| 244 | |||
| 245 | fprintf(stderr, "[connected:%s]\n", udid); | ||
| 246 | fflush(stderr); | ||
| 247 | |||
| 248 | return 0; | ||
| 249 | } | ||
| 250 | |||
| 251 | /** | ||
| 252 | * Callback for device events | ||
| 253 | */ | ||
| 254 | static void device_event_cb(const idevice_event_t* event, void* userdata) | ||
| 255 | { | ||
| 256 | if (use_network && event->conn_type != CONNECTION_NETWORK) { | ||
| 257 | return; | ||
| 258 | } else if (!use_network && event->conn_type != CONNECTION_USBMUXD) { | ||
| 259 | return; | ||
| 260 | } | ||
| 261 | if (event->event == IDEVICE_DEVICE_ADD) { | ||
| 262 | if (!bt_packet_logger) { | ||
| 263 | if (!udid) { | ||
| 264 | udid = strdup(event->udid); | ||
| 265 | } | ||
| 266 | if (strcmp(udid, event->udid) == 0) { | ||
| 267 | if (start_logging() != 0) { | ||
| 268 | fprintf(stderr, "Could not start logger for udid %s\n", udid); | ||
| 269 | } | ||
| 270 | } | ||
| 271 | } | ||
| 272 | } else if (event->event == IDEVICE_DEVICE_REMOVE) { | ||
| 273 | if (bt_packet_logger && (strcmp(udid, event->udid) == 0)) { | ||
| 274 | stop_logging(); | ||
| 275 | fprintf(stderr, "[disconnected:%s]\n", udid); | ||
| 276 | if (exit_on_disconnect) { | ||
| 277 | quit_flag++; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | /** | ||
| 284 | * signal handler function for cleaning up properly | ||
| 285 | */ | ||
| 286 | static void clean_exit(int sig) | ||
| 287 | { | ||
| 288 | fprintf(stderr, "\nExiting...\n"); | ||
| 289 | quit_flag++; | ||
| 290 | } | ||
| 291 | |||
| 292 | /** | ||
| 293 | * print usage information | ||
| 294 | */ | ||
| 295 | static void print_usage(int argc, char **argv, int is_error) | ||
| 296 | { | ||
| 297 | char *name = NULL; | ||
| 298 | name = strrchr(argv[0], '/'); | ||
| 299 | fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] <FILE>\n", (name ? name + 1: argv[0])); | ||
| 300 | fprintf(is_error ? stderr : stdout, | ||
| 301 | "\n" \ | ||
| 302 | "Capture HCI packets from a connected device.\n" \ | ||
| 303 | "\n" \ | ||
| 304 | "OPTIONS:\n" \ | ||
| 305 | " -u, --udid UDID target specific device by UDID\n" \ | ||
| 306 | " -n, --network connect to network device\n" \ | ||
| 307 | " -f, --format FORMAT logging format: packetlogger (default) or pcap\n" \ | ||
| 308 | " -x, --exit exit when device disconnects\n" \ | ||
| 309 | " -h, --help prints usage information\n" \ | ||
| 310 | " -d, --debug enable communication debugging\n" \ | ||
| 311 | " -v, --version prints version information\n" \ | ||
| 312 | "\n" \ | ||
| 313 | "Homepage: <" PACKAGE_URL ">\n" | ||
| 314 | "Bug Reports: <" PACKAGE_BUGREPORT ">\n" | ||
| 315 | ); | ||
| 316 | } | ||
| 317 | |||
| 318 | /** | ||
| 319 | * Program entry | ||
| 320 | */ | ||
| 321 | int main(int argc, char *argv[]) | ||
| 322 | { | ||
| 323 | int c = 0; | ||
| 324 | const struct option longopts[] = { | ||
| 325 | { "debug", no_argument, NULL, 'd' }, | ||
| 326 | { "help", no_argument, NULL, 'h' }, | ||
| 327 | { "udid", required_argument, NULL, 'u' }, | ||
| 328 | { "format", required_argument, NULL, 'f' }, | ||
| 329 | { "network", no_argument, NULL, 'n' }, | ||
| 330 | { "exit", no_argument, NULL, 'x' }, | ||
| 331 | { "version", no_argument, NULL, 'v' }, | ||
| 332 | { NULL, 0, NULL, 0} | ||
| 333 | }; | ||
| 334 | |||
| 335 | signal(SIGINT, clean_exit); | ||
| 336 | signal(SIGTERM, clean_exit); | ||
| 337 | #ifndef _WIN32 | ||
| 338 | signal(SIGQUIT, clean_exit); | ||
| 339 | signal(SIGPIPE, SIG_IGN); | ||
| 340 | #endif | ||
| 341 | |||
| 342 | while ((c = getopt_long(argc, argv, "dhu:f:nxv", longopts, NULL)) != -1) { | ||
| 343 | switch (c) { | ||
| 344 | case 'd': | ||
| 345 | idevice_set_debug_level(1); | ||
| 346 | break; | ||
| 347 | case 'u': | ||
| 348 | if (!*optarg) { | ||
| 349 | fprintf(stderr, "ERROR: UDID must not be empty!\n"); | ||
| 350 | print_usage(argc, argv, 1); | ||
| 351 | return 2; | ||
| 352 | } | ||
| 353 | free(udid); | ||
| 354 | udid = strdup(optarg); | ||
| 355 | break; | ||
| 356 | case 'f': | ||
| 357 | if (!*optarg) { | ||
| 358 | fprintf(stderr, "ERROR: FORMAT must not be empty!\n"); | ||
| 359 | print_usage(argc, argv, 1); | ||
| 360 | return 2; | ||
| 361 | } | ||
| 362 | free(log_format_string); | ||
| 363 | log_format_string = strdup(optarg); | ||
| 364 | break; | ||
| 365 | case 'n': | ||
| 366 | use_network = 1; | ||
| 367 | break; | ||
| 368 | case 'x': | ||
| 369 | exit_on_disconnect = 1; | ||
| 370 | break; | ||
| 371 | case 'h': | ||
| 372 | print_usage(argc, argv, 0); | ||
| 373 | return 0; | ||
| 374 | case 'v': | ||
| 375 | printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); | ||
| 376 | return 0; | ||
| 377 | default: | ||
| 378 | print_usage(argc, argv, 1); | ||
| 379 | return 2; | ||
| 380 | } | ||
| 381 | } | ||
| 382 | |||
| 383 | if (optind < argc) { | ||
| 384 | out_filename = argv[optind]; | ||
| 385 | // printf("Output File: %s\n", out_filename); | ||
| 386 | } | ||
| 387 | else { | ||
| 388 | print_usage(argc, argv, 1); | ||
| 389 | return 2; | ||
| 390 | } | ||
| 391 | |||
| 392 | if (log_format_string != NULL){ | ||
| 393 | if (strcmp("packetlogger", log_format_string) == 0){ | ||
| 394 | log_format = LOG_FORMAT_PACKETLOGGER; | ||
| 395 | } else if (strcmp("pcap", log_format_string) == 0){ | ||
| 396 | log_format = LOG_FORMAT_PCAP; | ||
| 397 | } else { | ||
| 398 | printf("Unknown logging format: '%s'\n", log_format_string); | ||
| 399 | print_usage(argc, argv, 1); | ||
| 400 | return 2; | ||
| 401 | } | ||
| 402 | } | ||
| 403 | |||
| 404 | int num = 0; | ||
| 405 | idevice_info_t *devices = NULL; | ||
| 406 | idevice_get_device_list_extended(&devices, &num); | ||
| 407 | idevice_device_list_extended_free(devices); | ||
| 408 | if (num == 0) { | ||
| 409 | if (!udid) { | ||
| 410 | fprintf(stderr, "No device found. Plug in a device or pass UDID with -u to wait for device to be available.\n"); | ||
| 411 | return -1; | ||
| 412 | } else { | ||
| 413 | fprintf(stderr, "Waiting for device with UDID %s to become available...\n", udid); | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | // support streaming to stdout | ||
| 418 | if (strcmp(out_filename, "-") == 0){ | ||
| 419 | packetlogger_file = stdout; | ||
| 420 | } else { | ||
| 421 | packetlogger_file = fopen(out_filename, "wb"); | ||
| 422 | } | ||
| 423 | |||
| 424 | |||
| 425 | if (packetlogger_file == NULL){ | ||
| 426 | fprintf(stderr, "Failed to open file %s, errno = %d\n", out_filename, errno); | ||
| 427 | return -2; | ||
| 428 | } | ||
| 429 | |||
| 430 | switch (log_format){ | ||
| 431 | case LOG_FORMAT_PCAP: | ||
| 432 | // printf("Output Format: PCAP\n"); | ||
| 433 | // write PCAP file header | ||
| 434 | (void) fwrite(&pcap_file_header, 1, sizeof(pcap_file_header), packetlogger_file); | ||
| 435 | break; | ||
| 436 | case LOG_FORMAT_PACKETLOGGER: | ||
| 437 | printf("Output Format: PacketLogger\n"); | ||
| 438 | break; | ||
| 439 | default: | ||
| 440 | assert(0); | ||
| 441 | return -2; | ||
| 442 | } | ||
| 443 | idevice_subscription_context_t context = NULL; | ||
| 444 | idevice_events_subscribe(&context, device_event_cb, NULL); | ||
| 445 | |||
| 446 | while (!quit_flag) { | ||
| 447 | sleep(1); | ||
| 448 | } | ||
| 449 | |||
| 450 | idevice_events_unsubscribe(context); | ||
| 451 | stop_logging(); | ||
| 452 | |||
| 453 | fclose(packetlogger_file); | ||
| 454 | |||
| 455 | free(udid); | ||
| 456 | |||
| 457 | return 0; | ||
| 458 | } | ||
