Skip to content

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

fix ibutton write and create copy ibutton cmd

Geod2bdfae5de34
diff --git a/src/Controllers/OneWireController.cpp b/src/Controllers/OneWireController.cpp
index f8159713..8e3ba778 100644
--- a/src/Controllers/OneWireController.cpp
+++ b/src/Controllers/OneWireController.cpp
@@ -13,37 +13,15 @@ OneWireController::OneWireController(ITerminalView& terminalView, IInput& termin
Entry point for command
*/
void OneWireController::handleCommand(const TerminalCommand& command) {
- if (command.getRoot() == "scan") {
- handleScan();
- }
-
- else if (command.getRoot() == "ping") {
- handlePing();
- }
-
- else if (command.getRoot() == "sniff") {
- handleSniff();
- }
-
- else if (command.getRoot() == "read") {
- handleRead();
- }
-
- else if (command.getRoot() == "write") {
- handleWrite(command);
- }
-
- else if (command.getRoot() == "temp") {
- handleTemperature();
- }
-
- else if (command.getRoot() == "config") {
- handleConfig();
- }
-
- else {
- handleHelp();
- }
+ if (command.getRoot() == "scan") handleScan();
+ else if (command.getRoot() == "ping") handlePing();
+ else if (command.getRoot() == "sniff") handleSniff();
+ else if (command.getRoot() == "read") handleRead();
+ else if (command.getRoot() == "write") handleWrite(command);
+ else if (command.getRoot() == "copy") handleCopy(command);
+ else if (command.getRoot() == "temp") handleTemperature();
+ else if (command.getRoot() == "config") handleConfig();
+ else handleHelp();
}
/*
@@ -227,7 +205,7 @@ void OneWireController::handleWrite(const TerminalCommand& cmd) {
ID Write
*/
void OneWireController::handleIdWrite(std::vector<uint8_t> idBytes) {
- const int maxRetries = 3;
+ const int maxRetries = 5;
int attempt = 0;
bool success = false;
@@ -236,22 +214,24 @@ void OneWireController::handleIdWrite(std::vector<uint8_t> idBytes) {
// Wait detection
while (!oneWireService.reset()) {
delay(1);
- if (terminalInput.readChar() == '\n') {
- terminalView.println("OneWire Write: Aborted by user.");
- return;
+ auto key = terminalInput.readChar();
+ if (key == '\r' || key == '\n') {
+ terminalView.println("");
+ terminalView.println("OneWire Write: Stopped by user.");
+ break;
}
}
- // Try to write and verify 3 times
+ // Try to write and verify X times
while (attempt < maxRetries && !success) {
attempt++;
terminalView.println("Attempt " + std::to_string(attempt) + "...");
- // Ecriture
+ // Write
oneWireService.writeRw1990(state.getOneWirePin(), idBytes.data(), idBytes.size());
delay(50);
- // Read ID
+ // Read ID to verify
uint8_t buffer[8];
if (!oneWireService.reset()) continue;
oneWireService.write(0x33); // Read ROM
@@ -261,22 +241,16 @@ void OneWireController::handleIdWrite(std::vector<uint8_t> idBytes) {
if (memcmp(buffer, idBytes.data(), 7) != 0) {
terminalView.println("Mismatch in ROM ID bytes.");
continue;
- }
-
- // CRC error
- uint8_t crc = oneWireService.crc8(buffer, 7);
- if (crc != buffer[7]) {
- terminalView.println("CRC error after write.");
- continue;
- }
+ }
success = true;
+ break;
}
if (success) {
terminalView.println("OneWire Write: ID write successful.");
} else {
- terminalView.println("OneWire Write: Failed after 3 attempts.");
+ terminalView.println("OneWire Write: Failed to write.");
}
}
@@ -284,7 +258,7 @@ void OneWireController::handleIdWrite(std::vector<uint8_t> idBytes) {
Scratchpad Write
*/
void OneWireController::handleScratchpadWrite(std::vector<uint8_t> scratchpadBytes) {
- const int maxRetries = 3;
+ const int maxRetries = 5;
int attempt = 0;
bool success = false;
@@ -292,15 +266,15 @@ void OneWireController::handleScratchpadWrite(std::vector<uint8_t> scratchpadByt
// Wait for device presence
while (!oneWireService.reset()) {
- delay(1);
auto c = terminalInput.readChar();
if (c == '\n' || c == '\r') {
terminalView.println("Aborted by user.");
return;
}
+ delay(1);
}
- // Try up to 3 times
+ // Try up to X times
while (attempt < maxRetries && !success) {
attempt++;
terminalView.println("Attempt " + std::to_string(attempt) + "...");
@@ -346,6 +320,68 @@ void OneWireController::handleScratchpadWrite(std::vector<uint8_t> scratchpadByt
}
}
+/*
+Copy
+*/
+void OneWireController::handleCopy(const TerminalCommand& command) {
+ if (command.getSubcommand() == "ibutton") {
+ handleIdCopy();
+ } else {
+ terminalView.println("OneWire Copy: Invalid syntax. Use:");
+ terminalView.println(" copy ibutton");
+ }
+}
+
+/*
+ID Copy (ibutton)
+*/
+void OneWireController::handleIdCopy() {
+ terminalView.println("OneWire Copy: Insert source tag... Press [ENTER] to stop\n");
+ uint8_t id[8];
+
+ // Wait source tag
+ while (!oneWireService.reset()) {
+ auto key = terminalInput.readChar();
+ if (key == '\r' || key == '\n') {
+ terminalView.println("");
+ terminalView.println("OneWire Copy: Stopped by user.");
+ return;
+ }
+ delay(100);
+ }
+
+ // Read
+ terminalView.println("OneWire Read: in progress...");
+ oneWireService.write(0x33); // Read ROM
+ oneWireService.readBytes(id, 8);
+
+ // Print Rom ID
+ std::ostringstream oss;
+ oss << std::uppercase << std::hex << std::setfill('0');
+ for (int i = 0; i < 8; ++i) {
+ oss << std::setw(2) << static_cast<int>(id[i]);
+ if (i < 7) oss << " ";
+ }
+ terminalView.println("ROM ID: " + oss.str());
+
+
+ // Wait target tag
+ terminalView.println("Remove source tag and insert target clone... Press [ENTER] when ready.");
+ while (true) {
+ auto c = terminalInput.readChar();
+ if (c == '\r' || c== '\n') {
+ terminalView.println("");
+ terminalView.println("OneWire Copy: Starting ID write...");
+ break;
+ }
+
+ }
+
+ // Write readed ID to target tag
+ std::vector<uint8_t> idVec(id, id + 8);
+ handleIdWrite(idVec);
+}
+
/*
Config
*/
@@ -521,6 +557,7 @@ void OneWireController::handleHelp() {
terminalView.println(" read");
terminalView.println(" write id <8 bytes>");
terminalView.println(" write sp <8 bytes>");
+ terminalView.println(" copy ibutton");
terminalView.println(" temp");
terminalView.println(" config");
terminalView.println(" raw instructions, [0X33 r:8] ...");
diff --git a/src/Controllers/OneWireController.h b/src/Controllers/OneWireController.h
index fb23db6e..4433a7f7 100644
--- a/src/Controllers/OneWireController.h
+++ b/src/Controllers/OneWireController.h
@@ -65,6 +65,12 @@ private:
// Write a ROM ID to a device (e.g. iButton emulator)
void handleIdWrite(std::vector<uint8_t> idBytes);
+ // Copy 1wire data
+ void handleCopy(const TerminalCommand& command);
+
+ // Read a source ibutton to copy its ID on another one
+ void handleIdCopy();
+
// Write to scratchpad memory
void handleScratchpadWrite(std::vector<uint8_t> scratchpadBytes);
diff --git a/src/Controllers/UtilityController.cpp b/src/Controllers/UtilityController.cpp
index 20054a76..9482ffe6 100644
--- a/src/Controllers/UtilityController.cpp
+++ b/src/Controllers/UtilityController.cpp
@@ -279,6 +279,7 @@ void UtilityController::handleHelp() {
terminalView.println(" write id <8 bytes> - Write ibutton ID");
terminalView.println(" write sp <8 bytes> - Write scratchpad");
terminalView.println(" temp - Read temperature");
+ terminalView.println(" copy ibutton - Copy ibutton tag");
terminalView.println(" config - Configure settings");
terminalView.println(" [0xAA r:8] ... - Instruction syntax");
diff --git a/src/Services/OneWireService.cpp b/src/Services/OneWireService.cpp
index abc7e4cb..59bd8458 100644
--- a/src/Services/OneWireService.cpp
+++ b/src/Services/OneWireService.cpp
@@ -24,21 +24,54 @@ void OneWireService::writeBytes(const uint8_t* data, uint8_t len) {
}
void OneWireService::writeRw1990(uint8_t pin, uint8_t* data, size_t len) {
- // Reset + Write Serial
+ // Based on: https://github.com/ArminJo/iButtonProgrammer/blob/master/iButtonProgrammer.ino
+ // @ArminJo
+
+ // Configure for bit bang
+ pinMode(pin, INPUT_PULLUP);
+ delay(10);
+
oneWire->reset();
- oneWire->write(0xD5);
+ delay(1);
- // Write each byte with 10ms delay to power the eeprom
+ // Read is necessary before write
+ oneWire->write(0x33);
for (uint8_t i = 0; i < 8; i++) {
- uint8_t b = data[i];
+ oneWire->read();
+ }
+ delay(5);
+
+ // Reset for write
+ oneWire->reset();
+ delay(1);
+
+ // Write mode
+ oneWire->write(0xD5);
+
+ // Bit-bang each byte
+ for (size_t byteIndex = 0; byteIndex < len; byteIndex++) {
+ uint8_t byte = data[byteIndex];
for (uint8_t bit = 0; bit < 8; bit++) {
- bool v = b & 0x01;
- oneWire->write(v ? 0xFF : 0x00);
- delay(10);
- b >>= 1;
+ bool bitValue = byte & 0x01;
+
+ if (bitValue) {
+ pinMode(pin, OUTPUT);
+ digitalWrite(pin, LOW);
+ delayMicroseconds(60);
+ pinMode(pin, INPUT);
+ delay(10);
+ } else {
+ pinMode(pin, OUTPUT);
+ digitalWrite(pin, LOW);
+ pinMode(pin, INPUT);
+ delay(10);
+ }
+
+ byte >>= 1;
}
}
+ delay(5);
oneWire->reset();
}