Repeater, room server, sensor: passwords can no longer be guessed over the air at full speed, and a replayed login no longer floods a reply
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
de92ca9ba2
commit
dbdae14566
8 files changed
+123
No files matched your search
@@ -359,10 +359,19 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr
|
|||||||
#if MESH_DEBUG
|
#if MESH_DEBUG
|
||||||
MESH_DEBUG_PRINTLN("Login, sender not in ACL");
|
MESH_DEBUG_PRINTLN("Login, sender not in ACL");
|
||||||
#endif
|
#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) {
|
if (client == NULL) {
|
||||||
uint8_t perms;
|
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
|
if (strcmp((char *)data, _prefs.password) == 0) { // check for valid admin password
|
||||||
perms = PERM_ACL_ADMIN;
|
perms = PERM_ACL_ADMIN;
|
||||||
} else if (strcmp((char *)data, _prefs.guest_password) == 0) { // check guest password
|
} 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
|
#if MESH_DEBUG
|
||||||
MESH_DEBUG_PRINTLN("Invalid password: %s", data);
|
MESH_DEBUG_PRINTLN("Invalid password: %s", data);
|
||||||
#endif
|
#endif
|
||||||
|
login_guard.failed(millis()); // CUSTOM (TeTeHacko)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
#include <helpers/AdvertDataHelpers.h>
|
#include <helpers/AdvertDataHelpers.h>
|
||||||
#include <helpers/ArduinoHelpers.h>
|
#include <helpers/ArduinoHelpers.h>
|
||||||
#include <helpers/ClientACL.h>
|
#include <helpers/ClientACL.h>
|
||||||
|
#include <helpers/LoginGuard.h> // CUSTOM (TeTeHacko)
|
||||||
#include <helpers/CommonCLI.h>
|
#include <helpers/CommonCLI.h>
|
||||||
#include <helpers/IdentityStore.h>
|
#include <helpers/IdentityStore.h>
|
||||||
#include <helpers/SimpleMeshTables.h>
|
#include <helpers/SimpleMeshTables.h>
|
||||||
@@ -197,6 +198,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
|||||||
bool _logging;
|
bool _logging;
|
||||||
NodePrefs _prefs;
|
NodePrefs _prefs;
|
||||||
ClientACL acl;
|
ClientACL acl;
|
||||||
|
LoginGuard login_guard; // CUSTOM (TeTeHacko)
|
||||||
CommonCLI _cli;
|
CommonCLI _cli;
|
||||||
uint8_t reply_data[MAX_PACKET_PAYLOAD];
|
uint8_t reply_data[MAX_PACKET_PAYLOAD];
|
||||||
uint8_t reply_path[MAX_PATH_SIZE];
|
uint8_t reply_path[MAX_PATH_SIZE];
|
||||||
|
|||||||
@@ -350,10 +350,19 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
|
|||||||
#if MESH_DEBUG
|
#if MESH_DEBUG
|
||||||
MESH_DEBUG_PRINTLN("Login, sender not in ACL");
|
MESH_DEBUG_PRINTLN("Login, sender not in ACL");
|
||||||
#endif
|
#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) {
|
if (client == NULL) {
|
||||||
uint8_t perm;
|
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
|
if (strcmp((char *)&data[8], _prefs.password) == 0) { // check for valid admin password
|
||||||
perm = PERM_ACL_ADMIN;
|
perm = PERM_ACL_ADMIN;
|
||||||
} else {
|
} else {
|
||||||
@@ -363,6 +372,7 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
|
|||||||
perm = PERM_ACL_GUEST;
|
perm = PERM_ACL_GUEST;
|
||||||
} else {
|
} else {
|
||||||
MESH_DEBUG_PRINTLN("Incorrect room password");
|
MESH_DEBUG_PRINTLN("Incorrect room password");
|
||||||
|
login_guard.failed(millis()); // CUSTOM (TeTeHacko)
|
||||||
return; // no response. Client will timeout
|
return; // no response. Client will timeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#include <helpers/CommonCLI.h>
|
#include <helpers/CommonCLI.h>
|
||||||
#include <helpers/StatsFormatHelper.h>
|
#include <helpers/StatsFormatHelper.h>
|
||||||
#include <helpers/ClientACL.h>
|
#include <helpers/ClientACL.h>
|
||||||
|
#include <helpers/LoginGuard.h> // CUSTOM (TeTeHacko)
|
||||||
#include <helpers/RegionMap.h>
|
#include <helpers/RegionMap.h>
|
||||||
#include <helpers/RoutingPolicy.h>
|
#include <helpers/RoutingPolicy.h>
|
||||||
#include <RTClib.h>
|
#include <RTClib.h>
|
||||||
@@ -100,6 +101,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
|||||||
TransportKeyStore key_store;
|
TransportKeyStore key_store;
|
||||||
RegionMap region_map, temp_map;
|
RegionMap region_map, temp_map;
|
||||||
ClientACL acl;
|
ClientACL acl;
|
||||||
|
LoginGuard login_guard; // CUSTOM (TeTeHacko)
|
||||||
CommonCLI _cli;
|
CommonCLI _cli;
|
||||||
unsigned long dirty_contacts_expiry;
|
unsigned long dirty_contacts_expiry;
|
||||||
uint8_t reply_data[MAX_PACKET_PAYLOAD];
|
uint8_t reply_data[MAX_PACKET_PAYLOAD];
|
||||||
|
|||||||
@@ -374,11 +374,20 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t*
|
|||||||
#endif
|
#endif
|
||||||
return 0;
|
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 {
|
} 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 (strcmp((char *) data, _prefs.password) != 0) { // check for valid admin password
|
||||||
#if MESH_DEBUG
|
#if MESH_DEBUG
|
||||||
MESH_DEBUG_PRINTLN("Invalid password: %s", &data[0]);
|
MESH_DEBUG_PRINTLN("Invalid password: %s", &data[0]);
|
||||||
#endif
|
#endif
|
||||||
|
login_guard.failed(millis()); // CUSTOM (TeTeHacko)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
#include <helpers/CommonCLI.h>
|
#include <helpers/CommonCLI.h>
|
||||||
#include <helpers/StatsFormatHelper.h>
|
#include <helpers/StatsFormatHelper.h>
|
||||||
#include <helpers/ClientACL.h>
|
#include <helpers/ClientACL.h>
|
||||||
|
#include <helpers/LoginGuard.h> // CUSTOM (TeTeHacko)
|
||||||
#include <helpers/RegionMap.h>
|
#include <helpers/RegionMap.h>
|
||||||
#include <RTClib.h>
|
#include <RTClib.h>
|
||||||
#include <target.h>
|
#include <target.h>
|
||||||
@@ -149,6 +150,7 @@ private:
|
|||||||
unsigned long next_local_advert, next_flood_advert;
|
unsigned long next_local_advert, next_flood_advert;
|
||||||
NodePrefs _prefs;
|
NodePrefs _prefs;
|
||||||
ClientACL acl;
|
ClientACL acl;
|
||||||
|
LoginGuard login_guard; // CUSTOM (TeTeHacko)
|
||||||
CommonCLI _cli;
|
CommonCLI _cli;
|
||||||
uint8_t reply_data[MAX_PACKET_PAYLOAD];
|
uint8_t reply_data[MAX_PACKET_PAYLOAD];
|
||||||
unsigned long dirty_contacts_expiry;
|
unsigned long dirty_contacts_expiry;
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
// 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++;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
#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();
|
||||||
|
}
|
||||||
Reference in new issue
Block a user