mifare_nested.c 14 KB

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