Skip to content

A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol

Added NetcatService SSH copycat

Andrei Vladescu32c1c2188513
diff --git a/src/Controllers/WifiController.cpp b/src/Controllers/WifiController.cpp
index 57be5df6..a8369315 100644
--- a/src/Controllers/WifiController.cpp
+++ b/src/Controllers/WifiController.cpp
@@ -367,6 +367,73 @@ void WifiController::handleSsh(const TerminalCommand& cmd) {
}
void WifiController::handleNetcat(const TerminalCommand& cmd){
+ // Check connection
+ if (!wifiService.isConnected()) {
+ terminalView.println("Netcat: You must be connected to Wi-Fi. Use 'connect' first.");
+ return;
+ }
+
+ // Check args
+ auto args = argTransformer.splitArgs(cmd.getArgs());
+ if (cmd.getSubcommand().empty() || args.size() < 1) {
+ terminalView.println("Usage: nc <host> <port>");
+ return;
+ }
+
+ std::string host = cmd.getSubcommand();
+ std::string port_str = args[0];
+
+
+ // Check port
+ int port = 1;
+ if (argTransformer.isValidNumber(port_str)) {
+ port = argTransformer.parseHexOrDec16(port_str);
+ } else {
+ terminalView.println("Netcat: Invalid port number. Use a valid integer.");
+ return;
+ }
+
+ if (port < 1 || port > 65535) {
+ terminalView.println("Netcat: Port must be between 1 and 65535.");
+ return;
+ }
+
+ // Connect, start the netcat task
+ terminalView.println("Netcat: Connecting to " + host + " with port " + port_str + "...");
+ netcatService.startTask(host, false, port);
+
+ // Wait 5sec for connection success
+ unsigned long start = millis();
+ while (!netcatService.isConnected() && millis() - start < 5000) {
+ delay(500);
+ }
+
+ // Can't connect
+ if (!netcatService.isConnected()) {
+ terminalView.println("\r\nNetcat: Connection failed.");
+ netcatService.close();
+ return;
+ }
+
+
+ // Connected, start the bridge loop
+ terminalView.println("Netcat: Connected. Shell started... Press [ANY ESP32 KEY] to stop.\n");
+ while (true) {
+ char terminalKey = terminalInput.readChar();
+ if (terminalKey != KEY_NONE) netcatService.writeChar(terminalKey);
+
+ char deviceKey = deviceInput.readChar();
+ if (deviceKey != KEY_NONE) break;
+
+ std::string output = netcatService.readOutputNonBlocking();
+ if (!output.empty()) terminalView.print(output);
+
+ delay(10);
+ }
+
+ // Close Netcat
+ netcatService.close();
+ terminalView.println("\r\n\nNetcat: Session closed.");
}
diff --git a/src/Controllers/WifiController.h b/src/Controllers/WifiController.h
index 84502801..30b5c1a9 100644
--- a/src/Controllers/WifiController.h
+++ b/src/Controllers/WifiController.h
@@ -5,6 +5,7 @@
#include <Services/WifiService.h>
#include <Services/NvsService.h>
#include <Services/SshService.h>
+#include <Services/NetcatService.h>
#include <Transformers/ArgTransformer.h>
#include <Models/TerminalCommand.h>
#include <States/GlobalState.h>
@@ -36,6 +37,7 @@ private:
WifiService& wifiService;
NvsService& nvsService;
SshService& sshService;
+ NetcatService& netcatService;
ArgTransformer& argTransformer;
GlobalState& state = GlobalState::getInstance();
bool configured = false;
diff --git a/src/Services/NetcatService.cpp b/src/Services/NetcatService.cpp
new file mode 100644
index 00000000..d9a9b62a
--- /dev/null
+++ b/src/Services/NetcatService.cpp
@@ -0,0 +1,116 @@
+#include "NetcatService.h"
+#include <Arduino.h>
+
+struct NetcatTaskParams {
+ std::string host;
+ int verbosity;
+ int port;
+ NetcatService* service;
+};
+
+void NetcatService::startTask(const std::string& host, int verbosity, int port) {
+ auto* params = new NetcatTaskParams{host, verbosity, port, this};
+ xTaskCreatePinnedToCore(connectTask, "NetcatConnect", 20000, params, 1, nullptr, 1);
+ delay(2000); // let the task begins
+}
+
+void NetcatService::connectTask(void* pvParams) {
+ auto* params = static_cast<NetcatTaskParams*>(pvParams);
+ params->service->connect(params->host, params->verbosity, params->port);
+ delete params;
+ vTaskDelete(nullptr);
+}
+
+bool NetcatService::connect(const std::string& host, 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 NetcatService::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 NetcatService::openChannel() {
+ // channel = ssh_channel_new(session);
+ // if (!channel || ssh_channel_open_session(channel) != SSH_OK) {
+ // return false;
+ // }
+ return true;
+}
+
+bool NetcatService::requestPty() {
+ // if (ssh_channel_request_pty(channel) != SSH_OK) {
+ // return false;
+ // }
+ return true;
+}
+
+bool NetcatService::startShell() {
+ // if (ssh_channel_request_shell(channel) != SSH_OK) {
+ // return false;
+ // }
+ return true;
+}
+
+bool NetcatService::isConnected() const {
+ // if (!connected || !session || !channel) return false;
+ //return ssh_channel_is_open(channel) && !ssh_channel_is_eof(channel);
+ return true;
+}
+
+void NetcatService::writeChar(char c) {
+ if (!isConnected()) return;
+ //ssh_channel_write(channel, &c, 1);
+}
+
+std::string NetcatService::readOutput() {
+ if (!isConnected()) return "";
+ char buf[256];
+ //int n = ssh_channel_read(channel, buf, sizeof(buf), 0);
+ //return (n > 0) ? std::string(buf, n) : "";
+ return 0;
+}
+
+std::string NetcatService::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) : "";
+ return 0;
+}
+
+void NetcatService::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/NetcatService.h b/src/Services/NetcatService.h
new file mode 100644
index 00000000..c76a54c7
--- /dev/null
+++ b/src/Services/NetcatService.h
@@ -0,0 +1,28 @@
+#pragma once
+
+#include <string>
+
+class NetcatService {
+public:
+ void startTask(const std::string& host,int verbosity, int port);
+
+ bool isConnected() const;
+ void writeChar(char c);
+ std::string readOutput();
+ std::string readOutputNonBlocking();
+ void close();
+
+private:
+ // Netcat 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, int verbosity, int port);
+ bool authenticate(const std::string& password);
+ bool openChannel();
+ bool requestPty();
+ bool startShell();
+
+ //ssh_session session = nullptr;
+ //ssh_channel channel = nullptr;
+ bool connected = false;
+};