mifare_fuzzer_scene_emulator.c 11 KB

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