weebo_scene_emulate.c 3.6 KB

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