2018-04-02 01:30:24 +02:00
|
|
|
|
|
|
|
// Basic Config
|
|
|
|
#include "globals.h"
|
2020-03-29 18:11:24 +02:00
|
|
|
#include "macsniff.h"
|
2018-04-02 01:30:24 +02:00
|
|
|
|
|
|
|
// Local logging tag
|
2019-02-27 00:49:32 +01:00
|
|
|
static const char TAG[] = __FILE__;
|
2018-04-02 01:30:24 +02:00
|
|
|
|
2021-01-01 15:55:22 +01:00
|
|
|
static QueueHandle_t MacQueue;
|
2020-11-07 22:32:36 +01:00
|
|
|
TaskHandle_t macProcessTask;
|
|
|
|
|
2020-12-28 16:12:00 +01:00
|
|
|
static uint32_t salt = renew_salt();
|
2018-04-04 12:45:31 +02:00
|
|
|
|
2020-12-28 16:12:00 +01:00
|
|
|
uint32_t renew_salt(void) {
|
|
|
|
salt = esp_random();
|
|
|
|
ESP_LOGV(TAG, "new salt = %04X", salt);
|
2018-06-12 19:55:31 +02:00
|
|
|
return salt;
|
2018-04-04 12:45:31 +02:00
|
|
|
}
|
2018-04-02 01:30:24 +02:00
|
|
|
|
2018-07-31 09:21:10 +02:00
|
|
|
int8_t isBeacon(uint64_t mac) {
|
2018-07-24 18:44:13 +02:00
|
|
|
it = std::find(beacons.begin(), beacons.end(), mac);
|
|
|
|
if (it != beacons.end())
|
|
|
|
return std::distance(beacons.begin(), it);
|
|
|
|
else
|
2018-07-31 09:21:10 +02:00
|
|
|
return -1;
|
2018-07-31 00:00:24 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 12:48:27 +02:00
|
|
|
// Display a key
|
|
|
|
void printKey(const char *name, const uint8_t *key, uint8_t len, bool lsb) {
|
|
|
|
const uint8_t *p;
|
|
|
|
char keystring[len + 1] = "", keybyte[3];
|
|
|
|
for (uint8_t i = 0; i < len; i++) {
|
|
|
|
p = lsb ? key + len - i - 1 : key + i;
|
2019-10-12 14:07:55 +02:00
|
|
|
snprintf(keybyte, 3, "%02X", *p);
|
2018-08-02 12:48:27 +02:00
|
|
|
strncat(keystring, keybyte, 2);
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "%s: %s", name, keystring);
|
|
|
|
}
|
|
|
|
|
2018-07-31 00:00:24 +02:00
|
|
|
uint64_t macConvert(uint8_t *paddr) {
|
2019-07-21 19:20:02 +02:00
|
|
|
uint64_t *mac;
|
|
|
|
mac = (uint64_t *)paddr;
|
2019-10-22 16:41:14 +02:00
|
|
|
return (__builtin_bswap64(*mac) >> 16);
|
2018-07-24 18:44:13 +02:00
|
|
|
}
|
|
|
|
|
2020-11-07 22:32:36 +01:00
|
|
|
esp_err_t macQueueInit() {
|
|
|
|
_ASSERT(MAC_QUEUE_SIZE > 0);
|
|
|
|
MacQueue = xQueueCreate(MAC_QUEUE_SIZE, sizeof(MacBuffer_t));
|
|
|
|
if (MacQueue == 0) {
|
|
|
|
ESP_LOGE(TAG, "Could not create MAC processing queue. Aborting.");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "MAC processing queue created, size %d Bytes",
|
|
|
|
MAC_QUEUE_SIZE * sizeof(MacBuffer_t));
|
|
|
|
|
|
|
|
xTaskCreatePinnedToCore(mac_process, // task function
|
|
|
|
"mac_process", // name of task
|
2020-12-22 15:20:32 +01:00
|
|
|
3072, // stack size of task
|
2020-11-07 22:32:36 +01:00
|
|
|
(void *)1, // parameter of the task
|
|
|
|
1, // priority of the task
|
|
|
|
&macProcessTask, // task handle
|
|
|
|
1); // CPU core
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sniffed MAC processing task
|
|
|
|
void mac_process(void *pvParameters) {
|
|
|
|
_ASSERT((uint32_t)pvParameters == 1); // FreeRTOS check
|
|
|
|
|
|
|
|
MacBuffer_t MacBuffer;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
|
|
|
// fetch next or wait for incoming MAC from sniffing queue
|
|
|
|
if (xQueueReceive(MacQueue, &MacBuffer, portMAX_DELAY) != pdTRUE) {
|
|
|
|
ESP_LOGE(TAG, "Premature return from xQueueReceive() with no data!");
|
|
|
|
continue;
|
2020-11-09 13:25:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// update traffic indicator
|
|
|
|
rf_load = uxQueueMessagesWaiting(MacQueue);
|
|
|
|
// process fetched mac
|
|
|
|
mac_analyze(MacBuffer);
|
2020-11-07 22:32:36 +01:00
|
|
|
}
|
|
|
|
delay(2); // yield to CPU
|
|
|
|
}
|
|
|
|
|
|
|
|
// enqueue message in MAC processing queue
|
|
|
|
void IRAM_ATTR mac_add(uint8_t *paddr, int8_t rssi, snifftype_t sniff_type) {
|
|
|
|
|
|
|
|
MacBuffer_t MacBuffer;
|
|
|
|
|
|
|
|
MacBuffer.rssi = rssi;
|
|
|
|
MacBuffer.sniff_type = sniff_type;
|
|
|
|
memcpy(MacBuffer.mac, paddr, 6);
|
|
|
|
|
|
|
|
if (xQueueSendToBackFromISR(MacQueue, (void *)&MacBuffer, (TickType_t)0) !=
|
2020-11-08 22:14:28 +01:00
|
|
|
pdPASS)
|
2020-11-07 22:32:36 +01:00
|
|
|
ESP_LOGW(TAG, "Dense radio traffic, packet lost!");
|
|
|
|
}
|
|
|
|
|
2020-11-08 22:14:28 +01:00
|
|
|
uint16_t mac_analyze(MacBuffer_t MacBuffer) {
|
2018-04-02 01:30:24 +02:00
|
|
|
|
2020-12-29 20:48:02 +01:00
|
|
|
uint32_t *mac; // pointer to shortened 4 byte MAC
|
|
|
|
uint32_t saltedmac;
|
|
|
|
uint16_t hashedmac;
|
|
|
|
|
2020-11-08 22:14:28 +01:00
|
|
|
if ((cfg.rssilimit) &&
|
|
|
|
(MacBuffer.rssi < cfg.rssilimit)) { // rssi is negative value
|
|
|
|
ESP_LOGI(TAG, "%s RSSI %d -> ignoring (limit: %d)",
|
|
|
|
(MacBuffer.sniff_type == MAC_SNIFF_WIFI) ? "WIFI" : "BLTH",
|
|
|
|
MacBuffer.rssi, cfg.rssilimit);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-06-12 19:55:31 +02:00
|
|
|
|
2020-11-08 22:14:28 +01:00
|
|
|
// in beacon monitor mode check if seen MAC is a known beacon
|
|
|
|
if (cfg.monitormode) {
|
|
|
|
int8_t beaconID = isBeacon(macConvert(MacBuffer.mac));
|
|
|
|
if (beaconID >= 0) {
|
|
|
|
ESP_LOGI(TAG, "Beacon ID#%d detected", beaconID);
|
|
|
|
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
|
|
|
|
blink_LED(COLOR_WHITE, 2000);
|
|
|
|
#endif
|
|
|
|
payload.reset();
|
|
|
|
payload.addAlarm(MacBuffer.rssi, beaconID);
|
2020-12-09 11:01:54 +01:00
|
|
|
SendPayload(BEACONPORT);
|
2020-11-08 22:14:28 +01:00
|
|
|
}
|
|
|
|
};
|
2019-07-21 19:20:02 +02:00
|
|
|
|
2020-11-08 22:14:28 +01:00
|
|
|
// only last 3 MAC Address bytes are used for MAC address anonymization
|
|
|
|
// but since it's uint32 we take 4 bytes to avoid 1st value to be 0.
|
|
|
|
// this gets MAC in msb (= reverse) order, but doesn't matter for hashing it.
|
|
|
|
mac = (uint32_t *)(MacBuffer.mac + 2);
|
|
|
|
|
|
|
|
// salt and hash MAC, and if new unique one, store identifier in container
|
|
|
|
// and increment counter on display
|
|
|
|
// https://en.wikipedia.org/wiki/MAC_Address_Anonymization
|
2018-06-12 19:55:31 +02:00
|
|
|
|
2020-12-28 16:12:00 +01:00
|
|
|
// reversed 4 byte MAC added to current salt
|
2020-12-29 20:48:02 +01:00
|
|
|
saltedmac = *mac + salt;
|
2020-12-28 16:12:00 +01:00
|
|
|
|
|
|
|
// hashed 4 byte MAC
|
|
|
|
// to save RAM, we use only lower 2 bytes of hash, since collisions don't
|
|
|
|
// matter in our use case
|
2020-12-29 20:48:02 +01:00
|
|
|
hashedmac = hash((const char *)&saltedmac, 4);
|
2020-12-28 16:12:00 +01:00
|
|
|
|
|
|
|
auto newmac = macs.insert(hashedmac); // add hashed MAC, if new unique
|
2020-11-08 22:14:28 +01:00
|
|
|
bool added =
|
|
|
|
newmac.second ? true : false; // true if hashed MAC is unique in container
|
2018-04-14 20:22:58 +02:00
|
|
|
|
2020-11-08 22:14:28 +01:00
|
|
|
// Count only if MAC was not yet seen
|
|
|
|
if (added) {
|
2020-11-07 22:32:36 +01:00
|
|
|
|
2020-11-08 22:14:28 +01:00
|
|
|
switch (MacBuffer.sniff_type) {
|
|
|
|
|
|
|
|
case MAC_SNIFF_WIFI:
|
|
|
|
macs_wifi++; // increment Wifi MACs counter
|
2018-06-12 19:55:31 +02:00
|
|
|
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
|
2020-11-08 22:14:28 +01:00
|
|
|
blink_LED(COLOR_GREEN, 50);
|
2018-06-12 19:55:31 +02:00
|
|
|
#endif
|
2020-11-08 22:14:28 +01:00
|
|
|
break;
|
2020-11-07 22:32:36 +01:00
|
|
|
|
2020-11-08 22:14:28 +01:00
|
|
|
#if (BLECOUNTER)
|
|
|
|
case MAC_SNIFF_BLE:
|
|
|
|
macs_ble++; // increment BLE Macs counter
|
|
|
|
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
|
|
|
|
blink_LED(COLOR_MAGENTA, 50);
|
2020-11-07 22:32:36 +01:00
|
|
|
#endif
|
2020-11-08 22:14:28 +01:00
|
|
|
break;
|
2020-11-07 22:32:36 +01:00
|
|
|
|
2020-11-08 22:14:28 +01:00
|
|
|
#if (COUNT_ENS)
|
|
|
|
case MAC_SNIFF_BLE_ENS:
|
|
|
|
macs_ble++; // increment BLE Macs counter
|
|
|
|
cwa_mac_add(hashedmac); // process ENS beacon
|
2018-06-12 19:55:31 +02:00
|
|
|
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
|
2020-11-08 22:14:28 +01:00
|
|
|
blink_LED(COLOR_WHITE, 50);
|
2018-06-12 19:55:31 +02:00
|
|
|
#endif
|
2020-11-08 22:14:28 +01:00
|
|
|
break;
|
2020-11-07 22:32:36 +01:00
|
|
|
|
2020-11-08 22:14:28 +01:00
|
|
|
#endif // COUNT_ENS
|
2020-11-07 22:32:36 +01:00
|
|
|
#endif // BLECOUNTER
|
2018-07-24 21:23:27 +02:00
|
|
|
|
2020-11-08 22:14:28 +01:00
|
|
|
} // switch
|
|
|
|
} // added
|
|
|
|
|
|
|
|
// Log scan result
|
|
|
|
ESP_LOGV(TAG,
|
2020-12-28 16:12:00 +01:00
|
|
|
"%s %s RSSI %ddBi -> MAC %0x:%0x:%0x:%0x:%0x:%0x -> salted %04X"
|
|
|
|
" -> hashed %04X -> WiFi:%d "
|
2020-11-08 22:14:28 +01:00
|
|
|
"BLTH:%d "
|
2020-09-02 11:21:56 +02:00
|
|
|
#if (COUNT_ENS)
|
2020-11-08 22:14:28 +01:00
|
|
|
"(CWA:%d)"
|
2020-09-02 11:21:56 +02:00
|
|
|
#endif
|
2020-11-08 22:14:28 +01:00
|
|
|
"-> %d Bytes left",
|
|
|
|
added ? "new " : "known",
|
|
|
|
MacBuffer.sniff_type == MAC_SNIFF_WIFI ? "WiFi" : "BLTH",
|
2020-12-28 16:12:00 +01:00
|
|
|
MacBuffer.rssi, MacBuffer.mac[0], MacBuffer.mac[1], MacBuffer.mac[2],
|
|
|
|
MacBuffer.mac[3], MacBuffer.mac[4], MacBuffer.mac[5], saltedmac,
|
|
|
|
hashedmac, macs_wifi, macs_ble,
|
2020-09-02 11:21:56 +02:00
|
|
|
#if (COUNT_ENS)
|
2020-11-08 22:14:28 +01:00
|
|
|
cwa_report(),
|
2018-06-12 19:55:31 +02:00
|
|
|
#endif
|
2020-11-08 22:14:28 +01:00
|
|
|
getFreeRAM());
|
2018-06-10 21:03:16 +02:00
|
|
|
|
2020-11-08 22:14:28 +01:00
|
|
|
// if an unknown Wifi or BLE mac was counted, return hash of this mac, else 0
|
|
|
|
return (added ? hashedmac : 0);
|
2020-11-24 22:12:47 +01:00
|
|
|
}
|