attack.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include "attack.h"
  2. #include "../fuzzer_i.h"
  3. #include <input/input.h>
  4. #include <gui/elements.h>
  5. #define ATTACK_SCENE_MAX_UID_LENGTH 25
  6. #define UID_MAX_DISPLAYED_LEN (8U)
  7. #define LIFT_RIGHT_OFFSET (3)
  8. struct FuzzerViewAttack {
  9. View* view;
  10. FuzzerViewAttackCallback callback;
  11. void* context;
  12. };
  13. typedef struct {
  14. uint8_t time_delay; // 1 = 100ms
  15. uint8_t time_delay_min; // 1 = 100ms
  16. uint8_t emu_time; // 1 = 100ms
  17. uint8_t emu_time_min; // 1 = 100ms
  18. bool td_emt_cursor; // false - time_delay, true - emu_time
  19. const char* attack_name;
  20. const char* protocol_name;
  21. FuzzerAttackState attack_state;
  22. FuriString* uid_str;
  23. } FuzzerViewAttackModel;
  24. void fuzzer_view_attack_reset_data(
  25. FuzzerViewAttack* view,
  26. const char* attack_name,
  27. const char* protocol_name) {
  28. furi_assert(view);
  29. with_view_model(
  30. view->view,
  31. FuzzerViewAttackModel * model,
  32. {
  33. model->attack_name = attack_name;
  34. model->protocol_name = protocol_name;
  35. model->attack_state = FuzzerAttackStateIdle;
  36. furi_string_set_str(model->uid_str, "Not_set");
  37. },
  38. true);
  39. }
  40. void fuzzer_view_attack_set_uid(FuzzerViewAttack* view, const FuzzerPayload* uid) {
  41. furi_assert(view);
  42. furi_assert(uid->data);
  43. with_view_model(
  44. view->view,
  45. FuzzerViewAttackModel * model,
  46. {
  47. furi_string_printf(model->uid_str, "%02X", uid->data[0]);
  48. for(uint8_t i = 1; i < uid->data_size; i++) {
  49. furi_string_cat_printf(model->uid_str, ":%02X", uid->data[i]);
  50. }
  51. },
  52. true);
  53. }
  54. void fuzzer_view_attack_start(FuzzerViewAttack* view) {
  55. furi_assert(view);
  56. with_view_model(
  57. view->view,
  58. FuzzerViewAttackModel * model,
  59. { model->attack_state = FuzzerAttackStateRunning; },
  60. true);
  61. }
  62. void fuzzer_view_attack_stop(FuzzerViewAttack* view) {
  63. furi_assert(view);
  64. with_view_model(
  65. view->view,
  66. FuzzerViewAttackModel * model,
  67. { model->attack_state = FuzzerAttackStateOff; },
  68. true);
  69. }
  70. void fuzzer_view_attack_pause(FuzzerViewAttack* view) {
  71. furi_assert(view);
  72. with_view_model(
  73. view->view,
  74. FuzzerViewAttackModel * model,
  75. { model->attack_state = FuzzerAttackStateIdle; },
  76. true);
  77. }
  78. void fuzzer_view_attack_end(FuzzerViewAttack* view) {
  79. furi_assert(view);
  80. with_view_model(
  81. view->view,
  82. FuzzerViewAttackModel * model,
  83. { model->attack_state = FuzzerAttackStateEnd; },
  84. true);
  85. }
  86. void fuzzer_view_attack_set_callback(
  87. FuzzerViewAttack* view_attack,
  88. FuzzerViewAttackCallback callback,
  89. void* context) {
  90. furi_assert(view_attack);
  91. view_attack->callback = callback;
  92. view_attack->context = context;
  93. }
  94. void fuzzer_view_attack_draw(Canvas* canvas, FuzzerViewAttackModel* model) {
  95. char temp_str[50];
  96. canvas_clear(canvas);
  97. canvas_set_color(canvas, ColorBlack);
  98. canvas_set_font(canvas, FontPrimary);
  99. canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, model->attack_name);
  100. uint16_t crt;
  101. canvas_set_font(canvas, FontPrimary);
  102. if(!model->td_emt_cursor) {
  103. canvas_set_font(canvas, FontSecondary);
  104. snprintf(temp_str, sizeof(temp_str), "Time delay:");
  105. canvas_draw_str_aligned(canvas, LIFT_RIGHT_OFFSET, 21, AlignLeft, AlignBottom, temp_str);
  106. crt = canvas_string_width(canvas, temp_str);
  107. canvas_set_font(canvas, FontPrimary);
  108. snprintf(
  109. temp_str, sizeof(temp_str), "%d.%d", model->time_delay / 10, model->time_delay % 10);
  110. canvas_draw_str_aligned(
  111. canvas, crt + LIFT_RIGHT_OFFSET + 3, 21, AlignLeft, AlignBottom, temp_str);
  112. canvas_set_font(canvas, FontSecondary);
  113. snprintf(
  114. temp_str, sizeof(temp_str), "EmT: %d.%d", model->emu_time / 10, model->emu_time % 10);
  115. canvas_draw_str_aligned(
  116. canvas, 128 - LIFT_RIGHT_OFFSET, 21, AlignRight, AlignBottom, temp_str);
  117. } else {
  118. canvas_set_font(canvas, FontSecondary);
  119. snprintf(
  120. temp_str,
  121. sizeof(temp_str),
  122. "TD: %d.%d",
  123. model->time_delay / 10,
  124. model->time_delay % 10);
  125. canvas_draw_str_aligned(canvas, LIFT_RIGHT_OFFSET, 21, AlignLeft, AlignBottom, temp_str);
  126. canvas_set_font(canvas, FontPrimary);
  127. snprintf(temp_str, sizeof(temp_str), "%d.%d", model->emu_time / 10, model->emu_time % 10);
  128. canvas_draw_str_aligned(
  129. canvas, 128 - LIFT_RIGHT_OFFSET, 21, AlignRight, AlignBottom, temp_str);
  130. crt = canvas_string_width(canvas, temp_str);
  131. canvas_set_font(canvas, FontSecondary);
  132. snprintf(temp_str, sizeof(temp_str), "Emulation time:");
  133. canvas_draw_str_aligned(
  134. canvas, 128 - LIFT_RIGHT_OFFSET - crt - 3, 21, AlignRight, AlignBottom, temp_str);
  135. }
  136. canvas_set_font(canvas, FontSecondary);
  137. canvas_draw_str_aligned(canvas, 64, 26, AlignCenter, AlignTop, model->protocol_name);
  138. canvas_set_font(canvas, FontPrimary);
  139. if(128 < canvas_string_width(canvas, furi_string_get_cstr(model->uid_str))) {
  140. canvas_set_font(canvas, FontSecondary);
  141. }
  142. canvas_draw_str_aligned(
  143. canvas, 64, 38, AlignCenter, AlignTop, furi_string_get_cstr(model->uid_str));
  144. canvas_set_font(canvas, FontSecondary);
  145. if(model->attack_state == FuzzerAttackStateRunning) {
  146. elements_button_center(canvas, "Stop");
  147. } else if(model->attack_state == FuzzerAttackStateIdle) {
  148. if(model->td_emt_cursor) {
  149. elements_button_center(canvas, "Start");
  150. elements_button_left(canvas, "EmT -");
  151. elements_button_right(canvas, "+ EmT");
  152. } else {
  153. elements_button_center(canvas, "Start");
  154. elements_button_left(canvas, "TD -");
  155. elements_button_right(canvas, "+ TD");
  156. }
  157. } else if(model->attack_state == FuzzerAttackStateEnd) {
  158. // elements_button_center(canvas, "Restart"); // Reset
  159. elements_button_left(canvas, "Exit");
  160. }
  161. }
  162. bool fuzzer_view_attack_input(InputEvent* event, void* context) {
  163. furi_assert(context);
  164. FuzzerViewAttack* view_attack = context;
  165. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  166. view_attack->callback(FuzzerCustomEventViewAttackBack, view_attack->context);
  167. return true;
  168. } else if(event->key == InputKeyOk && event->type == InputTypeShort) {
  169. view_attack->callback(FuzzerCustomEventViewAttackOk, view_attack->context);
  170. return true;
  171. } else if(event->key == InputKeyLeft) {
  172. with_view_model(
  173. view_attack->view,
  174. FuzzerViewAttackModel * model,
  175. {
  176. if(model->attack_state == FuzzerAttackStateIdle) {
  177. if(!model->td_emt_cursor) {
  178. // TimeDelay --
  179. if(event->type == InputTypeShort) {
  180. if(model->time_delay > model->time_delay_min) {
  181. model->time_delay--;
  182. }
  183. } else if(event->type == InputTypeLong) {
  184. if((model->time_delay - 10) >= model->time_delay_min) {
  185. model->time_delay -= 10;
  186. } else {
  187. model->time_delay = model->time_delay_min;
  188. }
  189. }
  190. } else {
  191. // EmuTime --
  192. if(event->type == InputTypeShort) {
  193. if(model->emu_time > model->emu_time_min) {
  194. model->emu_time--;
  195. }
  196. } else if(event->type == InputTypeLong) {
  197. if((model->emu_time - 10) >= model->emu_time_min) {
  198. model->emu_time -= 10;
  199. } else {
  200. model->emu_time = model->emu_time_min;
  201. }
  202. }
  203. }
  204. } else if(
  205. (model->attack_state == FuzzerAttackStateEnd) &&
  206. (event->type == InputTypeShort)) {
  207. // Exit if Ended
  208. view_attack->callback(FuzzerCustomEventViewAttackBack, view_attack->context);
  209. }
  210. },
  211. true);
  212. return true;
  213. } else if(event->key == InputKeyRight) {
  214. with_view_model(
  215. view_attack->view,
  216. FuzzerViewAttackModel * model,
  217. {
  218. if(model->attack_state == FuzzerAttackStateIdle) {
  219. if(!model->td_emt_cursor) {
  220. // TimeDelay ++
  221. if(event->type == InputTypeShort) {
  222. if(model->time_delay < FUZZ_TIME_DELAY_MAX) {
  223. model->time_delay++;
  224. }
  225. } else if(event->type == InputTypeLong) {
  226. model->time_delay += 10;
  227. if(model->time_delay > FUZZ_TIME_DELAY_MAX) {
  228. model->time_delay = FUZZ_TIME_DELAY_MAX;
  229. }
  230. }
  231. } else {
  232. // EmuTime ++
  233. if(event->type == InputTypeShort) {
  234. if(model->emu_time < FUZZ_TIME_DELAY_MAX) {
  235. model->emu_time++;
  236. }
  237. } else if(event->type == InputTypeLong) {
  238. model->emu_time += 10;
  239. if(model->emu_time > FUZZ_TIME_DELAY_MAX) {
  240. model->emu_time = FUZZ_TIME_DELAY_MAX;
  241. }
  242. }
  243. }
  244. } else {
  245. // Nothing
  246. }
  247. },
  248. true);
  249. return true;
  250. } else if(
  251. (event->key == InputKeyUp || event->key == InputKeyDown) &&
  252. event->type == InputTypeShort) {
  253. with_view_model(
  254. view_attack->view,
  255. FuzzerViewAttackModel * model,
  256. { model->td_emt_cursor = !model->td_emt_cursor; },
  257. true);
  258. return true;
  259. }
  260. return true;
  261. }
  262. void fuzzer_view_attack_enter(void* context) {
  263. furi_assert(context);
  264. }
  265. void fuzzer_view_attack_exit(void* context) {
  266. furi_assert(context);
  267. FuzzerViewAttack* view_attack = context;
  268. with_view_model(
  269. view_attack->view, FuzzerViewAttackModel * model, { model->td_emt_cursor = false; }, true);
  270. }
  271. FuzzerViewAttack* fuzzer_view_attack_alloc() {
  272. if(fuzzer_proto_get_max_data_size() > UID_MAX_DISPLAYED_LEN) {
  273. furi_crash("Maximum of displayed bytes exceeded");
  274. }
  275. FuzzerViewAttack* view_attack = malloc(sizeof(FuzzerViewAttack));
  276. // View allocation and configuration
  277. view_attack->view = view_alloc();
  278. view_allocate_model(view_attack->view, ViewModelTypeLocking, sizeof(FuzzerViewAttackModel));
  279. view_set_context(view_attack->view, view_attack);
  280. view_set_draw_callback(view_attack->view, (ViewDrawCallback)fuzzer_view_attack_draw);
  281. view_set_input_callback(view_attack->view, fuzzer_view_attack_input);
  282. view_set_enter_callback(view_attack->view, fuzzer_view_attack_enter);
  283. view_set_exit_callback(view_attack->view, fuzzer_view_attack_exit);
  284. with_view_model(
  285. view_attack->view,
  286. FuzzerViewAttackModel * model,
  287. {
  288. model->time_delay = fuzzer_proto_get_def_idle_time();
  289. model->time_delay_min = 0; // model->time_delay;
  290. model->emu_time = fuzzer_proto_get_def_emu_time();
  291. model->emu_time_min = 2; // model->emu_time;
  292. model->uid_str = furi_string_alloc_set_str("Not_set");
  293. // malloc(ATTACK_SCENE_MAX_UID_LENGTH + 1);
  294. model->attack_state = FuzzerAttackStateOff;
  295. model->td_emt_cursor = false;
  296. // strcpy(model->uid_str, "Not_set");
  297. model->attack_name = "Not_set";
  298. model->protocol_name = "Not_set";
  299. },
  300. true);
  301. return view_attack;
  302. }
  303. void fuzzer_view_attack_free(FuzzerViewAttack* view_attack) {
  304. furi_assert(view_attack);
  305. with_view_model(
  306. view_attack->view,
  307. FuzzerViewAttackModel * model,
  308. { furi_string_free(model->uid_str); },
  309. true);
  310. view_free(view_attack->view);
  311. free(view_attack);
  312. }
  313. View* fuzzer_view_attack_get_view(FuzzerViewAttack* view_attack) {
  314. furi_assert(view_attack);
  315. return view_attack->view;
  316. }
  317. uint8_t fuzzer_view_attack_get_time_delay(FuzzerViewAttack* view) {
  318. furi_assert(view);
  319. uint8_t time_delay;
  320. with_view_model(
  321. view->view, FuzzerViewAttackModel * model, { time_delay = model->time_delay; }, false);
  322. return time_delay;
  323. }
  324. uint8_t fuzzer_view_attack_get_emu_time(FuzzerViewAttack* view) {
  325. furi_assert(view);
  326. uint8_t emu_time;
  327. with_view_model(
  328. view->view, FuzzerViewAttackModel * model, { emu_time = model->emu_time; }, false);
  329. return emu_time;
  330. }