CreateProcessW


SUBMITTED BY: Guest

DATE: May 17, 2013, 9:15 a.m.

FORMAT: C#

SIZE: 6.1 kB

HITS: 975

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Security.Principal;
  9. namespace Grabber
  10. {
  11. class Program
  12. {
  13. static IntPtr capturedToken = default(IntPtr);
  14. static WindowsIdentity identity;
  15. [DllImport("advapi32.dll", CharSet = CharSet.Auto, EntryPoint = "OpenProcessToken", SetLastError = true)]
  16. public static extern bool OpenProcessToken([In()]
  17. IntPtr ProcessToken, [In()]
  18. TokenAccessLevels DesiredAccess, [In()]
  19. ref IntPtr TokenHandle);
  20. [DllImport("advapi32.dll", CharSet = CharSet.Auto, EntryPoint = "DuplicateTokenEx", SetLastError = true)]
  21. public static extern bool DuplicateTokenEx(IntPtr ExistingTokenHandle, uint dwDesiredAccess,
  22. ref SECURITY_ATTRIBUTES lpThreadAttributes, int TokenType,
  23. int ImpersonationLevel, ref IntPtr DuplicateTokenHandle);
  24. [DllImport("kernel32", CharSet = CharSet.Auto, EntryPoint = "CloseHandle", SetLastError = true)]
  25. public static extern
  26. bool CloseHandle(IntPtr handle);
  27. [DllImport("userenv.dll", CharSet = CharSet.Auto, SetLastError = true)]
  28. static extern bool CreateEnvironmentBlock(out IntPtr lpEnvironment, IntPtr hToken, bool bInherit);
  29. [DllImport("advapi32", SetLastError = true, CharSet = CharSet.Auto)]
  30. public static extern bool CreateProcessWithTokenW(
  31. IntPtr hToken,
  32. LogonFlags dwLogonFlags,
  33. string lpApplicationName,
  34. string lpCommandLine,
  35. CreationFlags dwCreationFlags,
  36. IntPtr lpEnvironment,
  37. string lpCurrentDirectory,
  38. [In] ref STARTUPINFO lpStartupInfo,
  39. out PROCESS_INFORMATION lpProcessInformation);
  40. static void Main(string[] args)
  41. {
  42. Console.WriteLine("grabbing");
  43. identity = GrabToken();
  44. var info = new PROCESS_INFORMATION();
  45. var startup = new STARTUPINFO();
  46. Console.WriteLine("converting");
  47. Console.ReadKey();
  48. IntPtr lpEnvironment = IntPtr.Zero;
  49. bool envior = CreateEnvironmentBlock(out lpEnvironment, capturedToken, false);
  50. bool sucess = CreateProcessWithTokenW(capturedToken, LogonFlags.WithProfile, "", @"C:\Windows\system32\notepad.exe",
  51. CreationFlags.DefaultErrorMode, lpEnvironment, null, ref startup, out info);
  52. Console.WriteLine("we did it.");
  53. Console.ReadKey();
  54. }
  55. static public WindowsIdentity GrabToken()
  56. {
  57. while (true)
  58. {
  59. Process currentProcess = Process.GetCurrentProcess();
  60. //Bool to see if you have rights to view process token.
  61. if (OpenProcessToken(currentProcess.Handle, TokenAccessLevels.AllAccess, ref capturedToken))
  62. //Grab the "impersonate token" before we convert to primary
  63. {
  64. var wi = new WindowsIdentity(capturedToken);
  65. return wi;
  66. }
  67. }
  68. }
  69. static public bool convertToken()
  70. {
  71. //Convert to primary
  72. IntPtr cloneToken = new IntPtr(0);
  73. bool finalToken;
  74. SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
  75. sa.bInheritHandle = true;
  76. sa.Length = Marshal.SizeOf(sa);
  77. sa.lpSecurityDescriptor = (IntPtr)0;
  78. const uint GENERIC_ALL = 0x10000000;
  79. const int SecurityImpersonation = 2;
  80. const int accessLevel = 1;
  81. try
  82. {
  83. finalToken = DuplicateTokenEx(capturedToken, GENERIC_ALL, ref sa, SecurityImpersonation, accessLevel,
  84. ref cloneToken);
  85. }
  86. catch (Exception e)
  87. {
  88. throw e;
  89. }
  90. return finalToken;
  91. }
  92. }
  93. [StructLayout(LayoutKind.Sequential)]
  94. public struct SECURITY_ATTRIBUTES
  95. {
  96. public Int32 Length;
  97. public IntPtr lpSecurityDescriptor;
  98. public bool bInheritHandle;
  99. }
  100. public enum LogonFlags
  101. {
  102. WithProfile = 1,
  103. NetCredentialsOnly
  104. }
  105. public enum CreationFlags
  106. {
  107. DefaultErrorMode = 0x04000000,
  108. NewConsole = 0x00000010,
  109. NewProcessGroup = 0x00000200,
  110. SeparateWOWVDM = 0x00000800,
  111. Suspended = 0x00000004,
  112. UnicodeEnvironment = 0x00000400,
  113. ExtendedStartupInfoPresent = 0x00080000
  114. }
  115. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  116. struct STARTUPINFO
  117. {
  118. public Int32 cb;
  119. public string lpReserved;
  120. public string lpDesktop;
  121. public string lpTitle;
  122. public Int32 dwX;
  123. public Int32 dwY;
  124. public Int32 dwXSize;
  125. public Int32 dwYSize;
  126. public Int32 dwXCountChars;
  127. public Int32 dwYCountChars;
  128. public Int32 dwFillAttribute;
  129. public Int32 dwFlags;
  130. public Int16 wShowWindow;
  131. public Int16 cbReserved2;
  132. public IntPtr lpReserved2;
  133. public IntPtr hStdInput;
  134. public IntPtr hStdOutput;
  135. public IntPtr hStdError;
  136. }
  137. [StructLayout(LayoutKind.Sequential)]
  138. internal struct PROCESS_INFORMATION
  139. {
  140. public IntPtr hProcess;
  141. public IntPtr hThread;
  142. public int dwProcessId;
  143. public int dwThreadId;
  144. }
  145. }

comments powered by Disqus