A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol
C arduinobluetoothcan-busdebuggingeepromesp32flipperzerogpiohardware-hackingi2cjtagopenocdprotocolspwmradiorfidspisubghzuartwifi
add unstable base imp for 2wire and sle4442
Geocf794eaa4acb
diff --git a/src/Controllers/TwoWireController.cpp b/src/Controllers/TwoWireController.cppindex 37d60141..2c3492b6 100644--- a/src/Controllers/TwoWireController.cpp+++ b/src/Controllers/TwoWireController.cpp@@ -3,57 +3,182 @@/*Constructor*/-TwoWireController::TwoWireController(ITerminalView& terminalView, IInput& terminalInput)- : terminalView(terminalView), terminalInput(terminalInput) {}-+TwoWireController::TwoWireController(ITerminalView& terminalView, IInput& terminalInput, UserInputManager& userInputManager, TwoWireService& twoWireService)+ : terminalView(terminalView), terminalInput(terminalInput), userInputManager(userInputManager), twoWireService(twoWireService) {}/*-Entry point for command+Entry point for 2WIRE command*/void TwoWireController::handleCommand(const TerminalCommand& cmd) {- if (cmd.getRoot() == "sniff") {- handleSniff();- } else if (cmd.getRoot() == "config") {+ if (cmd.getRoot() == "config") {handleConfig();+ // Not fully fonctionnal for now+ // } if (cmd.getRoot() == "at") {+ // handleATR();++ // } else if (cmd.getRoot() == "test") {+ // handleTest();+ // } else if (cmd.getRoot() == "sec") {+ // handleSniff();+ // } else if (cmd.getRoot() == "dump") {+ // handleDump();} else {- terminalView.println("");- terminalView.println("Unknown 2WIRE command. Usage:");- terminalView.println(" sniff - Sniff 8-bit data with start/stop bits [NYI]");- terminalView.println(" config - Configure 2WIRE settings [NYI]");- terminalView.println(" [..] - Instruction syntax supported [NYI]");- terminalView.println("");+ handleHelp();}}+void TwoWireController::handleTest() {+ ensureConfigured();++ terminalView.println("\n2WIRE TEST: Reading security memory...");+ twoWireService.sendCommand(0x31, 0x00, 0x00);+ auto sec = twoWireService.readResponse(4);+ std::string secStr = "Security:";+ for (auto b : sec) {+ char buf[8];+ sprintf(buf, " 0x%02X", b);+ secStr += buf;+ }+ terminalView.println(secStr);+ delay(2);++ terminalView.println("\n==> Read main memory [0x30 0x00 0x10]");+ twoWireService.sendCommand(0x30, 0x00, 0x10);+ auto main = twoWireService.readResponse(16);+ std::string mainStr = "Main:";+ for (auto b : main) {+ char buf[8];+ sprintf(buf, " 0x%02X", b);+ mainStr += buf;+ }+ terminalView.println(mainStr);+ delay(2);++ terminalView.println("\n==> Read PIN [0x33 0x00 0x03]");+ twoWireService.sendCommand(0x33, 0x00, 0x03);+ auto pin = twoWireService.readResponse(3);+ std::string pinStr = "PIN:";+ for (auto b : pin) {+ char buf[8];+ sprintf(buf, " 0x%02X", b);+ pinStr += buf;+ }+ terminalView.println(pinStr);+}++/*-Entry point for instruction+Entry point for 2WIRE instruction*/void TwoWireController::handleInstruction(const std::vector<ByteCode>& bytecodes) {- terminalView.println("Instruction handling not yet implemented");+ // Instruction support à venir+ terminalView.println("[TODO] Instruction support for 2WIRE not yet implemented.");}-/*Sniff*/-void TwoWireController::handleSniff() {- terminalView.println("2WIRE sniff [NYI]");+void TwoWireController::handleSec() {+ twoWireService.testReadSecurityMemory();}+/*+Perform ATR+*/+void TwoWireController::handleATR() {+ ensureConfigured();+ terminalView.println("\n2WIRE ATR: Performing...");++ auto atr = twoWireService.performATR();+ std::stringstream ss;+ ss << "ATR: ";++ if (atr.empty()) {+ terminalView.println("No response received.");+ } else {+ terminalView.print("ATR: ");+ for (uint8_t b : atr) {+ ss << "0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(b) << " ";+ }+ terminalView.println(ss.str());+ }+}/*-Config+Dump+*/+void TwoWireController::handleDump() {+ ensureConfigured();++ terminalView.println("\n2WIRE DUMP: Reading full main memory (256 bytes)...");++ for (int i = 0; i < 16; ++i) {+ uint8_t addr = i * 16;+ twoWireService.sendCommand(0x30, addr, 16);+ auto data = twoWireService.readResponse(16);++ std::stringstream ss;+ ss << std::hex << std::uppercase << std::setfill('0');+ ss << std::setw(2) << addr << ": ";++ for (auto b : data) {+ ss << std::setw(2) << static_cast<int>(b) << " ";+ }++ terminalView.println(ss.str());+ }++ terminalView.println("Done.");+}++/*+Configuration*/void TwoWireController::handleConfig() {- terminalView.println(" [INFO] This mode is not implemented yet.");- terminalView.println(" Support is planned for a future release.\n");+ terminalView.println("\nConfigure 2WIRE Pins:");+ const auto& forbidden = state.getProtectedPins();++ uint8_t clk = userInputManager.readValidatedPinNumber("CLK pin", state.getTwoWireClkPin(), forbidden);+ state.setTwoWireClkPin(clk);++ uint8_t io = userInputManager.readValidatedPinNumber("IO pin", state.getTwoWireIoPin(), forbidden);+ state.setTwoWireIoPin(io);++ uint8_t rst = userInputManager.readValidatedPinNumber("RST pin", state.getTwoWireRstPin(), forbidden);+ state.setTwoWireRstPin(rst);++ twoWireService.configure(clk, io, rst);+ configured = true;++ terminalView.println("2WIRE configuration applied.\n");}+/*+Help+*/+void TwoWireController::handleHelp() {+ terminalView.println("\n2WIRE Commands:\n"+ " NOT YET FUNCTIONNAL"+ " atr Perform ATR handshake\n"+ " dump Read full 256-byte memory\n"+ " config Set CLK/IO/RST pins\n"+ " [..] Instruction syntax supported later\n");+}+/*+Ensure Configuration+*/void TwoWireController::ensureConfigured() {if (!configured) {handleConfig();- configured = true;+ } else {+ twoWireService.configure(+ state.getTwoWireClkPin(),+ state.getTwoWireIoPin(),+ state.getTwoWireRstPin()+ );}-}\ No newline at end of file+}++diff --git a/src/Controllers/TwoWireController.h b/src/Controllers/TwoWireController.hindex 9e271156..2cf53c73 100644--- a/src/Controllers/TwoWireController.h+++ b/src/Controllers/TwoWireController.h@@ -8,11 +8,14 @@#include "Interfaces/IInput.h"#include "Models/TerminalCommand.h"#include "Models/ByteCode.h"+#include "Services/TwoWireService.h"+#include "Managers/UserInputManager.h"+#include "States/GlobalState.h"class TwoWireController {public:// Constructor- TwoWireController(ITerminalView& terminalView, IInput& terminalInput);+ TwoWireController(ITerminalView& terminalView, IInput& terminalInput, UserInputManager& userInputManager, TwoWireService& twoWireService);// Entry point for handle raw user commandvoid handleCommand(const TerminalCommand& cmd);@@ -26,11 +29,16 @@ public:private:ITerminalView& terminalView; // Terminal outputIInput& terminalInput; // User input+ UserInputManager& userInputManager;+ TwoWireService& twoWireService;+ GlobalState& state = GlobalState::getInstance();bool configured = false; // Init state- // Start sniffing 2WIRE bus- void handleSniff();-- // Set up 2WIRE parameters+ // Command handlers+ void handleSec();+ void handleATR();void handleConfig();+ void handleHelp();+ void handleTest();+ void handleDump();};diff --git a/src/Providers/DependencyProvider.cpp b/src/Providers/DependencyProvider.cppindex 37b205f6..65c488a0 100644--- a/src/Providers/DependencyProvider.cpp+++ b/src/Providers/DependencyProvider.cpp@@ -15,6 +15,7 @@ DependencyProvider::DependencyProvider(ITerminalView& terminalView, IDeviceView&uartService(),i2cService(),oneWireService(),+ twoWireService(),infraredService(),spiService(),pinService(),@@ -38,7 +39,7 @@ DependencyProvider::DependencyProvider(ITerminalView& terminalView, IDeviceView&hdUartController(terminalView, terminalInput, deviceInput, hdUartService, uartService, argTransformer, userInputManager),spiController(terminalView, terminalInput, spiService, sdService, argTransformer, userInputManager, binaryAnalyzeManager),jtagController(terminalView, terminalInput),- twoWireController(terminalView, terminalInput),+ twoWireController(terminalView, terminalInput, userInputManager, twoWireService),threeWireController(terminalView, terminalInput),dioController(terminalView, terminalInput, pinService, argTransformer),ledController(terminalView, terminalInput, ledService),@@ -61,6 +62,7 @@ LedService& DependencyProvider::getLedService() { return ledService; }I2cService& DependencyProvider::getI2cService() { return i2cService; }UartService& DependencyProvider::getUartService() { return uartService; }OneWireService& DependencyProvider::getOneWireService() { return oneWireService; }+TwoWireService& DependencyProvider::getTwoWireService() { return twoWireService; }InfraredService& DependencyProvider::getInfraredService() { return infraredService; }IUsbService& DependencyProvider::getUsbService() { return usbService; }SpiService& DependencyProvider::getSpiService() { return spiService; }@@ -97,7 +99,7 @@ WebRequestTransformer& DependencyProvider::getWebRequestTransformer() {return we// ManagersCommandHistoryManager& DependencyProvider::getCommandHistoryManager() { return commandHistoryManager ;}UserInputManager& DependencyProvider::getUserInputManager() { return userInputManager; }-BinaryAnalyzeManager& DependencyProvider::getFlashAnalyzeManager() { return binaryAnalyzeManager; }+BinaryAnalyzeManager& DependencyProvider::getBinaryAnalyzeManager() { return binaryAnalyzeManager; }// SelectorsHorizontalSelector& DependencyProvider::getHorizontalSelector() { return horizontalSelector ;}@@ -105,8 +107,6 @@ HorizontalSelector& DependencyProvider::getHorizontalSelector() { return horizon// ConfigTerminalTypeConfigurator& DependencyProvider::getTerminalTypeConfigurator() { return terminalTypeConfigurator ;}--// Disable interfacesvoid DependencyProvider::disableAllProtocols() {getUartService().end();diff --git a/src/Providers/DependencyProvider.h b/src/Providers/DependencyProvider.hindex 752d5dfa..c4bf65d2 100644--- a/src/Providers/DependencyProvider.h+++ b/src/Providers/DependencyProvider.h@@ -72,6 +72,7 @@ public:UartService& getUartService();I2cService& getI2cService();OneWireService& getOneWireService();+ TwoWireService& getTwoWireService();InfraredService& getInfraredService();IUsbService& getUsbService();SpiService& getSpiService();@@ -108,7 +109,7 @@ public:// ManagersCommandHistoryManager& getCommandHistoryManager();UserInputManager& getUserInputManager();- BinaryAnalyzeManager& getFlashAnalyzeManager();+ BinaryAnalyzeManager& getBinaryAnalyzeManager();// SelectorsHorizontalSelector& getHorizontalSelector();@@ -135,6 +136,7 @@ private:UartService uartService;I2cService i2cService;OneWireService oneWireService;+ TwoWireService twoWireService;InfraredService infraredService;HdUartService hdUartService;SpiService spiService;diff --git a/src/Services/TwoWireService.cpp b/src/Services/TwoWireService.cppnew file mode 100644index 00000000..454c8624--- /dev/null+++ b/src/Services/TwoWireService.cpp@@ -0,0 +1,155 @@+#include "TwoWireService.h"+#include <Arduino.h>++void TwoWireService::configure(uint8_t clk, uint8_t io, uint8_t rst) {+ clkPin = clk;+ ioPin = io;+ rstPin = rst;++ gpio_reset_pin((gpio_num_t)clkPin);+ gpio_reset_pin((gpio_num_t)rstPin);+ gpio_reset_pin((gpio_num_t)ioPin);++ gpio_set_direction((gpio_num_t)clkPin, GPIO_MODE_OUTPUT);+ gpio_set_direction((gpio_num_t)rstPin, GPIO_MODE_OUTPUT);+ gpio_set_direction((gpio_num_t)ioPin, GPIO_MODE_OUTPUT);++ gpio_set_level((gpio_num_t)clkPin, 0);+ gpio_set_level((gpio_num_t)rstPin, 0);+ gpio_set_level((gpio_num_t)ioPin, 1); // release+}++void TwoWireService::setRST(bool level) {+ gpio_set_level((gpio_num_t)rstPin, level ? 1 : 0);+}++void TwoWireService::setCLK(bool level) {+ gpio_set_level((gpio_num_t)clkPin, level ? 1 : 0);+}++void TwoWireService::setIO(bool level) {+ if (level) {+ gpio_set_direction((gpio_num_t)ioPin, GPIO_MODE_INPUT);+ gpio_set_pull_mode((gpio_num_t)ioPin, GPIO_PULLUP_ONLY);+ } else {+ gpio_set_pull_mode((gpio_num_t)ioPin, GPIO_FLOATING);+ gpio_set_direction((gpio_num_t)ioPin, GPIO_MODE_OUTPUT);+ gpio_set_level((gpio_num_t)ioPin, 0);+ }+}++bool TwoWireService::readIO() {+ gpio_set_direction((gpio_num_t)ioPin, GPIO_MODE_INPUT);+ return gpio_get_level((gpio_num_t)ioPin);+}++void TwoWireService::pulseClock() {+ setCLK(false);+ delayMicroseconds(5);+ setCLK(true);+ delayMicroseconds(5);+ setCLK(false);+}++void TwoWireService::writeBit(bool bit) {+ setIO(bit);+ pulseClock();+}++bool TwoWireService::readBit() {+ setIO(true);+ delayMicroseconds(5);+ setCLK(true);+ delayMicroseconds(5);+ bool bit = readIO();+ setCLK(false);+ delayMicroseconds(5);+ return bit;+}++void TwoWireService::writeByte(uint8_t byte) {+ for (uint8_t i = 0; i < 8; i++) {+ writeBit((byte >> i) & 1);+ }+}++uint8_t TwoWireService::readByte() {+ uint8_t b = 0;+ for (uint8_t i = 0; i < 8; i++) {+ bool bit = readBit();+ if (bit) b |= (1 << i);+ }+ return b;+}++void TwoWireService::sendStart() {+ setIO(true);+ setCLK(true);+ delayMicroseconds(5);+ setIO(false);+ delayMicroseconds(5);+ setCLK(false);+}++void TwoWireService::sendStop() {+ setIO(false);+ setCLK(true);+ delayMicroseconds(5);+ setIO(true);+ delayMicroseconds(5);+ setCLK(false);+}++void TwoWireService::sendCommand(uint8_t a, uint8_t b, uint8_t c) {+ Serial.printf("Sending command: 0x%02X 0x%02X 0x%02X\n\r", a, b, c);+ sendStart();+ writeByte(a);+ writeByte(b);+ writeByte(c);+ sendStop();+}++std::vector<uint8_t> TwoWireService::readResponse(uint16_t len) {+ std::vector<uint8_t> data;+ for (uint16_t i = 0; i < len; i++) {+ data.push_back(readByte());+ }+ return data;+}++void TwoWireService::sendClocks(uint16_t ticks) {+ for (uint16_t i = 0; i < ticks; i++) {+ pulseClock();+ }+}++std::vector<uint8_t> TwoWireService::performATR() {+ std::vector<uint8_t> atr;++ setRST(false); // RST LOW+ delay(1); // wait++ setRST(true); // RST release+ pulseClock(); // tick clock++ delayMicroseconds(50);+ setRST(false); // RST LOW++ // Read 4 bytes lsb first+ return readResponse(4);+}++void TwoWireService::testReadSecurityMemory() {+ Serial.println("==> Reading SLE4442 security memory (0x31 0x00 0x00)");++ sendCommand(0x31, 0x00, 0x00);+ std::vector<uint8_t> sec = readResponse(4);+ Serial.print("Security: ");+ for (uint8_t b : sec) Serial.printf("0x%02X ", b);+ Serial.println();++ if (sec.size() >= 1) {+ uint8_t attempts = sec[0];+ Serial.printf("Remaining unlock attempts: %d (0x%02X)\n", attempts & 0x07, attempts);+ }+}diff --git a/src/Services/TwoWireService.h b/src/Services/TwoWireService.hnew file mode 100644index 00000000..5cc30c5a--- /dev/null+++ b/src/Services/TwoWireService.h@@ -0,0 +1,35 @@+#pragma once++#include <Arduino.h>+#include <vector>++class TwoWireService {+public:+ void configure(uint8_t clkPin, uint8_t ioPin, uint8_t rstPin);++ void setRST(bool level);+ void setCLK(bool level);+ void setIO(bool level);+ bool readIO();++ void pulseClock();++ void writeBit(bool bit);+ bool readBit();+ void writeByte(uint8_t byte);+ uint8_t readByte();++ void sendStart();+ void sendStop();+ void sendCommand(uint8_t a, uint8_t b, uint8_t c);+ std::vector<uint8_t> readResponse(uint16_t len);+ void sendClocks(uint16_t ticks);++ std::vector<uint8_t> performATR();+ void testReadSecurityMemory();++private:+ uint8_t clkPin;+ uint8_t ioPin;+ uint8_t rstPin;+};diff --git a/src/States/GlobalState.h b/src/States/GlobalState.hindex 68a57ae2..71aa980c 100644--- a/src/States/GlobalState.h+++ b/src/States/GlobalState.h@@ -46,6 +46,11 @@ private:// OneWire Default Pinuint8_t oneWirePin = 1; // pin par défaut+ // TwoWire Default Configuration+ uint8_t twoWireClkPin = 1;+ uint8_t twoWireIoPin = 2;+ uint8_t twoWireRstPin = 3;+// UART Default Configurationunsigned long uartBaudRate = 9600;uint32_t uartConfig = 0x800001c; // SERIAL_8N1 by default@@ -140,6 +145,15 @@ public:uint8_t getOneWirePin() const { return oneWirePin; }void setOneWirePin(uint8_t pin) { oneWirePin = pin; }+ // TwoWire+ uint8_t getTwoWireClkPin() const { return twoWireClkPin; }+ uint8_t getTwoWireIoPin() const { return twoWireIoPin; }+ uint8_t getTwoWireRstPin() const { return twoWireRstPin; }++ void setTwoWireClkPin(uint8_t pin) { twoWireClkPin = pin; }+ void setTwoWireIoPin(uint8_t pin) { twoWireIoPin = pin; }+ void setTwoWireRstPin(uint8_t pin) { twoWireRstPin = pin; }+// UARTunsigned long getUartBaudRate() const { return uartBaudRate; }uint32_t getUartConfig() const { return uartConfig; }