unleashed-firmware/applications/external/pacs_fuzzer/views/attack.c

444 lines
15 KiB
C
Raw Normal View History

2023-06-02 20:33:28 +03:00
#include "attack.h"
#include "../fuzzer_i.h"
#include <input/input.h>
#include <gui/elements.h>
2023-06-06 22:43:44 +03:00
#define ATACK_VIEW_V2
// #define ATACK_VIEW_V2_1
#define ATACK_VIEW_V2_2
2023-06-02 20:33:28 +03:00
#define ATTACK_SCENE_MAX_UID_LENGTH 25
2023-06-05 14:10:51 +03:00
#define UID_MAX_DISPLAYED_LEN (8U)
2023-06-06 22:43:44 +03:00
#define LIFT_RIGHT_OFFSET (3)
2023-06-02 20:33:28 +03:00
struct FuzzerViewAttack {
View* view;
FuzzerViewAttackCallback callback;
void* context;
};
typedef struct {
2023-06-06 22:43:44 +03:00
uint8_t time_delay; // 1 = 100ms
uint8_t time_delay_min; // 1 = 100ms
uint8_t emu_time; // 1 = 100ms
uint8_t emu_time_min; // 1 = 100ms
bool td_emt_cursor; // false - time_delay, true - emu_time
2023-06-02 20:33:28 +03:00
const char* attack_name;
const char* protocol_name;
2023-06-05 17:49:30 +03:00
FuzzerAttackState attack_state;
2023-06-06 11:14:23 +03:00
FuriString* uid_str;
2023-06-02 20:33:28 +03:00
} FuzzerViewAttackModel;
void fuzzer_view_attack_reset_data(
FuzzerViewAttack* view,
const char* attack_name,
2023-06-05 14:10:51 +03:00
const char* protocol_name) {
2023-06-02 20:33:28 +03:00
furi_assert(view);
with_view_model(
view->view,
FuzzerViewAttackModel * model,
{
model->attack_name = attack_name;
model->protocol_name = protocol_name;
2023-06-05 17:49:30 +03:00
model->attack_state = FuzzerAttackStateIdle;
2023-06-06 11:14:23 +03:00
furi_string_set_str(model->uid_str, "Not_set");
2023-06-02 20:33:28 +03:00
},
true);
}
void fuzzer_view_attack_set_uid(FuzzerViewAttack* view, const FuzzerPayload* uid) {
2023-06-02 20:33:28 +03:00
furi_assert(view);
furi_assert(uid->data);
2023-06-05 14:10:51 +03:00
2023-06-02 20:33:28 +03:00
with_view_model(
view->view,
FuzzerViewAttackModel * model,
{
furi_string_printf(model->uid_str, "%02X", uid->data[0]);
for(uint8_t i = 1; i < uid->data_size; i++) {
furi_string_cat_printf(model->uid_str, ":%02X", uid->data[i]);
2023-06-06 11:14:23 +03:00
}
2023-06-02 20:33:28 +03:00
},
true);
}
2023-06-05 17:49:30 +03:00
void fuzzer_view_attack_start(FuzzerViewAttack* view) {
2023-06-03 00:32:32 +03:00
furi_assert(view);
with_view_model(
2023-06-05 17:49:30 +03:00
view->view,
FuzzerViewAttackModel * model,
{ model->attack_state = FuzzerAttackStateRunning; },
true);
}
void fuzzer_view_attack_stop(FuzzerViewAttack* view) {
furi_assert(view);
with_view_model(
view->view,
FuzzerViewAttackModel * model,
{ model->attack_state = FuzzerAttackStateOff; },
true);
}
void fuzzer_view_attack_pause(FuzzerViewAttack* view) {
furi_assert(view);
with_view_model(
view->view,
FuzzerViewAttackModel * model,
{ model->attack_state = FuzzerAttackStateIdle; },
true);
}
void fuzzer_view_attack_end(FuzzerViewAttack* view) {
furi_assert(view);
with_view_model(
view->view,
FuzzerViewAttackModel * model,
{ model->attack_state = FuzzerAttackStateEnd; },
true);
2023-06-03 00:32:32 +03:00
}
2023-06-02 20:33:28 +03:00
void fuzzer_view_attack_set_callback(
FuzzerViewAttack* view_attack,
FuzzerViewAttackCallback callback,
void* context) {
furi_assert(view_attack);
view_attack->callback = callback;
view_attack->context = context;
}
void fuzzer_view_attack_draw(Canvas* canvas, FuzzerViewAttackModel* model) {
2023-06-06 22:43:44 +03:00
char temp_str[50];
2023-06-02 20:33:28 +03:00
canvas_clear(canvas);
canvas_set_color(canvas, ColorBlack);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, model->attack_name);
2023-06-06 22:43:44 +03:00
#ifndef ATACK_VIEW_V2
canvas_set_font(canvas, FontSecondary);
snprintf(
temp_str,
sizeof(temp_str),
"Time delay: %d.%d",
model->time_delay / 10,
model->time_delay % 10);
canvas_draw_str_aligned(canvas, 64, 14, AlignCenter, AlignTop, temp_str);
#elif defined(ATACK_VIEW_V2_1)
canvas_set_font(canvas, FontSecondary);
if(!model->td_emt_cursor) {
snprintf(
temp_str,
sizeof(temp_str),
"Time delay: %d.%d EmT: %d.%d",
model->time_delay / 10,
model->time_delay % 10,
model->emu_time / 10,
model->emu_time % 10);
} else {
snprintf(
temp_str,
sizeof(temp_str),
"TD: %d.%d Emulation time: %d.%d",
model->time_delay / 10,
model->time_delay % 10,
model->emu_time / 10,
model->emu_time % 10);
}
canvas_draw_str_aligned(canvas, 64, 21, AlignCenter, AlignBottom, temp_str);
#elif defined(ATACK_VIEW_V2_2)
uint16_t crt;
canvas_set_font(canvas, FontPrimary);
if(!model->td_emt_cursor) {
canvas_set_font(canvas, FontSecondary);
snprintf(temp_str, sizeof(temp_str), "Time delay:");
canvas_draw_str_aligned(canvas, LIFT_RIGHT_OFFSET, 21, AlignLeft, AlignBottom, temp_str);
crt = canvas_string_width(canvas, temp_str);
canvas_set_font(canvas, FontPrimary);
snprintf(
temp_str, sizeof(temp_str), "%d.%d", model->time_delay / 10, model->time_delay % 10);
canvas_draw_str_aligned(
canvas, crt + LIFT_RIGHT_OFFSET + 3, 21, AlignLeft, AlignBottom, temp_str);
canvas_set_font(canvas, FontSecondary);
snprintf(
temp_str, sizeof(temp_str), "EmT: %d.%d", model->emu_time / 10, model->emu_time % 10);
canvas_draw_str_aligned(
canvas, 128 - LIFT_RIGHT_OFFSET, 21, AlignRight, AlignBottom, temp_str);
} else {
canvas_set_font(canvas, FontSecondary);
snprintf(
temp_str,
sizeof(temp_str),
"TD: %d.%d",
model->time_delay / 10,
model->time_delay % 10);
canvas_draw_str_aligned(canvas, LIFT_RIGHT_OFFSET, 21, AlignLeft, AlignBottom, temp_str);
canvas_set_font(canvas, FontPrimary);
snprintf(temp_str, sizeof(temp_str), "%d.%d", model->emu_time / 10, model->emu_time % 10);
canvas_draw_str_aligned(
canvas, 128 - LIFT_RIGHT_OFFSET, 21, AlignRight, AlignBottom, temp_str);
crt = canvas_string_width(canvas, temp_str);
canvas_set_font(canvas, FontSecondary);
snprintf(temp_str, sizeof(temp_str), "Emulation time:");
canvas_draw_str_aligned(
canvas, 128 - LIFT_RIGHT_OFFSET - crt - 3, 21, AlignRight, AlignBottom, temp_str);
}
#else
canvas_set_font(canvas, FontSecondary);
snprintf(
temp_str,
sizeof(temp_str),
"Time delay: %d.%d Emu time: %d.%d",
model->time_delay / 10,
model->time_delay % 10,
model->emu_time / 10,
model->emu_time % 10);
canvas_draw_str_aligned(canvas, 64, 14, AlignCenter, AlignTop, temp_str);
#endif
2023-06-02 20:33:28 +03:00
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 64, 26, AlignCenter, AlignTop, model->protocol_name);
canvas_set_font(canvas, FontPrimary);
2023-06-06 11:14:23 +03:00
if(128 < canvas_string_width(canvas, furi_string_get_cstr(model->uid_str))) {
2023-06-02 20:33:28 +03:00
canvas_set_font(canvas, FontSecondary);
}
2023-06-06 11:14:23 +03:00
canvas_draw_str_aligned(
canvas, 64, 38, AlignCenter, AlignTop, furi_string_get_cstr(model->uid_str));
2023-06-02 20:33:28 +03:00
2023-06-03 00:32:32 +03:00
canvas_set_font(canvas, FontSecondary);
2023-06-05 17:49:30 +03:00
if(model->attack_state == FuzzerAttackStateRunning) {
2023-06-02 20:33:28 +03:00
elements_button_center(canvas, "Stop");
2023-06-05 17:49:30 +03:00
} else if(model->attack_state == FuzzerAttackStateIdle) {
2023-06-06 22:43:44 +03:00
#ifndef ATACK_VIEW_V2
2023-06-02 20:33:28 +03:00
elements_button_center(canvas, "Start");
elements_button_left(canvas, "TD -");
elements_button_right(canvas, "+ TD");
2023-06-06 22:43:44 +03:00
#else
if(model->td_emt_cursor) {
elements_button_center(canvas, "Start");
elements_button_left(canvas, "EmT -");
elements_button_right(canvas, "+ EmT");
} else {
elements_button_center(canvas, "Start");
elements_button_left(canvas, "TD -");
elements_button_right(canvas, "+ TD");
}
#endif
2023-06-05 17:49:30 +03:00
} else if(model->attack_state == FuzzerAttackStateEnd) {
// elements_button_center(canvas, "Restart"); // Reset
elements_button_left(canvas, "Exit");
2023-06-02 20:33:28 +03:00
}
}
bool fuzzer_view_attack_input(InputEvent* event, void* context) {
furi_assert(context);
FuzzerViewAttack* view_attack = context;
if(event->key == InputKeyBack && event->type == InputTypeShort) {
view_attack->callback(FuzzerCustomEventViewAttackBack, view_attack->context);
return true;
} else if(event->key == InputKeyOk && event->type == InputTypeShort) {
view_attack->callback(FuzzerCustomEventViewAttackOk, view_attack->context);
return true;
} else if(event->key == InputKeyLeft) {
with_view_model(
view_attack->view,
FuzzerViewAttackModel * model,
{
2023-06-05 17:49:30 +03:00
if(model->attack_state == FuzzerAttackStateIdle) {
2023-06-06 22:43:44 +03:00
if(!model->td_emt_cursor) {
// TimeDelay --
if(event->type == InputTypeShort) {
if(model->time_delay > model->time_delay_min) {
model->time_delay--;
}
} else if(event->type == InputTypeLong) {
if((model->time_delay - 10) >= model->time_delay_min) {
model->time_delay -= 10;
} else {
model->time_delay = model->time_delay_min;
}
2023-06-02 20:33:28 +03:00
}
2023-06-06 22:43:44 +03:00
} else {
// EmuTime --
if(event->type == InputTypeShort) {
if(model->emu_time > model->emu_time_min) {
model->emu_time--;
}
} else if(event->type == InputTypeLong) {
if((model->emu_time - 10) >= model->emu_time_min) {
model->emu_time -= 10;
} else {
model->emu_time = model->emu_time_min;
}
2023-06-02 20:33:28 +03:00
}
}
2023-06-05 17:49:30 +03:00
} else if(
(model->attack_state == FuzzerAttackStateEnd) &&
(event->type == InputTypeShort)) {
// Exit if Ended
view_attack->callback(FuzzerCustomEventViewAttackBack, view_attack->context);
2023-06-02 20:33:28 +03:00
}
},
true);
return true;
} else if(event->key == InputKeyRight) {
with_view_model(
view_attack->view,
FuzzerViewAttackModel * model,
{
2023-06-05 17:49:30 +03:00
if(model->attack_state == FuzzerAttackStateIdle) {
2023-06-06 22:43:44 +03:00
if(!model->td_emt_cursor) {
// TimeDelay ++
if(event->type == InputTypeShort) {
if(model->time_delay < FUZZ_TIME_DELAY_MAX) {
model->time_delay++;
}
} else if(event->type == InputTypeLong) {
model->time_delay += 10;
if(model->time_delay > FUZZ_TIME_DELAY_MAX) {
model->time_delay = FUZZ_TIME_DELAY_MAX;
}
2023-06-02 20:33:28 +03:00
}
2023-06-06 22:43:44 +03:00
} else {
// EmuTime ++
if(event->type == InputTypeShort) {
if(model->emu_time < FUZZ_TIME_DELAY_MAX) {
model->emu_time++;
}
} else if(event->type == InputTypeLong) {
model->emu_time += 10;
if(model->emu_time > FUZZ_TIME_DELAY_MAX) {
model->emu_time = FUZZ_TIME_DELAY_MAX;
}
2023-06-02 20:33:28 +03:00
}
}
2023-06-05 17:49:30 +03:00
} else {
// Nothing
2023-06-02 20:33:28 +03:00
}
},
true);
return true;
2023-06-06 22:43:44 +03:00
} else if(
(event->key == InputKeyUp || event->key == InputKeyDown) &&
event->type == InputTypeShort) {
with_view_model(
view_attack->view,
FuzzerViewAttackModel * model,
{ model->td_emt_cursor = !model->td_emt_cursor; },
true);
return true;
2023-06-02 20:33:28 +03:00
}
return true;
}
void fuzzer_view_attack_enter(void* context) {
furi_assert(context);
}
void fuzzer_view_attack_exit(void* context) {
furi_assert(context);
2023-06-06 22:43:44 +03:00
FuzzerViewAttack* view_attack = context;
with_view_model(
view_attack->view, FuzzerViewAttackModel * model, { model->td_emt_cursor = false; }, true);
2023-06-02 20:33:28 +03:00
}
FuzzerViewAttack* fuzzer_view_attack_alloc() {
2023-06-05 14:10:51 +03:00
if(fuzzer_proto_get_max_data_size() > UID_MAX_DISPLAYED_LEN) {
furi_crash("Maximum of displayed bytes exceeded");
}
2023-06-02 20:33:28 +03:00
FuzzerViewAttack* view_attack = malloc(sizeof(FuzzerViewAttack));
// View allocation and configuration
view_attack->view = view_alloc();
view_allocate_model(view_attack->view, ViewModelTypeLocking, sizeof(FuzzerViewAttackModel));
view_set_context(view_attack->view, view_attack);
view_set_draw_callback(view_attack->view, (ViewDrawCallback)fuzzer_view_attack_draw);
view_set_input_callback(view_attack->view, fuzzer_view_attack_input);
view_set_enter_callback(view_attack->view, fuzzer_view_attack_enter);
view_set_exit_callback(view_attack->view, fuzzer_view_attack_exit);
with_view_model(
view_attack->view,
FuzzerViewAttackModel * model,
{
2023-06-06 22:43:44 +03:00
model->time_delay = fuzzer_proto_get_def_idle_time();
model->time_delay_min = 0; // model->time_delay;
model->emu_time = fuzzer_proto_get_def_emu_time();
model->emu_time_min = 2; // model->emu_time;
2023-06-06 11:14:23 +03:00
model->uid_str = furi_string_alloc_set_str("Not_set");
// malloc(ATTACK_SCENE_MAX_UID_LENGTH + 1);
2023-06-05 17:49:30 +03:00
model->attack_state = FuzzerAttackStateOff;
2023-06-06 22:43:44 +03:00
model->td_emt_cursor = false;
2023-06-02 20:33:28 +03:00
2023-06-06 11:14:23 +03:00
// strcpy(model->uid_str, "Not_set");
2023-06-02 20:33:28 +03:00
model->attack_name = "Not_set";
model->protocol_name = "Not_set";
},
true);
return view_attack;
}
void fuzzer_view_attack_free(FuzzerViewAttack* view_attack) {
furi_assert(view_attack);
with_view_model(
2023-06-06 11:14:23 +03:00
view_attack->view,
FuzzerViewAttackModel * model,
{ furi_string_free(model->uid_str); },
true);
2023-06-02 20:33:28 +03:00
view_free(view_attack->view);
free(view_attack);
}
View* fuzzer_view_attack_get_view(FuzzerViewAttack* view_attack) {
furi_assert(view_attack);
return view_attack->view;
2023-06-03 00:32:32 +03:00
}
uint8_t fuzzer_view_attack_get_time_delay(FuzzerViewAttack* view) {
furi_assert(view);
uint8_t time_delay;
with_view_model(
view->view, FuzzerViewAttackModel * model, { time_delay = model->time_delay; }, false);
return time_delay;
2023-06-06 22:43:44 +03:00
}
uint8_t fuzzer_view_attack_get_emu_time(FuzzerViewAttack* view) {
furi_assert(view);
uint8_t emu_time;
with_view_model(
view->view, FuzzerViewAttackModel * model, { emu_time = model->emu_time; }, false);
return emu_time;
2023-06-02 20:33:28 +03:00
}