Skip to content

Instantly share code, notes, and snippets.

@shelld0n
Created February 1, 2020 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shelld0n/807975737d0726b455deed924acb7de3 to your computer and use it in GitHub Desktop.
Save shelld0n/807975737d0726b455deed924acb7de3 to your computer and use it in GitHub Desktop.
CreateProcessWithTokenW_code
// Duplicate token and spawn a new cmd.exe process
myAPI.SECURITY_IMPERSONATION_LEVEL seImpersonateLevel = myAPI.SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation;
myAPI.TOKEN_TYPE tokenType = myAPI.TOKEN_TYPE.TokenPrimary;
IntPtr pNewToken = new IntPtr();
myAPI.SECURITY_ATTRIBUTES sec_att = new myAPI.SECURITY_ATTRIBUTES();
if (!myAPI.DuplicateTokenEx(tokenHandle, myAPI.TOKEN_ALL_ACCESS, ref sec_att, seImpersonateLevel, tokenType, out pNewToken)) { Console.WriteLine("Can't Adjust access Token"); Environment.Exit(2); };
myAPI.STARTUPINFO si = new myAPI.STARTUPINFO();
myAPI.PROCESS_INFORMATION pi;
bool ret;
ret = myAPI.CreateProcessWithTokenW(pNewToken, myAPI.LogonFlags.NetCredentialsOnly, "C:\\Windows\\System32\\cmd.exe", null, myAPI.CreationFlags.NewConsole, IntPtr.Zero, null, ref si, out pi);
Console.WriteLine(ret);
// End the routine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment