Skip to content

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

add buetooth sniffer

Geo72339f6512b7
diff --git a/src/Controllers/BluetoothController.cpp b/src/Controllers/BluetoothController.cpp
index afb42f04..8b94c37b 100644
--- a/src/Controllers/BluetoothController.cpp
+++ b/src/Controllers/BluetoothController.cpp
@@ -25,6 +25,8 @@ void BluetoothController::handleCommand(const TerminalCommand& cmd) {
handlePair(cmd);
} else if (root == "spoof") {
handleSpoof(cmd);
+ } else if (root == "sniff") {
+ handleSniff(cmd);
} else if (root == "status") {
handleStatus();
} else if (root == "server") {
@@ -44,21 +46,16 @@ void BluetoothController::handleCommand(const TerminalCommand& cmd) {
Scan
*/
void BluetoothController::handleScan() {
- terminalView.println("Bluetooth Scan: In progress for 10 sec...");
- terminalView.println("");
-
- bluetoothService.init();
- auto devices = bluetoothService.scanDevices(10);
+ terminalView.println("Bluetooth Scan: In progress for 10 sec...\n");
- if (devices.empty()) {
+ auto lines = bluetoothService.scanDevices(10);
+ if (lines.empty()) {
terminalView.println("Bluetooth Scan: No devices");
return;
}
- for (size_t i = 0; i < devices.size(); ++i) {
- const auto& dev = devices[i];
- terminalView.println(" " + std::to_string(i) + ": " + dev.name + " @ " + dev.address + " RSSI: " + std::to_string(dev.rssi));
- terminalView.println("");
+ for (const auto& line : lines) {
+ terminalView.println(" " + line + "\n");
}
}
@@ -66,6 +63,7 @@ void BluetoothController::handleScan() {
Pair
*/
void BluetoothController::handlePair(const TerminalCommand& cmd) {
+ bluetoothService.switchToMode(BluetoothMode::CLIENT);
std::string addr = cmd.getSubcommand();
if (addr.empty()) {
terminalView.println("Usage: pair <mac address>");
@@ -111,6 +109,38 @@ void BluetoothController::handleStatus() {
}
}
+/*
+Sniff
+*/
+void BluetoothController::handleSniff(const TerminalCommand& cmd) {
+ terminalView.println("Bluetooth Sniff: Started... Press [Enter] to stop.\n");
+
+ bluetoothService.switchToMode(BluetoothMode::CLIENT);
+ BluetoothService::startPassiveBluetoothSniffing();
+
+ unsigned long lastPull = 0;
+
+ while (true) {
+ // Enter press
+ char key = terminalInput.readChar();
+ if (key == '\r' || key == '\n') break;
+
+ // Show paquets if any
+ if (millis() - lastPull > 200) {
+ auto logs = BluetoothService::getBluetoothSniffLog();
+ for (const auto& line : logs) {
+ terminalView.println(line);
+ }
+ lastPull = millis();
+ }
+
+ delay(10);
+ }
+
+ BluetoothService::stopPassiveBluetoothSniffing();
+ terminalView.println("Bluetooth Sniff: Stopped by user.\n");
+}
+
/*
Server
*/
@@ -248,13 +278,6 @@ void BluetoothController::handleHelp() {
terminalView.println(" reset");
}
-/*
-Sniff
-*/
-void BluetoothController::handleSniff() {
- terminalView.println("Bluetooth Sniff: Not Yet Implemented");
-}
-
/*
Ensure Config
*/
diff --git a/src/Controllers/BluetoothController.h b/src/Controllers/BluetoothController.h
index c923b2e1..02baff95 100644
--- a/src/Controllers/BluetoothController.h
+++ b/src/Controllers/BluetoothController.h
@@ -51,7 +51,7 @@ private:
void handleMouse(const TerminalCommand& cmd);
// Sniff BT server I/O
- void handleSniff();
+ void handleSniff(const TerminalCommand& cmd);
// Available BT commands
void handleHelp();
diff --git a/src/Services/BluetoothService.cpp b/src/Services/BluetoothService.cpp
index 0a69b9a9..58d3472f 100644
--- a/src/Services/BluetoothService.cpp
+++ b/src/Services/BluetoothService.cpp
@@ -1,5 +1,12 @@
#include "BluetoothService.h"
+// Static var for the sniffer
+std::vector<std::string> BluetoothService::bluetoothSniffLog;
+portMUX_TYPE BluetoothService::bluetoothSniffMux = portMUX_INITIALIZER_UNLOCKED;
+BLEScan* BluetoothService::bleScan = nullptr;
+std::string BluetoothService::lastAdParsed = "";
+
+// Callback connect/disconnect
class BluetoothServerCallbacks : public BLEServerCallbacks {
private:
BluetoothService& service;
@@ -161,24 +168,45 @@ void BluetoothService::clickMouse() {
sendMouseReport(0, 0, 0x00); // Release
}
-std::vector<ScannedDevice> BluetoothService::scanDevices(int seconds) {
+void BluetoothService::switchToMode(BluetoothMode newMode) {
+ if (mode == newMode) return;
+
+ if (mode == BluetoothMode::SERVER || mode == BluetoothMode::CLIENT) {
+ BLEDevice::deinit(true);
+ }
+
+ // Init pour le nouveau mode
+ if (newMode == BluetoothMode::CLIENT || newMode == BluetoothMode::SERVER) {
+ BLEDevice::init(""); // Peut être changé si tu veux des logs personnalisés
+ }
+
+ mode = newMode;
+}
+
+std::vector<std::string> BluetoothService::scanDevices(int seconds) {
+ switchToMode(BluetoothMode::CLIENT);
+ stopPassiveBluetoothSniffing();
+
auto scan = BLEDevice::getScan();
scan->setActiveScan(true);
-
auto results = scan->start(seconds);
- std::vector<ScannedDevice> devices;
+
+ std::vector<std::string> formattedDevices;
for (int i = 0; i < results.getCount(); ++i) {
BLEAdvertisedDevice device = results.getDevice(i);
- devices.push_back({
- device.getName().empty() ? "(unknown)" : device.getName(),
- device.getAddress().toString(),
- device.getRSSI()
- });
+
+ std::string name = device.getName().empty() ? "(unknown)" : device.getName();
+ std::string addr = device.getAddress().toString();
+ int rssi = device.getRSSI();
+ std::string type = isLikelyConnectable(device) ? "Connectable" : "Non-Connectable";
+ std::string entry = addr + " | " + name + " | RSSI: " + std::to_string(rssi) + " | Type: " + type;
+
+ formattedDevices.push_back(entry);
}
scan->clearResults();
- return devices;
+ return formattedDevices;
}
std::vector<std::string> BluetoothService::connectTo(const std::string& addr) {
@@ -267,6 +295,213 @@ void BluetoothService::clearBondedDevices() {
free(bonded);
}
+void BluetoothService::PassiveAdvertisedDeviceCallbacks::onResult(BLEAdvertisedDevice advertisedDevice) {
+ // Basic infos
+ std::string name = advertisedDevice.getName().empty() ? "(unknown)" : advertisedDevice.getName();
+ std::string addr = advertisedDevice.getAddress().toString();
+ int rssi = advertisedDevice.getRSSI();
+
+ // Check type
+ std::string type;
+ if (isLikelyConnectable(advertisedDevice)) {
+ type = "Connectable ";
+ } else {
+ type = "Non-Connectable";
+ }
+
+ // Build the log entry
+ std::string logEntry = "[BLE] " + addr + " | " + name + " | RSSI: " + std::to_string(rssi) + " | Type: " + type;
+
+ // Parse AD infos
+ std::string adParsed = parseAdTypes(advertisedDevice.getPayload(), advertisedDevice.getPayloadLength());
+
+ // No AD duplicate
+ if (adParsed == lastAdParsed) return;
+ lastAdParsed = adParsed;
+
+ // Ad AD infos to log
+ if (!adParsed.empty()) {
+ logEntry += " | " + adParsed;
+ }
+
+ // Add log to shared vector, erase the first element if max size is reach
+ portENTER_CRITICAL(&BluetoothService::bluetoothSniffMux);
+ if (bluetoothSniffLog.size() > 200) bluetoothSniffLog.erase(bluetoothSniffLog.begin());
+ BluetoothService::bluetoothSniffLog.push_back(logEntry);
+ portEXIT_CRITICAL(&BluetoothService::bluetoothSniffMux);
+}
+
+void BluetoothService::startPassiveBluetoothSniffing() {
+ if (!BLEDevice::getInitialized()) {
+ BLEDevice::init("Sniffer");
+ }
+
+ bleScan = BLEDevice::getScan();
+ bleScan->setAdvertisedDeviceCallbacks(new PassiveAdvertisedDeviceCallbacks(), true);
+ bleScan->setActiveScan(false);
+ bleScan->start(0, nullptr);
+}
+
+void BluetoothService::stopPassiveBluetoothSniffing() {
+ if (bleScan) {
+ bleScan->stop();
+ bleScan->setAdvertisedDeviceCallbacks(nullptr);
+ bleScan->clearResults();
+ bleScan = nullptr;
+ }
+ bluetoothSniffLog.clear();
+}
+
+std::vector<std::string> BluetoothService::getBluetoothSniffLog() {
+ std::vector<std::string> copy;
+ portENTER_CRITICAL(&bluetoothSniffMux);
+ copy.swap(bluetoothSniffLog);
+ portEXIT_CRITICAL(&bluetoothSniffMux);
+ return copy;
+}
+
+bool BluetoothService::isLikelyConnectable(BLEAdvertisedDevice& device) {
+ uint8_t* payload = device.getPayload();
+ size_t len = device.getPayloadLength();
+
+ for (size_t i = 0; i + 1 < len;) {
+ uint8_t field_len = payload[i];
+ if (field_len == 0 || i + field_len + 1 > len) break;
+
+ uint8_t ad_type = payload[i + 1];
+
+ if (ad_type == 0x01 && field_len >= 2) { // Flags
+ uint8_t flags = payload[i + 2];
+ // Bit 0x02 General Discoverable Mode
+ // Bit 0x04 BR/EDR Not Supported (Only BLE)
+ return (flags & 0x02) != 0;
+ }
+
+ i += field_len + 1;
+ }
+
+ return false;
+}
+
+std::string BluetoothService::parseAdTypes(const uint8_t* payload, size_t len) {
+ std::string result;
+ size_t i = 0;
+
+ while (i + 1 < len) {
+ uint8_t field_len = payload[i];
+ if (field_len == 0 || i + field_len + 1 > len) break;
+
+ uint8_t ad_type = payload[i + 1];
+ const uint8_t* data = payload + i + 2;
+
+ std::string entry;
+
+ switch (ad_type) {
+ case 0x01: { // Flags
+ entry = "AD 0x01: Flags: ";
+ uint8_t flags = data[0];
+ if (flags & 0x01) entry += "LE Limited, ";
+ if (flags & 0x02) entry += "LE General, ";
+ if (flags & 0x04) entry += "BR/EDR Not Supported, ";
+ if (flags & 0x08) entry += "LE+BR/EDR (Controller), ";
+ if (flags & 0x10) entry += "LE+BR/EDR (Host), ";
+ if (!entry.empty() && entry.back() == ' ') entry.erase(entry.size() - 2); // remove ", "
+ break;
+ }
+ case 0x02:
+ case 0x03: { // 16bit UUIDs
+ entry = "AD 0x03: UUID16: ";
+ for (int j = 0; j + 1 < field_len - 1; j += 2) {
+ uint16_t uuid = data[j] | (data[j + 1] << 8);
+ char uuidStr[8];
+ snprintf(uuidStr, sizeof(uuidStr), "0x%04X ", uuid);
+ entry += uuidStr;
+ }
+ break;
+ }
+ case 0x06:
+ case 0x07: { // 128bit UUIDs
+ entry = "AD 0x07: UUID128: ";
+ for (int j = 0; j + 15 < field_len - 1; j += 16) {
+ char uuidStr[40];
+ snprintf(uuidStr, sizeof(uuidStr),
+ "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X ",
+ data[j+15], data[j+14], data[j+13], data[j+12],
+ data[j+11], data[j+10],
+ data[j+9], data[j+8],
+ data[j+7], data[j+6],
+ data[j+5], data[j+4], data[j+3], data[j+2], data[j+1], data[j+0]);
+ entry += uuidStr;
+ }
+ break;
+ }
+ case 0x08: // Shortened Local Name
+ case 0x09: { // Complete Local Name
+ entry = "AD 0x09: Name: ";
+ entry.append(reinterpret_cast<const char*>(data), field_len - 1);
+ break;
+ }
+ case 0x0A: { // Tx Power Level
+ entry = "AD 0x0A: Tx Power: ";
+ char val[6];
+ snprintf(val, sizeof(val), "%d dBm", (int8_t)data[0]);
+ entry += val;
+ break;
+ }
+ case 0x16: {
+ entry = "AD 0x16: ServiceData16: ";
+ // UUID16 + raw data
+ if (field_len >= 3) {
+ uint16_t uuid = data[0] | (data[1] << 8);
+ char uuidStr[16];
+ snprintf(uuidStr, sizeof(uuidStr), "UUID 0x%04X, Data: ", uuid);
+ entry += uuidStr;
+ for (int j = 2; j < field_len - 1; ++j) {
+ char byteStr[4];
+ snprintf(byteStr, sizeof(byteStr), "%02X ", data[j]);
+ entry += byteStr;
+ }
+ }
+ break;
+ }
+ case 0xFF: { // Manufacturer Specific
+ entry = "AD 0xFF: Raw ";
+ for (int j = 0; j < field_len - 1; ++j) {
+ char byteStr[4];
+ snprintf(byteStr, sizeof(byteStr), "%02X ", data[j]);
+ entry += byteStr;
+ }
+ break;
+ }
+ default: {
+ char typeLabel[24];
+ snprintf(typeLabel, sizeof(typeLabel), "AD 0x%02X: Raw ", ad_type);
+ entry = typeLabel;
+ for (int j = 0; j < field_len - 1; ++j) {
+ char byteStr[4];
+ snprintf(byteStr, sizeof(byteStr), "%02X ", data[j]);
+ entry += byteStr;
+ }
+ break;
+ }
+ }
+
+ // Trim space if needed
+ if (!entry.empty() && entry.back() == ' ') entry.pop_back();
+
+ // Add entry with separator
+ result += entry + " | ";
+
+ i += field_len + 1;
+ }
+
+ // Remove trailing " | " if present
+ if (result.size() >= 3 && result.substr(result.size() - 3) == " | ")
+ result.erase(result.size() - 3);
+
+ return result;
+}
+
const uint8_t BluetoothService::HID_REPORT_MAP[] = {
// Mouse report
0x05, 0x01, // Usage Page (Generic Desktop)
diff --git a/src/Services/BluetoothService.h b/src/Services/BluetoothService.h
index a04f3d71..be2f6127 100644
--- a/src/Services/BluetoothService.h
+++ b/src/Services/BluetoothService.h
@@ -20,6 +20,8 @@ struct ScannedDevice {
std::string name;
std::string address;
int rssi;
+ std::string type;
+ std::string adSummary;
};
class BluetoothService {
@@ -30,7 +32,15 @@ private:
bool connected = false;
static const uint8_t HID_REPORT_MAP[];
BluetoothMode mode = BluetoothMode::NONE;
+ static BLEScan* bleScan;
+ static std::string lastAdParsed;
+
public:
+ class PassiveAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
+ public:
+ void onResult(BLEAdvertisedDevice advertisedDevice) override;
+ };
+
// begin / end server BT
void begin(const std::string& deviceName = "Bus-Pirate-Blueooth");
void end();
@@ -61,8 +71,20 @@ public:
std::string getMacAddress();
BluetoothMode getMode();
void clearBondedDevices();
-
- std::vector<ScannedDevice> scanDevices(int seconds = 10);
+ void switchToMode(BluetoothMode newMode);
+
+ // Scan
+ std::vector<std::string> scanDevices(int seconds = 10);
std::vector<std::string> connectTo(const std::string& addr);
+
+ // Bluetooth sniffing
+ class PassiveBLEAdvertisedDeviceCallbacks;
+ static void startPassiveBluetoothSniffing();
+ static void stopPassiveBluetoothSniffing();
+ static std::vector<std::string> getBluetoothSniffLog();
+ static bool isLikelyConnectable(BLEAdvertisedDevice& device);
+ static std::string parseAdTypes(const uint8_t* payload, size_t len);
+ static std::vector<std::string> bluetoothSniffLog;
+ static portMUX_TYPE bluetoothSniffMux;
};