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
|
|
|
|
2019-07-21 19:20:02 +02:00
|
|
|
#if (VENDORFILTER)
|
2018-06-12 19:55:31 +02:00
|
|
|
#include "vendor_array.h"
|
2018-04-02 01:30:24 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Local logging tag
|
2019-02-27 00:49:32 +01:00
|
|
|
static const char TAG[] = __FILE__;
|
2018-04-02 01:30:24 +02:00
|
|
|
|
2020-09-01 11:53:49 +02:00
|
|
|
// used here and in corona.cpp
|
2020-09-02 11:21:56 +02:00
|
|
|
uint16_t salt = 0;
|
|
|
|
uint16_t hashedmac = 0; // temporary buffer for generated hash value
|
2018-04-04 12:45:31 +02:00
|
|
|
|
2018-09-23 22:12:10 +02:00
|
|
|
uint16_t get_salt(void) {
|
|
|
|
salt = (uint16_t)random(65536); // get new 16bit random for salting hashes
|
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
|
|
|
}
|
|
|
|
|
2018-04-02 01:30:24 +02:00
|
|
|
bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) {
|
|
|
|
|
2019-07-21 19:20:02 +02:00
|
|
|
if (!salt) // ensure we have salt (appears after radio is turned on)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char buff[10]; // temporary buffer for printf
|
2018-06-12 19:55:31 +02:00
|
|
|
bool added = false;
|
2020-09-02 11:21:56 +02:00
|
|
|
int8_t beaconID; // beacon number in test monitor mode
|
|
|
|
uint32_t *mac; // temporary buffer for shortened MAC
|
2018-06-12 19:55:31 +02:00
|
|
|
|
|
|
|
// only last 3 MAC Address bytes are used for MAC address anonymization
|
2019-07-21 19:20:02 +02:00
|
|
|
// 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 *)(paddr + 2);
|
|
|
|
|
|
|
|
#if (VENDORFILTER)
|
|
|
|
uint32_t *oui; // temporary buffer for vendor OUI
|
|
|
|
oui = (uint32_t *)paddr;
|
2018-06-12 19:55:31 +02:00
|
|
|
|
|
|
|
// use OUI vendor filter list only on Wifi, not on BLE
|
|
|
|
if ((sniff_type == MAC_SNIFF_BLE) ||
|
2019-07-21 19:20:02 +02:00
|
|
|
std::find(vendors.begin(), vendors.end(), __builtin_bswap32(*oui) >> 8) !=
|
|
|
|
vendors.end()) {
|
2018-06-12 19:55:31 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// 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-07-24 21:48:11 +02:00
|
|
|
snprintf(buff, sizeof(buff), "%08X",
|
2019-07-21 19:20:02 +02:00
|
|
|
*mac + (uint32_t)salt); // convert unsigned 32-bit salted MAC
|
|
|
|
// to 8 digit hex string
|
|
|
|
hashedmac = rokkit(&buff[3], 5); // hash MAC 8 digit -> 5 digit
|
2018-06-12 19:55:31 +02:00
|
|
|
auto newmac = macs.insert(hashedmac); // add hashed MAC, if new unique
|
|
|
|
added = newmac.second ? true
|
|
|
|
: false; // true if hashed MAC is unique in container
|
2018-04-14 20:22:58 +02:00
|
|
|
|
2018-04-19 15:17:23 +02:00
|
|
|
// Count only if MAC was not yet seen
|
2018-04-14 20:22:58 +02:00
|
|
|
if (added) {
|
2018-06-12 19:55:31 +02:00
|
|
|
// increment counter and one blink led
|
|
|
|
if (sniff_type == MAC_SNIFF_WIFI) {
|
|
|
|
macs_wifi++; // increment Wifi MACs counter
|
|
|
|
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
|
|
|
|
blink_LED(COLOR_GREEN, 50);
|
|
|
|
#endif
|
|
|
|
}
|
2019-07-21 19:20:02 +02:00
|
|
|
#if (BLECOUNTER)
|
2018-06-12 19:55:31 +02:00
|
|
|
else if (sniff_type == MAC_SNIFF_BLE) {
|
|
|
|
macs_ble++; // increment BLE Macs counter
|
|
|
|
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
|
|
|
|
blink_LED(COLOR_MAGENTA, 50);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
2018-07-24 21:23:27 +02:00
|
|
|
|
|
|
|
// in beacon monitor mode check if seen MAC is a known beacon
|
|
|
|
if (cfg.monitormode) {
|
2018-07-31 00:00:24 +02:00
|
|
|
beaconID = isBeacon(macConvert(paddr));
|
2018-07-31 09:21:10 +02:00
|
|
|
if (beaconID >= 0) {
|
2018-08-04 14:37:41 +02:00
|
|
|
ESP_LOGI(TAG, "Beacon ID#%d detected", beaconID);
|
2018-07-24 21:54:40 +02:00
|
|
|
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
|
|
|
|
blink_LED(COLOR_WHITE, 2000);
|
|
|
|
#endif
|
2018-07-24 21:23:27 +02:00
|
|
|
payload.reset();
|
2019-03-09 15:25:44 +01:00
|
|
|
payload.addAlarm(rssi, beaconID);
|
2019-02-03 14:12:21 +01:00
|
|
|
SendPayload(BEACONPORT, prio_high);
|
2018-07-24 21:23:27 +02:00
|
|
|
}
|
2020-09-02 11:21:56 +02:00
|
|
|
};
|
2018-07-24 21:23:27 +02:00
|
|
|
|
|
|
|
} // added
|
2018-04-19 15:17:23 +02:00
|
|
|
|
|
|
|
// Log scan result
|
2020-09-02 11:21:56 +02:00
|
|
|
ESP_LOGD(TAG,
|
2019-07-21 19:20:02 +02:00
|
|
|
"%s %s RSSI %ddBi -> salted MAC %s -> Hash %04X -> WiFi:%d "
|
2020-09-01 11:53:49 +02:00
|
|
|
"BLTH:%d "
|
2020-09-02 11:21:56 +02:00
|
|
|
#if (COUNT_ENS)
|
|
|
|
"(CWA:%d)"
|
|
|
|
#endif
|
2020-09-01 11:53:49 +02:00
|
|
|
"-> %d Bytes left",
|
2018-06-12 19:55:31 +02:00
|
|
|
added ? "new " : "known",
|
|
|
|
sniff_type == MAC_SNIFF_WIFI ? "WiFi" : "BLTH", rssi, buff,
|
2020-09-02 11:21:56 +02:00
|
|
|
hashedmac, macs_wifi, macs_ble,
|
|
|
|
#if (COUNT_ENS)
|
2020-09-01 11:53:49 +02:00
|
|
|
cwa_report(),
|
|
|
|
#endif
|
|
|
|
getFreeRAM());
|
2018-12-22 14:37:47 +01:00
|
|
|
|
2019-07-21 19:20:02 +02:00
|
|
|
#if (VENDORFILTER)
|
2018-06-12 19:55:31 +02:00
|
|
|
} else {
|
|
|
|
// Very noisy
|
|
|
|
// ESP_LOGD(TAG, "Filtered MAC %02X:%02X:%02X:%02X:%02X:%02X",
|
|
|
|
// paddr[0],paddr[1],paddr[2],paddr[3],paddr[5],paddr[5]);
|
|
|
|
}
|
|
|
|
#endif
|
2018-06-10 21:03:16 +02:00
|
|
|
|
2018-06-12 19:55:31 +02:00
|
|
|
// True if MAC WiFi/BLE was new
|
|
|
|
return added; // function returns bool if a new and unique Wifi or BLE mac was
|
|
|
|
// counted (true) or not (false)
|
2019-10-22 16:41:14 +02:00
|
|
|
}
|