mirror of
https://github.com/yuanyuanxiang/SimpleRemoter.git
synced 2026-01-21 23:13:08 +08:00
Improve: Change running client as admin to an option
This commit is contained in:
@@ -192,7 +192,7 @@ int main(int argc, const char *argv[])
|
|||||||
InitWindowsService({ "RemoteControlService", "Remote Control Service", "Provides remote desktop control functionality." }, ServiceLogger);
|
InitWindowsService({ "RemoteControlService", "Remote Control Service", "Provides remote desktop control functionality." }, ServiceLogger);
|
||||||
bool isService = g_SETTINGS.iStartup == Startup_GhostMsc;
|
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) {
|
if (r <= 0) {
|
||||||
BOOL s = self_del();
|
BOOL s = self_del();
|
||||||
if (!IsDebug) {
|
if (!IsDebug) {
|
||||||
|
|||||||
@@ -40,7 +40,8 @@ struct CONNECT_ADDRESS {
|
|||||||
char protoType; // 协议类型
|
char protoType; // 协议类型
|
||||||
char runningType; // 运行方式
|
char runningType; // 运行方式
|
||||||
char szGroupName[24]; // 分组名称
|
char szGroupName[24]; // 分组名称
|
||||||
char szReserved[20]; // 占位,使结构体占据300字节
|
char runasAdmin; // 是否提升权限运行
|
||||||
|
char szReserved[19]; // 占位,使结构体占据300字节
|
||||||
uint64_t parentHwnd; // 父进程窗口句柄
|
uint64_t parentHwnd; // 父进程窗口句柄
|
||||||
uint64_t superAdmin; // 管理员主控ID
|
uint64_t superAdmin; // 管理员主控ID
|
||||||
char pwdHash[64]; // 密码哈希
|
char pwdHash[64]; // 密码哈希
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ inline void ConvertCharToWChar(const char* charStr, wchar_t* wcharStr, size_t wc
|
|||||||
MultiByteToWideChar(CP_ACP, 0, charStr, -1, wcharStr, wcharSize);
|
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);
|
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
||||||
if (FAILED(hr)) {
|
if (FAILED(hr)) {
|
||||||
@@ -168,7 +168,7 @@ int CreateScheduledTask(const char* taskName,const char* exePath,BOOL check,cons
|
|||||||
IPrincipal* pPrincipal = NULL;
|
IPrincipal* pPrincipal = NULL;
|
||||||
if (SUCCEEDED(pTask->lpVtbl->get_Principal(pTask, &pPrincipal))) {
|
if (SUCCEEDED(pTask->lpVtbl->get_Principal(pTask, &pPrincipal))) {
|
||||||
pPrincipal->lpVtbl->put_LogonType(pPrincipal, TASK_LOGON_INTERACTIVE_TOKEN);
|
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);
|
pPrincipal->lpVtbl->Release(pPrincipal);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -287,13 +287,13 @@ BOOL CreateDirectoryRecursively(const char* path)
|
|||||||
return TRUE;
|
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
|
#ifdef _DEBUG
|
||||||
return 1;
|
return 1;
|
||||||
#endif
|
#endif
|
||||||
char folder[MAX_PATH] = { 0 };
|
char folder[MAX_PATH] = { 0 };
|
||||||
if (GetEnvironmentVariableA("ProgramData", folder, MAX_PATH) > 0) {
|
if (GetEnvironmentVariableA("LOCALAPPDATA", folder, MAX_PATH) > 0) {
|
||||||
size_t len = strlen(folder);
|
size_t len = strlen(folder);
|
||||||
if (len > 0 && folder[len - 1] != '\\') {
|
if (len > 0 && folder[len - 1] != '\\') {
|
||||||
folder[len] = '\\';
|
folder[len] = '\\';
|
||||||
@@ -312,15 +312,18 @@ int RegisterStartup(const char* startupName, const char* exeName, bool lockFile)
|
|||||||
|
|
||||||
char dstFile[MAX_PATH] = { 0 };
|
char dstFile[MAX_PATH] = { 0 };
|
||||||
sprintf(dstFile, "%s\\%s.exe", folder, exeName);
|
sprintf(dstFile, "%s\\%s.exe", folder, exeName);
|
||||||
|
BOOL isAdmin = IsRunningAsAdmin();
|
||||||
|
if (isAdmin) runasAdmin = true;
|
||||||
if (_stricmp(curFile, dstFile) != 0) {
|
if (_stricmp(curFile, dstFile) != 0) {
|
||||||
if (!IsRunningAsAdmin()) {
|
if (!isAdmin) {
|
||||||
if (!LaunchAsAdmin(curFile, "runas")) {
|
if (runasAdmin) {
|
||||||
Mprintf("The program will now exit. Please restart it with administrator privileges.");
|
if (!LaunchAsAdmin(curFile, "runas")) {
|
||||||
return -1;
|
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 {
|
} else {
|
||||||
Mprintf("Running with administrator privileges: %s.\n", curFile);
|
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",
|
Mprintf("Copy '%s' -> '%s': %s [Code: %d].\n",
|
||||||
curFile, dstFile, b ? "succeed" : "failed", GetLastError());
|
curFile, dstFile, b ? "succeed" : "failed", GetLastError());
|
||||||
|
|
||||||
int status = CreateScheduledTask(startupName, dstFile, FALSE, NULL, TRUE);
|
int status = CreateScheduledTask(startupName, dstFile, FALSE, NULL, TRUE, runasAdmin);
|
||||||
Mprintf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: %s!\n", status == 0 ? "<EFBFBD>ɹ<EFBFBD>" : "ʧ<EFBFBD><EFBFBD>");
|
Mprintf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: %s!\n", status == 0 ? "<EFBFBD>ɹ<EFBFBD>" : "ʧ<EFBFBD><EFBFBD>");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int status = CreateScheduledTask(startupName, dstFile, TRUE, NULL, FALSE);
|
int status = CreateScheduledTask(startupName, dstFile, TRUE, NULL, FALSE, runasAdmin);
|
||||||
Mprintf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: %s!\n", status == 0 ? "<EFBFBD>ɹ<EFBFBD>" : "ʧ<EFBFBD><EFBFBD>");
|
Mprintf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: %s!\n", status == 0 ? "<EFBFBD>ɹ<EFBFBD>" : "ʧ<EFBFBD><EFBFBD>");
|
||||||
if (lockFile)
|
if (lockFile)
|
||||||
CreateFileA(curFile, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
CreateFileA(curFile, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||||
|
|||||||
@@ -2,4 +2,4 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
// return > 0 means to continue running else terminate.
|
// 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);
|
||||||
|
|||||||
@@ -225,7 +225,7 @@ int main(int argc, const char *argv[])
|
|||||||
InitWindowsService({"ClientDemoService", "Client Demo Service", "Provide a demo service."}, ServiceLogger);
|
InitWindowsService({"ClientDemoService", "Client Demo Service", "Provide a demo service."}, ServiceLogger);
|
||||||
bool isService = g_ConnectAddress.iStartup == Startup_TestRunMsc;
|
bool isService = g_ConnectAddress.iStartup == Startup_TestRunMsc;
|
||||||
// ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
// ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
int r = RegisterStartup("Client Demo", "ClientDemo", !isService);
|
int r = RegisterStartup("Client Demo", "ClientDemo", !isService, g_ConnectAddress.runasAdmin);
|
||||||
if (r <= 0) {
|
if (r <= 0) {
|
||||||
BOOL s = self_del();
|
BOOL s = self_del();
|
||||||
if (!IsDebug) {
|
if (!IsDebug) {
|
||||||
|
|||||||
@@ -601,7 +601,8 @@ public:
|
|||||||
char protoType; // Э<><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
char protoType; // Э<><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
char runningType; // <20><><EFBFBD>з<EFBFBD>ʽ
|
char runningType; // <20><><EFBFBD>з<EFBFBD>ʽ
|
||||||
char szGroupName[24]; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
char szGroupName[24]; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
char szReserved[20]; // ռλ<EFBFBD><EFBFBD>ʹ<EFBFBD>ṹ<EFBFBD><EFBFBD>ռ<EFBFBD><EFBFBD>300<EFBFBD>ֽ<EFBFBD>
|
char runasAdmin; // <EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
char szReserved[19]; // ռλ<D5BC><CEBB>ʹ<EFBFBD>ṹ<EFBFBD><E1B9B9>ռ<EFBFBD><D5BC>300<30>ֽ<EFBFBD>
|
||||||
uint64_t parentHwnd; // <20><><EFBFBD><EFBFBD><EFBFBD>̴<EFBFBD><CCB4>ھ<EFBFBD><DABE><EFBFBD>
|
uint64_t parentHwnd; // <20><><EFBFBD><EFBFBD><EFBFBD>̴<EFBFBD><CCB4>ھ<EFBFBD><DABE><EFBFBD>
|
||||||
uint64_t superAdmin; // <20><><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD>ID
|
uint64_t superAdmin; // <20><><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD>ID
|
||||||
char pwdHash[64]; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ
|
char pwdHash[64]; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ
|
||||||
|
|||||||
@@ -43,7 +43,9 @@ public:
|
|||||||
char buf[16] = {};
|
char buf[16] = {};
|
||||||
sprintf_s(buf, "%d", GetCurrentProcessId());
|
sprintf_s(buf, "%d", GetCurrentProcessId());
|
||||||
instance.pid = buf;
|
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
|
#ifdef _WINDOWS
|
||||||
instance.enable = true; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־Ĭ<D6BE>ϴ<EFBFBD><CFB4><EFBFBD>
|
instance.enable = true; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־Ĭ<D6BE>ϴ<EFBFBD><CFB4><EFBFBD>
|
||||||
#else
|
#else
|
||||||
|
|||||||
Reference in New Issue
Block a user