mifare_fuzzer_scene_emulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "../mifare_fuzzer_i.h"
  2. #include <notification/notification.h>
  3. #include <notification/notification_messages.h>
  4. uint8_t tick_counter = 0;
  5. uint8_t attack_step = 0;
  6. uint8_t id_uid_test[9][7] = {
  7. {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17},
  8. {0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28},
  9. {0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39},
  10. {0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a},
  11. {0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b},
  12. {0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c},
  13. {0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d},
  14. {0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e},
  15. {0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f},
  16. };
  17. /// @brief mifare_fuzzer_scene_emulator_callback()
  18. /// @param event
  19. /// @param context
  20. static void mifare_fuzzer_scene_emulator_callback(MifareFuzzerEvent event, void* context) {
  21. //FURI_LOG_D(TAG, "mifare_fuzzer_scene_emulator_callback()");
  22. furi_assert(context);
  23. MifareFuzzerApp* app = context;
  24. view_dispatcher_send_custom_event(app->view_dispatcher, event);
  25. }
  26. /// @brief mifare_fuzzer_scene_emulator_on_enter()
  27. /// @param context
  28. void mifare_fuzzer_scene_emulator_on_enter(void* context) {
  29. //FURI_LOG_D(TAG, "mifare_fuzzer_scene_emulator_on_enter()");
  30. MifareFuzzerApp* app = context;
  31. MifareFuzzerEmulator* emulator = app->emulator_view;
  32. // init callback
  33. mifare_fuzzer_emulator_set_callback(emulator, mifare_fuzzer_scene_emulator_callback, app);
  34. // init ticks
  35. tick_counter = 0;
  36. mifare_fuzzer_emulator_set_tick_num(app->emulator_view, tick_counter);
  37. emulator->ticks_between_cards = MIFARE_FUZZER_DEFAULT_TICKS_BETWEEN_CARDS;
  38. mifare_fuzzer_emulator_set_ticks_between_cards(
  39. app->emulator_view, emulator->ticks_between_cards);
  40. // init default card data
  41. Iso14443_3aData nfc_data;
  42. nfc_data.atqa[0] = 0x00;
  43. nfc_data.atqa[1] = 0x00;
  44. nfc_data.sak = 0x00;
  45. if(app->card == MifareCardUltralight) {
  46. nfc_data.uid_len = 0x07;
  47. } else {
  48. nfc_data.uid_len = 0x04;
  49. }
  50. for(uint32_t i = 0; i < nfc_data.uid_len; i++) {
  51. nfc_data.uid[i] = 0x00;
  52. }
  53. mifare_fuzzer_emulator_set_nfc_data(app->emulator_view, nfc_data);
  54. // init other vars
  55. attack_step = 0;
  56. // switch to view
  57. view_dispatcher_switch_to_view(app->view_dispatcher, MifareFuzzerViewEmulator);
  58. }
  59. /// @brief mifare_fuzzer_scene_emulator_on_event()
  60. /// @param context
  61. /// @param event
  62. /// @return
  63. bool mifare_fuzzer_scene_emulator_on_event(void* context, SceneManagerEvent event) {
  64. //FURI_LOG_D(TAG, "mifare_fuzzer_scene_emulator_on_event()");
  65. MifareFuzzerApp* app = context;
  66. MifareFuzzerEmulator* emulator = app->emulator_view;
  67. bool consumed = false;
  68. if(event.type == SceneManagerEventTypeCustom) {
  69. if(event.event == MifareFuzzerEventStartAttack) {
  70. NfcDevice* nfc_device = NULL;
  71. bool nfc_device_parsed = false;
  72. if(app->card_file_path) {
  73. }
  74. Iso14443_3aData* nfc_data = iso14443_3a_alloc();
  75. if(app->nfc_device_parsed) {
  76. iso14443_3a_copy(
  77. nfc_data,
  78. nfc_device_get_data(app->worker->nfc_device, NfcProtocolIso14443_3a));
  79. }
  80. // Stop worker
  81. mifare_fuzzer_worker_stop(app->worker);
  82. // Set card type
  83. // TODO: Move somewhere else, I do not like this to be there
  84. if(app->card == MifareCardClassic1k) {
  85. nfc_data->atqa[0] = 0x04;
  86. nfc_data->atqa[1] = 0x00;
  87. nfc_data->sak = 0x08;
  88. nfc_data->uid_len = 0x04;
  89. } else if(app->card == MifareCardClassic4k) {
  90. nfc_data->atqa[0] = 0x02;
  91. nfc_data->atqa[1] = 0x00;
  92. nfc_data->sak = 0x18;
  93. nfc_data->uid_len = 0x04;
  94. } else if(app->card == MifareCardUltralight) {
  95. nfc_data->atqa[0] = 0x44;
  96. nfc_data->atqa[1] = 0x00;
  97. nfc_data->sak = 0x00;
  98. nfc_data->uid_len = 0x07;
  99. }
  100. // Set UIDs
  101. if(app->attack == MifareFuzzerAttackTestValues) {
  102. // Load test UIDs
  103. for(uint8_t i = 0; i < nfc_data->uid_len; i++) {
  104. nfc_data->uid[i] = id_uid_test[attack_step][i];
  105. }
  106. // Next UIDs on next loop
  107. if(attack_step >= 8) {
  108. attack_step = 0;
  109. } else {
  110. attack_step++;
  111. }
  112. } else if(app->attack == MifareFuzzerAttackRandomValues) {
  113. if(app->card == MifareCardUltralight) {
  114. // First byte of a 7 byte UID is the manufacturer-code
  115. // https://github.com/Proxmark/proxmark3/blob/master/client/taginfo.c
  116. // https://stackoverflow.com/questions/37837730/mifare-cards-distinguish-between-4-byte-and-7-byte-uids
  117. // https://stackoverflow.com/questions/31233652/how-to-detect-manufacturer-from-nfc-tag-using-android
  118. // TODO: Manufacture-code must be selectable from a list
  119. // use a fixed manufacture-code for now: 0x04 = NXP Semiconductors Germany
  120. nfc_data->uid[0] = 0x04;
  121. for(uint8_t i = 1; i < nfc_data->uid_len; i++) {
  122. nfc_data->uid[i] = (furi_hal_random_get() & 0xFF);
  123. }
  124. } else {
  125. for(uint8_t i = 0; i < nfc_data->uid_len; i++) {
  126. nfc_data->uid[i] = (furi_hal_random_get() & 0xFF);
  127. }
  128. }
  129. } else if(app->attack == MifareFuzzerAttackLoadUidsFromFile) {
  130. //bool end_of_list = false;
  131. // read stream
  132. while(true) {
  133. furi_string_reset(app->uid_str);
  134. if(!stream_read_line(app->uids_stream, app->uid_str)) {
  135. // restart from beginning on empty line
  136. stream_rewind(app->uids_stream);
  137. continue;
  138. //end_of_list = true;
  139. }
  140. // Skip comments
  141. if(furi_string_get_char(app->uid_str, 0) == '#') continue;
  142. // Skip lines with invalid length
  143. if((furi_string_size(app->uid_str) != 9) &&
  144. (furi_string_size(app->uid_str) != 15))
  145. continue;
  146. break;
  147. }
  148. // TODO: stop on end of list?
  149. //if(end_of_list) break;
  150. // parse string to UID
  151. // TODO: a better validation on input?
  152. for(uint8_t i = 0; i < nfc_data->uid_len; i++) {
  153. if(i <= ((furi_string_size(app->uid_str) - 1) / 2)) {
  154. char temp_str[3];
  155. temp_str[0] = furi_string_get_cstr(app->uid_str)[i * 2];
  156. temp_str[1] = furi_string_get_cstr(app->uid_str)[i * 2 + 1];
  157. temp_str[2] = '\0';
  158. nfc_data->uid[i] = (uint8_t)strtol(temp_str, NULL, 16);
  159. } else {
  160. nfc_data->uid[i] = 0x00;
  161. }
  162. }
  163. }
  164. mifare_fuzzer_emulator_set_nfc_data(app->emulator_view, *nfc_data);
  165. if(nfc_device_parsed) {
  166. mifare_fuzzer_worker_set_nfc_device(app->worker, nfc_device);
  167. } else {
  168. mifare_fuzzer_worker_set_nfc_data(app->worker, *nfc_data);
  169. }
  170. // Reset tick_counter
  171. tick_counter = 0;
  172. mifare_fuzzer_emulator_set_tick_num(app->emulator_view, tick_counter);
  173. // Start worker
  174. mifare_fuzzer_worker_start(app->worker);
  175. if(nfc_device_parsed) {
  176. notification_message(app->notifications, &sequence_blink_start_magenta);
  177. }
  178. } else if(event.event == MifareFuzzerEventStopAttack) {
  179. //FURI_LOG_D(TAG, "mifare_fuzzer_scene_emulator_on_event() :: MifareFuzzerEventStopAttack");
  180. // Stop worker
  181. mifare_fuzzer_worker_stop(app->worker);
  182. notification_message(app->notifications, &sequence_blink_stop);
  183. } else if(event.event == MifareFuzzerEventIncrementTicks) {
  184. if(!emulator->is_attacking) {
  185. if(emulator->ticks_between_cards < MIFARE_FUZZER_MAX_TICKS_BETWEEN_CARDS) {
  186. emulator->ticks_between_cards++;
  187. mifare_fuzzer_emulator_set_ticks_between_cards(
  188. app->emulator_view, emulator->ticks_between_cards);
  189. };
  190. };
  191. } else if(event.event == MifareFuzzerEventDecrementTicks) {
  192. if(!emulator->is_attacking) {
  193. if(emulator->ticks_between_cards > MIFARE_FUZZER_MIN_TICKS_BETWEEN_CARDS) {
  194. emulator->ticks_between_cards--;
  195. mifare_fuzzer_emulator_set_ticks_between_cards(
  196. app->emulator_view, emulator->ticks_between_cards);
  197. };
  198. };
  199. }
  200. consumed = true;
  201. } else if(event.type == SceneManagerEventTypeTick) {
  202. //FURI_LOG_D(TAG, "mifare_fuzzer_scene_emulator_on_event() :: SceneManagerEventTypeTick");
  203. // Used to check tick length (not perfect but enough)
  204. //DateTime curr_dt;
  205. //furi_hal_rtc_get_datetime(&curr_dt);
  206. //FURI_LOG_D(TAG, "Time is: %.2d:%.2d:%.2d", curr_dt.hour, curr_dt.minute, curr_dt.second);
  207. // If emulator is attacking
  208. if(emulator->is_attacking) {
  209. // increment tick_counter
  210. tick_counter++;
  211. mifare_fuzzer_emulator_set_tick_num(app->emulator_view, tick_counter);
  212. //FURI_LOG_D(TAG, "tick_counter is: %.2d", tick_counter);
  213. if(tick_counter >= emulator->ticks_between_cards) {
  214. // Queue event for changing UID
  215. view_dispatcher_send_custom_event(
  216. app->view_dispatcher, MifareFuzzerEventStartAttack);
  217. }
  218. }
  219. consumed = true;
  220. }
  221. return consumed;
  222. }
  223. /// @brief mifare_fuzzer_scene_emulator_on_exit()
  224. /// @param context
  225. void mifare_fuzzer_scene_emulator_on_exit(void* context) {
  226. //FURI_LOG_D(TAG, "mifare_fuzzer_scene_emulator_on_exit()");
  227. MifareFuzzerApp* app = context;
  228. notification_message(app->notifications, &sequence_blink_stop);
  229. mifare_fuzzer_worker_stop(app->worker);
  230. if(app->attack == MifareFuzzerAttackLoadUidsFromFile) {
  231. furi_string_reset(app->uid_str);
  232. stream_rewind(app->uids_stream);
  233. buffered_file_stream_close(app->uids_stream);
  234. }
  235. }