weebo_scene_emulate.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "../weebo_i.h"
  2. #include <nfc/protocols/mf_ultralight/mf_ultralight_listener.h>
  3. #define TAG "SceneEmulate"
  4. void weebo_scene_emulate_calculate_pwd(uint8_t* uid, uint8_t* pwd) {
  5. pwd[0] = uid[1] ^ uid[3] ^ 0xAA;
  6. pwd[1] = uid[2] ^ uid[4] ^ 0x55;
  7. pwd[2] = uid[3] ^ uid[5] ^ 0xAA;
  8. pwd[3] = uid[4] ^ uid[6] ^ 0x55;
  9. }
  10. void weebo_scene_emulate_remix(Weebo* weebo) {
  11. uint8_t PWD[4];
  12. uint8_t UID[8];
  13. uint8_t modified[NTAG215_SIZE];
  14. MfUltralightData* data = mf_ultralight_alloc();
  15. nfc_device_copy_data(weebo->nfc_device, NfcProtocolMfUltralight, data);
  16. //random uid
  17. FURI_LOG_D(TAG, "Generating random UID");
  18. UID[0] = 0x04;
  19. furi_hal_random_fill_buf(UID + 1, 6);
  20. UID[7] = UID[3] ^ UID[4] ^ UID[5] ^ UID[6];
  21. memcpy(weebo->figure + NFC3D_UID_OFFSET, UID, 8);
  22. memcpy(data->iso14443_3a_data->uid, UID, 7);
  23. //pack
  24. nfc3d_amiibo_pack(&weebo->amiiboKeys, weebo->figure, modified);
  25. //copy data in
  26. for(size_t i = 0; i < 130; i++) {
  27. memcpy(
  28. data->page[i].data, modified + i * MF_ULTRALIGHT_PAGE_SIZE, MF_ULTRALIGHT_PAGE_SIZE);
  29. }
  30. //new pwd
  31. weebo_scene_emulate_calculate_pwd(data->iso14443_3a_data->uid, PWD);
  32. memcpy(data->page[133].data, PWD, sizeof(PWD));
  33. //set data
  34. nfc_device_set_data(weebo->nfc_device, NfcProtocolMfUltralight, data);
  35. mf_ultralight_free(data);
  36. }
  37. void weebo_scene_emulate_widget_callback(GuiButtonType result, InputType type, void* context) {
  38. furi_assert(context);
  39. Weebo* weebo = context;
  40. if(type == InputTypeShort) {
  41. view_dispatcher_send_custom_event(weebo->view_dispatcher, result);
  42. }
  43. }
  44. void weebo_scene_emulate_draw_screen(Weebo* weebo) {
  45. Widget* widget = weebo->widget;
  46. FuriString* info_str = furi_string_alloc();
  47. FuriString* uid_str = furi_string_alloc();
  48. const MfUltralightData* data = nfc_device_get_data(weebo->nfc_device, NfcProtocolMfUltralight);
  49. furi_string_cat_printf(info_str, "Emulating");
  50. furi_string_cat_printf(
  51. uid_str,
  52. "%02X%02X%02X%02X%02X%02X%02X",
  53. data->iso14443_3a_data->uid[0],
  54. data->iso14443_3a_data->uid[1],
  55. data->iso14443_3a_data->uid[2],
  56. data->iso14443_3a_data->uid[3],
  57. data->iso14443_3a_data->uid[4],
  58. data->iso14443_3a_data->uid[5],
  59. data->iso14443_3a_data->uid[6]);
  60. widget_reset(widget);
  61. widget_add_string_element(
  62. widget, 64, 5, AlignCenter, AlignCenter, FontSecondary, furi_string_get_cstr(info_str));
  63. widget_add_string_element(
  64. widget, 64, 25, AlignCenter, AlignCenter, FontSecondary, furi_string_get_cstr(uid_str));
  65. widget_add_button_element(
  66. widget, GuiButtonTypeCenter, "Remix", weebo_scene_emulate_widget_callback, weebo);
  67. furi_string_free(info_str);
  68. furi_string_free(uid_str);
  69. view_dispatcher_switch_to_view(weebo->view_dispatcher, WeeboViewWidget);
  70. }
  71. void weebo_scene_emulate_on_enter(void* context) {
  72. Weebo* weebo = context;
  73. nfc_device_load(weebo->nfc_device, furi_string_get_cstr(weebo->load_path));
  74. const MfUltralightData* data = nfc_device_get_data(weebo->nfc_device, NfcProtocolMfUltralight);
  75. weebo->listener = nfc_listener_alloc(weebo->nfc, NfcProtocolMfUltralight, data);
  76. nfc_listener_start(weebo->listener, NULL, NULL);
  77. weebo_scene_emulate_draw_screen(weebo);
  78. weebo_blink_start(weebo);
  79. }
  80. bool weebo_scene_emulate_on_event(void* context, SceneManagerEvent event) {
  81. Weebo* weebo = context;
  82. bool consumed = false;
  83. if(event.type == SceneManagerEventTypeCustom) {
  84. scene_manager_set_scene_state(weebo->scene_manager, WeeboSceneEmulate, event.event);
  85. if(event.event == GuiButtonTypeCenter) {
  86. //stop listener
  87. FURI_LOG_D(TAG, "Stopping listener");
  88. nfc_listener_stop(weebo->listener);
  89. nfc_listener_free(weebo->listener);
  90. weebo->listener = NULL;
  91. weebo_scene_emulate_remix(weebo);
  92. //start listener
  93. FURI_LOG_D(TAG, "Starting listener");
  94. const MfUltralightData* data =
  95. nfc_device_get_data(weebo->nfc_device, NfcProtocolMfUltralight);
  96. weebo->listener = nfc_listener_alloc(weebo->nfc, NfcProtocolMfUltralight, data);
  97. nfc_listener_start(weebo->listener, NULL, NULL);
  98. weebo_scene_emulate_draw_screen(weebo);
  99. consumed = true;
  100. }
  101. }
  102. return consumed;
  103. }
  104. void weebo_scene_emulate_on_exit(void* context) {
  105. Weebo* weebo = context;
  106. if(weebo->listener) {
  107. nfc_listener_stop(weebo->listener);
  108. nfc_listener_free(weebo->listener);
  109. weebo->listener = NULL;
  110. }
  111. widget_reset(weebo->widget);
  112. weebo_blink_stop(weebo);
  113. }