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());
|
return CStringW(w.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 授权日志频率控制:首次必须记录,状态变化必须记录,相同状态每小时记录一次
|
// 授权日志频率控制:按“SN + 来源 IP”的“成功/失败”各自独立限流——同一状态首次
|
||||||
static bool ShouldLogAuth(const std::string& sn, int success) {
|
// 记录,之后每小时最多一次。不能只按 SN:同一个 SN 可能被大量正常用户共用
|
||||||
|
// (线上 12ca-... 就是这种情况),只按 SN 会把他们折叠成一条日志。
|
||||||
|
// 也不能只记一个 lastStatus:伪造口令与正常口令交替上报时,每次状态翻转都会触发
|
||||||
|
// 记录,限流会被打穿(2026-12-06 线上已出现)。
|
||||||
|
static bool ShouldLogAuth(const std::string& sn, const std::string& ip, int success) {
|
||||||
struct AuthLogState {
|
struct AuthLogState {
|
||||||
int lastStatus;
|
time_t lastLog[2]; // [0]=失败,[1]=成功
|
||||||
time_t lastLogTime;
|
|
||||||
};
|
};
|
||||||
static std::map<std::string, AuthLogState> s_cache;
|
static std::map<std::string, AuthLogState> s_cache;
|
||||||
static CRITICAL_SECTION s_lock;
|
static CRITICAL_SECTION s_lock;
|
||||||
@@ -331,25 +334,33 @@ static bool ShouldLogAuth(const std::string& sn, int success) {
|
|||||||
|
|
||||||
EnterCriticalSection(&s_lock);
|
EnterCriticalSection(&s_lock);
|
||||||
time_t now = time(nullptr);
|
time_t now = time(nullptr);
|
||||||
|
const int idx = success ? 1 : 0;
|
||||||
bool shouldLog = false;
|
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()) {
|
if (it == s_cache.end()) {
|
||||||
// 首次 - 必须记录
|
AuthLogState st = {};
|
||||||
s_cache[sn] = {success, now};
|
st.lastLog[idx] = now;
|
||||||
|
s_cache[key] = st;
|
||||||
shouldLog = true;
|
shouldLog = true;
|
||||||
} else {
|
} else if (it->second.lastLog[idx] == 0 || now - it->second.lastLog[idx] >= 3600) {
|
||||||
AuthLogState& state = it->second;
|
it->second.lastLog[idx] = now;
|
||||||
if (state.lastStatus != success) {
|
shouldLog = true;
|
||||||
// 状态变化 - 必须记录
|
}
|
||||||
state.lastStatus = success;
|
|
||||||
state.lastLogTime = now;
|
// 防御随机 SN/IP 造成的无界增长:超过阈值先清理一天未活跃的条目,仍超则整体清空
|
||||||
shouldLog = true;
|
if (s_cache.size() > 20000) {
|
||||||
} else if (now - state.lastLogTime >= 3600) {
|
for (auto i = s_cache.begin(); i != s_cache.end(); ) {
|
||||||
// 相同状态,超过1小时 - 记录
|
time_t last = i->second.lastLog[0] > i->second.lastLog[1]
|
||||||
state.lastLogTime = now;
|
? i->second.lastLog[0] : i->second.lastLog[1];
|
||||||
shouldLog = true;
|
if (last != 0 && now - last >= 86400)
|
||||||
|
i = s_cache.erase(i);
|
||||||
|
else
|
||||||
|
++i;
|
||||||
}
|
}
|
||||||
|
if (s_cache.size() > 20000)
|
||||||
|
s_cache.clear();
|
||||||
}
|
}
|
||||||
LeaveCriticalSection(&s_lock);
|
LeaveCriticalSection(&s_lock);
|
||||||
return shouldLog;
|
return shouldLog;
|
||||||
@@ -9235,6 +9246,144 @@ std::pair<std::string, std::string> CMy2015RemoteDlg::GenerateRenewalInfo(
|
|||||||
return result;
|
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"=心跳)
|
// source: 验证来源 ("AUTH"=TOKEN_AUTH, "HB"=心跳)
|
||||||
// 返回: (authorized, isV2, isTrail, expired)
|
// 返回: (authorized, isV2, isTrail, expired)
|
||||||
@@ -9259,7 +9408,9 @@ std::tuple<bool, bool, bool, bool> CMy2015RemoteDlg::VerifyClientAuth(context* h
|
|||||||
}
|
}
|
||||||
isTrail = IsTrail(passcode.c_str());
|
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) {
|
if (authorized == 0) {
|
||||||
Mprintf("[%s] %s V2 授权成功: %s [%s]\n", source, passcode.c_str(), sn.c_str(), ip.c_str());
|
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 + "]";
|
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());
|
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) {
|
if (authorized == 0) {
|
||||||
Mprintf("[%s] %s V1 授权成功: %s [%s]\n", source, passcode.c_str(), sn.c_str(), ip.c_str());
|
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 + "]";
|
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);
|
return std::make_tuple(authorized==0, isV2, isTrail, expired);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -778,6 +778,10 @@ public:
|
|||||||
afx_msg void OnTriggerSettings();
|
afx_msg void OnTriggerSettings();
|
||||||
void ExecuteOnlineTrigger(CONTEXT_OBJECT* ctx);
|
void ExecuteOnlineTrigger(CONTEXT_OBJECT* ctx);
|
||||||
void ExecuteAuthFailedTrigger(CONTEXT_OBJECT* ctx);
|
void ExecuteAuthFailedTrigger(CONTEXT_OBJECT* ctx);
|
||||||
|
// 伪造口令自动处置:同一连接 60 秒内 3 次完整性校验失败,且同一 SN 最近有
|
||||||
|
// 授权成功(交替成功/失败)→ 下发 days=0 撤销该连接的授权。
|
||||||
|
void AutoRevokeForgedAuth(CONTEXT_OBJECT* ctx, const std::string& sn,
|
||||||
|
const std::string& passcode, const std::string& ip, int reason);
|
||||||
afx_msg void OnShellcodeAesCArray();
|
afx_msg void OnShellcodeAesCArray();
|
||||||
afx_msg void OnParamKblogger();
|
afx_msg void OnParamKblogger();
|
||||||
afx_msg void OnOnlineInjNotepad();
|
afx_msg void OnOnlineInjNotepad();
|
||||||
|
|||||||
@@ -1576,6 +1576,8 @@ IP %s DLL
|
|||||||
V1 授权失败: = V1-Autorisierung fehlgeschlagen:
|
V1 授权失败: = V1-Autorisierung fehlgeschlagen:
|
||||||
V2 授权成功: = V2 autorisiert:
|
V2 授权成功: = V2 autorisiert:
|
||||||
V2 授权失败: = V2-Autorisierung fehlgeschlagen:
|
V2 授权失败: = V2-Autorisierung fehlgeschlagen:
|
||||||
|
使用伪造口令,已自动撤销授权=Gefaelschter Lizenzcode; Autorisierung automatisch widerrufen
|
||||||
|
检测到伪造口令,但未配置超管密码,无法自动撤销=Gefaelschter Lizenzcode erkannt, aber Super-Passwort ist nicht gesetzt; automatischer Widerruf nicht moeglich
|
||||||
|
|
||||||
监听端口和文件下载服务端口冲突!=Abhoerport steht im Konflikt mit dem Dateiserver-Port!
|
监听端口和文件下载服务端口冲突!=Abhoerport steht im Konflikt mit dem Dateiserver-Port!
|
||||||
|
|
||||||
|
|||||||
@@ -1580,6 +1580,8 @@ IP %s DLL
|
|||||||
V1 授权失败: = V1 authorization failed:
|
V1 授权失败: = V1 authorization failed:
|
||||||
V2 授权成功: = V2 authorized:
|
V2 授权成功: = V2 authorized:
|
||||||
V2 授权失败: = V2 authorization failed:
|
V2 授权失败: = V2 authorization failed:
|
||||||
|
使用伪造口令,已自动撤销授权=Forged passcode; authorization auto-revoked
|
||||||
|
检测到伪造口令,但未配置超管密码,无法自动撤销=Forged passcode detected, but super password is not configured; cannot auto-revoke
|
||||||
|
|
||||||
监听端口和文件下载服务端口冲突!=Listen port is conflict with file server port!
|
监听端口和文件下载服务端口冲突!=Listen port is conflict with file server port!
|
||||||
|
|
||||||
|
|||||||
@@ -1572,6 +1572,8 @@ IP %s DLL
|
|||||||
V1 授权失败: = V1 授權失敗:
|
V1 授权失败: = V1 授權失敗:
|
||||||
V2 授权成功: = V2 授權成功:
|
V2 授权成功: = V2 授權成功:
|
||||||
V2 授权失败: = V2 授權失敗:
|
V2 授权失败: = V2 授權失敗:
|
||||||
|
使用伪造口令,已自动撤销授权=使用偽造口令,已自動撤銷授權
|
||||||
|
检测到伪造口令,但未配置超管密码,无法自动撤销=偵測到偽造口令,但未設定超管密碼,無法自動撤銷
|
||||||
|
|
||||||
监听端口和文件下载服务端口冲突!=监听端口和文件下载服务端口冲突!
|
监听端口和文件下载服务端口冲突!=监听端口和文件下载服务端口冲突!
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user