A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol
C arduinobluetoothcan-busdebuggingeepromesp32flipperzerogpiohardware-hackingi2cjtagopenocdprotocolspwmradiorfidspisubghzuartwifi
add ssh client for wifi mode
Geo2a5063cf1fcb
diff --git a/platformio.ini b/platformio.iniindex 50a46950..b7b0561b 100644--- a/platformio.ini+++ b/platformio.ini@@ -21,6 +21,7 @@ lib_deps =esp32pinghideakitai/ESP32SPISlave@^0.6.8gilman88/XModem@^1.0.3+ ewpa/LibSSH-ESP32@^5.6.0build_flags =-D CONFIG_TINYUSB_ENABLED=1-D CONFIG_TINYUSB_CDC_ENABLED=1@@ -95,6 +96,7 @@ lib_deps =esp32pinghideakitai/ESP32SPISlave@^0.6.8gilman88/XModem@^1.0.3+ ewpa/LibSSH-ESP32@^5.6.0build_flags =-DDEVICE_M5STICKdiff --git a/src/Controllers/UtilityController.cpp b/src/Controllers/UtilityController.cppindex a3e768f4..4f040546 100644--- a/src/Controllers/UtilityController.cpp+++ b/src/Controllers/UtilityController.cpp@@ -379,6 +379,7 @@ void UtilityController::handleHelp() {terminalView.println(" status - Show Wi-Fi status");terminalView.println(" disconnect - Disconnect from Wi-Fi");terminalView.println(" ap <ssid> <password> - Set access point");+ terminalView.println(" ssh <h> <u> <p> [p] - Open SSH session");terminalView.println(" webui - Show the web UI IP");terminalView.println(" reset - Reset interface");diff --git a/src/Controllers/WifiController.cpp b/src/Controllers/WifiController.cppindex 6657bb7e..15853b36 100644--- a/src/Controllers/WifiController.cpp+++ b/src/Controllers/WifiController.cpp@@ -3,8 +3,8 @@/*Constructor*/-WifiController::WifiController(ITerminalView& terminalView, IInput& terminalInput, WifiService& wifiService, NvsService& nvsService, ArgTransformer& argTransformer)- : terminalView(terminalView), terminalInput(terminalInput), wifiService(wifiService), nvsService(nvsService), argTransformer(argTransformer) {}+WifiController::WifiController(ITerminalView& terminalView, IInput& terminalInput, IInput& deviceInput, WifiService& wifiService, SshService& sshService, NvsService& nvsService, ArgTransformer& argTransformer)+ : terminalView(terminalView), terminalInput(terminalInput), deviceInput(deviceInput), wifiService(wifiService), sshService(sshService), nvsService(nvsService), argTransformer(argTransformer) {}/*Entry point for command@@ -30,6 +30,8 @@ void WifiController::handleCommand(const TerminalCommand& cmd) {handleSniff(cmd);} else if (root == "webui") {handleWebUi(cmd);+ } else if (root == "ssh") {+ handleSsh(cmd);} else if (root == "reset") {handleReset();} else {@@ -275,6 +277,66 @@ void WifiController::handleWebUi(const TerminalCommand&) {}}+/*+SSH+*/+void WifiController::handleSsh(const TerminalCommand& cmd) {+ // Check args+ auto args = argTransformer.splitArgs(cmd.getArgs());+ if (cmd.getSubcommand().empty() || args.size() < 2) {+ terminalView.println("Usage: ssh <host> <user> <password> [port]");+ return;+ }++ // Check port+ int port = 22;+ if (args.size() == 3) {+ if (argTransformer.isValidNumber(args[2])) {+ port = argTransformer.parseHexOrDec16(args[2]);+ }+ }++ std::string host = cmd.getSubcommand();+ std::string user = args[0];+ std::string pass = args[1];++ // Connect, start the ssh task+ terminalView.println("SSH: Connecting to " + host + " as " + user + " with port " + std::to_string(port) + "...");+ sshService.startTask(host, user, pass, false, port);++ // Wait 5sec for connection success+ unsigned long start = millis();+ while (!sshService.isConnected() && millis() - start < 5000) {+ delay(500);+ }++ // Can't connect+ if (!sshService.isConnected()) {+ terminalView.println("\r\nSSH: Connection failed.");+ sshService.close();+ return;+ }++ // Connected, start the bridge loop+ terminalView.println("SSH: Connected. Shell started... Press [ANY ESP32 KEY] to stop.\n");+ while (true) {+ char terminalKey = terminalInput.readChar();+ if (terminalKey != KEY_NONE) sshService.writeChar(terminalKey);++ char deviceKey = deviceInput.readChar();+ if (deviceKey != KEY_NONE) break;++ std::string output = sshService.readOutputNonBlocking();+ if (!output.empty()) terminalView.print(output);++ delay(10);+ }++ // Close SSH+ sshService.close();+ terminalView.println("\r\n\nSSH: Session closed.");+}+/*Config*/@@ -299,10 +361,14 @@ void WifiController::handleHelp() {terminalView.println(" status");terminalView.println(" disconnect");terminalView.println(" ap <ssid> <password>");+ terminalView.println(" ssh <host> <username> <password> [port]");terminalView.println(" webui");terminalView.println(" reset");}+/*+Ensure Configuration+*/void WifiController::ensureConfigured() {if (!configured) {handleConfig();diff --git a/src/Controllers/WifiController.h b/src/Controllers/WifiController.hindex c60d7fd2..931108de 100644--- a/src/Controllers/WifiController.h+++ b/src/Controllers/WifiController.h@@ -4,6 +4,7 @@#include <Interfaces/IInput.h>#include <Services/WifiService.h>#include <Services/NvsService.h>+#include <Services/SshService.h>#include <Transformers/ArgTransformer.h>#include <Models/TerminalCommand.h>#include <States/GlobalState.h>@@ -12,7 +13,15 @@class WifiController {public:// Constructor- WifiController(ITerminalView& terminalView, IInput& terminalInput, WifiService& wifiService, NvsService& nvsService, ArgTransformer& argTransformer);+ WifiController(+ ITerminalView& terminalView,+ IInput& terminalInput,+ IInput& deviceInput,+ WifiService& wifiService,+ SshService& sshService,+ NvsService& nvsService,+ ArgTransformer& argTransformer+ );// Entry point for Wi-Fi commandvoid handleCommand(const TerminalCommand& cmd);@@ -23,8 +32,10 @@ public:private:ITerminalView& terminalView;IInput& terminalInput;+ IInput& deviceInput;WifiService& wifiService;NvsService& nvsService;+ SshService& sshService;ArgTransformer& argTransformer;GlobalState& state = GlobalState::getInstance();bool configured = false;@@ -57,6 +68,9 @@ private:// Spoof Mac addrvoid handleSpoof(const TerminalCommand& cmd);+ // Connect to SSH host+ void handleSsh(const TerminalCommand& cmd);+// Reset Wi-Fi configurationvoid handleReset();diff --git a/src/Providers/DependencyProvider.cpp b/src/Providers/DependencyProvider.cppindex 251b1678..a05c53e5 100644--- a/src/Providers/DependencyProvider.cpp+++ b/src/Providers/DependencyProvider.cpp@@ -22,6 +22,7 @@ DependencyProvider::DependencyProvider(ITerminalView& terminalView, IDeviceView&bluetoothService(),wifiService(),i2sService(),+ sshService(),commandTransformer(),instructionTransformer(),argTransformer(),@@ -45,7 +46,7 @@ DependencyProvider::DependencyProvider(ITerminalView& terminalView, IDeviceView&ledController(terminalView, terminalInput, ledService, argTransformer, userInputManager),bluetoothController(terminalView, terminalInput, bluetoothService, argTransformer),i2sController(terminalView, terminalInput, i2sService, argTransformer, userInputManager),- wifiController(terminalView, terminalInput, wifiService, nvsService, argTransformer)+ wifiController(terminalView, terminalInput, deviceInput, wifiService, sshService, nvsService, argTransformer){}// Accessors for core components@@ -71,6 +72,7 @@ PinService& DependencyProvider::getPinService() { return pinService; }WifiService& DependencyProvider::getWifiService() { return wifiService; }BluetoothService& DependencyProvider::getBluetoothService() { return bluetoothService; }I2sService& DependencyProvider::getI2sService() { return i2sService; }+SshService& DependencyProvider::getSshService() { return sshService; }// ControllersUartController& DependencyProvider::getUartController() { return uartController; }diff --git a/src/Providers/DependencyProvider.h b/src/Providers/DependencyProvider.hindex c4bf65d2..02a788d3 100644--- a/src/Providers/DependencyProvider.h+++ b/src/Providers/DependencyProvider.h@@ -25,6 +25,7 @@ and injecting shared instances of core components#include "Services/BluetoothService.h"#include "Services/WifiService.h"#include "Services/I2sService.h"+#include "Services/SshService.h"#include "Controllers/UartController.h"#include "Controllers/I2cController.h"#include "Controllers/OneWireController.h"@@ -81,6 +82,7 @@ public:BluetoothService& getBluetoothService();WifiService& getWifiService();I2sService& getI2sService();+ SshService& getSshService();// ControllersUartController& getUartController();@@ -99,7 +101,7 @@ public:BluetoothController& getBluetoothController();WifiController& getWifiController();I2sController& getI2sController();-+// TransformersTerminalCommandTransformer& getCommandTransformer();InstructionTransformer& getInstructionTransformer();@@ -144,6 +146,7 @@ private:WifiService wifiService;BluetoothService bluetoothService;I2sService i2sService;+ SshService sshService;// ControllersUartController uartController;diff --git a/src/Services/SshService.cpp b/src/Services/SshService.cppnew file mode 100644index 00000000..c51655ab--- /dev/null+++ b/src/Services/SshService.cpp@@ -0,0 +1,113 @@+#include "SshService.h"+#include <Arduino.h>++struct SshTaskParams {+ std::string host, user, pass;+ int verbosity;+ int port;+ SshService* service;+};++void SshService::startTask(const std::string& host, const std::string& user, const std::string& pass, int verbosity, int port) {+ auto* params = new SshTaskParams{host, user, pass, verbosity, port, this};+ xTaskCreatePinnedToCore(connectTask, "SSHConnect", 20000, params, 1, nullptr, 1);+ delay(2000); // let the task begins+}++void SshService::connectTask(void* pvParams) {+ auto* params = static_cast<SshTaskParams*>(pvParams);+ params->service->connect(params->host, params->user, params->pass, params->verbosity, params->port);+ delete params;+ vTaskDelete(nullptr);+}++bool SshService::connect(const std::string& host, const std::string& user, const std::string& pass, int verbosity, int port) {+ session = ssh_new();+ if (!session) return false;++ ssh_options_set(session, SSH_OPTIONS_HOST, host.c_str());+ ssh_options_set(session, SSH_OPTIONS_USER, user.c_str());+ ssh_options_set(session, SSH_OPTIONS_PORT, &port);+ ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);++ if (ssh_connect(session) != SSH_OK) {+ close();+ return false;+ }++ if (!authenticate(pass)) return false;+ if (!openChannel()) return false;+ if (!requestPty()) return false;+ if (!startShell()) return false;++ connected = true;+ return true;+}++bool SshService::authenticate(const std::string& password) {+ int rc = ssh_userauth_password(session, nullptr, password.c_str());+ if (rc != SSH_AUTH_SUCCESS) {+ return false;+ }+ return true;+}++bool SshService::openChannel() {+ channel = ssh_channel_new(session);+ if (!channel || ssh_channel_open_session(channel) != SSH_OK) {+ return false;+ }+ return true;+}++bool SshService::requestPty() {+ if (ssh_channel_request_pty(channel) != SSH_OK) {+ return false;+ }+ return true;+}++bool SshService::startShell() {+ if (ssh_channel_request_shell(channel) != SSH_OK) {+ return false;+ }+ return true;+}++bool SshService::isConnected() const {+ if (!connected || !session || !channel) return false;+ return ssh_channel_is_open(channel) && !ssh_channel_is_eof(channel);+}++void SshService::writeChar(char c) {+ if (!isConnected()) return;+ ssh_channel_write(channel, &c, 1);+}++std::string SshService::readOutput() {+ if (!isConnected()) return "";+ char buf[256];+ int n = ssh_channel_read(channel, buf, sizeof(buf), 0);+ return (n > 0) ? std::string(buf, n) : "";+}++std::string SshService::readOutputNonBlocking() {+ if (!isConnected()) return "";+ char buf[256];+ int n = ssh_channel_read_nonblocking(channel, buf, sizeof(buf), 0);+ return (n > 0) ? std::string(buf, n) : "";+}++void SshService::close() {+ if (channel) {+ ssh_channel_close(channel);+ ssh_channel_free(channel);+ channel = nullptr;+ }+ if (session) {+ ssh_disconnect(session);+ ssh_free(session);+ session = nullptr;+ }+ connected = false;+}diff --git a/src/Services/SshService.h b/src/Services/SshService.hnew file mode 100644index 00000000..5685c057--- /dev/null+++ b/src/Services/SshService.h@@ -0,0 +1,29 @@+#pragma once++#include <string>+#include <libssh/libssh.h>++class SshService {+public:+ void startTask(const std::string& host, const std::string& user, const std::string& pass, int verbosity, int port);++ bool isConnected() const;+ void writeChar(char c);+ std::string readOutput();+ std::string readOutputNonBlocking();+ void close();++private:+ // SSH Task, cause overflow if it runs in the main loop, so it must run in a dedicated FreeRTOS task with a larger stack+ static void connectTask(void* pvParams);++ bool connect(const std::string& host, const std::string& user, const std::string& pass, int verbosity, int port=22);+ bool authenticate(const std::string& password);+ bool openChannel();+ bool requestPty();+ bool startShell();++ ssh_session session = nullptr;+ ssh_channel channel = nullptr;+ bool connected = false;+};