-
Notifications
You must be signed in to change notification settings - Fork 20
/
mifare_nested.c
409 lines (333 loc) · 14.3 KB
/
mifare_nested.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "mifare_nested_i.h"
#include <gui/elements.h>
bool mifare_nested_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
MifareNested* mifare_nested = context;
return scene_manager_handle_custom_event(mifare_nested->scene_manager, event);
}
bool mifare_nested_back_event_callback(void* context) {
furi_assert(context);
MifareNested* mifare_nested = context;
return scene_manager_handle_back_event(mifare_nested->scene_manager);
}
void mifare_nested_tick_event_callback(void* context) {
furi_assert(context);
MifareNested* mifare_nested = context;
scene_manager_handle_tick_event(mifare_nested->scene_manager);
}
void mifare_nested_show_loading_popup(void* context, bool show) {
MifareNested* mifare_nested = context;
TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);
if(show) {
// Raise timer priority so that animations can play
vTaskPrioritySet(timer_task, configMAX_PRIORITIES - 1);
view_dispatcher_switch_to_view(mifare_nested->view_dispatcher, MifareNestedViewLoading);
} else {
// Restore default timer priority
vTaskPrioritySet(timer_task, configTIMER_TASK_PRIORITY);
}
}
NestedState* collection_alloc() {
NestedState* nested = malloc(sizeof(NestedState));
nested->view = view_alloc();
view_allocate_model(nested->view, ViewModelTypeLocking, sizeof(NestedAttackViewModel));
with_view_model(
nested->view,
NestedAttackViewModel * model,
{
model->header = furi_string_alloc();
furi_string_set(model->header, "Collecting nonces");
model->keys_count = 0;
model->hardnested_states = 0;
model->lost_tag = false;
model->calibrating = false;
model->need_prediction = false;
model->hardnested = false;
},
false);
return nested;
}
CheckKeysState* check_keys_alloc() {
CheckKeysState* state = malloc(sizeof(CheckKeysState));
state->view = view_alloc();
view_allocate_model(state->view, ViewModelTypeLocking, sizeof(CheckKeysViewModel));
with_view_model(
state->view,
CheckKeysViewModel * model,
{
model->header = furi_string_alloc();
furi_string_set(model->header, "Checking keys");
model->lost_tag = false;
},
false);
return state;
}
static void nested_draw_callback(Canvas* canvas, void* model) {
NestedAttackViewModel* m = model;
if(m->lost_tag) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Lost the tag!");
canvas_set_font(canvas, FontSecondary);
elements_multiline_text_aligned(
canvas, 64, 23, AlignCenter, AlignTop, "Make sure the tag is\npositioned correctly.");
} else if(m->calibrating) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Calibrating...");
canvas_set_font(canvas, FontSecondary);
if(!m->need_prediction) {
elements_multiline_text_aligned(
canvas, 64, 23, AlignCenter, AlignTop, "Don't touch or move\nFlipper/Tag!");
} else {
elements_multiline_text_aligned(
canvas, 64, 18, AlignCenter, AlignTop, "Don't touch or move tag!");
canvas_set_font(canvas, FontPrimary);
elements_multiline_text_aligned(
canvas, 64, 30, AlignCenter, AlignTop, "Calibration will take\nmore time");
}
} else if(m->hardnested) {
char draw_str[32] = {};
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(
canvas, 64, 2, AlignCenter, AlignTop, furi_string_get_cstr(m->header));
canvas_set_font(canvas, FontSecondary);
float progress =
m->keys_count == 0 ? 0 : (float)(m->nonces_collected) / (float)(m->keys_count);
if(progress > 1.0) {
progress = 1.0;
}
elements_progress_bar(canvas, 5, 15, 120, progress);
canvas_set_font(canvas, FontSecondary);
snprintf(
draw_str,
sizeof(draw_str),
"Nonces collected: %lu/%lu",
m->nonces_collected,
m->keys_count);
canvas_draw_str_aligned(canvas, 1, 28, AlignLeft, AlignTop, draw_str);
snprintf(draw_str, sizeof(draw_str), "States found: %lu/256", m->hardnested_states);
canvas_draw_str_aligned(canvas, 1, 40, AlignLeft, AlignTop, draw_str);
} else {
char draw_str[32] = {};
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(
canvas, 64, 2, AlignCenter, AlignTop, furi_string_get_cstr(m->header));
canvas_set_font(canvas, FontSecondary);
float progress =
m->keys_count == 0 ? 0 : (float)(m->nonces_collected) / (float)(m->keys_count);
if(progress > 1.0) {
progress = 1.0;
}
elements_progress_bar(canvas, 5, 15, 120, progress);
canvas_set_font(canvas, FontSecondary);
snprintf(
draw_str,
sizeof(draw_str),
"Nonces collected: %lu/%lu",
m->nonces_collected,
m->keys_count);
canvas_draw_str_aligned(canvas, 1, 28, AlignLeft, AlignTop, draw_str);
}
elements_button_center(canvas, "Stop");
}
static void check_keys_draw_callback(Canvas* canvas, void* model) {
CheckKeysViewModel* m = model;
if(m->lost_tag) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Lost the tag!");
canvas_set_font(canvas, FontSecondary);
elements_multiline_text_aligned(
canvas, 64, 23, AlignCenter, AlignTop, "Make sure the tag is\npositioned correctly.");
} else if(m->processing_keys) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Processing keys...");
canvas_set_font(canvas, FontSecondary);
elements_multiline_text_aligned(
canvas, 64, 23, AlignCenter, AlignTop, "Checking which keys you\nalready have...");
} else {
char draw_str[32] = {};
char draw_sub_str[32] = {};
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(
canvas, 64, 2, AlignCenter, AlignTop, furi_string_get_cstr(m->header));
canvas_set_font(canvas, FontSecondary);
float progress = m->keys_count == 0 ? 0 :
(float)(m->keys_checked) / (float)(m->keys_count);
if(progress > 1.0) {
progress = 1.0;
}
elements_progress_bar(canvas, 5, 15, 120, progress);
canvas_set_font(canvas, FontSecondary);
snprintf(
draw_str, sizeof(draw_str), "Keys checked: %lu/%lu", m->keys_checked, m->keys_count);
canvas_draw_str_aligned(canvas, 1, 28, AlignLeft, AlignTop, draw_str);
snprintf(
draw_sub_str,
sizeof(draw_sub_str),
"Keys found: %lu/%lu",
m->keys_found,
m->keys_total);
canvas_draw_str_aligned(canvas, 1, 40, AlignLeft, AlignTop, draw_sub_str);
}
elements_button_center(canvas, "Stop");
}
static bool nested_input_callback(InputEvent* event, void* context) {
MifareNested* mifare_nested = context;
bool consumed = false;
if(event->type == InputTypeShort && (event->key == InputKeyBack || event->key == InputKeyOk)) {
scene_manager_search_and_switch_to_previous_scene(mifare_nested->scene_manager, 0);
consumed = true;
}
return consumed;
}
MifareNested* mifare_nested_alloc() {
MifareNested* mifare_nested = malloc(sizeof(MifareNested));
mifare_nested->worker = mifare_nested_worker_alloc();
mifare_nested->view_dispatcher = view_dispatcher_alloc();
mifare_nested->scene_manager =
scene_manager_alloc(&mifare_nested_scene_handlers, mifare_nested);
view_dispatcher_enable_queue(mifare_nested->view_dispatcher);
view_dispatcher_set_event_callback_context(mifare_nested->view_dispatcher, mifare_nested);
view_dispatcher_set_custom_event_callback(
mifare_nested->view_dispatcher, mifare_nested_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
mifare_nested->view_dispatcher, mifare_nested_back_event_callback);
view_dispatcher_set_tick_event_callback(
mifare_nested->view_dispatcher, mifare_nested_tick_event_callback, 100);
// Nfc device
mifare_nested->nfc_dev = nfc_device_alloc();
// Open GUI record
mifare_nested->gui = furi_record_open(RECORD_GUI);
view_dispatcher_attach_to_gui(
mifare_nested->view_dispatcher, mifare_nested->gui, ViewDispatcherTypeFullscreen);
// Open Notification record
mifare_nested->notifications = furi_record_open(RECORD_NOTIFICATION);
// Submenu
mifare_nested->submenu = submenu_alloc();
view_dispatcher_add_view(
mifare_nested->view_dispatcher,
MifareNestedViewMenu,
submenu_get_view(mifare_nested->submenu));
// Popup
mifare_nested->popup = popup_alloc();
view_dispatcher_add_view(
mifare_nested->view_dispatcher,
MifareNestedViewPopup,
popup_get_view(mifare_nested->popup));
// Loading
mifare_nested->loading = loading_alloc();
view_dispatcher_add_view(
mifare_nested->view_dispatcher,
MifareNestedViewLoading,
loading_get_view(mifare_nested->loading));
// Text Input
mifare_nested->text_input = text_input_alloc();
view_dispatcher_add_view(
mifare_nested->view_dispatcher,
MifareNestedViewTextInput,
text_input_get_view(mifare_nested->text_input));
// Custom Widget
mifare_nested->widget = widget_alloc();
view_dispatcher_add_view(
mifare_nested->view_dispatcher,
MifareNestedViewWidget,
widget_get_view(mifare_nested->widget));
// Variable Item List
mifare_nested->variable_item_list = variable_item_list_alloc();
view_dispatcher_add_view(
mifare_nested->view_dispatcher,
MifareNestedViewVariableList,
variable_item_list_get_view(mifare_nested->variable_item_list));
// Nested attack state
NestedState* plugin_state = collection_alloc();
view_set_context(plugin_state->view, mifare_nested);
mifare_nested->nested_state = plugin_state;
view_dispatcher_add_view(
mifare_nested->view_dispatcher, MifareNestedViewCollecting, plugin_state->view);
// Check keys attack state
CheckKeysState* keys_state = check_keys_alloc();
view_set_context(keys_state->view, mifare_nested);
mifare_nested->keys_state = keys_state;
view_dispatcher_add_view(
mifare_nested->view_dispatcher, MifareNestedViewCheckKeys, keys_state->view);
KeyInfo_t* key_info = malloc(sizeof(KeyInfo_t));
mifare_nested->keys = key_info;
MifareNestedSettings* settings = malloc(sizeof(MifareNestedSettings));
settings->only_hardnested = false;
mifare_nested->settings = settings;
view_set_draw_callback(plugin_state->view, nested_draw_callback);
view_set_input_callback(plugin_state->view, nested_input_callback);
view_set_draw_callback(keys_state->view, check_keys_draw_callback);
view_set_input_callback(keys_state->view, nested_input_callback);
mifare_nested->collecting_type = MifareNestedWorkerStateReady;
mifare_nested->run = NestedRunIdle;
return mifare_nested;
}
void mifare_nested_free(MifareNested* mifare_nested) {
furi_assert(mifare_nested);
// Nfc device
nfc_device_free(mifare_nested->nfc_dev);
// Submenu
view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewMenu);
submenu_free(mifare_nested->submenu);
// Popup
view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewPopup);
popup_free(mifare_nested->popup);
// Loading
view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewLoading);
loading_free(mifare_nested->loading);
// TextInput
view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewTextInput);
text_input_free(mifare_nested->text_input);
// Custom Widget
view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewWidget);
widget_free(mifare_nested->widget);
// Variable Item List
view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewVariableList);
variable_item_list_free(mifare_nested->variable_item_list);
// Nested
view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewCollecting);
// Check keys
view_dispatcher_remove_view(mifare_nested->view_dispatcher, MifareNestedViewCheckKeys);
// Nonces states
free(mifare_nested->nonces);
free(mifare_nested->nested_state);
// Keys
free(mifare_nested->keys);
// Settings
free(mifare_nested->settings);
// Worker
mifare_nested_worker_stop(mifare_nested->worker);
mifare_nested_worker_free(mifare_nested->worker);
// View Dispatcher
view_dispatcher_free(mifare_nested->view_dispatcher);
// Scene Manager
scene_manager_free(mifare_nested->scene_manager);
// GUI
furi_record_close(RECORD_GUI);
mifare_nested->gui = NULL;
// Notifications
furi_record_close(RECORD_NOTIFICATION);
mifare_nested->notifications = NULL;
free(mifare_nested);
}
void mifare_nested_blink_start(MifareNested* mifare_nested) {
notification_message(mifare_nested->notifications, &mifare_nested_sequence_blink_start_blue);
}
void mifare_nested_blink_calibration_start(MifareNested* mifare_nested) {
notification_message(
mifare_nested->notifications, &mifare_nested_sequence_blink_start_magenta);
}
void mifare_nested_blink_nonce_collection_start(MifareNested* mifare_nested) {
notification_message(mifare_nested->notifications, &mifare_nested_sequence_blink_start_yellow);
}
void mifare_nested_blink_stop(MifareNested* mifare_nested) {
notification_message(mifare_nested->notifications, &mifare_nested_sequence_blink_stop);
}
int32_t mifare_nested_app(void* p) {
UNUSED(p);
MifareNested* mifare_nested = mifare_nested_alloc();
scene_manager_next_scene(mifare_nested->scene_manager, MifareNestedSceneStart);
view_dispatcher_run(mifare_nested->view_dispatcher);
mifare_nested_free(mifare_nested);
return 0;
}