From c98511ba3661365bb0ec24f53aa5c65a4c072c80 Mon Sep 17 00:00:00 2001 From: Florian <1technophile@users.noreply.github.com> Date: Mon, 28 Nov 2022 20:06:36 -0600 Subject: [PATCH] Set the AP name with the short name plus the 4 first digits of the ESP mac (#1331) so as to don't disclose the password used for WM. Also change the wm password from MAc to be the last 8 digits, as these will change more than the first 8 ones. Also make consistent the ifndef qnd the parsing between WM_PWD_FROM_MAC and USE_MAC_AS_GATEWAY_NAME --- main/User_config.h | 10 ++++------ main/main.ino | 27 +++++++++++++-------------- platformio.ini | 2 +- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/main/User_config.h b/main/User_config.h index 711b318..d682fad 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -50,7 +50,7 @@ */ /*-------------DEFINE GATEWAY NAME BELOW IT CAN ALSO BE DEFINED IN platformio.ini----------------*/ -// Uncomment to use the MAC address in the format of 112233445566 as the gateway name +// Uncomment to use the MAC address first 4 digits in the format of 5566 as the gateway name // Any definition of Gateway_Name will be ignored. The Gateway_Short_name _ MAC will be used as the access point name. //#define USE_MAC_AS_GATEWAY_NAME #ifndef Gateway_Name @@ -114,11 +114,9 @@ const byte mac[] = {0xDE, 0xED, 0xBA, 0xFE, 0x54, 0x95}; //W5100 ethernet shield # endif #endif -#define WM_PWD_FROM_MAC false // enable to set the password from the 8 first digit of the ESP mac address for enhanced security, enabling this option requires to have access to the MAC address, either through a sticker or with serial monitoring -#if !WM_PWD_FROM_MAC -# ifndef WifiManager_password -# define WifiManager_password "your_password" //this is going to be the WPA2-PSK password for the initial setup access point -# endif +//#define WM_PWD_FROM_MAC true // enable to set the password from the 8 first digit of the ESP mac address for enhanced security, enabling this option requires to have access to the MAC address, either through a sticker or with serial monitoring +#ifndef WifiManager_password +# define WifiManager_password "your_password" //this is going to be the WPA2-PSK password for the initial setup access point #endif #ifndef WifiManager_ssid # define WifiManager_ssid Gateway_Name //this is the network name of the initial setup access point diff --git a/main/main.ino b/main/main.ino index 4dc5138..9c9e77b 100644 --- a/main/main.ino +++ b/main/main.ino @@ -182,6 +182,10 @@ char gateway_name[parameters_size + 1] = Gateway_Name; char WifiManager_ssid[MAC_NAME_MAX_LEN]; char ota_hostname[MAC_NAME_MAX_LEN]; #endif +#ifdef WM_PWD_FROM_MAC +# undef WifiManager_password; +char WifiManager_password[9]; +#endif bool connectedOnce = false; //indicate if we have been connected once to MQTT bool connected = false; //indicate whether we are currently connected. Used to detected re-connection int failure_number_ntwk = 0; // number of failure connecting to network @@ -658,14 +662,18 @@ void setup() { Log.notice(F("OpenMQTTGateway Version: " OMG_VERSION CR)); # endif -# ifdef USE_MAC_AS_GATEWAY_NAME String s = WiFi.macAddress(); +# ifdef USE_MAC_AS_GATEWAY_NAME sprintf(gateway_name, "%.2s%.2s%.2s%.2s%.2s%.2s", s.c_str(), s.c_str() + 3, s.c_str() + 6, s.c_str() + 9, s.c_str() + 12, s.c_str() + 15); - snprintf(WifiManager_ssid, MAC_NAME_MAX_LEN, "%s_%s", Gateway_Short_Name, gateway_name); + snprintf(WifiManager_ssid, MAC_NAME_MAX_LEN, "%s_%.2s%.2s", Gateway_Short_Name, s.c_str(), s.c_str() + 3); strcpy(ota_hostname, WifiManager_ssid); Log.notice(F("OTA Hostname: %s.local" CR), ota_hostname); # endif +# ifdef WM_PWD_FROM_MAC // From ESP Mac Address, last 8 digits as the password + sprintf(WifiManager_password, "%.2s%.2s%.2s%.2s", + s.c_str() + 6, s.c_str() + 9, s.c_str() + 12, s.c_str() + 15); +# endif # ifdef ESP32_ETHERNET setup_ethernet_esp32(); @@ -1232,18 +1240,9 @@ void setup_wifimanager(bool reset_settings) { if (!wifi_reconnect_bypass()) // if we didn't connect with saved credential we start Wifimanager web portal { - // Wifi Manager password - String wm_password; -# if WM_PWD_FROM_MAC // From ESP Mac Address - wm_password = WiFi.macAddress(); - wm_password.replace(":", ""); - wm_password.substring(0, 8); -# else - wm_password = WifiManager_password; // From macro definition -# endif # ifdef ESP32 if (lowpowermode < 2) { - displayPrint("Connect your phone to WIFI AP:", WifiManager_ssid, const_cast(wm_password.c_str())); + displayPrint("Connect your phone to WIFI AP:", WifiManager_ssid, WifiManager_password); } else { // in case of low power mode we put the ESP to sleep again if we didn't get connected (typical in case the wifi is down) # ifdef ZgatewayBT lowPowerESP32(); @@ -1253,11 +1252,11 @@ void setup_wifimanager(bool reset_settings) { ErrorIndicatorON(); InfoIndicatorON(); - Log.notice(F("Connect your phone to WIFI AP: %s with PWD: %s" CR), WifiManager_ssid, wm_password.c_str()); + Log.notice(F("Connect your phone to WIFI AP: %s with PWD: %s" CR), WifiManager_ssid, WifiManager_password); //fetches ssid and pass and tries to connect //if it does not connect it starts an access point with the specified name //and goes into a blocking loop awaiting configuration - if (!wifiManager.autoConnect(WifiManager_ssid, wm_password.c_str())) { + if (!wifiManager.autoConnect(WifiManager_ssid, WifiManager_password)) { Log.warning(F("failed to connect and hit timeout" CR)); delay(3000); //reset and try again diff --git a/platformio.ini b/platformio.ini index dbafc71..bd46c42 100644 --- a/platformio.ini +++ b/platformio.ini @@ -292,7 +292,7 @@ build_flags = '-DZgatewayWeatherStation="WeatherStation"' '-DsimplePublishing=true' '-DWM_PWD_FROM_MAC=true' - '-DGateway_Name="OpenMQTTGateway_ESP32_ALL"' + '-DUSE_MAC_AS_GATEWAY_NAME=true' [env:esp32dev-rf] platform = ${com.esp32_platform}