A simple launch of an external application with user privileges (dynamically adding users using the NetUserAdd function and the user_info_1 struct, and then starting the program with CREATEPROCESSWITHLOGONW)

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[CPP]View PlainCopy
  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. BOOL bRet = FALSE;
  12. wchar* pusername = NULL;
  13. wchar* ppassword = NULL;
  14. Startupinfo si = {sizeof (SI)};
  15. process_information pi = {0};
  16. WCHAR Szapp[max_path * 2] = {0};
  17. //Check User Name
  18. size_t Nlen = wcslen (lpszusername) + 1;
  19. Pusername = new Wchar[nlen];
  20. STRINGCCHPRINTFW (Pusername, Nlen, L"%s", lpszUserName);
  21. //Check Password
  22. Nlen = (NULL! = Lpszpassword)? (Wcslen (Lpszpassword) + 1): 2;
  23. Ppassword = new Wchar[nlen];
  24. STRINGCCHPRINTFW (Ppassword, Nlen, L"%s", (NULL! = Lpszpassword)? lpszpassword:l"");
  25. User_info_1 UI;
  26. DWORD dwerror = 0;
  27. DWORD dwlevel = 1;
  28. Ui.usri1_name = Pusername;
  29. Ui.usri1_password = Ppassword;
  30. Ui.usri1_priv = User_priv_user;
  31. Ui.usri1_home_dir = NULL;
  32. Ui.usri1_comment = NULL;
  33. Ui.usri1_flags = Uf_script;
  34. Ui.usri1_script_path = NULL;
  35. //ADD User
  36. if (nerr_success! = NetUserAdd (NULL, Dwlevel, (lpbyte) &ui, &dwerror))
  37. {
  38. Goto _end_;
  39. }
  40. if ((NULL! = lpszcmdline) && wcslen (lpszcmdline))
  41. STRINGCCHPRINTFW (Szapp, _countof (Szapp), L"%s%s", lpszapplication, lpszCmdLine);
  42. Else
  43. STRINGCCHPRINTFW (Szapp, _countof (Szapp), L"%s", lpszapplication);
  44. if (createprocesswithlogonw (lpszUserName, NULL, Lpszpassword, logon_with_profile, NULL, Szapp, 0, NULL, NULL, & Si, &pi))
  45. {
  46. BRet = TRUE;
  47. CloseHandle (Pi.hthread);
  48. CloseHandle (pi.hprocess);
  49. }
  50. Else
  51. {
  52. dwerror = GetLastError ();
  53. Goto _cleanup_;
  54. }
  55. BRet = TRUE;
  56. _cleanup_:
  57. //Delete User
  58. Netuserdel (NULL, lpszUserName);
  59. _end_:
  60. if (NULL! = Ppassword)
  61. {
  62. delete[] Ppassword;
  63. Ppassword = NULL;
  64. }
  65. if (NULL! = pusername)
  66. {
  67. delete[] Pusername;
  68. Pusername = NULL;
  69. }
  70. return bRet;
  71. }
  72. Test code
  73. #include "stdafx.h"
  74. #include <Windows.h>
  75. #include <lm.h>
  76. #include <strsafe.h>
  77. #pragma comment (lib, "Netapi32.lib")
  78. int _tmain (int argc, _tchar* argv[])
  79. {
  80. Executeasuser (l "ABC", l "Hello", l"F:\\11.exe", NULL);
  81. return 0;
  82. }
http://blog.csdn.net/visualeleven/article/details/7640475

This creates a new account that can be used to openprocesstoken+createrestrictedtoken the privileges of the current process's token for CreateProcessAsUser

A simple launch of an external application with user privileges (dynamically adding users using the NetUserAdd function and the user_info_1 struct, and then starting the program with CREATEPROCESSWITHLOGONW)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.