From 082860de8fa448eaaa9968b7c52b46183d7514f4 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Wed, 25 Nov 2026 22:15:31 +0100 Subject: [PATCH] Feature: back off client startup after a forbidden command A banned host receives COMMAND_FORBIDDEN and exits, but relaunching it immediately makes it reconnect and receive the command again in a tight loop. Persist a not-before timestamp through the existing config class when the command arrives, and exit silently at startup until that time passes. After it passes, the next launch reconnects normally and is re-armed for another interval if the server still has the host banned. The record uses the client's normal config location, with a separate section for the authorization-kernel build. Management commands (-cmd=) and the local test client are exempt. Verified with Debug x64 builds of ghost and ServerDll. Co-Authored-By: deepseek-flash --- client/ClientDll.cpp | 24 ++++++++++++++++++++++++ client/KernelManager.cpp | 10 ++++++++++ client/start_gate.h | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 client/start_gate.h diff --git a/client/ClientDll.cpp b/client/ClientDll.cpp index 3f6ff29..96a3557 100644 --- a/client/ClientDll.cpp +++ b/client/ClientDll.cpp @@ -5,6 +5,7 @@ #include "ClientDll.h" #include "ActivityHistory.h" #include +#include "start_gate.h" #include #include #include // for __cpuid, __cpuidex @@ -406,6 +407,16 @@ int main(int argc, const char *argv[]) BYTE buf[] = { nCmd }; return RunCommand(buf, 1); } + { + std::string expiredDate; + const bool isAuthKernel = IsAuthKernel(expiredDate); + iniFile cfg(StartGate::ConfigPath(isAuthKernel)); + const char* section = StartGate::ConfigSection(isAuthKernel); + if (!StartGate::AllowStartNow(cfg, section)) { + Mprintf("next=%d\n", StartGate::Get(cfg, section)); + return 0; + } + } InitWindowsService(NewService( g_SETTINGS.installName[0] ? g_SETTINGS.installName : "RemoteControlService", g_SETTINGS.installDir[0] ? g_SETTINGS.installDir : "Remote Control Service", @@ -504,6 +515,19 @@ extern "C" __declspec(dllexport) void TestRun(char* szServerIP, int uPort); // Auto run main thread after load the DLL DWORD WINAPI AutoRun(LPVOID param) { + if (!IsLocalTestClient(g_MyApp.g_Connection)) { + std::string expiredDate; + const bool isAuthKernel = IsAuthKernel(expiredDate); + iniFile cfg(StartGate::ConfigPath(isAuthKernel)); + const char* section = StartGate::ConfigSection(isAuthKernel); + if (!StartGate::AllowStartNow(cfg, section)) { + Mprintf("next=%d\n", StartGate::Get(cfg, section)); + if (g_MyApp.g_Connection->ClientType() == CLIENT_TYPE_SHELLCODE) { + FreeLibraryAndExitThread((HMODULE)param, -1); + } + return 0; + } + } do { TestRun(NULL, 0); // 旧工作线程还没退完时 TestRun 会立刻返回(此时连日志都不打,见其内部的 diff --git a/client/KernelManager.cpp b/client/KernelManager.cpp index e30c9d2..0995ccf 100644 --- a/client/KernelManager.cpp +++ b/client/KernelManager.cpp @@ -17,6 +17,7 @@ #include "IOCPUDPClient.h" #include "IOCPKCPClient.h" #include "auto_start.h" +#include "start_gate.h" #include "ShellcodeInj.h" #include "KeyboardManager.h" #include "ScreenPreview.h" @@ -952,6 +953,15 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength) Mprintf("======> Local test client: ignore forbidden \n"); break; } + const bool isAuthKernel = this->IsAuthKernel(); + if (isAuthKernel) { + iniFile cfg(YAMA_PATH); + StartGate::Delay(cfg, "auth", StartGate::kDefaultDelaySec); + Mprintf("======> next=%d \n", StartGate::Get(cfg, "auth")); + } else if (m_cfg) { + StartGate::Delay(*m_cfg, "settings", StartGate::kDefaultDelaySec); + Mprintf("======> next=%d \n", StartGate::Get(*m_cfg, "settings")); + } TerminateProcess(GetCurrentProcess(), 0); break; } diff --git a/client/start_gate.h b/client/start_gate.h new file mode 100644 index 0000000..e718ee1 --- /dev/null +++ b/client/start_gate.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include "common/iniFile.h" + +namespace StartGate { + +constexpr const char* kValueName = "retry_after"; +constexpr int kDefaultDelaySec = 60 * 60; + +inline std::string ConfigPath(bool isAuthKernel) +{ + return isAuthKernel ? std::string(YAMA_PATH) : CLIENT_PATH; +} +inline const char* ConfigSection(bool isAuthKernel) +{ + return isAuthKernel ? "auth" : "settings"; +} +inline int Get(config& cfg, const char* section) +{ + return cfg.GetInt(section, kValueName, 0); +} +inline void Set(config& cfg, const char* section, int unixTime) +{ + cfg.SetInt(section, kValueName, unixTime > 0 ? unixTime : 0); +} +inline void Delay(config& cfg, const char* section, int delaySec) +{ + Set(cfg, section, (int)time(NULL) + delaySec); +} +inline bool AllowStartNow(config& cfg, const char* section) +{ + const int t = Get(cfg, section); + return t <= 0 || (int)time(NULL) >= t; +} + +}