Skip to content

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

add smartcard support for 2wire

Geofbb36599cbb1
diff --git a/src/Controllers/TwoWireController.cpp b/src/Controllers/TwoWireController.cpp
index 217451b4..ad10076e 100644
--- a/src/Controllers/TwoWireController.cpp
+++ b/src/Controllers/TwoWireController.cpp
@@ -12,124 +12,164 @@ Entry point for 2WIRE command
void TwoWireController::handleCommand(const TerminalCommand& cmd) {
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 if (cmd.getRoot() == "sniff") {
+ handleSniff();
+ } else if (cmd.getRoot() == "smartcard") {
+ handleSmartCard(cmd);
} else {
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 2WIRE instruction
*/
void TwoWireController::handleInstruction(const std::vector<ByteCode>& bytecodes) {
- // Instruction support à venir
terminalView.println("[TODO] Instruction support for 2WIRE not yet implemented.");
}
/*
Sniff
*/
-void TwoWireController::handleSec() {
- twoWireService.testReadSecurityMemory();
+void TwoWireController::handleSniff() {
+ terminalView.println("2WIRE Sniff [NYI]\n");
}
/*
-Perform ATR
+Smartcard
*/
-void TwoWireController::handleATR() {
+void TwoWireController::handleSmartCard(const TerminalCommand& cmd) {
+ std::string sub = cmd.getSubcommand();
+
+ if (sub == "probe") {
+ handleSmartCardProbe();
+ } else if (sub == "security") {
+ handleSmartCardSecurity();
+ } else if (sub == "dump") {
+ handleSmartCardDump();
+ } else {
+ terminalView.println("Unknown smartcard subcommand. Usage:");
+ terminalView.println(" smartcard probe");
+ terminalView.println(" smartcard security");
+ terminalView.println(" smartcard dump");
+ }
+}
+
+/*
+Smartcard Security
+*/
+void TwoWireController::handleSmartCardSecurity() {
ensureConfigured();
- terminalView.println("\n2WIRE ATR: Performing...");
- auto atr = twoWireService.performATR();
- std::stringstream ss;
- ss << "ATR: ";
+ terminalView.println("2WIRE Security: Perfoming...\n");
+
+ // Security memory
+ terminalView.println(" [Security Memory] Command: 0x31 0x00 0x00");
+ twoWireService.sendCommand(0x31, 0x00, 0x00);
+ auto sec = twoWireService.readResponse(4);
- 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());
+ // Validate
+ bool allZero = std::all_of(sec.begin(), sec.end(), [](uint8_t b) { return b == 0x00; });
+ bool allFF = std::all_of(sec.begin(), sec.end(), [](uint8_t b) { return b == 0xFF; });
+ if (sec.empty() || allZero || allFF) {
+ terminalView.println("2WIRE Security: No smartcard detected (response invalid)");
+ return;
+ }
+
+ // Display
+ std::stringstream secOut;
+ secOut << " Security Bytes: ";
+ for (auto b : sec) secOut << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)b << " ";
+ terminalView.println(secOut.str());
+
+ if (!sec.empty()) {
+ uint8_t attempts = twoWireService.parseSmartCardRemainingAttempts(sec[0]);
+ terminalView.println(" Remaining Unlock Attempts: " + std::to_string(attempts));
}
+
+ terminalView.println("\n2WIRE Security: Completed.");
}
/*
-Dump
+Smartcard Probe (ATR)
*/
-void TwoWireController::handleDump() {
- ensureConfigured();
+void TwoWireController::handleSmartCardProbe() {
+ terminalView.println("\n2WIRE ATR: Performing...\n");
+
+ // ATR
+ auto atr = twoWireService.performSmartCardAtr();
+ std::stringstream ss;
+ ss << "ATR: ";
+
+ // Validate
+ if (atr.empty() || atr[0] == 0x00 || atr[0] == 0xFF) {
+ terminalView.println("2WIRE ATR: No response received from smartcard");
+ return;
+ }
- terminalView.println("\n2WIRE DUMP: Reading full main memory (256 bytes)...");
+ // Decode and display
+ auto decodedAtr = twoWireService.parseSmartCardAtr(atr);
+ for (uint8_t b : atr) {
+ ss << "0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(b) << " ";
+ }
+ terminalView.println(decodedAtr);
+ terminalView.println("2WIRE ATR: Completed.");
+}
- for (int i = 0; i < 16; ++i) {
- uint8_t addr = i * 16;
- twoWireService.sendCommand(0x30, addr, 16);
- auto data = twoWireService.readResponse(16);
+/*
+Smartcard Dump
+*/
+void TwoWireController::handleSmartCardDump() {
+ terminalView.println("\n2WIRE Dump: Reading full memory (MAIN + SEC + PROTECT)...");
+
+ // Dump 256 bytes + sec mem + protection mem
+ auto dump = twoWireService.dumpSmartCardFullMemory();
+ if (dump.size() != 264) {
+ terminalView.println("2WIRE Dump: Failed, unexpected size.");
+ return;
+ }
- std::stringstream ss;
- ss << std::hex << std::uppercase << std::setfill('0');
- ss << std::setw(2) << addr << ": ";
+ // Validate data
+ bool allZero = std::all_of(dump.begin(), dump.end(), [](uint8_t b) { return b == 0x00; });
+ bool allFF = std::all_of(dump.begin(), dump.end(), [](uint8_t b) { return b == 0xFF; });
+ if (allZero || allFF) {
+ terminalView.println("2WIRE Dump: Smartcard is blank or no smartcard detected");
+ return;
+ }
- for (auto b : data) {
- ss << std::setw(2) << static_cast<int>(b) << " ";
+ // Main memory (0–255)
+ terminalView.println("\n[Main Memory]");
+ for (int i = 0; i < 256; i += 16) {
+ std::stringstream line;
+ line << std::hex << std::uppercase << std::setfill('0');
+ line << std::setw(2) << i << ": ";
+ for (int j = 0; j < 16; ++j) {
+ line << std::setw(2) << static_cast<int>(dump[i + j]) << " ";
}
+ terminalView.println(line.str());
+ }
- terminalView.println(ss.str());
+ // Security memory (256–259)
+ terminalView.println("\n[Security Memory]");
+ std::stringstream sec;
+ sec << "SEC: ";
+ for (int i = 256; i < 260; ++i) {
+ sec << "0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(dump[i]) << " ";
+ }
+ uint8_t attempts = twoWireService.parseSmartCardRemainingAttempts(dump[256]);
+ sec << "→ Attempts Left: " << std::dec << (int)attempts;
+ terminalView.println(sec.str());
+
+ // Protection memory (260–263)
+ terminalView.println("\n[Protection Memory]");
+ std::stringstream prt;
+ prt << "PRT: ";
+ for (int i = 260; i < 264; ++i) {
+ prt << "0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(dump[i]) << " ";
}
+ terminalView.println(prt.str());
- terminalView.println("Done.");
+ terminalView.println("\n2WIRE Dump: Completed.");
}
/*
@@ -161,12 +201,14 @@ void TwoWireController::handleConfig() {
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");
+ terminalView.println("Unknown 2Wire command. Usage:");
+ terminalView.println(" config");
+ terminalView.println(" sniff");
+ terminalView.println(" smartcard probe");
+ terminalView.println(" smartcard security");
+ terminalView.println(" smartcard dump");
+ terminalView.println(" [0xAB r:4] Instruction syntax [NYI]");
+
}
/*
@@ -175,13 +217,14 @@ Ensure Configuration
void TwoWireController::ensureConfigured() {
if (!configured) {
handleConfig();
- } else {
- twoWireService.configure(
- state.getTwoWireClkPin(),
- state.getTwoWireIoPin(),
- state.getTwoWireRstPin()
- );
- }
+ configured = true;
+ }
+
+ twoWireService.configure(
+ state.getTwoWireClkPin(),
+ state.getTwoWireIoPin(),
+ state.getTwoWireRstPin()
+ );
}
diff --git a/src/Controllers/TwoWireController.h b/src/Controllers/TwoWireController.h
index 2cf53c73..7068d461 100644
--- a/src/Controllers/TwoWireController.h
+++ b/src/Controllers/TwoWireController.h
@@ -27,18 +27,31 @@ public:
void ensureConfigured();
private:
- ITerminalView& terminalView; // Terminal output
- IInput& terminalInput; // User input
- UserInputManager& userInputManager;
- TwoWireService& twoWireService;
- GlobalState& state = GlobalState::getInstance();
- bool configured = false; // Init state
+ // Sniff 2wire bus
+ void handleSniff();
+
+ // Smartcard commands
+ void handleSmartCard(const TerminalCommand& cmd);
+
+ // Perform ATR and decode it
+ void handleSmartCardProbe();
+
+ // Display smartcard security memory
+ void handleSmartCardSecurity();
- // Command handlers
- void handleSec();
- void handleATR();
+ // Dump the 256 bytes content of main memory + sec/prt memory
+ void handleSmartCardDump();
+
+ // User pin configuration
void handleConfig();
+
+ // Show available commands
void handleHelp();
- void handleTest();
- void handleDump();
+
+ ITerminalView& terminalView;
+ IInput& terminalInput;
+ UserInputManager& userInputManager;
+ TwoWireService& twoWireService;
+ GlobalState& state = GlobalState::getInstance();
+ bool configured = false;
};
diff --git a/src/Controllers/UtilityController.cpp b/src/Controllers/UtilityController.cpp
index 4f040546..3cd8656c 100644
--- a/src/Controllers/UtilityController.cpp
+++ b/src/Controllers/UtilityController.cpp
@@ -307,11 +307,14 @@ void UtilityController::handleHelp() {
terminalView.println("");
terminalView.println(" 7. 2WIRE:");
terminalView.println(" sniff - View traffic");
- terminalView.println(" [..] - Instruction syntax");
+ terminalView.println(" smartcard probe - Detect and decode ATR");
+ terminalView.println(" smartcard security - Show security memory");
+ terminalView.println(" smartcard dump - Dump full memory");
+ terminalView.println(" config - Configure settings");
terminalView.println("");
terminalView.println(" 8. 3WIRE:");
- terminalView.println(" [..] - Instruction syntax");
+ terminalView.println(" [NYI] - Not Yet Implemented");
terminalView.println("");
terminalView.println(" 9. DIO:");
diff --git a/src/Services/TwoWireService.cpp b/src/Services/TwoWireService.cpp
index 5e7d0135..37d4ad5c 100644
--- a/src/Services/TwoWireService.cpp
+++ b/src/Services/TwoWireService.cpp
@@ -44,7 +44,7 @@ void TwoWireService::setCLK(bool level) {
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);
+ gpio_set_pull_mode((gpio_num_t)ioPin, GPIO_FLOATING);
} else {
gpio_set_pull_mode((gpio_num_t)ioPin, GPIO_FLOATING);
gpio_set_direction((gpio_num_t)ioPin, GPIO_MODE_OUTPUT);
@@ -71,8 +71,6 @@ void TwoWireService::writeBit(bool bit) {
}
bool TwoWireService::readBit() {
- setIO(true);
- delayMicroseconds(5);
setCLK(true);
delayMicroseconds(5);
bool bit = readIO();
@@ -115,7 +113,6 @@ void TwoWireService::sendStop() {
}
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);
@@ -137,33 +134,120 @@ void TwoWireService::sendClocks(uint16_t ticks) {
}
}
-std::vector<uint8_t> TwoWireService::performATR() {
- std::vector<uint8_t> atr;
+std::vector<uint8_t> TwoWireService::performSmartCardAtr() {
+ // dummy tick to 'load' the clock, avoiding us delay on first call
+ setCLK(true);
+ delayMicroseconds(5);
+ setCLK(false);
+ // Start ATR
+ setIO(false); // IO to output
setRST(false); // RST LOW
delay(1); // wait
-
setRST(true); // RST release
- pulseClock(); // tick clock
-
- delayMicroseconds(50);
+ pulseClock(); // Clock tick
+ delayMicroseconds(50); // wait
setRST(false); // RST LOW
+ setIO(true); // IO to input
- // Read 4 bytes lsb first
return readResponse(4);
}
-void TwoWireService::testReadSecurityMemory() {
- Serial.println("==> Reading SLE4442 security memory (0x31 0x00 0x00)");
+std::string TwoWireService::parseSmartCardAtr(const std::vector<uint8_t>& atr) {
+ char line[64];
+ std::string out;
+
+ if (atr.size() < 4) {
+ snprintf(line, sizeof(line), "ATR too short (%d bytes)\r\n", (int)atr.size());
+ return line;
+ }
+
+ const sle44xx_atr_t* head = reinterpret_cast<const sle44xx_atr_t*>(atr.data());
+
+ snprintf(line, sizeof(line), " ATR: 0x%02X 0x%02X 0x%02X 0x%02X\r\n", atr[0], atr[1], atr[2], atr[3]);
+ out += line;
+
+ snprintf(line, sizeof(line), " Protocol Type: %s (%d)\r\n",
+ (head->protocol_type == 0b1010) ? "S" : "unknown",
+ head->protocol_type);
+ out += line;
+
+ out += parseSmartCardStructureIdentifier(head->structure_identifier);
+
+ out += " Read Mode: ";
+ out += head->read_with_defined_length ? "Defined Length\r\n" : "Read to End\r\n";
+
+ if (head->data_units == 0b0000) {
+ out += " Data Units: Undefined\r\n";
+ } else {
+ int size = 1 << (head->data_units + 6);
+ snprintf(line, sizeof(line), " Data Units: %d\r\n", size);
+ out += line;
+ }
+
+ int bit_len = 1 << head->data_units_bits;
+ snprintf(line, sizeof(line), " Data Unit Bit Length: %d\r\n", bit_len);
+ out += line;
+
+ return out;
+}
+
+std::string TwoWireService::parseSmartCardStructureIdentifier(uint8_t id) {
+ std::string out = " Structure Identifier: ";
+ switch (id) {
+ case 0b000:
+ out += "Reserved for ISO/IEC Use\r\n";
+ break;
+ case 0b010:
+ out += "Standard Memory Structure (Type 1)\r\n";
+ break;
+ case 0b110:
+ out += "Proprietary Memory\r\n";
+ break;
+ default:
+ out += "Application-Specific\r\n";
+ break;
+ }
+ return out;
+}
+
+uint8_t TwoWireService::parseSmartCardRemainingAttempts(uint8_t statusByte) {
+ resetSmartCard();
+ uint8_t attemptsBits = statusByte & 0x07;
+ int attempts = 0;
+ for (int i = 0; i < 3; ++i) {
+ if (attemptsBits & (1 << i)) {
+ attempts++;
+ }
+ }
+ return attempts;
+}
+
+std::vector<uint8_t> TwoWireService::dumpSmartCardFullMemory() {
+ resetSmartCard();
+ std::vector<uint8_t> dump;
+
+ // Main memory 256 bytes
+ sendCommand(0x30, 0x00, 0x00);
+ for (int i = 0; i < 256; ++i) {
+ dump.push_back(readByte());
+ }
+ // Security memory 4 bytes
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);
+ for (int i = 0; i < 4; ++i) {
+ dump.push_back(readByte());
+ }
+
+ // Protection memory 4 bytes
+ sendCommand(0x34, 0x00, 0x00);
+ for (int i = 0; i < 4; ++i) {
+ dump.push_back(readByte());
}
+
+ return dump; // total: 264 bytes
+}
+
+void TwoWireService::resetSmartCard() {
+ sendClocks(256);
}
diff --git a/src/Services/TwoWireService.h b/src/Services/TwoWireService.h
index 092b82c5..4c0dfc42 100644
--- a/src/Services/TwoWireService.h
+++ b/src/Services/TwoWireService.h
@@ -5,30 +5,43 @@
class TwoWireService {
public:
+ typedef struct {
+ uint8_t protocol_type : 4;
+ uint8_t structure_identifier : 4;
+
+ uint8_t read_with_defined_length : 1;
+ uint8_t data_units_bits : 3;
+ uint8_t data_units : 4;
+ } sle44xx_atr_t;
+
void configure(uint8_t clkPin, uint8_t ioPin, uint8_t rstPin);
void end();
-
+
void setRST(bool level);
void setCLK(bool level);
void setIO(bool level);
bool readIO();
-
+
void pulseClock();
-
+ void sendClocks(uint16_t ticks);
+
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();
-
+
+ // Smartcard
+ std::vector<uint8_t> performSmartCardAtr();
+ std::string parseSmartCardAtr(const std::vector<uint8_t>& atr);
+ uint8_t parseSmartCardRemainingAttempts(uint8_t statusByte);
+ std::string parseSmartCardStructureIdentifier(uint8_t id);
+ std::vector<uint8_t> dumpSmartCardFullMemory();
+ void resetSmartCard();
private:
uint8_t clkPin;
uint8_t ioPin;