swiftpair.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "swiftpair.h"
  2. #include "_protocols.h"
  3. #include <storage/storage.h>
  4. #include <toolbox/stream/file_stream.h>
  5. // Hacked together by @Willy-JL and @Spooks4576
  6. // Documentation at https://learn.microsoft.com/en-us/windows-hardware/design/component-guidelines/bluetooth-swift-pair
  7. static char names[256][sizeof(((SwiftpairCfg*)0)->name)];
  8. static uint8_t names_count = 0;
  9. static const char* get_name(const Payload* payload) {
  10. UNUSED(payload);
  11. return "SwiftPair";
  12. }
  13. static const char* random_name() {
  14. if(names_count == 0) {
  15. // Fill random names
  16. Storage* storage = furi_record_open(RECORD_STORAGE);
  17. Stream* stream = file_stream_alloc(storage);
  18. FuriString* line = furi_string_alloc();
  19. if(!storage_file_exists(storage, APP_DATA_PATH("swiftpair.txt"))) {
  20. // Copy default names
  21. storage_common_copy(
  22. storage, APP_ASSETS_PATH("swiftpair.txt"), APP_DATA_PATH("swiftpair.txt"));
  23. }
  24. if(file_stream_open(
  25. stream, APP_DATA_PATH("swiftpair.txt"), FSAM_READ, FSOM_OPEN_EXISTING)) {
  26. while(stream_read_line(stream, line)) {
  27. furi_string_replace_all(line, "\r", "");
  28. furi_string_replace_all(line, "\n", "");
  29. if(furi_string_size(line)) {
  30. strlcpy(names[names_count++], furi_string_get_cstr(line), sizeof(names[0]));
  31. }
  32. }
  33. }
  34. furi_string_free(line);
  35. file_stream_close(stream);
  36. stream_free(stream);
  37. furi_record_close(RECORD_STORAGE);
  38. if(names_count == 0) {
  39. // Add fallback if list is empty
  40. strlcpy(names[names_count++], "SwiftPair", sizeof(names[0]));
  41. }
  42. }
  43. return names[rand() % names_count];
  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 = random_name();
  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. view_dispatcher_send_custom_event(ctx->view_dispatcher, 0);
  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 == SceneManagerEventTypeCustom) {
  136. scene_manager_previous_scene(ctx->scene_manager);
  137. return true;
  138. }
  139. if(event.type == SceneManagerEventTypeBack) {
  140. payload->mode = PayloadModeRandom;
  141. }
  142. return false;
  143. }
  144. void scene_swiftpair_name_on_exit(void* _ctx) {
  145. Ctx* ctx = _ctx;
  146. text_input_reset(ctx->text_input);
  147. }