From df6cb25b3844597e85c9bce3618a3a4af2718d4d Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 31 Mar 2018 13:54:40 +0200 Subject: [PATCH] created first version with hash --- platformio.ini | 4 ++- src/globals.h | 2 +- src/main.cpp | 3 +- src/main.h | 2 +- src/rokkithash.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++ src/wifisniffer.cpp | 24 ++++++++++----- 6 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 src/rokkithash.cpp diff --git a/platformio.ini b/platformio.ini index 26c735d5..4c4cb5c4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -70,7 +70,9 @@ board = esp32dev framework = arduino monitor_baud = 115200 upload_speed = 921600 -lib_deps = U8g2 +lib_deps = + U8g2 + ESP32 BLE Arduino@>=0.4.9 build_flags = ;set log level, we need build_flag for this, otherwise we can't use ESP_LOGx in arduino framework -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE diff --git a/src/globals.h b/src/globals.h index 69212710..1735068a 100644 --- a/src/globals.h +++ b/src/globals.h @@ -34,7 +34,7 @@ extern uint64_t uptimecounter; extern osjob_t sendjob; extern int macnum, blenum, countermode, screensaver, adrmode, lorasf, txpower, rlim; extern bool joinstate; -extern std::set macs; +extern std::set macs; #ifdef HAS_DISPLAY extern HAS_DISPLAY u8x8; diff --git a/src/main.cpp b/src/main.cpp index 2d64f6eb..27e040ce 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -46,7 +46,7 @@ int macnum = 0, blenum = 0; uint64_t uptimecounter = 0; bool joinstate = false; -std::set macs; // associative container holds filtered MAC adresses +std::set macs; // associative container holds filtered MAC adresses // this variable will be changed in the ISR, and read in main loop static volatile bool ButtonTriggered = false; @@ -67,7 +67,6 @@ void eraseConfig(void); void saveConfig(void); void loadConfig(void); - /* begin LMIC specific parts ------------------------------------------------------------ */ // LMIC enhanced Pin mapping diff --git a/src/main.h b/src/main.h index 2e67f211..c0e45d09 100644 --- a/src/main.h +++ b/src/main.h @@ -1,5 +1,5 @@ // program version -#define PROGVERSION "1.2.53" // use max 10 chars here! +#define PROGVERSION "1.2.54" // use max 10 chars here! #define PROGNAME "PAXCNT" // Verbose enables serial output diff --git a/src/rokkithash.cpp b/src/rokkithash.cpp new file mode 100644 index 00000000..24e81be4 --- /dev/null +++ b/src/rokkithash.cpp @@ -0,0 +1,75 @@ +/* +Porting - 2014 Alex K +Algorithm (c) Paul Hsieh http://www.azillionmonkeys.com/qed/hash.html + + +The latest version of this library may be found at: + https://github.com/mrbio/Arduino-rokkit-hash + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include + +uint32_t rokkit(const char * data, int len) { + uint32_t hash, tmp; + int rem; + + if (len <= 0 || data == 0) return 0; + hash = len; + rem = len & 3; + len >>= 2; + + /* Main loop */ + while (len > 0) { + hash += *((uint16_t*)data); + tmp = (*((uint16_t*)(data+2)) << 11) ^ hash; + hash = (hash << 16) ^ tmp; + data += 2*2; + hash += hash >> 11; + len--; + } + + /* Handle end cases */ + switch (rem) { + case 3: hash += *((uint16_t*)data); + hash ^= hash << 16; + hash ^= ((signed char)data[2]) << 18; + hash += hash >> 11; + break; + case 2: hash += *((uint16_t*)data); + hash ^= hash << 11; + hash += hash >> 17; + break; + case 1: hash += (signed char)*data; + hash ^= hash << 10; + hash += hash >> 1; + } + + /* Force "avalanching" of final 127 bits */ + hash ^= hash << 3; + hash += hash >> 5; + hash ^= hash << 4; + hash += hash >> 17; + hash ^= hash << 25; + hash += hash >> 6; + + return hash; +} diff --git a/src/wifisniffer.cpp b/src/wifisniffer.cpp index a68aba6f..84f46964 100644 --- a/src/wifisniffer.cpp +++ b/src/wifisniffer.cpp @@ -14,6 +14,9 @@ // Local logging tag static const char *TAG = "wifisniffer"; +// function defined in rokkithash.cpp +uint32_t rokkit(const char * , int ); + static wifi_country_t wifi_country = {.cc=WIFI_MY_COUNTRY, .schan=WIFI_CHANNEL_MIN, .nchan=WIFI_CHANNEL_MAX, .policy=WIFI_COUNTRY_POLICY_MANUAL}; typedef struct { @@ -56,8 +59,10 @@ 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_ieee80211_packet_t *ipkt = (wifi_ieee80211_packet_t *)ppkt->payload; const wifi_ieee80211_mac_hdr_t *hdr = &ipkt->hdr; - char counter [10]; - std::pair::iterator, bool> newmac; + char counter [11]; // uint32_t -> 4 byte -> 10 decimals + '0' terminator -> 11 chars + char macbuf [21]; // uint64_t -> 8 byte -> 20 decimals + '0' terminator -> 21 chars + uint32_t hashedmac; + std::pair::iterator, bool> newmac; if (( cfg.rssilimit == 0 ) || (ppkt->rx_ctrl.rssi > cfg.rssilimit )) { // rssi is negative value uint64_t addr2int = ( (uint64_t)hdr->addr2[0] ) | ( (uint64_t)hdr->addr2[1] << 8 ) | ( (uint64_t)hdr->addr2[2] << 16 ) | \ @@ -69,17 +74,20 @@ void wifi_sniffer_packet_handler(void* buff, wifi_promiscuous_pkt_type_t type) { if ( std::find(vendors.begin(), vendors.end(), vendor2int) != vendors.end() ) { #endif - // INFO: RSSI when found MAC in range + // log rssi info for scanned MAC ESP_LOGI(TAG, "WiFi RSSI: %02d", ppkt->rx_ctrl.rssi); - // if new unique MAC logged increment counter on display - newmac = macs.insert(addr2int); + // if found new unique MAC hash it and increment counter on display + itoa(addr2int, macbuf, 10); // convert 64 bit MAC to decimal string + hashedmac = rokkit(macbuf, 10); // hash MAC for privacy, use 10 chars to store in uint32_t set + newmac = macs.insert(hashedmac); // store hashed MAC if new unique + //if ( (newmac.second) && ((uint32_t)hdr->addr2[0] & 0x03 == 0) ) { // filter local and group MACs if (newmac.second) { macnum++; - itoa(macnum, counter, 10); + itoa(macnum, counter, 10); // 10 -> decimal counter value u8x8.draw2x2String(0, 0, counter); - ESP_LOGI(TAG, "MAC counter: %4i", macnum); - } + ESP_LOGI(TAG, "#%04i -> Hash %u", macnum, hashedmac); + } #ifdef VENDORFILTER }