clang-format: macsniff.cpp, macsniff.h, main.cpp, main.h

This commit is contained in:
Klaus K Wilting 2018-06-12 19:55:31 +02:00
parent 16ff1b4b60
commit 6250a0c308
4 changed files with 116 additions and 101 deletions

View File

@ -9,13 +9,16 @@
// Local logging tag // Local logging tag
static const char TAG[] = "wifi"; static const char TAG[] = "wifi";
static wifi_country_t wifi_country = {.cc=WIFI_MY_COUNTRY, .schan=WIFI_CHANNEL_MIN, .nchan=WIFI_CHANNEL_MAX, .policy=WIFI_COUNTRY_POLICY_MANUAL}; static wifi_country_t wifi_country = {.cc = WIFI_MY_COUNTRY,
.schan = WIFI_CHANNEL_MIN,
.nchan = WIFI_CHANNEL_MAX,
.policy = WIFI_COUNTRY_POLICY_MANUAL};
// globals // globals
uint16_t salt; uint16_t salt;
uint16_t reset_salt(void) { uint16_t reset_salt(void) {
salt = random(65536); // get new 16bit random for salting hashes and set global salt var salt = random(65536); // get new 16bit random for salting hashes
return salt; return salt;
} }
@ -28,23 +31,30 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) {
// only last 3 MAC Address bytes are used for MAC address anonymization // 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 // but since it's uint32 we take 4 bytes to avoid 1st value to be 0
addr2int = ( (uint32_t)paddr[2] ) | ( (uint32_t)paddr[3] << 8 ) | ( (uint32_t)paddr[4] << 16 ) | ( (uint32_t)paddr[5] << 24 ); addr2int = ((uint32_t)paddr[2]) | ((uint32_t)paddr[3] << 8) |
((uint32_t)paddr[4] << 16) | ((uint32_t)paddr[5] << 24);
#ifdef VENDORFILTER #ifdef VENDORFILTER
vendor2int = ( (uint32_t)paddr[2] ) | ( (uint32_t)paddr[1] << 8 ) | ( (uint32_t)paddr[0] << 16 ); vendor2int = ((uint32_t)paddr[2]) | ((uint32_t)paddr[1] << 8) |
((uint32_t)paddr[0] << 16);
// use OUI vendor filter list only on Wifi, not on BLE // use OUI vendor filter list only on Wifi, not on BLE
if ( (sniff_type==MAC_SNIFF_BLE) || std::find(vendors.begin(), vendors.end(), vendor2int) != vendors.end() ) if ((sniff_type == MAC_SNIFF_BLE) ||
{ std::find(vendors.begin(), vendors.end(), vendor2int) != vendors.end()) {
#endif #endif
// salt and hash MAC, and if new unique one, store identifier in container and increment counter on display // 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 // https://en.wikipedia.org/wiki/MAC_Address_Anonymization
addr2int += (uint32_t)salt; // add 16-bit salt to pseudo MAC addr2int += (uint32_t)salt; // add 16-bit salt to pseudo MAC
snprintf(buff, sizeof(buff), "%08X", addr2int); // convert unsigned 32-bit salted MAC to 8 digit hex string snprintf(
hashedmac = rokkit(&buff[3], 5); // hash MAC last string value, use 5 chars to fit hash in uint16_t container buff, sizeof(buff), "%08X",
auto newmac = macs.insert(hashedmac); // add hashed MAC to total container if new unique addr2int); // convert unsigned 32-bit salted MAC to 8 digit hex string
added = newmac.second ? true:false; // true if hashed MAC is unique in container hashedmac = rokkit(&buff[3], 5); // hash MAC last string value, use 5 chars
// to fit hash in uint16_t container
auto newmac = macs.insert(hashedmac); // add hashed MAC, if new unique
added = newmac.second ? true
: false; // true if hashed MAC is unique in container
// Count only if MAC was not yet seen // Count only if MAC was not yet seen
if (added) { if (added) {
@ -66,32 +76,39 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) {
} }
// Log scan result // Log scan result
ESP_LOGI(TAG, "%s %s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d -> %d Bytes left", ESP_LOGI(TAG,
"%s %s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d -> "
"%d Bytes left",
added ? "new " : "known", added ? "new " : "known",
sniff_type==MAC_SNIFF_WIFI ? "WiFi":"BLTH", sniff_type == MAC_SNIFF_WIFI ? "WiFi" : "BLTH", rssi, buff,
rssi, buff, hashedmac, macs_wifi, macs_ble, hashedmac, macs_wifi, macs_ble, ESP.getFreeHeap());
ESP.getFreeHeap());
#ifdef VENDORFILTER #ifdef VENDORFILTER
} else { } else {
// Very noisy // 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]); // ESP_LOGD(TAG, "Filtered MAC %02X:%02X:%02X:%02X:%02X:%02X",
// paddr[0],paddr[1],paddr[2],paddr[3],paddr[5],paddr[5]);
} }
#endif #endif
// True if MAC WiFi/BLE was new // 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) return added; // function returns bool if a new and unique Wifi or BLE mac was
// counted (true) or not (false)
} }
void wifi_sniffer_init(void) { void wifi_sniffer_init(void) {
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
cfg.nvs_enable = 0; // we don't need any wifi settings from NVRAM cfg.nvs_enable = 0; // we don't need any wifi settings from NVRAM
wifi_promiscuous_filter_t filter = {.filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT}; // we need only MGMT frames wifi_promiscuous_filter_t filter = {
.filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT}; // we need only MGMT frames
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); // configure Wifi with cfg ESP_ERROR_CHECK(esp_wifi_init(&cfg)); // configure Wifi with cfg
ESP_ERROR_CHECK(esp_wifi_set_country(&wifi_country)); // set locales for RF and channels ESP_ERROR_CHECK(
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); // we don't need NVRAM esp_wifi_set_country(&wifi_country)); // set locales for RF and channels
ESP_ERROR_CHECK(
esp_wifi_set_storage(WIFI_STORAGE_RAM)); // we don't need NVRAM
// ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL)); // ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
ESP_ERROR_CHECK(esp_wifi_set_promiscuous_filter(&filter)); // set MAC frame filter ESP_ERROR_CHECK(
esp_wifi_set_promiscuous_filter(&filter)); // set MAC frame filter
ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler)); ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler));
ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true)); // now switch on monitor mode ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true)); // now switch on monitor mode
} }
@ -101,16 +118,19 @@ void wifi_sniffer_set_channel(uint8_t channel) {
} }
// using IRAM_:ATTR here to speed up callback function // using IRAM_:ATTR here to speed up callback function
IRAM_ATTR void wifi_sniffer_packet_handler(void* buff, wifi_promiscuous_pkt_type_t type) { IRAM_ATTR void wifi_sniffer_packet_handler(void *buff,
wifi_promiscuous_pkt_type_t type) {
const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buff; const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buff;
const wifi_ieee80211_packet_t *ipkt = (wifi_ieee80211_packet_t *)ppkt->payload; const wifi_ieee80211_packet_t *ipkt =
(wifi_ieee80211_packet_t *)ppkt->payload;
const wifi_ieee80211_mac_hdr_t *hdr = &ipkt->hdr; const wifi_ieee80211_mac_hdr_t *hdr = &ipkt->hdr;
if ((cfg.rssilimit) && (ppkt->rx_ctrl.rssi < cfg.rssilimit )) { // rssi is negative value if ((cfg.rssilimit) &&
ESP_LOGI(TAG, "WiFi RSSI %d -> ignoring (limit: %d)", ppkt->rx_ctrl.rssi, cfg.rssilimit); (ppkt->rx_ctrl.rssi < cfg.rssilimit)) { // rssi is negative value
ESP_LOGI(TAG, "WiFi RSSI %d -> ignoring (limit: %d)", ppkt->rx_ctrl.rssi,
cfg.rssilimit);
} else { } else {
uint8_t *p = (uint8_t *)hdr->addr2; uint8_t *p = (uint8_t *)hdr->addr2;
mac_add(p, ppkt->rx_ctrl.rssi, MAC_SNIFF_WIFI); mac_add(p, ppkt->rx_ctrl.rssi, MAC_SNIFF_WIFI);
} }
} }

View File

@ -1,6 +1,5 @@
/* /*
ESP32-Paxcounter
Copyright 2018 Oliver Brandmueller <ob@sysadm.in> Copyright 2018 Oliver Brandmueller <ob@sysadm.in>
Copyright 2018 Klaus Wilting <verkehrsrot@arcor.de> Copyright 2018 Klaus Wilting <verkehrsrot@arcor.de>

View File

@ -10,10 +10,7 @@
//--- Declarations --- //--- Declarations ---
enum led_states { enum led_states { LED_OFF, LED_ON };
LED_OFF,
LED_ON
};
#if defined(CFG_eu868) #if defined(CFG_eu868)
const char lora_datarate[] = {"1211100908077BFSNA"}; const char lora_datarate[] = {"1211100908077BFSNA"};
@ -28,7 +25,6 @@ void reset_counters(void);
void blink_LED(uint16_t set_color, uint16_t set_blinkduration); void blink_LED(uint16_t set_color, uint16_t set_blinkduration);
void led_loop(void); void led_loop(void);
// defined in blescan.cpp // defined in blescan.cpp
#ifdef BLECOUNTER #ifdef BLECOUNTER
void start_BLEscan(void); void start_BLEscan(void);