mifare_nested.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include "mifare_nested_i.h"
  2. #include <gui/elements.h>
  3. bool mifare_nested_custom_event_callback(void* context, uint32_t event) {
  4. furi_assert(context);
  5. MifareNested* mifare_nested = context;
  6. return scene_manager_handle_custom_event(mifare_nested->scene_manager, event);
  7. }
  8. bool mifare_nested_back_event_callback(void* context) {
  9. furi_assert(context);
  10. MifareNested* mifare_nested = context;
  11. return scene_manager_handle_back_event(mifare_nested->scene_manager);
  12. }
  13. void mifare_nested_tick_event_callback(void* context) {
  14. furi_assert(context);
  15. MifareNested* mifare_nested = context;
  16. scene_manager_handle_tick_event(mifare_nested->scene_manager);
  17. }
  18. void mifare_nested_show_loading_popup(void* context, bool show) {
  19. MifareNested* mifare_nested = context;
  20. TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);
  21. if(show) {
  22. // Raise timer priority so that animations can play
  23. vTaskPrioritySet(timer_task, configMAX_PRIORITIES - 1);
  24. view_dispatcher_switch_to_view(mifare_nested->view_dispatcher, MifareNestedViewLoading);
  25. } else {
  26. // Restore default timer priority
  27. vTaskPrioritySet(timer_task, configTIMER_TASK_PRIORITY);
  28. }
  29. }
  30. NestedState* collection_alloc() {
  31. NestedState* nested = malloc(sizeof(NestedState));
  32. nested->view = view_alloc();
  33. view_allocate_model(nested->view, ViewModelTypeLocking, sizeof(NestedAttackViewModel));
  34. with_view_model(
  35. nested->view,
  36. NestedAttackViewModel * model,
  37. {
  38. model->header = furi_string_alloc();
  39. furi_string_set(model->header, "Collecting nonces");
  40. model->keys_count = 0;
  41. model->lost_tag = false;
  42. model->calibrating = false;
  43. model->need_prediction = false;
  44. },
  45. false);
  46. return nested;
  47. }
  48. CheckKeysState* check_keys_alloc() {
  49. CheckKeysState* state = malloc(sizeof(CheckKeysState));
  50. state->view = view_alloc();
  51. view_allocate_model(state->view, ViewModelTypeLocking, sizeof(CheckKeysViewModel));
  52. with_view_model(
  53. state->view,
  54. CheckKeysViewModel * model,
  55. {
  56. model->header = furi_string_alloc();
  57. furi_string_set(model->header, "Checking keys");
  58. model->lost_tag = false;
  59. },
  60. false);
  61. return state;
  62. }
  63. static void nested_draw_callback(Canvas* canvas, void* model) {
  64. NestedAttackViewModel* m = model;
  65. if(m->lost_tag) {
  66. canvas_set_font(canvas, FontPrimary);
  67. canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Lost the tag!");
  68. canvas_set_font(canvas, FontSecondary);
  69. elements_multiline_text_aligned(
  70. canvas, 64, 23, AlignCenter, AlignTop, "Make sure the tag is\npositioned correctly.");
  71. } else if(m->calibrating) {
  72. canvas_set_font(canvas, FontPrimary);
  73. canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Calibrating...");
  74. canvas_set_font(canvas, FontSecondary);
  75. if(!m->need_prediction) {
  76. elements_multiline_text_aligned(
  77. canvas, 64, 23, AlignCenter, AlignTop, "Don't touch or move\nFlipper/Tag!");
  78. } else {
  79. elements_multiline_text_aligned(
  80. canvas, 64, 18, AlignCenter, AlignTop, "Don't touch or move tag!");
  81. canvas_set_font(canvas, FontPrimary);
  82. elements_multiline_text_aligned(
  83. canvas, 64, 30, AlignCenter, AlignTop, "Calibration will take\nmore time");
  84. }
  85. } else {
  86. char draw_str[32] = {};
  87. canvas_set_font(canvas, FontPrimary);
  88. canvas_draw_str_aligned(
  89. canvas, 64, 2, AlignCenter, AlignTop, furi_string_get_cstr(m->header));
  90. canvas_set_font(canvas, FontSecondary);
  91. float progress =
  92. m->keys_count == 0 ? 0 : (float)(m->nonces_collected) / (float)(m->keys_count);
  93. if(progress > 1.0) {
  94. progress = 1.0;
  95. }
  96. elements_progress_bar(canvas, 5, 15, 120, progress);
  97. canvas_set_font(canvas, FontSecondary);
  98. snprintf(
  99. draw_str,
  100. sizeof(draw_str),
  101. "Nonces collected: %lu/%lu",
  102. m->nonces_collected,
  103. m->keys_count);
  104. canvas_draw_str_aligned(canvas, 1, 28, AlignLeft, AlignTop, draw_str);
  105. }
  106. elements_button_center(canvas, "Stop");
  107. }
  108. static void check_keys_draw_callback(Canvas* canvas, void* model) {
  109. CheckKeysViewModel* m = model;
  110. if(m->lost_tag) {
  111. canvas_set_font(canvas, FontPrimary);
  112. canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Lost the tag!");
  113. canvas_set_font(canvas, FontSecondary);
  114. elements_multiline_text_aligned(
  115. canvas, 64, 23, AlignCenter, AlignTop, "Make sure the tag is\npositioned correctly.");
  116. } else if(m->processing_keys) {
  117. canvas_set_font(canvas, FontPrimary);
  118. canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Processing keys...");
  119. canvas_set_font(canvas, FontSecondary);
  120. elements_multiline_text_aligned(
  121. canvas, 64, 23, AlignCenter, AlignTop, "Checking which keys you\nalready have...");
  122. } else {
  123. char draw_str[32] = {};
  124. char draw_sub_str[32] = {};
  125. canvas_set_font(canvas, FontPrimary);
  126. canvas_draw_str_aligned(
  127. canvas, 64, 2, AlignCenter, AlignTop, furi_string_get_cstr(m->header));
  128. canvas_set_font(canvas, FontSecondary);
  129. float progress = m->keys_count == 0 ? 0 :
  130. (float)(m->keys_checked) / (float)(m->keys_count);
  131. if(progress > 1.0) {
  132. progress = 1.0;
  133. }
  134. elements_progress_bar(canvas, 5, 15, 120, progress);
  135. canvas_set_font(canvas, FontSecondary);
  136. snprintf(
  137. draw_str, sizeof(draw_str), "Keys checked: %lu/%lu", m->keys_checked, m->keys_count);
  138. canvas_draw_str_aligned(canvas, 1, 28, AlignLeft, AlignTop, draw_str);
  139. snprintf(
  140. draw_sub_str,
  141. sizeof(draw_sub_str),
  142. "Keys found: %lu/%lu",
  143. m->keys_found,
  144. m->keys_total);
  145. canvas_draw_str_aligned(canvas, 1, 40, AlignLeft, AlignTop, draw_sub_str);
  146. }
  147. elements_button_center(canvas, "Stop");
  148. }
  149. static bool nested_input_callback(InputEvent* event, void* context) {
  150. MifareNested* mifare_nested = context;
  151. bool consumed = false;
  152. if(event->type == InputTypeShort && (event->key == InputKeyBack || event->key == InputKeyOk)) {
  153. scene_manager_search_and_switch_to_previous_scene(mifare_nested->scene_manager, 0);
  154. consumed = true;
  155. }
  156. return consumed;
  157. }
  158. MifareNested* mifare_nested_alloc() {
  159. MifareNested* mifare_nested = malloc(sizeof(MifareNested));
  160. mifare_nested->worker = mifare_nested_worker_alloc();
  161. mifare_nested->view_dispatcher = view_dispatcher_alloc();
  162. mifare_nested->scene_manager =
  163. scene_manager_alloc(&mifare_nested_scene_handlers, mifare_nested);
  164. view_dispatcher_enable_queue(mifare_nested->view_dispatcher);
  165. view_dispatcher_set_event_callback_context(mifare_nested->view_dispatcher, mifare_nested);
  166. view_dispatcher_set_custom_event_callback(
  167. mifare_nested->view_dispatcher, mifare_nested_custom_event_callback);
  168. view_dispatcher_set_navigation_event_callback(
  169. mifare_nested->view_dispatcher, mifare_nested_back_event_callback);
  170. view_dispatcher_set_tick_event_callback(
  171. mifare_nested->view_dispatcher, mifare_nested_tick_event_callback, 100);
  172. // Nfc device
  173. mifare_nested->nfc_dev = nfc_device_alloc();
  174. // Open GUI record
  175. mifare_nested->gui = furi_record_open(RECORD_GUI);
  176. view_dispatcher_attach_to_gui(
  177. mifare_nested->view_dispatcher, mifare_nested->gui, ViewDispatcherTypeFullscreen);
  178. // Open Notification record
  179. mifare_nested->notifications = furi_record_open(RECORD_NOTIFICATION);
  180. // Submenu
  181. mifare_nested->submenu = submenu_alloc();
  182. view_dispatcher_add_view(
  183. mifare_nested->view_dispatcher,
  184. MifareNestedViewMenu,
  185. submenu_get_view(mifare_nested->submenu));
  186. // Popup
  187. mifare_nested->popup = popup_alloc();
  188. view_dispatcher_add_view(
  189. mifare_nested->view_dispatcher,
  190. MifareNestedViewPopup,
  191. popup_get_view(mifare_nested->popup));
  192. // Loading
  193. mifare_nested->loading = loading_alloc();
  194. view_dispatcher_add_view(
  195. mifare_nested->view_dispatcher,
  196. MifareNestedViewLoading,
  197. loading_get_view(mifare_nested->loading));
  198. // Text Input
  199. mifare_nested->text_input = text_input_alloc();
  200. view_dispatcher_add_view(
  201. mifare_nested->view_dispatcher,
  202. MifareNestedViewTextInput,
  203. text_input_get_view(mifare_nested->text_input));
  204. // Custom Widget
  205. mifare_nested->widget = widget_alloc();
  206. view_dispatcher_add_view(
  207. mifare_nested->view_dispatcher,
  208. MifareNestedViewWidget,
  209. widget_get_view(mifare_nested->widget));
  210. // Nested attack state
  211. NestedState* plugin_state = collection_alloc();
  212. view_set_context(plugin_state->view, mifare_nested);
  213. mifare_nested->nested_state = plugin_state;
  214. view_dispatcher_add_view(
  215. mifare_nested->view_dispatcher, MifareNestedViewCollecting, plugin_state->view);
  216. // Check keys attack state
  217. CheckKeysState* keys_state = check_keys_alloc();
  218. view_set_context(keys_state->view, mifare_nested);
  219. mifare_nested->keys_state = keys_state;
  220. view_dispatcher_add_view(
  221. mifare_nested->view_dispatcher, MifareNestedViewCheckKeys, keys_state->view);
  222. KeyInfo_t* key_info = malloc(sizeof(KeyInfo_t));
  223. mifare_nested->keys = key_info;
  224. view_set_draw_callback(plugin_state->view, nested_draw_callback);
  225. view_set_input_callback(plugin_state->view, nested_input_callback);
  226. view_set_draw_callback(keys_state->view, check_keys_draw_callback);
  227. view_set_input_callback(keys_state->view, nested_input_callback);
  228. mifare_nested->collecting_type = MifareNestedWorkerStateReady;
  229. mifare_nested->run = NestedRunIdle;
  230. return mifare_nested;
  231. }
  232. void mifare_nested_free(MifareNested* mifare_nested) {
  233. furi_assert(mifare_nested);
  234. // Nfc device
  235. nfc_device_free(mifare_nested->nfc_dev);
  236. // Submenu
  237. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewMenu);
  238. submenu_free(mifare_nested->submenu);
  239. // Popup
  240. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewPopup);
  241. popup_free(mifare_nested->popup);
  242. // Loading
  243. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewLoading);
  244. loading_free(mifare_nested->loading);
  245. // TextInput
  246. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewTextInput);
  247. text_input_free(mifare_nested->text_input);
  248. // Custom Widget
  249. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewWidget);
  250. widget_free(mifare_nested->widget);
  251. // Nested
  252. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewCollecting);
  253. // Check keys
  254. view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewCheckKeys);
  255. free(mifare_nested->nonces);
  256. free(mifare_nested->nested_state);
  257. // Worker
  258. mifare_nested_worker_stop(mifare_nested->worker);
  259. mifare_nested_worker_free(mifare_nested->worker);
  260. // View Dispatcher
  261. view_dispatcher_free(mifare_nested->view_dispatcher);
  262. // Scene Manager
  263. scene_manager_free(mifare_nested->scene_manager);
  264. // GUI
  265. furi_record_close(RECORD_GUI);
  266. mifare_nested->gui = NULL;
  267. // Notifications
  268. furi_record_close(RECORD_NOTIFICATION);
  269. mifare_nested->notifications = NULL;
  270. free(mifare_nested);
  271. }
  272. void mifare_nested_blink_start(MifareNested* mifare_nested) {
  273. notification_message(mifare_nested->notifications, &mifare_nested_sequence_blink_start_blue);
  274. }
  275. void mifare_nested_blink_calibration_start(MifareNested* mifare_nested) {
  276. notification_message(
  277. mifare_nested->notifications, &mifare_nested_sequence_blink_start_magenta);
  278. }
  279. void mifare_nested_blink_nonce_collection_start(MifareNested* mifare_nested) {
  280. notification_message(mifare_nested->notifications, &mifare_nested_sequence_blink_start_yellow);
  281. }
  282. void mifare_nested_blink_stop(MifareNested* mifare_nested) {
  283. notification_message(mifare_nested->notifications, &mifare_nested_sequence_blink_stop);
  284. }
  285. int32_t mifare_nested_app(void* p) {
  286. UNUSED(p);
  287. MifareNested* mifare_nested = mifare_nested_alloc();
  288. scene_manager_next_scene(mifare_nested->scene_manager, MifareNestedSceneStart);
  289. view_dispatcher_run(mifare_nested->view_dispatcher);
  290. mifare_nested_free(mifare_nested);
  291. return 0;
  292. }