Skip to content

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

create base imp for ethernet mode

Geofb69591113e0
diff --git a/src/Controllers/EthernetController.cpp b/src/Controllers/EthernetController.cpp
new file mode 100644
index 00000000..4d551185
--- /dev/null
+++ b/src/Controllers/EthernetController.cpp
@@ -0,0 +1,182 @@
+#include "Controllers/EthernetController.h"
+
+/*
+Constructor
+*/
+EthernetController::EthernetController(ITerminalView& terminalView,
+ IInput& terminalInput,
+ IInput& deviceInput,
+ EthernetService& ethernetService,
+ ArgTransformer& argTransformer,
+ UserInputManager& userInputManager)
+: terminalView(terminalView)
+, terminalInput(terminalInput)
+, deviceInput(deviceInput)
+, ethernetService(ethernetService)
+, argTransformer(argTransformer)
+, userInputManager(userInputManager)
+{}
+
+/*
+Entry point for command
+*/
+void EthernetController::handleCommand(const TerminalCommand& cmd) {
+ const auto& root = cmd.getRoot();
+
+ if (root == "config") handleConfig();
+ else if (root == "connect") handleConnect();
+ else if (root == "status") handleStatus();
+ else if (root == "reset") handleReset();
+ else handleHelp();
+}
+
+/*
+Connect using DHCP
+*/
+void EthernetController::handleConnect() {
+
+ unsigned long timeoutMs = 5000;
+ std::string macStr;
+ std::array<uint8_t,6> mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x42 };
+
+ auto confirmation = userInputManager.readYesNo(
+ "Connect to Ethernet using DHCP with default MAC?",
+ true
+ );
+
+ // Ask for MAC if not confirmed
+ if (!confirmation) {
+ macStr = userInputManager.readValidatedHexString(
+ "MAC (AA:BB:CC:DD:EE:FF)",
+ 6,
+ true
+ );
+
+ argTransformer.parseMac(macStr, mac);
+ }
+
+ terminalView.println("Ethernet: DHCP…");
+ if (!ethernetService.beginDHCP(mac, timeoutMs)) {
+ terminalView.println("Ethernet: DHCP failed.");
+ return;
+ }
+
+ terminalView.println("Ethernet: Connected via DHCP.");
+ terminalView.println(" IP: " + ethernetService.getLocalIP());
+ terminalView.println(" GW: " + ethernetService.getGatewayIp());
+ terminalView.println(" MASK: " + ethernetService.getSubnetMask());
+ terminalView.println(" DNS: " + ethernetService.getDns());
+}
+
+/*
+Config W5500
+*/
+void EthernetController::handleConfig() {
+ terminalView.println("\nEthernet (W5500) Configuration:");
+
+ const auto& forbidden = state.getProtectedPins();
+
+ uint8_t defCS = state.getEthernetCsPin();
+ uint8_t defRST = state.getEthernetRstPin();
+ uint8_t defSCK = state.getEthernetSckPin();
+ uint8_t defMISO = state.getEthernetMisoPin();
+ uint8_t defMOSI = state.getEthernetMosiPin();
+ uint32_t defHz = state.getEthernetFrequency();
+
+ // User input for configuration
+ uint8_t cs = userInputManager.readValidatedPinNumber("W5500 CS pin", defCS, forbidden);
+ uint8_t sck = userInputManager.readValidatedPinNumber("W5500 SCK pin", defSCK, forbidden);
+ uint8_t miso = userInputManager.readValidatedPinNumber("W5500 MISO pin", defMISO, forbidden);
+ uint8_t mosi = userInputManager.readValidatedPinNumber("W5500 MOSI pin", defMOSI, forbidden);
+
+ // RST optional
+ bool useReset = userInputManager.readYesNo(
+ "Use a RESET (RST) pin?",
+ false
+ );
+
+ uint8_t rst = 255;
+ if (useReset) {
+ rst = userInputManager.readValidatedPinNumber("RST pin", (defRST == 255 ? 4 : defRST), forbidden);
+ }
+
+ // Frequency SPI
+ uint32_t hz = userInputManager.readValidatedUint32("SPI frequency (Hz)", defHz);
+
+ // Save
+ state.setEthernetCsPin(cs);
+ state.setEthernetSckPin(sck);
+ state.setEthernetMisoPin(miso);
+ state.setEthernetMosiPin(mosi);
+ state.setEthernetRstPin(useReset ? rst : 255);
+ state.setEthernetFrequency(hz);
+
+ // Configure
+ ethernetService.configure(
+ cs,
+ (useReset ? rst : -1),
+ sck,
+ miso,
+ mosi,
+ hz
+ );
+
+ terminalView.println("Ethernet configured.");
+}
+
+/*
+W55000 Status
+*/
+void EthernetController::handleStatus()
+{
+ if (ethernetService.isConnected()) {
+ terminalView.println("Ethernet: Connected");
+ terminalView.println(" IP: " + ethernetService.getLocalIP());
+ } else {
+ terminalView.println("Ethernet: Not connected.");
+ }
+ terminalView.println(" MAC: " + ethernetService.getMac());
+
+ auto hw = ethernetService.hardwareStatusRaw();
+ auto lk = ethernetService.linkStatusRaw();
+ terminalView.println(" HW: " + std::to_string(hw) + " | LINK: " + std::to_string(lk) + (ethernetService.linkUp() ? " (UP)" : " (DOWN)"));
+}
+
+/*
+Reset
+*/
+void EthernetController::handleReset()
+{
+ // ethernetService.disconnect();
+ terminalView.println("Ethernet: Interface reset. Disconnected. [NYI]");
+}
+
+/*
+Help
+*/
+void EthernetController::handleHelp() {
+ terminalView.println("Ethernet commands:");
+ terminalView.println(" status");
+ terminalView.println(" connect");
+ terminalView.println(" reset");
+ terminalView.println(" config");
+}
+
+/*
+Ensure W5500 is configured
+*/
+void EthernetController::ensureConfigured() {
+ if (!configured) {
+ handleConfig();
+ configured = true;
+ return;
+ }
+
+ // Reconfigure in case these pins have been used somewhere else
+ auto cs = state.getEthernetCsPin();
+ auto sck = state.getEthernetSckPin();
+ auto miso = state.getEthernetMisoPin();
+ auto mosi = state.getEthernetMosiPin();
+ auto frequency = state.getEthernetFrequency();
+ ethernetService.configure(cs, sck, miso, mosi, frequency);
+}
diff --git a/src/Controllers/EthernetController.h b/src/Controllers/EthernetController.h
new file mode 100644
index 00000000..af1584f0
--- /dev/null
+++ b/src/Controllers/EthernetController.h
@@ -0,0 +1,54 @@
+#pragma once
+
+#include <Arduino.h>
+#include <string>
+#include <vector>
+#include <array>
+
+#include "Interfaces/ITerminalView.h"
+#include "Interfaces/IInput.h"
+#include "Services/EthernetService.h"
+#include "Services/SshService.h"
+#include "Services/NetcatService.h"
+#include "Services/NmapService.h"
+#include "Transformers/ArgTransformer.h"
+#include "Models/TerminalCommand.h"
+#include "States/GlobalState.h"
+#include "Managers/UserInputManager.h"
+
+class EthernetController {
+public:
+ EthernetController(ITerminalView& terminalView,
+ IInput& terminalInput,
+ IInput& deviceInput,
+ EthernetService& ethernetService,
+ ArgTransformer& argTransformer,
+ UserInputManager& userInputManager);
+
+ // Entry point for Ethernet command
+ void handleCommand(const TerminalCommand& cmd);
+
+ // Ensure Ethernet is configured before any action
+ void ensureConfigured();
+
+private:
+ // Handlers
+ void handleConfig();
+ void handleStatus();
+ void handleConnect();
+ void handleReset();
+ void handleHelp();
+
+private:
+ ITerminalView& terminalView;
+ IInput& terminalInput;
+ IInput& deviceInput;
+
+ EthernetService& ethernetService;
+ ArgTransformer& argTransformer;
+ UserInputManager& userInputManager;
+
+ GlobalState& state = GlobalState::getInstance();
+
+ bool configured = false;
+};
diff --git a/src/Services/EthernetService.cpp b/src/Services/EthernetService.cpp
new file mode 100644
index 00000000..20fda04c
--- /dev/null
+++ b/src/Services/EthernetService.cpp
@@ -0,0 +1,79 @@
+#include "EthernetService.h"
+
+EthernetService::EthernetService() {}
+
+void EthernetService::configure(int8_t pinCS, int8_t pinRST, int8_t pinSCK, int8_t pinMISO, int8_t pinMOSI, uint32_t spiHz) {
+ _pinCS = pinCS;
+ _pinRST = pinRST;
+ _pinSCK = pinSCK;
+ _pinMISO= pinMISO;
+ _pinMOSI= pinMOSI;
+ _spiHz = spiHz;
+
+ SPI.end();
+ SPI.begin(_pinSCK, _pinMISO, _pinMOSI, _pinCS);
+
+ Ethernet.init(_pinCS);
+
+ hardReset();
+
+ _configured = true;
+}
+
+void EthernetService::hardReset() {
+ if (_pinRST < 0) return;
+ pinMode(_pinRST, OUTPUT);
+ digitalWrite(_pinRST, LOW);
+ delay(5);
+ digitalWrite(_pinRST, HIGH);
+ delay(200);
+}
+
+bool EthernetService::beginDHCP(const std::array<uint8_t,6>& mac, unsigned long timeoutMs) {
+ if (!_configured) return false;
+ _mac = mac;
+
+ int ret = Ethernet.begin(const_cast<uint8_t*>(_mac.data()),
+ timeoutMs,
+ 4000); // responseTimeout
+
+ if (ret != 1) return false;
+ return true;
+}
+
+bool EthernetService::isConnected() const {
+ return linkUp() && Ethernet.localIP();
+}
+
+void EthernetService::maintain() {
+ Ethernet.maintain();
+}
+
+int EthernetService::hardwareStatusRaw() const {
+ return (int)Ethernet.hardwareStatus();
+}
+int EthernetService::linkStatusRaw() const {
+ return (int)Ethernet.linkStatus();
+}
+bool EthernetService::linkUp() const {
+ return Ethernet.linkStatus() == LinkON;
+}
+
+std::string EthernetService::getLocalIP() const {
+ return Ethernet.localIP().toString().c_str();
+}
+std::string EthernetService::getSubnetMask() const {
+ return Ethernet.subnetMask().toString().c_str();
+}
+std::string EthernetService::getGatewayIp() const {
+ return Ethernet.gatewayIP().toString().c_str();
+}
+std::string EthernetService::getDns() const {
+ return Ethernet.dnsServerIP().toString().c_str();
+}
+std::string EthernetService::getMac() const {
+ char buf[18];
+ sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
+ _mac[0],_mac[1],_mac[2],_mac[3],_mac[4],_mac[5]);
+ return std::string(buf);
+}
\ No newline at end of file
diff --git a/src/Services/EthernetService.h b/src/Services/EthernetService.h
new file mode 100644
index 00000000..bcba973e
--- /dev/null
+++ b/src/Services/EthernetService.h
@@ -0,0 +1,50 @@
+#pragma once
+
+#include <Arduino.h>
+#include <SPI.h>
+#include <Ethernet.h>
+#include <ESP32Ping.h>
+
+#include <array>
+#include <string>
+#include <memory>
+
+class EthernetService {
+public:
+ EthernetService();
+
+ void configure(int8_t pinCS,
+ int8_t pinRST = -1,
+ int8_t pinSCK = 18,
+ int8_t pinMISO = 19,
+ int8_t pinMOSI = 23,
+ uint32_t spiHz = 26000000UL);
+
+ bool beginDHCP(const std::array<uint8_t,6>& mac, unsigned long timeoutMs = 10000);
+ bool isConnected() const;
+ void maintain();
+
+ std::string getLocalIP() const;
+ std::string getSubnetMask()const;
+ std::string getGatewayIp() const;
+ std::string getDns() const;
+ std::string getMac() const;
+
+ int hardwareStatusRaw() const; // Ethernet.hardwareStatus()
+ int linkStatusRaw() const; // Ethernet.linkStatus()
+ bool linkUp() const;
+
+private:
+ void hardReset();
+
+ // pins / conf SPI
+ int8_t _pinCS = 5;
+ int8_t _pinRST = -1;
+ int8_t _pinSCK = 18;
+ int8_t _pinMISO = 19;
+ int8_t _pinMOSI = 23;
+ uint32_t _spiHz = 26000000UL;
+
+ std::array<uint8_t,6> _mac {{0,0,0,0,0,0}};
+ bool _configured = false;
+};