码迷,mamicode.com
首页 > Web开发 > 详细

一个简单的以User权限启动外部应用程序(用NetUserAdd函数和USER_INFO_1结构体动态添加用户,然后用CreateProcessWithLogonW启动程序)

时间:2016-10-27 07:53:59      阅读:363      评论:0      收藏:0      [点我收藏+]

标签: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启动程序)

标签:sizeof   tchar   etl   sdn   inf   rac   href   bre   ack   

原文地址:http://www.cnblogs.com/findumars/p/6002469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!