attack.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 = FuzzerAttackStatePause; },
  76. true);
  77. }
  78. void fuzzer_view_attack_idle(FuzzerViewAttack* view) {
  79. furi_assert(view);
  80. with_view_model(
  81. view->view,
  82. FuzzerViewAttackModel * model,
  83. { model->attack_state = FuzzerAttackStateIdle; },
  84. true);
  85. }
  86. void fuzzer_view_attack_end(FuzzerViewAttack* view) {
  87. furi_assert(view);
  88. with_view_model(
  89. view->view,
  90. FuzzerViewAttackModel * model,
  91. { model->attack_state = FuzzerAttackStateEnd; },
  92. true);
  93. }
  94. void fuzzer_view_attack_set_callback(
  95. FuzzerViewAttack* view_attack,
  96. FuzzerViewAttackCallback callback,
  97. void* context) {
  98. furi_assert(view_attack);
  99. view_attack->callback = callback;
  100. view_attack->context = context;
  101. }
  102. static void fuzzer_view_attack_draw_idle(Canvas* canvas, FuzzerViewAttackModel* model) {
  103. if(model->td_emt_cursor) {
  104. elements_button_center(canvas, "Start");
  105. elements_button_left(canvas, "EmT -");
  106. elements_button_right(canvas, "+ EmT");
  107. } else {
  108. elements_button_center(canvas, "Start");
  109. elements_button_left(canvas, "TD -");
  110. elements_button_right(canvas, "+ TD");
  111. }
  112. }
  113. static void fuzzer_view_attack_draw_running(Canvas* canvas, FuzzerViewAttackModel* model) {
  114. UNUSED(model);
  115. elements_button_center(canvas, "Stop");
  116. }
  117. static void fuzzer_view_attack_draw_end(Canvas* canvas, FuzzerViewAttackModel* model) {
  118. UNUSED(model);
  119. // elements_button_center(canvas, "Restart"); // Reset
  120. elements_button_left(canvas, "Exit");
  121. }
  122. void fuzzer_view_attack_draw(Canvas* canvas, FuzzerViewAttackModel* model) {
  123. char temp_str[50];
  124. canvas_clear(canvas);
  125. canvas_set_color(canvas, ColorBlack);
  126. canvas_set_font(canvas, FontPrimary);
  127. canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, model->attack_name);
  128. uint16_t crt;
  129. canvas_set_font(canvas, FontPrimary);
  130. if(!model->td_emt_cursor) {
  131. canvas_set_font(canvas, FontSecondary);
  132. snprintf(temp_str, sizeof(temp_str), "Time delay:");
  133. canvas_draw_str_aligned(canvas, LIFT_RIGHT_OFFSET, 21, AlignLeft, AlignBottom, temp_str);
  134. crt = canvas_string_width(canvas, temp_str);
  135. canvas_set_font(canvas, FontPrimary);
  136. snprintf(
  137. temp_str, sizeof(temp_str), "%d.%d", model->time_delay / 10, model->time_delay % 10);
  138. canvas_draw_str_aligned(
  139. canvas, crt + LIFT_RIGHT_OFFSET + 3, 21, AlignLeft, AlignBottom, temp_str);
  140. canvas_set_font(canvas, FontSecondary);
  141. snprintf(
  142. temp_str, sizeof(temp_str), "EmT: %d.%d", model->emu_time / 10, model->emu_time % 10);
  143. canvas_draw_str_aligned(
  144. canvas, 128 - LIFT_RIGHT_OFFSET, 21, AlignRight, AlignBottom, temp_str);
  145. } else {
  146. canvas_set_font(canvas, FontSecondary);
  147. snprintf(
  148. temp_str,
  149. sizeof(temp_str),
  150. "TD: %d.%d",
  151. model->time_delay / 10,
  152. model->time_delay % 10);
  153. canvas_draw_str_aligned(canvas, LIFT_RIGHT_OFFSET, 21, AlignLeft, AlignBottom, temp_str);
  154. canvas_set_font(canvas, FontPrimary);
  155. snprintf(temp_str, sizeof(temp_str), "%d.%d", model->emu_time / 10, model->emu_time % 10);
  156. canvas_draw_str_aligned(
  157. canvas, 128 - LIFT_RIGHT_OFFSET, 21, AlignRight, AlignBottom, temp_str);
  158. crt = canvas_string_width(canvas, temp_str);
  159. canvas_set_font(canvas, FontSecondary);
  160. snprintf(temp_str, sizeof(temp_str), "Emulation time:");
  161. canvas_draw_str_aligned(
  162. canvas, 128 - LIFT_RIGHT_OFFSET - crt - 3, 21, AlignRight, AlignBottom, temp_str);
  163. }
  164. canvas_set_font(canvas, FontSecondary);
  165. canvas_draw_str_aligned(canvas, 64, 26, AlignCenter, AlignTop, model->protocol_name);
  166. canvas_set_font(canvas, FontPrimary);
  167. if(128 < canvas_string_width(canvas, furi_string_get_cstr(model->uid_str))) {
  168. canvas_set_font(canvas, FontSecondary);
  169. }
  170. canvas_draw_str_aligned(
  171. canvas, 64, 38, AlignCenter, AlignTop, furi_string_get_cstr(model->uid_str));
  172. canvas_set_font(canvas, FontSecondary);
  173. if(model->attack_state == FuzzerAttackStateRunning) {
  174. fuzzer_view_attack_draw_running(canvas, model);
  175. } else if(model->attack_state == FuzzerAttackStateIdle) {
  176. fuzzer_view_attack_draw_idle(canvas, model);
  177. } else if(model->attack_state == FuzzerAttackStatePause) {
  178. elements_button_left(canvas, "Prev");
  179. elements_button_right(canvas, "Next");
  180. elements_button_center(canvas, "Try"); // XXX
  181. } else if(model->attack_state == FuzzerAttackStateEnd) {
  182. fuzzer_view_attack_draw_end(canvas, model);
  183. }
  184. }
  185. static bool fuzzer_view_attack_input_idle(
  186. FuzzerViewAttack* view_attack,
  187. InputEvent* event,
  188. FuzzerViewAttackModel* model) {
  189. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  190. view_attack->callback(FuzzerCustomEventViewAttackExit, view_attack->context);
  191. return true;
  192. } else if(event->key == InputKeyOk && event->type == InputTypeShort) {
  193. view_attack->callback(FuzzerCustomEventViewAttackRunAttack, view_attack->context);
  194. return true;
  195. } else if(event->key == InputKeyLeft) {
  196. if(!model->td_emt_cursor) {
  197. // TimeDelay --
  198. if(event->type == InputTypeShort) {
  199. if(model->time_delay > model->time_delay_min) {
  200. model->time_delay--;
  201. }
  202. } else if(event->type == InputTypeLong) {
  203. if((model->time_delay - 10) >= model->time_delay_min) {
  204. model->time_delay -= 10;
  205. } else {
  206. model->time_delay = model->time_delay_min;
  207. }
  208. }
  209. } else {
  210. // EmuTime --
  211. if(event->type == InputTypeShort) {
  212. if(model->emu_time > model->emu_time_min) {
  213. model->emu_time--;
  214. }
  215. } else if(event->type == InputTypeLong) {
  216. if((model->emu_time - 10) >= model->emu_time_min) {
  217. model->emu_time -= 10;
  218. } else {
  219. model->emu_time = model->emu_time_min;
  220. }
  221. }
  222. }
  223. return true;
  224. } else if(event->key == InputKeyRight) {
  225. if(!model->td_emt_cursor) {
  226. // TimeDelay ++
  227. if(event->type == InputTypeShort) {
  228. if(model->time_delay < FUZZ_TIME_DELAY_MAX) {
  229. model->time_delay++;
  230. }
  231. } else if(event->type == InputTypeLong) {
  232. model->time_delay += 10;
  233. if(model->time_delay > FUZZ_TIME_DELAY_MAX) {
  234. model->time_delay = FUZZ_TIME_DELAY_MAX;
  235. }
  236. }
  237. } else {
  238. // EmuTime ++
  239. if(event->type == InputTypeShort) {
  240. if(model->emu_time < FUZZ_TIME_DELAY_MAX) {
  241. model->emu_time++;
  242. }
  243. } else if(event->type == InputTypeLong) {
  244. model->emu_time += 10;
  245. if(model->emu_time > FUZZ_TIME_DELAY_MAX) {
  246. model->emu_time = FUZZ_TIME_DELAY_MAX;
  247. }
  248. }
  249. }
  250. return true;
  251. } else if(
  252. (event->key == InputKeyUp || event->key == InputKeyDown) &&
  253. event->type == InputTypeShort) {
  254. with_view_model(
  255. view_attack->view,
  256. FuzzerViewAttackModel * model,
  257. { model->td_emt_cursor = !model->td_emt_cursor; },
  258. true);
  259. return true;
  260. }
  261. return true;
  262. }
  263. static bool fuzzer_view_attack_input_end(
  264. FuzzerViewAttack* view_attack,
  265. InputEvent* event,
  266. FuzzerViewAttackModel* model) {
  267. UNUSED(model);
  268. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  269. view_attack->callback(FuzzerCustomEventViewAttackExit, view_attack->context);
  270. return true;
  271. // } else if(event->key == InputKeyOk && event->type == InputTypeShort) {
  272. // view_attack->callback(FuzzerCustomEventViewAttackOk, view_attack->context);
  273. // return true;
  274. } else if(event->key == InputKeyLeft && event->type == InputTypeShort) {
  275. // Exit if Ended
  276. view_attack->callback(FuzzerCustomEventViewAttackExit, view_attack->context);
  277. }
  278. return true;
  279. }
  280. bool fuzzer_view_attack_input(InputEvent* event, void* context) {
  281. furi_assert(context);
  282. FuzzerViewAttack* view_attack = context;
  283. // if(event->key == InputKeyBack && event->type == InputTypeShort) {
  284. // view_attack->callback(FuzzerCustomEventViewAttackBack, view_attack->context);
  285. // return true;
  286. // } else if(event->key == InputKeyOk && event->type == InputTypeShort) {
  287. // view_attack->callback(FuzzerCustomEventViewAttackOk, view_attack->context);
  288. // return true;
  289. // } else
  290. // {
  291. with_view_model(
  292. view_attack->view,
  293. FuzzerViewAttackModel * model,
  294. {
  295. switch(model->attack_state) {
  296. case FuzzerAttackStateIdle:
  297. fuzzer_view_attack_input_idle(view_attack, event, model);
  298. break;
  299. case FuzzerAttackStateEnd:
  300. fuzzer_view_attack_input_end(view_attack, event, model);
  301. break;
  302. case FuzzerAttackStateRunning:
  303. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  304. view_attack->callback(FuzzerCustomEventViewAttackIdle, view_attack->context);
  305. } else if(event->key == InputKeyOk && event->type == InputTypeShort) {
  306. view_attack->callback(FuzzerCustomEventViewAttackIdle, view_attack->context);
  307. // view_attack->callback(FuzzerCustomEventViewAttackPause, view_attack->context);
  308. }
  309. break;
  310. case FuzzerAttackStatePause:
  311. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  312. view_attack->callback(FuzzerCustomEventViewAttackIdle, view_attack->context);
  313. }
  314. break;
  315. default:
  316. break;
  317. }
  318. },
  319. true);
  320. // }
  321. return true;
  322. }
  323. void fuzzer_view_attack_enter(void* context) {
  324. furi_assert(context);
  325. }
  326. void fuzzer_view_attack_exit(void* context) {
  327. furi_assert(context);
  328. FuzzerViewAttack* view_attack = context;
  329. with_view_model(
  330. view_attack->view, FuzzerViewAttackModel * model, { model->td_emt_cursor = false; }, true);
  331. }
  332. FuzzerViewAttack* fuzzer_view_attack_alloc() {
  333. if(fuzzer_proto_get_max_data_size() > UID_MAX_DISPLAYED_LEN) {
  334. furi_crash("Maximum of displayed bytes exceeded");
  335. }
  336. FuzzerViewAttack* view_attack = malloc(sizeof(FuzzerViewAttack));
  337. // View allocation and configuration
  338. view_attack->view = view_alloc();
  339. view_allocate_model(view_attack->view, ViewModelTypeLocking, sizeof(FuzzerViewAttackModel));
  340. view_set_context(view_attack->view, view_attack);
  341. view_set_draw_callback(view_attack->view, (ViewDrawCallback)fuzzer_view_attack_draw);
  342. view_set_input_callback(view_attack->view, fuzzer_view_attack_input);
  343. view_set_enter_callback(view_attack->view, fuzzer_view_attack_enter);
  344. view_set_exit_callback(view_attack->view, fuzzer_view_attack_exit);
  345. with_view_model(
  346. view_attack->view,
  347. FuzzerViewAttackModel * model,
  348. {
  349. model->time_delay = fuzzer_proto_get_def_idle_time();
  350. model->time_delay_min = 0; // model->time_delay;
  351. model->emu_time = fuzzer_proto_get_def_emu_time();
  352. model->emu_time_min = 2; // model->emu_time;
  353. model->uid_str = furi_string_alloc_set_str("Not_set");
  354. // malloc(ATTACK_SCENE_MAX_UID_LENGTH + 1);
  355. model->attack_state = FuzzerAttackStateOff;
  356. model->td_emt_cursor = false;
  357. // strcpy(model->uid_str, "Not_set");
  358. model->attack_name = "Not_set";
  359. model->protocol_name = "Not_set";
  360. },
  361. true);
  362. return view_attack;
  363. }
  364. void fuzzer_view_attack_free(FuzzerViewAttack* view_attack) {
  365. furi_assert(view_attack);
  366. with_view_model(
  367. view_attack->view,
  368. FuzzerViewAttackModel * model,
  369. { furi_string_free(model->uid_str); },
  370. true);
  371. view_free(view_attack->view);
  372. free(view_attack);
  373. }
  374. View* fuzzer_view_attack_get_view(FuzzerViewAttack* view_attack) {
  375. furi_assert(view_attack);
  376. return view_attack->view;
  377. }
  378. uint8_t fuzzer_view_attack_get_time_delay(FuzzerViewAttack* view) {
  379. furi_assert(view);
  380. uint8_t time_delay;
  381. with_view_model(
  382. view->view, FuzzerViewAttackModel * model, { time_delay = model->time_delay; }, false);
  383. return time_delay;
  384. }
  385. uint8_t fuzzer_view_attack_get_emu_time(FuzzerViewAttack* view) {
  386. furi_assert(view);
  387. uint8_t emu_time;
  388. with_view_model(
  389. view->view, FuzzerViewAttackModel * model, { emu_time = model->emu_time; }, false);
  390. return emu_time;
  391. }