dict_attack.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include "dict_attack.h"
  2. #include <gui/elements.h>
  3. typedef enum {
  4. DictAttackStateRead,
  5. DictAttackStateCardRemoved,
  6. } DictAttackState;
  7. struct DictAttack {
  8. View* view;
  9. DictAttackCallback callback;
  10. void* context;
  11. };
  12. typedef struct {
  13. DictAttackState state;
  14. MfClassicType type;
  15. FuriString* header;
  16. uint8_t sectors_total;
  17. uint8_t sectors_read;
  18. uint8_t sector_current;
  19. uint8_t keys_total;
  20. uint8_t keys_found;
  21. uint16_t dict_keys_total;
  22. uint16_t dict_keys_current;
  23. bool is_key_attack;
  24. uint8_t key_attack_current_sector;
  25. } DictAttackViewModel;
  26. static void dict_attack_draw_callback(Canvas* canvas, void* model) {
  27. DictAttackViewModel* m = model;
  28. if(m->state == DictAttackStateCardRemoved) {
  29. canvas_set_font(canvas, FontPrimary);
  30. canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Lost the tag!");
  31. canvas_set_font(canvas, FontSecondary);
  32. elements_multiline_text_aligned(
  33. canvas, 64, 23, AlignCenter, AlignTop, "Make sure the tag is\npositioned correctly.");
  34. } else if(m->state == DictAttackStateRead) {
  35. char draw_str[32] = {};
  36. canvas_set_font(canvas, FontSecondary);
  37. canvas_draw_str_aligned(
  38. canvas, 64, 0, AlignCenter, AlignTop, furi_string_get_cstr(m->header));
  39. if(m->is_key_attack) {
  40. snprintf(
  41. draw_str,
  42. sizeof(draw_str),
  43. "Reuse key check for sector: %d",
  44. m->key_attack_current_sector);
  45. } else {
  46. snprintf(draw_str, sizeof(draw_str), "Unlocking sector: %d", m->sector_current);
  47. }
  48. canvas_draw_str_aligned(canvas, 0, 10, AlignLeft, AlignTop, draw_str);
  49. float dict_progress = m->dict_keys_total == 0 ?
  50. 0 :
  51. (float)(m->dict_keys_current) / (float)(m->dict_keys_total);
  52. float progress = m->sectors_total == 0 ? 0 :
  53. ((float)(m->sector_current) + dict_progress) /
  54. (float)(m->sectors_total);
  55. if(progress > 1.0) {
  56. progress = 1.0;
  57. }
  58. if(m->dict_keys_current == 0) {
  59. // Cause when people see 0 they think it's broken
  60. snprintf(draw_str, sizeof(draw_str), "%d/%d", 1, m->dict_keys_total);
  61. } else {
  62. snprintf(
  63. draw_str, sizeof(draw_str), "%d/%d", m->dict_keys_current, m->dict_keys_total);
  64. }
  65. elements_progress_bar_with_text(canvas, 0, 20, 128, dict_progress, draw_str);
  66. canvas_set_font(canvas, FontSecondary);
  67. snprintf(draw_str, sizeof(draw_str), "Keys found: %d/%d", m->keys_found, m->keys_total);
  68. canvas_draw_str_aligned(canvas, 0, 33, AlignLeft, AlignTop, draw_str);
  69. snprintf(
  70. draw_str, sizeof(draw_str), "Sectors Read: %d/%d", m->sectors_read, m->sectors_total);
  71. canvas_draw_str_aligned(canvas, 0, 43, AlignLeft, AlignTop, draw_str);
  72. }
  73. elements_button_center(canvas, "Skip");
  74. }
  75. static bool dict_attack_input_callback(InputEvent* event, void* context) {
  76. DictAttack* dict_attack = context;
  77. bool consumed = false;
  78. if(event->type == InputTypeShort && event->key == InputKeyOk) {
  79. if(dict_attack->callback) {
  80. dict_attack->callback(dict_attack->context);
  81. }
  82. consumed = true;
  83. }
  84. return consumed;
  85. }
  86. DictAttack* dict_attack_alloc() {
  87. DictAttack* dict_attack = malloc(sizeof(DictAttack));
  88. dict_attack->view = view_alloc();
  89. view_allocate_model(dict_attack->view, ViewModelTypeLocking, sizeof(DictAttackViewModel));
  90. view_set_draw_callback(dict_attack->view, dict_attack_draw_callback);
  91. view_set_input_callback(dict_attack->view, dict_attack_input_callback);
  92. view_set_context(dict_attack->view, dict_attack);
  93. with_view_model(
  94. dict_attack->view,
  95. DictAttackViewModel * model,
  96. { model->header = furi_string_alloc(); },
  97. false);
  98. return dict_attack;
  99. }
  100. void dict_attack_free(DictAttack* dict_attack) {
  101. furi_assert(dict_attack);
  102. with_view_model(
  103. dict_attack->view,
  104. DictAttackViewModel * model,
  105. { furi_string_free(model->header); },
  106. false);
  107. view_free(dict_attack->view);
  108. free(dict_attack);
  109. }
  110. void dict_attack_reset(DictAttack* dict_attack) {
  111. furi_assert(dict_attack);
  112. with_view_model(
  113. dict_attack->view,
  114. DictAttackViewModel * model,
  115. {
  116. model->state = DictAttackStateRead;
  117. model->type = MfClassicType1k;
  118. model->sectors_total = 1;
  119. model->sectors_read = 0;
  120. model->sector_current = 0;
  121. model->keys_total = 0;
  122. model->keys_found = 0;
  123. model->dict_keys_total = 0;
  124. model->dict_keys_current = 0;
  125. model->is_key_attack = false;
  126. furi_string_reset(model->header);
  127. },
  128. false);
  129. }
  130. View* dict_attack_get_view(DictAttack* dict_attack) {
  131. furi_assert(dict_attack);
  132. return dict_attack->view;
  133. }
  134. void dict_attack_set_callback(DictAttack* dict_attack, DictAttackCallback callback, void* context) {
  135. furi_assert(dict_attack);
  136. furi_assert(callback);
  137. dict_attack->callback = callback;
  138. dict_attack->context = context;
  139. }
  140. void dict_attack_set_header(DictAttack* dict_attack, const char* header) {
  141. furi_assert(dict_attack);
  142. furi_assert(header);
  143. with_view_model(
  144. dict_attack->view,
  145. DictAttackViewModel * model,
  146. { furi_string_set(model->header, header); },
  147. true);
  148. }
  149. void dict_attack_set_card_detected(DictAttack* dict_attack) {
  150. furi_assert(dict_attack);
  151. with_view_model(
  152. dict_attack->view,
  153. DictAttackViewModel * model,
  154. {
  155. model->state = DictAttackStateRead;
  156. model->sectors_total = 1;
  157. model->keys_total = model->sectors_total;
  158. },
  159. true);
  160. }
  161. void dict_attack_set_card_removed(DictAttack* dict_attack) {
  162. furi_assert(dict_attack);
  163. with_view_model(
  164. dict_attack->view,
  165. DictAttackViewModel * model,
  166. { model->state = DictAttackStateCardRemoved; },
  167. true);
  168. }
  169. void dict_attack_set_sector_read(DictAttack* dict_attack, uint8_t sec_read) {
  170. furi_assert(dict_attack);
  171. with_view_model(
  172. dict_attack->view, DictAttackViewModel * model, { model->sectors_read = sec_read; }, true);
  173. }
  174. void dict_attack_set_keys_found(DictAttack* dict_attack, uint8_t keys_found) {
  175. furi_assert(dict_attack);
  176. with_view_model(
  177. dict_attack->view, DictAttackViewModel * model, { model->keys_found = keys_found; }, true);
  178. }
  179. void dict_attack_set_current_sector(DictAttack* dict_attack, uint8_t curr_sec) {
  180. furi_assert(dict_attack);
  181. with_view_model(
  182. dict_attack->view,
  183. DictAttackViewModel * model,
  184. {
  185. model->sector_current = curr_sec;
  186. model->dict_keys_current = 0;
  187. },
  188. true);
  189. }
  190. void dict_attack_inc_current_sector(DictAttack* dict_attack) {
  191. furi_assert(dict_attack);
  192. with_view_model(
  193. dict_attack->view,
  194. DictAttackViewModel * model,
  195. {
  196. if(model->sector_current < model->sectors_total) {
  197. model->sector_current++;
  198. model->dict_keys_current = 0;
  199. }
  200. },
  201. true);
  202. }
  203. void dict_attack_inc_keys_found(DictAttack* dict_attack) {
  204. furi_assert(dict_attack);
  205. with_view_model(
  206. dict_attack->view,
  207. DictAttackViewModel * model,
  208. {
  209. if(model->keys_found < model->keys_total) {
  210. model->keys_found++;
  211. }
  212. },
  213. true);
  214. }
  215. void dict_attack_set_total_dict_keys(DictAttack* dict_attack, uint16_t dict_keys_total) {
  216. furi_assert(dict_attack);
  217. with_view_model(
  218. dict_attack->view,
  219. DictAttackViewModel * model,
  220. { model->dict_keys_total = dict_keys_total; },
  221. true);
  222. }
  223. void dict_attack_inc_current_dict_key(DictAttack* dict_attack, uint16_t keys_tried) {
  224. furi_assert(dict_attack);
  225. with_view_model(
  226. dict_attack->view,
  227. DictAttackViewModel * model,
  228. {
  229. if(model->dict_keys_current + keys_tried < model->dict_keys_total) {
  230. model->dict_keys_current += keys_tried;
  231. }
  232. },
  233. true);
  234. }
  235. void dict_attack_set_key_attack(DictAttack* dict_attack, bool is_key_attack, uint8_t sector) {
  236. furi_assert(dict_attack);
  237. with_view_model(
  238. dict_attack->view,
  239. DictAttackViewModel * model,
  240. {
  241. model->is_key_attack = is_key_attack;
  242. model->key_attack_current_sector = sector;
  243. },
  244. true);
  245. }
  246. void dict_attack_inc_key_attack_current_sector(DictAttack* dict_attack) {
  247. furi_assert(dict_attack);
  248. with_view_model(
  249. dict_attack->view,
  250. DictAttackViewModel * model,
  251. {
  252. if(model->key_attack_current_sector < model->sectors_total) {
  253. model->key_attack_current_sector++;
  254. }
  255. },
  256. true);
  257. }