diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 33037996..f98e73b3 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -359,10 +359,19 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr #if MESH_DEBUG MESH_DEBUG_PRINTLN("Login, sender not in ACL"); #endif + } else { + // CUSTOM (TeTeHacko): a blank login has a replay floor too -- replayed, it + // gained nothing, but each copy made us flood a reply over the mesh. + if (sender_timestamp <= client->last_timestamp) return 0; + acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); + client->last_timestamp = sender_timestamp; } } if (client == NULL) { uint8_t perms; + // CUSTOM (TeTeHacko): capped wrong passwords (LoginGuard.h); a known admin is never locked out + ClientInfo* known = acl.getClient(sender.pub_key, PUB_KEY_SIZE); + if (login_guard.locked(millis()) && !(known && known->isAdmin())) return 0; if (strcmp((char *)data, _prefs.password) == 0) { // check for valid admin password perms = PERM_ACL_ADMIN; } else if (strcmp((char *)data, _prefs.guest_password) == 0) { // check guest password @@ -371,6 +380,7 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr #if MESH_DEBUG MESH_DEBUG_PRINTLN("Invalid password: %s", data); #endif + login_guard.failed(millis()); // CUSTOM (TeTeHacko) return 0; } diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index e3c52dd3..1493952f 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -27,6 +27,7 @@ #include #include #include +#include // CUSTOM (TeTeHacko) #include #include #include @@ -197,6 +198,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { bool _logging; NodePrefs _prefs; ClientACL acl; + LoginGuard login_guard; // CUSTOM (TeTeHacko) CommonCLI _cli; uint8_t reply_data[MAX_PACKET_PAYLOAD]; uint8_t reply_path[MAX_PATH_SIZE]; diff --git a/examples/simple_room_server/MyMesh.cpp b/examples/simple_room_server/MyMesh.cpp index a20c780b..d5a5628a 100644 --- a/examples/simple_room_server/MyMesh.cpp +++ b/examples/simple_room_server/MyMesh.cpp @@ -350,10 +350,19 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m #if MESH_DEBUG MESH_DEBUG_PRINTLN("Login, sender not in ACL"); #endif + } else { + // CUSTOM (TeTeHacko): a blank login has a replay floor too -- replayed, it + // gained nothing, but each copy made us flood a reply over the mesh. + if (sender_timestamp <= client->last_timestamp) return; + acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); + client->last_timestamp = sender_timestamp; } } if (client == NULL) { uint8_t perm; + // CUSTOM (TeTeHacko): capped wrong passwords (LoginGuard.h); a known admin is never locked out + ClientInfo* known = acl.getClient(sender.pub_key, PUB_KEY_SIZE); + if (login_guard.locked(millis()) && !(known && known->isAdmin())) return; if (strcmp((char *)&data[8], _prefs.password) == 0) { // check for valid admin password perm = PERM_ACL_ADMIN; } else { @@ -363,6 +372,7 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m perm = PERM_ACL_GUEST; } else { MESH_DEBUG_PRINTLN("Incorrect room password"); + login_guard.failed(millis()); // CUSTOM (TeTeHacko) return; // no response. Client will timeout } } diff --git a/examples/simple_room_server/MyMesh.h b/examples/simple_room_server/MyMesh.h index 896ae832..1b429a29 100644 --- a/examples/simple_room_server/MyMesh.h +++ b/examples/simple_room_server/MyMesh.h @@ -20,6 +20,7 @@ #include #include #include +#include // CUSTOM (TeTeHacko) #include #include #include @@ -100,6 +101,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { TransportKeyStore key_store; RegionMap region_map, temp_map; ClientACL acl; + LoginGuard login_guard; // CUSTOM (TeTeHacko) CommonCLI _cli; unsigned long dirty_contacts_expiry; uint8_t reply_data[MAX_PACKET_PAYLOAD]; diff --git a/examples/simple_sensor/SensorMesh.cpp b/examples/simple_sensor/SensorMesh.cpp index ecb91e3e..eac8e3e5 100644 --- a/examples/simple_sensor/SensorMesh.cpp +++ b/examples/simple_sensor/SensorMesh.cpp @@ -374,11 +374,20 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* #endif return 0; } + // CUSTOM (TeTeHacko): a blank login has a replay floor too -- replayed, it + // gained nothing, but each copy made us send a reply. + if (sender_timestamp <= client->last_timestamp) return 0; + acl.persistTimestamp(client, sender_timestamp, getRTCClock()->getCurrentTime()); + client->last_timestamp = sender_timestamp; } else { + // CUSTOM (TeTeHacko): capped wrong passwords (LoginGuard.h); a known admin is never locked out + ClientInfo* known = acl.getClient(sender.pub_key, PUB_KEY_SIZE); + if (login_guard.locked(millis()) && !(known && known->isAdmin())) return 0; if (strcmp((char *) data, _prefs.password) != 0) { // check for valid admin password #if MESH_DEBUG MESH_DEBUG_PRINTLN("Invalid password: %s", &data[0]); #endif + login_guard.failed(millis()); // CUSTOM (TeTeHacko) return 0; } diff --git a/examples/simple_sensor/SensorMesh.h b/examples/simple_sensor/SensorMesh.h index 641ef3dc..d941d6d6 100644 --- a/examples/simple_sensor/SensorMesh.h +++ b/examples/simple_sensor/SensorMesh.h @@ -22,6 +22,7 @@ #include #include #include +#include // CUSTOM (TeTeHacko) #include #include #include @@ -149,6 +150,7 @@ private: unsigned long next_local_advert, next_flood_advert; NodePrefs _prefs; ClientACL acl; + LoginGuard login_guard; // CUSTOM (TeTeHacko) CommonCLI _cli; uint8_t reply_data[MAX_PACKET_PAYLOAD]; unsigned long dirty_contacts_expiry; diff --git a/src/helpers/LoginGuard.h b/src/helpers/LoginGuard.h new file mode 100644 index 00000000..03d1a99e --- /dev/null +++ b/src/helpers/LoginGuard.h @@ -0,0 +1,41 @@ +#pragma once + +#include + +// CUSTOM (TeTeHacko): a cap on wrong passwords. Upstream checks every login, +// so a password can be guessed over the air as fast as the channel carries +// packets, from a fresh identity each time, and a wrong one is not even +// answered. After LOGIN_FAIL_MAX wrong passwords within LOGIN_FAIL_SECS no +// password is checked until that window ends. +// +// The caller lets a client that is already an admin in the ACL through anyway: +// a login is encrypted with the sender's key, so nobody can send one in an +// admin's name, and a guessing attack cannot lock the admins out. +// +// Times are millis(), compared by difference: a clock set backwards or the +// 49-day wrap cannot stretch the window. + +#ifndef LOGIN_FAIL_MAX + #define LOGIN_FAIL_MAX 4 +#endif +#ifndef LOGIN_FAIL_SECS + #define LOGIN_FAIL_SECS 180 +#endif + +class LoginGuard { + uint32_t _since = 0; // millis() of the first failure in the window + uint16_t _fails = 0; + + bool inWindow(uint32_t now_ms) const { return now_ms - _since < LOGIN_FAIL_SECS * 1000UL; } + +public: + bool locked(uint32_t now_ms) const { return _fails >= LOGIN_FAIL_MAX && inWindow(now_ms); } + + void failed(uint32_t now_ms) { + if (_fails == 0 || !inWindow(now_ms)) { + _since = now_ms; + _fails = 0; + } + if (_fails < LOGIN_FAIL_MAX) _fails++; + } +}; diff --git a/test/test_login_guard/test_login_guard.cpp b/test/test_login_guard/test_login_guard.cpp new file mode 100644 index 00000000..ff5a70f4 --- /dev/null +++ b/test/test_login_guard/test_login_guard.cpp @@ -0,0 +1,47 @@ +#include +#include "helpers/LoginGuard.h" + +static const uint32_t WINDOW_MS = LOGIN_FAIL_SECS * 1000UL; + +TEST(LoginGuard, LocksAfterTheCap) { + LoginGuard g; + for (int i = 0; i < LOGIN_FAIL_MAX; i++) { + EXPECT_FALSE(g.locked(1000 + i)); + g.failed(1000 + i); + } + EXPECT_TRUE(g.locked(1000 + LOGIN_FAIL_MAX)); +} + +TEST(LoginGuard, UnlocksWhenTheWindowEnds) { + LoginGuard g; + for (int i = 0; i < LOGIN_FAIL_MAX; i++) g.failed(1000); + EXPECT_TRUE(g.locked(1000 + WINDOW_MS - 1)); + EXPECT_FALSE(g.locked(1000 + WINDOW_MS)); +} + +TEST(LoginGuard, FailuresInAnOldWindowDoNotCount) { + LoginGuard g; + for (int i = 0; i < LOGIN_FAIL_MAX - 1; i++) g.failed(1000); + g.failed(1000 + WINDOW_MS); // a new window: one failure, not LOGIN_FAIL_MAX + EXPECT_FALSE(g.locked(1000 + WINDOW_MS + 1)); +} + +TEST(LoginGuard, LockedFailuresDoNotExtendTheWindow) { + LoginGuard g; + for (int i = 0; i < LOGIN_FAIL_MAX; i++) g.failed(1000); + for (uint32_t t = 1000; t < 1000 + WINDOW_MS; t += 1000) g.failed(t); // the caller does not, but still + EXPECT_FALSE(g.locked(1000 + WINDOW_MS)); +} + +TEST(LoginGuard, SurvivesTheMillisWrap) { + LoginGuard g; + const uint32_t start = 0xFFFFFFFFu - 1000; + for (int i = 0; i < LOGIN_FAIL_MAX; i++) g.failed(start); + EXPECT_TRUE(g.locked(start + 2000)); // wrapped past 0 + EXPECT_FALSE(g.locked(start + WINDOW_MS)); +} + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}