A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol
C arduinobluetoothcan-busdebuggingeepromesp32flipperzerogpiohardware-hackingi2cjtagopenocdprotocolspwmradiorfidspisubghzuartwifi
implement i2c_sniffer
Geo6fcc82d60276
diff --git a/src/Controllers/I2cController.cpp b/src/Controllers/I2cController.cppindex c409f934..66edc63d 100644--- a/src/Controllers/I2cController.cpp+++ b/src/Controllers/I2cController.cpp@@ -78,55 +78,35 @@ void I2cController::handleScan() {/*Sniff-*/+*/void I2cController::handleSniff() {- terminalView.println("I2C Sniff Mode: capturing raw bytes.");- terminalView.println("Press ENTER to stop.\n");- terminalView.println("");-- uint8_t sclPin = state.getI2cSclPin();- uint8_t sdaPin = state.getI2cSdaPin();- uint32_t freq = state.getI2cFrequency();- uint32_t delayUs = 500000 / freq;- if (delayUs < 1) delayUs = 1;+ terminalView.println("I2C Sniffer: Listening... Press ENTER to stop.\n");+ i2c_sniffer_begin(state.getI2cSclPin(), state.getI2cSdaPin()); // dont need freq to work+ i2c_sniffer_setup();- uint8_t bitCount = 0;- uint8_t currentByte = 0;- int lastSCL = digitalRead(sclPin);-- unsigned long lastCharCheck = millis();+ std::string line;while (true) {+ char key = terminalInput.readChar();+ if (key == '\r' || key == '\n') break;- lastCharCheck = millis();- char c = terminalInput.readChar();- if (c == '\r' || c == '\n') break;-- int scl = digitalRead(sclPin);- int sda = digitalRead(sdaPin);-- // Detect rising edge on SCL- if (lastSCL == LOW && scl == HIGH) {- currentByte <<= 1;- if (sda) currentByte |= 1;- bitCount++;-- if (bitCount == 8) {- std::stringstream ss;- ss << "0x" << std::hex << std::uppercase << (int)currentByte << " ";- terminalView.print(ss.str());+ while (i2c_sniffer_available()) {+ char c = i2c_sniffer_read();- bitCount = 0;- currentByte = 0;+ if (c == '\n') {+ line += " ";+ terminalView.print(line);+ line.clear();+ } else {+ line += c;}}-- lastSCL = scl;- delayMicroseconds(delayUs);+ delay(5);}- terminalView.println("");- terminalView.println("I2C Sniff: Exited.");+ i2c_sniffer_reset_buffer();+ i2c_sniffer_stop();+ terminalView.println("\n\nI2C Sniffer: Stopped.");}/*diff --git a/src/Controllers/I2cController.h b/src/Controllers/I2cController.hindex ec458f59..3a43b403 100644--- a/src/Controllers/I2cController.h+++ b/src/Controllers/I2cController.h@@ -11,6 +11,9 @@#include "States/GlobalState.h"#include "Transformers/ArgTransformer.h"#include "Managers/UserInputManager.h"+#include "Vendors/i2c_sniffer.h"++struct I2cSniffState;class I2cController {public:@@ -41,7 +44,7 @@ private:// Scan the I2C bus for devicesvoid handleScan();- // Start sniffing I2C traffic+ // Start sniffing I2C traffic passivelyvoid handleSniff();// Read data from an I2C devicediff --git a/src/Vendors/CardWifiSetup.cpp b/src/Vendors/CardWifiSetup.cppindex e960b587..8ac02a30 100644--- a/src/Vendors/CardWifiSetup.cpp+++ b/src/Vendors/CardWifiSetup.cpp@@ -26,7 +26,6 @@ String inputText(const String& prompt, int x, int y) {M5Cardputer.update();if (M5Cardputer.Keyboard.isChange()) {if (M5Cardputer.Keyboard.isPressed()) {- M5Cardputer.Speaker.tone(1437, 20);Keyboard_Class::KeysState status = M5Cardputer.Keyboard.keysState();for (auto i : status.word) {data += i;@@ -87,7 +86,6 @@ String selectWifiNetwork(int numNetworks) {M5Cardputer.update();if (M5Cardputer.Keyboard.isChange()) {if (M5Cardputer.Keyboard.isPressed()) {- M5Cardputer.Speaker.tone(1437, 20);M5Cardputer.Display.fillRect(0, 11, 20, 75, BLACK);Keyboard_Class::KeysState status = M5Cardputer.Keyboard.keysState();diff --git a/src/Vendors/i2c_sniffer.cpp b/src/Vendors/i2c_sniffer.cppnew file mode 100644index 00000000..39c81ac9--- /dev/null+++ b/src/Vendors/i2c_sniffer.cpp@@ -0,0 +1,168 @@+/**+ * @AUTHOR Ákos Szabó (Whitehawk Tailor) - [email protected]+ *+ * This is an I2C sniffer that logs traffic on I2C BUS.+ *+ * It is not part of the I2C BUS. It is neither a Master, nor a Slave and puts no data to the lines.+ * It just listens and logs the communication.+ *+ * Two pins as imput are attached to SDC and SDA lines.+ * Since the I2C communications runs on 400kHz so,+ * the tool that runs this program should be fast.+ * This was tested on an ESP32 bord Heltec WiFi Lora32 v2+ * ESP32 core runs on 240MHz.+ * It means there are 600 ESP32 cycles during one I2C clock tick.+ *+ *+ * The program uses interrupts to detect+ * the raise edge of the SCL - bit transfer+ * the falling edge of SDA if SCL is HIGH- START+ * the raise edge of SDA if SCL id HIGH - STOP+ *+ * In the interrupt routines there is just a few line of code+ * that mainly sets the status and stores the incoming bits.+ * Otherwise the program gets timeout panic in interrupt handler and+ * restart the CPU.+ * https://github.com/WhitehawkTailor/I2C-sniffer/+ */++#include "i2c_sniffer.h"++static uint8_t sniffer_scl_pin = 1;+static uint8_t sniffer_sda_pin = 2;+#define I2C_IDLE 0+#define I2C_TRX 2++static volatile byte i2cStatus = I2C_IDLE;+static uint32_t lastStartMillis = 0;+static volatile byte dataBuffer[9600];+extern volatile uint16_t bufferPoiW = 0;+static uint16_t bufferPoiR = 0;+static volatile byte bitCount = 0;+static volatile uint16_t byteCount = 0;+static volatile byte i2cBitD = 0;+static volatile byte i2cBitD2 = 0;+static volatile byte i2cBitC = 0;+static volatile byte i2cClk = 0;+static volatile byte i2cAck = 0;+static volatile byte i2cCase = 0;+static volatile uint16_t falseStart = 0;+static volatile uint16_t sclUpCnt = 0;+static volatile uint16_t sdaUpCnt = 0;+static volatile uint16_t sdaDownCnt = 0;++void i2c_sniffer_begin(uint8_t scl, uint8_t sda) {+ sniffer_scl_pin = scl;+ sniffer_sda_pin = sda;+}++void IRAM_ATTR i2cTriggerOnRaisingSCL() {+ sclUpCnt++;+ if (i2cStatus == I2C_IDLE) {+ falseStart++;+ }++ i2cBitC = digitalRead(sniffer_sda_pin);+ i2cCase = 0;+ if (bitCount == 8) i2cCase = 1;+ if (bitCount == 7 && byteCount == 0) i2cCase = 2;++ bitCount++;++ switch (i2cCase) {+ case 0:+ dataBuffer[bufferPoiW++] = '0' + i2cBitC;+ break;+ case 1:+ dataBuffer[bufferPoiW++] = (i2cBitC ? '-' : '+');+ byteCount++;+ bitCount = 0;+ break;+ case 2:+ dataBuffer[bufferPoiW++] = (i2cBitC ? 'R' : 'W');+ break;+ }+}++void IRAM_ATTR i2cTriggerOnChangeSDA() {+ do {+ i2cBitD = digitalRead(sniffer_sda_pin);+ i2cBitD2 = digitalRead(sniffer_sda_pin);+ } while (i2cBitD != i2cBitD2);++ if (i2cBitD) {+ i2cClk = digitalRead(sniffer_scl_pin);+ if (i2cStatus != I2C_IDLE && i2cClk == 1) {+ i2cStatus = I2C_IDLE;+ bitCount = 0;+ byteCount = 0;+ bufferPoiW--;+ dataBuffer[bufferPoiW++] = 's';+ dataBuffer[bufferPoiW++] = '\n';+ }+ sdaUpCnt++;+ } else {+ i2cClk = digitalRead(sniffer_scl_pin);+ if (i2cStatus == I2C_IDLE && i2cClk) {+ i2cStatus = I2C_TRX;+ bitCount = 0;+ byteCount = 0;+ dataBuffer[bufferPoiW++] = 'S';+ }+ sdaDownCnt++;+ }+}++static void resetI2cVariable() {+ i2cStatus = I2C_IDLE;+ bufferPoiW = 0;+ bufferPoiR = 0;+ bitCount = 0;+ falseStart = 0;+}++static void processDataBuffer() {+ if (bufferPoiW == bufferPoiR) return;++ uint16_t pw = bufferPoiW;++ for (int i = bufferPoiR; i < pw; i++) {+ bufferPoiR++;+ }++ if (i2cStatus == I2C_IDLE && pw == bufferPoiW) {+ bufferPoiW = 0;+ bufferPoiR = 0;+ }+}++void i2c_sniffer_setup() {+ pinMode(sniffer_scl_pin, INPUT_PULLUP);+ pinMode(sniffer_sda_pin, INPUT_PULLUP);+ resetI2cVariable();+ attachInterrupt(sniffer_scl_pin, i2cTriggerOnRaisingSCL, RISING);+ attachInterrupt(sniffer_sda_pin, i2cTriggerOnChangeSDA, CHANGE);+}++void i2c_sniffer_stop() {+ detachInterrupt(sniffer_scl_pin);+ detachInterrupt(sniffer_sda_pin);+ i2cStatus = I2C_IDLE;+}++bool i2c_sniffer_available() {+ if (i2cStatus == I2C_IDLE) processDataBuffer();+ return bufferPoiR != bufferPoiW;+}++char i2c_sniffer_read() {+ if (bufferPoiR == bufferPoiW) return '\0';+ return dataBuffer[bufferPoiR++];+}++void i2c_sniffer_reset_buffer() {+ bufferPoiW = 0;+ bufferPoiR = 0;+ bitCount = 0;+ byteCount = 0;+}diff --git a/src/Vendors/i2c_sniffer.h b/src/Vendors/i2c_sniffer.hnew file mode 100644index 00000000..f8dda883--- /dev/null+++ b/src/Vendors/i2c_sniffer.h@@ -0,0 +1,48 @@+/**+ * @AUTHOR Ákos Szabó (Whitehawk Tailor) - [email protected]+ *+ * This is an I2C sniffer that logs traffic on I2C BUS.+ *+ * It is not part of the I2C BUS. It is neither a Master, nor a Slave and puts no data to the lines.+ * It just listens and logs the communication.+ *+ * Two pins as imput are attached to SDC and SDA lines.+ * Since the I2C communications runs on 400kHz so,+ * the tool that runs this program should be fast.+ * This was tested on an ESP32 bord Heltec WiFi Lora32 v2+ * ESP32 core runs on 240MHz.+ * It means there are 600 ESP32 cycles during one I2C clock tick.+ *+ *+ * The program uses interrupts to detect+ * the raise edge of the SCL - bit transfer+ * the falling edge of SDA if SCL is HIGH- START+ * the raise edge of SDA if SCL id HIGH - STOP+ *+ * In the interrupt routines there is just a few line of code+ * that mainly sets the status and stores the incoming bits.+ * Otherwise the program gets timeout panic in interrupt handler and+ * restart the CPU.+ * https://github.com/WhitehawkTailor/I2C-sniffer/+ */+#include <Arduino.h>++#pragma once++#ifdef __cplusplus+extern "C" {+#endif++extern volatile uint16_t bufferPoiW;++void i2c_sniffer_begin(uint8_t scl, uint8_t sda);+void i2c_sniffer_setup();+void i2c_sniffer_stop();+void i2c_sniffer_reset();+bool i2c_sniffer_available();+char i2c_sniffer_read();+void i2c_sniffer_reset_buffer();++#ifdef __cplusplus+}+#endif