mifare_fuzzer_scene_emulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. const MfClassicData* mf_classic_data = NULL;
  72. bool nfc_device_parsed = false;
  73. if(app->card_file_path) {
  74. nfc_device = app->worker->nfc_device;
  75. const char* path = furi_string_get_cstr(app->card_file_path);
  76. if(nfc_device_load(nfc_device, path)) {
  77. nfc_device_parsed = true;
  78. mf_classic_data = nfc_device_get_data(nfc_device, NfcProtocolMfClassic);
  79. if(mf_classic_data->type == MfClassicType1k) {
  80. app->card = MifareCardClassic1k;
  81. } else if(mf_classic_data->type == MfClassicType4k) {
  82. app->card = MifareCardClassic4k;
  83. } else if(nfc_device_get_protocol(nfc_device) == NfcProtocolMfUltralight) {
  84. app->card = MifareCardUltralight;
  85. }
  86. mifare_fuzzer_emulator_set_card(emulator, app->card, app->card_file_path);
  87. }
  88. }
  89. Iso14443_3aData* nfc_data;
  90. if(mf_classic_data) {
  91. nfc_data = mf_classic_data->iso14443_3a_data;
  92. } else {
  93. nfc_data = iso14443_3a_alloc();
  94. }
  95. // Stop worker
  96. mifare_fuzzer_worker_stop(app->worker);
  97. // Set card type
  98. // TODO: Move somewhere else, I do not like this to be there
  99. if(app->card == MifareCardClassic1k) {
  100. nfc_data->atqa[0] = 0x04;
  101. nfc_data->atqa[1] = 0x00;
  102. nfc_data->sak = 0x08;
  103. nfc_data->uid_len = 0x04;
  104. } else if(app->card == MifareCardClassic4k) {
  105. nfc_data->atqa[0] = 0x02;
  106. nfc_data->atqa[1] = 0x00;
  107. nfc_data->sak = 0x18;
  108. nfc_data->uid_len = 0x04;
  109. } else if(app->card == MifareCardUltralight) {
  110. nfc_data->atqa[0] = 0x44;
  111. nfc_data->atqa[1] = 0x00;
  112. nfc_data->sak = 0x00;
  113. nfc_data->uid_len = 0x07;
  114. }
  115. // Set UIDs
  116. if(app->attack == MifareFuzzerAttackTestValues) {
  117. // Load test UIDs
  118. for(uint8_t i = 0; i < nfc_data->uid_len; i++) {
  119. nfc_data->uid[i] = id_uid_test[attack_step][i];
  120. }
  121. // Next UIDs on next loop
  122. if(attack_step >= 8) {
  123. attack_step = 0;
  124. } else {
  125. attack_step++;
  126. }
  127. } else if(app->attack == MifareFuzzerAttackRandomValues) {
  128. if(app->card == MifareCardUltralight) {
  129. // First byte of a 7 byte UID is the manufacturer-code
  130. // https://github.com/Proxmark/proxmark3/blob/master/client/taginfo.c
  131. // https://stackoverflow.com/questions/37837730/mifare-cards-distinguish-between-4-byte-and-7-byte-uids
  132. // https://stackoverflow.com/questions/31233652/how-to-detect-manufacturer-from-nfc-tag-using-android
  133. // TODO: Manufacture-code must be selectable from a list
  134. // use a fixed manufacture-code for now: 0x04 = NXP Semiconductors Germany
  135. nfc_data->uid[0] = 0x04;
  136. for(uint8_t i = 1; i < nfc_data->uid_len; i++) {
  137. nfc_data->uid[i] = (furi_hal_random_get() & 0xFF);
  138. }
  139. } else {
  140. for(uint8_t i = 0; i < nfc_data->uid_len; i++) {
  141. nfc_data->uid[i] = (furi_hal_random_get() & 0xFF);
  142. }
  143. }
  144. } else if(app->attack == MifareFuzzerAttackLoadUidsFromFile) {
  145. //bool end_of_list = false;
  146. // read stream
  147. while(true) {
  148. furi_string_reset(app->uid_str);
  149. if(!stream_read_line(app->uids_stream, app->uid_str)) {
  150. // restart from beginning on empty line
  151. stream_rewind(app->uids_stream);
  152. continue;
  153. //end_of_list = true;
  154. }
  155. // Skip comments
  156. if(furi_string_get_char(app->uid_str, 0) == '#') continue;
  157. // Skip lines with invalid length
  158. if((furi_string_size(app->uid_str) != 9) &&
  159. (furi_string_size(app->uid_str) != 15))
  160. continue;
  161. break;
  162. }
  163. // TODO: stop on end of list?
  164. //if(end_of_list) break;
  165. // parse string to UID
  166. // TODO: a better validation on input?
  167. for(uint8_t i = 0; i < nfc_data->uid_len; i++) {
  168. if(i <= ((furi_string_size(app->uid_str) - 1) / 2)) {
  169. char temp_str[3];
  170. temp_str[0] = furi_string_get_cstr(app->uid_str)[i * 2];
  171. temp_str[1] = furi_string_get_cstr(app->uid_str)[i * 2 + 1];
  172. temp_str[2] = '\0';
  173. nfc_data->uid[i] = (uint8_t)strtol(temp_str, NULL, 16);
  174. } else {
  175. nfc_data->uid[i] = 0x00;
  176. }
  177. }
  178. }
  179. mifare_fuzzer_emulator_set_nfc_data(app->emulator_view, *nfc_data);
  180. if(nfc_device_parsed) {
  181. mifare_fuzzer_worker_set_nfc_device(app->worker, nfc_device);
  182. } else {
  183. mifare_fuzzer_worker_set_nfc_data(app->worker, *nfc_data);
  184. }
  185. // Reset tick_counter
  186. tick_counter = 0;
  187. mifare_fuzzer_emulator_set_tick_num(app->emulator_view, tick_counter);
  188. // Start worker
  189. mifare_fuzzer_worker_start(app->worker);
  190. if(nfc_device_parsed) {
  191. notification_message(app->notifications, &sequence_blink_start_magenta);
  192. }
  193. } else if(event.event == MifareFuzzerEventStopAttack) {
  194. //FURI_LOG_D(TAG, "mifare_fuzzer_scene_emulator_on_event() :: MifareFuzzerEventStopAttack");
  195. // Stop worker
  196. mifare_fuzzer_worker_stop(app->worker);
  197. notification_message(app->notifications, &sequence_blink_stop);
  198. } else if(event.event == MifareFuzzerEventIncrementTicks) {
  199. if(!emulator->is_attacking) {
  200. if(emulator->ticks_between_cards < MIFARE_FUZZER_MAX_TICKS_BETWEEN_CARDS) {
  201. emulator->ticks_between_cards++;
  202. mifare_fuzzer_emulator_set_ticks_between_cards(
  203. app->emulator_view, emulator->ticks_between_cards);
  204. };
  205. };
  206. } else if(event.event == MifareFuzzerEventDecrementTicks) {
  207. if(!emulator->is_attacking) {
  208. if(emulator->ticks_between_cards > MIFARE_FUZZER_MIN_TICKS_BETWEEN_CARDS) {
  209. emulator->ticks_between_cards--;
  210. mifare_fuzzer_emulator_set_ticks_between_cards(
  211. app->emulator_view, emulator->ticks_between_cards);
  212. };
  213. };
  214. }
  215. consumed = true;
  216. } else if(event.type == SceneManagerEventTypeTick) {
  217. //FURI_LOG_D(TAG, "mifare_fuzzer_scene_emulator_on_event() :: SceneManagerEventTypeTick");
  218. // Used to check tick length (not perfect but enough)
  219. //FuriHalRtcDateTime curr_dt;
  220. //furi_hal_rtc_get_datetime(&curr_dt);
  221. //FURI_LOG_D(TAG, "Time is: %.2d:%.2d:%.2d", curr_dt.hour, curr_dt.minute, curr_dt.second);
  222. // If emulator is attacking
  223. if(emulator->is_attacking) {
  224. // increment tick_counter
  225. tick_counter++;
  226. mifare_fuzzer_emulator_set_tick_num(app->emulator_view, tick_counter);
  227. //FURI_LOG_D(TAG, "tick_counter is: %.2d", tick_counter);
  228. if(tick_counter >= emulator->ticks_between_cards) {
  229. // Queue event for changing UID
  230. view_dispatcher_send_custom_event(
  231. app->view_dispatcher, MifareFuzzerEventStartAttack);
  232. }
  233. }
  234. consumed = true;
  235. }
  236. return consumed;
  237. }
  238. /// @brief mifare_fuzzer_scene_emulator_on_exit()
  239. /// @param context
  240. void mifare_fuzzer_scene_emulator_on_exit(void* context) {
  241. //FURI_LOG_D(TAG, "mifare_fuzzer_scene_emulator_on_exit()");
  242. MifareFuzzerApp* app = context;
  243. notification_message(app->notifications, &sequence_blink_stop);
  244. mifare_fuzzer_worker_stop(app->worker);
  245. if(app->attack == MifareFuzzerAttackLoadUidsFromFile) {
  246. furi_string_reset(app->uid_str);
  247. stream_rewind(app->uids_stream);
  248. buffered_file_stream_close(app->uids_stream);
  249. }
  250. }