Skip to content

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

complete smartcard sle44 implementation

Geo532656c3f8eb
diff --git a/src/Controllers/TwoWireController.cpp b/src/Controllers/TwoWireController.cpp
index 34635a71..6893f84a 100644
--- a/src/Controllers/TwoWireController.cpp
+++ b/src/Controllers/TwoWireController.cpp
@@ -10,15 +10,11 @@ TwoWireController::TwoWireController(ITerminalView& terminalView, IInput& termin
Entry point for 2WIRE command
*/
void TwoWireController::handleCommand(const TerminalCommand& cmd) {
- if (cmd.getRoot() == "config") {
- handleConfig();
- } else if (cmd.getRoot() == "sniff") {
- handleSniff();
- } else if (cmd.getRoot() == "smartcard") {
- handleSmartCard(cmd);
- } else {
- handleHelp();
- }
+ if (cmd.getRoot() == "config") handleConfig();
+ else if (cmd.getRoot() == "sniff") handleSniff();
+ else if (cmd.getRoot() == "smartcard") handleSmartCard(cmd);
+ else handleHelp();
+
}
/*
@@ -41,17 +37,23 @@ Smartcard
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 {
+ if (sub == "probe") handleSmartCardProbe();
+ else if (sub == "security") handleSmartCardSecurity();
+ else if (sub == "dump") handleSmartCardDump();
+ else if (sub == "unlock") handleSmartCardUnlock();
+ else if (sub == "psc") handleSmartCardPsc(cmd);
+ else if (sub == "write") handleSmartCardWrite(cmd);
+ else if (sub == "protect") handleSmartCardProtect();
+ else {
terminalView.println("Unknown smartcard subcommand. Usage:");
terminalView.println(" smartcard probe");
terminalView.println(" smartcard security");
terminalView.println(" smartcard dump");
+ terminalView.println(" smartcard unlock");
+ terminalView.println(" smartcard protect");
+ terminalView.println(" smartcard psc [get]");
+ terminalView.println(" smartcard psc set");
+ terminalView.println(" smartcard write");
}
}
@@ -60,6 +62,7 @@ Smartcard Security
*/
void TwoWireController::handleSmartCardSecurity() {
ensureConfigured();
+ twoWireService.resetSmartCard();
terminalView.println("2WIRE Security: Perfoming...\n");
@@ -72,7 +75,7 @@ void TwoWireController::handleSmartCardSecurity() {
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)");
+ terminalView.println("2WIRE Security: ❌ No smartcard detected (response invalid)");
return;
}
@@ -87,7 +90,7 @@ void TwoWireController::handleSmartCardSecurity() {
terminalView.println(" Remaining Unlock Attempts: " + std::to_string(attempts));
}
- terminalView.println("\n2WIRE Security: Completed.");
+ terminalView.println("\n2WIRE Security: ✅ Completed.");
}
/*
@@ -103,7 +106,7 @@ void TwoWireController::handleSmartCardProbe() {
// Validate
if (atr.empty() || atr[0] == 0x00 || atr[0] == 0xFF) {
- terminalView.println("2WIRE ATR: No response received from smartcard");
+ terminalView.println("2WIRE ATR: ❌ No response received from smartcard");
return;
}
@@ -113,19 +116,23 @@ void TwoWireController::handleSmartCardProbe() {
ss << "0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(b) << " ";
}
terminalView.println(decodedAtr);
- terminalView.println("2WIRE ATR: Completed.");
+
+ twoWireService.resetSmartCard();
+ terminalView.println("2WIRE ATR: ✅ Completed.");
}
/*
Smartcard Dump
*/
void TwoWireController::handleSmartCardDump() {
+ twoWireService.resetSmartCard();
+ delay(10);
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.");
+ terminalView.println("\n2WIRE Dump: ❌ Failed, unexpected size.");
return;
}
@@ -133,7 +140,7 @@ void TwoWireController::handleSmartCardDump() {
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");
+ terminalView.println("\n2WIRE Dump: ❌ Smartcard is blank or no smartcard detected");
return;
}
@@ -169,7 +176,132 @@ void TwoWireController::handleSmartCardDump() {
}
terminalView.println(prt.str());
- terminalView.println("\n2WIRE Dump: Completed.");
+ twoWireService.resetSmartCard();
+ terminalView.println("\n2WIRE Dump: ✅ Completed.");
+}
+
+/*
+Smartcard Protect
+*/
+void TwoWireController::handleSmartCardProtect() {
+ twoWireService.resetSmartCard();
+ terminalView.println("⚠️ The smartcard will PERMANENTLY disable writes to main memory.");
+ bool confirm = userInputManager.readYesNo("Are you sure you want to lock it PERMANENTLY?", false);
+ if (!confirm) {
+ terminalView.println("\n❌ Lock cancelled.");
+ return;
+ }
+
+ bool ok = twoWireService.protectSmartCard();
+ if (ok) terminalView.println("\n✅ Smartcard successfully locked (writes disabled).");
+ else terminalView.println("\n❌ Failed to lock smartcard.");
+}
+
+/*
+Smartcard Unlock
+*/
+void TwoWireController::handleSmartCardUnlock() {
+ twoWireService.resetSmartCard();
+ terminalView.println("2WIRE Unlock: Attempting unlock procedure...");
+
+ std::string psc = userInputManager.readValidatedHexString("Enter PSC (PIN Code) (ex: 123456)", 3);
+ if (psc.length() != 6) {
+ terminalView.println("PSC (PIN Code) must be 3 bytes (6 hex chars)");
+ return;
+ }
+
+ uint8_t pscBytes[3] = {
+ static_cast<uint8_t>(std::stoi(psc.substr(0, 2), nullptr, 16)),
+ static_cast<uint8_t>(std::stoi(psc.substr(2, 2), nullptr, 16)),
+ static_cast<uint8_t>(std::stoi(psc.substr(4, 2), nullptr, 16))
+ };
+
+ bool success = twoWireService.unlockSmartCard(pscBytes);
+
+ if (success) {
+ terminalView.println("\n✅ Unlock successful: Access to main memory granted.");
+ } else {
+ terminalView.println("\n❌ Unlock failed: PSC incorrect or no attempts remaining.");
+ }
+
+ // Number of attempts left
+ auto secAfter = twoWireService.readSmartCardSecurityMemory();
+ if (!secAfter.empty()) {
+ uint8_t attempts = twoWireService.parseSmartCardRemainingAttempts(secAfter[0]);
+ terminalView.println("→ Remaining Attempts: " + std::to_string(attempts));
+ }
+}
+
+/*
+Smartcard PSC (PIN Code)
+*/
+void TwoWireController::handleSmartCardPsc(const TerminalCommand& cmd) {
+ twoWireService.resetSmartCard();
+ std::string arg = cmd.getArgs();
+
+ if (arg.empty()) {
+ arg = "get"; // Default to "get" if no argument provided
+ }
+
+ // GET PSC
+ if (arg == "get") {
+ uint8_t psc[3];
+ bool ok = twoWireService.getSmartCardPSC(psc);
+ if (ok) {
+ terminalView.println("ℹ️ Note: The PSC (PIN Code) can only be read if the smartcard is unlocked.");
+ std::stringstream ss;
+ ss << "🔐 Current PSC (PIN Code): ";
+ for (int i = 0; i < 3; ++i)
+ ss << std::hex << std::setw(2) << std::setfill('0') << (int)psc[i] << " ";
+ terminalView.println(ss.str());
+
+ } else {
+ terminalView.println("\n❌ Failed to read PSC (PIN Code).");
+ }
+
+ // SET PSC
+ } else if (arg == "set") {
+ std::string newPscStr = userInputManager.readValidatedHexString("Enter new 6 hex digit PSC (PIN Code)", 3);
+ if (newPscStr.length() != 6) {
+ terminalView.println("PSC (PIN Code) must be 6 hex digits");
+ return;
+ }
+
+ uint8_t newPsc[3] = {
+ static_cast<uint8_t>(std::stoi(newPscStr.substr(0, 2), nullptr, 16)),
+ static_cast<uint8_t>(std::stoi(newPscStr.substr(2, 2), nullptr, 16)),
+ static_cast<uint8_t>(std::stoi(newPscStr.substr(4, 2), nullptr, 16))
+ };
+
+ bool ok = twoWireService.updateSmartCardPSC(newPsc);
+ if (ok) {
+ terminalView.println("\n✅ PSC (PIN Code) updated successfully.");
+ } else {
+ terminalView.println("\nℹ️ Note: The PSC (PIN Code) can only be set if the smartcard is unlocked.");
+ terminalView.println("❌ Failed to update PSC (PIN Code).");
+ }
+ }
+}
+
+// Smartcard Write
+void TwoWireController::handleSmartCardWrite(const TerminalCommand&) {
+ twoWireService.resetSmartCard();
+
+ int offset = userInputManager.readValidatedUint8("Enter offset (0–255 or 0x..)", 0);
+ if (offset < 0 || offset >= 256) {
+ terminalView.println("\n❌ Invalid offset (must be between 0 and 255).");
+ return;
+ }
+
+ int data = userInputManager.readValidatedUint8("Enter data byte (0–255 or 0x..)", 0);
+ if (data < 0 || data > 0xFF) {
+ terminalView.println("\n❌ Invalid data byte.");
+ return;
+ }
+
+ bool ok = twoWireService.writeSmartCardMainMemory(static_cast<uint8_t>(offset), static_cast<uint8_t>(data));
+ if (ok) terminalView.println("\n✅ Write successful.");
+ else terminalView.println("\n❌ Write failed.");
}
/*
@@ -189,7 +321,6 @@ void TwoWireController::handleConfig() {
state.setTwoWireRstPin(rst);
twoWireService.configure(clk, io, rst);
- configured = true;
terminalView.println("2WIRE configuration applied.\n");
}
@@ -200,12 +331,16 @@ Help
void TwoWireController::handleHelp() {
terminalView.println("Unknown 2Wire command. Usage:");
terminalView.println(" config");
- terminalView.println(" sniff");
+ terminalView.println(" sniff [NYI]");
terminalView.println(" smartcard probe");
terminalView.println(" smartcard security");
terminalView.println(" smartcard dump");
+ terminalView.println(" smartcard write");
+ terminalView.println(" smartcard unlock");
+ terminalView.println(" smartcard protect");
+ terminalView.println(" smartcard psc [get]");
+ terminalView.println(" smartcard psc set");
terminalView.println(" [0xAB r:4] Instruction syntax [NYI]");
-
}
/*
diff --git a/src/Controllers/TwoWireController.h b/src/Controllers/TwoWireController.h
index 7068d461..58f04cf5 100644
--- a/src/Controllers/TwoWireController.h
+++ b/src/Controllers/TwoWireController.h
@@ -33,15 +33,27 @@ private:
// Smartcard commands
void handleSmartCard(const TerminalCommand& cmd);
- // Perform ATR and decode it
+ // Perform a smartcard Answer To Reset and decode it
void handleSmartCardProbe();
- // Display smartcard security memory
+ // Display smartcard security memory (left attempts)
void handleSmartCardSecurity();
// Dump the 256 bytes content of main memory + sec/prt memory
void handleSmartCardDump();
+ // Unlock smartcard memory with the PSC (PIN code)
+ void handleSmartCardUnlock();
+
+ // Protect smartcard memory permanently
+ void handleSmartCardProtect();
+
+ // PSC (Pin code) commands
+ void handleSmartCardPsc(const TerminalCommand& cmd);
+
+ // Write a byte at given address in main memory
+ void handleSmartCardWrite(const TerminalCommand& cmd);
+
// User pin configuration
void handleConfig();
diff --git a/src/Controllers/UtilityController.cpp b/src/Controllers/UtilityController.cpp
index 9ca756c2..816a19de 100644
--- a/src/Controllers/UtilityController.cpp
+++ b/src/Controllers/UtilityController.cpp
@@ -339,6 +339,11 @@ void UtilityController::handleHelp() {
terminalView.println(" smartcard probe - Detect and decode ATR");
terminalView.println(" smartcard security - Show security memory");
terminalView.println(" smartcard dump - Dump full memory");
+ terminalView.println(" smartcard psc [get] - Read current PSC (PIN Code)");
+ terminalView.println(" smartcard psc set - Set a new PSC (PIN Code)");
+ terminalView.println(" smartcard unlock - Unlock card with PIN Code");
+ terminalView.println(" smartcard write - Write to main memory");
+ terminalView.println(" smartcard protect - Protect card memory");
terminalView.println(" config - Configure settings");
terminalView.println(" [0xAB r:4] - Instruction syntax");
diff --git a/src/Services/TwoWireService.cpp b/src/Services/TwoWireService.cpp
index 37d4ad5c..7a6ca273 100644
--- a/src/Services/TwoWireService.cpp
+++ b/src/Services/TwoWireService.cpp
@@ -134,6 +134,14 @@ void TwoWireService::sendClocks(uint16_t ticks) {
}
}
+bool TwoWireService::waitIOHigh(uint32_t maxTicks) {
+ for (uint32_t i = 0; i < maxTicks; ++i) {
+ pulseClock();
+ if (readIO()) return true;
+ }
+ return false; // timeout
+}
+
std::vector<uint8_t> TwoWireService::performSmartCardAtr() {
// dummy tick to 'load' the clock, avoiding us delay on first call
setCLK(true);
@@ -251,3 +259,141 @@ std::vector<uint8_t> TwoWireService::dumpSmartCardFullMemory() {
void TwoWireService::resetSmartCard() {
sendClocks(256);
}
+
+void TwoWireService::updateSmartCardSecurityAttempts(uint8_t pattern) {
+ sendCommand(0x39, 0x00, pattern);
+ resetSmartCard();
+}
+
+void TwoWireService::compareSmartCardVerificationData(uint8_t address, uint8_t value) {
+ sendCommand(0x33, address, value);
+ resetSmartCard();
+}
+
+void TwoWireService::writeSmartCardSecurityMemory(uint8_t address, uint8_t value) {
+ sendCommand(0x39, address, value);
+ resetSmartCard();
+}
+
+void TwoWireService::writeSmartCardProtectionMemory(uint8_t address, uint8_t value) {
+ sendCommand(0x3C, address, value);
+ resetSmartCard();
+}
+
+bool TwoWireService::writeSmartCardMainMemory(uint8_t address, uint8_t value) {
+ sendCommand(0x38, address, value);
+ resetSmartCard();
+
+ // Read to verify
+ auto readBack = readSmartCardMainMemory(address, 1);
+ return !readBack.empty() && readBack[0] == value;
+}
+
+std::vector<uint8_t> TwoWireService::readSmartCardMainMemory(uint8_t startAddress, uint16_t length) {
+ std::vector<uint8_t> buf;
+ sendCommand(0x30, startAddress, 0x00);
+ for (uint16_t i = 0; i < length; ++i) {
+ buf.push_back(readByte());
+ }
+ return buf;
+}
+
+std::vector<uint8_t> TwoWireService::readSmartCardSecurityMemory() {
+ sendCommand(0x31, 0x00, 0x00);
+ return readResponse(4);
+}
+
+std::vector<uint8_t> TwoWireService::readSmartCardProtectionMemory() {
+ sendCommand(0x34, 0x00, 0x00);
+ return readResponse(4);
+}
+
+bool TwoWireService::updateSmartCardPSC(const uint8_t psc[3]) {
+ writeSmartCardSecurityMemory(1, psc[0]);
+ writeSmartCardSecurityMemory(2, psc[1]);
+ writeSmartCardSecurityMemory(3, psc[2]);
+
+ std::vector<uint8_t> secmem = readSmartCardSecurityMemory();
+ if (secmem.size() < 4) {
+ return false;
+ }
+
+ if (secmem[1] != psc[0] || secmem[2] != psc[1] || secmem[3] != psc[2]) {
+ return false;
+ }
+ return true;
+}
+
+bool TwoWireService::getSmartCardPSC(uint8_t out_psc[3]) {
+ std::vector<uint8_t> secmem = readSmartCardSecurityMemory();
+ if (secmem.size() < 4) {
+ return false;
+ }
+ out_psc[0] = secmem[1];
+ out_psc[1] = secmem[2];
+ out_psc[2] = secmem[3];
+ return true;
+}
+
+bool TwoWireService::protectSmartCard() {
+ uint32_t value = 0xFFFFFFFF;
+
+ for (uint8_t i = 0; i < 4; ++i) {
+ uint8_t byte = (value >> (8 * i)) & 0xFF;
+ writeSmartCardProtectionMemory(i, byte);
+ }
+
+ // Lire back pour vérification
+ auto check = readSmartCardProtectionMemory();
+ if (check.size() != 4) return false;
+
+ uint32_t readValue =
+ (check[3] << 24) |
+ (check[2] << 16) |
+ (check[1] << 8) |
+ (check[0]);
+
+ return (readValue & value) == value;
+}
+
+bool TwoWireService::unlockSmartCard(const uint8_t psc[3]) {
+ // Read sec mem
+ std::vector<uint8_t> secmem = readSmartCardSecurityMemory();
+ if (secmem.size() < 1) {
+ return false;
+ }
+
+ // Verify attempts
+ uint8_t sec = secmem[0];
+ uint8_t pattern = 0;
+
+ if (sec & 0b100) {
+ pattern = 0b011;
+ } else if (sec & 0b010) {
+ pattern = 0b101;
+ } else if (sec & 0b001) {
+ pattern = 0b110;
+ } else {
+ // No attempts left
+ return false;
+ }
+
+ // Update security attempts
+ updateSmartCardSecurityAttempts(pattern);
+
+ // Send the PSC
+ compareSmartCardVerificationData(1, psc[0]);
+ compareSmartCardVerificationData(2, psc[1]);
+ compareSmartCardVerificationData(3, psc[2]);
+
+ // Reinitialize security memory
+ writeSmartCardSecurityMemory(0, 0xFF);
+
+ // Read back to verify
+ std::vector<uint8_t> secmemAfter = readSmartCardSecurityMemory();
+ if (secmemAfter.size() < 1 || secmemAfter[0] != 0x07) {
+ return false;
+ }
+
+ return true;
+}
\ No newline at end of file
diff --git a/src/Services/TwoWireService.h b/src/Services/TwoWireService.h
index 4c0dfc42..87b2332e 100644
--- a/src/Services/TwoWireService.h
+++ b/src/Services/TwoWireService.h
@@ -6,9 +6,8 @@
class TwoWireService {
public:
typedef struct {
- uint8_t protocol_type : 4;
- uint8_t structure_identifier : 4;
-
+ 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;
@@ -24,6 +23,7 @@ public:
void pulseClock();
void sendClocks(uint16_t ticks);
+ bool waitIOHigh(uint32_t maxTicks);
void writeBit(bool bit);
bool readBit();
@@ -42,6 +42,19 @@ public:
std::string parseSmartCardStructureIdentifier(uint8_t id);
std::vector<uint8_t> dumpSmartCardFullMemory();
void resetSmartCard();
+ void updateSmartCardSecurityAttempts(uint8_t pattern);
+ void compareSmartCardVerificationData(uint8_t address, uint8_t value);
+ void writeSmartCardSecurityMemory(uint8_t address, uint8_t value);
+ void writeSmartCardProtectionMemory(uint8_t address, uint8_t value);
+ bool writeSmartCardMainMemory(uint8_t address, uint8_t value);
+ std::vector<uint8_t> readSmartCardSecurityMemory();
+ std::vector<uint8_t> readSmartCardMainMemory(uint8_t startAddress, uint16_t length);
+ std::vector<uint8_t> readSmartCardProtectionMemory();
+ bool protectSmartCard();
+ bool unlockSmartCard(const uint8_t psc[3]);
+ bool updateSmartCardPSC(const uint8_t psc[3]);
+ bool getSmartCardPSC(uint8_t out_psc[3]);
+
private:
uint8_t clkPin;
uint8_t ioPin;