Pārlūkot izejas kodu

Merge flipper_blackhat from https://github.com/o7-machinehum/flipper-blackhat-app

Willy-JL 9 mēneši atpakaļ
vecāks
revīzija
6bf384bd1a

+ 30 - 0
flipper_blackhat/.github/workflows/push.yml

@@ -0,0 +1,30 @@
+name: Push
+
+on:
+  push:
+    branches: '**'
+
+jobs:
+  build:
+    runs-on: ubuntu-latest
+
+    steps:
+      - name: Checkout code
+        uses: actions/checkout@v2
+        with:
+          submodules: true
+          fetch-depth: 0
+
+      - name: Install dependencies
+        run: |
+            sudo pip install ufbt --break-system-packages
+
+      - name: Build
+        run: |
+            ufbt
+
+      - name: Upload Artifact
+        uses: actions/upload-artifact@v4
+        with:
+          name: push
+          path: dist/blackhat.fap

+ 28 - 0
flipper_blackhat/blackhat_app.c

@@ -31,6 +31,8 @@ BlackhatApp* blackhat_app_alloc()
 
     app->dialogs = furi_record_open(RECORD_DIALOGS);
 
+    memset(app->cmd, 0x00, sizeof(app->cmd));
+
     app->gui = furi_record_open(RECORD_GUI);
 
     app->view_dispatcher = view_dispatcher_alloc();
@@ -60,6 +62,14 @@ BlackhatApp* blackhat_app_alloc()
         variable_item_list_get_view(app->var_item_list)
     );
 
+    // Var item list for script
+    app->script_item_list = variable_item_list_alloc();
+    view_dispatcher_add_view(
+        app->view_dispatcher,
+        BlackhatAppViewScriptItemList,
+        variable_item_list_get_view(app->script_item_list)
+    );
+
     app->text_input = text_input_alloc();
     view_dispatcher_add_view(
         app->view_dispatcher,
@@ -72,11 +82,16 @@ BlackhatApp* blackhat_app_alloc()
     }
 
     app->text_box = text_box_alloc();
+
     view_dispatcher_add_view(
         app->view_dispatcher,
         BlackhatAppViewConsoleOutput,
         text_box_get_view(app->text_box)
     );
+
+    app->script_text = malloc(BLACKHAT_TEXT_BOX_STORE_SIZE);
+    app->script_text_ptr = 0;
+
     app->text_box_store = furi_string_alloc();
     furi_string_reserve(app->text_box_store, BLACKHAT_TEXT_BOX_STORE_SIZE);
 
