Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. ACTIVECONSOLESESSIONIDFUNC lpfnProc; // WTSGetActiveConsoleSessionId function pointer
  2. HMODULE hModule = NULL; // Instance for kernel32.dll library
  3. DWORD dwSessionId = 0; // Session ID
  4. HANDLE hToken = NULL; // Active session token
  5. HANDLE hDupToken = NULL; // Duplicate session token
  6. WCHAR szErr[1024] = {0};
  7.  
  8. LPVOID lpEnvironment = NULL; // Environtment block
  9.  
  10. // Get the active session ID
  11. hModule = LoadLibrary(KERNEL32LIB);
  12. if(!hModule)
  13. {
  14. //wsprintf(szErr, L"LoadLibrary Error: %d", GetLastError());
  15. return;
  16. }
  17.  
  18. lpfnProc = (ACTIVECONSOLESESSIONIDFUNC)GetProcAddress(hModule,"WTSGetActiveConsoleSessionId");
  19.  
  20. dwSessionId = lpfnProc();
  21.  
  22. // Get token of the logged in user by the active session ID
  23. BOOL bRet = WTSQueryUserToken(dwSessionId, &hToken);
  24.  
  25. if (!bRet)
  26. {
  27. //wsprintf(szErr, L"WTSQueryUserToken Error: %d", GetLastError());
  28. return;
  29. }
  30.  
  31. // Get duplicate token from the active logged in user's token
  32. bRet = DuplicateTokenEx(hToken, // Active session token
  33. MAXIMUM_ALLOWED, // Desired access
  34. NULL, // Token attributes
  35. SecurityIdentification, // Impersonation level
  36. TokenPrimary, // Token type
  37. &hDupToken); // New/Duplicate token
  38. if (!bRet)
  39. {
  40. //wsprintf(szErr, L"DuplicateTokenEx Error: %d", GetLastError());
  41. return;
  42. }
  43.  
  44. // Get all necessary environment variables of logged in user
  45. // to pass them to the process
  46.  
  47. bRet = CreateEnvironmentBlock(&lpEnvironment, // Environment block
  48. hDupToken, // New token
  49. FALSE); // Inheritence
  50. if(!bRet)
  51. {
  52. //wsprintf(szErr, L"CreateEnvironmentBlock Error: %d", GetLastError());
  53. return;
  54. }
  55.  
  56. HDESK hdeskInput=OpenInputDesktop(0, FALSE, 0); // does not set GetLastError(), so GetLastError() is arbitrary if NULL is returned
  57. if( hdeskInput==NULL ) {
  58. TRACE( "hdeskInput==NULL" );
  59. return false;
  60. }
  61.  
  62. // Initialize Startup and Process info
  63. startupInfo->cb = sizeof(STARTUPINFO);
  64. startupInfo->lpDesktop = TEXT("winsta0\default");
  65.  
  66.  
  67.  
  68. // Start the process on behalf of the current user
  69.  
  70. BOOL returnCode = CreateProcessAsUser(hDupToken,
  71. applicationName,
  72. commandLine,
  73. NULL,
  74. NULL,
  75. FALSE,
  76. NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT,
  77. lpEnvironment,
  78. NULL,
  79. startupInfo,
  80. &processInformation);
  81.  
  82. //Function to run a process as active user from windows service
  83. void ImpersonateActiveUserAndRun(WCHAR* path, WCHAR* args)
  84. {
  85. DWORD session_id = -1;
  86. DWORD session_count = 0;
  87.  
  88. WTS_SESSION_INFOA *pSession = NULL;
  89.  
  90.  
  91. if (WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSession, &session_count))
  92. {
  93. //log success
  94. }
  95. else
  96. {
  97. //log error
  98. return;
  99. }
  100.  
  101. for (int i = 0; i < session_count; i++)
  102. {
  103. session_id = pSession[i].SessionId;
  104.  
  105. WTS_CONNECTSTATE_CLASS wts_connect_state = WTSDisconnected;
  106. WTS_CONNECTSTATE_CLASS* ptr_wts_connect_state = NULL;
  107.  
  108. DWORD bytes_returned = 0;
  109. if (::WTSQuerySessionInformation(
  110. WTS_CURRENT_SERVER_HANDLE,
  111. session_id,
  112. WTSConnectState,
  113. reinterpret_cast<LPTSTR*>(&ptr_wts_connect_state),
  114. &bytes_returned))
  115. {
  116. wts_connect_state = *ptr_wts_connect_state;
  117. ::WTSFreeMemory(ptr_wts_connect_state);
  118. if (wts_connect_state != WTSActive) continue;
  119. }
  120. else
  121. {
  122. //log error
  123. continue;
  124. }
  125.  
  126. HANDLE hImpersonationToken;
  127.  
  128. if (!WTSQueryUserToken(session_id, &hImpersonationToken))
  129. {
  130. //log error
  131. continue;
  132. }
  133.  
  134.  
  135. //Get real token from impersonation token
  136. DWORD neededSize1 = 0;
  137. HANDLE *realToken = new HANDLE;
  138. if (GetTokenInformation(hImpersonationToken, (::TOKEN_INFORMATION_CLASS) TokenLinkedToken, realToken, sizeof(HANDLE), &neededSize1))
  139. {
  140. CloseHandle(hImpersonationToken);
  141. hImpersonationToken = *realToken;
  142. }
  143. else
  144. {
  145. //log error
  146. continue;
  147. }
  148.  
  149.  
  150. HANDLE hUserToken;
  151.  
  152. if (!DuplicateTokenEx(hImpersonationToken,
  153. //0,
  154. //MAXIMUM_ALLOWED,
  155. TOKEN_ASSIGN_PRIMARY | TOKEN_ALL_ACCESS | MAXIMUM_ALLOWED,
  156. NULL,
  157. SecurityImpersonation,
  158. TokenPrimary,
  159. &hUserToken))
  160. {
  161. //log error
  162. continue;
  163. }
  164.  
  165. // Get user name of this process
  166. //LPTSTR pUserName = NULL;
  167. WCHAR* pUserName;
  168. DWORD user_name_len = 0;
  169.  
  170. if (WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, session_id, WTSUserName, &pUserName, &user_name_len))
  171. {
  172. //log username contained in pUserName WCHAR string
  173. }
  174.  
  175. //Free memory
  176. if (pUserName) WTSFreeMemory(pUserName);
  177.  
  178. ImpersonateLoggedOnUser(hUserToken);
  179.  
  180. STARTUPINFOW StartupInfo;
  181. GetStartupInfoW(&StartupInfo);
  182. StartupInfo.cb = sizeof(STARTUPINFOW);
  183. //StartupInfo.lpDesktop = "winsta0\default";
  184.  
  185. PROCESS_INFORMATION processInfo;
  186.  
  187. SECURITY_ATTRIBUTES Security1;
  188. Security1.nLength = sizeof SECURITY_ATTRIBUTES;
  189.  
  190. SECURITY_ATTRIBUTES Security2;
  191. Security2.nLength = sizeof SECURITY_ATTRIBUTES;
  192.  
  193. void* lpEnvironment = NULL;
  194.  
  195. // Get all necessary environment variables of logged in user
  196. // to pass them to the new process
  197. BOOL resultEnv = CreateEnvironmentBlock(&lpEnvironment, hUserToken, FALSE);
  198. if (!resultEnv)
  199. {
  200. //log error
  201. continue;
  202. }
  203.  
  204. WCHAR PP[1024]; //path and parameters
  205. ZeroMemory(PP, 1024 * sizeof WCHAR);
  206. wcscpy(PP, path);
  207. wcscat(PP, L" ");
  208. wcscat(PP, args);
  209.  
  210. // Start the process on behalf of the current user
  211. BOOL result = CreateProcessAsUserW(hUserToken,
  212. NULL,
  213. PP,
  214. //&Security1,
  215. //&Security2,
  216. NULL,
  217. NULL,
  218. FALSE,
  219. NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
  220. //lpEnvironment,
  221. NULL,
  222. //"C:\ProgramData\some_dir",
  223. NULL,
  224. &StartupInfo,
  225. &processInfo);
  226.  
  227. if (!result)
  228. {
  229. //log error
  230. }
  231. else
  232. {
  233. //log success
  234. }
  235.  
  236. DestroyEnvironmentBlock(lpEnvironment);
  237.  
  238. CloseHandle(hImpersonationToken);
  239. CloseHandle(hUserToken);
  240. CloseHandle(realToken);
  241.  
  242. RevertToSelf();
  243. }
  244.  
  245. WTSFreeMemory(pSession);
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement