nfc_scene_mf_classic_dict_attack.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "../nfc_i.h"
  2. typedef enum {
  3. DictAttackStateIdle,
  4. DictAttackStateUserDictInProgress,
  5. DictAttackStateFlipperDictInProgress,
  6. } DictAttackState;
  7. bool nfc_dict_attack_worker_callback(NfcWorkerEvent event, void* context) {
  8. furi_assert(context);
  9. Nfc* nfc = context;
  10. view_dispatcher_send_custom_event(nfc->view_dispatcher, event);
  11. return true;
  12. }
  13. void nfc_dict_attack_dict_attack_result_callback(void* context) {
  14. furi_assert(context);
  15. Nfc* nfc = context;
  16. view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventDictAttackSkip);
  17. }
  18. static void nfc_scene_mf_classic_dict_attack_update_view(Nfc* nfc) {
  19. MfClassicData* data = &nfc->dev->dev_data.mf_classic_data;
  20. uint8_t sectors_read = 0;
  21. uint8_t keys_found = 0;
  22. // Calculate found keys and read sectors
  23. mf_classic_get_read_sectors_and_keys(data, &sectors_read, &keys_found);
  24. dict_attack_set_keys_found(nfc->dict_attack, keys_found);
  25. dict_attack_set_sector_read(nfc->dict_attack, sectors_read);
  26. }
  27. static void nfc_scene_mf_classic_dict_attack_prepare_view(Nfc* nfc, DictAttackState state) {
  28. MfClassicData* data = &nfc->dev->dev_data.mf_classic_data;
  29. NfcWorkerState worker_state = NfcWorkerStateReady;
  30. // Identify scene state
  31. if(state == DictAttackStateIdle) {
  32. if(mf_classic_dict_check_presence(MfClassicDictTypeUser)) {
  33. state = DictAttackStateUserDictInProgress;
  34. } else {
  35. state = DictAttackStateFlipperDictInProgress;
  36. }
  37. } else if(state == DictAttackStateUserDictInProgress) {
  38. state = DictAttackStateFlipperDictInProgress;
  39. }
  40. // Setup view
  41. if(state == DictAttackStateUserDictInProgress) {
  42. worker_state = NfcWorkerStateMfClassicUserDictAttack;
  43. dict_attack_set_header(nfc->dict_attack, "Mf Classic User Dict.");
  44. } else if(state == DictAttackStateFlipperDictInProgress) {
  45. worker_state = NfcWorkerStateMfClassicFlipperDictAttack;
  46. dict_attack_set_header(nfc->dict_attack, "Mf Classic Flipper Dict.");
  47. }
  48. scene_manager_set_scene_state(nfc->scene_manager, NfcSceneMfClassicDictAttack, state);
  49. dict_attack_set_callback(nfc->dict_attack, nfc_dict_attack_dict_attack_result_callback, nfc);
  50. dict_attack_set_current_sector(nfc->dict_attack, 0);
  51. dict_attack_set_card_detected(nfc->dict_attack, data->type);
  52. nfc_scene_mf_classic_dict_attack_update_view(nfc);
  53. nfc_worker_start(
  54. nfc->worker, worker_state, &nfc->dev->dev_data, nfc_dict_attack_worker_callback, nfc);
  55. }
  56. void nfc_scene_mf_classic_dict_attack_on_enter(void* context) {
  57. Nfc* nfc = context;
  58. nfc_scene_mf_classic_dict_attack_prepare_view(nfc, DictAttackStateIdle);
  59. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewDictAttack);
  60. nfc_blink_start(nfc);
  61. }
  62. bool nfc_scene_mf_classic_dict_attack_on_event(void* context, SceneManagerEvent event) {
  63. Nfc* nfc = context;
  64. MfClassicData* data = &nfc->dev->dev_data.mf_classic_data;
  65. bool consumed = false;
  66. uint32_t state =
  67. scene_manager_get_scene_state(nfc->scene_manager, NfcSceneMfClassicDictAttack);
  68. if(event.type == SceneManagerEventTypeCustom) {
  69. if(event.event == NfcWorkerEventSuccess) {
  70. if(state == DictAttackStateUserDictInProgress) {
  71. nfc_worker_stop(nfc->worker);
  72. nfc_scene_mf_classic_dict_attack_prepare_view(nfc, state);
  73. consumed = true;
  74. } else {
  75. notification_message(nfc->notifications, &sequence_success);
  76. scene_manager_next_scene(nfc->scene_manager, NfcSceneMfClassicReadSuccess);
  77. consumed = true;
  78. }
  79. } else if(event.event == NfcWorkerEventAborted) {
  80. if(state == DictAttackStateUserDictInProgress) {
  81. nfc_scene_mf_classic_dict_attack_prepare_view(nfc, state);
  82. consumed = true;
  83. } else {
  84. notification_message(nfc->notifications, &sequence_success);
  85. scene_manager_next_scene(nfc->scene_manager, NfcSceneMfClassicReadSuccess);
  86. consumed = true;
  87. }
  88. } else if(event.event == NfcWorkerEventCardDetected) {
  89. dict_attack_set_card_detected(nfc->dict_attack, data->type);
  90. consumed = true;
  91. } else if(event.event == NfcWorkerEventNoCardDetected) {
  92. dict_attack_set_card_removed(nfc->dict_attack);
  93. consumed = true;
  94. } else if(event.event == NfcWorkerEventFoundKeyA) {
  95. dict_attack_inc_keys_found(nfc->dict_attack);
  96. consumed = true;
  97. } else if(event.event == NfcWorkerEventFoundKeyB) {
  98. dict_attack_inc_keys_found(nfc->dict_attack);
  99. consumed = true;
  100. } else if(event.event == NfcWorkerEventNewSector) {
  101. nfc_scene_mf_classic_dict_attack_update_view(nfc);
  102. dict_attack_inc_current_sector(nfc->dict_attack);
  103. consumed = true;
  104. } else if(event.event == NfcCustomEventDictAttackSkip) {
  105. if(state == DictAttackStateUserDictInProgress) {
  106. nfc_worker_stop(nfc->worker);
  107. consumed = true;
  108. } else if(state == DictAttackStateFlipperDictInProgress) {
  109. nfc_worker_stop(nfc->worker);
  110. consumed = true;
  111. }
  112. }
  113. } else if(event.type == SceneManagerEventTypeBack) {
  114. scene_manager_next_scene(nfc->scene_manager, NfcSceneExitConfirm);
  115. consumed = true;
  116. }
  117. return consumed;
  118. }
  119. void nfc_scene_mf_classic_dict_attack_on_exit(void* context) {
  120. Nfc* nfc = context;
  121. // Stop worker
  122. nfc_worker_stop(nfc->worker);
  123. dict_attack_reset(nfc->dict_attack);
  124. nfc_blink_stop(nfc);
  125. }