Fix: auto-revoke forged license passcodes and stop auth log flooding
A trial user's serial was alternating between the built-in trial passcode (authorized) and a forged 50-year/1000-host passcode (rejected) on every heartbeat. ShouldLogAuth keyed its throttle on the serial alone and logged every status flip, so the rate limit was defeated and the master log filled with alternating success/failure lines. ShouldLogAuth now tracks the success and failure states independently per serial and source IP: each logs on first sight and then at most once per hour, with a bounded cache. Keying only by serial would collapse the many normal trial users that share one serial into a single line. AutoRevokeForgedAuth handles the offending connection itself. Integrity failures (V1 codes 2/3, V2 code 4) are counted per connection context, and three within 60 seconds send the same days=0 CMD_AUTHORIZATION as the manual "revoke authorization" menu, with a 10-minute cooldown. It acts only when the same serial also had a successful auth in the last two minutes, which is the observed abuse pattern; a batch of failures after a password rotation or a client pointed at another master has no success event and is left alone. Expired and already-revoked licences never trigger it. Without a configured super password it only warns. Translations added. Debug x64 server builds; not runtime-tested. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
e265527610
commit
a32219f05c
5 files changed
+186
-20
No files matched your search
@@ -316,11 +316,14 @@ static CStringW AnsiToWide(const char* s, UINT cp = CP_ACP)
|
||||
return CStringW(w.c_str());
|
||||
}
|
||||
|
||||
// 授权日志频率控制:首次必须记录,状态变化必须记录,相同状态每小时记录一次
|
||||
static bool ShouldLogAuth(const std::string& sn, int success) {
|
||||
// 授权日志频率控制:按“SN + 来源 IP”的“成功/失败”各自独立限流——同一状态首次
|
||||
// 记录,之后每小时最多一次。不能只按 SN:同一个 SN 可能被大量正常用户共用
|
||||
// (线上 12ca-... 就是这种情况),只按 SN 会把他们折叠成一条日志。
|
||||
// 也不能只记一个 lastStatus:伪造口令与正常口令交替上报时,每次状态翻转都会触发
|
||||
// 记录,限流会被打穿(2026-12-06 线上已出现)。
|
||||
static bool ShouldLogAuth(const std::string& sn, const std::string& ip, int success) {
|
||||
struct AuthLogState {
|
||||
int lastStatus;
|
||||
time_t lastLogTime;
|
||||
time_t lastLog[2]; // [0]=失败,[1]=成功
|
||||
};
|
||||
static std::map<std::string, AuthLogState> s_cache;
|
||||
static CRITICAL_SECTION s_lock;
|
||||
@@ -331,25 +334,33 @@ static bool ShouldLogAuth(const std::string& sn, int success) {
|
||||
|
||||
EnterCriticalSection(&s_lock);
|
||||
time_t now = time(nullptr);
|
||||
const int idx = success ? 1 : 0;
|
||||
bool shouldLog = false;
|
||||
const std::string key = sn + "|" + ip;
|
||||
|
||||
auto it = s_cache.find(sn);
|
||||
auto it = s_cache.find(key);
|
||||
if (it == s_cache.end()) {
|
||||
// 首次 - 必须记录
|
||||
s_cache[sn] = {success, now};
|
||||
AuthLogState st = {};
|
||||
st.lastLog[idx] = now;
|
||||
s_cache[key] = st;
|
||||
shouldLog = true;
|
||||
} else {
|
||||
AuthLogState& state = it->second;
|
||||
if (state.lastStatus != success) {
|
||||
// 状态变化 - 必须记录
|
||||
state.lastStatus = success;
|
||||
state.lastLogTime = now;
|
||||
shouldLog = true;
|
||||
} else if (now - state.lastLogTime >= 3600) {
|
||||
// 相同状态,超过1小时 - 记录
|
||||
state.lastLogTime = now;
|
||||
shouldLog = true;
|
||||
} else if (it->second.lastLog[idx] == 0 || now - it->second.lastLog[idx] >= 3600) {
|
||||
it->second.lastLog[idx] = now;
|
||||
shouldLog = true;
|
||||
}
|
||||
|
||||
// 防御随机 SN/IP 造成的无界增长:超过阈值先清理一天未活跃的条目,仍超则整体清空
|
||||
if (s_cache.size() > 20000) {
|
||||
for (auto i = s_cache.begin(); i != s_cache.end(); ) {
|
||||
time_t last = i->second.lastLog[0] > i->second.lastLog[1]
|
||||
? i->second.lastLog[0] : i->second.lastLog[1];
|
||||
if (last != 0 && now - last >= 86400)
|
||||
i = s_cache.erase(i);
|
||||
else
|
||||
++i;
|
||||
}
|
||||
if (s_cache.size() > 20000)
|
||||
s_cache.clear();
|
||||
}
|
||||
LeaveCriticalSection(&s_lock);
|
||||
return shouldLog;
|
||||
@@ -9235,6 +9246,144 @@ std::pair<std::string, std::string> CMy2015RemoteDlg::GenerateRenewalInfo(
|
||||
return result;
|
||||
}
|
||||
|
||||
// 最近一次授权成功的时间(按 SN)。用于区分“同一 SN 交替成功/失败”的伪造行为
|
||||
// 与密码轮换/接错主控导致的整体失败——后者没有成功事件,不能自动清空客户端授权。
|
||||
static CRITICAL_SECTION& AuthSuccessLock()
|
||||
{
|
||||
static CRITICAL_SECTION s_lock;
|
||||
static std::once_flag s_initFlag;
|
||||
std::call_once(s_initFlag, []() {
|
||||
InitializeCriticalSection(&s_lock);
|
||||
});
|
||||
return s_lock;
|
||||
}
|
||||
|
||||
static std::map<std::string, time_t>& AuthSuccessTimes()
|
||||
{
|
||||
static std::map<std::string, time_t> s_times;
|
||||
return s_times;
|
||||
}
|
||||
|
||||
static void NoteAuthSuccess(const std::string& sn)
|
||||
{
|
||||
CRITICAL_SECTION& lock = AuthSuccessLock();
|
||||
EnterCriticalSection(&lock);
|
||||
time_t now = time(nullptr);
|
||||
auto& times = AuthSuccessTimes();
|
||||
times[sn] = now;
|
||||
if (times.size() > 10000) {
|
||||
for (auto i = times.begin(); i != times.end(); ) {
|
||||
if (now - i->second >= 3600)
|
||||
i = times.erase(i);
|
||||
else
|
||||
++i;
|
||||
}
|
||||
if (times.size() > 10000)
|
||||
times.clear();
|
||||
}
|
||||
LeaveCriticalSection(&lock);
|
||||
}
|
||||
|
||||
static bool HasRecentAuthSuccess(const std::string& sn, int windowSec)
|
||||
{
|
||||
CRITICAL_SECTION& lock = AuthSuccessLock();
|
||||
EnterCriticalSection(&lock);
|
||||
auto it = AuthSuccessTimes().find(sn);
|
||||
bool recent = it != AuthSuccessTimes().end() && time(nullptr) - it->second <= windowSec;
|
||||
LeaveCriticalSection(&lock);
|
||||
return recent;
|
||||
}
|
||||
|
||||
// 伪造口令自动处置:同一连接 60 秒内 3 次完整性校验失败,且同一 SN 最近有授权成功
|
||||
// (即“交替成功/失败”,线上伪造口令的典型形态)→ 下发与右键“撤销授权”相同的
|
||||
// days=0 命令。只影响出问题的连接,不按 SN 吊销——同一个 SN 可能同时有使用正常
|
||||
// 试用口令的实例在线。不满足“最近成功”条件时不动手:密码轮换/接错主控会让一批
|
||||
// 客户端持续失败,自动清空它们的授权属于误伤。
|
||||
// reason 只用于日志:V1 的 2=fixed key 不符、3=HMAC 不符;V2 的 4=签名校验失败。
|
||||
void CMy2015RemoteDlg::AutoRevokeForgedAuth(CONTEXT_OBJECT* ctx, const std::string& sn,
|
||||
const std::string& passcode, const std::string& ip,
|
||||
int reason)
|
||||
{
|
||||
if (!ctx) return;
|
||||
|
||||
struct State { int count; time_t windowStart; time_t lastAction; std::string peer; };
|
||||
static std::map<uintptr_t, State> s_state;
|
||||
static CRITICAL_SECTION s_lock;
|
||||
static std::once_flag s_initFlag;
|
||||
std::call_once(s_initFlag, []() {
|
||||
InitializeCriticalSection(&s_lock);
|
||||
});
|
||||
|
||||
// 不能用 GetPeerName 当连接标识:TCP/KCP/UDP 上下文只返回 IP,同一 NAT/FRP
|
||||
// 后的连接会共用计数。用上下文指针,并用 peer 字符串识别地址复用。
|
||||
const uintptr_t key = reinterpret_cast<uintptr_t>(ctx);
|
||||
const std::string peer = ctx->GetPeerName();
|
||||
const time_t now = time(nullptr);
|
||||
bool trigger = false;
|
||||
|
||||
EnterCriticalSection(&s_lock);
|
||||
auto it = s_state.find(key);
|
||||
if (it == s_state.end() || it->second.peer != peer) {
|
||||
State st = { 1, now, 0, peer };
|
||||
s_state[key] = st;
|
||||
it = s_state.find(key);
|
||||
} else {
|
||||
if (now - it->second.windowStart > 60) {
|
||||
it->second.windowStart = now;
|
||||
it->second.count = 0;
|
||||
}
|
||||
++it->second.count;
|
||||
}
|
||||
// 同一连接最多 10 分钟撤销一次,避免改版客户端无视命令后反复下发
|
||||
if (it->second.count >= 3 &&
|
||||
(it->second.lastAction == 0 || now - it->second.lastAction >= 600) &&
|
||||
HasRecentAuthSuccess(sn, 120)) {
|
||||
it->second.lastAction = now;
|
||||
it->second.count = 0;
|
||||
trigger = true;
|
||||
}
|
||||
// 连接状态表防膨胀
|
||||
if (s_state.size() > 4096) {
|
||||
for (auto i = s_state.begin(); i != s_state.end(); ) {
|
||||
if (now - i->second.windowStart >= 3600 && now - i->second.lastAction >= 3600)
|
||||
i = s_state.erase(i);
|
||||
else
|
||||
++i;
|
||||
}
|
||||
if (s_state.size() > 4096)
|
||||
s_state.clear();
|
||||
}
|
||||
LeaveCriticalSection(&s_lock);
|
||||
|
||||
if (!trigger)
|
||||
return;
|
||||
|
||||
Mprintf("[License] forged passcode detected: SN=%s passcode=%s [%s] reason=%d\n",
|
||||
sn.c_str(), passcode.c_str(), ip.c_str(), reason);
|
||||
|
||||
if (m_superPass.empty()) {
|
||||
Mprintf("[License] auto-revoke skipped: super password is not configured\n");
|
||||
std::string tip = std::string(_L("检测到伪造口令,但未配置超管密码,无法自动撤销")) + ": " + sn;
|
||||
PostMessageA(WM_SHOWMESSAGE, (WPARAM)new CharMsg(tip.c_str()), NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
// 与 OnOnlineUnauthorize 的“撤销授权”同格式:days=0 让对端清空授权并退出
|
||||
BYTE bToken[32] = { CMD_AUTHORIZATION };
|
||||
unsigned short days = 0;
|
||||
unsigned short num = 1;
|
||||
uint32_t tm = clock();
|
||||
memcpy(bToken + 1, &days, sizeof(days));
|
||||
memcpy(bToken + 3, &num, sizeof(num));
|
||||
memcpy(bToken + 8, &tm, sizeof(tm));
|
||||
uint64_t signature = SignMessage(m_superPass, bToken, 12);
|
||||
memcpy(bToken + 12, &signature, sizeof(signature));
|
||||
ctx->Send2Client(bToken, sizeof(bToken));
|
||||
|
||||
std::string tip = sn + " " + std::string(_L("使用伪造口令,已自动撤销授权")) + ": " + ip;
|
||||
PostMessageA(WM_SHOWMESSAGE, (WPARAM)new CharMsg(tip.c_str()), NULL);
|
||||
}
|
||||
|
||||
// 统一的授权验证函数
|
||||
// source: 验证来源 ("AUTH"=TOKEN_AUTH, "HB"=心跳)
|
||||
// 返回: (authorized, isV2, isTrail, expired)
|
||||
@@ -9259,7 +9408,9 @@ std::tuple<bool, bool, bool, bool> CMy2015RemoteDlg::VerifyClientAuth(context* h
|
||||
}
|
||||
isTrail = IsTrail(passcode.c_str());
|
||||
}
|
||||
if (ShouldLogAuth(sn, authorized)) {
|
||||
if (authorized == 4) // V2 签名校验失败 = 伪造/篡改口令
|
||||
AutoRevokeForgedAuth(sender, sn, passcode, ip, authorized);
|
||||
if (ShouldLogAuth(sn, ip, authorized == 0)) {
|
||||
if (authorized == 0) {
|
||||
Mprintf("[%s] %s V2 授权成功: %s [%s]\n", source, passcode.c_str(), sn.c_str(), ip.c_str());
|
||||
std::string tip = passcode + std::string(_L(" V2 授权成功: ")) + sn + "[" + ip + "]";
|
||||
@@ -9281,7 +9432,9 @@ std::tuple<bool, bool, bool, bool> CMy2015RemoteDlg::VerifyClientAuth(context* h
|
||||
}
|
||||
isTrail = IsTrail(passcode.c_str());
|
||||
}
|
||||
if (ShouldLogAuth(sn, authorized)) {
|
||||
if (authorized == 2 || authorized == 3) // fixed key / HMAC 校验失败 = 伪造/篡改口令
|
||||
AutoRevokeForgedAuth(sender, sn, passcode, ip, authorized);
|
||||
if (ShouldLogAuth(sn, ip, authorized == 0)) {
|
||||
if (authorized == 0) {
|
||||
Mprintf("[%s] %s V1 授权成功: %s [%s]\n", source, passcode.c_str(), sn.c_str(), ip.c_str());
|
||||
std::string tip = passcode + std::string(_L(" V1 授权成功: ")) + sn + "[" + ip + "]";
|
||||
@@ -9295,6 +9448,9 @@ std::tuple<bool, bool, bool, bool> CMy2015RemoteDlg::VerifyClientAuth(context* h
|
||||
}
|
||||
}
|
||||
|
||||
if (authorized == 0)
|
||||
NoteAuthSuccess(sn);
|
||||
|
||||
return std::make_tuple(authorized==0, isV2, isTrail, expired);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user