我写了一个需要以系统身份运行的程序。我添加链接器选项'UAC执行级别‘到'requireAdministrator’,它弹出UAC,因为它应该,但现在我需要从管理员升级到系统,我怎么做呢?我想过打开程序的令牌并将其注入系统令牌,但这不是合法的方式。我怎么才能干净利落地做它,因为我知道一旦你管理你就可以成为系统。
发布于 2018-11-15 02:21:20
编写一个windows服务。默认情况下,它将以SYSTEM
用户身份运行。
一个简单的教程是here,但请记住,安装服务需要管理员权限。
发布于 2018-11-15 02:58:10
以管理员身份运行后,以下是一些选项:
从我的一个项目复制,并稍作修改:
#include #include #include #include #include #include #include #include #include #杂注注释(lib,"taskschd.lib") #杂注注释(lib,"comsupp.lib") HRESULT WINAPI CreateSchedTask(WCHAR *wszExePath) { ITaskService *pService = NULL;ITaskFolder *pRoot = NULL;ITaskDefinition *p= NULL;ITaskSettings *pSettings = NULL;IRegistrationInfo *pInfo = NULL;ITriggerCollection *pCollection = NULL;ITrigger *pTrigger = NULL;ITimeTrigger *pTime = NULL;IPrincipal *pPrincipal = NULL;IActionCollection *pActionCollection = NULL;IAction *pAction = NULL;IExecAction *pExecAction = NULL;IRegisteredTask *pRegTask = NULL;SYSTEMTIME stNow;WCHAR wFmt100;VARIANT vBlank = _variant_t();CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);//CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_PKT_PRIVACY,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,0,NULL);CoCreateInstance(CLSID_TaskScheduler,NULL,CLSCTX_INPROC_SERVER,IID_ITaskService,(LPVOID *)&pService);pService->Connect(vBlank,vBlank);pService->GetFolder(SysAllocString(L"\"),&pRoot);pService->NewTask(0,&pTask);pService->Release();pTask->get_RegistrationInfo(&pInfo);pInfo->put_Author(SysAllocString(L"TASKNAMEHERE"));pInfo->Release();pTask->get_Settings(&pSettings);pSettings->put_StartWhenAvailable(VARIANT_TRUE);pSettings->put_Enabled(VARIANT_TRUE);pSettings->Release();pTask->get_Triggers(&pCollection);pCollection->Create(TASK_TRIGGER_TIME,&p pTrigger);pCollection->Release();pTime->put_StartBoundary(SysAllocString(wFmt));触发器->查询接口(IID_ITimeTrigger,(void **)&pTime);GetLocalTime(&stNow);//注意:请将-07:00替换为您的时区StringCchPrintfW(wFmt,100,L"%.4hu-%.2hu-%.2huT%.2hu:%.2hu:%.2hu-07:00",stNow.wYear,stNow.wMonth,stNow.wDay,stNow.wHour,stNow.wMinute,stNow.wSecond + 30);pTriggerStringCchPrintfW(wFmt,100,L"%.4hu-%.2hu-%.2huT%.2hu:%.2hu:%.2hu-07:00",stNow.wYear,stNow.wMonth,stNow.wDay,stNow.wHour,stNow.wMinute + 1,stNow.wSecond);pTime->put_put(SysAllocString(WFmt));pTime->Release();pTask->get_Actions(&pActionCollection);pActionCollection->Create(TASK_ACTION_EXEC,&pAction);pActionCollection->Release();pAction->QueryInterface(IID_IExecAction,(void **)&pExecAction);pAction->Release();pAction pExecAction->Release();pTask->get_ pPrincipal->put_LogonType(TASK_LOGON_SERVICE_ACCOUNT);(&pPrincipal);pPrincipal->put_RunLevel(TASK_RUNLEVEL_HIGHEST);pPrincipal->put_LogonType(TASK_LOGON_SERVICE_ACCOUNT);pTask->put_pRoot(PPrincipal);pPrincipal->Release();pRoot->RegisterTaskDefinition( SysAllocString(L“系统提升”),pTask,TASK_CREATE_OR_UPDATE,_variant_t(L"NT AUTHORITY\SYSTEM"),_variant_t(),TASK_LOGON_SERVICE_ACCOUNT,_variant_t(L""),&pRegTask);pRoot->Release();pTask->Release();pRegTask->Release();CoUninitialize();return S_OK;} INT APIENTRY wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPWSTR lpCmdLine,INT nShowCmd) { WCHAR wUsername100,wExePathMAX_PATH;GetEnvironmentVariableW(L“用户名”,wUsername,100);if (!wcschr(wUsername,L'$')) { GetModuleFileNameW(hInstance,wExePath,MAX_PATH);CreateSchedTask(wExePath);} else { //注意: MessageBox和其他图形用户界面函数将不起作用,因为进程不是在winsta0\default //文件I/O中运行,而是处理hLog = CreateFileW(L"C:\Temp\Log.txt",GENERIC_WRITE | GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);DWORD dwWritten;UINT uLen;CHAR szBuf100;SetFilePointer(hLog,0,NULL,FILE_END);StringCchPrintfA(szBuf,100,“来自NT AUTHORITY\ System \r\n",wUsername);StringCbLengthA(szBuf,100,&uLen);WriteFile(hLog,szBuf,uLen,&dwWritten,NULL);CloseHandle(hLog);} return 0;}
https://stackoverflow.com/questions/53306380
复制相似问题