一個簡單的以User許可權啟動外部應用程式(用NetUserAdd函數和USER_INFO_1結構體動態添加使用者,然後用CreateProcessWithLogonW啟動程式)

來源:互聯網
上載者:User

標籤:sizeof   tchar   etl   sdn   inf   rac   href   bre   ack   

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

[cpp] view plain copy 
  1. BOOL ExecuteAsUser(LPCWSTR lpszUserName, LPCWSTR lpszPassword, LPCWSTR lpszApplication, LPCWSTR lpszCmdLine)  
  2. {  
  3.     if(NULL == lpszUserName)  
  4.     {  
  5.         return FALSE;  
  6.     }  
  7.     if(NULL == lpszApplication)  
  8.     {  
  9.         return FALSE;  
  10.     }  
  11.   
  12.     BOOL bRet = FALSE;  
  13.     WCHAR* pUserName = NULL;  
  14.     WCHAR* pPassword = NULL;  
  15.     STARTUPINFO si = {sizeof(si)};  
  16.     PROCESS_INFORMATION pi = {0};  
  17.     WCHAR szApp[MAX_PATH * 2] = {0};  
  18.   
  19.     // Check User Name  
  20.     size_t nLen = wcslen(lpszUserName) + 1;  
  21.     pUserName = new WCHAR[nLen];  
  22.     StringCchPrintfW(pUserName, nLen, L"%s", lpszUserName);  
  23.       
  24.     // Check Password  
  25.     nLen = (NULL != lpszPassword) ? (wcslen(lpszPassword) + 1) : 2;  
  26.     pPassword = new WCHAR[nLen];  
  27.     StringCchPrintfW(pPassword, nLen, L"%s", (NULL != lpszPassword) ? lpszPassword : L"");  
  28.   
  29.     USER_INFO_1 ui;  
  30.     DWORD dwError = 0;  
  31.     DWORD dwLevel = 1;  
  32.     ui.usri1_name = pUserName;  
  33.     ui.usri1_password = pPassword;  
  34.     ui.usri1_priv = USER_PRIV_USER;  
  35.     ui.usri1_home_dir = NULL;  
  36.     ui.usri1_comment = NULL;  
  37.     ui.usri1_flags = UF_SCRIPT;  
  38.     ui.usri1_script_path = NULL;  
  39.     // Add User  
  40.     if(NERR_Success != NetUserAdd(NULL, dwLevel, (LPBYTE)&ui, &dwError))  
  41.     {  
  42.         goto _END_;  
  43.     }  
  44.   
  45.     if((NULL != lpszCmdLine) && wcslen(lpszCmdLine))  
  46.         StringCchPrintfW(szApp, _countof(szApp), L"%s %s", lpszApplication, lpszCmdLine);  
  47.     else  
  48.         StringCchPrintfW(szApp, _countof(szApp), L"%s", lpszApplication);  
  49.   
  50.     if(CreateProcessWithLogonW(lpszUserName, NULL, lpszPassword, LOGON_WITH_PROFILE, NULL, szApp, 0, NULL, NULL, &si, &pi))  
  51.     {  
  52.         bRet = TRUE;  
  53.         CloseHandle(pi.hThread);  
  54.         CloseHandle(pi.hProcess);  
  55.     }  
  56.     else  
  57.     {  
  58.         dwError = GetLastError();  
  59.         goto _CLEANUP_;  
  60.     }  
  61.     bRet = TRUE;  
  62.   
  63. _CLEANUP_:  
  64.     // Delete User  
  65.     NetUserDel(NULL, lpszUserName);  
  66. _END_:  
  67.     if(NULL != pPassword)  
  68.     {  
  69.         delete[] pPassword;  
  70.         pPassword = NULL;  
  71.     }  
  72.     if(NULL != pUserName)  
  73.     {  
  74.         delete[] pUserName;  
  75.         pUserName = NULL;  
  76.     }  
  77.     return bRet;  
  78. }  
  79.   
  80. // 測試代碼  
  81. #include "stdafx.h"  
  82.   
  83. #include <Windows.h>  
  84. #include <lm.h>  
  85. #include <strsafe.h>  
  86. #pragma comment(lib, "Netapi32.lib")  
  87.   
  88. int _tmain(int argc, _TCHAR* argv[])  
  89. {  
  90.     ExecuteAsUser(L"ABC", L"Hello", L"F:\\11.exe", NULL);  
  91.     return 0;  
  92. }  
  http://blog.csdn.net/visualeleven/article/details/7640475

 

這樣需要建立新的賬戶,可用OpenProcessToken+CreateRestrictedToken削去當前進程的令牌的特權用於CreateProcessAsUser  

一個簡單的以User許可權啟動外部應用程式(用NetUserAdd函數和USER_INFO_1結構體動態添加使用者,然後用CreateProcessWithLogonW啟動程式)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.