wifi_marauder_scene_console_output.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "../wifi_marauder_app_i.h"
  2. #include "../wifi_marauder_flasher.h"
  3. char* _wifi_marauder_get_prefix_from_cmd(const char* command) {
  4. int end = strcspn(command, " ");
  5. char* prefix = (char*)malloc(sizeof(char) * (end + 1));
  6. strncpy(prefix, command, end);
  7. prefix[end] = '\0';
  8. return prefix;
  9. }
  10. bool _wifi_marauder_is_save_pcaps_enabled(WifiMarauderApp* app) {
  11. if(!app->ok_to_save_pcaps) {
  12. return false;
  13. }
  14. // If it is a script that contains a sniff function
  15. if(app->script != NULL) {
  16. return wifi_marauder_script_has_stage(app->script, WifiMarauderScriptStageTypeSniffRaw) ||
  17. wifi_marauder_script_has_stage(
  18. app->script, WifiMarauderScriptStageTypeSniffBeacon) ||
  19. wifi_marauder_script_has_stage(
  20. app->script, WifiMarauderScriptStageTypeSniffDeauth) ||
  21. wifi_marauder_script_has_stage(app->script, WifiMarauderScriptStageTypeSniffEsp) ||
  22. wifi_marauder_script_has_stage(
  23. app->script, WifiMarauderScriptStageTypeSniffPmkid) ||
  24. wifi_marauder_script_has_stage(app->script, WifiMarauderScriptStageTypeSniffPwn);
  25. }
  26. // If it is a sniff function
  27. return app->is_command && app->selected_tx_string &&
  28. strncmp("sniff", app->selected_tx_string, strlen("sniff")) == 0;
  29. }
  30. void wifi_marauder_console_output_handle_rx_data_cb(uint8_t* buf, size_t len, void* context) {
  31. furi_assert(context);
  32. WifiMarauderApp* app = context;
  33. if(app->is_writing_log) {
  34. app->has_saved_logs_this_session = true;
  35. storage_file_write(app->log_file, buf, len);
  36. }
  37. // If text box store gets too big, then truncate it
  38. app->text_box_store_strlen += len;
  39. if(app->text_box_store_strlen >= WIFI_MARAUDER_TEXT_BOX_STORE_SIZE - 1) {
  40. furi_string_right(app->text_box_store, app->text_box_store_strlen / 2);
  41. app->text_box_store_strlen = furi_string_size(app->text_box_store) + len;
  42. }
  43. // Null-terminate buf and append to text box store
  44. buf[len] = '\0';
  45. furi_string_cat_printf(app->text_box_store, "%s", buf);
  46. view_dispatcher_send_custom_event(app->view_dispatcher, WifiMarauderEventRefreshConsoleOutput);
  47. }
  48. void wifi_marauder_console_output_handle_rx_packets_cb(uint8_t* buf, size_t len, void* context) {
  49. furi_assert(context);
  50. WifiMarauderApp* app = context;
  51. if(app->is_writing_pcap) {
  52. storage_file_write(app->capture_file, buf, len);
  53. }
  54. }
  55. void wifi_marauder_scene_console_output_on_enter(void* context) {
  56. WifiMarauderApp* app = context;
  57. // Reset text box and set font
  58. TextBox* text_box = app->text_box;
  59. text_box_reset(text_box);
  60. text_box_set_font(text_box, TextBoxFontText);
  61. // Set focus on start or end
  62. if(app->focus_console_start) {
  63. text_box_set_focus(text_box, TextBoxFocusStart);
  64. } else {
  65. text_box_set_focus(text_box, TextBoxFocusEnd);
  66. }
  67. // Set command-related messages
  68. if(app->is_command) {
  69. furi_string_reset(app->text_box_store);
  70. app->text_box_store_strlen = 0;
  71. // Help message
  72. if(0 == strncmp("help", app->selected_tx_string, strlen("help"))) {
  73. const char* help_msg = "Marauder companion " WIFI_MARAUDER_APP_VERSION "\n";
  74. furi_string_cat_str(app->text_box_store, help_msg);
  75. app->text_box_store_strlen += strlen(help_msg);
  76. }
  77. // Stopscan message
  78. if(app->show_stopscan_tip) {
  79. const char* help_msg = "Press BACK to send stopscan\n";
  80. furi_string_cat_str(app->text_box_store, help_msg);
  81. app->text_box_store_strlen += strlen(help_msg);
  82. }
  83. }
  84. // Set starting text
  85. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  86. // Set scene state and switch view
  87. scene_manager_set_scene_state(app->scene_manager, WifiMarauderSceneConsoleOutput, 0);
  88. view_dispatcher_switch_to_view(app->view_dispatcher, WifiMarauderAppViewConsoleOutput);
  89. // Register callbacks to receive data
  90. // setup callback for general log rx thread
  91. if(app->flash_mode) {
  92. wifi_marauder_uart_set_handle_rx_data_cb(
  93. app->uart,
  94. wifi_marauder_flash_handle_rx_data_cb); // setup callback for general log rx thread
  95. } else {
  96. wifi_marauder_uart_set_handle_rx_data_cb(
  97. app->uart,
  98. wifi_marauder_console_output_handle_rx_data_cb); // setup callback for general log rx thread
  99. }
  100. wifi_marauder_uart_set_handle_rx_data_cb(
  101. app->lp_uart,
  102. wifi_marauder_console_output_handle_rx_packets_cb); // setup callback for packets rx thread
  103. if(app->flash_mode) {
  104. wifi_marauder_flash_start_thread(app);
  105. }
  106. // Get ready to send command
  107. if((app->is_command && app->selected_tx_string) || app->script) {
  108. const char* prefix =
  109. strlen(app->selected_tx_string) > 0 ?
  110. _wifi_marauder_get_prefix_from_cmd(app->selected_tx_string) : // Function name
  111. app->script->name; // Script name
  112. // Create files *before* sending command
  113. // (it takes time to iterate through the directory)
  114. if(app->ok_to_save_logs) {
  115. strcpy(
  116. app->log_file_path,
  117. sequential_file_resolve_path(
  118. app->storage, MARAUDER_APP_FOLDER_LOGS, prefix, "log"));
  119. if(app->log_file_path != NULL) {
  120. if(storage_file_open(
  121. app->log_file, app->log_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  122. app->is_writing_log = true;
  123. } else {
  124. dialog_message_show_storage_error(app->dialogs, "Cannot open log file");
  125. }
  126. } else {
  127. dialog_message_show_storage_error(app->dialogs, "Cannot resolve log path");
  128. }
  129. }
  130. // If it is a sniff function or script, open the pcap file for recording
  131. if(_wifi_marauder_is_save_pcaps_enabled(app)) {
  132. if(sequential_file_open(
  133. app->storage, app->capture_file, MARAUDER_APP_FOLDER_PCAPS, prefix, "pcap")) {
  134. app->is_writing_pcap = true;
  135. } else {
  136. dialog_message_show_storage_error(app->dialogs, "Cannot open pcap file");
  137. }
  138. }
  139. // Send command with newline '\n'
  140. if(app->selected_tx_string) {
  141. wifi_marauder_uart_tx(
  142. (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
  143. wifi_marauder_uart_tx((uint8_t*)("\n"), 1);
  144. }
  145. // Run the script if the file with the script has been opened
  146. if(app->script != NULL) {
  147. app->script_worker = wifi_marauder_script_worker_alloc();
  148. wifi_marauder_script_worker_start(app->script_worker, app->script);
  149. }
  150. }
  151. }
  152. bool wifi_marauder_scene_console_output_on_event(void* context, SceneManagerEvent event) {
  153. WifiMarauderApp* app = context;
  154. bool consumed = false;
  155. if(event.type == SceneManagerEventTypeCustom) {
  156. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  157. consumed = true;
  158. } else if(event.type == SceneManagerEventTypeTick) {
  159. consumed = true;
  160. }
  161. return consumed;
  162. }
  163. void wifi_marauder_scene_console_output_on_exit(void* context) {
  164. WifiMarauderApp* app = context;
  165. // Automatically stop the scan when exiting view
  166. if(app->is_command) {
  167. wifi_marauder_uart_tx((uint8_t*)("stopscan\n"), strlen("stopscan\n"));
  168. furi_delay_ms(50);
  169. }
  170. if(app->flash_mode) {
  171. wifi_marauder_flash_stop_thread(app);
  172. }
  173. // Unregister rx callback
  174. wifi_marauder_uart_set_handle_rx_data_cb(app->uart, NULL);
  175. wifi_marauder_uart_set_handle_rx_data_cb(app->lp_uart, NULL);
  176. wifi_marauder_script_worker_free(app->script_worker);
  177. app->script_worker = NULL;
  178. app->is_writing_pcap = false;
  179. if(app->capture_file && storage_file_is_open(app->capture_file)) {
  180. storage_file_close(app->capture_file);
  181. }
  182. app->is_writing_log = false;
  183. if(app->log_file && storage_file_is_open(app->log_file)) {
  184. storage_file_close(app->log_file);
  185. }
  186. }