weebo_scene_emulate.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. //stop listener
  17. FURI_LOG_D(TAG, "Stopping listener");
  18. nfc_listener_stop(weebo->listener);
  19. nfc_listener_free(weebo->listener);
  20. weebo->listener = NULL;
  21. //random uid
  22. FURI_LOG_D(TAG, "Generating random UID");
  23. UID[0] = 0x04;
  24. furi_hal_random_fill_buf(UID + 1, 6);
  25. UID[7] = UID[3] ^ UID[4] ^ UID[5] ^ UID[6];
  26. memcpy(weebo->figure + NFC3D_UID_OFFSET, UID, 8);
  27. memcpy(data->iso14443_3a_data->uid, UID, 7);
  28. FURI_LOG_D(TAG, "New UID: %02X%02X%02X%02X%02X%02X%02X%02X",
  29. UID[0], UID[1], UID[2], UID[3], UID[4], UID[5], UID[6], UID[7]);
  30. //pack
  31. nfc3d_amiibo_pack(&weebo->amiiboKeys, weebo->figure, modified);
  32. //copy data in
  33. for(size_t i = 0; i < 130; i++) {
  34. FURI_LOG_D(TAG, "Copy page %d", i);
  35. memcpy(
  36. data->page[i].data, modified + i * MF_ULTRALIGHT_PAGE_SIZE, MF_ULTRALIGHT_PAGE_SIZE);
  37. }
  38. //new pwd
  39. weebo_scene_emulate_calculate_pwd(data->iso14443_3a_data->uid, PWD);
  40. memcpy(data->page[133].data, PWD, sizeof(PWD));
  41. //set data
  42. nfc_device_set_data(weebo->nfc_device, NfcProtocolMfUltralight, data);
  43. //start listener
  44. FURI_LOG_D(TAG, "Starting listener");
  45. weebo->listener = nfc_listener_alloc(weebo->nfc, NfcProtocolMfUltralight, data);
  46. nfc_listener_start(weebo->listener, NULL, NULL);
  47. mf_ultralight_free(data);
  48. }
  49. void weebo_scene_emulate_widget_callback(GuiButtonType result, InputType type, void* context) {
  50. furi_assert(context);
  51. Weebo* weebo = context;
  52. if(type == InputTypeShort) {
  53. view_dispatcher_send_custom_event(weebo->view_dispatcher, result);
  54. }
  55. }
  56. void weebo_scene_emulate_on_enter(void* context) {
  57. Weebo* weebo = context;
  58. Widget* widget = weebo->widget;
  59. nfc_device_load(weebo->nfc_device, furi_string_get_cstr(weebo->load_path));
  60. const MfUltralightData* data = nfc_device_get_data(weebo->nfc_device, NfcProtocolMfUltralight);
  61. weebo->listener = nfc_listener_alloc(weebo->nfc, NfcProtocolMfUltralight, data);
  62. nfc_listener_start(weebo->listener, NULL, NULL);
  63. FuriString* info_str = furi_string_alloc();
  64. furi_string_cat_printf(info_str, "Emulating");
  65. widget_add_string_element(
  66. widget, 64, 5, AlignCenter, AlignCenter, FontSecondary, furi_string_get_cstr(info_str));
  67. widget_add_button_element(
  68. widget, GuiButtonTypeCenter, "Remix", weebo_scene_emulate_widget_callback, weebo);
  69. furi_string_free(info_str);
  70. weebo_blink_start(weebo);
  71. view_dispatcher_switch_to_view(weebo->view_dispatcher, WeeboViewWidget);
  72. }
  73. bool weebo_scene_emulate_on_event(void* context, SceneManagerEvent event) {
  74. Weebo* weebo = context;
  75. bool consumed = false;
  76. if(event.type == SceneManagerEventTypeCustom) {
  77. scene_manager_set_scene_state(weebo->scene_manager, WeeboSceneEmulate, event.event);
  78. if(event.event == GuiButtonTypeCenter) {
  79. weebo_scene_emulate_remix(weebo);
  80. consumed = true;
  81. }
  82. }
  83. return consumed;
  84. }
  85. void weebo_scene_emulate_on_exit(void* context) {
  86. Weebo* weebo = context;
  87. if(weebo->listener) {
  88. nfc_listener_stop(weebo->listener);
  89. nfc_listener_free(weebo->listener);
  90. weebo->listener = NULL;
  91. }
  92. widget_reset(weebo->widget);
  93. weebo_blink_stop(weebo);
  94. }