@@ -89,11 +104,23 @@ void blackhat_app_free(BlackhatApp* app)
 {
     furi_assert(app);
 
+    for(int i = 0 ; i < app->num_scripts ; i++) {
+        if(app->cmd[i])
+            free(app->cmd[i]);
+    }
+
     // Views
     view_dispatcher_remove_view(
         app->view_dispatcher, BlackhatAppViewVarItemList
     );
+
+    view_dispatcher_remove_view(
+        app->view_dispatcher, BlackhatAppViewScriptItemList
+    );
+
+    variable_item_list_free(app->script_item_list);
     variable_item_list_free(app->var_item_list);
+
     view_dispatcher_remove_view(app->view_dispatcher, BlackhatAppViewTextInput);
     text_input_free(app->text_input);
     view_dispatcher_remove_view(
@@ -125,6 +152,7 @@ int32_t blackhat_app(void* p)
     expansion_disable(expansion);
 
     BlackhatApp* blackhat_app = blackhat_app_alloc();
+    blackhat_app->scanned = false;
 
     bool otg_was_enabled = furi_hal_power_is_otg_enabled();
     // turn off 5v, so it gets reset on startup

+ 20 - 1
flipper_blackhat/blackhat_app_i.h

@@ -1,6 +1,8 @@
 #pragma once
 
 #include <dialogs/dialogs.h>
+#include <dolphin/dolphin.h>
+#include <furi.h>
 #include <gui/gui.h>
 #include <gui/modules/loading.h>
 #include <gui/modules/text_box.h>
@@ -9,18 +11,24 @@
 #include <gui/scene_manager.h>
 #include <gui/view_dispatcher.h>
 #include <gui/view_stack.h>
+#include <input/input.h>
+#include <notification/notification.h>
+#include <notification/notification_messages.h>
+#include <stdio.h>
 
 #include "blackhat_app.h"
 #include "blackhat_custom_event.h"
 #include "blackhat_uart.h"
 #include "scenes/blackhat_scene.h"
 
-#define NUM_MENU_ITEMS (16)
+#define NUM_MENU_ITEMS (18)
 
 #define BLACKHAT_TEXT_BOX_STORE_SIZE (4096)
 #define UART_CH FuriHalSerialIdUsart
 
 #define SHELL_CMD "whoami"
+#define SCAN_CMD "bh script scan"
+#define RUN_CMD "bh scriptrun"
 #define WIFI_CON_CMD "bh wifi connect"
 #define SET_INET_SSID_CMD "bh set SSID"
 #define SET_INET_PWD_CMD "bh set PASS"
@@ -64,6 +72,15 @@ struct BlackhatApp {
     SceneManager* scene_manager;
 
     FuriString* text_box_store;
+
+    // For custom scripts
+    char* script_text;
+    size_t script_text_ptr;
+    int num_scripts;
+    char* cmd[64];
+    bool scanned;
+    VariableItemList* script_item_list;
+
     size_t text_box_store_strlen;
     TextBox* text_box;
 
@@ -79,10 +96,12 @@ struct BlackhatApp {
     char text_store[128];
     char text_input_ch[ENTER_NAME_LENGTH];
     bool text_input_req;
+    bool is_script_scan;
 };
 
 typedef enum {
     BlackhatAppViewVarItemList,
+    BlackhatAppViewScriptItemList,
     BlackhatAppViewConsoleOutput,
     BlackhatAppViewStartPortal,
     BlackhatAppViewTextInput,

+ 1 - 0
flipper_blackhat/scenes/blackhat_scene_config.h

@@ -1,3 +1,4 @@
 ADD_SCENE(blackhat, start, Start)
+ADD_SCENE(blackhat, scripts, Scripts)
 ADD_SCENE(blackhat, console_output, ConsoleOutput)
 ADD_SCENE(blackhat, rename, Rename)

+ 25 - 1
flipper_blackhat/scenes/blackhat_scene_console_output.c

@@ -15,10 +15,15 @@ void blackhat_console_output_handle_rx_data_cb(
             furi_string_size(app->text_box_store) + len;
     }
 
+    // We gotta parse the output
+    if (app->is_script_scan) {
+        memcpy(&app->script_text[app->script_text_ptr], buf, len);
+        app->script_text_ptr += len;
+    }
+
     // Null-terminate buf and append to text box store
     buf[len] = '\0';
     furi_string_cat_printf(app->text_box_store, "%s", buf);
-
     text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
 }
 
@@ -40,6 +45,23 @@ void blackhat_scene_console_output_on_enter(void* context)
         return;
     }
 
+    app->is_script_scan = false;
+    if (!strcmp(app->selected_tx_string, SCAN_CMD)) {
+        app->is_script_scan = true;
+        app->script_text_ptr = 0;
+        app->scanned = true;
+    }
+
+    if (!strcmp(app->selected_tx_string, RUN_CMD)) {
+        if (app->scanned) {
+            app->script_text_ptr++;
+            app->script_text[app->script_text_ptr] = 0x00;
+            scene_manager_next_scene(app->scene_manager, BlackhatSceneScripts);
+        }
+        return;
+    }
+
+    FURI_LOG_I("selected_tx_string", "%s", app->selected_tx_string);
     snprintf(
         app->text_store,
         sizeof(app->text_store),
@@ -51,9 +73,11 @@ void blackhat_scene_console_output_on_enter(void* context)
     FURI_LOG_I("tag/app name", "%s", app->text_store);
 
     text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
+
     scene_manager_set_scene_state(
         app->scene_manager, BlackhatSceneConsoleOutput, 0
     );
+
     view_dispatcher_switch_to_view(
         app->view_dispatcher, BlackhatAppViewConsoleOutput
     );

+ 95 - 0
flipper_blackhat/scenes/blackhat_scene_scripts.c

@@ -0,0 +1,95 @@
+#include "../blackhat_app_i.h"
+
+static bool console = false;
+
+static void blackhat_scene_script_list_enter_callback(
+    void* context, uint32_t index
+)
+{
+    furi_assert(context);
+    BlackhatApp* app = context;
+
+    console = true;
+    app->selected_tx_string = "bh script run";
+    app->selected_option_item_text = app->cmd[index + 1];
+    app->text_input_req = false;
+    app->selected_menu_index = index;
+
+    FURI_LOG_I("tag/app name", "%s", app->selected_tx_string);
+
+    scene_manager_search_and_switch_to_previous_scene(
+        app->scene_manager, BlackhatAppViewConsoleOutput
+    );
+}
+
+static void blackhat_scene_script_list_change_callback(VariableItem* item)
+{
+    UNUSED(item);
+}
+
+void blackhat_scene_scripts_on_enter(void* context)
+{
+    BlackhatApp* app = context;
+    VariableItemList* var_item_list = app->script_item_list;
+    size_t i = 0;
+    int start = 0;
+    app->num_scripts = 0;
+
+    console = false;
+
+    while (app->script_text[i++]) {
+        if (app->script_text[i] == '\n') {
+            i++;
+
+            if(app->cmd[app->num_scripts]) {
+                free(app->cmd[app->num_scripts]);
+            }
+
+            app->cmd[app->num_scripts] = malloc(i - start);
+
+            memcpy(
+                app->cmd[app->num_scripts], &app->script_text[start], i - start
+            );
+            start = i;
+            app->num_scripts++;
+        }
+    }
+
+    variable_item_list_set_enter_callback(
+        var_item_list, blackhat_scene_script_list_enter_callback, app
+    );
+
+    for (int i = 1; i < app->num_scripts; i++) {
+        variable_item_list_add(
+            var_item_list,
+            app->cmd[i],
+            1,
+            blackhat_scene_script_list_change_callback,
+            app
+        );
+    }
+
+    view_dispatcher_switch_to_view(
+        app->view_dispatcher, BlackhatAppViewScriptItemList
+    );
+}
+
+bool blackhat_scene_scripts_on_event(void* context, SceneManagerEvent event)
+{
+    UNUSED(context);
+    UNUSED(event);
+
+    return false;
+}
+
+void blackhat_scene_scripts_on_exit(void* context)
+{
+    BlackhatApp* app = context;
+    variable_item_list_reset(app->script_item_list);
+
+    if(!console) {
+        scene_manager_search_and_switch_to_previous_scene(
+            app->scene_manager, BlackhatSceneStart
+        );
+    }
+}

+ 7 - 38
flipper_blackhat/scenes/blackhat_scene_start.c

@@ -2,6 +2,8 @@
 
 BlackhatItem items[] = {
     {"Shell", {""}, 1, NULL, SHELL_CMD, false},
+    {"Scan for Scripts", {""}, 1, NULL, SCAN_CMD, false},
+    {"Run Script", {""}, 1, NULL, RUN_CMD, false},
     {"Connect WiFi", {""}, 1, NULL, WIFI_CON_CMD, FOCUS_CONSOLE_END},
     {"Set inet SSID", {""}, 1, NULL, SET_INET_SSID_CMD, true},
     {"Set inet Password", {""}, 1, NULL, SET_INET_PWD_CMD, true},
@@ -47,8 +49,9 @@ static void blackhat_scene_start_var_list_enter_callback(
     app->selected_menu_index = index;
 
     app->selected_option_item_text = item->selected_option;
-    view_dispatcher_send_custom_event(
-        app->view_dispatcher, BlackhatEventStartConsole
+
+    scene_manager_next_scene(
+        app->scene_manager, BlackhatAppViewConsoleOutput
     );
 }
 
@@ -102,11 +105,6 @@ void blackhat_scene_start_on_enter(void* context)
         );
     }
 
-    variable_item_list_set_selected_item(
-        var_item_list,
-        scene_manager_get_scene_state(app->scene_manager, BlackhatSceneStart)
-    );
-
     view_dispatcher_switch_to_view(
         app->view_dispatcher, BlackhatAppViewVarItemList
     );
@@ -115,37 +113,8 @@ void blackhat_scene_start_on_enter(void* context)
 bool blackhat_scene_start_on_event(void* context, SceneManagerEvent event)
 {
     UNUSED(context);
-    BlackhatApp* app = context;
-    bool consumed = false;
-
-    if (event.type == SceneManagerEventTypeCustom) {
-        if (event.event == BlackhatEventStartPortal) {
-            scene_manager_set_scene_state(
-                app->scene_manager, BlackhatSceneStart, app->selected_menu_index
-            );
-            scene_manager_next_scene(
-                app->scene_manager, BlackhatAppViewStartPortal
-            );
-        } else if (event.event == BlackhatEventStartKeyboard) {
-            scene_manager_set_scene_state(
-                app->scene_manager, BlackhatSceneStart, app->selected_menu_index
-            );
-        } else if (event.event == BlackhatEventStartConsole) {
-            scene_manager_set_scene_state(
-                app->scene_manager, BlackhatSceneStart, app->selected_menu_index
-            );
-            scene_manager_next_scene(
-                app->scene_manager, BlackhatAppViewConsoleOutput
-            );
-        }
-        consumed = true;
-    } else if (event.type == SceneManagerEventTypeTick) {
-        app->selected_menu_index =
-            variable_item_list_get_selected_item_index(app->var_item_list);
-        consumed = true;
-    }
-
-    return consumed;
+    UNUSED(event);
+    return false;
 }
 
 void blackhat_scene_start_on_exit(void* context)