|
| 1 | +#include "../picopass_i.h" |
| 2 | +#include <dolphin/dolphin.h> |
| 3 | +#include "../picopass_elite_keygen.h" |
| 4 | + |
| 5 | +#define PICOPASS_SCENE_DICT_ATTACK_KEYS_BATCH_UPDATE (10) |
| 6 | +#define PICOPASS_SCENE_ELITE_KEYGEN_ATTACK_LIMIT (2000) |
| 7 | + |
| 8 | +NfcCommand picopass_elite_keygen_attack_worker_callback(PicopassPollerEvent event, void* context) { |
| 9 | + furi_assert(context); |
| 10 | + NfcCommand command = NfcCommandContinue; |
| 11 | + |
| 12 | + Picopass* picopass = context; |
| 13 | + |
| 14 | + if(event.type == PicopassPollerEventTypeRequestMode) { |
| 15 | + event.data->req_mode.mode = PicopassPollerModeRead; |
| 16 | + } else if(event.type == PicopassPollerEventTypeRequestKey) { |
| 17 | + uint8_t key[PICOPASS_KEY_LEN] = {}; |
| 18 | + bool is_key_provided = false; |
| 19 | + if(picopass->dict_attack_ctx.current_key < PICOPASS_SCENE_ELITE_KEYGEN_ATTACK_LIMIT) { |
| 20 | + picopass_elite_nextKey(key); |
| 21 | + is_key_provided = true; |
| 22 | + } |
| 23 | + |
| 24 | + memcpy(event.data->req_key.key, key, PICOPASS_KEY_LEN); |
| 25 | + event.data->req_key.is_elite_key = true; |
| 26 | + event.data->req_key.is_key_provided = is_key_provided; |
| 27 | + if(is_key_provided) { |
| 28 | + picopass->dict_attack_ctx.current_key++; |
| 29 | + if(picopass->dict_attack_ctx.current_key % |
| 30 | + PICOPASS_SCENE_DICT_ATTACK_KEYS_BATCH_UPDATE == |
| 31 | + 0) { |
| 32 | + view_dispatcher_send_custom_event( |
| 33 | + picopass->view_dispatcher, PicopassCustomEventDictAttackUpdateView); |
| 34 | + } |
| 35 | + } |
| 36 | + } else if( |
| 37 | + event.type == PicopassPollerEventTypeSuccess || |
| 38 | + event.type == PicopassPollerEventTypeFail || |
| 39 | + event.type == PicopassPollerEventTypeAuthFail) { |
| 40 | + const PicopassDeviceData* data = picopass_poller_get_data(picopass->poller); |
| 41 | + memcpy(&picopass->dev->dev_data, data, sizeof(PicopassDeviceData)); |
| 42 | + view_dispatcher_send_custom_event( |
| 43 | + picopass->view_dispatcher, PicopassCustomEventPollerSuccess); |
| 44 | + } else if(event.type == PicopassPollerEventTypeCardLost) { |
| 45 | + picopass->dict_attack_ctx.card_detected = false; |
| 46 | + view_dispatcher_send_custom_event( |
| 47 | + picopass->view_dispatcher, PicopassCustomEventDictAttackUpdateView); |
| 48 | + } else if(event.type == PicopassPollerEventTypeCardDetected) { |
| 49 | + picopass->dict_attack_ctx.card_detected = true; |
| 50 | + view_dispatcher_send_custom_event( |
| 51 | + picopass->view_dispatcher, PicopassCustomEventDictAttackUpdateView); |
| 52 | + } |
| 53 | + |
| 54 | + return command; |
| 55 | +} |
| 56 | + |
| 57 | +static void picopass_scene_elite_keygen_attack_update_view(Picopass* instance) { |
| 58 | + if(instance->dict_attack_ctx.card_detected) { |
| 59 | + dict_attack_set_card_detected(instance->dict_attack); |
| 60 | + dict_attack_set_header(instance->dict_attack, instance->dict_attack_ctx.name); |
| 61 | + dict_attack_set_total_dict_keys( |
| 62 | + instance->dict_attack, PICOPASS_SCENE_ELITE_KEYGEN_ATTACK_LIMIT); |
| 63 | + dict_attack_set_current_dict_key( |
| 64 | + instance->dict_attack, instance->dict_attack_ctx.current_key); |
| 65 | + } else { |
| 66 | + dict_attack_set_card_removed(instance->dict_attack); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +static void picopass_scene_elite_keygen_attack_callback(void* context) { |
| 71 | + Picopass* instance = context; |
| 72 | + |
| 73 | + view_dispatcher_send_custom_event( |
| 74 | + instance->view_dispatcher, PicopassCustomEventDictAttackSkip); |
| 75 | +} |
| 76 | + |
| 77 | +void picopass_scene_elite_keygen_attack_on_enter(void* context) { |
| 78 | + Picopass* picopass = context; |
| 79 | + dolphin_deed(DolphinDeedNfcRead); |
| 80 | + |
| 81 | + // Setup dict attack context |
| 82 | + uint32_t state = PicopassSceneEliteKeygenAttack; |
| 83 | + |
| 84 | + picopass->dict = keys_dict_alloc( |
| 85 | + PICOPASS_ICLASS_STANDARD_DICT_FLIPPER_NAME, KeysDictModeOpenExisting, PICOPASS_KEY_LEN); |
| 86 | + |
| 87 | + dict_attack_reset(picopass->dict_attack); |
| 88 | + picopass->dict_attack_ctx.card_detected = false; |
| 89 | + picopass->dict_attack_ctx.total_keys = PICOPASS_SCENE_ELITE_KEYGEN_ATTACK_LIMIT; |
| 90 | + picopass->dict_attack_ctx.current_key = 0; |
| 91 | + picopass->dict_attack_ctx.name = "Elite Keygen Attack"; |
| 92 | + scene_manager_set_scene_state(picopass->scene_manager, PicopassSceneEliteKeygenAttack, state); |
| 93 | + |
| 94 | + // Setup view |
| 95 | + picopass_scene_elite_keygen_attack_update_view(picopass); |
| 96 | + dict_attack_set_callback( |
| 97 | + picopass->dict_attack, picopass_scene_elite_keygen_attack_callback, picopass); |
| 98 | + |
| 99 | + // Start worker |
| 100 | + picopass->poller = picopass_poller_alloc(picopass->nfc); |
| 101 | + picopass_poller_start( |
| 102 | + picopass->poller, picopass_elite_keygen_attack_worker_callback, picopass); |
| 103 | + |
| 104 | + view_dispatcher_switch_to_view(picopass->view_dispatcher, PicopassViewDictAttack); |
| 105 | + picopass_blink_start(picopass); |
| 106 | +} |
| 107 | + |
| 108 | +bool picopass_scene_elite_keygen_attack_on_event(void* context, SceneManagerEvent event) { |
| 109 | + Picopass* picopass = context; |
| 110 | + bool consumed = false; |
| 111 | + |
| 112 | + if(event.type == SceneManagerEventTypeCustom) { |
| 113 | + if(event.event == PicopassCustomEventPollerSuccess) { |
| 114 | + scene_manager_next_scene(picopass->scene_manager, PicopassSceneReadCardSuccess); |
| 115 | + consumed = true; |
| 116 | + } else if(event.event == PicopassCustomEventDictAttackUpdateView) { |
| 117 | + picopass_scene_elite_keygen_attack_update_view(picopass); |
| 118 | + consumed = true; |
| 119 | + } else if(event.event == PicopassCustomEventDictAttackSkip) { |
| 120 | + scene_manager_next_scene(picopass->scene_manager, PicopassSceneReadCardSuccess); |
| 121 | + consumed = true; |
| 122 | + } |
| 123 | + } |
| 124 | + return consumed; |
| 125 | +} |
| 126 | + |
| 127 | +void picopass_scene_elite_keygen_attack_on_exit(void* context) { |
| 128 | + Picopass* picopass = context; |
| 129 | + |
| 130 | + if(picopass->dict) { |
| 131 | + keys_dict_free(picopass->dict); |
| 132 | + picopass->dict = NULL; |
| 133 | + } |
| 134 | + picopass->dict_attack_ctx.current_key = 0; |
| 135 | + picopass->dict_attack_ctx.total_keys = 0; |
| 136 | + picopass_elite_reset(); |
| 137 | + |
| 138 | + picopass_poller_stop(picopass->poller); |
| 139 | + picopass_poller_free(picopass->poller); |
| 140 | + |
| 141 | + // Clear view |
| 142 | + popup_reset(picopass->popup); |
| 143 | + |
| 144 | + picopass_blink_stop(picopass); |
| 145 | +} |
0 commit comments