mifare_nested.c 14 KB

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