فهرست منبع

Implement saving logs, rest of settings prompts

0xchocolate 2 سال پیش
والد
کامیت
3cf2ec7863

+ 33 - 5
applications/external/wifi_marauder_companion/scenes/wifi_marauder_scene_console_output.c

@@ -4,6 +4,17 @@ void wifi_marauder_console_output_handle_rx_data_cb(uint8_t* buf, size_t len, vo
     furi_assert(context);
     WifiMarauderApp* app = context;
 
+    if(app->ok_to_save_logs) {
+        if(!app->is_writing_log) {
+            app->is_writing_log = true;
+            if(!app->log_file || !storage_file_is_open(app->log_file)) {
+                wifi_marauder_create_log_file(app);
+            }
+        }
+
+        storage_file_write(app->log_file, buf, len);
+    }
+
     // If text box store gets too big, then truncate it
     app->text_box_store_strlen += len;
     if(app->text_box_store_strlen >= WIFI_MARAUDER_TEXT_BOX_STORE_SIZE - 1) {
@@ -21,15 +32,20 @@ void wifi_marauder_console_output_handle_rx_packets_cb(uint8_t* buf, size_t len,
     furi_assert(context);
     WifiMarauderApp* app = context;
 
+    if(!app->ok_to_save_pcaps) {
+        // user has declined this feature
+        return;
+    }
+
     // If it is a sniff function, open the pcap file for recording
-    if(strncmp("sniff", app->selected_tx_string, strlen("sniff")) == 0 && !app->is_writing) {
-        app->is_writing = true;
+    if(strncmp("sniff", app->selected_tx_string, strlen("sniff")) == 0 && !app->is_writing_pcap) {
+        app->is_writing_pcap = true;
         if(!app->capture_file || !storage_file_is_open(app->capture_file)) {
             wifi_marauder_create_pcap_file(app);
         }
     }
 
-    if(app->is_writing) {
+    if(app->is_writing_pcap) {
         storage_file_write(app->capture_file, buf, len);
     }
 }
@@ -64,7 +80,14 @@ void wifi_marauder_scene_console_output_on_enter(void* context) {
     // Set starting text - for "View Log", this will just be what was already in the text box store
     text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
     if(furi_string_empty(app->text_box_store)) {
-        text_box_set_text(app->text_box, "The log is empty! :(\nTry sending a command?");
+        char help_msg[256];
+        snprintf(
+            help_msg,
+            sizeof(help_msg),
+            "The log is empty! :(\nTry sending a command?\n\nSaving pcaps to flipper sdcard: %s\nSaving logs to flipper sdcard: %s",
+            app->ok_to_save_pcaps ? "ON" : "OFF",
+            app->ok_to_save_logs ? "ON" : "OFF");
+        text_box_set_text(app->text_box, help_msg);
     }
 
     scene_manager_set_scene_state(app->scene_manager, WifiMarauderSceneConsoleOutput, 0);
@@ -113,8 +136,13 @@ void wifi_marauder_scene_console_output_on_exit(void* context) {
         wifi_marauder_uart_tx((uint8_t*)("stopscan\n"), strlen("stopscan\n"));
     }
 
-    app->is_writing = false;
+    app->is_writing_pcap = false;
     if(app->capture_file && storage_file_is_open(app->capture_file)) {
         storage_file_close(app->capture_file);
     }
+
+    app->is_writing_log = false;
+    if(app->log_file && storage_file_is_open(app->log_file)) {
+        storage_file_close(app->log_file);
+    }
 }

+ 92 - 14
applications/external/wifi_marauder_companion/scenes/wifi_marauder_scene_settings_init.c

@@ -1,5 +1,11 @@
 #include "../wifi_marauder_app_i.h"
 
+const char* Y = "Y";
+const char* N = "N";
+
+#define PROMPT_PCAPS 0
+#define PROMPT_LOGS 1
+
 void wifi_marauder_scene_settings_init_widget_callback(
     GuiButtonType result,
     InputType type,
@@ -10,24 +16,44 @@ void wifi_marauder_scene_settings_init_widget_callback(
     }
 }
 
-void wifi_marauder_scene_settings_init_on_enter(void* context) {
-    WifiMarauderApp* app = context;
+void wifi_marauder_scene_settings_init_setup_widget(WifiMarauderApp* app) {
     Widget* widget = app->widget;
 
+    widget_reset(widget);
+
     widget_add_button_element(
         widget, GuiButtonTypeLeft, "No", wifi_marauder_scene_settings_init_widget_callback, app);
     widget_add_button_element(
         widget, GuiButtonTypeRight, "Yes", wifi_marauder_scene_settings_init_widget_callback, app);
 
-    widget_add_string_element(app->widget, 0, 0, AlignLeft, AlignTop, FontPrimary, "Save pcaps?");
-    widget_add_text_scroll_element(
-        app->widget,
-        0,
-        12,
-        128,
-        38,
-        "With compatible marauder\nfirmware, you can choose to\nsave captures (pcaps) to the\nflipper sd card here:\n/" MARAUDER_APP_FOLDER_USER
-        "\n\nYou can change this setting in the app at any time. Would\nyou like to enable this feature now?");
+    if(app->which_prompt == PROMPT_PCAPS) {
+        widget_add_string_element(widget, 0, 0, AlignLeft, AlignTop, FontPrimary, "Save pcaps?");
+        widget_add_text_scroll_element(
+            widget,
+            0,
+            12,
+            128,
+            38,
+            "With compatible marauder\nfirmware, you can choose to\nsave captures (pcaps) to the\nflipper sd card here:\n/" MARAUDER_APP_FOLDER_USER
+            "\n\nYou can change this setting in the app at any time. Would\nyou like to enable this feature now?");
+    } else {
+        widget_add_string_element(widget, 0, 0, AlignLeft, AlignTop, FontPrimary, "Save logs?");
+        widget_add_text_scroll_element(
+            widget,
+            0,
+            12,
+            128,
+            38,
+            "This app supports saving text\nlogs of console output to the\nflipper sd card here:\n/" MARAUDER_APP_FOLDER_USER
+            "\n\nYou can change this setting in the app at any time. Would\nyou like to enable this feature now?");
+    }
+}
+
+void wifi_marauder_scene_settings_init_on_enter(void* context) {
+    WifiMarauderApp* app = context;
+
+    app->which_prompt = PROMPT_PCAPS;
+    wifi_marauder_scene_settings_init_setup_widget(app);
 
     view_dispatcher_switch_to_view(app->view_dispatcher, WifiMarauderAppViewWidget);
 }
@@ -37,9 +63,55 @@ bool wifi_marauder_scene_settings_init_on_event(void* context, SceneManagerEvent
     bool consumed = false;
 
     if(event.type == SceneManagerEventTypeCustom) {
-        // go back to start scene (main menu)
-        app->need_to_prompt_settings_init = false;
-        scene_manager_previous_scene(app->scene_manager);
+        // get which button press: "Yes" or "No"
+        if(event.event == GuiButtonTypeRight) {
+            // Yes
+            if(app->which_prompt == PROMPT_PCAPS) {
+                app->ok_to_save_pcaps = true;
+            } else {
+                app->ok_to_save_logs = true;
+            }
+        } else if(event.event == GuiButtonTypeLeft) {
+            // No
+            if(app->which_prompt == PROMPT_PCAPS) {
+                app->ok_to_save_pcaps = false;
+            } else {
+                app->ok_to_save_logs = false;
+            }
+        }
+
+        // save setting to file, load next widget or scene
+        if(app->which_prompt == PROMPT_PCAPS) {
+            if(storage_file_open(
+                   app->save_pcap_setting_file,
+                   SAVE_PCAP_SETTING_FILEPATH,
+                   FSAM_WRITE,
+                   FSOM_CREATE_ALWAYS)) {
+                const char* ok = app->ok_to_save_pcaps ? Y : N;
+                storage_file_write(app->save_pcap_setting_file, ok, sizeof(ok));
+            } else {
+                dialog_message_show_storage_error(app->dialogs, "Cannot save settings");
+            }
+            storage_file_close(app->save_pcap_setting_file);
+            // same scene, different-looking widget
+            app->which_prompt = PROMPT_LOGS;
+            wifi_marauder_scene_settings_init_setup_widget(app);
+        } else {
+            if(storage_file_open(
+                   app->save_logs_setting_file,
+                   SAVE_LOGS_SETTING_FILEPATH,
+                   FSAM_WRITE,
+                   FSOM_CREATE_ALWAYS)) {
+                const char* ok = app->ok_to_save_logs ? Y : N;
+                storage_file_write(app->save_logs_setting_file, ok, sizeof(ok));
+            } else {
+                dialog_message_show_storage_error(app->dialogs, "Cannot save settings");
+            }
+            storage_file_close(app->save_logs_setting_file);
+            // go back to start scene (main menu)
+            app->need_to_prompt_settings_init = false;
+            scene_manager_previous_scene(app->scene_manager);
+        }
         consumed = true;
     }
 
@@ -49,4 +121,10 @@ bool wifi_marauder_scene_settings_init_on_event(void* context, SceneManagerEvent
 void wifi_marauder_scene_settings_init_on_exit(void* context) {
     WifiMarauderApp* app = context;
     widget_reset(app->widget);
+    if(storage_file_is_open(app->save_pcap_setting_file)) {
+        storage_file_close(app->save_pcap_setting_file);
+    }
+    if(storage_file_is_open(app->save_logs_setting_file)) {
+        storage_file_close(app->save_logs_setting_file);
+    }
 }

+ 2 - 2
applications/external/wifi_marauder_companion/scenes/wifi_marauder_scene_start.c

@@ -127,13 +127,13 @@ const WifiMarauderItem items[NUM_MENU_ITEMS] = {
     {"Update", {"ota", "sd"}, 2, {"update -w", "update -s"}, NO_ARGS, FOCUS_CONSOLE_END, NO_TIP},
     {"Reboot", {""}, 1, {"reboot"}, NO_ARGS, FOCUS_CONSOLE_END, NO_TIP},
     {"Help", {""}, 1, {"help"}, NO_ARGS, FOCUS_CONSOLE_START, SHOW_STOPSCAN_TIP},
-    {"Save to flipper sdcard",
+    {"Save to flipper sdcard", // keep as last entry or change logic in callback below
      {""},
      1,
      {""},
      NO_ARGS,
      FOCUS_CONSOLE_START,
-     NO_TIP}, // keep at bottom or change logic below
+     NO_TIP},
 };
 
 static void wifi_marauder_scene_start_var_list_enter_callback(void* context, uint32_t index) {

+ 27 - 0
applications/external/wifi_marauder_companion/wifi_marauder_app.c

@@ -28,6 +28,7 @@ WifiMarauderApp* wifi_marauder_app_alloc() {
     app->dialogs = furi_record_open(RECORD_DIALOGS);
     app->storage = furi_record_open(RECORD_STORAGE);
     app->capture_file = storage_file_alloc(app->storage);
+    app->log_file = storage_file_alloc(app->storage);
     app->save_pcap_setting_file = storage_file_alloc(app->storage);
     app->save_logs_setting_file = storage_file_alloc(app->storage);
 
@@ -89,6 +90,30 @@ void wifi_marauder_make_app_folder(WifiMarauderApp* app) {
     }
 }
 
+void wifi_marauder_load_settings(WifiMarauderApp* app) {
+    if(storage_file_open(
+           app->save_pcap_setting_file,
+           SAVE_PCAP_SETTING_FILEPATH,
+           FSAM_READ,
+           FSOM_OPEN_EXISTING)) {
+        char ok[1];
+        storage_file_read(app->save_pcap_setting_file, ok, sizeof(ok));
+        app->ok_to_save_pcaps = ok[0] == 'Y';
+    }
+    storage_file_close(app->save_pcap_setting_file);
+
+    if(storage_file_open(
+           app->save_logs_setting_file,
+           SAVE_LOGS_SETTING_FILEPATH,
+           FSAM_READ,
+           FSOM_OPEN_EXISTING)) {
+        char ok[1];
+        storage_file_read(app->save_logs_setting_file, ok, sizeof(ok));
+        app->ok_to_save_logs = ok[0] == 'Y';
+    }
+    storage_file_close(app->save_logs_setting_file);
+}
+
 void wifi_marauder_app_free(WifiMarauderApp* app) {
     furi_assert(app);
 
@@ -102,6 +127,7 @@ void wifi_marauder_app_free(WifiMarauderApp* app) {
     furi_string_free(app->text_box_store);
     text_input_free(app->text_input);
     storage_file_free(app->capture_file);
+    storage_file_free(app->log_file);
     storage_file_free(app->save_pcap_setting_file);
     storage_file_free(app->save_logs_setting_file);
 
@@ -125,6 +151,7 @@ int32_t wifi_marauder_app(void* p) {
     WifiMarauderApp* wifi_marauder_app = wifi_marauder_app_alloc();
 
     wifi_marauder_make_app_folder(wifi_marauder_app);
+    wifi_marauder_load_settings(wifi_marauder_app);
 
     wifi_marauder_app->uart = wifi_marauder_usart_init(wifi_marauder_app);
     wifi_marauder_app->lp_uart = wifi_marauder_lp_uart_init(wifi_marauder_app);

+ 6 - 1
applications/external/wifi_marauder_companion/wifi_marauder_app_i.h

@@ -41,9 +41,13 @@ struct WifiMarauderApp {
     TextInput* text_input;
     Storage* storage;
     File* capture_file;
+    File* log_file;
     File* save_pcap_setting_file;
     File* save_logs_setting_file;
     bool need_to_prompt_settings_init;
+    int which_prompt;
+    bool ok_to_save_pcaps;
+    bool ok_to_save_logs;
     DialogsApp* dialogs;
 
     VariableItemList* var_item_list;
@@ -58,7 +62,8 @@ struct WifiMarauderApp {
     bool is_custom_tx_string;
     bool focus_console_start;
     bool show_stopscan_tip;
-    bool is_writing;
+    bool is_writing_pcap;
+    bool is_writing_log;
 
     // For input source and destination MAC in targeted deauth attack
     int special_case_input_step;

+ 26 - 2
applications/external/wifi_marauder_companion/wifi_marauder_pcap.c

@@ -1,7 +1,7 @@
 #include "wifi_marauder_app_i.h"
 #include "wifi_marauder_pcap.h"
 
-void wifi_marauder_get_prefix_from_cmd(char* dest, const char* command) {
+void wifi_marauder_get_prefix_from_sniff_cmd(char* dest, const char* command) {
     int start, end, delta;
     start = strlen("sniff");
     end = strcspn(command, " ");
@@ -10,10 +10,17 @@ void wifi_marauder_get_prefix_from_cmd(char* dest, const char* command) {
     dest[delta] = '\0';
 }
 
+void wifi_marauder_get_prefix_from_cmd(char* dest, const char* command) {
+    int end;
+    end = strcspn(command, " ");
+    strncpy(dest, command, end);
+    dest[end] = '\0';
+}
+
 void wifi_marauder_create_pcap_file(WifiMarauderApp* app) {
     char prefix[10];
     char capture_file_path[100];
-    wifi_marauder_get_prefix_from_cmd(prefix, app->selected_tx_string);
+    wifi_marauder_get_prefix_from_sniff_cmd(prefix, app->selected_tx_string);
 
     int i = 0;
     do {
@@ -30,4 +37,21 @@ void wifi_marauder_create_pcap_file(WifiMarauderApp* app) {
     if(!storage_file_open(app->capture_file, capture_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
         dialog_message_show_storage_error(app->dialogs, "Cannot open pcap file");
     }
+}
+
+void wifi_marauder_create_log_file(WifiMarauderApp* app) {
+    char prefix[10];
+    char log_file_path[100];
+    wifi_marauder_get_prefix_from_cmd(prefix, app->selected_tx_string);
+
+    int i = 0;
+    do {
+        snprintf(
+            log_file_path, sizeof(log_file_path), "%s/%s_%d.log", MARAUDER_APP_FOLDER, prefix, i);
+        i++;
+    } while(storage_file_exists(app->storage, log_file_path));
+
+    if(!storage_file_open(app->log_file, log_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
+        dialog_message_show_storage_error(app->dialogs, "Cannot open log file");
+    }
 }

+ 10 - 1
applications/external/wifi_marauder_companion/wifi_marauder_pcap.h

@@ -8,4 +8,13 @@
  * 
  * @param app Application context
  */
-void wifi_marauder_create_pcap_file(WifiMarauderApp* app);
+void wifi_marauder_create_pcap_file(WifiMarauderApp* app);
+
+/**
+ * Creates a log file to store text from console output.
+ * The file name will have a prefix according to the command being performed by the application (Eg: scanap_0.log)
+ *
+ * @param app Application context
+ */
+// same as wifi_marauder_create_pcap_file, but for log files (to save console text output)
+void wifi_marauder_create_log_file(WifiMarauderApp* app);