From d79df9ed5751bc9beb27ea0393f2a58a245f9fb3 Mon Sep 17 00:00:00 2001 From: Dmitriy Shafranskiy Date: Thu, 18 Feb 2021 08:15:52 +0100 Subject: [PATCH 1/9] removed ota import from main class --- src/main.cpp | 4 ++-- src/system/ota.cpp | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 03da465..34ece2c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,7 +15,6 @@ #include "reporting/reporting.h" #include "system/ntp.h" #include "system/ota.h" -#include void ConnectToWifi() { @@ -57,9 +56,10 @@ void setup() #ifdef TOMCAT_PORT serveTomcat(); #endif + } void loop() { - ArduinoOTA.handle(); + LoopOTA(); } \ No newline at end of file diff --git a/src/system/ota.cpp b/src/system/ota.cpp index e6bcc3d..6979bb9 100644 --- a/src/system/ota.cpp +++ b/src/system/ota.cpp @@ -32,3 +32,7 @@ void configureOTA() ArduinoOTA.setRebootOnSuccess(true); ArduinoOTA.begin(); } + +void LoopOta(){ + ArduinoOTA.handle(); +} \ No newline at end of file From 43905a705f3c4bb918515cbf19c5c527709a8801 Mon Sep 17 00:00:00 2001 From: Dmitriy Shafranskiy Date: Thu, 18 Feb 2021 08:18:05 +0100 Subject: [PATCH 2/9] extracted conosle logging into separate class --- src/reporting/consolelog.cpp | 13 +++++++++++++ src/reporting/consolelog.h | 11 +++++++++++ src/reporting/mqtt.cpp | 3 --- 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 src/reporting/consolelog.cpp create mode 100644 src/reporting/consolelog.h diff --git a/src/reporting/consolelog.cpp b/src/reporting/consolelog.cpp new file mode 100644 index 0000000..055d4bf --- /dev/null +++ b/src/reporting/consolelog.cpp @@ -0,0 +1,13 @@ +#include "Arduino.h" + +void consoleLogNotifyAttackOccurred(String attackerIpAddress){ + Serial.println("[NOTIFICATION]: Attack occured from: " + attackerIpAddress); +} + +void consoleLogResetAttackState(){ + Serial.println("Resetting attack state"); +} + +void consoleLogNotify(String message){ + Serial.println("[NOTIFICATION]: " + message); +} \ No newline at end of file diff --git a/src/reporting/consolelog.h b/src/reporting/consolelog.h new file mode 100644 index 0000000..c801a45 --- /dev/null +++ b/src/reporting/consolelog.h @@ -0,0 +1,11 @@ +#pragma once +#ifndef _CONSOLE_LOG_H_ +#define _CONSOLE_LOG_H_ + +void consoleLogNotifyAttackOccurred(String attackerIpAddress); + +void consoleLogResetAttackState(); + +void consoleLogNotify(String message); + +#endif \ No newline at end of file diff --git a/src/reporting/mqtt.cpp b/src/reporting/mqtt.cpp index e31ad98..313e6d2 100644 --- a/src/reporting/mqtt.cpp +++ b/src/reporting/mqtt.cpp @@ -13,14 +13,11 @@ void mqttNotify(String message){ //TODO - subscribe to reset / configure commands in topic ? void mqttNotifyAttackOccurred(String attackerIpAddress){ - Serial.println("Attack occured from: " + attackerIpAddress); - mqttClient.publish("/security/honeypot/attackinprogress", 2, true, "True"); mqttClient.publish("/security/honeypot/attackerip", 2, true, attackerIpAddress.c_str()); } void mqttResetAttackState(){ - Serial.println("Resetting attack state"); mqttClient.publish("/security/honeypot/attackinprogress", 2, false, "False"); } From 137ffff54b22927bc40552d99dce8ebf49afe3ea Mon Sep 17 00:00:00 2001 From: Dmitriy Shafranskiy Date: Thu, 18 Feb 2021 08:18:29 +0100 Subject: [PATCH 3/9] missing header file --- src/system/ota.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/system/ota.h b/src/system/ota.h index c3b04d7..c7a1b0e 100644 --- a/src/system/ota.h +++ b/src/system/ota.h @@ -3,5 +3,6 @@ #define _OTA_H_ void configureOTA(); +void LoopOTA(); #endif \ No newline at end of file From d69c709f4582e397cd2e74eb040d08d6eb8b3be5 Mon Sep 17 00:00:00 2001 From: Dmitriy Shafranskiy Date: Thu, 18 Feb 2021 08:41:07 +0100 Subject: [PATCH 4/9] added e-mail notifications. tested with esp8266 --- platformio.ini | 17 ++++-- src/main.cpp | 3 +- src/reporting/email.cpp | 101 ++++++++++++++++++++++++++++++++++++ src/reporting/email.h | 7 +++ src/reporting/reporting.cpp | 65 ++++++++++++++++++++++- src/reporting/reporting.h | 1 + src/system/ota.cpp | 2 +- src/user_config.h | 15 ++++-- 8 files changed, 200 insertions(+), 11 deletions(-) create mode 100644 src/reporting/email.cpp create mode 100644 src/reporting/email.h diff --git a/platformio.ini b/platformio.ini index 79439e8..c4e048d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -2,7 +2,7 @@ src_dir = src lib_dir = lib data_dir = resources -default_envs = esp32 +default_envs = nodemcuv2 ; extra_scripts = LittleFSBuilder.py @@ -18,6 +18,7 @@ lib_deps = AsyncMqttClient@0.8.2 arduino-libraries/NTPClient 1532 ;thijse/Arduino-Log + mobizt/ESP Mail Client@^1.0.13 build_unflags = -Wdeprecated-declarations @@ -26,10 +27,16 @@ build_unflags = platform = espressif8266 board = nodemcuv2 +build_flags = + -Teagle.flash.4m.ld + [env:nodemcuv2ota] platform = espressif8266 board = nodemcuv2 +build_flags = + -Teagle.flash.4m.ld + # Don't forget to allow app through firewall - for platformio & Python upload_port = 192.168.1.1 upload_protocol = espota @@ -39,11 +46,11 @@ upload_flags = -i 192.168.1.1 ; -a 'H0We8Adu0SYGFUNTt25C7yuJ' -build_flags = - -Teagle.flash.4m.ld - [env:esp32] platform = espressif32 board = esp32dev build_flags = - -Teagle.flash.4m.ld \ No newline at end of file + -Teagle.flash.4m.ld + +# include_libs +# ;mobizt/ESP32 Mail Client@^2.1.6 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 34ece2c..85e497f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -61,5 +61,6 @@ void setup() void loop() { - LoopOTA(); + LoopOTA(); + notifyLoop(); } \ No newline at end of file diff --git a/src/reporting/email.cpp b/src/reporting/email.cpp new file mode 100644 index 0000000..059f3dd --- /dev/null +++ b/src/reporting/email.cpp @@ -0,0 +1,101 @@ +#include +#include "user_config.h" +#include +#include +#include + +SMTPSession smtp; +ESP_Mail_Session session; + +void smtpCallback(SMTP_Status status); + +void emailInit(){ + smtp.debug(1); + + smtp.callback(smtpCallback); + + session.server.host_name = SMTP_HOST; + session.server.port = SMTP_PORT; + session.login.email = AUTHOR_EMAIL; + session.login.password = AUTHOR_PASSWORD; + // session.login.user_domain = "mydomain.net"; +} + +void sendMail(const char* subject, const char* mailBody) +{ + SMTP_Message message; + + message.sender.name = "ESP Honeypot"; + message.sender.email = AUTHOR_EMAIL; + message.subject = subject; + message.addRecipient("Honeypot User", EMAIL_TARGET_RECEPIENT); + + message.text.content = mailBody; + +// TODO - should we expose encding here as param? + message.text.charSet = "utf-8"; + + /** The content transfer encoding e.g. + * enc_7bit or "7bit" (not encoded) + * enc_qp or "quoted-printable" (encoded) + * enc_base64 or "base64" (encoded) + * enc_binary or "binary" (not encoded) + * enc_8bit or "8bit" (not encoded) + * The default value is "7bit" + */ + // message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit; + + /** The message priority + * esp_mail_smtp_priority_high or 1 + * esp_mail_smtp_priority_normal or 3 + * esp_mail_smtp_priority_low or 5 + * The default value is esp_mail_smtp_priority_low + */ + message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_normal; + + /** The Delivery Status Notifications e.g. + * esp_mail_smtp_notify_never + * esp_mail_smtp_notify_success + * esp_mail_smtp_notify_failure + * esp_mail_smtp_notify_delay + * The default value is esp_mail_smtp_notify_never + */ + message.response.notify = esp_mail_smtp_notify_success | esp_mail_smtp_notify_failure | esp_mail_smtp_notify_delay; + + /* Connect to server with the session config */ + if (!smtp.connect(&session)) + return; + + /* Start sending Email and close the session */ + if (!MailClient.sendMail(&smtp, &message)) + Serial.println("Error sending Email, " + smtp.errorReason()); +} + +void smtpCallback(SMTP_Status status) +{ + /* Print the current status */ + Serial.println(status.info()); + + /* Print the sending result */ + if (status.success()) + { + Serial.println("----------------"); + Serial.printf("Message sent success: %d\n", status.completedCount()); + Serial.printf("Message sent failled: %d\n", status.failedCount()); + Serial.println("----------------\n"); + struct tm dt; + + for (size_t i = 0; i < smtp.sendingResult.size(); i++) + { + SMTP_Result result = smtp.sendingResult.getItem(i); + localtime_r(&result.timesstamp, &dt); + + Serial.printf("Message No: %d\n", i + 1); + Serial.printf("Status: %s\n", result.completed ? "success" : "failed"); + Serial.printf("Date/Time: %d/%d/%d %d:%d:%d\n", dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec); + Serial.printf("Recipient: %s\n", result.recipients); + Serial.printf("Subject: %s\n", result.subject); + } + Serial.println("----------------\n"); + } +} \ No newline at end of file diff --git a/src/reporting/email.h b/src/reporting/email.h new file mode 100644 index 0000000..8cf17e7 --- /dev/null +++ b/src/reporting/email.h @@ -0,0 +1,7 @@ +#pragma once +#ifndef _EMAIL_H_ +#define _EMAIL_H_ + +void sendMail(const char* subject, const char* mailBody); +void emailInit(); +#endif \ No newline at end of file diff --git a/src/reporting/reporting.cpp b/src/reporting/reporting.cpp index 9d96d8a..53cfc95 100644 --- a/src/reporting/reporting.cpp +++ b/src/reporting/reporting.cpp @@ -1,24 +1,65 @@ #include "Arduino.h" #include "../user_config.h" + +boolean messagesAvailable = false; + +String notifyMessage = ""; +String attackerIpAddress = ""; + +#if MQTT_ENABLED #include "mqtt.h" +#endif + +#if EMAIL_ENABLED +#include "email.h" +#endif + +#include "consolelog.h" void initReporting() { #if MQTT_ENABLED mqttInit(); #endif + +#if EMAIL_ENABLED + emailInit(); +#endif } + void notify(String message) { + messagesAvailable = true; + notifyMessage = message; +} + +void notifyAttackOccurred(String attackerIp) +{ + messagesAvailable = true; + attackerIpAddress = attackerIp; +} + +void sendNotify(String message) +{ + consoleLogNotify(message); + #if MQTT_ENABLED mqttNotify(message); #endif +#if EMAIL_ENABLED + sendMail(String("Notification").c_str(), message.c_str()); +#endif } -void notifyAttackOccurred(String attackerIpAddress) +void sendNotifyAttackOccurred(String attackerIpAddress) { + consoleLogNotifyAttackOccurred(attackerIpAddress); + #if MQTT_ENABLED mqttNotifyAttackOccurred(attackerIpAddress); #endif +#if EMAIL_ENABLED + sendMail("Attack had occurred!", attackerIpAddress.c_str()); +#endif } void resetAttackState() { @@ -26,3 +67,25 @@ void resetAttackState() mqttResetAttackState(); #endif } + +void notifyLoop() +{ + if (!messagesAvailable) + { + return; + } + + if (notifyMessage.length() > 0) + { + sendNotify(notifyMessage); + notifyMessage = ""; + messagesAvailable = false; + } + + if (attackerIpAddress.length() > 0) + { + sendNotifyAttackOccurred(attackerIpAddress); + attackerIpAddress = ""; + messagesAvailable = false; + } +} \ No newline at end of file diff --git a/src/reporting/reporting.h b/src/reporting/reporting.h index 3fe4797..b9951fc 100644 --- a/src/reporting/reporting.h +++ b/src/reporting/reporting.h @@ -6,5 +6,6 @@ void notifyAttackOccurred(String attackerIpAddress); void notify(String message); void resetAttackState(); void initReporting(); +void notifyLoop(); #endif \ No newline at end of file diff --git a/src/system/ota.cpp b/src/system/ota.cpp index 6979bb9..ed08980 100644 --- a/src/system/ota.cpp +++ b/src/system/ota.cpp @@ -33,6 +33,6 @@ void configureOTA() ArduinoOTA.begin(); } -void LoopOta(){ +void LoopOTA(){ ArduinoOTA.handle(); } \ No newline at end of file diff --git a/src/user_config.h b/src/user_config.h index 5166b33..80dc5a2 100644 --- a/src/user_config.h +++ b/src/user_config.h @@ -2,7 +2,7 @@ #define user_config_h #ifndef WIFI_SERVER_AP_NAME - #define WIFI_SERVER_AP_NAME "WFT" + #define WIFI_SERVER_AP_NAME "WZA23B" #endif #ifndef WIFI_SERVER_AP_PASSWORD @@ -15,7 +15,7 @@ #define HOST_NAME "ds214" -#define MAC "00:11:32:85:ac:29" +#define MAC "00:11:32:85:ac:16" #define NTP_TIME_OFFSET 7200 @@ -25,7 +25,7 @@ //============ REPORTING SECTION ================ // -#define MQTT_ENABLED true +#define MQTT_ENABLED false #define MQTT_HOST "192.168.1.1" #define MQTT_TOPIC "/security/honeypot" #define MQTT_PORT 1883 @@ -33,4 +33,13 @@ #define MQTT_USER "Honeypot" #define MQTT_PASSWORD "NDCU74EJoh2N69GRhMfc" + +// =========== EMAIL =========================== +#define EMAIL_ENABLED false +#define SMTP_HOST "smtp.google.com" +#define SMTP_PORT 465 +#define AUTHOR_EMAIL "sender@gmail.com" +#define AUTHOR_PASSWORD "sender_password" +#define EMAIL_TARGET_RECEPIENT "recepient@gmail.com" + #endif \ No newline at end of file From 17a92ba02b6d9091ddcba13fa89e433ecd18d453 Mon Sep 17 00:00:00 2001 From: Dmitriy Shafranskiy Date: Thu, 18 Feb 2021 08:42:09 +0100 Subject: [PATCH 5/9] more config updates --- src/user_config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/user_config.h b/src/user_config.h index 80dc5a2..9e8e651 100644 --- a/src/user_config.h +++ b/src/user_config.h @@ -2,7 +2,7 @@ #define user_config_h #ifndef WIFI_SERVER_AP_NAME - #define WIFI_SERVER_AP_NAME "WZA23B" + #define WIFI_SERVER_AP_NAME "WFT" #endif #ifndef WIFI_SERVER_AP_PASSWORD From c5d28cbf2fad2edf68e9c08f93f3c5306247d190 Mon Sep 17 00:00:00 2001 From: Dmitriy Shafranskiy Date: Thu, 18 Feb 2021 08:45:03 +0100 Subject: [PATCH 6/9] building any branch --- .github/workflows/esp.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/esp.yml b/.github/workflows/esp.yml index de04cf9..19246a0 100644 --- a/.github/workflows/esp.yml +++ b/.github/workflows/esp.yml @@ -1,10 +1,5 @@ name: ESP Build -on: - push: - branches: - - master - jobs: test_builds: From 81ecab4a9db393ce9da235e1df38ccb0274b957d Mon Sep 17 00:00:00 2001 From: Dmitriy Shafranskiy Date: Thu, 18 Feb 2021 08:46:07 +0100 Subject: [PATCH 7/9] any --- .github/workflows/esp.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/esp.yml b/.github/workflows/esp.yml index 19246a0..7a80fd0 100644 --- a/.github/workflows/esp.yml +++ b/.github/workflows/esp.yml @@ -1,5 +1,8 @@ name: ESP Build +on: + push: + jobs: test_builds: From 5cde4a92794d04bad2a10ed3b9d444be6640c163 Mon Sep 17 00:00:00 2001 From: Dmitriy Shafranskiy Date: Fri, 19 Feb 2021 08:17:53 +0100 Subject: [PATCH 8/9] trying to fix CI --- .github/workflows/esp.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/esp.yml b/.github/workflows/esp.yml index 7a80fd0..dee29fd 100644 --- a/.github/workflows/esp.yml +++ b/.github/workflows/esp.yml @@ -9,6 +9,7 @@ jobs: runs-on: ubuntu-latest strategy: + fail-fast: false matrix: test-platform: - nodemcuv2 From 62e8e41686f62f6112177286957015d7df8dd59d Mon Sep 17 00:00:00 2001 From: Dmitriy Shafranskiy Date: Fri, 19 Feb 2021 17:30:26 +0100 Subject: [PATCH 9/9] fixed esp32 build issues --- src/reporting/email.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/reporting/email.cpp b/src/reporting/email.cpp index 059f3dd..ac1933d 100644 --- a/src/reporting/email.cpp +++ b/src/reporting/email.cpp @@ -1,7 +1,11 @@ #include #include "user_config.h" #include + +#ifdef ESP8266 #include +#endif + #include SMTPSession smtp;